* dwarf2read.c (struct comp_unit_head): Fix coding style.
[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    2004
4    Free Software Foundation, Inc.
5
6    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7    Inc.  with support from Florida State University (under contract
8    with the Ada Joint Program Office), and Silicon Graphics, Inc.
9    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11    support in dwarfread.c
12
13    This file is part of GDB.
14
15    This program is free software; you can redistribute it and/or modify
16    it under the terms of the GNU General Public License as published by
17    the Free Software Foundation; either version 2 of the License, or (at
18    your option) any later version.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 59 Temple Place - Suite 330,
28    Boston, MA 02111-1307, USA.  */
29
30 #include "defs.h"
31 #include "bfd.h"
32 #include "symtab.h"
33 #include "gdbtypes.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 #include "hashtab.h"
48 #include "command.h"
49 #include "gdbcmd.h"
50
51 #include <fcntl.h>
52 #include "gdb_string.h"
53 #include "gdb_assert.h"
54 #include <sys/types.h>
55
56 /* A note on memory usage for this file.
57    
58    At the present time, this code reads the debug info sections into
59    the objfile's objfile_obstack.  A definite improvement for startup
60    time, on platforms which do not emit relocations for debug
61    sections, would be to use mmap instead.  The object's complete
62    debug information is loaded into memory, partly to simplify
63    absolute DIE references.
64
65    Whether using obstacks or mmap, the sections should remain loaded
66    until the objfile is released, and pointers into the section data
67    can be used for any other data associated to the objfile (symbol
68    names, type names, location expressions to name a few).  */
69
70 #ifndef DWARF2_REG_TO_REGNUM
71 #define DWARF2_REG_TO_REGNUM(REG) (REG)
72 #endif
73
74 #if 0
75 /* .debug_info header for a compilation unit
76    Because of alignment constraints, this structure has padding and cannot
77    be mapped directly onto the beginning of the .debug_info section.  */
78 typedef struct comp_unit_header
79   {
80     unsigned int length;        /* length of the .debug_info
81                                    contribution */
82     unsigned short version;     /* version number -- 2 for DWARF
83                                    version 2 */
84     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
85     unsigned char addr_size;    /* byte size of an address -- 4 */
86   }
87 _COMP_UNIT_HEADER;
88 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
89 #endif
90
91 /* .debug_pubnames header
92    Because of alignment constraints, this structure has padding and cannot
93    be mapped directly onto the beginning of the .debug_info section.  */
94 typedef struct pubnames_header
95   {
96     unsigned int length;        /* length of the .debug_pubnames
97                                    contribution  */
98     unsigned char version;      /* version number -- 2 for DWARF
99                                    version 2 */
100     unsigned int info_offset;   /* offset into .debug_info section */
101     unsigned int info_size;     /* byte size of .debug_info section
102                                    portion */
103   }
104 _PUBNAMES_HEADER;
105 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
106
107 /* .debug_pubnames header
108    Because of alignment constraints, this structure has padding and cannot
109    be mapped directly onto the beginning of the .debug_info section.  */
110 typedef struct aranges_header
111   {
112     unsigned int length;        /* byte len of the .debug_aranges
113                                    contribution */
114     unsigned short version;     /* version number -- 2 for DWARF
115                                    version 2 */
116     unsigned int info_offset;   /* offset into .debug_info section */
117     unsigned char addr_size;    /* byte size of an address */
118     unsigned char seg_size;     /* byte size of segment descriptor */
119   }
120 _ARANGES_HEADER;
121 #define _ACTUAL_ARANGES_HEADER_SIZE 12
122
123 /* .debug_line statement program prologue
124    Because of alignment constraints, this structure has padding and cannot
125    be mapped directly onto the beginning of the .debug_info section.  */
126 typedef struct statement_prologue
127   {
128     unsigned int total_length;  /* byte length of the statement
129                                    information */
130     unsigned short version;     /* version number -- 2 for DWARF
131                                    version 2 */
132     unsigned int prologue_length;       /* # bytes between prologue &
133                                            stmt program */
134     unsigned char minimum_instruction_length;   /* byte size of
135                                                    smallest instr */
136     unsigned char default_is_stmt;      /* initial value of is_stmt
137                                            register */
138     char line_base;
139     unsigned char line_range;
140     unsigned char opcode_base;  /* number assigned to first special
141                                    opcode */
142     unsigned char *standard_opcode_lengths;
143   }
144 _STATEMENT_PROLOGUE;
145
146 static const struct objfile_data *dwarf2_objfile_data_key;
147
148 struct dwarf2_per_objfile
149 {
150   /* Sizes of debugging sections.  */
151   unsigned int info_size;
152   unsigned int abbrev_size;
153   unsigned int line_size;
154   unsigned int pubnames_size;
155   unsigned int aranges_size;
156   unsigned int loc_size;
157   unsigned int macinfo_size;
158   unsigned int str_size;
159   unsigned int ranges_size;
160   unsigned int frame_size;
161   unsigned int eh_frame_size;
162
163   /* Loaded data from the sections.  */
164   char *info_buffer;
165   char *abbrev_buffer;
166   char *line_buffer;
167   char *str_buffer;
168   char *macinfo_buffer;
169   char *ranges_buffer;
170   char *loc_buffer;
171
172   /* A list of all the compilation units.  This is used to locate
173      the target compilation unit of a particular reference.  */
174   struct dwarf2_per_cu_data **all_comp_units;
175
176   /* The number of compilation units in ALL_COMP_UNITS.  */
177   int n_comp_units;
178
179   /* A chain of compilation units that are currently read in, so that
180      they can be freed later.  */
181   struct dwarf2_per_cu_data *read_in_chain;
182 };
183
184 static struct dwarf2_per_objfile *dwarf2_per_objfile;
185
186 static asection *dwarf_info_section;
187 static asection *dwarf_abbrev_section;
188 static asection *dwarf_line_section;
189 static asection *dwarf_pubnames_section;
190 static asection *dwarf_aranges_section;
191 static asection *dwarf_loc_section;
192 static asection *dwarf_macinfo_section;
193 static asection *dwarf_str_section;
194 static asection *dwarf_ranges_section;
195 asection *dwarf_frame_section;
196 asection *dwarf_eh_frame_section;
197
198 /* names of the debugging sections */
199
200 #define INFO_SECTION     ".debug_info"
201 #define ABBREV_SECTION   ".debug_abbrev"
202 #define LINE_SECTION     ".debug_line"
203 #define PUBNAMES_SECTION ".debug_pubnames"
204 #define ARANGES_SECTION  ".debug_aranges"
205 #define LOC_SECTION      ".debug_loc"
206 #define MACINFO_SECTION  ".debug_macinfo"
207 #define STR_SECTION      ".debug_str"
208 #define RANGES_SECTION   ".debug_ranges"
209 #define FRAME_SECTION    ".debug_frame"
210 #define EH_FRAME_SECTION ".eh_frame"
211
212 /* local data types */
213
214 /* We hold several abbreviation tables in memory at the same time. */
215 #ifndef ABBREV_HASH_SIZE
216 #define ABBREV_HASH_SIZE 121
217 #endif
218
219 /* The data in a compilation unit header, after target2host
220    translation, looks like this.  */
221 struct comp_unit_head
222 {
223   unsigned long length;
224   short version;
225   unsigned int abbrev_offset;
226   unsigned char addr_size;
227   unsigned char signed_addr_p;
228
229   /* Size of file offsets; either 4 or 8.  */
230   unsigned int offset_size;
231
232   /* Size of the length field; either 4 or 12.  */
233   unsigned int initial_length_size;
234
235   /* Offset to the first byte of this compilation unit header in the
236      .debug_info section, for resolving relative reference dies.  */
237   unsigned int offset;
238
239   /* Pointer to this compilation unit header in the .debug_info
240      section.  */
241   char *cu_head_ptr;
242
243   /* Pointer to the first die of this compilation unit.  This will be
244      the first byte following the compilation unit header.  */
245   char *first_die_ptr;
246
247   /* Pointer to the next compilation unit header in the program.  */
248   struct comp_unit_head *next;
249
250   /* Base address of this compilation unit.  */
251   CORE_ADDR base_address;
252
253   /* Non-zero if base_address has been set.  */
254   int base_known;
255 };
256
257 /* Fixed size for the DIE hash table.  */
258 #ifndef REF_HASH_SIZE
259 #define REF_HASH_SIZE 1021
260 #endif
261
262 /* Internal state when decoding a particular compilation unit.  */
263 struct dwarf2_cu
264 {
265   /* The objfile containing this compilation unit.  */
266   struct objfile *objfile;
267
268   /* The header of the compilation unit.
269
270      FIXME drow/2003-11-10: Some of the things from the comp_unit_head
271      should logically be moved to the dwarf2_cu structure.  */
272   struct comp_unit_head header;
273
274   struct function_range *first_fn, *last_fn, *cached_fn;
275
276   /* The language we are debugging.  */
277   enum language language;
278   const struct language_defn *language_defn;
279
280   const char *producer;
281
282   /* The generic symbol table building routines have separate lists for
283      file scope symbols and all all other scopes (local scopes).  So
284      we need to select the right one to pass to add_symbol_to_list().
285      We do it by keeping a pointer to the correct list in list_in_scope.
286
287      FIXME: The original dwarf code just treated the file scope as the
288      first local scope, and all other local scopes as nested local
289      scopes, and worked fine.  Check to see if we really need to
290      distinguish these in buildsym.c.  */
291   struct pending **list_in_scope;
292
293   /* Maintain an array of referenced fundamental types for the current
294      compilation unit being read.  For DWARF version 1, we have to construct
295      the fundamental types on the fly, since no information about the
296      fundamental types is supplied.  Each such fundamental type is created by
297      calling a language dependent routine to create the type, and then a
298      pointer to that type is then placed in the array at the index specified
299      by it's FT_<TYPENAME> value.  The array has a fixed size set by the
300      FT_NUM_MEMBERS compile time constant, which is the number of predefined
301      fundamental types gdb knows how to construct.  */
302   struct type *ftypes[FT_NUM_MEMBERS];  /* Fundamental types */
303
304   /* DWARF abbreviation table associated with this compilation unit.  */
305   struct abbrev_info **dwarf2_abbrevs;
306
307   /* Storage for the abbrev table.  */
308   struct obstack abbrev_obstack;
309
310   /* Hash table holding all the loaded partial DIEs.  */
311   htab_t partial_dies;
312
313   /* Storage for things with the same lifetime as this read-in compilation
314      unit, including partial DIEs.  */
315   struct obstack comp_unit_obstack;
316
317   /* When multiple dwarf2_cu structures are living in memory, this field
318      chains them all together, so that they can be released efficiently.
319      We will probably also want a generation counter so that most-recently-used
320      compilation units are cached...  */
321   struct dwarf2_per_cu_data *read_in_chain;
322
323   /* Backchain to our per_cu entry if the tree has been built.  */
324   struct dwarf2_per_cu_data *per_cu;
325
326   /* How many compilation units ago was this CU last referenced?  */
327   int last_used;
328
329   /* A hash table of die offsets for following references.  */
330   struct die_info *die_ref_table[REF_HASH_SIZE];
331
332   /* Full DIEs if read in.  */
333   struct die_info *dies;
334
335   /* A set of pointers to dwarf2_per_cu_data objects for compilation
336      units referenced by this one.  Only set during full symbol processing;
337      partial symbol tables do not have dependencies.  */
338   htab_t dependencies;
339
340   /* Mark used when releasing cached dies.  */
341   unsigned int mark : 1;
342
343   /* This flag will be set if this compilation unit might include
344      inter-compilation-unit references.  */
345   unsigned int has_form_ref_addr : 1;
346
347   /* This flag will be set if this compilation unit includes any
348      DW_TAG_namespace DIEs.  If we know that there are explicit
349      DIEs for namespaces, we don't need to try to infer them
350      from mangled names.  */
351   unsigned int has_namespace_info : 1;
352 };
353
354 /* Persistent data held for a compilation unit, even when not
355    processing it.  We put a pointer to this structure in the
356    read_symtab_private field of the psymtab.  If we encounter
357    inter-compilation-unit references, we also maintain a sorted
358    list of all compilation units.  */
359
360 struct dwarf2_per_cu_data
361 {
362   /* The start offset and length of this compilation unit.  2**31-1
363      bytes should suffice to store the length of any compilation unit
364      - if it doesn't, GDB will fall over anyway.  */
365   unsigned long offset;
366   unsigned long length : 31;
367
368   /* Flag indicating this compilation unit will be read in before
369      any of the current compilation units are processed.  */
370   unsigned long queued : 1;
371
372   /* Set iff currently read in.  */
373   struct dwarf2_cu *cu;
374
375   /* If full symbols for this CU have been read in, then this field
376      holds a map of DIE offsets to types.  It isn't always possible
377      to reconstruct this information later, so we have to preserve
378      it.  */
379   htab_t type_hash;
380
381   /* The partial symbol table associated with this compilation unit.  */
382   struct partial_symtab *psymtab;
383 };
384
385 /* The line number information for a compilation unit (found in the
386    .debug_line section) begins with a "statement program header",
387    which contains the following information.  */
388 struct line_header
389 {
390   unsigned int total_length;
391   unsigned short version;
392   unsigned int header_length;
393   unsigned char minimum_instruction_length;
394   unsigned char default_is_stmt;
395   int line_base;
396   unsigned char line_range;
397   unsigned char opcode_base;
398
399   /* standard_opcode_lengths[i] is the number of operands for the
400      standard opcode whose value is i.  This means that
401      standard_opcode_lengths[0] is unused, and the last meaningful
402      element is standard_opcode_lengths[opcode_base - 1].  */
403   unsigned char *standard_opcode_lengths;
404
405   /* The include_directories table.  NOTE!  These strings are not
406      allocated with xmalloc; instead, they are pointers into
407      debug_line_buffer.  If you try to free them, `free' will get
408      indigestion.  */
409   unsigned int num_include_dirs, include_dirs_size;
410   char **include_dirs;
411
412   /* The file_names table.  NOTE!  These strings are not allocated
413      with xmalloc; instead, they are pointers into debug_line_buffer.
414      Don't try to free them directly.  */
415   unsigned int num_file_names, file_names_size;
416   struct file_entry
417   {
418     char *name;
419     unsigned int dir_index;
420     unsigned int mod_time;
421     unsigned int length;
422     int included_p; /* Non-zero if referenced by the Line Number Program.  */
423   } *file_names;
424
425   /* The start and end of the statement program following this
426      header.  These point into dwarf2_per_objfile->line_buffer.  */
427   char *statement_program_start, *statement_program_end;
428 };
429
430 /* When we construct a partial symbol table entry we only
431    need this much information. */
432 struct partial_die_info
433   {
434     /* Offset of this DIE.  */
435     unsigned int offset;
436
437     /* DWARF-2 tag for this DIE.  */
438     ENUM_BITFIELD(dwarf_tag) tag : 16;
439
440     /* Language code associated with this DIE.  This is only used
441        for the compilation unit DIE.  */
442     unsigned int language : 8;
443
444     /* Assorted flags describing the data found in this DIE.  */
445     unsigned int has_children : 1;
446     unsigned int is_external : 1;
447     unsigned int is_declaration : 1;
448     unsigned int has_type : 1;
449     unsigned int has_specification : 1;
450     unsigned int has_stmt_list : 1;
451     unsigned int has_pc_info : 1;
452
453     /* Flag set if the SCOPE field of this structure has been
454        computed.  */
455     unsigned int scope_set : 1;
456
457     /* The name of this DIE.  Normally the value of DW_AT_name, but
458        sometimes DW_TAG_MIPS_linkage_name or a string computed in some
459        other fashion.  */
460     char *name;
461     char *dirname;
462
463     /* The scope to prepend to our children.  This is generally
464        allocated on the comp_unit_obstack, so will disappear
465        when this compilation unit leaves the cache.  */
466     char *scope;
467
468     /* The location description associated with this DIE, if any.  */
469     struct dwarf_block *locdesc;
470
471     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
472     CORE_ADDR lowpc;
473     CORE_ADDR highpc;
474
475     /* Pointer into the info_buffer pointing at the target of
476        DW_AT_sibling, if any.  */
477     char *sibling;
478
479     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
480        DW_AT_specification (or DW_AT_abstract_origin or
481        DW_AT_extension).  */
482     unsigned int spec_offset;
483
484     /* If HAS_STMT_LIST, the offset of the Line Number Information data.  */
485     unsigned int line_offset;
486
487     /* Pointers to this DIE's parent, first child, and next sibling,
488        if any.  */
489     struct partial_die_info *die_parent, *die_child, *die_sibling;
490   };
491
492 /* This data structure holds the information of an abbrev. */
493 struct abbrev_info
494   {
495     unsigned int number;        /* number identifying abbrev */
496     enum dwarf_tag tag;         /* dwarf tag */
497     unsigned short has_children;                /* boolean */
498     unsigned short num_attrs;   /* number of attributes */
499     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
500     struct abbrev_info *next;   /* next in chain */
501   };
502
503 struct attr_abbrev
504   {
505     enum dwarf_attribute name;
506     enum dwarf_form form;
507   };
508
509 /* This data structure holds a complete die structure. */
510 struct die_info
511   {
512     enum dwarf_tag tag;         /* Tag indicating type of die */
513     unsigned int abbrev;        /* Abbrev number */
514     unsigned int offset;        /* Offset in .debug_info section */
515     unsigned int num_attrs;     /* Number of attributes */
516     struct attribute *attrs;    /* An array of attributes */
517     struct die_info *next_ref;  /* Next die in ref hash table */
518
519     /* The dies in a compilation unit form an n-ary tree.  PARENT
520        points to this die's parent; CHILD points to the first child of
521        this node; and all the children of a given node are chained
522        together via their SIBLING fields, terminated by a die whose
523        tag is zero.  */
524     struct die_info *child;     /* Its first child, if any.  */
525     struct die_info *sibling;   /* Its next sibling, if any.  */
526     struct die_info *parent;    /* Its parent, if any.  */
527
528     struct type *type;          /* Cached type information */
529   };
530
531 /* Attributes have a name and a value */
532 struct attribute
533   {
534     enum dwarf_attribute name;
535     enum dwarf_form form;
536     union
537       {
538         char *str;
539         struct dwarf_block *blk;
540         unsigned long unsnd;
541         long int snd;
542         CORE_ADDR addr;
543       }
544     u;
545   };
546
547 struct function_range
548 {
549   const char *name;
550   CORE_ADDR lowpc, highpc;
551   int seen_line;
552   struct function_range *next;
553 };
554
555 /* Get at parts of an attribute structure */
556
557 #define DW_STRING(attr)    ((attr)->u.str)
558 #define DW_UNSND(attr)     ((attr)->u.unsnd)
559 #define DW_BLOCK(attr)     ((attr)->u.blk)
560 #define DW_SND(attr)       ((attr)->u.snd)
561 #define DW_ADDR(attr)      ((attr)->u.addr)
562
563 /* Blocks are a bunch of untyped bytes. */
564 struct dwarf_block
565   {
566     unsigned int size;
567     char *data;
568   };
569
570 #ifndef ATTR_ALLOC_CHUNK
571 #define ATTR_ALLOC_CHUNK 4
572 #endif
573
574 /* Allocate fields for structs, unions and enums in this size.  */
575 #ifndef DW_FIELD_ALLOC_CHUNK
576 #define DW_FIELD_ALLOC_CHUNK 4
577 #endif
578
579 /* A zeroed version of a partial die for initialization purposes.  */
580 static struct partial_die_info zeroed_partial_die;
581
582 /* FIXME: decode_locdesc sets these variables to describe the location
583    to the caller.  These ought to be a structure or something.   If
584    none of the flags are set, the object lives at the address returned
585    by decode_locdesc.  */
586
587 static int isreg;               /* Object lives in register.
588                                    decode_locdesc's return value is
589                                    the register number.  */
590
591 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
592    but this would require a corresponding change in unpack_field_as_long
593    and friends.  */
594 static int bits_per_byte = 8;
595
596 /* The routines that read and process dies for a C struct or C++ class
597    pass lists of data member fields and lists of member function fields
598    in an instance of a field_info structure, as defined below.  */
599 struct field_info
600   {
601     /* List of data member and baseclasses fields. */
602     struct nextfield
603       {
604         struct nextfield *next;
605         int accessibility;
606         int virtuality;
607         struct field field;
608       }
609      *fields;
610
611     /* Number of fields.  */
612     int nfields;
613
614     /* Number of baseclasses.  */
615     int nbaseclasses;
616
617     /* Set if the accesibility of one of the fields is not public.  */
618     int non_public_fields;
619
620     /* Member function fields array, entries are allocated in the order they
621        are encountered in the object file.  */
622     struct nextfnfield
623       {
624         struct nextfnfield *next;
625         struct fn_field fnfield;
626       }
627      *fnfields;
628
629     /* Member function fieldlist array, contains name of possibly overloaded
630        member function, number of overloaded member functions and a pointer
631        to the head of the member function field chain.  */
632     struct fnfieldlist
633       {
634         char *name;
635         int length;
636         struct nextfnfield *head;
637       }
638      *fnfieldlists;
639
640     /* Number of entries in the fnfieldlists array.  */
641     int nfnfields;
642   };
643
644 /* One item on the queue of compilation units to read in full symbols
645    for.  */
646 struct dwarf2_queue_item
647 {
648   struct dwarf2_per_cu_data *per_cu;
649   struct dwarf2_queue_item *next;
650 };
651
652 /* The current queue.  */
653 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
654
655 /* Loaded secondary compilation units are kept in memory until they
656    have not been referenced for the processing of this many
657    compilation units.  Set this to zero to disable caching.  Cache
658    sizes of up to at least twenty will improve startup time for
659    typical inter-CU-reference binaries, at an obvious memory cost.  */
660 static int dwarf2_max_cache_age = 5;
661
662 /* Various complaints about symbol reading that don't abort the process */
663
664 static void
665 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
666 {
667   complaint (&symfile_complaints,
668              "statement list doesn't fit in .debug_line section");
669 }
670
671 static void
672 dwarf2_complex_location_expr_complaint (void)
673 {
674   complaint (&symfile_complaints, "location expression too complex");
675 }
676
677 static void
678 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
679                                               int arg3)
680 {
681   complaint (&symfile_complaints,
682              "const value length mismatch for '%s', got %d, expected %d", arg1,
683              arg2, arg3);
684 }
685
686 static void
687 dwarf2_macros_too_long_complaint (void)
688 {
689   complaint (&symfile_complaints,
690              "macro info runs off end of `.debug_macinfo' section");
691 }
692
693 static void
694 dwarf2_macro_malformed_definition_complaint (const char *arg1)
695 {
696   complaint (&symfile_complaints,
697              "macro debug info contains a malformed macro definition:\n`%s'",
698              arg1);
699 }
700
701 static void
702 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
703 {
704   complaint (&symfile_complaints,
705              "invalid attribute class or form for '%s' in '%s'", arg1, arg2);
706 }
707
708 /* local function prototypes */
709
710 static void dwarf2_locate_sections (bfd *, asection *, void *);
711
712 #if 0
713 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
714 #endif
715
716 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
717                                            struct objfile *);
718
719 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
720                                            struct partial_die_info *,
721                                            struct partial_symtab *);
722
723 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
724
725 static void scan_partial_symbols (struct partial_die_info *,
726                                   CORE_ADDR *, CORE_ADDR *,
727                                   struct dwarf2_cu *);
728
729 static void add_partial_symbol (struct partial_die_info *,
730                                 struct dwarf2_cu *);
731
732 static int pdi_needs_namespace (enum dwarf_tag tag);
733
734 static void add_partial_namespace (struct partial_die_info *pdi,
735                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
736                                    struct dwarf2_cu *cu);
737
738 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
739                                      struct dwarf2_cu *cu);
740
741 static char *locate_pdi_sibling (struct partial_die_info *orig_pdi,
742                                  char *info_ptr,
743                                  bfd *abfd,
744                                  struct dwarf2_cu *cu);
745
746 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
747
748 static void psymtab_to_symtab_1 (struct partial_symtab *);
749
750 char *dwarf2_read_section (struct objfile *, asection *);
751
752 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
753
754 static void dwarf2_free_abbrev_table (void *);
755
756 static struct abbrev_info *peek_die_abbrev (char *, int *, struct dwarf2_cu *);
757
758 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
759                                                  struct dwarf2_cu *);
760
761 static struct partial_die_info *load_partial_dies (bfd *, char *, int,
762                                                    struct dwarf2_cu *);
763
764 static char *read_partial_die (struct partial_die_info *,
765                                struct abbrev_info *abbrev, unsigned int,
766                                bfd *, char *, struct dwarf2_cu *);
767
768 static struct partial_die_info *find_partial_die (unsigned long,
769                                                   struct dwarf2_cu *);
770
771 static void fixup_partial_die (struct partial_die_info *,
772                                struct dwarf2_cu *);
773
774 static char *read_full_die (struct die_info **, bfd *, char *,
775                             struct dwarf2_cu *, int *);
776
777 static char *read_attribute (struct attribute *, struct attr_abbrev *,
778                              bfd *, char *, struct dwarf2_cu *);
779
780 static char *read_attribute_value (struct attribute *, unsigned,
781                              bfd *, char *, struct dwarf2_cu *);
782
783 static unsigned int read_1_byte (bfd *, char *);
784
785 static int read_1_signed_byte (bfd *, char *);
786
787 static unsigned int read_2_bytes (bfd *, char *);
788
789 static unsigned int read_4_bytes (bfd *, char *);
790
791 static unsigned long read_8_bytes (bfd *, char *);
792
793 static CORE_ADDR read_address (bfd *, char *ptr, struct dwarf2_cu *,
794                                int *bytes_read);
795
796 static LONGEST read_initial_length (bfd *, char *,
797                                     struct comp_unit_head *, int *bytes_read);
798
799 static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
800                             int *bytes_read);
801
802 static char *read_n_bytes (bfd *, char *, unsigned int);
803
804 static char *read_string (bfd *, char *, unsigned int *);
805
806 static char *read_indirect_string (bfd *, char *, const struct comp_unit_head *,
807                                    unsigned int *);
808
809 static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
810
811 static long read_signed_leb128 (bfd *, char *, unsigned int *);
812
813 static char *skip_leb128 (bfd *, char *);
814
815 static void set_cu_language (unsigned int, struct dwarf2_cu *);
816
817 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
818                                       struct dwarf2_cu *);
819
820 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
821                                struct dwarf2_cu *cu);
822
823 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
824
825 static struct die_info *die_specification (struct die_info *die,
826                                            struct dwarf2_cu *);
827
828 static void free_line_header (struct line_header *lh);
829
830 static void add_file_name (struct line_header *, char *, unsigned int,
831                            unsigned int, unsigned int);
832
833 static struct line_header *(dwarf_decode_line_header
834                             (unsigned int offset,
835                              bfd *abfd, struct dwarf2_cu *cu));
836
837 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
838                                 struct dwarf2_cu *, struct partial_symtab *);
839
840 static void dwarf2_start_subfile (char *, char *);
841
842 static struct symbol *new_symbol (struct die_info *, struct type *,
843                                   struct dwarf2_cu *);
844
845 static void dwarf2_const_value (struct attribute *, struct symbol *,
846                                 struct dwarf2_cu *);
847
848 static void dwarf2_const_value_data (struct attribute *attr,
849                                      struct symbol *sym,
850                                      int bits);
851
852 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
853
854 static struct type *die_containing_type (struct die_info *,
855                                          struct dwarf2_cu *);
856
857 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
858
859 static void read_type_die (struct die_info *, struct dwarf2_cu *);
860
861 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
862
863 static char *typename_concat (struct obstack *, const char *prefix, const char *suffix,
864                               struct dwarf2_cu *);
865
866 static void read_typedef (struct die_info *, struct dwarf2_cu *);
867
868 static void read_base_type (struct die_info *, struct dwarf2_cu *);
869
870 static void read_subrange_type (struct die_info *die, struct dwarf2_cu *cu);
871
872 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
873
874 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
875
876 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
877
878 static int dwarf2_get_pc_bounds (struct die_info *,
879                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
880
881 static void get_scope_pc_bounds (struct die_info *,
882                                  CORE_ADDR *, CORE_ADDR *,
883                                  struct dwarf2_cu *);
884
885 static void dwarf2_add_field (struct field_info *, struct die_info *,
886                               struct dwarf2_cu *);
887
888 static void dwarf2_attach_fields_to_type (struct field_info *,
889                                           struct type *, struct dwarf2_cu *);
890
891 static void dwarf2_add_member_fn (struct field_info *,
892                                   struct die_info *, struct type *,
893                                   struct dwarf2_cu *);
894
895 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
896                                              struct type *, struct dwarf2_cu *);
897
898 static void read_structure_type (struct die_info *, struct dwarf2_cu *);
899
900 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
901
902 static char *determine_class_name (struct die_info *die, struct dwarf2_cu *cu);
903
904 static void read_common_block (struct die_info *, struct dwarf2_cu *);
905
906 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
907
908 static const char *namespace_name (struct die_info *die,
909                                    int *is_anonymous, struct dwarf2_cu *);
910
911 static void read_enumeration_type (struct die_info *, struct dwarf2_cu *);
912
913 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
914
915 static struct type *dwarf_base_type (int, int, struct dwarf2_cu *);
916
917 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
918
919 static void read_array_type (struct die_info *, struct dwarf2_cu *);
920
921 static enum dwarf_array_dim_ordering read_array_order (struct die_info *, 
922                                                        struct dwarf2_cu *);
923
924 static void read_tag_pointer_type (struct die_info *, struct dwarf2_cu *);
925
926 static void read_tag_ptr_to_member_type (struct die_info *,
927                                          struct dwarf2_cu *);
928
929 static void read_tag_reference_type (struct die_info *, struct dwarf2_cu *);
930
931 static void read_tag_const_type (struct die_info *, struct dwarf2_cu *);
932
933 static void read_tag_volatile_type (struct die_info *, struct dwarf2_cu *);
934
935 static void read_tag_string_type (struct die_info *, struct dwarf2_cu *);
936
937 static void read_subroutine_type (struct die_info *, struct dwarf2_cu *);
938
939 static struct die_info *read_comp_unit (char *, bfd *, struct dwarf2_cu *);
940
941 static struct die_info *read_die_and_children (char *info_ptr, bfd *abfd,
942                                                struct dwarf2_cu *,
943                                                char **new_info_ptr,
944                                                struct die_info *parent);
945
946 static struct die_info *read_die_and_siblings (char *info_ptr, bfd *abfd,
947                                                struct dwarf2_cu *,
948                                                char **new_info_ptr,
949                                                struct die_info *parent);
950
951 static void free_die_list (struct die_info *);
952
953 static void process_die (struct die_info *, struct dwarf2_cu *);
954
955 static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
956
957 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
958
959 static struct die_info *dwarf2_extension (struct die_info *die,
960                                           struct dwarf2_cu *);
961
962 static char *dwarf_tag_name (unsigned int);
963
964 static char *dwarf_attr_name (unsigned int);
965
966 static char *dwarf_form_name (unsigned int);
967
968 static char *dwarf_stack_op_name (unsigned int);
969
970 static char *dwarf_bool_name (unsigned int);
971
972 static char *dwarf_type_encoding_name (unsigned int);
973
974 #if 0
975 static char *dwarf_cfi_name (unsigned int);
976
977 struct die_info *copy_die (struct die_info *);
978 #endif
979
980 static struct die_info *sibling_die (struct die_info *);
981
982 static void dump_die (struct die_info *);
983
984 static void dump_die_list (struct die_info *);
985
986 static void store_in_ref_table (unsigned int, struct die_info *,
987                                 struct dwarf2_cu *);
988
989 static unsigned int dwarf2_get_ref_die_offset (struct attribute *,
990                                                struct dwarf2_cu *);
991
992 static int dwarf2_get_attr_constant_value (struct attribute *, int);
993
994 static struct die_info *follow_die_ref (struct die_info *,
995                                         struct attribute *,
996                                         struct dwarf2_cu *);
997
998 static struct type *dwarf2_fundamental_type (struct objfile *, int,
999                                              struct dwarf2_cu *);
1000
1001 /* memory allocation interface */
1002
1003 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1004
1005 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1006
1007 static struct die_info *dwarf_alloc_die (void);
1008
1009 static void initialize_cu_func_list (struct dwarf2_cu *);
1010
1011 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1012                                  struct dwarf2_cu *);
1013
1014 static void dwarf_decode_macros (struct line_header *, unsigned int,
1015                                  char *, bfd *, struct dwarf2_cu *);
1016
1017 static int attr_form_is_block (struct attribute *);
1018
1019 static void
1020 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
1021                              struct dwarf2_cu *cu);
1022
1023 static char *skip_one_die (char *info_ptr, struct abbrev_info *abbrev,
1024                            struct dwarf2_cu *cu);
1025
1026 static void free_stack_comp_unit (void *);
1027
1028 static void *hashtab_obstack_allocate (void *data, size_t size, size_t count);
1029
1030 static void dummy_obstack_deallocate (void *object, void *data);
1031
1032 static hashval_t partial_die_hash (const void *item);
1033
1034 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1035
1036 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1037   (unsigned long offset, struct objfile *objfile);
1038
1039 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1040   (unsigned long offset, struct objfile *objfile);
1041
1042 static void free_one_comp_unit (void *);
1043
1044 static void free_cached_comp_units (void *);
1045
1046 static void age_cached_comp_units (void);
1047
1048 static void free_one_cached_comp_unit (void *);
1049
1050 static void set_die_type (struct die_info *, struct type *,
1051                           struct dwarf2_cu *);
1052
1053 static void reset_die_and_siblings_types (struct die_info *,
1054                                           struct dwarf2_cu *);
1055
1056 static void create_all_comp_units (struct objfile *);
1057
1058 static struct dwarf2_cu *load_full_comp_unit (struct dwarf2_per_cu_data *);
1059
1060 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1061
1062 static void dwarf2_add_dependence (struct dwarf2_cu *,
1063                                    struct dwarf2_per_cu_data *);
1064
1065 static void dwarf2_mark (struct dwarf2_cu *);
1066
1067 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1068
1069 /* Try to locate the sections we need for DWARF 2 debugging
1070    information and return true if we have enough to do something.  */
1071
1072 int
1073 dwarf2_has_info (struct objfile *objfile)
1074 {
1075   struct dwarf2_per_objfile *data;
1076
1077   /* Initialize per-objfile state.  */
1078   data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1079   memset (data, 0, sizeof (*data));
1080   set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1081   dwarf2_per_objfile = data;
1082
1083   dwarf_info_section = 0;
1084   dwarf_abbrev_section = 0;
1085   dwarf_line_section = 0;
1086   dwarf_str_section = 0;
1087   dwarf_macinfo_section = 0;
1088   dwarf_frame_section = 0;
1089   dwarf_eh_frame_section = 0;
1090   dwarf_ranges_section = 0;
1091   dwarf_loc_section = 0;
1092   
1093   bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1094   return (dwarf_info_section != NULL && dwarf_abbrev_section != NULL);
1095 }
1096
1097 /* This function is mapped across the sections and remembers the
1098    offset and size of each of the debugging sections we are interested
1099    in.  */
1100
1101 static void
1102 dwarf2_locate_sections (bfd *ignore_abfd, asection *sectp, void *ignore_ptr)
1103 {
1104   if (strcmp (sectp->name, INFO_SECTION) == 0)
1105     {
1106       dwarf2_per_objfile->info_size = bfd_get_section_size (sectp);
1107       dwarf_info_section = sectp;
1108     }
1109   else if (strcmp (sectp->name, ABBREV_SECTION) == 0)
1110     {
1111       dwarf2_per_objfile->abbrev_size = bfd_get_section_size (sectp);
1112       dwarf_abbrev_section = sectp;
1113     }
1114   else if (strcmp (sectp->name, LINE_SECTION) == 0)
1115     {
1116       dwarf2_per_objfile->line_size = bfd_get_section_size (sectp);
1117       dwarf_line_section = sectp;
1118     }
1119   else if (strcmp (sectp->name, PUBNAMES_SECTION) == 0)
1120     {
1121       dwarf2_per_objfile->pubnames_size = bfd_get_section_size (sectp);
1122       dwarf_pubnames_section = sectp;
1123     }
1124   else if (strcmp (sectp->name, ARANGES_SECTION) == 0)
1125     {
1126       dwarf2_per_objfile->aranges_size = bfd_get_section_size (sectp);
1127       dwarf_aranges_section = sectp;
1128     }
1129   else if (strcmp (sectp->name, LOC_SECTION) == 0)
1130     {
1131       dwarf2_per_objfile->loc_size = bfd_get_section_size (sectp);
1132       dwarf_loc_section = sectp;
1133     }
1134   else if (strcmp (sectp->name, MACINFO_SECTION) == 0)
1135     {
1136       dwarf2_per_objfile->macinfo_size = bfd_get_section_size (sectp);
1137       dwarf_macinfo_section = sectp;
1138     }
1139   else if (strcmp (sectp->name, STR_SECTION) == 0)
1140     {
1141       dwarf2_per_objfile->str_size = bfd_get_section_size (sectp);
1142       dwarf_str_section = sectp;
1143     }
1144   else if (strcmp (sectp->name, FRAME_SECTION) == 0)
1145     {
1146       dwarf2_per_objfile->frame_size = bfd_get_section_size (sectp);
1147       dwarf_frame_section = sectp;
1148     }
1149   else if (strcmp (sectp->name, EH_FRAME_SECTION) == 0)
1150     {
1151       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1152       if (aflag & SEC_HAS_CONTENTS)
1153         {
1154           dwarf2_per_objfile->eh_frame_size = bfd_get_section_size (sectp);
1155           dwarf_eh_frame_section = sectp;
1156         }
1157     }
1158   else if (strcmp (sectp->name, RANGES_SECTION) == 0)
1159     {
1160       dwarf2_per_objfile->ranges_size = bfd_get_section_size (sectp);
1161       dwarf_ranges_section = sectp;
1162     }
1163 }
1164
1165 /* Build a partial symbol table.  */
1166
1167 void
1168 dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
1169 {
1170   /* We definitely need the .debug_info and .debug_abbrev sections */
1171
1172   dwarf2_per_objfile->info_buffer = dwarf2_read_section (objfile, dwarf_info_section);
1173   dwarf2_per_objfile->abbrev_buffer = dwarf2_read_section (objfile, dwarf_abbrev_section);
1174
1175   if (dwarf_line_section)
1176     dwarf2_per_objfile->line_buffer = dwarf2_read_section (objfile, dwarf_line_section);
1177   else
1178     dwarf2_per_objfile->line_buffer = NULL;
1179
1180   if (dwarf_str_section)
1181     dwarf2_per_objfile->str_buffer = dwarf2_read_section (objfile, dwarf_str_section);
1182   else
1183     dwarf2_per_objfile->str_buffer = NULL;
1184
1185   if (dwarf_macinfo_section)
1186     dwarf2_per_objfile->macinfo_buffer = dwarf2_read_section (objfile,
1187                                                 dwarf_macinfo_section);
1188   else
1189     dwarf2_per_objfile->macinfo_buffer = NULL;
1190
1191   if (dwarf_ranges_section)
1192     dwarf2_per_objfile->ranges_buffer = dwarf2_read_section (objfile, dwarf_ranges_section);
1193   else
1194     dwarf2_per_objfile->ranges_buffer = NULL;
1195
1196   if (dwarf_loc_section)
1197     dwarf2_per_objfile->loc_buffer = dwarf2_read_section (objfile, dwarf_loc_section);
1198   else
1199     dwarf2_per_objfile->loc_buffer = NULL;
1200
1201   if (mainline
1202       || (objfile->global_psymbols.size == 0
1203           && objfile->static_psymbols.size == 0))
1204     {
1205       init_psymbol_list (objfile, 1024);
1206     }
1207
1208 #if 0
1209   if (dwarf_aranges_offset && dwarf_pubnames_offset)
1210     {
1211       /* Things are significantly easier if we have .debug_aranges and
1212          .debug_pubnames sections */
1213
1214       dwarf2_build_psymtabs_easy (objfile, mainline);
1215     }
1216   else
1217 #endif
1218     /* only test this case for now */
1219     {
1220       /* In this case we have to work a bit harder */
1221       dwarf2_build_psymtabs_hard (objfile, mainline);
1222     }
1223 }
1224
1225 #if 0
1226 /* Build the partial symbol table from the information in the
1227    .debug_pubnames and .debug_aranges sections.  */
1228
1229 static void
1230 dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
1231 {
1232   bfd *abfd = objfile->obfd;
1233   char *aranges_buffer, *pubnames_buffer;
1234   char *aranges_ptr, *pubnames_ptr;
1235   unsigned int entry_length, version, info_offset, info_size;
1236
1237   pubnames_buffer = dwarf2_read_section (objfile,
1238                                          dwarf_pubnames_section);
1239   pubnames_ptr = pubnames_buffer;
1240   while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames_size)
1241     {
1242       struct comp_unit_head cu_header;
1243       int bytes_read;
1244
1245       entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
1246                                          &bytes_read);
1247       pubnames_ptr += bytes_read;
1248       version = read_1_byte (abfd, pubnames_ptr);
1249       pubnames_ptr += 1;
1250       info_offset = read_4_bytes (abfd, pubnames_ptr);
1251       pubnames_ptr += 4;
1252       info_size = read_4_bytes (abfd, pubnames_ptr);
1253       pubnames_ptr += 4;
1254     }
1255
1256   aranges_buffer = dwarf2_read_section (objfile,
1257                                         dwarf_aranges_section);
1258
1259 }
1260 #endif
1261
1262 /* Read in the comp unit header information from the debug_info at
1263    info_ptr. */
1264
1265 static char *
1266 read_comp_unit_head (struct comp_unit_head *cu_header,
1267                      char *info_ptr, bfd *abfd)
1268 {
1269   int signed_addr;
1270   int bytes_read;
1271   cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
1272                                            &bytes_read);
1273   info_ptr += bytes_read;
1274   cu_header->version = read_2_bytes (abfd, info_ptr);
1275   info_ptr += 2;
1276   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1277                                           &bytes_read);
1278   info_ptr += bytes_read;
1279   cu_header->addr_size = read_1_byte (abfd, info_ptr);
1280   info_ptr += 1;
1281   signed_addr = bfd_get_sign_extend_vma (abfd);
1282   if (signed_addr < 0)
1283     internal_error (__FILE__, __LINE__,
1284                     "read_comp_unit_head: dwarf from non elf file");
1285   cu_header->signed_addr_p = signed_addr;
1286   return info_ptr;
1287 }
1288
1289 static char *
1290 partial_read_comp_unit_head (struct comp_unit_head *header, char *info_ptr,
1291                              bfd *abfd)
1292 {
1293   char *beg_of_comp_unit = info_ptr;
1294
1295   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1296
1297   if (header->version != 2)
1298     error ("Dwarf Error: wrong version in compilation unit header "
1299            "(is %d, should be %d) [in module %s]", header->version,
1300            2, bfd_get_filename (abfd));
1301
1302   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev_size)
1303     error ("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1304            "(offset 0x%lx + 6) [in module %s]",
1305            (long) header->abbrev_offset,
1306            (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1307            bfd_get_filename (abfd));
1308
1309   if (beg_of_comp_unit + header->length + header->initial_length_size
1310       > dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
1311     error ("Dwarf Error: bad length (0x%lx) in compilation unit header "
1312            "(offset 0x%lx + 0) [in module %s]",
1313            (long) header->length,
1314            (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1315            bfd_get_filename (abfd));
1316
1317   return info_ptr;
1318 }
1319
1320 /* Allocate a new partial symtab for file named NAME and mark this new
1321    partial symtab as being an include of PST.  */
1322
1323 static void
1324 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1325                                struct objfile *objfile)
1326 {
1327   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1328
1329   subpst->section_offsets = pst->section_offsets;
1330   subpst->textlow = 0;
1331   subpst->texthigh = 0;
1332
1333   subpst->dependencies = (struct partial_symtab **)
1334     obstack_alloc (&objfile->objfile_obstack,
1335                    sizeof (struct partial_symtab *));
1336   subpst->dependencies[0] = pst;
1337   subpst->number_of_dependencies = 1;
1338
1339   subpst->globals_offset = 0;
1340   subpst->n_global_syms = 0;
1341   subpst->statics_offset = 0;
1342   subpst->n_static_syms = 0;
1343   subpst->symtab = NULL;
1344   subpst->read_symtab = pst->read_symtab;
1345   subpst->readin = 0;
1346
1347   /* No private part is necessary for include psymtabs.  This property
1348      can be used to differentiate between such include psymtabs and
1349      the regular ones.  */
1350   subpst->read_symtab_private = NULL;
1351 }
1352
1353 /* Read the Line Number Program data and extract the list of files
1354    included by the source file represented by PST.  Build an include
1355    partial symtab for each of these included files.
1356    
1357    This procedure assumes that there *is* a Line Number Program in
1358    the given CU.  Callers should check that PDI->HAS_STMT_LIST is set
1359    before calling this procedure.  */
1360
1361 static void
1362 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1363                                struct partial_die_info *pdi,
1364                                struct partial_symtab *pst)
1365 {
1366   struct objfile *objfile = cu->objfile;
1367   bfd *abfd = objfile->obfd;
1368   struct line_header *lh;
1369
1370   lh = dwarf_decode_line_header (pdi->line_offset, abfd, cu);
1371   if (lh == NULL)
1372     return;  /* No linetable, so no includes.  */
1373
1374   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1375
1376   free_line_header (lh);
1377 }
1378
1379
1380 /* Build the partial symbol table by doing a quick pass through the
1381    .debug_info and .debug_abbrev sections.  */
1382
1383 static void
1384 dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
1385 {
1386   /* Instead of reading this into a big buffer, we should probably use
1387      mmap()  on architectures that support it. (FIXME) */
1388   bfd *abfd = objfile->obfd;
1389   char *info_ptr;
1390   char *beg_of_comp_unit;
1391   struct partial_die_info comp_unit_die;
1392   struct partial_symtab *pst;
1393   struct cleanup *back_to;
1394   CORE_ADDR lowpc, highpc, baseaddr;
1395
1396   info_ptr = dwarf2_per_objfile->info_buffer;
1397
1398   /* Any cached compilation units will be linked by the per-objfile
1399      read_in_chain.  Make sure to free them when we're done.  */
1400   back_to = make_cleanup (free_cached_comp_units, NULL);
1401
1402   create_all_comp_units (objfile);
1403
1404   /* Since the objects we're extracting from .debug_info vary in
1405      length, only the individual functions to extract them (like
1406      read_comp_unit_head and load_partial_die) can really know whether
1407      the buffer is large enough to hold another complete object.
1408
1409      At the moment, they don't actually check that.  If .debug_info
1410      holds just one extra byte after the last compilation unit's dies,
1411      then read_comp_unit_head will happily read off the end of the
1412      buffer.  read_partial_die is similarly casual.  Those functions
1413      should be fixed.
1414
1415      For this loop condition, simply checking whether there's any data
1416      left at all should be sufficient.  */
1417   while (info_ptr < (dwarf2_per_objfile->info_buffer
1418                      + dwarf2_per_objfile->info_size))
1419     {
1420       struct cleanup *back_to_inner;
1421       struct dwarf2_cu cu;
1422       struct abbrev_info *abbrev;
1423       unsigned int bytes_read;
1424       struct dwarf2_per_cu_data *this_cu;
1425
1426       beg_of_comp_unit = info_ptr;
1427
1428       memset (&cu, 0, sizeof (cu));
1429
1430       obstack_init (&cu.comp_unit_obstack);
1431
1432       back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
1433
1434       cu.objfile = objfile;
1435       info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr, abfd);
1436
1437       /* Complete the cu_header */
1438       cu.header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1439       cu.header.first_die_ptr = info_ptr;
1440       cu.header.cu_head_ptr = beg_of_comp_unit;
1441
1442       cu.list_in_scope = &file_symbols;
1443
1444       /* Read the abbrevs for this compilation unit into a table */
1445       dwarf2_read_abbrevs (abfd, &cu);
1446       make_cleanup (dwarf2_free_abbrev_table, &cu);
1447
1448       this_cu = dwarf2_find_comp_unit (cu.header.offset, objfile);
1449
1450       /* Read the compilation unit die */
1451       abbrev = peek_die_abbrev (info_ptr, &bytes_read, &cu);
1452       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1453                                    abfd, info_ptr, &cu);
1454
1455       /* Set the language we're debugging */
1456       set_cu_language (comp_unit_die.language, &cu);
1457
1458       /* Allocate a new partial symbol table structure */
1459       pst = start_psymtab_common (objfile, objfile->section_offsets,
1460                                   comp_unit_die.name ? comp_unit_die.name : "",
1461                                   comp_unit_die.lowpc,
1462                                   objfile->global_psymbols.next,
1463                                   objfile->static_psymbols.next);
1464
1465       if (comp_unit_die.dirname)
1466         pst->dirname = xstrdup (comp_unit_die.dirname);
1467
1468       pst->read_symtab_private = (char *) this_cu;
1469
1470       baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1471
1472       /* Store the function that reads in the rest of the symbol table */
1473       pst->read_symtab = dwarf2_psymtab_to_symtab;
1474
1475       /* If this compilation unit was already read in, free the
1476          cached copy in order to read it in again.  This is
1477          necessary because we skipped some symbols when we first
1478          read in the compilation unit (see load_partial_dies).
1479          This problem could be avoided, but the benefit is
1480          unclear.  */
1481       if (this_cu->cu != NULL)
1482         free_one_cached_comp_unit (this_cu->cu);
1483
1484       cu.per_cu = this_cu;
1485
1486       /* Note that this is a pointer to our stack frame, being
1487          added to a global data structure.  It will be cleaned up
1488          in free_stack_comp_unit when we finish with this
1489          compilation unit.  */
1490       this_cu->cu = &cu;
1491
1492       this_cu->psymtab = pst;
1493
1494       /* Check if comp unit has_children.
1495          If so, read the rest of the partial symbols from this comp unit.
1496          If not, there's no more debug_info for this comp unit. */
1497       if (comp_unit_die.has_children)
1498         {
1499           struct partial_die_info *first_die;
1500
1501           lowpc = ((CORE_ADDR) -1);
1502           highpc = ((CORE_ADDR) 0);
1503
1504           first_die = load_partial_dies (abfd, info_ptr, 1, &cu);
1505
1506           scan_partial_symbols (first_die, &lowpc, &highpc, &cu);
1507
1508           /* If we didn't find a lowpc, set it to highpc to avoid
1509              complaints from `maint check'.  */
1510           if (lowpc == ((CORE_ADDR) -1))
1511             lowpc = highpc;
1512
1513           /* If the compilation unit didn't have an explicit address range,
1514              then use the information extracted from its child dies.  */
1515           if (! comp_unit_die.has_pc_info)
1516             {
1517               comp_unit_die.lowpc = lowpc;
1518               comp_unit_die.highpc = highpc;
1519             }
1520         }
1521       pst->textlow = comp_unit_die.lowpc + baseaddr;
1522       pst->texthigh = comp_unit_die.highpc + baseaddr;
1523
1524       pst->n_global_syms = objfile->global_psymbols.next -
1525         (objfile->global_psymbols.list + pst->globals_offset);
1526       pst->n_static_syms = objfile->static_psymbols.next -
1527         (objfile->static_psymbols.list + pst->statics_offset);
1528       sort_pst_symbols (pst);
1529
1530       /* If there is already a psymtab or symtab for a file of this
1531          name, remove it. (If there is a symtab, more drastic things
1532          also happen.) This happens in VxWorks.  */
1533       free_named_symtabs (pst->filename);
1534
1535       if (comp_unit_die.has_stmt_list)
1536         {
1537           /* Get the list of files included in the current compilation unit,
1538              and build a psymtab for each of them.  */
1539           dwarf2_build_include_psymtabs (&cu, &comp_unit_die, pst);
1540         }
1541
1542       info_ptr = beg_of_comp_unit + cu.header.length
1543                                   + cu.header.initial_length_size;
1544
1545       do_cleanups (back_to_inner);
1546     }
1547   do_cleanups (back_to);
1548 }
1549
1550 /* Load the DIEs for a secondary CU into memory.  */
1551
1552 static void
1553 load_comp_unit (struct dwarf2_per_cu_data *this_cu, struct objfile *objfile)
1554 {
1555   bfd *abfd = objfile->obfd;
1556   char *info_ptr, *beg_of_comp_unit;
1557   struct partial_die_info comp_unit_die;
1558   struct dwarf2_cu *cu;
1559   struct abbrev_info *abbrev;
1560   unsigned int bytes_read;
1561   struct cleanup *back_to;
1562
1563   info_ptr = dwarf2_per_objfile->info_buffer + this_cu->offset;
1564   beg_of_comp_unit = info_ptr;
1565
1566   cu = xmalloc (sizeof (struct dwarf2_cu));
1567   memset (cu, 0, sizeof (struct dwarf2_cu));
1568
1569   obstack_init (&cu->comp_unit_obstack);
1570
1571   cu->objfile = objfile;
1572   info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr, abfd);
1573
1574   /* Complete the cu_header.  */
1575   cu->header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1576   cu->header.first_die_ptr = info_ptr;
1577   cu->header.cu_head_ptr = beg_of_comp_unit;
1578
1579   /* Read the abbrevs for this compilation unit into a table.  */
1580   dwarf2_read_abbrevs (abfd, cu);
1581   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
1582
1583   /* Read the compilation unit die.  */
1584   abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
1585   info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1586                                abfd, info_ptr, cu);
1587
1588   /* Set the language we're debugging.  */
1589   set_cu_language (comp_unit_die.language, cu);
1590
1591   /* Link this compilation unit into the compilation unit tree.  */
1592   this_cu->cu = cu;
1593   cu->per_cu = this_cu;
1594
1595   /* Check if comp unit has_children.
1596      If so, read the rest of the partial symbols from this comp unit.
1597      If not, there's no more debug_info for this comp unit. */
1598   if (comp_unit_die.has_children)
1599     load_partial_dies (abfd, info_ptr, 0, cu);
1600
1601   do_cleanups (back_to);
1602 }
1603
1604 /* Create a list of all compilation units in OBJFILE.  We do this only
1605    if an inter-comp-unit reference is found; presumably if there is one,
1606    there will be many, and one will occur early in the .debug_info section.
1607    So there's no point in building this list incrementally.  */
1608
1609 static void
1610 create_all_comp_units (struct objfile *objfile)
1611 {
1612   int n_allocated;
1613   int n_comp_units;
1614   struct dwarf2_per_cu_data **all_comp_units;
1615   char *info_ptr = dwarf2_per_objfile->info_buffer;
1616
1617   n_comp_units = 0;
1618   n_allocated = 10;
1619   all_comp_units = xmalloc (n_allocated
1620                             * sizeof (struct dwarf2_per_cu_data *));
1621   
1622   while (info_ptr < dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
1623     {
1624       struct comp_unit_head cu_header;
1625       char *beg_of_comp_unit;
1626       struct dwarf2_per_cu_data *this_cu;
1627       unsigned long offset;
1628       int bytes_read;
1629
1630       offset = info_ptr - dwarf2_per_objfile->info_buffer;
1631
1632       /* Read just enough information to find out where the next
1633          compilation unit is.  */
1634       cu_header.length = read_initial_length (objfile->obfd, info_ptr,
1635                                               &cu_header, &bytes_read);
1636
1637       /* Save the compilation unit for later lookup.  */
1638       this_cu = obstack_alloc (&objfile->objfile_obstack,
1639                                sizeof (struct dwarf2_per_cu_data));
1640       memset (this_cu, 0, sizeof (*this_cu));
1641       this_cu->offset = offset;
1642       this_cu->length = cu_header.length + cu_header.initial_length_size;
1643
1644       if (n_comp_units == n_allocated)
1645         {
1646           n_allocated *= 2;
1647           all_comp_units = xrealloc (all_comp_units,
1648                                      n_allocated
1649                                      * sizeof (struct dwarf2_per_cu_data *));
1650         }
1651       all_comp_units[n_comp_units++] = this_cu;
1652
1653       info_ptr = info_ptr + this_cu->length;
1654     }
1655
1656   dwarf2_per_objfile->all_comp_units
1657     = obstack_alloc (&objfile->objfile_obstack,
1658                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1659   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
1660           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1661   xfree (all_comp_units);
1662   dwarf2_per_objfile->n_comp_units = n_comp_units;
1663 }
1664
1665 /* Process all loaded DIEs for compilation unit CU, starting at FIRST_DIE.
1666    Also set *LOWPC and *HIGHPC to the lowest and highest PC values found
1667    in CU.  */
1668
1669 static void
1670 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
1671                       CORE_ADDR *highpc, struct dwarf2_cu *cu)
1672 {
1673   struct objfile *objfile = cu->objfile;
1674   bfd *abfd = objfile->obfd;
1675   struct partial_die_info *pdi;
1676
1677   /* Now, march along the PDI's, descending into ones which have
1678      interesting children but skipping the children of the other ones,
1679      until we reach the end of the compilation unit.  */
1680
1681   pdi = first_die;
1682
1683   while (pdi != NULL)
1684     {
1685       fixup_partial_die (pdi, cu);
1686
1687       /* Anonymous namespaces have no name but have interesting
1688          children, so we need to look at them.  Ditto for anonymous
1689          enums.  */
1690
1691       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
1692           || pdi->tag == DW_TAG_enumeration_type)
1693         {
1694           switch (pdi->tag)
1695             {
1696             case DW_TAG_subprogram:
1697               if (pdi->has_pc_info)
1698                 {
1699                   if (pdi->lowpc < *lowpc)
1700                     {
1701                       *lowpc = pdi->lowpc;
1702                     }
1703                   if (pdi->highpc > *highpc)
1704                     {
1705                       *highpc = pdi->highpc;
1706                     }
1707                   if (!pdi->is_declaration)
1708                     {
1709                       add_partial_symbol (pdi, cu);
1710                     }
1711                 }
1712               break;
1713             case DW_TAG_variable:
1714             case DW_TAG_typedef:
1715             case DW_TAG_union_type:
1716               if (!pdi->is_declaration)
1717                 {
1718                   add_partial_symbol (pdi, cu);
1719                 }
1720               break;
1721             case DW_TAG_class_type:
1722             case DW_TAG_structure_type:
1723               if (!pdi->is_declaration)
1724                 {
1725                   add_partial_symbol (pdi, cu);
1726                 }
1727               break;
1728             case DW_TAG_enumeration_type:
1729               if (!pdi->is_declaration)
1730                 add_partial_enumeration (pdi, cu);
1731               break;
1732             case DW_TAG_base_type:
1733             case DW_TAG_subrange_type:
1734               /* File scope base type definitions are added to the partial
1735                  symbol table.  */
1736               add_partial_symbol (pdi, cu);
1737               break;
1738             case DW_TAG_namespace:
1739               add_partial_namespace (pdi, lowpc, highpc, cu);
1740               break;
1741             default:
1742               break;
1743             }
1744         }
1745
1746       /* If the die has a sibling, skip to the sibling.  */
1747
1748       pdi = pdi->die_sibling;
1749     }
1750 }
1751
1752 /* Functions used to compute the fully scoped name of a partial DIE.
1753
1754    Normally, this is simple.  For C++, the parent DIE's fully scoped
1755    name is concatenated with "::" and the partial DIE's name.  For
1756    Java, the same thing occurs except that "." is used instead of "::".
1757    Enumerators are an exception; they use the scope of their parent
1758    enumeration type, i.e. the name of the enumeration type is not
1759    prepended to the enumerator.
1760
1761    There are two complexities.  One is DW_AT_specification; in this
1762    case "parent" means the parent of the target of the specification,
1763    instead of the direct parent of the DIE.  The other is compilers
1764    which do not emit DW_TAG_namespace; in this case we try to guess
1765    the fully qualified name of structure types from their members'
1766    linkage names.  This must be done using the DIE's children rather
1767    than the children of any DW_AT_specification target.  We only need
1768    to do this for structures at the top level, i.e. if the target of
1769    any DW_AT_specification (if any; otherwise the DIE itself) does not
1770    have a parent.  */
1771
1772 /* Compute the scope prefix associated with PDI's parent, in
1773    compilation unit CU.  The result will be allocated on CU's
1774    comp_unit_obstack, or a copy of the already allocated PDI->NAME
1775    field.  NULL is returned if no prefix is necessary.  */
1776 static char *
1777 partial_die_parent_scope (struct partial_die_info *pdi,
1778                           struct dwarf2_cu *cu)
1779 {
1780   char *grandparent_scope;
1781   struct partial_die_info *parent, *real_pdi;
1782
1783   /* We need to look at our parent DIE; if we have a DW_AT_specification,
1784      then this means the parent of the specification DIE.  */
1785
1786   real_pdi = pdi;
1787   while (real_pdi->has_specification)
1788     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
1789
1790   parent = real_pdi->die_parent;
1791   if (parent == NULL)
1792     return NULL;
1793
1794   if (parent->scope_set)
1795     return parent->scope;
1796
1797   fixup_partial_die (parent, cu);
1798
1799   grandparent_scope = partial_die_parent_scope (parent, cu);
1800
1801   if (parent->tag == DW_TAG_namespace
1802       || parent->tag == DW_TAG_structure_type
1803       || parent->tag == DW_TAG_class_type
1804       || parent->tag == DW_TAG_union_type)
1805     {
1806       if (grandparent_scope == NULL)
1807         parent->scope = parent->name;
1808       else
1809         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
1810                                          parent->name, cu);
1811     }
1812   else if (parent->tag == DW_TAG_enumeration_type)
1813     /* Enumerators should not get the name of the enumeration as a prefix.  */
1814     parent->scope = grandparent_scope;
1815   else
1816     {
1817       /* FIXME drow/2004-04-01: What should we be doing with
1818          function-local names?  For partial symbols, we should probably be
1819          ignoring them.  */
1820       complaint (&symfile_complaints,
1821                  "unhandled containing DIE tag %d for DIE at %d",
1822                  parent->tag, pdi->offset);
1823       parent->scope = grandparent_scope;
1824     }
1825
1826   parent->scope_set = 1;
1827   return parent->scope;
1828 }
1829
1830 /* Return the fully scoped name associated with PDI, from compilation unit
1831    CU.  The result will be allocated with malloc.  */
1832 static char *
1833 partial_die_full_name (struct partial_die_info *pdi,
1834                        struct dwarf2_cu *cu)
1835 {
1836   char *parent_scope;
1837
1838   parent_scope = partial_die_parent_scope (pdi, cu);
1839   if (parent_scope == NULL)
1840     return NULL;
1841   else
1842     return typename_concat (NULL, parent_scope, pdi->name, cu);
1843 }
1844
1845 static void
1846 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
1847 {
1848   struct objfile *objfile = cu->objfile;
1849   CORE_ADDR addr = 0;
1850   char *actual_name;
1851   const char *my_prefix;
1852   const struct partial_symbol *psym = NULL;
1853   CORE_ADDR baseaddr;
1854   int built_actual_name = 0;
1855
1856   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1857
1858   actual_name = NULL;
1859
1860   if (pdi_needs_namespace (pdi->tag))
1861     {
1862       actual_name = partial_die_full_name (pdi, cu);
1863       if (actual_name)
1864         built_actual_name = 1;
1865     }
1866
1867   if (actual_name == NULL)
1868     actual_name = pdi->name;
1869
1870   switch (pdi->tag)
1871     {
1872     case DW_TAG_subprogram:
1873       if (pdi->is_external)
1874         {
1875           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
1876              mst_text, objfile); */
1877           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1878                                       VAR_DOMAIN, LOC_BLOCK,
1879                                       &objfile->global_psymbols,
1880                                       0, pdi->lowpc + baseaddr,
1881                                       cu->language, objfile);
1882         }
1883       else
1884         {
1885           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
1886              mst_file_text, objfile); */
1887           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1888                                       VAR_DOMAIN, LOC_BLOCK,
1889                                       &objfile->static_psymbols,
1890                                       0, pdi->lowpc + baseaddr,
1891                                       cu->language, objfile);
1892         }
1893       break;
1894     case DW_TAG_variable:
1895       if (pdi->is_external)
1896         {
1897           /* Global Variable.
1898              Don't enter into the minimal symbol tables as there is
1899              a minimal symbol table entry from the ELF symbols already.
1900              Enter into partial symbol table if it has a location
1901              descriptor or a type.
1902              If the location descriptor is missing, new_symbol will create
1903              a LOC_UNRESOLVED symbol, the address of the variable will then
1904              be determined from the minimal symbol table whenever the variable
1905              is referenced.
1906              The address for the partial symbol table entry is not
1907              used by GDB, but it comes in handy for debugging partial symbol
1908              table building.  */
1909
1910           if (pdi->locdesc)
1911             addr = decode_locdesc (pdi->locdesc, cu);
1912           if (pdi->locdesc || pdi->has_type)
1913             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1914                                         VAR_DOMAIN, LOC_STATIC,
1915                                         &objfile->global_psymbols,
1916                                         0, addr + baseaddr,
1917                                         cu->language, objfile);
1918         }
1919       else
1920         {
1921           /* Static Variable. Skip symbols without location descriptors.  */
1922           if (pdi->locdesc == NULL)
1923             return;
1924           addr = decode_locdesc (pdi->locdesc, cu);
1925           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
1926              mst_file_data, objfile); */
1927           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1928                                       VAR_DOMAIN, LOC_STATIC,
1929                                       &objfile->static_psymbols,
1930                                       0, addr + baseaddr,
1931                                       cu->language, objfile);
1932         }
1933       break;
1934     case DW_TAG_typedef:
1935     case DW_TAG_base_type:
1936     case DW_TAG_subrange_type:
1937       add_psymbol_to_list (actual_name, strlen (actual_name),
1938                            VAR_DOMAIN, LOC_TYPEDEF,
1939                            &objfile->static_psymbols,
1940                            0, (CORE_ADDR) 0, cu->language, objfile);
1941       break;
1942     case DW_TAG_namespace:
1943       add_psymbol_to_list (actual_name, strlen (actual_name),
1944                            VAR_DOMAIN, LOC_TYPEDEF,
1945                            &objfile->global_psymbols,
1946                            0, (CORE_ADDR) 0, cu->language, objfile);
1947       break;
1948     case DW_TAG_class_type:
1949     case DW_TAG_structure_type:
1950     case DW_TAG_union_type:
1951     case DW_TAG_enumeration_type:
1952       /* Skip aggregate types without children, these are external
1953          references.  */
1954       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
1955          static vs. global.  */
1956       if (pdi->has_children == 0)
1957         return;
1958       add_psymbol_to_list (actual_name, strlen (actual_name),
1959                            STRUCT_DOMAIN, LOC_TYPEDEF,
1960                            (cu->language == language_cplus
1961                             || cu->language == language_java)
1962                            ? &objfile->global_psymbols
1963                            : &objfile->static_psymbols,
1964                            0, (CORE_ADDR) 0, cu->language, objfile);
1965
1966       if (cu->language == language_cplus
1967           || cu->language == language_java)
1968         {
1969           /* For C++ and Java, these implicitly act as typedefs as well. */
1970           add_psymbol_to_list (actual_name, strlen (actual_name),
1971                                VAR_DOMAIN, LOC_TYPEDEF,
1972                                &objfile->global_psymbols,
1973                                0, (CORE_ADDR) 0, cu->language, objfile);
1974         }
1975       break;
1976     case DW_TAG_enumerator:
1977       add_psymbol_to_list (actual_name, strlen (actual_name),
1978                            VAR_DOMAIN, LOC_CONST,
1979                            (cu->language == language_cplus
1980                             || cu->language == language_java)
1981                            ? &objfile->global_psymbols
1982                            : &objfile->static_psymbols,
1983                            0, (CORE_ADDR) 0, cu->language, objfile);
1984       break;
1985     default:
1986       break;
1987     }
1988
1989   /* Check to see if we should scan the name for possible namespace
1990      info.  Only do this if this is C++, if we don't have namespace
1991      debugging info in the file, if the psym is of an appropriate type
1992      (otherwise we'll have psym == NULL), and if we actually had a
1993      mangled name to begin with.  */
1994
1995   /* FIXME drow/2004-02-22: Why don't we do this for classes, i.e. the
1996      cases which do not set PSYM above?  */
1997
1998   if (cu->language == language_cplus
1999       && cu->has_namespace_info == 0
2000       && psym != NULL
2001       && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
2002     cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
2003                                          objfile);
2004
2005   if (built_actual_name)
2006     xfree (actual_name);
2007 }
2008
2009 /* Determine whether a die of type TAG living in a C++ class or
2010    namespace needs to have the name of the scope prepended to the
2011    name listed in the die.  */
2012
2013 static int
2014 pdi_needs_namespace (enum dwarf_tag tag)
2015 {
2016   switch (tag)
2017     {
2018     case DW_TAG_namespace:
2019     case DW_TAG_typedef:
2020     case DW_TAG_class_type:
2021     case DW_TAG_structure_type:
2022     case DW_TAG_union_type:
2023     case DW_TAG_enumeration_type:
2024     case DW_TAG_enumerator:
2025       return 1;
2026     default:
2027       return 0;
2028     }
2029 }
2030
2031 /* Read a partial die corresponding to a namespace; also, add a symbol
2032    corresponding to that namespace to the symbol table.  NAMESPACE is
2033    the name of the enclosing namespace.  */
2034
2035 static void
2036 add_partial_namespace (struct partial_die_info *pdi,
2037                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
2038                        struct dwarf2_cu *cu)
2039 {
2040   struct objfile *objfile = cu->objfile;
2041
2042   /* Add a symbol for the namespace.  */
2043
2044   add_partial_symbol (pdi, cu);
2045
2046   /* Now scan partial symbols in that namespace.  */
2047
2048   if (pdi->has_children)
2049     scan_partial_symbols (pdi->die_child, lowpc, highpc, cu);
2050 }
2051
2052 /* See if we can figure out if the class lives in a namespace.  We do
2053    this by looking for a member function; its demangled name will
2054    contain namespace info, if there is any.  */
2055
2056 static void
2057 guess_structure_name (struct partial_die_info *struct_pdi,
2058                       struct dwarf2_cu *cu)
2059 {
2060   if ((cu->language == language_cplus
2061        || cu->language == language_java)
2062       && cu->has_namespace_info == 0
2063       && struct_pdi->has_children)
2064     {
2065       /* NOTE: carlton/2003-10-07: Getting the info this way changes
2066          what template types look like, because the demangler
2067          frequently doesn't give the same name as the debug info.  We
2068          could fix this by only using the demangled name to get the
2069          prefix (but see comment in read_structure_type).  */
2070
2071       struct partial_die_info *child_pdi = struct_pdi->die_child;
2072       struct partial_die_info *real_pdi;
2073
2074       /* If this DIE (this DIE's specification, if any) has a parent, then
2075          we should not do this.  We'll prepend the parent's fully qualified
2076          name when we create the partial symbol.  */
2077
2078       real_pdi = struct_pdi;
2079       while (real_pdi->has_specification)
2080         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
2081
2082       if (real_pdi->die_parent != NULL)
2083         return;
2084
2085       while (child_pdi != NULL)
2086         {
2087           if (child_pdi->tag == DW_TAG_subprogram)
2088             {
2089               char *actual_class_name
2090                 = language_class_name_from_physname (cu->language_defn,
2091                                                      child_pdi->name);
2092               if (actual_class_name != NULL)
2093                 {
2094                   struct_pdi->name
2095                     = obsavestring (actual_class_name,
2096                                     strlen (actual_class_name),
2097                                     &cu->comp_unit_obstack);
2098                   xfree (actual_class_name);
2099                 }
2100               break;
2101             }
2102
2103           child_pdi = child_pdi->die_sibling;
2104         }
2105     }
2106 }
2107
2108 /* Read a partial die corresponding to an enumeration type.  */
2109
2110 static void
2111 add_partial_enumeration (struct partial_die_info *enum_pdi,
2112                          struct dwarf2_cu *cu)
2113 {
2114   struct objfile *objfile = cu->objfile;
2115   bfd *abfd = objfile->obfd;
2116   struct partial_die_info *pdi;
2117
2118   if (enum_pdi->name != NULL)
2119     add_partial_symbol (enum_pdi, cu);
2120
2121   pdi = enum_pdi->die_child;
2122   while (pdi)
2123     {
2124       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
2125         complaint (&symfile_complaints, "malformed enumerator DIE ignored");
2126       else
2127         add_partial_symbol (pdi, cu);
2128       pdi = pdi->die_sibling;
2129     }
2130 }
2131
2132 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2133    Return the corresponding abbrev, or NULL if the number is zero (indicating
2134    an empty DIE).  In either case *BYTES_READ will be set to the length of
2135    the initial number.  */
2136
2137 static struct abbrev_info *
2138 peek_die_abbrev (char *info_ptr, int *bytes_read, struct dwarf2_cu *cu)
2139 {
2140   bfd *abfd = cu->objfile->obfd;
2141   unsigned int abbrev_number;
2142   struct abbrev_info *abbrev;
2143
2144   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2145
2146   if (abbrev_number == 0)
2147     return NULL;
2148
2149   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2150   if (!abbrev)
2151     {
2152       error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
2153                       bfd_get_filename (abfd));
2154     }
2155
2156   return abbrev;
2157 }
2158
2159 /* Scan the debug information for CU starting at INFO_PTR.  Returns a
2160    pointer to the end of a series of DIEs, terminated by an empty
2161    DIE.  Any children of the skipped DIEs will also be skipped.  */
2162
2163 static char *
2164 skip_children (char *info_ptr, struct dwarf2_cu *cu)
2165 {
2166   struct abbrev_info *abbrev;
2167   unsigned int bytes_read;
2168
2169   while (1)
2170     {
2171       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2172       if (abbrev == NULL)
2173         return info_ptr + bytes_read;
2174       else
2175         info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
2176     }
2177 }
2178
2179 /* Scan the debug information for CU starting at INFO_PTR.  INFO_PTR
2180    should point just after the initial uleb128 of a DIE, and the
2181    abbrev corresponding to that skipped uleb128 should be passed in
2182    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
2183    children.  */
2184
2185 static char *
2186 skip_one_die (char *info_ptr, struct abbrev_info *abbrev,
2187               struct dwarf2_cu *cu)
2188 {
2189   unsigned int bytes_read;
2190   struct attribute attr;
2191   bfd *abfd = cu->objfile->obfd;
2192   unsigned int form, i;
2193
2194   for (i = 0; i < abbrev->num_attrs; i++)
2195     {
2196       /* The only abbrev we care about is DW_AT_sibling.  */
2197       if (abbrev->attrs[i].name == DW_AT_sibling)
2198         {
2199           read_attribute (&attr, &abbrev->attrs[i],
2200                           abfd, info_ptr, cu);
2201           if (attr.form == DW_FORM_ref_addr)
2202             complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
2203           else
2204             return dwarf2_per_objfile->info_buffer
2205               + dwarf2_get_ref_die_offset (&attr, cu);
2206         }
2207
2208       /* If it isn't DW_AT_sibling, skip this attribute.  */
2209       form = abbrev->attrs[i].form;
2210     skip_attribute:
2211       switch (form)
2212         {
2213         case DW_FORM_addr:
2214         case DW_FORM_ref_addr:
2215           info_ptr += cu->header.addr_size;
2216           break;
2217         case DW_FORM_data1:
2218         case DW_FORM_ref1:
2219         case DW_FORM_flag:
2220           info_ptr += 1;
2221           break;
2222         case DW_FORM_data2:
2223         case DW_FORM_ref2:
2224           info_ptr += 2;
2225           break;
2226         case DW_FORM_data4:
2227         case DW_FORM_ref4:
2228           info_ptr += 4;
2229           break;
2230         case DW_FORM_data8:
2231         case DW_FORM_ref8:
2232           info_ptr += 8;
2233           break;
2234         case DW_FORM_string:
2235           read_string (abfd, info_ptr, &bytes_read);
2236           info_ptr += bytes_read;
2237           break;
2238         case DW_FORM_strp:
2239           info_ptr += cu->header.offset_size;
2240           break;
2241         case DW_FORM_block:
2242           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2243           info_ptr += bytes_read;
2244           break;
2245         case DW_FORM_block1:
2246           info_ptr += 1 + read_1_byte (abfd, info_ptr);
2247           break;
2248         case DW_FORM_block2:
2249           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2250           break;
2251         case DW_FORM_block4:
2252           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2253           break;
2254         case DW_FORM_sdata:
2255         case DW_FORM_udata:
2256         case DW_FORM_ref_udata:
2257           info_ptr = skip_leb128 (abfd, info_ptr);
2258           break;
2259         case DW_FORM_indirect:
2260           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2261           info_ptr += bytes_read;
2262           /* We need to continue parsing from here, so just go back to
2263              the top.  */
2264           goto skip_attribute;
2265
2266         default:
2267           error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
2268                  dwarf_form_name (form),
2269                  bfd_get_filename (abfd));
2270         }
2271     }
2272
2273   if (abbrev->has_children)
2274     return skip_children (info_ptr, cu);
2275   else
2276     return info_ptr;
2277 }
2278
2279 /* Locate ORIG_PDI's sibling; INFO_PTR should point to the start of
2280    the next DIE after ORIG_PDI.  */
2281
2282 static char *
2283 locate_pdi_sibling (struct partial_die_info *orig_pdi, char *info_ptr,
2284                     bfd *abfd, struct dwarf2_cu *cu)
2285 {
2286   /* Do we know the sibling already?  */
2287
2288   if (orig_pdi->sibling)
2289     return orig_pdi->sibling;
2290
2291   /* Are there any children to deal with?  */
2292
2293   if (!orig_pdi->has_children)
2294     return info_ptr;
2295
2296   /* Skip the children the long way.  */
2297
2298   return skip_children (info_ptr, cu);
2299 }
2300
2301 /* Expand this partial symbol table into a full symbol table.  */
2302
2303 static void
2304 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
2305 {
2306   /* FIXME: This is barely more than a stub.  */
2307   if (pst != NULL)
2308     {
2309       if (pst->readin)
2310         {
2311           warning ("bug: psymtab for %s is already read in.", pst->filename);
2312         }
2313       else
2314         {
2315           if (info_verbose)
2316             {
2317               printf_filtered ("Reading in symbols for %s...", pst->filename);
2318               gdb_flush (gdb_stdout);
2319             }
2320
2321           /* Restore our global data.  */
2322           dwarf2_per_objfile = objfile_data (pst->objfile,
2323                                              dwarf2_objfile_data_key);
2324
2325           psymtab_to_symtab_1 (pst);
2326
2327           /* Finish up the debug error message.  */
2328           if (info_verbose)
2329             printf_filtered ("done.\n");
2330         }
2331     }
2332 }
2333
2334 /* Add PER_CU to the queue.  */
2335
2336 static void
2337 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
2338 {
2339   struct dwarf2_queue_item *item;
2340
2341   per_cu->queued = 1;
2342   item = xmalloc (sizeof (*item));
2343   item->per_cu = per_cu;
2344   item->next = NULL;
2345
2346   if (dwarf2_queue == NULL)
2347     dwarf2_queue = item;
2348   else
2349     dwarf2_queue_tail->next = item;
2350
2351   dwarf2_queue_tail = item;
2352 }
2353
2354 /* Process the queue.  */
2355
2356 static void
2357 process_queue (struct objfile *objfile)
2358 {
2359   struct dwarf2_queue_item *item, *next_item;
2360
2361   /* Initially, there is just one item on the queue.  Load its DIEs,
2362      and the DIEs of any other compilation units it requires,
2363      transitively.  */
2364
2365   for (item = dwarf2_queue; item != NULL; item = item->next)
2366     {
2367       /* Read in this compilation unit.  This may add new items to
2368          the end of the queue.  */
2369       load_full_comp_unit (item->per_cu);
2370
2371       item->per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
2372       dwarf2_per_objfile->read_in_chain = item->per_cu;
2373
2374       /* If this compilation unit has already had full symbols created,
2375          reset the TYPE fields in each DIE.  */
2376       if (item->per_cu->psymtab->readin)
2377         reset_die_and_siblings_types (item->per_cu->cu->dies,
2378                                       item->per_cu->cu);
2379     }
2380
2381   /* Now everything left on the queue needs to be read in.  Process
2382      them, one at a time, removing from the queue as we finish.  */
2383   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
2384     {
2385       if (!item->per_cu->psymtab->readin)
2386         process_full_comp_unit (item->per_cu);
2387
2388       item->per_cu->queued = 0;
2389       next_item = item->next;
2390       xfree (item);
2391     }
2392
2393   dwarf2_queue_tail = NULL;
2394 }
2395
2396 /* Free all allocated queue entries.  This function only releases anything if
2397    an error was thrown; if the queue was processed then it would have been
2398    freed as we went along.  */
2399
2400 static void
2401 dwarf2_release_queue (void *dummy)
2402 {
2403   struct dwarf2_queue_item *item, *last;
2404
2405   item = dwarf2_queue;
2406   while (item)
2407     {
2408       /* Anything still marked queued is likely to be in an
2409          inconsistent state, so discard it.  */
2410       if (item->per_cu->queued)
2411         {
2412           if (item->per_cu->cu != NULL)
2413             free_one_cached_comp_unit (item->per_cu->cu);
2414           item->per_cu->queued = 0;
2415         }
2416
2417       last = item;
2418       item = item->next;
2419       xfree (last);
2420     }
2421
2422   dwarf2_queue = dwarf2_queue_tail = NULL;
2423 }
2424
2425 /* Read in full symbols for PST, and anything it depends on.  */
2426
2427 static void
2428 psymtab_to_symtab_1 (struct partial_symtab *pst)
2429 {
2430   struct dwarf2_per_cu_data *per_cu;
2431   struct cleanup *back_to;
2432   int i;
2433
2434   for (i = 0; i < pst->number_of_dependencies; i++)
2435     if (!pst->dependencies[i]->readin)
2436       {
2437         /* Inform about additional files that need to be read in.  */
2438         if (info_verbose)
2439           {
2440             fputs_filtered (" ", gdb_stdout);
2441             wrap_here ("");
2442             fputs_filtered ("and ", gdb_stdout);
2443             wrap_here ("");
2444             printf_filtered ("%s...", pst->dependencies[i]->filename);
2445             wrap_here ("");     /* Flush output */
2446             gdb_flush (gdb_stdout);
2447           }
2448         psymtab_to_symtab_1 (pst->dependencies[i]);
2449       }
2450
2451   per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
2452
2453   if (per_cu == NULL)
2454     {
2455       /* It's an include file, no symbols to read for it.
2456          Everything is in the parent symtab.  */
2457       pst->readin = 1;
2458       return;
2459     }
2460
2461   back_to = make_cleanup (dwarf2_release_queue, NULL);
2462
2463   queue_comp_unit (per_cu);
2464
2465   process_queue (pst->objfile);
2466
2467   /* Age the cache, releasing compilation units that have not
2468      been used recently.  */
2469   age_cached_comp_units ();
2470
2471   do_cleanups (back_to);
2472 }
2473
2474 /* Load the DIEs associated with PST and PER_CU into memory.  */
2475
2476 static struct dwarf2_cu *
2477 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
2478 {
2479   struct partial_symtab *pst = per_cu->psymtab;
2480   bfd *abfd = pst->objfile->obfd;
2481   struct dwarf2_cu *cu;
2482   unsigned long offset;
2483   char *info_ptr;
2484   struct cleanup *back_to, *free_cu_cleanup;
2485   struct attribute *attr;
2486   CORE_ADDR baseaddr;
2487
2488   /* Set local variables from the partial symbol table info.  */
2489   offset = per_cu->offset;
2490
2491   info_ptr = dwarf2_per_objfile->info_buffer + offset;
2492
2493   cu = xmalloc (sizeof (struct dwarf2_cu));
2494   memset (cu, 0, sizeof (struct dwarf2_cu));
2495
2496   /* If an error occurs while loading, release our storage.  */
2497   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
2498
2499   cu->objfile = pst->objfile;
2500
2501   /* read in the comp_unit header  */
2502   info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
2503
2504   /* Read the abbrevs for this compilation unit  */
2505   dwarf2_read_abbrevs (abfd, cu);
2506   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
2507
2508   cu->header.offset = offset;
2509
2510   cu->per_cu = per_cu;
2511   per_cu->cu = cu;
2512
2513   /* We use this obstack for block values in dwarf_alloc_block.  */
2514   obstack_init (&cu->comp_unit_obstack);
2515
2516   cu->dies = read_comp_unit (info_ptr, abfd, cu);
2517
2518   /* We try not to read any attributes in this function, because not
2519      all objfiles needed for references have been loaded yet, and symbol
2520      table processing isn't initialized.  But we have to set the CU language,
2521      or we won't be able to build types correctly.  */
2522   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
2523   if (attr)
2524     set_cu_language (DW_UNSND (attr), cu);
2525   else
2526     set_cu_language (language_minimal, cu);
2527
2528   do_cleanups (back_to);
2529
2530   /* We've successfully allocated this compilation unit.  Let our caller
2531      clean it up when finished with it.  */
2532   discard_cleanups (free_cu_cleanup);
2533
2534   return cu;
2535 }
2536
2537 /* Generate full symbol information for PST and CU, whose DIEs have
2538    already been loaded into memory.  */
2539
2540 static void
2541 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
2542 {
2543   struct partial_symtab *pst = per_cu->psymtab;
2544   struct dwarf2_cu *cu = per_cu->cu;
2545   struct objfile *objfile = pst->objfile;
2546   bfd *abfd = objfile->obfd;
2547   CORE_ADDR lowpc, highpc;
2548   struct symtab *symtab;
2549   struct cleanup *back_to;
2550   struct attribute *attr;
2551   CORE_ADDR baseaddr;
2552
2553   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2554
2555   /* We're in the global namespace.  */
2556   processing_current_prefix = "";
2557
2558   buildsym_init ();
2559   back_to = make_cleanup (really_free_pendings, NULL);
2560
2561   cu->list_in_scope = &file_symbols;
2562
2563   /* Find the base address of the compilation unit for range lists and
2564      location lists.  It will normally be specified by DW_AT_low_pc.
2565      In DWARF-3 draft 4, the base address could be overridden by
2566      DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2567      compilation units with discontinuous ranges.  */
2568
2569   cu->header.base_known = 0;
2570   cu->header.base_address = 0;
2571
2572   attr = dwarf2_attr (cu->dies, DW_AT_entry_pc, cu);
2573   if (attr)
2574     {
2575       cu->header.base_address = DW_ADDR (attr);
2576       cu->header.base_known = 1;
2577     }
2578   else
2579     {
2580       attr = dwarf2_attr (cu->dies, DW_AT_low_pc, cu);
2581       if (attr)
2582         {
2583           cu->header.base_address = DW_ADDR (attr);
2584           cu->header.base_known = 1;
2585         }
2586     }
2587
2588   /* Do line number decoding in read_file_scope () */
2589   process_die (cu->dies, cu);
2590
2591   /* Some compilers don't define a DW_AT_high_pc attribute for the
2592      compilation unit.  If the DW_AT_high_pc is missing, synthesize
2593      it, by scanning the DIE's below the compilation unit.  */
2594   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
2595
2596   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
2597
2598   /* Set symtab language to language from DW_AT_language.
2599      If the compilation is from a C file generated by language preprocessors,
2600      do not set the language if it was already deduced by start_subfile.  */
2601   if (symtab != NULL
2602       && !(cu->language == language_c && symtab->language != language_c))
2603     {
2604       symtab->language = cu->language;
2605     }
2606   pst->symtab = symtab;
2607   pst->readin = 1;
2608
2609   do_cleanups (back_to);
2610 }
2611
2612 /* Process a die and its children.  */
2613
2614 static void
2615 process_die (struct die_info *die, struct dwarf2_cu *cu)
2616 {
2617   switch (die->tag)
2618     {
2619     case DW_TAG_padding:
2620       break;
2621     case DW_TAG_compile_unit:
2622       read_file_scope (die, cu);
2623       break;
2624     case DW_TAG_subprogram:
2625       read_subroutine_type (die, cu);
2626       read_func_scope (die, cu);
2627       break;
2628     case DW_TAG_inlined_subroutine:
2629       /* FIXME:  These are ignored for now.
2630          They could be used to set breakpoints on all inlined instances
2631          of a function and make GDB `next' properly over inlined functions.  */
2632       break;
2633     case DW_TAG_lexical_block:
2634     case DW_TAG_try_block:
2635     case DW_TAG_catch_block:
2636       read_lexical_block_scope (die, cu);
2637       break;
2638     case DW_TAG_class_type:
2639     case DW_TAG_structure_type:
2640     case DW_TAG_union_type:
2641       read_structure_type (die, cu);
2642       process_structure_scope (die, cu);
2643       break;
2644     case DW_TAG_enumeration_type:
2645       read_enumeration_type (die, cu);
2646       process_enumeration_scope (die, cu);
2647       break;
2648
2649     /* FIXME drow/2004-03-14: These initialize die->type, but do not create
2650        a symbol or process any children.  Therefore it doesn't do anything
2651        that won't be done on-demand by read_type_die.  */
2652     case DW_TAG_subroutine_type:
2653       read_subroutine_type (die, cu);
2654       break;
2655     case DW_TAG_array_type:
2656       read_array_type (die, cu);
2657       break;
2658     case DW_TAG_pointer_type:
2659       read_tag_pointer_type (die, cu);
2660       break;
2661     case DW_TAG_ptr_to_member_type:
2662       read_tag_ptr_to_member_type (die, cu);
2663       break;
2664     case DW_TAG_reference_type:
2665       read_tag_reference_type (die, cu);
2666       break;
2667     case DW_TAG_string_type:
2668       read_tag_string_type (die, cu);
2669       break;
2670     /* END FIXME */
2671
2672     case DW_TAG_base_type:
2673       read_base_type (die, cu);
2674       /* Add a typedef symbol for the type definition, if it has a
2675          DW_AT_name.  */
2676       new_symbol (die, die->type, cu);
2677       break;
2678     case DW_TAG_subrange_type:
2679       read_subrange_type (die, cu);
2680       /* Add a typedef symbol for the type definition, if it has a
2681          DW_AT_name.  */
2682       new_symbol (die, die->type, cu);
2683       break;
2684     case DW_TAG_common_block:
2685       read_common_block (die, cu);
2686       break;
2687     case DW_TAG_common_inclusion:
2688       break;
2689     case DW_TAG_namespace:
2690       processing_has_namespace_info = 1;
2691       read_namespace (die, cu);
2692       break;
2693     case DW_TAG_imported_declaration:
2694     case DW_TAG_imported_module:
2695       /* FIXME: carlton/2002-10-16: Eventually, we should use the
2696          information contained in these.  DW_TAG_imported_declaration
2697          dies shouldn't have children; DW_TAG_imported_module dies
2698          shouldn't in the C++ case, but conceivably could in the
2699          Fortran case, so we'll have to replace this gdb_assert if
2700          Fortran compilers start generating that info.  */
2701       processing_has_namespace_info = 1;
2702       gdb_assert (die->child == NULL);
2703       break;
2704     default:
2705       new_symbol (die, NULL, cu);
2706       break;
2707     }
2708 }
2709
2710 static void
2711 initialize_cu_func_list (struct dwarf2_cu *cu)
2712 {
2713   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
2714 }
2715
2716 static void
2717 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
2718 {
2719   struct objfile *objfile = cu->objfile;
2720   struct comp_unit_head *cu_header = &cu->header;
2721   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2722   CORE_ADDR lowpc = ((CORE_ADDR) -1);
2723   CORE_ADDR highpc = ((CORE_ADDR) 0);
2724   struct attribute *attr;
2725   char *name = "<unknown>";
2726   char *comp_dir = NULL;
2727   struct die_info *child_die;
2728   bfd *abfd = objfile->obfd;
2729   struct line_header *line_header = 0;
2730   CORE_ADDR baseaddr;
2731   
2732   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2733
2734   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
2735
2736   /* If we didn't find a lowpc, set it to highpc to avoid complaints
2737      from finish_block.  */
2738   if (lowpc == ((CORE_ADDR) -1))
2739     lowpc = highpc;
2740   lowpc += baseaddr;
2741   highpc += baseaddr;
2742
2743   attr = dwarf2_attr (die, DW_AT_name, cu);
2744   if (attr)
2745     {
2746       name = DW_STRING (attr);
2747     }
2748   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
2749   if (attr)
2750     {
2751       comp_dir = DW_STRING (attr);
2752       if (comp_dir)
2753         {
2754           /* Irix 6.2 native cc prepends <machine>.: to the compilation
2755              directory, get rid of it.  */
2756           char *cp = strchr (comp_dir, ':');
2757
2758           if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2759             comp_dir = cp + 1;
2760         }
2761     }
2762
2763   attr = dwarf2_attr (die, DW_AT_language, cu);
2764   if (attr)
2765     {
2766       set_cu_language (DW_UNSND (attr), cu);
2767     }
2768
2769   attr = dwarf2_attr (die, DW_AT_producer, cu);
2770   if (attr) 
2771     cu->producer = DW_STRING (attr);
2772   
2773   /* We assume that we're processing GCC output. */
2774   processing_gcc_compilation = 2;
2775 #if 0
2776   /* FIXME:Do something here.  */
2777   if (dip->at_producer != NULL)
2778     {
2779       handle_producer (dip->at_producer);
2780     }
2781 #endif
2782
2783   /* The compilation unit may be in a different language or objfile,
2784      zero out all remembered fundamental types.  */
2785   memset (cu->ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
2786
2787   start_symtab (name, comp_dir, lowpc);
2788   record_debugformat ("DWARF 2");
2789
2790   initialize_cu_func_list (cu);
2791
2792   /* Process all dies in compilation unit.  */
2793   if (die->child != NULL)
2794     {
2795       child_die = die->child;
2796       while (child_die && child_die->tag)
2797         {
2798           process_die (child_die, cu);
2799           child_die = sibling_die (child_die);
2800         }
2801     }
2802
2803   /* Decode line number information if present.  */
2804   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2805   if (attr)
2806     {
2807       unsigned int line_offset = DW_UNSND (attr);
2808       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
2809       if (line_header)
2810         {
2811           make_cleanup ((make_cleanup_ftype *) free_line_header,
2812                         (void *) line_header);
2813           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
2814         }
2815     }
2816
2817   /* Decode macro information, if present.  Dwarf 2 macro information
2818      refers to information in the line number info statement program
2819      header, so we can only read it if we've read the header
2820      successfully.  */
2821   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
2822   if (attr && line_header)
2823     {
2824       unsigned int macro_offset = DW_UNSND (attr);
2825       dwarf_decode_macros (line_header, macro_offset,
2826                            comp_dir, abfd, cu);
2827     }
2828   do_cleanups (back_to);
2829 }
2830
2831 static void
2832 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
2833                      struct dwarf2_cu *cu)
2834 {
2835   struct function_range *thisfn;
2836
2837   thisfn = (struct function_range *)
2838     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
2839   thisfn->name = name;
2840   thisfn->lowpc = lowpc;
2841   thisfn->highpc = highpc;
2842   thisfn->seen_line = 0;
2843   thisfn->next = NULL;
2844
2845   if (cu->last_fn == NULL)
2846       cu->first_fn = thisfn;
2847   else
2848       cu->last_fn->next = thisfn;
2849
2850   cu->last_fn = thisfn;
2851 }
2852
2853 static void
2854 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
2855 {
2856   struct objfile *objfile = cu->objfile;
2857   struct context_stack *new;
2858   CORE_ADDR lowpc;
2859   CORE_ADDR highpc;
2860   struct die_info *child_die;
2861   struct attribute *attr;
2862   char *name;
2863   const char *previous_prefix = processing_current_prefix;
2864   struct cleanup *back_to = NULL;
2865   CORE_ADDR baseaddr;
2866
2867   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2868
2869   name = dwarf2_linkage_name (die, cu);
2870
2871   /* Ignore functions with missing or empty names and functions with
2872      missing or invalid low and high pc attributes.  */
2873   if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2874     return;
2875
2876   if (cu->language == language_cplus
2877       || cu->language == language_java)
2878     {
2879       struct die_info *spec_die = die_specification (die, cu);
2880
2881       /* NOTE: carlton/2004-01-23: We have to be careful in the
2882          presence of DW_AT_specification.  For example, with GCC 3.4,
2883          given the code
2884
2885            namespace N {
2886              void foo() {
2887                // Definition of N::foo.
2888              }
2889            }
2890
2891          then we'll have a tree of DIEs like this:
2892
2893          1: DW_TAG_compile_unit
2894            2: DW_TAG_namespace        // N
2895              3: DW_TAG_subprogram     // declaration of N::foo
2896            4: DW_TAG_subprogram       // definition of N::foo
2897                 DW_AT_specification   // refers to die #3
2898
2899          Thus, when processing die #4, we have to pretend that we're
2900          in the context of its DW_AT_specification, namely the contex
2901          of die #3.  */
2902         
2903       if (spec_die != NULL)
2904         {
2905           char *specification_prefix = determine_prefix (spec_die, cu);
2906           processing_current_prefix = specification_prefix;
2907           back_to = make_cleanup (xfree, specification_prefix);
2908         }
2909     }
2910
2911   lowpc += baseaddr;
2912   highpc += baseaddr;
2913
2914   /* Record the function range for dwarf_decode_lines.  */
2915   add_to_cu_func_list (name, lowpc, highpc, cu);
2916
2917   new = push_context (0, lowpc);
2918   new->name = new_symbol (die, die->type, cu);
2919
2920   /* If there is a location expression for DW_AT_frame_base, record
2921      it.  */
2922   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
2923   if (attr)
2924     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
2925        expression is being recorded directly in the function's symbol
2926        and not in a separate frame-base object.  I guess this hack is
2927        to avoid adding some sort of frame-base adjunct/annex to the
2928        function's symbol :-(.  The problem with doing this is that it
2929        results in a function symbol with a location expression that
2930        has nothing to do with the location of the function, ouch!  The
2931        relationship should be: a function's symbol has-a frame base; a
2932        frame-base has-a location expression.  */
2933     dwarf2_symbol_mark_computed (attr, new->name, cu);
2934
2935   cu->list_in_scope = &local_symbols;
2936
2937   if (die->child != NULL)
2938     {
2939       child_die = die->child;
2940       while (child_die && child_die->tag)
2941         {
2942           process_die (child_die, cu);
2943           child_die = sibling_die (child_die);
2944         }
2945     }
2946
2947   new = pop_context ();
2948   /* Make a block for the local symbols within.  */
2949   finish_block (new->name, &local_symbols, new->old_blocks,
2950                 lowpc, highpc, objfile);
2951   
2952   /* In C++, we can have functions nested inside functions (e.g., when
2953      a function declares a class that has methods).  This means that
2954      when we finish processing a function scope, we may need to go
2955      back to building a containing block's symbol lists.  */
2956   local_symbols = new->locals;
2957   param_symbols = new->params;
2958
2959   /* If we've finished processing a top-level function, subsequent
2960      symbols go in the file symbol list.  */
2961   if (outermost_context_p ())
2962     cu->list_in_scope = &file_symbols;
2963
2964   processing_current_prefix = previous_prefix;
2965   if (back_to != NULL)
2966     do_cleanups (back_to);
2967 }
2968
2969 /* Process all the DIES contained within a lexical block scope.  Start
2970    a new scope, process the dies, and then close the scope.  */
2971
2972 static void
2973 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
2974 {
2975   struct objfile *objfile = cu->objfile;
2976   struct context_stack *new;
2977   CORE_ADDR lowpc, highpc;
2978   struct die_info *child_die;
2979   CORE_ADDR baseaddr;
2980
2981   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2982
2983   /* Ignore blocks with missing or invalid low and high pc attributes.  */
2984   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
2985      as multiple lexical blocks?  Handling children in a sane way would
2986      be nasty.  Might be easier to properly extend generic blocks to 
2987      describe ranges.  */
2988   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2989     return;
2990   lowpc += baseaddr;
2991   highpc += baseaddr;
2992
2993   push_context (0, lowpc);
2994   if (die->child != NULL)
2995     {
2996       child_die = die->child;
2997       while (child_die && child_die->tag)
2998         {
2999           process_die (child_die, cu);
3000           child_die = sibling_die (child_die);
3001         }
3002     }
3003   new = pop_context ();
3004
3005   if (local_symbols != NULL)
3006     {
3007       finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3008                     highpc, objfile);
3009     }
3010   local_symbols = new->locals;
3011 }
3012
3013 /* Get low and high pc attributes from a die.  Return 1 if the attributes
3014    are present and valid, otherwise, return 0.  Return -1 if the range is
3015    discontinuous, i.e. derived from DW_AT_ranges information.  */
3016 static int
3017 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
3018                       CORE_ADDR *highpc, struct dwarf2_cu *cu)
3019 {
3020   struct objfile *objfile = cu->objfile;
3021   struct comp_unit_head *cu_header = &cu->header;
3022   struct attribute *attr;
3023   bfd *obfd = objfile->obfd;
3024   CORE_ADDR low = 0;
3025   CORE_ADDR high = 0;
3026   int ret = 0;
3027
3028   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3029   if (attr)
3030     {
3031       high = DW_ADDR (attr);
3032       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3033       if (attr)
3034         low = DW_ADDR (attr);
3035       else
3036         /* Found high w/o low attribute.  */
3037         return 0;
3038
3039       /* Found consecutive range of addresses.  */
3040       ret = 1;
3041     }
3042   else
3043     {
3044       attr = dwarf2_attr (die, DW_AT_ranges, cu);
3045       if (attr != NULL)
3046         {
3047           unsigned int addr_size = cu_header->addr_size;
3048           CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3049           /* Value of the DW_AT_ranges attribute is the offset in the
3050              .debug_ranges section.  */
3051           unsigned int offset = DW_UNSND (attr);
3052           /* Base address selection entry.  */
3053           CORE_ADDR base;
3054           int found_base;
3055           int dummy;
3056           char *buffer;
3057           CORE_ADDR marker;
3058           int low_set;
3059  
3060           found_base = cu_header->base_known;
3061           base = cu_header->base_address;
3062
3063           if (offset >= dwarf2_per_objfile->ranges_size)
3064             {
3065               complaint (&symfile_complaints,
3066                          "Offset %d out of bounds for DW_AT_ranges attribute",
3067                          offset);
3068               return 0;
3069             }
3070           buffer = dwarf2_per_objfile->ranges_buffer + offset;
3071
3072           /* Read in the largest possible address.  */
3073           marker = read_address (obfd, buffer, cu, &dummy);
3074           if ((marker & mask) == mask)
3075             {
3076               /* If we found the largest possible address, then
3077                  read the base address.  */
3078               base = read_address (obfd, buffer + addr_size, cu, &dummy);
3079               buffer += 2 * addr_size;
3080               offset += 2 * addr_size;
3081               found_base = 1;
3082             }
3083
3084           low_set = 0;
3085
3086           while (1)
3087             {
3088               CORE_ADDR range_beginning, range_end;
3089
3090               range_beginning = read_address (obfd, buffer, cu, &dummy);
3091               buffer += addr_size;
3092               range_end = read_address (obfd, buffer, cu, &dummy);
3093               buffer += addr_size;
3094               offset += 2 * addr_size;
3095
3096               /* An end of list marker is a pair of zero addresses.  */
3097               if (range_beginning == 0 && range_end == 0)
3098                 /* Found the end of list entry.  */
3099                 break;
3100
3101               /* Each base address selection entry is a pair of 2 values.
3102                  The first is the largest possible address, the second is
3103                  the base address.  Check for a base address here.  */
3104               if ((range_beginning & mask) == mask)
3105                 {
3106                   /* If we found the largest possible address, then
3107                      read the base address.  */
3108                   base = read_address (obfd, buffer + addr_size, cu, &dummy);
3109                   found_base = 1;
3110                   continue;
3111                 }
3112
3113               if (!found_base)
3114                 {
3115                   /* We have no valid base address for the ranges
3116                      data.  */
3117                   complaint (&symfile_complaints,
3118                              "Invalid .debug_ranges data (no base address)");
3119                   return 0;
3120                 }
3121
3122               range_beginning += base;
3123               range_end += base;
3124
3125               /* FIXME: This is recording everything as a low-high
3126                  segment of consecutive addresses.  We should have a
3127                  data structure for discontiguous block ranges
3128                  instead.  */
3129               if (! low_set)
3130                 {
3131                   low = range_beginning;
3132                   high = range_end;
3133                   low_set = 1;
3134                 }
3135               else
3136                 {
3137                   if (range_beginning < low)
3138                     low = range_beginning;
3139                   if (range_end > high)
3140                     high = range_end;
3141                 }
3142             }
3143
3144           if (! low_set)
3145             /* If the first entry is an end-of-list marker, the range
3146                describes an empty scope, i.e. no instructions.  */
3147             return 0;
3148
3149           ret = -1;
3150         }
3151     }
3152
3153   if (high < low)
3154     return 0;
3155
3156   /* When using the GNU linker, .gnu.linkonce. sections are used to
3157      eliminate duplicate copies of functions and vtables and such.
3158      The linker will arbitrarily choose one and discard the others.
3159      The AT_*_pc values for such functions refer to local labels in
3160      these sections.  If the section from that file was discarded, the
3161      labels are not in the output, so the relocs get a value of 0.
3162      If this is a discarded function, mark the pc bounds as invalid,
3163      so that GDB will ignore it.  */
3164   if (low == 0 && (bfd_get_file_flags (obfd) & HAS_RELOC) == 0)
3165     return 0;
3166
3167   *lowpc = low;
3168   *highpc = high;
3169   return ret;
3170 }
3171
3172 /* Get the low and high pc's represented by the scope DIE, and store
3173    them in *LOWPC and *HIGHPC.  If the correct values can't be
3174    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
3175
3176 static void
3177 get_scope_pc_bounds (struct die_info *die,
3178                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
3179                      struct dwarf2_cu *cu)
3180 {
3181   CORE_ADDR best_low = (CORE_ADDR) -1;
3182   CORE_ADDR best_high = (CORE_ADDR) 0;
3183   CORE_ADDR current_low, current_high;
3184
3185   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3186     {
3187       best_low = current_low;
3188       best_high = current_high;
3189     }
3190   else
3191     {
3192       struct die_info *child = die->child;
3193
3194       while (child && child->tag)
3195         {
3196           switch (child->tag) {
3197           case DW_TAG_subprogram:
3198             if (dwarf2_get_pc_bounds (child, &current_low, &current_high, cu))
3199               {
3200                 best_low = min (best_low, current_low);
3201                 best_high = max (best_high, current_high);
3202               }
3203             break;
3204           case DW_TAG_namespace:
3205             /* FIXME: carlton/2004-01-16: Should we do this for
3206                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
3207                that current GCC's always emit the DIEs corresponding
3208                to definitions of methods of classes as children of a
3209                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3210                the DIEs giving the declarations, which could be
3211                anywhere).  But I don't see any reason why the
3212                standards says that they have to be there.  */
3213             get_scope_pc_bounds (child, &current_low, &current_high, cu);
3214
3215             if (current_low != ((CORE_ADDR) -1))
3216               {
3217                 best_low = min (best_low, current_low);
3218                 best_high = max (best_high, current_high);
3219               }
3220             break;
3221           default:
3222             /* Ignore. */
3223             break;
3224           }
3225
3226           child = sibling_die (child);
3227         }
3228     }
3229
3230   *lowpc = best_low;
3231   *highpc = best_high;
3232 }
3233
3234 /* Add an aggregate field to the field list.  */
3235
3236 static void
3237 dwarf2_add_field (struct field_info *fip, struct die_info *die,
3238                   struct dwarf2_cu *cu)
3239
3240   struct objfile *objfile = cu->objfile;
3241   struct nextfield *new_field;
3242   struct attribute *attr;
3243   struct field *fp;
3244   char *fieldname = "";
3245
3246   /* Allocate a new field list entry and link it in.  */
3247   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3248   make_cleanup (xfree, new_field);
3249   memset (new_field, 0, sizeof (struct nextfield));
3250   new_field->next = fip->fields;
3251   fip->fields = new_field;
3252   fip->nfields++;
3253
3254   /* Handle accessibility and virtuality of field.
3255      The default accessibility for members is public, the default
3256      accessibility for inheritance is private.  */
3257   if (die->tag != DW_TAG_inheritance)
3258     new_field->accessibility = DW_ACCESS_public;
3259   else
3260     new_field->accessibility = DW_ACCESS_private;
3261   new_field->virtuality = DW_VIRTUALITY_none;
3262
3263   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
3264   if (attr)
3265     new_field->accessibility = DW_UNSND (attr);
3266   if (new_field->accessibility != DW_ACCESS_public)
3267     fip->non_public_fields = 1;
3268   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
3269   if (attr)
3270     new_field->virtuality = DW_UNSND (attr);
3271
3272   fp = &new_field->field;
3273
3274   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
3275     {
3276       /* Data member other than a C++ static data member.  */
3277       
3278       /* Get type of field.  */
3279       fp->type = die_type (die, cu);
3280
3281       FIELD_STATIC_KIND (*fp) = 0;
3282
3283       /* Get bit size of field (zero if none).  */
3284       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
3285       if (attr)
3286         {
3287           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3288         }
3289       else
3290         {
3291           FIELD_BITSIZE (*fp) = 0;
3292         }
3293
3294       /* Get bit offset of field.  */
3295       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
3296       if (attr)
3297         {
3298           FIELD_BITPOS (*fp) =
3299             decode_locdesc (DW_BLOCK (attr), cu) * bits_per_byte;
3300         }
3301       else
3302         FIELD_BITPOS (*fp) = 0;
3303       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
3304       if (attr)
3305         {
3306           if (BITS_BIG_ENDIAN)
3307             {
3308               /* For big endian bits, the DW_AT_bit_offset gives the
3309                  additional bit offset from the MSB of the containing
3310                  anonymous object to the MSB of the field.  We don't
3311                  have to do anything special since we don't need to
3312                  know the size of the anonymous object.  */
3313               FIELD_BITPOS (*fp) += DW_UNSND (attr);
3314             }
3315           else
3316             {
3317               /* For little endian bits, compute the bit offset to the
3318                  MSB of the anonymous object, subtract off the number of
3319                  bits from the MSB of the field to the MSB of the
3320                  object, and then subtract off the number of bits of
3321                  the field itself.  The result is the bit offset of
3322                  the LSB of the field.  */
3323               int anonymous_size;
3324               int bit_offset = DW_UNSND (attr);
3325
3326               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3327               if (attr)
3328                 {
3329                   /* The size of the anonymous object containing
3330                      the bit field is explicit, so use the
3331                      indicated size (in bytes).  */
3332                   anonymous_size = DW_UNSND (attr);
3333                 }
3334               else
3335                 {
3336                   /* The size of the anonymous object containing
3337                      the bit field must be inferred from the type
3338                      attribute of the data member containing the
3339                      bit field.  */
3340                   anonymous_size = TYPE_LENGTH (fp->type);
3341                 }
3342               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
3343                 - bit_offset - FIELD_BITSIZE (*fp);
3344             }
3345         }
3346
3347       /* Get name of field.  */
3348       attr = dwarf2_attr (die, DW_AT_name, cu);
3349       if (attr && DW_STRING (attr))
3350         fieldname = DW_STRING (attr);
3351
3352       /* The name is already allocated along with this objfile, so we don't
3353          need to duplicate it for the type.  */
3354       fp->name = fieldname;
3355
3356       /* Change accessibility for artificial fields (e.g. virtual table
3357          pointer or virtual base class pointer) to private.  */
3358       if (dwarf2_attr (die, DW_AT_artificial, cu))
3359         {
3360           new_field->accessibility = DW_ACCESS_private;
3361           fip->non_public_fields = 1;
3362         }
3363     }
3364   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
3365     {
3366       /* C++ static member.  */
3367
3368       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
3369          is a declaration, but all versions of G++ as of this writing
3370          (so through at least 3.2.1) incorrectly generate
3371          DW_TAG_variable tags.  */
3372       
3373       char *physname;
3374
3375       /* Get name of field.  */
3376       attr = dwarf2_attr (die, DW_AT_name, cu);
3377       if (attr && DW_STRING (attr))
3378         fieldname = DW_STRING (attr);
3379       else
3380         return;
3381
3382       /* Get physical name.  */
3383       physname = dwarf2_linkage_name (die, cu);
3384
3385       /* The name is already allocated along with this objfile, so we don't
3386          need to duplicate it for the type.  */
3387       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
3388       FIELD_TYPE (*fp) = die_type (die, cu);
3389       FIELD_NAME (*fp) = fieldname;
3390     }
3391   else if (die->tag == DW_TAG_inheritance)
3392     {
3393       /* C++ base class field.  */
3394       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
3395       if (attr)
3396         FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
3397                               * bits_per_byte);
3398       FIELD_BITSIZE (*fp) = 0;
3399       FIELD_STATIC_KIND (*fp) = 0;
3400       FIELD_TYPE (*fp) = die_type (die, cu);
3401       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
3402       fip->nbaseclasses++;
3403     }
3404 }
3405
3406 /* Create the vector of fields, and attach it to the type.  */
3407
3408 static void
3409 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
3410                               struct dwarf2_cu *cu)
3411 {
3412   int nfields = fip->nfields;
3413
3414   /* Record the field count, allocate space for the array of fields,
3415      and create blank accessibility bitfields if necessary.  */
3416   TYPE_NFIELDS (type) = nfields;
3417   TYPE_FIELDS (type) = (struct field *)
3418     TYPE_ALLOC (type, sizeof (struct field) * nfields);
3419   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3420
3421   if (fip->non_public_fields)
3422     {
3423       ALLOCATE_CPLUS_STRUCT_TYPE (type);
3424
3425       TYPE_FIELD_PRIVATE_BITS (type) =
3426         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3427       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3428
3429       TYPE_FIELD_PROTECTED_BITS (type) =
3430         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3431       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3432
3433       TYPE_FIELD_IGNORE_BITS (type) =
3434         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3435       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3436     }
3437
3438   /* If the type has baseclasses, allocate and clear a bit vector for
3439      TYPE_FIELD_VIRTUAL_BITS.  */
3440   if (fip->nbaseclasses)
3441     {
3442       int num_bytes = B_BYTES (fip->nbaseclasses);
3443       char *pointer;
3444
3445       ALLOCATE_CPLUS_STRUCT_TYPE (type);
3446       pointer = (char *) TYPE_ALLOC (type, num_bytes);
3447       TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
3448       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
3449       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
3450     }
3451
3452   /* Copy the saved-up fields into the field vector.  Start from the head
3453      of the list, adding to the tail of the field array, so that they end
3454      up in the same order in the array in which they were added to the list.  */
3455   while (nfields-- > 0)
3456     {
3457       TYPE_FIELD (type, nfields) = fip->fields->field;
3458       switch (fip->fields->accessibility)
3459         {
3460         case DW_ACCESS_private:
3461           SET_TYPE_FIELD_PRIVATE (type, nfields);
3462           break;
3463
3464         case DW_ACCESS_protected:
3465           SET_TYPE_FIELD_PROTECTED (type, nfields);
3466           break;
3467
3468         case DW_ACCESS_public:
3469           break;
3470
3471         default:
3472           /* Unknown accessibility.  Complain and treat it as public.  */
3473           {
3474             complaint (&symfile_complaints, "unsupported accessibility %d",
3475                        fip->fields->accessibility);
3476           }
3477           break;
3478         }
3479       if (nfields < fip->nbaseclasses)
3480         {
3481           switch (fip->fields->virtuality)
3482             {
3483             case DW_VIRTUALITY_virtual:
3484             case DW_VIRTUALITY_pure_virtual:
3485               SET_TYPE_FIELD_VIRTUAL (type, nfields);
3486               break;
3487             }
3488         }
3489       fip->fields = fip->fields->next;
3490     }
3491 }
3492
3493 /* Add a member function to the proper fieldlist.  */
3494
3495 static void
3496 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
3497                       struct type *type, struct dwarf2_cu *cu)
3498 {
3499   struct objfile *objfile = cu->objfile;
3500   struct attribute *attr;
3501   struct fnfieldlist *flp;
3502   int i;
3503   struct fn_field *fnp;
3504   char *fieldname;
3505   char *physname;
3506   struct nextfnfield *new_fnfield;
3507
3508   /* Get name of member function.  */
3509   attr = dwarf2_attr (die, DW_AT_name, cu);
3510   if (attr && DW_STRING (attr))
3511     fieldname = DW_STRING (attr);
3512   else
3513     return;
3514
3515   /* Get the mangled name.  */
3516   physname = dwarf2_linkage_name (die, cu);
3517
3518   /* Look up member function name in fieldlist.  */
3519   for (i = 0; i < fip->nfnfields; i++)
3520     {
3521       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
3522         break;
3523     }
3524
3525   /* Create new list element if necessary.  */
3526   if (i < fip->nfnfields)
3527     flp = &fip->fnfieldlists[i];
3528   else
3529     {
3530       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
3531         {
3532           fip->fnfieldlists = (struct fnfieldlist *)
3533             xrealloc (fip->fnfieldlists,
3534                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
3535                       * sizeof (struct fnfieldlist));
3536           if (fip->nfnfields == 0)
3537             make_cleanup (free_current_contents, &fip->fnfieldlists);
3538         }
3539       flp = &fip->fnfieldlists[fip->nfnfields];
3540       flp->name = fieldname;
3541       flp->length = 0;
3542       flp->head = NULL;
3543       fip->nfnfields++;
3544     }
3545
3546   /* Create a new member function field and chain it to the field list
3547      entry. */
3548   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
3549   make_cleanup (xfree, new_fnfield);
3550   memset (new_fnfield, 0, sizeof (struct nextfnfield));
3551   new_fnfield->next = flp->head;
3552   flp->head = new_fnfield;
3553   flp->length++;
3554
3555   /* Fill in the member function field info.  */
3556   fnp = &new_fnfield->fnfield;
3557   /* The name is already allocated along with this objfile, so we don't
3558      need to duplicate it for the type.  */
3559   fnp->physname = physname ? physname : "";
3560   fnp->type = alloc_type (objfile);
3561   if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
3562     {
3563       int nparams = TYPE_NFIELDS (die->type);
3564
3565       /* TYPE is the domain of this method, and DIE->TYPE is the type
3566            of the method itself (TYPE_CODE_METHOD).  */
3567       smash_to_method_type (fnp->type, type,
3568                             TYPE_TARGET_TYPE (die->type),
3569                             TYPE_FIELDS (die->type),
3570                             TYPE_NFIELDS (die->type),
3571                             TYPE_VARARGS (die->type));
3572
3573       /* Handle static member functions.
3574          Dwarf2 has no clean way to discern C++ static and non-static
3575          member functions. G++ helps GDB by marking the first
3576          parameter for non-static member functions (which is the
3577          this pointer) as artificial. We obtain this information
3578          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
3579       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
3580         fnp->voffset = VOFFSET_STATIC;
3581     }
3582   else
3583     complaint (&symfile_complaints, "member function type missing for '%s'",
3584                physname);
3585
3586   /* Get fcontext from DW_AT_containing_type if present.  */
3587   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
3588     fnp->fcontext = die_containing_type (die, cu);
3589
3590   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
3591      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
3592
3593   /* Get accessibility.  */
3594   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
3595   if (attr)
3596     {
3597       switch (DW_UNSND (attr))
3598         {
3599         case DW_ACCESS_private:
3600           fnp->is_private = 1;
3601           break;
3602         case DW_ACCESS_protected:
3603           fnp->is_protected = 1;
3604           break;
3605         }
3606     }
3607
3608   /* Check for artificial methods.  */
3609   attr = dwarf2_attr (die, DW_AT_artificial, cu);
3610   if (attr && DW_UNSND (attr) != 0)
3611     fnp->is_artificial = 1;
3612
3613   /* Get index in virtual function table if it is a virtual member function.  */
3614   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
3615   if (attr)
3616     {
3617       /* Support the .debug_loc offsets */
3618       if (attr_form_is_block (attr))
3619         {
3620           fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
3621         }
3622       else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3623         {
3624           dwarf2_complex_location_expr_complaint ();
3625         }
3626       else
3627         {
3628           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
3629                                                  fieldname);
3630         }
3631    }
3632 }
3633
3634 /* Create the vector of member function fields, and attach it to the type.  */
3635
3636 static void
3637 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
3638                                  struct dwarf2_cu *cu)
3639 {
3640   struct fnfieldlist *flp;
3641   int total_length = 0;
3642   int i;
3643
3644   ALLOCATE_CPLUS_STRUCT_TYPE (type);
3645   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3646     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
3647
3648   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
3649     {
3650       struct nextfnfield *nfp = flp->head;
3651       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
3652       int k;
3653
3654       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
3655       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
3656       fn_flp->fn_fields = (struct fn_field *)
3657         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
3658       for (k = flp->length; (k--, nfp); nfp = nfp->next)
3659         fn_flp->fn_fields[k] = nfp->fnfield;
3660
3661       total_length += flp->length;
3662     }
3663
3664   TYPE_NFN_FIELDS (type) = fip->nfnfields;
3665   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3666 }
3667
3668
3669 /* Returns non-zero if NAME is the name of a vtable member in CU's
3670    language, zero otherwise.  */
3671 static int
3672 is_vtable_name (const char *name, struct dwarf2_cu *cu)
3673 {
3674   static const char vptr[] = "_vptr";
3675   static const char vtable[] = "vtable";
3676
3677   /* Look for the C++ and Java forms of the vtable.  */
3678   if ((cu->language == language_java
3679        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
3680        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
3681        && is_cplus_marker (name[sizeof (vptr) - 1])))
3682     return 1;
3683
3684   return 0;
3685 }
3686
3687
3688 /* Called when we find the DIE that starts a structure or union scope
3689    (definition) to process all dies that define the members of the
3690    structure or union.
3691
3692    NOTE: we need to call struct_type regardless of whether or not the
3693    DIE has an at_name attribute, since it might be an anonymous
3694    structure or union.  This gets the type entered into our set of
3695    user defined types.
3696
3697    However, if the structure is incomplete (an opaque struct/union)
3698    then suppress creating a symbol table entry for it since gdb only
3699    wants to find the one with the complete definition.  Note that if
3700    it is complete, we just call new_symbol, which does it's own
3701    checking about whether the struct/union is anonymous or not (and
3702    suppresses creating a symbol table entry itself).  */
3703
3704 static void
3705 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
3706 {
3707   struct objfile *objfile = cu->objfile;
3708   struct type *type;
3709   struct attribute *attr;
3710   const char *previous_prefix = processing_current_prefix;
3711   struct cleanup *back_to = NULL;
3712
3713   if (die->type)
3714     return;
3715
3716   type = alloc_type (objfile);
3717
3718   INIT_CPLUS_SPECIFIC (type);
3719   attr = dwarf2_attr (die, DW_AT_name, cu);
3720   if (attr && DW_STRING (attr))
3721     {
3722       if (cu->language == language_cplus
3723           || cu->language == language_java)
3724         {
3725           char *new_prefix = determine_class_name (die, cu);
3726           TYPE_TAG_NAME (type) = obsavestring (new_prefix,
3727                                                strlen (new_prefix),
3728                                                &objfile->objfile_obstack);
3729           back_to = make_cleanup (xfree, new_prefix);
3730           processing_current_prefix = new_prefix;
3731         }
3732       else
3733         {
3734           /* The name is already allocated along with this objfile, so
3735              we don't need to duplicate it for the type.  */
3736           TYPE_TAG_NAME (type) = DW_STRING (attr);
3737         }
3738     }
3739
3740   if (die->tag == DW_TAG_structure_type)
3741     {
3742       TYPE_CODE (type) = TYPE_CODE_STRUCT;
3743     }
3744   else if (die->tag == DW_TAG_union_type)
3745     {
3746       TYPE_CODE (type) = TYPE_CODE_UNION;
3747     }
3748   else
3749     {
3750       /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
3751          in gdbtypes.h.  */
3752       TYPE_CODE (type) = TYPE_CODE_CLASS;
3753     }
3754
3755   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3756   if (attr)
3757     {
3758       TYPE_LENGTH (type) = DW_UNSND (attr);
3759     }
3760   else
3761     {
3762       TYPE_LENGTH (type) = 0;
3763     }
3764
3765   if (die_is_declaration (die, cu))
3766     TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
3767
3768   /* We need to add the type field to the die immediately so we don't
3769      infinitely recurse when dealing with pointers to the structure
3770      type within the structure itself. */
3771   set_die_type (die, type, cu);
3772
3773   if (die->child != NULL && ! die_is_declaration (die, cu))
3774     {
3775       struct field_info fi;
3776       struct die_info *child_die;
3777       struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
3778
3779       memset (&fi, 0, sizeof (struct field_info));
3780
3781       child_die = die->child;
3782
3783       while (child_die && child_die->tag)
3784         {
3785           if (child_die->tag == DW_TAG_member
3786               || child_die->tag == DW_TAG_variable)
3787             {
3788               /* NOTE: carlton/2002-11-05: A C++ static data member
3789                  should be a DW_TAG_member that is a declaration, but
3790                  all versions of G++ as of this writing (so through at
3791                  least 3.2.1) incorrectly generate DW_TAG_variable
3792                  tags for them instead.  */
3793               dwarf2_add_field (&fi, child_die, cu);
3794             }
3795           else if (child_die->tag == DW_TAG_subprogram)
3796             {
3797               /* C++ member function. */
3798               read_type_die (child_die, cu);
3799               dwarf2_add_member_fn (&fi, child_die, type, cu);
3800             }
3801           else if (child_die->tag == DW_TAG_inheritance)
3802             {
3803               /* C++ base class field.  */
3804               dwarf2_add_field (&fi, child_die, cu);
3805             }
3806           child_die = sibling_die (child_die);
3807         }
3808
3809       /* Attach fields and member functions to the type.  */
3810       if (fi.nfields)
3811         dwarf2_attach_fields_to_type (&fi, type, cu);
3812       if (fi.nfnfields)
3813         {
3814           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
3815
3816           /* Get the type which refers to the base class (possibly this
3817              class itself) which contains the vtable pointer for the current
3818              class from the DW_AT_containing_type attribute.  */
3819
3820           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
3821             {
3822               struct type *t = die_containing_type (die, cu);
3823
3824               TYPE_VPTR_BASETYPE (type) = t;
3825               if (type == t)
3826                 {
3827                   int i;
3828
3829                   /* Our own class provides vtbl ptr.  */
3830                   for (i = TYPE_NFIELDS (t) - 1;
3831                        i >= TYPE_N_BASECLASSES (t);
3832                        --i)
3833                     {
3834                       char *fieldname = TYPE_FIELD_NAME (t, i);
3835
3836                       if (is_vtable_name (fieldname, cu))
3837                         {
3838                           TYPE_VPTR_FIELDNO (type) = i;
3839                           break;
3840                         }
3841                     }
3842
3843                   /* Complain if virtual function table field not found.  */
3844                   if (i < TYPE_N_BASECLASSES (t))
3845                     complaint (&symfile_complaints,
3846                                "virtual function table pointer not found when defining class '%s'",
3847                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
3848                                "");
3849                 }
3850               else
3851                 {
3852                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
3853                 }
3854             }
3855         }
3856
3857       do_cleanups (back_to);
3858     }
3859
3860   processing_current_prefix = previous_prefix;
3861   if (back_to != NULL)
3862     do_cleanups (back_to);
3863 }
3864
3865 static void
3866 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
3867 {
3868   struct objfile *objfile = cu->objfile;
3869   const char *previous_prefix = processing_current_prefix;
3870   struct die_info *child_die = die->child;
3871
3872   if (TYPE_TAG_NAME (die->type) != NULL)
3873     processing_current_prefix = TYPE_TAG_NAME (die->type);
3874
3875   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
3876      snapshots) has been known to create a die giving a declaration
3877      for a class that has, as a child, a die giving a definition for a
3878      nested class.  So we have to process our children even if the
3879      current die is a declaration.  Normally, of course, a declaration
3880      won't have any children at all.  */
3881
3882   while (child_die != NULL && child_die->tag)
3883     {
3884       if (child_die->tag == DW_TAG_member
3885           || child_die->tag == DW_TAG_variable
3886           || child_die->tag == DW_TAG_inheritance)
3887         {
3888           /* Do nothing.  */
3889         }
3890       else
3891         process_die (child_die, cu);
3892
3893       child_die = sibling_die (child_die);
3894     }
3895
3896   if (die->child != NULL && ! die_is_declaration (die, cu))
3897     new_symbol (die, die->type, cu);
3898
3899   processing_current_prefix = previous_prefix;
3900 }
3901
3902 /* Given a DW_AT_enumeration_type die, set its type.  We do not
3903    complete the type's fields yet, or create any symbols.  */
3904
3905 static void
3906 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
3907 {
3908   struct objfile *objfile = cu->objfile;
3909   struct type *type;
3910   struct attribute *attr;
3911
3912   if (die->type)
3913     return;
3914
3915   type = alloc_type (objfile);
3916
3917   TYPE_CODE (type) = TYPE_CODE_ENUM;
3918   attr = dwarf2_attr (die, DW_AT_name, cu);
3919   if (attr && DW_STRING (attr))
3920     {
3921       char *name = DW_STRING (attr);
3922
3923       if (processing_has_namespace_info)
3924         {
3925           TYPE_TAG_NAME (type) = typename_concat (&objfile->objfile_obstack,
3926                                                   processing_current_prefix,
3927                                                   name, cu);
3928         }
3929       else
3930         {
3931           /* The name is already allocated along with this objfile, so
3932              we don't need to duplicate it for the type.  */
3933           TYPE_TAG_NAME (type) = name;
3934         }
3935     }
3936
3937   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3938   if (attr)
3939     {
3940       TYPE_LENGTH (type) = DW_UNSND (attr);
3941     }
3942   else
3943     {
3944       TYPE_LENGTH (type) = 0;
3945     }
3946
3947   set_die_type (die, type, cu);
3948 }
3949
3950 /* Determine the name of the type represented by DIE, which should be
3951    a named C++ or Java compound type.  Return the name in question; the caller
3952    is responsible for xfree()'ing it.  */
3953
3954 static char *
3955 determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
3956 {
3957   struct cleanup *back_to = NULL;
3958   struct die_info *spec_die = die_specification (die, cu);
3959   char *new_prefix = NULL;
3960
3961   /* If this is the definition of a class that is declared by another
3962      die, then processing_current_prefix may not be accurate; see
3963      read_func_scope for a similar example.  */
3964   if (spec_die != NULL)
3965     {
3966       char *specification_prefix = determine_prefix (spec_die, cu);
3967       processing_current_prefix = specification_prefix;
3968       back_to = make_cleanup (xfree, specification_prefix);
3969     }
3970
3971   /* If we don't have namespace debug info, guess the name by trying
3972      to demangle the names of members, just like we did in
3973      guess_structure_name.  */
3974   if (!processing_has_namespace_info)
3975     {
3976       struct die_info *child;
3977
3978       for (child = die->child;
3979            child != NULL && child->tag != 0;
3980            child = sibling_die (child))
3981         {
3982           if (child->tag == DW_TAG_subprogram)
3983             {
3984               new_prefix 
3985                 = language_class_name_from_physname (cu->language_defn,
3986                                                      dwarf2_linkage_name
3987                                                      (child, cu));
3988
3989               if (new_prefix != NULL)
3990                 break;
3991             }
3992         }
3993     }
3994
3995   if (new_prefix == NULL)
3996     {
3997       const char *name = dwarf2_name (die, cu);
3998       new_prefix = typename_concat (NULL, processing_current_prefix,
3999                                     name ? name : "<<anonymous>>", 
4000                                     cu);
4001     }
4002
4003   if (back_to != NULL)
4004     do_cleanups (back_to);
4005
4006   return new_prefix;
4007 }
4008
4009 /* Given a pointer to a die which begins an enumeration, process all
4010    the dies that define the members of the enumeration, and create the
4011    symbol for the enumeration type.
4012
4013    NOTE: We reverse the order of the element list.  */
4014
4015 static void
4016 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
4017 {
4018   struct objfile *objfile = cu->objfile;
4019   struct die_info *child_die;
4020   struct field *fields;
4021   struct attribute *attr;
4022   struct symbol *sym;
4023   int num_fields;
4024   int unsigned_enum = 1;
4025
4026   num_fields = 0;
4027   fields = NULL;
4028   if (die->child != NULL)
4029     {
4030       child_die = die->child;
4031       while (child_die && child_die->tag)
4032         {
4033           if (child_die->tag != DW_TAG_enumerator)
4034             {
4035               process_die (child_die, cu);
4036             }
4037           else
4038             {
4039               attr = dwarf2_attr (child_die, DW_AT_name, cu);
4040               if (attr)
4041                 {
4042                   sym = new_symbol (child_die, die->type, cu);
4043                   if (SYMBOL_VALUE (sym) < 0)
4044                     unsigned_enum = 0;
4045
4046                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
4047                     {
4048                       fields = (struct field *)
4049                         xrealloc (fields,
4050                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
4051                                   * sizeof (struct field));
4052                     }
4053
4054                   FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
4055                   FIELD_TYPE (fields[num_fields]) = NULL;
4056                   FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
4057                   FIELD_BITSIZE (fields[num_fields]) = 0;
4058                   FIELD_STATIC_KIND (fields[num_fields]) = 0;
4059
4060                   num_fields++;
4061                 }
4062             }
4063
4064           child_die = sibling_die (child_die);
4065         }
4066
4067       if (num_fields)
4068         {
4069           TYPE_NFIELDS (die->type) = num_fields;
4070           TYPE_FIELDS (die->type) = (struct field *)
4071             TYPE_ALLOC (die->type, sizeof (struct field) * num_fields);
4072           memcpy (TYPE_FIELDS (die->type), fields,
4073                   sizeof (struct field) * num_fields);
4074           xfree (fields);
4075         }
4076       if (unsigned_enum)
4077         TYPE_FLAGS (die->type) |= TYPE_FLAG_UNSIGNED;
4078     }
4079
4080   new_symbol (die, die->type, cu);
4081 }
4082
4083 /* Extract all information from a DW_TAG_array_type DIE and put it in
4084    the DIE's type field.  For now, this only handles one dimensional
4085    arrays.  */
4086
4087 static void
4088 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
4089 {
4090   struct objfile *objfile = cu->objfile;
4091   struct die_info *child_die;
4092   struct type *type = NULL;
4093   struct type *element_type, *range_type, *index_type;
4094   struct type **range_types = NULL;
4095   struct attribute *attr;
4096   int ndim = 0;
4097   struct cleanup *back_to;
4098
4099   /* Return if we've already decoded this type. */
4100   if (die->type)
4101     {
4102       return;
4103     }
4104
4105   element_type = die_type (die, cu);
4106
4107   /* Irix 6.2 native cc creates array types without children for
4108      arrays with unspecified length.  */
4109   if (die->child == NULL)
4110     {
4111       index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
4112       range_type = create_range_type (NULL, index_type, 0, -1);
4113       set_die_type (die, create_array_type (NULL, element_type, range_type),
4114                     cu);
4115       return;
4116     }
4117
4118   back_to = make_cleanup (null_cleanup, NULL);
4119   child_die = die->child;
4120   while (child_die && child_die->tag)
4121     {
4122       if (child_die->tag == DW_TAG_subrange_type)
4123         {
4124           read_subrange_type (child_die, cu);
4125
4126           if (child_die->type != NULL)
4127             {
4128               /* The range type was succesfully read. Save it for
4129                  the array type creation.  */
4130               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
4131                 {
4132                   range_types = (struct type **)
4133                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
4134                               * sizeof (struct type *));
4135                   if (ndim == 0)
4136                     make_cleanup (free_current_contents, &range_types);
4137                 }
4138               range_types[ndim++] = child_die->type;
4139             }
4140         }
4141       child_die = sibling_die (child_die);
4142     }
4143
4144   /* Dwarf2 dimensions are output from left to right, create the
4145      necessary array types in backwards order.  */
4146
4147   type = element_type;
4148
4149   if (read_array_order (die, cu) == DW_ORD_col_major)
4150     {
4151       int i = 0;
4152       while (i < ndim)
4153         type = create_array_type (NULL, type, range_types[i++]);
4154     }
4155   else
4156     {
4157       while (ndim-- > 0)
4158         type = create_array_type (NULL, type, range_types[ndim]);
4159     }
4160
4161   /* Understand Dwarf2 support for vector types (like they occur on
4162      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
4163      array type.  This is not part of the Dwarf2/3 standard yet, but a
4164      custom vendor extension.  The main difference between a regular
4165      array and the vector variant is that vectors are passed by value
4166      to functions.  */
4167   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
4168   if (attr)
4169     TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
4170
4171   do_cleanups (back_to);
4172
4173   /* Install the type in the die. */
4174   set_die_type (die, type, cu);
4175 }
4176
4177 static enum dwarf_array_dim_ordering
4178 read_array_order (struct die_info *die, struct dwarf2_cu *cu) 
4179 {
4180   struct attribute *attr;
4181
4182   attr = dwarf2_attr (die, DW_AT_ordering, cu);
4183
4184   if (attr) return DW_SND (attr);
4185
4186   /*
4187     GNU F77 is a special case, as at 08/2004 array type info is the
4188     opposite order to the dwarf2 specification, but data is still 
4189     laid out as per normal fortran.
4190
4191     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need 
4192     version checking.
4193   */
4194
4195   if (cu->language == language_fortran &&
4196       cu->producer && strstr (cu->producer, "GNU F77"))
4197     {
4198       return DW_ORD_row_major;
4199     }
4200
4201   switch (cu->language_defn->la_array_ordering) 
4202     {
4203     case array_column_major:
4204       return DW_ORD_col_major;
4205     case array_row_major:
4206     default:
4207       return DW_ORD_row_major;
4208     };
4209 }
4210
4211
4212 /* First cut: install each common block member as a global variable.  */
4213
4214 static void
4215 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
4216 {
4217   struct die_info *child_die;
4218   struct attribute *attr;
4219   struct symbol *sym;
4220   CORE_ADDR base = (CORE_ADDR) 0;
4221
4222   attr = dwarf2_attr (die, DW_AT_location, cu);
4223   if (attr)
4224     {
4225       /* Support the .debug_loc offsets */
4226       if (attr_form_is_block (attr))
4227         {
4228           base = decode_locdesc (DW_BLOCK (attr), cu);
4229         }
4230       else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
4231         {
4232           dwarf2_complex_location_expr_complaint ();
4233         }
4234       else
4235         {
4236           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4237                                                  "common block member");
4238         }
4239     }
4240   if (die->child != NULL)
4241     {
4242       child_die = die->child;
4243       while (child_die && child_die->tag)
4244         {
4245           sym = new_symbol (child_die, NULL, cu);
4246           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
4247           if (attr)
4248             {
4249               SYMBOL_VALUE_ADDRESS (sym) =
4250                 base + decode_locdesc (DW_BLOCK (attr), cu);
4251               add_symbol_to_list (sym, &global_symbols);
4252             }
4253           child_die = sibling_die (child_die);
4254         }
4255     }
4256 }
4257
4258 /* Read a C++ namespace.  */
4259
4260 static void
4261 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
4262 {
4263   struct objfile *objfile = cu->objfile;
4264   const char *previous_prefix = processing_current_prefix;
4265   const char *name;
4266   int is_anonymous;
4267   struct die_info *current_die;
4268   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4269
4270   name = namespace_name (die, &is_anonymous, cu);
4271
4272   /* Now build the name of the current namespace.  */
4273
4274   if (previous_prefix[0] == '\0')
4275     {
4276       processing_current_prefix = name;
4277     }
4278   else
4279     {
4280       char *temp_name = typename_concat (NULL, previous_prefix, name, cu);
4281       make_cleanup (xfree, temp_name);
4282       processing_current_prefix = temp_name;
4283     }
4284
4285   /* Add a symbol associated to this if we haven't seen the namespace
4286      before.  Also, add a using directive if it's an anonymous
4287      namespace.  */
4288
4289   if (dwarf2_extension (die, cu) == NULL)
4290     {
4291       struct type *type;
4292
4293       /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
4294          this cast will hopefully become unnecessary.  */
4295       type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
4296                         (char *) processing_current_prefix,
4297                         objfile);
4298       TYPE_TAG_NAME (type) = TYPE_NAME (type);
4299
4300       new_symbol (die, type, cu);
4301       set_die_type (die, type, cu);
4302
4303       if (is_anonymous)
4304         cp_add_using_directive (processing_current_prefix,
4305                                 strlen (previous_prefix),
4306                                 strlen (processing_current_prefix));
4307     }
4308
4309   if (die->child != NULL)
4310     {
4311       struct die_info *child_die = die->child;
4312       
4313       while (child_die && child_die->tag)
4314         {
4315           process_die (child_die, cu);
4316           child_die = sibling_die (child_die);
4317         }
4318     }
4319
4320   processing_current_prefix = previous_prefix;
4321   do_cleanups (back_to);
4322 }
4323
4324 /* Return the name of the namespace represented by DIE.  Set
4325    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
4326    namespace.  */
4327
4328 static const char *
4329 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
4330 {
4331   struct die_info *current_die;
4332   const char *name = NULL;
4333
4334   /* Loop through the extensions until we find a name.  */
4335
4336   for (current_die = die;
4337        current_die != NULL;
4338        current_die = dwarf2_extension (die, cu))
4339     {
4340       name = dwarf2_name (current_die, cu);
4341       if (name != NULL)
4342         break;
4343     }
4344
4345   /* Is it an anonymous namespace?  */
4346
4347   *is_anonymous = (name == NULL);
4348   if (*is_anonymous)
4349     name = "(anonymous namespace)";
4350
4351   return name;
4352 }
4353
4354 /* Extract all information from a DW_TAG_pointer_type DIE and add to
4355    the user defined type vector.  */
4356
4357 static void
4358 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
4359 {
4360   struct comp_unit_head *cu_header = &cu->header;
4361   struct type *type;
4362   struct attribute *attr_byte_size;
4363   struct attribute *attr_address_class;
4364   int byte_size, addr_class;
4365
4366   if (die->type)
4367     {
4368       return;
4369     }
4370
4371   type = lookup_pointer_type (die_type (die, cu));
4372
4373   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
4374   if (attr_byte_size)
4375     byte_size = DW_UNSND (attr_byte_size);
4376   else
4377     byte_size = cu_header->addr_size;
4378
4379   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
4380   if (attr_address_class)
4381     addr_class = DW_UNSND (attr_address_class);
4382   else
4383     addr_class = DW_ADDR_none;
4384
4385   /* If the pointer size or address class is different than the
4386      default, create a type variant marked as such and set the
4387      length accordingly.  */
4388   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
4389     {
4390       if (ADDRESS_CLASS_TYPE_FLAGS_P ())
4391         {
4392           int type_flags;
4393
4394           type_flags = ADDRESS_CLASS_TYPE_FLAGS (byte_size, addr_class);
4395           gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
4396           type = make_type_with_address_space (type, type_flags);
4397         }
4398       else if (TYPE_LENGTH (type) != byte_size)
4399         {
4400           complaint (&symfile_complaints, "invalid pointer size %d", byte_size);
4401         }
4402       else {
4403         /* Should we also complain about unhandled address classes?  */
4404       }
4405     }
4406
4407   TYPE_LENGTH (type) = byte_size;
4408   set_die_type (die, type, cu);
4409 }
4410
4411 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
4412    the user defined type vector.  */
4413
4414 static void
4415 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
4416 {
4417   struct objfile *objfile = cu->objfile;
4418   struct type *type;
4419   struct type *to_type;
4420   struct type *domain;
4421
4422   if (die->type)
4423     {
4424       return;
4425     }
4426
4427   type = alloc_type (objfile);
4428   to_type = die_type (die, cu);
4429   domain = die_containing_type (die, cu);
4430   smash_to_member_type (type, domain, to_type);
4431
4432   set_die_type (die, type, cu);
4433 }
4434
4435 /* Extract all information from a DW_TAG_reference_type DIE and add to
4436    the user defined type vector.  */
4437
4438 static void
4439 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
4440 {
4441   struct comp_unit_head *cu_header = &cu->header;
4442   struct type *type;
4443   struct attribute *attr;
4444
4445   if (die->type)
4446     {
4447       return;
4448     }
4449
4450   type = lookup_reference_type (die_type (die, cu));
4451   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4452   if (attr)
4453     {
4454       TYPE_LENGTH (type) = DW_UNSND (attr);
4455     }
4456   else
4457     {
4458       TYPE_LENGTH (type) = cu_header->addr_size;
4459     }
4460   set_die_type (die, type, cu);
4461 }
4462
4463 static void
4464 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
4465 {
4466   struct type *base_type;
4467
4468   if (die->type)
4469     {
4470       return;
4471     }
4472
4473   base_type = die_type (die, cu);
4474   set_die_type (die, make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0),
4475                 cu);
4476 }
4477
4478 static void
4479 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
4480 {
4481   struct type *base_type;
4482
4483   if (die->type)
4484     {
4485       return;
4486     }
4487
4488   base_type = die_type (die, cu);
4489   set_die_type (die, make_cv_type (TYPE_CONST (base_type), 1, base_type, 0),
4490                 cu);
4491 }
4492
4493 /* Extract all information from a DW_TAG_string_type DIE and add to
4494    the user defined type vector.  It isn't really a user defined type,
4495    but it behaves like one, with other DIE's using an AT_user_def_type
4496    attribute to reference it.  */
4497
4498 static void
4499 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
4500 {
4501   struct objfile *objfile = cu->objfile;
4502   struct type *type, *range_type, *index_type, *char_type;
4503   struct attribute *attr;
4504   unsigned int length;
4505
4506   if (die->type)
4507     {
4508       return;
4509     }
4510
4511   attr = dwarf2_attr (die, DW_AT_string_length, cu);
4512   if (attr)
4513     {
4514       length = DW_UNSND (attr);
4515     }
4516   else
4517     {
4518       /* check for the DW_AT_byte_size attribute */
4519       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4520       if (attr)
4521         {
4522           length = DW_UNSND (attr);
4523         }
4524       else
4525         {
4526           length = 1;
4527         }
4528     }
4529   index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
4530   range_type = create_range_type (NULL, index_type, 1, length);
4531   if (cu->language == language_fortran)
4532     {
4533       /* Need to create a unique string type for bounds
4534          information */
4535       type = create_string_type (0, range_type);
4536     }
4537   else
4538     {
4539       char_type = dwarf2_fundamental_type (objfile, FT_CHAR, cu);
4540       type = create_string_type (char_type, range_type);
4541     }
4542   set_die_type (die, type, cu);
4543 }
4544
4545 /* Handle DIES due to C code like:
4546
4547    struct foo
4548    {
4549    int (*funcp)(int a, long l);
4550    int b;
4551    };
4552
4553    ('funcp' generates a DW_TAG_subroutine_type DIE)
4554  */
4555
4556 static void
4557 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
4558 {
4559   struct type *type;            /* Type that this function returns */
4560   struct type *ftype;           /* Function that returns above type */
4561   struct attribute *attr;
4562
4563   /* Decode the type that this subroutine returns */
4564   if (die->type)
4565     {
4566       return;
4567     }
4568   type = die_type (die, cu);
4569   ftype = make_function_type (type, (struct type **) 0);
4570
4571   /* All functions in C++ and Java have prototypes.  */
4572   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
4573   if ((attr && (DW_UNSND (attr) != 0))
4574       || cu->language == language_cplus
4575       || cu->language == language_java)
4576     TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
4577
4578   if (die->child != NULL)
4579     {
4580       struct die_info *child_die;
4581       int nparams = 0;
4582       int iparams = 0;
4583
4584       /* Count the number of parameters.
4585          FIXME: GDB currently ignores vararg functions, but knows about
4586          vararg member functions.  */
4587       child_die = die->child;
4588       while (child_die && child_die->tag)
4589         {
4590           if (child_die->tag == DW_TAG_formal_parameter)
4591             nparams++;
4592           else if (child_die->tag == DW_TAG_unspecified_parameters)
4593             TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
4594           child_die = sibling_die (child_die);
4595         }
4596
4597       /* Allocate storage for parameters and fill them in.  */
4598       TYPE_NFIELDS (ftype) = nparams;
4599       TYPE_FIELDS (ftype) = (struct field *)
4600         TYPE_ALLOC (ftype, nparams * sizeof (struct field));
4601
4602       child_die = die->child;
4603       while (child_die && child_die->tag)
4604         {
4605           if (child_die->tag == DW_TAG_formal_parameter)
4606             {
4607               /* Dwarf2 has no clean way to discern C++ static and non-static
4608                  member functions. G++ helps GDB by marking the first
4609                  parameter for non-static member functions (which is the
4610                  this pointer) as artificial. We pass this information
4611                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
4612               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
4613               if (attr)
4614                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
4615               else
4616                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
4617               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
4618               iparams++;
4619             }
4620           child_die = sibling_die (child_die);
4621         }
4622     }
4623
4624   set_die_type (die, ftype, cu);
4625 }
4626
4627 static void
4628 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
4629 {
4630   struct objfile *objfile = cu->objfile;
4631   struct attribute *attr;
4632   char *name = NULL;
4633
4634   if (!die->type)
4635     {
4636       attr = dwarf2_attr (die, DW_AT_name, cu);
4637       if (attr && DW_STRING (attr))
4638         {
4639           name = DW_STRING (attr);
4640         }
4641       set_die_type (die, init_type (TYPE_CODE_TYPEDEF, 0,
4642                                     TYPE_FLAG_TARGET_STUB, name, objfile),
4643                     cu);
4644       TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
4645     }
4646 }
4647
4648 /* Find a representation of a given base type and install
4649    it in the TYPE field of the die.  */
4650
4651 static void
4652 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
4653 {
4654   struct objfile *objfile = cu->objfile;
4655   struct type *type;
4656   struct attribute *attr;
4657   int encoding = 0, size = 0;
4658
4659   /* If we've already decoded this die, this is a no-op. */
4660   if (die->type)
4661     {
4662       return;
4663     }
4664
4665   attr = dwarf2_attr (die, DW_AT_encoding, cu);
4666   if (attr)
4667     {
4668       encoding = DW_UNSND (attr);
4669     }
4670   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4671   if (attr)
4672     {
4673       size = DW_UNSND (attr);
4674     }
4675   attr = dwarf2_attr (die, DW_AT_name, cu);
4676   if (attr && DW_STRING (attr))
4677     {
4678       enum type_code code = TYPE_CODE_INT;
4679       int type_flags = 0;
4680
4681       switch (encoding)
4682         {
4683         case DW_ATE_address:
4684           /* Turn DW_ATE_address into a void * pointer.  */
4685           code = TYPE_CODE_PTR;
4686           type_flags |= TYPE_FLAG_UNSIGNED;
4687           break;
4688         case DW_ATE_boolean:
4689           code = TYPE_CODE_BOOL;
4690           type_flags |= TYPE_FLAG_UNSIGNED;
4691           break;
4692         case DW_ATE_complex_float:
4693           code = TYPE_CODE_COMPLEX;
4694           break;
4695         case DW_ATE_float:
4696           code = TYPE_CODE_FLT;
4697           break;
4698         case DW_ATE_signed:
4699         case DW_ATE_signed_char:
4700           break;
4701         case DW_ATE_unsigned:
4702         case DW_ATE_unsigned_char:
4703           type_flags |= TYPE_FLAG_UNSIGNED;
4704           break;
4705         default:
4706           complaint (&symfile_complaints, "unsupported DW_AT_encoding: '%s'",
4707                      dwarf_type_encoding_name (encoding));
4708           break;
4709         }
4710       type = init_type (code, size, type_flags, DW_STRING (attr), objfile);
4711       if (encoding == DW_ATE_address)
4712         TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID,
4713                                                            cu);
4714       else if (encoding == DW_ATE_complex_float)
4715         {
4716           if (size == 32)
4717             TYPE_TARGET_TYPE (type)
4718               = dwarf2_fundamental_type (objfile, FT_EXT_PREC_FLOAT, cu);
4719           else if (size == 16)
4720             TYPE_TARGET_TYPE (type)
4721               = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
4722           else if (size == 8)
4723             TYPE_TARGET_TYPE (type)
4724               = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
4725         }
4726     }
4727   else
4728     {
4729       type = dwarf_base_type (encoding, size, cu);
4730     }
4731   set_die_type (die, type, cu);
4732 }
4733
4734 /* Read the given DW_AT_subrange DIE.  */
4735
4736 static void
4737 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
4738 {
4739   struct type *base_type;
4740   struct type *range_type;
4741   struct attribute *attr;
4742   int low = 0;
4743   int high = -1;
4744   
4745   /* If we have already decoded this die, then nothing more to do.  */
4746   if (die->type)
4747     return;
4748
4749   base_type = die_type (die, cu);
4750   if (base_type == NULL)
4751     {
4752       complaint (&symfile_complaints,
4753                 "DW_AT_type missing from DW_TAG_subrange_type");
4754       return;
4755     }
4756
4757   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
4758     base_type = alloc_type (NULL);
4759
4760   if (cu->language == language_fortran)
4761     { 
4762       /* FORTRAN implies a lower bound of 1, if not given.  */
4763       low = 1;
4764     }
4765
4766   /* FIXME: For variable sized arrays either of these could be
4767      a variable rather than a constant value.  We'll allow it,
4768      but we don't know how to handle it.  */
4769   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
4770   if (attr)
4771     low = dwarf2_get_attr_constant_value (attr, 0);
4772
4773   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
4774   if (attr)
4775     {       
4776       if (attr->form == DW_FORM_block1)
4777         {
4778           /* GCC encodes arrays with unspecified or dynamic length
4779              with a DW_FORM_block1 attribute.
4780              FIXME: GDB does not yet know how to handle dynamic
4781              arrays properly, treat them as arrays with unspecified
4782              length for now.
4783
4784              FIXME: jimb/2003-09-22: GDB does not really know
4785              how to handle arrays of unspecified length
4786              either; we just represent them as zero-length
4787              arrays.  Choose an appropriate upper bound given
4788              the lower bound we've computed above.  */
4789           high = low - 1;
4790         }
4791       else
4792         high = dwarf2_get_attr_constant_value (attr, 1);
4793     }
4794
4795   range_type = create_range_type (NULL, base_type, low, high);
4796
4797   attr = dwarf2_attr (die, DW_AT_name, cu);
4798   if (attr && DW_STRING (attr))
4799     TYPE_NAME (range_type) = DW_STRING (attr);
4800   
4801   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4802   if (attr)
4803     TYPE_LENGTH (range_type) = DW_UNSND (attr);
4804
4805   set_die_type (die, range_type, cu);
4806 }
4807   
4808
4809 /* Read a whole compilation unit into a linked list of dies.  */
4810
4811 static struct die_info *
4812 read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
4813 {
4814   return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
4815 }
4816
4817 /* Read a single die and all its descendents.  Set the die's sibling
4818    field to NULL; set other fields in the die correctly, and set all
4819    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
4820    location of the info_ptr after reading all of those dies.  PARENT
4821    is the parent of the die in question.  */
4822
4823 static struct die_info *
4824 read_die_and_children (char *info_ptr, bfd *abfd,
4825                        struct dwarf2_cu *cu,
4826                        char **new_info_ptr,
4827                        struct die_info *parent)
4828 {
4829   struct die_info *die;
4830   char *cur_ptr;
4831   int has_children;
4832
4833   cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
4834   store_in_ref_table (die->offset, die, cu);
4835
4836   if (has_children)
4837     {
4838       die->child = read_die_and_siblings (cur_ptr, abfd, cu,
4839                                           new_info_ptr, die);
4840     }
4841   else
4842     {
4843       die->child = NULL;
4844       *new_info_ptr = cur_ptr;
4845     }
4846
4847   die->sibling = NULL;
4848   die->parent = parent;
4849   return die;
4850 }
4851
4852 /* Read a die, all of its descendents, and all of its siblings; set
4853    all of the fields of all of the dies correctly.  Arguments are as
4854    in read_die_and_children.  */
4855
4856 static struct die_info *
4857 read_die_and_siblings (char *info_ptr, bfd *abfd,
4858                        struct dwarf2_cu *cu,
4859                        char **new_info_ptr,
4860                        struct die_info *parent)
4861 {
4862   struct die_info *first_die, *last_sibling;
4863   char *cur_ptr;
4864
4865   cur_ptr = info_ptr;
4866   first_die = last_sibling = NULL;
4867
4868   while (1)
4869     {
4870       struct die_info *die
4871         = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
4872
4873       if (!first_die)
4874         {
4875           first_die = die;
4876         }
4877       else
4878         {
4879           last_sibling->sibling = die;
4880         }
4881
4882       if (die->tag == 0)
4883         {
4884           *new_info_ptr = cur_ptr;
4885           return first_die;
4886         }
4887       else
4888         {
4889           last_sibling = die;
4890         }
4891     }
4892 }
4893
4894 /* Free a linked list of dies.  */
4895
4896 static void
4897 free_die_list (struct die_info *dies)
4898 {
4899   struct die_info *die, *next;
4900
4901   die = dies;
4902   while (die)
4903     {
4904       if (die->child != NULL)
4905         free_die_list (die->child);
4906       next = die->sibling;
4907       xfree (die->attrs);
4908       xfree (die);
4909       die = next;
4910     }
4911 }
4912
4913 /* Read the contents of the section at OFFSET and of size SIZE from the
4914    object file specified by OBJFILE into the objfile_obstack and return it.  */
4915
4916 char *
4917 dwarf2_read_section (struct objfile *objfile, asection *sectp)
4918 {
4919   bfd *abfd = objfile->obfd;
4920   char *buf, *retbuf;
4921   bfd_size_type size = bfd_get_section_size (sectp);
4922
4923   if (size == 0)
4924     return NULL;
4925
4926   buf = (char *) obstack_alloc (&objfile->objfile_obstack, size);
4927   retbuf
4928     = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
4929   if (retbuf != NULL)
4930     return retbuf;
4931
4932   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
4933       || bfd_bread (buf, size, abfd) != size)
4934     error ("Dwarf Error: Can't read DWARF data from '%s'",
4935            bfd_get_filename (abfd));
4936
4937   return buf;
4938 }
4939
4940 /* In DWARF version 2, the description of the debugging information is
4941    stored in a separate .debug_abbrev section.  Before we read any
4942    dies from a section we read in all abbreviations and install them
4943    in a hash table.  This function also sets flags in CU describing
4944    the data found in the abbrev table.  */
4945
4946 static void
4947 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
4948 {
4949   struct comp_unit_head *cu_header = &cu->header;
4950   char *abbrev_ptr;
4951   struct abbrev_info *cur_abbrev;
4952   unsigned int abbrev_number, bytes_read, abbrev_name;
4953   unsigned int abbrev_form, hash_number;
4954   struct attr_abbrev *cur_attrs;
4955   unsigned int allocated_attrs;
4956
4957   /* Initialize dwarf2 abbrevs */
4958   obstack_init (&cu->abbrev_obstack);
4959   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
4960                                       (ABBREV_HASH_SIZE
4961                                        * sizeof (struct abbrev_info *)));
4962   memset (cu->dwarf2_abbrevs, 0,
4963           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
4964
4965   abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
4966   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4967   abbrev_ptr += bytes_read;
4968
4969   allocated_attrs = ATTR_ALLOC_CHUNK;
4970   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
4971   
4972   /* loop until we reach an abbrev number of 0 */
4973   while (abbrev_number)
4974     {
4975       cur_abbrev = dwarf_alloc_abbrev (cu);
4976
4977       /* read in abbrev header */
4978       cur_abbrev->number = abbrev_number;
4979       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4980       abbrev_ptr += bytes_read;
4981       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
4982       abbrev_ptr += 1;
4983
4984       if (cur_abbrev->tag == DW_TAG_namespace)
4985         cu->has_namespace_info = 1;
4986
4987       /* now read in declarations */
4988       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4989       abbrev_ptr += bytes_read;
4990       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4991       abbrev_ptr += bytes_read;
4992       while (abbrev_name)
4993         {
4994           if (cur_abbrev->num_attrs == allocated_attrs)
4995             {
4996               allocated_attrs += ATTR_ALLOC_CHUNK;
4997               cur_attrs
4998                 = xrealloc (cur_attrs, (allocated_attrs
4999                                         * sizeof (struct attr_abbrev)));
5000             }
5001
5002           /* Record whether this compilation unit might have
5003              inter-compilation-unit references.  If we don't know what form
5004              this attribute will have, then it might potentially be a
5005              DW_FORM_ref_addr, so we conservatively expect inter-CU
5006              references.  */
5007
5008           if (abbrev_form == DW_FORM_ref_addr
5009               || abbrev_form == DW_FORM_indirect)
5010             cu->has_form_ref_addr = 1;
5011
5012           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
5013           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
5014           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5015           abbrev_ptr += bytes_read;
5016           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5017           abbrev_ptr += bytes_read;
5018         }
5019
5020       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
5021                                          (cur_abbrev->num_attrs
5022                                           * sizeof (struct attr_abbrev)));
5023       memcpy (cur_abbrev->attrs, cur_attrs,
5024               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
5025
5026       hash_number = abbrev_number % ABBREV_HASH_SIZE;
5027       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
5028       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
5029
5030       /* Get next abbreviation.
5031          Under Irix6 the abbreviations for a compilation unit are not
5032          always properly terminated with an abbrev number of 0.
5033          Exit loop if we encounter an abbreviation which we have
5034          already read (which means we are about to read the abbreviations
5035          for the next compile unit) or if the end of the abbreviation
5036          table is reached.  */
5037       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
5038           >= dwarf2_per_objfile->abbrev_size)
5039         break;
5040       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5041       abbrev_ptr += bytes_read;
5042       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
5043         break;
5044     }
5045
5046   xfree (cur_attrs);
5047 }
5048
5049 /* Release the memory used by the abbrev table for a compilation unit.  */
5050
5051 static void
5052 dwarf2_free_abbrev_table (void *ptr_to_cu)
5053 {
5054   struct dwarf2_cu *cu = ptr_to_cu;
5055
5056   obstack_free (&cu->abbrev_obstack, NULL);
5057   cu->dwarf2_abbrevs = NULL;
5058 }
5059
5060 /* Lookup an abbrev_info structure in the abbrev hash table.  */
5061
5062 static struct abbrev_info *
5063 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
5064 {
5065   unsigned int hash_number;
5066   struct abbrev_info *abbrev;
5067
5068   hash_number = number % ABBREV_HASH_SIZE;
5069   abbrev = cu->dwarf2_abbrevs[hash_number];
5070
5071   while (abbrev)
5072     {
5073       if (abbrev->number == number)
5074         return abbrev;
5075       else
5076         abbrev = abbrev->next;
5077     }
5078   return NULL;
5079 }
5080
5081 /* Returns nonzero if TAG represents a type that we might generate a partial
5082    symbol for.  */
5083
5084 static int
5085 is_type_tag_for_partial (int tag)
5086 {
5087   switch (tag)
5088     {
5089 #if 0
5090     /* Some types that would be reasonable to generate partial symbols for,
5091        that we don't at present.  */
5092     case DW_TAG_array_type:
5093     case DW_TAG_file_type:
5094     case DW_TAG_ptr_to_member_type:
5095     case DW_TAG_set_type:
5096     case DW_TAG_string_type:
5097     case DW_TAG_subroutine_type:
5098 #endif
5099     case DW_TAG_base_type:
5100     case DW_TAG_class_type:
5101     case DW_TAG_enumeration_type:
5102     case DW_TAG_structure_type:
5103     case DW_TAG_subrange_type:
5104     case DW_TAG_typedef:
5105     case DW_TAG_union_type:
5106       return 1;
5107     default:
5108       return 0;
5109     }
5110 }
5111
5112 /* Load all DIEs that are interesting for partial symbols into memory.  */
5113
5114 static struct partial_die_info *
5115 load_partial_dies (bfd *abfd, char *info_ptr, int building_psymtab,
5116                    struct dwarf2_cu *cu)
5117 {
5118   struct partial_die_info *part_die;
5119   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
5120   struct abbrev_info *abbrev;
5121   unsigned int bytes_read;
5122
5123   int nesting_level = 1;
5124
5125   parent_die = NULL;
5126   last_die = NULL;
5127
5128   cu->partial_dies
5129     = htab_create_alloc_ex (cu->header.length / 12,
5130                             partial_die_hash,
5131                             partial_die_eq,
5132                             NULL,
5133                             &cu->comp_unit_obstack,
5134                             hashtab_obstack_allocate,
5135                             dummy_obstack_deallocate);
5136
5137   part_die = obstack_alloc (&cu->comp_unit_obstack,
5138                             sizeof (struct partial_die_info));
5139
5140   while (1)
5141     {
5142       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5143
5144       /* A NULL abbrev means the end of a series of children.  */
5145       if (abbrev == NULL)
5146         {
5147           if (--nesting_level == 0)
5148             {
5149               /* PART_DIE was probably the last thing allocated on the
5150                  comp_unit_obstack, so we could call obstack_free
5151                  here.  We don't do that because the waste is small,
5152                  and will be cleaned up when we're done with this
5153                  compilation unit.  This way, we're also more robust
5154                  against other users of the comp_unit_obstack.  */
5155               return first_die;
5156             }
5157           info_ptr += bytes_read;
5158           last_die = parent_die;
5159           parent_die = parent_die->die_parent;
5160           continue;
5161         }
5162
5163       /* Check whether this DIE is interesting enough to save.  */
5164       if (!is_type_tag_for_partial (abbrev->tag)
5165           && abbrev->tag != DW_TAG_enumerator
5166           && abbrev->tag != DW_TAG_subprogram
5167           && abbrev->tag != DW_TAG_variable
5168           && abbrev->tag != DW_TAG_namespace)
5169         {
5170           /* Otherwise we skip to the next sibling, if any.  */
5171           info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5172           continue;
5173         }
5174
5175       info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5176                                    abfd, info_ptr, cu);
5177
5178       /* This two-pass algorithm for processing partial symbols has a
5179          high cost in cache pressure.  Thus, handle some simple cases
5180          here which cover the majority of C partial symbols.  DIEs
5181          which neither have specification tags in them, nor could have
5182          specification tags elsewhere pointing at them, can simply be
5183          processed and discarded.
5184
5185          This segment is also optional; scan_partial_symbols and
5186          add_partial_symbol will handle these DIEs if we chain
5187          them in normally.  When compilers which do not emit large
5188          quantities of duplicate debug information are more common,
5189          this code can probably be removed.  */
5190
5191       /* Any complete simple types at the top level (pretty much all
5192          of them, for a language without namespaces), can be processed
5193          directly.  */
5194       if (parent_die == NULL
5195           && part_die->has_specification == 0
5196           && part_die->is_declaration == 0
5197           && (part_die->tag == DW_TAG_typedef
5198               || part_die->tag == DW_TAG_base_type
5199               || part_die->tag == DW_TAG_subrange_type))
5200         {
5201           if (building_psymtab && part_die->name != NULL)
5202             add_psymbol_to_list (part_die->name, strlen (part_die->name),
5203                                  VAR_DOMAIN, LOC_TYPEDEF,
5204                                  &cu->objfile->static_psymbols,
5205                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
5206           info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5207           continue;
5208         }
5209
5210       /* If we're at the second level, and we're an enumerator, and
5211          our parent has no specification (meaning possibly lives in a
5212          namespace elsewhere), then we can add the partial symbol now
5213          instead of queueing it.  */
5214       if (part_die->tag == DW_TAG_enumerator
5215           && parent_die != NULL
5216           && parent_die->die_parent == NULL
5217           && parent_die->tag == DW_TAG_enumeration_type
5218           && parent_die->has_specification == 0)
5219         {
5220           if (part_die->name == NULL)
5221             complaint (&symfile_complaints, "malformed enumerator DIE ignored");
5222           else if (building_psymtab)
5223             add_psymbol_to_list (part_die->name, strlen (part_die->name),
5224                                  VAR_DOMAIN, LOC_CONST,
5225                                  (cu->language == language_cplus
5226                                   || cu->language == language_java)
5227                                  ? &cu->objfile->global_psymbols
5228                                  : &cu->objfile->static_psymbols,
5229                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
5230
5231           info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5232           continue;
5233         }
5234
5235       /* We'll save this DIE so link it in.  */
5236       part_die->die_parent = parent_die;
5237       part_die->die_sibling = NULL;
5238       part_die->die_child = NULL;
5239
5240       if (last_die && last_die == parent_die)
5241         last_die->die_child = part_die;
5242       else if (last_die)
5243         last_die->die_sibling = part_die;
5244
5245       last_die = part_die;
5246
5247       if (first_die == NULL)
5248         first_die = part_die;
5249
5250       /* Maybe add the DIE to the hash table.  Not all DIEs that we
5251          find interesting need to be in the hash table, because we
5252          also have the parent/sibling/child chains; only those that we
5253          might refer to by offset later during partial symbol reading.
5254
5255          For now this means things that might have be the target of a
5256          DW_AT_specification, DW_AT_abstract_origin, or
5257          DW_AT_extension.  DW_AT_extension will refer only to
5258          namespaces; DW_AT_abstract_origin refers to functions (and
5259          many things under the function DIE, but we do not recurse
5260          into function DIEs during partial symbol reading) and
5261          possibly variables as well; DW_AT_specification refers to
5262          declarations.  Declarations ought to have the DW_AT_declaration
5263          flag.  It happens that GCC forgets to put it in sometimes, but
5264          only for functions, not for types.
5265
5266          Adding more things than necessary to the hash table is harmless
5267          except for the performance cost.  Adding too few will result in
5268          internal errors in find_partial_die.  */
5269
5270       if (abbrev->tag == DW_TAG_subprogram
5271           || abbrev->tag == DW_TAG_variable
5272           || abbrev->tag == DW_TAG_namespace
5273           || part_die->is_declaration)
5274         {
5275           void **slot;
5276
5277           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5278                                            part_die->offset, INSERT);
5279           *slot = part_die;
5280         }
5281
5282       part_die = obstack_alloc (&cu->comp_unit_obstack,
5283                                 sizeof (struct partial_die_info));
5284
5285       /* For some DIEs we want to follow their children (if any).  For C
5286          we have no reason to follow the children of structures; for other
5287          languages we have to, both so that we can get at method physnames
5288          to infer fully qualified class names, and for DW_AT_specification.  */
5289       if (last_die->has_children
5290           && (last_die->tag == DW_TAG_namespace
5291               || last_die->tag == DW_TAG_enumeration_type
5292               || (cu->language != language_c
5293                   && (last_die->tag == DW_TAG_class_type
5294                       || last_die->tag == DW_TAG_structure_type
5295                       || last_die->tag == DW_TAG_union_type))))
5296         {
5297           nesting_level++;
5298           parent_die = last_die;
5299           continue;
5300         }
5301
5302       /* Otherwise we skip to the next sibling, if any.  */
5303       info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5304
5305       /* Back to the top, do it again.  */
5306     }
5307 }
5308
5309 /* Read a minimal amount of information into the minimal die structure.  */
5310
5311 static char *
5312 read_partial_die (struct partial_die_info *part_die,
5313                   struct abbrev_info *abbrev,
5314                   unsigned int abbrev_len, bfd *abfd,
5315                   char *info_ptr, struct dwarf2_cu *cu)
5316 {
5317   unsigned int bytes_read, i;
5318   struct attribute attr;
5319   int has_low_pc_attr = 0;
5320   int has_high_pc_attr = 0;
5321
5322   memset (part_die, 0, sizeof (struct partial_die_info));
5323
5324   part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
5325
5326   info_ptr += abbrev_len;
5327
5328   if (abbrev == NULL)
5329     return info_ptr;
5330
5331   part_die->tag = abbrev->tag;
5332   part_die->has_children = abbrev->has_children;
5333
5334   for (i = 0; i < abbrev->num_attrs; ++i)
5335     {
5336       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
5337
5338       /* Store the data if it is of an attribute we want to keep in a
5339          partial symbol table.  */
5340       switch (attr.name)
5341         {
5342         case DW_AT_name:
5343
5344           /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
5345           if (part_die->name == NULL)
5346             part_die->name = DW_STRING (&attr);
5347           break;
5348         case DW_AT_comp_dir:
5349           if (part_die->dirname == NULL)
5350             part_die->dirname = DW_STRING (&attr);
5351           break;
5352         case DW_AT_MIPS_linkage_name:
5353           part_die->name = DW_STRING (&attr);
5354           break;
5355         case DW_AT_low_pc:
5356           has_low_pc_attr = 1;
5357           part_die->lowpc = DW_ADDR (&attr);
5358           break;
5359         case DW_AT_high_pc:
5360           has_high_pc_attr = 1;
5361           part_die->highpc = DW_ADDR (&attr);
5362           break;
5363         case DW_AT_location:
5364           /* Support the .debug_loc offsets */
5365           if (attr_form_is_block (&attr))
5366             {
5367                part_die->locdesc = DW_BLOCK (&attr);
5368             }
5369           else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
5370             {
5371               dwarf2_complex_location_expr_complaint ();
5372             }
5373           else
5374             {
5375               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5376                                                      "partial symbol information");
5377             }
5378           break;
5379         case DW_AT_language:
5380           part_die->language = DW_UNSND (&attr);
5381           break;
5382         case DW_AT_external:
5383           part_die->is_external = DW_UNSND (&attr);
5384           break;
5385         case DW_AT_declaration:
5386           part_die->is_declaration = DW_UNSND (&attr);
5387           break;
5388         case DW_AT_type:
5389           part_die->has_type = 1;
5390           break;
5391         case DW_AT_abstract_origin:
5392         case DW_AT_specification:
5393         case DW_AT_extension:
5394           part_die->has_specification = 1;
5395           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr, cu);
5396           break;
5397         case DW_AT_sibling:
5398           /* Ignore absolute siblings, they might point outside of
5399              the current compile unit.  */
5400           if (attr.form == DW_FORM_ref_addr)
5401             complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
5402           else
5403             part_die->sibling = dwarf2_per_objfile->info_buffer
5404               + dwarf2_get_ref_die_offset (&attr, cu);
5405           break;
5406         case DW_AT_stmt_list:
5407           part_die->has_stmt_list = 1;
5408           part_die->line_offset = DW_UNSND (&attr);
5409           break;
5410         default:
5411           break;
5412         }
5413     }
5414
5415   /* When using the GNU linker, .gnu.linkonce. sections are used to
5416      eliminate duplicate copies of functions and vtables and such.
5417      The linker will arbitrarily choose one and discard the others.
5418      The AT_*_pc values for such functions refer to local labels in
5419      these sections.  If the section from that file was discarded, the
5420      labels are not in the output, so the relocs get a value of 0.
5421      If this is a discarded function, mark the pc bounds as invalid,
5422      so that GDB will ignore it.  */
5423   if (has_low_pc_attr && has_high_pc_attr
5424       && part_die->lowpc < part_die->highpc
5425       && (part_die->lowpc != 0
5426           || (bfd_get_file_flags (abfd) & HAS_RELOC)))
5427     part_die->has_pc_info = 1;
5428   return info_ptr;
5429 }
5430
5431 /* Find a cached partial DIE at OFFSET in CU.  */
5432
5433 static struct partial_die_info *
5434 find_partial_die_in_comp_unit (unsigned long offset, struct dwarf2_cu *cu)
5435 {
5436   struct partial_die_info *lookup_die = NULL;
5437   struct partial_die_info part_die;
5438
5439   part_die.offset = offset;
5440   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
5441
5442   if (lookup_die == NULL)
5443     internal_error (__FILE__, __LINE__,
5444                     "could not find partial DIE in cache\n");
5445
5446   return lookup_die;
5447 }
5448
5449 /* Find a partial DIE at OFFSET, which may or may not be in CU.  */
5450
5451 static struct partial_die_info *
5452 find_partial_die (unsigned long offset, struct dwarf2_cu *cu)
5453 {
5454   struct dwarf2_per_cu_data *per_cu;
5455
5456   if (offset >= cu->header.offset
5457       && offset < cu->header.offset + cu->header.length)
5458     return find_partial_die_in_comp_unit (offset, cu);
5459
5460   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5461
5462   if (per_cu->cu == NULL)
5463     {
5464       load_comp_unit (per_cu, cu->objfile);
5465       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5466       dwarf2_per_objfile->read_in_chain = per_cu;
5467     }
5468
5469   per_cu->cu->last_used = 0;
5470   return find_partial_die_in_comp_unit (offset, per_cu->cu);
5471 }
5472
5473 /* Adjust PART_DIE before generating a symbol for it.  This function
5474    may set the is_external flag or change the DIE's name.  */
5475
5476 static void
5477 fixup_partial_die (struct partial_die_info *part_die,
5478                    struct dwarf2_cu *cu)
5479 {
5480   /* If we found a reference attribute and the DIE has no name, try
5481      to find a name in the referred to DIE.  */
5482
5483   if (part_die->name == NULL && part_die->has_specification)
5484     {
5485       struct partial_die_info *spec_die;
5486
5487       spec_die = find_partial_die (part_die->spec_offset, cu);
5488
5489       fixup_partial_die (spec_die, cu);
5490
5491       if (spec_die->name)
5492         {
5493           part_die->name = spec_die->name;
5494
5495           /* Copy DW_AT_external attribute if it is set.  */
5496           if (spec_die->is_external)
5497             part_die->is_external = spec_die->is_external;
5498         }
5499     }
5500
5501   /* Set default names for some unnamed DIEs.  */
5502   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
5503                                  || part_die->tag == DW_TAG_class_type))
5504     part_die->name = "(anonymous class)";
5505
5506   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
5507     part_die->name = "(anonymous namespace)";
5508
5509   if (part_die->tag == DW_TAG_structure_type
5510       || part_die->tag == DW_TAG_class_type
5511       || part_die->tag == DW_TAG_union_type)
5512     guess_structure_name (part_die, cu);
5513 }
5514
5515 /* Read the die from the .debug_info section buffer.  Set DIEP to
5516    point to a newly allocated die with its information, except for its
5517    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
5518    whether the die has children or not.  */
5519
5520 static char *
5521 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
5522                struct dwarf2_cu *cu, int *has_children)
5523 {
5524   unsigned int abbrev_number, bytes_read, i, offset;
5525   struct abbrev_info *abbrev;
5526   struct die_info *die;
5527
5528   offset = info_ptr - dwarf2_per_objfile->info_buffer;
5529   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5530   info_ptr += bytes_read;
5531   if (!abbrev_number)
5532     {
5533       die = dwarf_alloc_die ();
5534       die->tag = 0;
5535       die->abbrev = abbrev_number;
5536       die->type = NULL;
5537       *diep = die;
5538       *has_children = 0;
5539       return info_ptr;
5540     }
5541
5542   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5543   if (!abbrev)
5544     {
5545       error ("Dwarf Error: could not find abbrev number %d [in module %s]",
5546              abbrev_number,
5547              bfd_get_filename (abfd));
5548     }
5549   die = dwarf_alloc_die ();
5550   die->offset = offset;
5551   die->tag = abbrev->tag;
5552   die->abbrev = abbrev_number;
5553   die->type = NULL;
5554
5555   die->num_attrs = abbrev->num_attrs;
5556   die->attrs = (struct attribute *)
5557     xmalloc (die->num_attrs * sizeof (struct attribute));
5558
5559   for (i = 0; i < abbrev->num_attrs; ++i)
5560     {
5561       info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
5562                                  abfd, info_ptr, cu);
5563
5564       /* If this attribute is an absolute reference to a different
5565          compilation unit, make sure that compilation unit is loaded
5566          also.  */
5567       if (die->attrs[i].form == DW_FORM_ref_addr
5568           && (DW_ADDR (&die->attrs[i]) < cu->header.offset
5569               || (DW_ADDR (&die->attrs[i])
5570                   >= cu->header.offset + cu->header.length)))
5571         {
5572           struct dwarf2_per_cu_data *per_cu;
5573           per_cu = dwarf2_find_containing_comp_unit (DW_ADDR (&die->attrs[i]),
5574                                                      cu->objfile);
5575
5576           /* Mark the dependence relation so that we don't flush PER_CU
5577              too early.  */
5578           dwarf2_add_dependence (cu, per_cu);
5579
5580           /* If it's already on the queue, we have nothing to do.  */
5581           if (per_cu->queued)
5582             continue;
5583
5584           /* If the compilation unit is already loaded, just mark it as
5585              used.  */
5586           if (per_cu->cu != NULL)
5587             {
5588               per_cu->cu->last_used = 0;
5589               continue;
5590             }
5591
5592           /* Add it to the queue.  */
5593           queue_comp_unit (per_cu);
5594        }
5595     }
5596
5597   *diep = die;
5598   *has_children = abbrev->has_children;
5599   return info_ptr;
5600 }
5601
5602 /* Read an attribute value described by an attribute form.  */
5603
5604 static char *
5605 read_attribute_value (struct attribute *attr, unsigned form,
5606                       bfd *abfd, char *info_ptr,
5607                       struct dwarf2_cu *cu)
5608 {
5609   struct comp_unit_head *cu_header = &cu->header;
5610   unsigned int bytes_read;
5611   struct dwarf_block *blk;
5612
5613   attr->form = form;
5614   switch (form)
5615     {
5616     case DW_FORM_addr:
5617     case DW_FORM_ref_addr:
5618       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
5619       info_ptr += bytes_read;
5620       break;
5621     case DW_FORM_block2:
5622       blk = dwarf_alloc_block (cu);
5623       blk->size = read_2_bytes (abfd, info_ptr);
5624       info_ptr += 2;
5625       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5626       info_ptr += blk->size;
5627       DW_BLOCK (attr) = blk;
5628       break;
5629     case DW_FORM_block4:
5630       blk = dwarf_alloc_block (cu);
5631       blk->size = read_4_bytes (abfd, info_ptr);
5632       info_ptr += 4;
5633       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5634       info_ptr += blk->size;
5635       DW_BLOCK (attr) = blk;
5636       break;
5637     case DW_FORM_data2:
5638       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
5639       info_ptr += 2;
5640       break;
5641     case DW_FORM_data4:
5642       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
5643       info_ptr += 4;
5644       break;
5645     case DW_FORM_data8:
5646       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
5647       info_ptr += 8;
5648       break;
5649     case DW_FORM_string:
5650       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
5651       info_ptr += bytes_read;
5652       break;
5653     case DW_FORM_strp:
5654       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
5655                                                &bytes_read);
5656       info_ptr += bytes_read;
5657       break;
5658     case DW_FORM_block:
5659       blk = dwarf_alloc_block (cu);
5660       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5661       info_ptr += bytes_read;
5662       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5663       info_ptr += blk->size;
5664       DW_BLOCK (attr) = blk;
5665       break;
5666     case DW_FORM_block1:
5667       blk = dwarf_alloc_block (cu);
5668       blk->size = read_1_byte (abfd, info_ptr);
5669       info_ptr += 1;
5670       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5671       info_ptr += blk->size;
5672       DW_BLOCK (attr) = blk;
5673       break;
5674     case DW_FORM_data1:
5675       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5676       info_ptr += 1;
5677       break;
5678     case DW_FORM_flag:
5679       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5680       info_ptr += 1;
5681       break;
5682     case DW_FORM_sdata:
5683       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
5684       info_ptr += bytes_read;
5685       break;
5686     case DW_FORM_udata:
5687       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5688       info_ptr += bytes_read;
5689       break;
5690     case DW_FORM_ref1:
5691       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
5692       info_ptr += 1;
5693       break;
5694     case DW_FORM_ref2:
5695       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
5696       info_ptr += 2;
5697       break;
5698     case DW_FORM_ref4:
5699       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
5700       info_ptr += 4;
5701       break;
5702     case DW_FORM_ref8:
5703       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
5704       info_ptr += 8;
5705       break;
5706     case DW_FORM_ref_udata:
5707       DW_ADDR (attr) = (cu->header.offset
5708                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
5709       info_ptr += bytes_read;
5710       break;
5711     case DW_FORM_indirect:
5712       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5713       info_ptr += bytes_read;
5714       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
5715       break;
5716     default:
5717       error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
5718              dwarf_form_name (form),
5719              bfd_get_filename (abfd));
5720     }
5721   return info_ptr;
5722 }
5723
5724 /* Read an attribute described by an abbreviated attribute.  */
5725
5726 static char *
5727 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
5728                 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
5729 {
5730   attr->name = abbrev->name;
5731   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
5732 }
5733
5734 /* read dwarf information from a buffer */
5735
5736 static unsigned int
5737 read_1_byte (bfd *abfd, char *buf)
5738 {
5739   return bfd_get_8 (abfd, (bfd_byte *) buf);
5740 }
5741
5742 static int
5743 read_1_signed_byte (bfd *abfd, char *buf)
5744 {
5745   return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
5746 }
5747
5748 static unsigned int
5749 read_2_bytes (bfd *abfd, char *buf)
5750 {
5751   return bfd_get_16 (abfd, (bfd_byte *) buf);
5752 }
5753
5754 static int
5755 read_2_signed_bytes (bfd *abfd, char *buf)
5756 {
5757   return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
5758 }
5759
5760 static unsigned int
5761 read_4_bytes (bfd *abfd, char *buf)
5762 {
5763   return bfd_get_32 (abfd, (bfd_byte *) buf);
5764 }
5765
5766 static int
5767 read_4_signed_bytes (bfd *abfd, char *buf)
5768 {
5769   return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
5770 }
5771
5772 static unsigned long
5773 read_8_bytes (bfd *abfd, char *buf)
5774 {
5775   return bfd_get_64 (abfd, (bfd_byte *) buf);
5776 }
5777
5778 static CORE_ADDR
5779 read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
5780 {
5781   struct comp_unit_head *cu_header = &cu->header;
5782   CORE_ADDR retval = 0;
5783
5784   if (cu_header->signed_addr_p)
5785     {
5786       switch (cu_header->addr_size)
5787         {
5788         case 2:
5789           retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
5790           break;
5791         case 4:
5792           retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
5793           break;
5794         case 8:
5795           retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
5796           break;
5797         default:
5798           internal_error (__FILE__, __LINE__,
5799                           "read_address: bad switch, signed [in module %s]",
5800                           bfd_get_filename (abfd));
5801         }
5802     }
5803   else
5804     {
5805       switch (cu_header->addr_size)
5806         {
5807         case 2:
5808           retval = bfd_get_16 (abfd, (bfd_byte *) buf);
5809           break;
5810         case 4:
5811           retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5812           break;
5813         case 8:
5814           retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5815           break;
5816         default:
5817           internal_error (__FILE__, __LINE__,
5818                           "read_address: bad switch, unsigned [in module %s]",
5819                           bfd_get_filename (abfd));
5820         }
5821     }
5822
5823   *bytes_read = cu_header->addr_size;
5824   return retval;
5825 }
5826
5827 /* Read the initial length from a section.  The (draft) DWARF 3
5828    specification allows the initial length to take up either 4 bytes
5829    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
5830    bytes describe the length and all offsets will be 8 bytes in length
5831    instead of 4.
5832
5833    An older, non-standard 64-bit format is also handled by this
5834    function.  The older format in question stores the initial length
5835    as an 8-byte quantity without an escape value.  Lengths greater
5836    than 2^32 aren't very common which means that the initial 4 bytes
5837    is almost always zero.  Since a length value of zero doesn't make
5838    sense for the 32-bit format, this initial zero can be considered to
5839    be an escape value which indicates the presence of the older 64-bit
5840    format.  As written, the code can't detect (old format) lengths
5841    greater than 4GB.  If it becomes necessary to handle lengths somewhat
5842    larger than 4GB, we could allow other small values (such as the
5843    non-sensical values of 1, 2, and 3) to also be used as escape values
5844    indicating the presence of the old format.
5845
5846    The value returned via bytes_read should be used to increment
5847    the relevant pointer after calling read_initial_length().
5848    
5849    As a side effect, this function sets the fields initial_length_size
5850    and offset_size in cu_header to the values appropriate for the
5851    length field.  (The format of the initial length field determines
5852    the width of file offsets to be fetched later with fetch_offset().)
5853    
5854    [ Note:  read_initial_length() and read_offset() are based on the
5855      document entitled "DWARF Debugging Information Format", revision
5856      3, draft 8, dated November 19, 2001.  This document was obtained
5857      from:
5858
5859         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
5860      
5861      This document is only a draft and is subject to change.  (So beware.)
5862
5863      Details regarding the older, non-standard 64-bit format were
5864      determined empirically by examining 64-bit ELF files produced
5865      by the SGI toolchain on an IRIX 6.5 machine.
5866
5867      - Kevin, July 16, 2002
5868    ] */
5869
5870 static LONGEST
5871 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
5872                      int *bytes_read)
5873 {
5874   LONGEST retval = 0;
5875
5876   retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5877
5878   if (retval == 0xffffffff)
5879     {
5880       retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
5881       *bytes_read = 12;
5882       if (cu_header != NULL)
5883         {
5884           cu_header->initial_length_size = 12;
5885           cu_header->offset_size = 8;
5886         }
5887     }
5888   else if (retval == 0)
5889     {
5890       /* Handle (non-standard) 64-bit DWARF2 formats such as that used
5891          by IRIX.  */
5892       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5893       *bytes_read = 8;
5894       if (cu_header != NULL)
5895         {
5896           cu_header->initial_length_size = 8;
5897           cu_header->offset_size = 8;
5898         }
5899     }
5900   else
5901     {
5902       *bytes_read = 4;
5903       if (cu_header != NULL)
5904         {
5905           cu_header->initial_length_size = 4;
5906           cu_header->offset_size = 4;
5907         }
5908     }
5909
5910  return retval;
5911 }
5912
5913 /* Read an offset from the data stream.  The size of the offset is
5914    given by cu_header->offset_size. */
5915
5916 static LONGEST
5917 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
5918              int *bytes_read)
5919 {
5920   LONGEST retval = 0;
5921
5922   switch (cu_header->offset_size)
5923     {
5924     case 4:
5925       retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5926       *bytes_read = 4;
5927       break;
5928     case 8:
5929       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5930       *bytes_read = 8;
5931       break;
5932     default:
5933       internal_error (__FILE__, __LINE__,
5934                       "read_offset: bad switch [in module %s]",
5935                       bfd_get_filename (abfd));
5936     }
5937
5938  return retval;
5939 }
5940
5941 static char *
5942 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
5943 {
5944   /* If the size of a host char is 8 bits, we can return a pointer
5945      to the buffer, otherwise we have to copy the data to a buffer
5946      allocated on the temporary obstack.  */
5947   gdb_assert (HOST_CHAR_BIT == 8);
5948   return buf;
5949 }
5950
5951 static char *
5952 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5953 {
5954   /* If the size of a host char is 8 bits, we can return a pointer
5955      to the string, otherwise we have to copy the string to a buffer
5956      allocated on the temporary obstack.  */
5957   gdb_assert (HOST_CHAR_BIT == 8);
5958   if (*buf == '\0')
5959     {
5960       *bytes_read_ptr = 1;
5961       return NULL;
5962     }
5963   *bytes_read_ptr = strlen (buf) + 1;
5964   return buf;
5965 }
5966
5967 static char *
5968 read_indirect_string (bfd *abfd, char *buf,
5969                       const struct comp_unit_head *cu_header,
5970                       unsigned int *bytes_read_ptr)
5971 {
5972   LONGEST str_offset = read_offset (abfd, buf, cu_header,
5973                                     (int *) bytes_read_ptr);
5974
5975   if (dwarf2_per_objfile->str_buffer == NULL)
5976     {
5977       error ("DW_FORM_strp used without .debug_str section [in module %s]",
5978                       bfd_get_filename (abfd));
5979       return NULL;
5980     }
5981   if (str_offset >= dwarf2_per_objfile->str_size)
5982     {
5983       error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
5984                       bfd_get_filename (abfd));
5985       return NULL;
5986     }
5987   gdb_assert (HOST_CHAR_BIT == 8);
5988   if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
5989     return NULL;
5990   return dwarf2_per_objfile->str_buffer + str_offset;
5991 }
5992
5993 static unsigned long
5994 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5995 {
5996   unsigned long result;
5997   unsigned int num_read;
5998   int i, shift;
5999   unsigned char byte;
6000
6001   result = 0;
6002   shift = 0;
6003   num_read = 0;
6004   i = 0;
6005   while (1)
6006     {
6007       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
6008       buf++;
6009       num_read++;
6010       result |= ((unsigned long)(byte & 127) << shift);
6011       if ((byte & 128) == 0)
6012         {
6013           break;
6014         }
6015       shift += 7;
6016     }
6017   *bytes_read_ptr = num_read;
6018   return result;
6019 }
6020
6021 static long
6022 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
6023 {
6024   long result;
6025   int i, shift, size, num_read;
6026   unsigned char byte;
6027
6028   result = 0;
6029   shift = 0;
6030   size = 32;
6031   num_read = 0;
6032   i = 0;
6033   while (1)
6034     {
6035       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
6036       buf++;
6037       num_read++;
6038       result |= ((long)(byte & 127) << shift);
6039       shift += 7;
6040       if ((byte & 128) == 0)
6041         {
6042           break;
6043         }
6044     }
6045   if ((shift < size) && (byte & 0x40))
6046     {
6047       result |= -(1 << shift);
6048     }
6049   *bytes_read_ptr = num_read;
6050   return result;
6051 }
6052
6053 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
6054
6055 static char *
6056 skip_leb128 (bfd *abfd, char *buf)
6057 {
6058   int byte;
6059
6060   while (1)
6061     {
6062       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
6063       buf++;
6064       if ((byte & 128) == 0)
6065         return buf;
6066     }
6067 }
6068
6069 static void
6070 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
6071 {
6072   switch (lang)
6073     {
6074     case DW_LANG_C89:
6075     case DW_LANG_C:
6076       cu->language = language_c;
6077       break;
6078     case DW_LANG_C_plus_plus:
6079       cu->language = language_cplus;
6080       break;
6081     case DW_LANG_Fortran77:
6082     case DW_LANG_Fortran90:
6083     case DW_LANG_Fortran95:
6084       cu->language = language_fortran;
6085       break;
6086     case DW_LANG_Mips_Assembler:
6087       cu->language = language_asm;
6088       break;
6089     case DW_LANG_Java:
6090       cu->language = language_java;
6091       break;
6092     case DW_LANG_Ada83:
6093     case DW_LANG_Ada95:
6094       cu->language = language_ada;
6095       break;
6096     case DW_LANG_Cobol74:
6097     case DW_LANG_Cobol85:
6098     case DW_LANG_Pascal83:
6099     case DW_LANG_Modula2:
6100     default:
6101       cu->language = language_minimal;
6102       break;
6103     }
6104   cu->language_defn = language_def (cu->language);
6105 }
6106
6107 /* Return the named attribute or NULL if not there.  */
6108
6109 static struct attribute *
6110 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
6111 {
6112   unsigned int i;
6113   struct attribute *spec = NULL;
6114
6115   for (i = 0; i < die->num_attrs; ++i)
6116     {
6117       if (die->attrs[i].name == name)
6118         return &die->attrs[i];
6119       if (die->attrs[i].name == DW_AT_specification
6120           || die->attrs[i].name == DW_AT_abstract_origin)
6121         spec = &die->attrs[i];
6122     }
6123
6124   if (spec)
6125     return dwarf2_attr (follow_die_ref (die, spec, cu), name, cu);
6126
6127   return NULL;
6128 }
6129
6130 /* Return non-zero iff the attribute NAME is defined for the given DIE,
6131    and holds a non-zero value.  This function should only be used for
6132    DW_FORM_flag attributes.  */
6133
6134 static int
6135 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
6136 {
6137   struct attribute *attr = dwarf2_attr (die, name, cu);
6138
6139   return (attr && DW_UNSND (attr));
6140 }
6141
6142 static int
6143 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
6144 {
6145   /* A DIE is a declaration if it has a DW_AT_declaration attribute
6146      which value is non-zero.  However, we have to be careful with
6147      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
6148      (via dwarf2_flag_true_p) follows this attribute.  So we may
6149      end up accidently finding a declaration attribute that belongs
6150      to a different DIE referenced by the specification attribute,
6151      even though the given DIE does not have a declaration attribute.  */
6152   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
6153           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
6154 }
6155
6156 /* Return the die giving the specification for DIE, if there is
6157    one.  */
6158
6159 static struct die_info *
6160 die_specification (struct die_info *die, struct dwarf2_cu *cu)
6161 {
6162   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification, cu);
6163
6164   if (spec_attr == NULL)
6165     return NULL;
6166   else
6167     return follow_die_ref (die, spec_attr, cu);
6168 }
6169
6170 /* Free the line_header structure *LH, and any arrays and strings it
6171    refers to.  */
6172 static void
6173 free_line_header (struct line_header *lh)
6174 {
6175   if (lh->standard_opcode_lengths)
6176     xfree (lh->standard_opcode_lengths);
6177
6178   /* Remember that all the lh->file_names[i].name pointers are
6179      pointers into debug_line_buffer, and don't need to be freed.  */
6180   if (lh->file_names)
6181     xfree (lh->file_names);
6182
6183   /* Similarly for the include directory names.  */
6184   if (lh->include_dirs)
6185     xfree (lh->include_dirs);
6186
6187   xfree (lh);
6188 }
6189
6190
6191 /* Add an entry to LH's include directory table.  */
6192 static void
6193 add_include_dir (struct line_header *lh, char *include_dir)
6194 {
6195   /* Grow the array if necessary.  */
6196   if (lh->include_dirs_size == 0)
6197     {
6198       lh->include_dirs_size = 1; /* for testing */
6199       lh->include_dirs = xmalloc (lh->include_dirs_size
6200                                   * sizeof (*lh->include_dirs));
6201     }
6202   else if (lh->num_include_dirs >= lh->include_dirs_size)
6203     {
6204       lh->include_dirs_size *= 2;
6205       lh->include_dirs = xrealloc (lh->include_dirs,
6206                                    (lh->include_dirs_size
6207                                     * sizeof (*lh->include_dirs)));
6208     }
6209
6210   lh->include_dirs[lh->num_include_dirs++] = include_dir;
6211 }
6212  
6213
6214 /* Add an entry to LH's file name table.  */
6215 static void
6216 add_file_name (struct line_header *lh,
6217                char *name,
6218                unsigned int dir_index,
6219                unsigned int mod_time,
6220                unsigned int length)
6221 {
6222   struct file_entry *fe;
6223
6224   /* Grow the array if necessary.  */
6225   if (lh->file_names_size == 0)
6226     {
6227       lh->file_names_size = 1; /* for testing */
6228       lh->file_names = xmalloc (lh->file_names_size
6229                                 * sizeof (*lh->file_names));
6230     }
6231   else if (lh->num_file_names >= lh->file_names_size)
6232     {
6233       lh->file_names_size *= 2;
6234       lh->file_names = xrealloc (lh->file_names,
6235                                  (lh->file_names_size
6236                                   * sizeof (*lh->file_names)));
6237     }
6238
6239   fe = &lh->file_names[lh->num_file_names++];
6240   fe->name = name;
6241   fe->dir_index = dir_index;
6242   fe->mod_time = mod_time;
6243   fe->length = length;
6244   fe->included_p = 0;
6245 }
6246  
6247
6248 /* Read the statement program header starting at OFFSET in
6249    .debug_line, according to the endianness of ABFD.  Return a pointer
6250    to a struct line_header, allocated using xmalloc.
6251
6252    NOTE: the strings in the include directory and file name tables of
6253    the returned object point into debug_line_buffer, and must not be
6254    freed.  */
6255 static struct line_header *
6256 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
6257                           struct dwarf2_cu *cu)
6258 {
6259   struct cleanup *back_to;
6260   struct line_header *lh;
6261   char *line_ptr;
6262   int bytes_read;
6263   int i;
6264   char *cur_dir, *cur_file;
6265
6266   if (dwarf2_per_objfile->line_buffer == NULL)
6267     {
6268       complaint (&symfile_complaints, "missing .debug_line section");
6269       return 0;
6270     }
6271
6272   /* Make sure that at least there's room for the total_length field.
6273      That could be 12 bytes long, but we're just going to fudge that.  */
6274   if (offset + 4 >= dwarf2_per_objfile->line_size)
6275     {
6276       dwarf2_statement_list_fits_in_line_number_section_complaint ();
6277       return 0;
6278     }
6279
6280   lh = xmalloc (sizeof (*lh));
6281   memset (lh, 0, sizeof (*lh));
6282   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
6283                           (void *) lh);
6284
6285   line_ptr = dwarf2_per_objfile->line_buffer + offset;
6286
6287   /* Read in the header.  */
6288   lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
6289   line_ptr += bytes_read;
6290   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
6291                                      + dwarf2_per_objfile->line_size))
6292     {
6293       dwarf2_statement_list_fits_in_line_number_section_complaint ();
6294       return 0;
6295     }
6296   lh->statement_program_end = line_ptr + lh->total_length;
6297   lh->version = read_2_bytes (abfd, line_ptr);
6298   line_ptr += 2;
6299   lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
6300   line_ptr += bytes_read;
6301   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
6302   line_ptr += 1;
6303   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
6304   line_ptr += 1;
6305   lh->line_base = read_1_signed_byte (abfd, line_ptr);
6306   line_ptr += 1;
6307   lh->line_range = read_1_byte (abfd, line_ptr);
6308   line_ptr += 1;
6309   lh->opcode_base = read_1_byte (abfd, line_ptr);
6310   line_ptr += 1;
6311   lh->standard_opcode_lengths
6312     = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
6313
6314   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
6315   for (i = 1; i < lh->opcode_base; ++i)
6316     {
6317       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
6318       line_ptr += 1;
6319     }
6320
6321   /* Read directory table.  */
6322   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6323     {
6324       line_ptr += bytes_read;
6325       add_include_dir (lh, cur_dir);
6326     }
6327   line_ptr += bytes_read;
6328
6329   /* Read file name table.  */
6330   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6331     {
6332       unsigned int dir_index, mod_time, length;
6333
6334       line_ptr += bytes_read;
6335       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6336       line_ptr += bytes_read;
6337       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6338       line_ptr += bytes_read;
6339       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6340       line_ptr += bytes_read;
6341
6342       add_file_name (lh, cur_file, dir_index, mod_time, length);
6343     }
6344   line_ptr += bytes_read;
6345   lh->statement_program_start = line_ptr; 
6346
6347   if (line_ptr > (dwarf2_per_objfile->line_buffer
6348                   + dwarf2_per_objfile->line_size))
6349     complaint (&symfile_complaints,
6350                "line number info header doesn't fit in `.debug_line' section");
6351
6352   discard_cleanups (back_to);
6353   return lh;
6354 }
6355
6356 /* This function exists to work around a bug in certain compilers
6357    (particularly GCC 2.95), in which the first line number marker of a
6358    function does not show up until after the prologue, right before
6359    the second line number marker.  This function shifts ADDRESS down
6360    to the beginning of the function if necessary, and is called on
6361    addresses passed to record_line.  */
6362
6363 static CORE_ADDR
6364 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
6365 {
6366   struct function_range *fn;
6367
6368   /* Find the function_range containing address.  */
6369   if (!cu->first_fn)
6370     return address;
6371
6372   if (!cu->cached_fn)
6373     cu->cached_fn = cu->first_fn;
6374
6375   fn = cu->cached_fn;
6376   while (fn)
6377     if (fn->lowpc <= address && fn->highpc > address)
6378       goto found;
6379     else
6380       fn = fn->next;
6381
6382   fn = cu->first_fn;
6383   while (fn && fn != cu->cached_fn)
6384     if (fn->lowpc <= address && fn->highpc > address)
6385       goto found;
6386     else
6387       fn = fn->next;
6388
6389   return address;
6390
6391  found:
6392   if (fn->seen_line)
6393     return address;
6394   if (address != fn->lowpc)
6395     complaint (&symfile_complaints,
6396                "misplaced first line number at 0x%lx for '%s'",
6397                (unsigned long) address, fn->name);
6398   fn->seen_line = 1;
6399   return fn->lowpc;
6400 }
6401
6402 /* Decode the Line Number Program (LNP) for the given line_header
6403    structure and CU.  The actual information extracted and the type
6404    of structures created from the LNP depends on the value of PST.
6405
6406    1. If PST is NULL, then this procedure uses the data from the program
6407       to create all necessary symbol tables, and their linetables.
6408       The compilation directory of the file is passed in COMP_DIR,
6409       and must not be NULL.
6410    
6411    2. If PST is not NULL, this procedure reads the program to determine
6412       the list of files included by the unit represented by PST, and
6413       builds all the associated partial symbol tables.  In this case,
6414       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
6415       is not used to compute the full name of the symtab, and therefore
6416       omitting it when building the partial symtab does not introduce
6417       the potential for inconsistency - a partial symtab and its associated
6418       symbtab having a different fullname -).  */
6419
6420 static void
6421 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
6422                     struct dwarf2_cu *cu, struct partial_symtab *pst)
6423 {
6424   char *line_ptr;
6425   char *line_end;
6426   unsigned int bytes_read;
6427   unsigned char op_code, extended_op, adj_opcode;
6428   CORE_ADDR baseaddr;
6429   struct objfile *objfile = cu->objfile;
6430   const int decode_for_pst_p = (pst != NULL);
6431
6432   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6433
6434   line_ptr = lh->statement_program_start;
6435   line_end = lh->statement_program_end;
6436
6437   /* Read the statement sequences until there's nothing left.  */
6438   while (line_ptr < line_end)
6439     {
6440       /* state machine registers  */
6441       CORE_ADDR address = 0;
6442       unsigned int file = 1;
6443       unsigned int line = 1;
6444       unsigned int column = 0;
6445       int is_stmt = lh->default_is_stmt;
6446       int basic_block = 0;
6447       int end_sequence = 0;
6448
6449       if (!decode_for_pst_p && lh->num_file_names >= file)
6450         {
6451           /* Start a subfile for the current file of the state machine.  */
6452           /* lh->include_dirs and lh->file_names are 0-based, but the
6453              directory and file name numbers in the statement program
6454              are 1-based.  */
6455           struct file_entry *fe = &lh->file_names[file - 1];
6456           char *dir;
6457
6458           if (fe->dir_index)
6459             dir = lh->include_dirs[fe->dir_index - 1];
6460           else
6461             dir = comp_dir;
6462           dwarf2_start_subfile (fe->name, dir);
6463         }
6464
6465       /* Decode the table.  */
6466       while (!end_sequence)
6467         {
6468           op_code = read_1_byte (abfd, line_ptr);
6469           line_ptr += 1;
6470
6471           if (op_code >= lh->opcode_base)
6472             {           
6473               /* Special operand.  */
6474               adj_opcode = op_code - lh->opcode_base;
6475               address += (adj_opcode / lh->line_range)
6476                 * lh->minimum_instruction_length;
6477               line += lh->line_base + (adj_opcode % lh->line_range);
6478               lh->file_names[file - 1].included_p = 1;
6479               if (!decode_for_pst_p)
6480                 {
6481                   /* Append row to matrix using current values.  */
6482                   record_line (current_subfile, line, 
6483                                check_cu_functions (address, cu));
6484                 }
6485               basic_block = 1;
6486             }
6487           else switch (op_code)
6488             {
6489             case DW_LNS_extended_op:
6490               read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6491               line_ptr += bytes_read;
6492               extended_op = read_1_byte (abfd, line_ptr);
6493               line_ptr += 1;
6494               switch (extended_op)
6495                 {
6496                 case DW_LNE_end_sequence:
6497                   end_sequence = 1;
6498                   lh->file_names[file - 1].included_p = 1;
6499                   if (!decode_for_pst_p)
6500                     record_line (current_subfile, 0, address);
6501                   break;
6502                 case DW_LNE_set_address:
6503                   address = read_address (abfd, line_ptr, cu, &bytes_read);
6504                   line_ptr += bytes_read;
6505                   address += baseaddr;
6506                   break;
6507                 case DW_LNE_define_file:
6508                   {
6509                     char *cur_file;
6510                     unsigned int dir_index, mod_time, length;
6511                     
6512                     cur_file = read_string (abfd, line_ptr, &bytes_read);
6513                     line_ptr += bytes_read;
6514                     dir_index =
6515                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6516                     line_ptr += bytes_read;
6517                     mod_time =
6518                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6519                     line_ptr += bytes_read;
6520                     length =
6521                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6522                     line_ptr += bytes_read;
6523                     add_file_name (lh, cur_file, dir_index, mod_time, length);
6524                   }
6525                   break;
6526                 default:
6527                   complaint (&symfile_complaints,
6528                              "mangled .debug_line section");
6529                   return;
6530                 }
6531               break;
6532             case DW_LNS_copy:
6533               lh->file_names[file - 1].included_p = 1;
6534               if (!decode_for_pst_p)
6535                 record_line (current_subfile, line, 
6536                              check_cu_functions (address, cu));
6537               basic_block = 0;
6538               break;
6539             case DW_LNS_advance_pc:
6540               address += lh->minimum_instruction_length
6541                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6542               line_ptr += bytes_read;
6543               break;
6544             case DW_LNS_advance_line:
6545               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
6546               line_ptr += bytes_read;
6547               break;
6548             case DW_LNS_set_file:
6549               {
6550                 /* The arrays lh->include_dirs and lh->file_names are
6551                    0-based, but the directory and file name numbers in
6552                    the statement program are 1-based.  */
6553                 struct file_entry *fe;
6554                 char *dir;
6555
6556                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6557                 line_ptr += bytes_read;
6558                 fe = &lh->file_names[file - 1];
6559                 if (fe->dir_index)
6560                   dir = lh->include_dirs[fe->dir_index - 1];
6561                 else
6562                   dir = comp_dir;
6563                 if (!decode_for_pst_p)
6564                   dwarf2_start_subfile (fe->name, dir);
6565               }
6566               break;
6567             case DW_LNS_set_column:
6568               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6569               line_ptr += bytes_read;
6570               break;
6571             case DW_LNS_negate_stmt:
6572               is_stmt = (!is_stmt);
6573               break;
6574             case DW_LNS_set_basic_block:
6575               basic_block = 1;
6576               break;
6577             /* Add to the address register of the state machine the
6578                address increment value corresponding to special opcode
6579                255.  I.e., this value is scaled by the minimum
6580                instruction length since special opcode 255 would have
6581                scaled the the increment.  */
6582             case DW_LNS_const_add_pc:
6583               address += (lh->minimum_instruction_length
6584                           * ((255 - lh->opcode_base) / lh->line_range));
6585               break;
6586             case DW_LNS_fixed_advance_pc:
6587               address += read_2_bytes (abfd, line_ptr);
6588               line_ptr += 2;
6589               break;
6590             default:
6591               {
6592                 /* Unknown standard opcode, ignore it.  */
6593                 int i;
6594
6595                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
6596                   {
6597                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6598                     line_ptr += bytes_read;
6599                   }
6600               }
6601             }
6602         }
6603     }
6604
6605   if (decode_for_pst_p)
6606     {
6607       int file_index;
6608
6609       /* Now that we're done scanning the Line Header Program, we can
6610          create the psymtab of each included file.  */
6611       for (file_index = 0; file_index < lh->num_file_names; file_index++)
6612         if (lh->file_names[file_index].included_p == 1)
6613           {
6614             char *include_name = lh->file_names [file_index].name;
6615     
6616             if (strcmp (include_name, pst->filename) != 0)
6617               dwarf2_create_include_psymtab (include_name, pst, objfile);
6618           }
6619     }
6620 }
6621
6622 /* Start a subfile for DWARF.  FILENAME is the name of the file and
6623    DIRNAME the name of the source directory which contains FILENAME
6624    or NULL if not known.
6625    This routine tries to keep line numbers from identical absolute and
6626    relative file names in a common subfile.
6627
6628    Using the `list' example from the GDB testsuite, which resides in
6629    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
6630    of /srcdir/list0.c yields the following debugging information for list0.c:
6631
6632    DW_AT_name:          /srcdir/list0.c
6633    DW_AT_comp_dir:              /compdir
6634    files.files[0].name: list0.h
6635    files.files[0].dir:  /srcdir
6636    files.files[1].name: list0.c
6637    files.files[1].dir:  /srcdir
6638
6639    The line number information for list0.c has to end up in a single
6640    subfile, so that `break /srcdir/list0.c:1' works as expected.  */
6641
6642 static void
6643 dwarf2_start_subfile (char *filename, char *dirname)
6644 {
6645   /* If the filename isn't absolute, try to match an existing subfile
6646      with the full pathname.  */
6647
6648   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
6649     {
6650       struct subfile *subfile;
6651       char *fullname = concat (dirname, "/", filename, NULL);
6652
6653       for (subfile = subfiles; subfile; subfile = subfile->next)
6654         {
6655           if (FILENAME_CMP (subfile->name, fullname) == 0)
6656             {
6657               current_subfile = subfile;
6658               xfree (fullname);
6659               return;
6660             }
6661         }
6662       xfree (fullname);
6663     }
6664   start_subfile (filename, dirname);
6665 }
6666
6667 static void
6668 var_decode_location (struct attribute *attr, struct symbol *sym,
6669                      struct dwarf2_cu *cu)
6670 {
6671   struct objfile *objfile = cu->objfile;
6672   struct comp_unit_head *cu_header = &cu->header;
6673
6674   /* NOTE drow/2003-01-30: There used to be a comment and some special
6675      code here to turn a symbol with DW_AT_external and a
6676      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
6677      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
6678      with some versions of binutils) where shared libraries could have
6679      relocations against symbols in their debug information - the
6680      minimal symbol would have the right address, but the debug info
6681      would not.  It's no longer necessary, because we will explicitly
6682      apply relocations when we read in the debug information now.  */
6683
6684   /* A DW_AT_location attribute with no contents indicates that a
6685      variable has been optimized away.  */
6686   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
6687     {
6688       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
6689       return;
6690     }
6691
6692   /* Handle one degenerate form of location expression specially, to
6693      preserve GDB's previous behavior when section offsets are
6694      specified.  If this is just a DW_OP_addr then mark this symbol
6695      as LOC_STATIC.  */
6696
6697   if (attr_form_is_block (attr)
6698       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
6699       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
6700     {
6701       int dummy;
6702
6703       SYMBOL_VALUE_ADDRESS (sym) =
6704         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
6705       fixup_symbol_section (sym, objfile);
6706       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
6707                                               SYMBOL_SECTION (sym));
6708       SYMBOL_CLASS (sym) = LOC_STATIC;
6709       return;
6710     }
6711
6712   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
6713      expression evaluator, and use LOC_COMPUTED only when necessary
6714      (i.e. when the value of a register or memory location is
6715      referenced, or a thread-local block, etc.).  Then again, it might
6716      not be worthwhile.  I'm assuming that it isn't unless performance
6717      or memory numbers show me otherwise.  */
6718
6719   dwarf2_symbol_mark_computed (attr, sym, cu);
6720   SYMBOL_CLASS (sym) = LOC_COMPUTED;
6721 }
6722
6723 /* Given a pointer to a DWARF information entry, figure out if we need
6724    to make a symbol table entry for it, and if so, create a new entry
6725    and return a pointer to it.
6726    If TYPE is NULL, determine symbol type from the die, otherwise
6727    used the passed type.  */
6728
6729 static struct symbol *
6730 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
6731 {
6732   struct objfile *objfile = cu->objfile;
6733   struct symbol *sym = NULL;
6734   char *name;
6735   struct attribute *attr = NULL;
6736   struct attribute *attr2 = NULL;
6737   CORE_ADDR baseaddr;
6738
6739   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6740
6741   if (die->tag != DW_TAG_namespace)
6742     name = dwarf2_linkage_name (die, cu);
6743   else
6744     name = TYPE_NAME (type);
6745
6746   if (name)
6747     {
6748       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
6749                                              sizeof (struct symbol));
6750       OBJSTAT (objfile, n_syms++);
6751       memset (sym, 0, sizeof (struct symbol));
6752
6753       /* Cache this symbol's name and the name's demangled form (if any).  */
6754       SYMBOL_LANGUAGE (sym) = cu->language;
6755       SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
6756
6757       /* Default assumptions.
6758          Use the passed type or decode it from the die.  */
6759       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6760       SYMBOL_CLASS (sym) = LOC_STATIC;
6761       if (type != NULL)
6762         SYMBOL_TYPE (sym) = type;
6763       else
6764         SYMBOL_TYPE (sym) = die_type (die, cu);
6765       attr = dwarf2_attr (die, DW_AT_decl_line, cu);
6766       if (attr)
6767         {
6768           SYMBOL_LINE (sym) = DW_UNSND (attr);
6769         }
6770       switch (die->tag)
6771         {
6772         case DW_TAG_label:
6773           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6774           if (attr)
6775             {
6776               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
6777             }
6778           SYMBOL_CLASS (sym) = LOC_LABEL;
6779           break;
6780         case DW_TAG_subprogram:
6781           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
6782              finish_block.  */
6783           SYMBOL_CLASS (sym) = LOC_BLOCK;
6784           attr2 = dwarf2_attr (die, DW_AT_external, cu);
6785           if (attr2 && (DW_UNSND (attr2) != 0))
6786             {
6787               add_symbol_to_list (sym, &global_symbols);
6788             }
6789           else
6790             {
6791               add_symbol_to_list (sym, cu->list_in_scope);
6792             }
6793           break;
6794         case DW_TAG_variable:
6795           /* Compilation with minimal debug info may result in variables
6796              with missing type entries. Change the misleading `void' type
6797              to something sensible.  */
6798           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
6799             SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
6800                                            TARGET_INT_BIT / HOST_CHAR_BIT, 0,
6801                                            "<variable, no debug info>",
6802                                            objfile);
6803           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6804           if (attr)
6805             {
6806               dwarf2_const_value (attr, sym, cu);
6807               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6808               if (attr2 && (DW_UNSND (attr2) != 0))
6809                 add_symbol_to_list (sym, &global_symbols);
6810               else
6811                 add_symbol_to_list (sym, cu->list_in_scope);
6812               break;
6813             }
6814           attr = dwarf2_attr (die, DW_AT_location, cu);
6815           if (attr)
6816             {
6817               var_decode_location (attr, sym, cu);
6818               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6819               if (attr2 && (DW_UNSND (attr2) != 0))
6820                 add_symbol_to_list (sym, &global_symbols);
6821               else
6822                 add_symbol_to_list (sym, cu->list_in_scope);
6823             }
6824           else
6825             {
6826               /* We do not know the address of this symbol.
6827                  If it is an external symbol and we have type information
6828                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
6829                  The address of the variable will then be determined from
6830                  the minimal symbol table whenever the variable is
6831                  referenced.  */
6832               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6833               if (attr2 && (DW_UNSND (attr2) != 0)
6834                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
6835                 {
6836                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
6837                   add_symbol_to_list (sym, &global_symbols);
6838                 }
6839             }
6840           break;
6841         case DW_TAG_formal_parameter:
6842           attr = dwarf2_attr (die, DW_AT_location, cu);
6843           if (attr)
6844             {
6845               var_decode_location (attr, sym, cu);
6846               /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary?  */
6847               if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
6848                 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
6849             }
6850           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6851           if (attr)
6852             {
6853               dwarf2_const_value (attr, sym, cu);
6854             }
6855           add_symbol_to_list (sym, cu->list_in_scope);
6856           break;
6857         case DW_TAG_unspecified_parameters:
6858           /* From varargs functions; gdb doesn't seem to have any
6859              interest in this information, so just ignore it for now.
6860              (FIXME?) */
6861           break;
6862         case DW_TAG_class_type:
6863         case DW_TAG_structure_type:
6864         case DW_TAG_union_type:
6865         case DW_TAG_enumeration_type:
6866           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6867           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6868
6869           /* Make sure that the symbol includes appropriate enclosing
6870              classes/namespaces in its name.  These are calculated in
6871              read_structure_type, and the correct name is saved in
6872              the type.  */
6873
6874           if (cu->language == language_cplus
6875               || cu->language == language_java)
6876             {
6877               struct type *type = SYMBOL_TYPE (sym);
6878               
6879               if (TYPE_TAG_NAME (type) != NULL)
6880                 {
6881                   /* FIXME: carlton/2003-11-10: Should this use
6882                      SYMBOL_SET_NAMES instead?  (The same problem also
6883                      arises further down in this function.)  */
6884                   /* The type's name is already allocated along with
6885                      this objfile, so we don't need to duplicate it
6886                      for the symbol.  */
6887                   SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
6888                 }
6889             }
6890
6891           {
6892             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
6893                really ever be static objects: otherwise, if you try
6894                to, say, break of a class's method and you're in a file
6895                which doesn't mention that class, it won't work unless
6896                the check for all static symbols in lookup_symbol_aux
6897                saves you.  See the OtherFileClass tests in
6898                gdb.c++/namespace.exp.  */
6899
6900             struct pending **list_to_add;
6901
6902             list_to_add = (cu->list_in_scope == &file_symbols
6903                            && (cu->language == language_cplus
6904                                || cu->language == language_java)
6905                            ? &global_symbols : cu->list_in_scope);
6906           
6907             add_symbol_to_list (sym, list_to_add);
6908
6909             /* The semantics of C++ state that "struct foo { ... }" also
6910                defines a typedef for "foo".  A Java class declaration also
6911                defines a typedef for the class.  Synthesize a typedef symbol
6912                so that "ptype foo" works as expected.  */
6913             if (cu->language == language_cplus
6914                 || cu->language == language_java)
6915               {
6916                 struct symbol *typedef_sym = (struct symbol *)
6917                   obstack_alloc (&objfile->objfile_obstack,
6918                                  sizeof (struct symbol));
6919                 *typedef_sym = *sym;
6920                 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
6921                 /* The symbol's name is already allocated along with
6922                    this objfile, so we don't need to duplicate it for
6923                    the type.  */
6924                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
6925                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NATURAL_NAME (sym);
6926                 add_symbol_to_list (typedef_sym, list_to_add);
6927               }
6928           }
6929           break;
6930         case DW_TAG_typedef:
6931           if (processing_has_namespace_info
6932               && processing_current_prefix[0] != '\0')
6933             {
6934               SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
6935                                                            processing_current_prefix,
6936                                                            name, cu);
6937             }
6938           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6939           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6940           add_symbol_to_list (sym, cu->list_in_scope);
6941           break;
6942         case DW_TAG_base_type:
6943         case DW_TAG_subrange_type:
6944           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6945           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6946           add_symbol_to_list (sym, cu->list_in_scope);
6947           break;
6948         case DW_TAG_enumerator:
6949           if (processing_has_namespace_info
6950               && processing_current_prefix[0] != '\0')
6951             {
6952               SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
6953                                                            processing_current_prefix,
6954                                                            name, cu);
6955             }
6956           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6957           if (attr)
6958             {
6959               dwarf2_const_value (attr, sym, cu);
6960             }
6961           {
6962             /* NOTE: carlton/2003-11-10: See comment above in the
6963                DW_TAG_class_type, etc. block.  */
6964
6965             struct pending **list_to_add;
6966
6967             list_to_add = (cu->list_in_scope == &file_symbols
6968                            && (cu->language == language_cplus
6969                                || cu->language == language_java)
6970                            ? &global_symbols : cu->list_in_scope);
6971           
6972             add_symbol_to_list (sym, list_to_add);
6973           }
6974           break;
6975         case DW_TAG_namespace:
6976           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6977           add_symbol_to_list (sym, &global_symbols);
6978           break;
6979         default:
6980           /* Not a tag we recognize.  Hopefully we aren't processing
6981              trash data, but since we must specifically ignore things
6982              we don't recognize, there is nothing else we should do at
6983              this point. */
6984           complaint (&symfile_complaints, "unsupported tag: '%s'",
6985                      dwarf_tag_name (die->tag));
6986           break;
6987         }
6988     }
6989   return (sym);
6990 }
6991
6992 /* Copy constant value from an attribute to a symbol.  */
6993
6994 static void
6995 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
6996                     struct dwarf2_cu *cu)
6997 {
6998   struct objfile *objfile = cu->objfile;
6999   struct comp_unit_head *cu_header = &cu->header;
7000   struct dwarf_block *blk;
7001
7002   switch (attr->form)
7003     {
7004     case DW_FORM_addr:
7005       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
7006         dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
7007                                                       cu_header->addr_size,
7008                                                       TYPE_LENGTH (SYMBOL_TYPE
7009                                                                    (sym)));
7010       SYMBOL_VALUE_BYTES (sym) = (char *)
7011         obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
7012       /* NOTE: cagney/2003-05-09: In-lined store_address call with
7013          it's body - store_unsigned_integer.  */
7014       store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
7015                               DW_ADDR (attr));
7016       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7017       break;
7018     case DW_FORM_block1:
7019     case DW_FORM_block2:
7020     case DW_FORM_block4:
7021     case DW_FORM_block:
7022       blk = DW_BLOCK (attr);
7023       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
7024         dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
7025                                                       blk->size,
7026                                                       TYPE_LENGTH (SYMBOL_TYPE
7027                                                                    (sym)));
7028       SYMBOL_VALUE_BYTES (sym) = (char *)
7029         obstack_alloc (&objfile->objfile_obstack, blk->size);
7030       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
7031       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7032       break;
7033
7034       /* The DW_AT_const_value attributes are supposed to carry the
7035          symbol's value "represented as it would be on the target
7036          architecture."  By the time we get here, it's already been
7037          converted to host endianness, so we just need to sign- or
7038          zero-extend it as appropriate.  */
7039     case DW_FORM_data1:
7040       dwarf2_const_value_data (attr, sym, 8);
7041       break;
7042     case DW_FORM_data2:
7043       dwarf2_const_value_data (attr, sym, 16);
7044       break;
7045     case DW_FORM_data4:
7046       dwarf2_const_value_data (attr, sym, 32);
7047       break;
7048     case DW_FORM_data8:
7049       dwarf2_const_value_data (attr, sym, 64);
7050       break;
7051
7052     case DW_FORM_sdata:
7053       SYMBOL_VALUE (sym) = DW_SND (attr);
7054       SYMBOL_CLASS (sym) = LOC_CONST;
7055       break;
7056
7057     case DW_FORM_udata:
7058       SYMBOL_VALUE (sym) = DW_UNSND (attr);
7059       SYMBOL_CLASS (sym) = LOC_CONST;
7060       break;
7061
7062     default:
7063       complaint (&symfile_complaints,
7064                  "unsupported const value attribute form: '%s'",
7065                  dwarf_form_name (attr->form));
7066       SYMBOL_VALUE (sym) = 0;
7067       SYMBOL_CLASS (sym) = LOC_CONST;
7068       break;
7069     }
7070 }
7071
7072
7073 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
7074    or zero-extend it as appropriate for the symbol's type.  */
7075 static void
7076 dwarf2_const_value_data (struct attribute *attr,
7077                          struct symbol *sym,
7078                          int bits)
7079 {
7080   LONGEST l = DW_UNSND (attr);
7081
7082   if (bits < sizeof (l) * 8)
7083     {
7084       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
7085         l &= ((LONGEST) 1 << bits) - 1;
7086       else
7087         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
7088     }
7089
7090   SYMBOL_VALUE (sym) = l;
7091   SYMBOL_CLASS (sym) = LOC_CONST;
7092 }
7093
7094
7095 /* Return the type of the die in question using its DW_AT_type attribute.  */
7096
7097 static struct type *
7098 die_type (struct die_info *die, struct dwarf2_cu *cu)
7099 {
7100   struct type *type;
7101   struct attribute *type_attr;
7102   struct die_info *type_die;
7103
7104   type_attr = dwarf2_attr (die, DW_AT_type, cu);
7105   if (!type_attr)
7106     {
7107       /* A missing DW_AT_type represents a void type.  */
7108       return dwarf2_fundamental_type (cu->objfile, FT_VOID, cu);
7109     }
7110   else
7111     type_die = follow_die_ref (die, type_attr, cu);
7112
7113   type = tag_type_to_type (type_die, cu);
7114   if (!type)
7115     {
7116       dump_die (type_die);
7117       error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
7118                       cu->objfile->name);
7119     }
7120   return type;
7121 }
7122
7123 /* Return the containing type of the die in question using its
7124    DW_AT_containing_type attribute.  */
7125
7126 static struct type *
7127 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
7128 {
7129   struct type *type = NULL;
7130   struct attribute *type_attr;
7131   struct die_info *type_die = NULL;
7132
7133   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
7134   if (type_attr)
7135     {
7136       type_die = follow_die_ref (die, type_attr, cu);
7137       type = tag_type_to_type (type_die, cu);
7138     }
7139   if (!type)
7140     {
7141       if (type_die)
7142         dump_die (type_die);
7143       error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]", 
7144                       cu->objfile->name);
7145     }
7146   return type;
7147 }
7148
7149 static struct type *
7150 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
7151 {
7152   if (die->type)
7153     {
7154       return die->type;
7155     }
7156   else
7157     {
7158       read_type_die (die, cu);
7159       if (!die->type)
7160         {
7161           dump_die (die);
7162           error ("Dwarf Error: Cannot find type of die [in module %s]", 
7163                           cu->objfile->name);
7164         }
7165       return die->type;
7166     }
7167 }
7168
7169 static void
7170 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
7171 {
7172   char *prefix = determine_prefix (die, cu);
7173   const char *old_prefix = processing_current_prefix;
7174   struct cleanup *back_to = make_cleanup (xfree, prefix);
7175   processing_current_prefix = prefix;
7176   
7177   switch (die->tag)
7178     {
7179     case DW_TAG_class_type:
7180     case DW_TAG_structure_type:
7181     case DW_TAG_union_type:
7182       read_structure_type (die, cu);
7183       break;
7184     case DW_TAG_enumeration_type:
7185       read_enumeration_type (die, cu);
7186       break;
7187     case DW_TAG_subprogram:
7188     case DW_TAG_subroutine_type:
7189       read_subroutine_type (die, cu);
7190       break;
7191     case DW_TAG_array_type:
7192       read_array_type (die, cu);
7193       break;
7194     case DW_TAG_pointer_type:
7195       read_tag_pointer_type (die, cu);
7196       break;
7197     case DW_TAG_ptr_to_member_type:
7198       read_tag_ptr_to_member_type (die, cu);
7199       break;
7200     case DW_TAG_reference_type:
7201       read_tag_reference_type (die, cu);
7202       break;
7203     case DW_TAG_const_type:
7204       read_tag_const_type (die, cu);
7205       break;
7206     case DW_TAG_volatile_type:
7207       read_tag_volatile_type (die, cu);
7208       break;
7209     case DW_TAG_string_type:
7210       read_tag_string_type (die, cu);
7211       break;
7212     case DW_TAG_typedef:
7213       read_typedef (die, cu);
7214       break;
7215     case DW_TAG_subrange_type:
7216       read_subrange_type (die, cu);
7217       break;
7218     case DW_TAG_base_type:
7219       read_base_type (die, cu);
7220       break;
7221     default:
7222       complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
7223                  dwarf_tag_name (die->tag));
7224       break;
7225     }
7226
7227   processing_current_prefix = old_prefix;
7228   do_cleanups (back_to);
7229 }
7230
7231 /* Return the name of the namespace/class that DIE is defined within,
7232    or "" if we can't tell.  The caller should xfree the result.  */
7233
7234 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
7235    therein) for an example of how to use this function to deal with
7236    DW_AT_specification.  */
7237
7238 static char *
7239 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
7240 {
7241   struct die_info *parent;
7242
7243   if (cu->language != language_cplus
7244       && cu->language != language_java)
7245     return NULL;
7246
7247   parent = die->parent;
7248
7249   if (parent == NULL)
7250     {
7251       return xstrdup ("");
7252     }
7253   else
7254     {
7255       switch (parent->tag) {
7256       case DW_TAG_namespace:
7257         {
7258           /* FIXME: carlton/2004-03-05: Should I follow extension dies
7259              before doing this check?  */
7260           if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
7261             {
7262               return xstrdup (TYPE_TAG_NAME (parent->type));
7263             }
7264           else
7265             {
7266               int dummy;
7267               char *parent_prefix = determine_prefix (parent, cu);
7268               char *retval = typename_concat (NULL, parent_prefix,
7269                                               namespace_name (parent, &dummy,
7270                                                               cu),
7271                                               cu);
7272               xfree (parent_prefix);
7273               return retval;
7274             }
7275         }
7276         break;
7277       case DW_TAG_class_type:
7278       case DW_TAG_structure_type:
7279         {
7280           if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
7281             {
7282               return xstrdup (TYPE_TAG_NAME (parent->type));
7283             }
7284           else
7285             {
7286               const char *old_prefix = processing_current_prefix;
7287               char *new_prefix = determine_prefix (parent, cu);
7288               char *retval;
7289
7290               processing_current_prefix = new_prefix;
7291               retval = determine_class_name (parent, cu);
7292               processing_current_prefix = old_prefix;
7293
7294               xfree (new_prefix);
7295               return retval;
7296             }
7297         }
7298       default:
7299         return determine_prefix (parent, cu);
7300       }
7301     }
7302 }
7303
7304 /* Return a newly-allocated string formed by concatenating PREFIX and
7305    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
7306    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
7307    perform an obconcat, otherwise allocate storage for the result.  The CU argument
7308    is used to determine the language and hence, the appropriate separator.  */
7309
7310 #define MAX_SEP_LEN 2  /* sizeof ("::")  */
7311
7312 static char *
7313 typename_concat (struct obstack *obs, const char *prefix, const char *suffix, 
7314                  struct dwarf2_cu *cu)
7315 {
7316   char *sep;
7317
7318   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
7319     sep = "";
7320   else if (cu->language == language_java)
7321     sep = ".";
7322   else
7323     sep = "::";
7324
7325   if (obs == NULL)
7326     {
7327       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
7328       retval[0] = '\0';
7329       
7330       if (prefix)
7331         {
7332           strcpy (retval, prefix);
7333           strcat (retval, sep);
7334         }
7335       if (suffix)
7336         strcat (retval, suffix);
7337       
7338       return retval;
7339     }
7340   else
7341     {
7342       /* We have an obstack.  */
7343       return obconcat (obs, prefix, sep, suffix);
7344     }
7345 }
7346
7347 static struct type *
7348 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
7349 {
7350   struct objfile *objfile = cu->objfile;
7351
7352   /* FIXME - this should not produce a new (struct type *)
7353      every time.  It should cache base types.  */
7354   struct type *type;
7355   switch (encoding)
7356     {
7357     case DW_ATE_address:
7358       type = dwarf2_fundamental_type (objfile, FT_VOID, cu);
7359       return type;
7360     case DW_ATE_boolean:
7361       type = dwarf2_fundamental_type (objfile, FT_BOOLEAN, cu);
7362       return type;
7363     case DW_ATE_complex_float:
7364       if (size == 16)
7365         {
7366           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX, cu);
7367         }
7368       else
7369         {
7370           type = dwarf2_fundamental_type (objfile, FT_COMPLEX, cu);
7371         }
7372       return type;
7373     case DW_ATE_float:
7374       if (size == 8)
7375         {
7376           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
7377         }
7378       else
7379         {
7380           type = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
7381         }
7382       return type;
7383     case DW_ATE_signed:
7384       switch (size)
7385         {
7386         case 1:
7387           type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
7388           break;
7389         case 2:
7390           type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT, cu);
7391           break;
7392         default:
7393         case 4:
7394           type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
7395           break;
7396         }
7397       return type;
7398     case DW_ATE_signed_char:
7399       type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
7400       return type;
7401     case DW_ATE_unsigned:
7402       switch (size)
7403         {
7404         case 1:
7405           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
7406           break;
7407         case 2:
7408           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT, cu);
7409           break;
7410         default:
7411         case 4:
7412           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER, cu);
7413           break;
7414         }
7415       return type;
7416     case DW_ATE_unsigned_char:
7417       type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
7418       return type;
7419     default:
7420       type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
7421       return type;
7422     }
7423 }
7424
7425 #if 0
7426 struct die_info *
7427 copy_die (struct die_info *old_die)
7428 {
7429   struct die_info *new_die;
7430   int i, num_attrs;
7431
7432   new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
7433   memset (new_die, 0, sizeof (struct die_info));
7434
7435   new_die->tag = old_die->tag;
7436   new_die->has_children = old_die->has_children;
7437   new_die->abbrev = old_die->abbrev;
7438   new_die->offset = old_die->offset;
7439   new_die->type = NULL;
7440
7441   num_attrs = old_die->num_attrs;
7442   new_die->num_attrs = num_attrs;
7443   new_die->attrs = (struct attribute *)
7444     xmalloc (num_attrs * sizeof (struct attribute));
7445
7446   for (i = 0; i < old_die->num_attrs; ++i)
7447     {
7448       new_die->attrs[i].name = old_die->attrs[i].name;
7449       new_die->attrs[i].form = old_die->attrs[i].form;
7450       new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
7451     }
7452
7453   new_die->next = NULL;
7454   return new_die;
7455 }
7456 #endif
7457
7458 /* Return sibling of die, NULL if no sibling.  */
7459
7460 static struct die_info *
7461 sibling_die (struct die_info *die)
7462 {
7463   return die->sibling;
7464 }
7465
7466 /* Get linkage name of a die, return NULL if not found.  */
7467
7468 static char *
7469 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
7470 {
7471   struct attribute *attr;
7472
7473   attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7474   if (attr && DW_STRING (attr))
7475     return DW_STRING (attr);
7476   attr = dwarf2_attr (die, DW_AT_name, cu);
7477   if (attr && DW_STRING (attr))
7478     return DW_STRING (attr);
7479   return NULL;
7480 }
7481
7482 /* Get name of a die, return NULL if not found.  */
7483
7484 static char *
7485 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
7486 {
7487   struct attribute *attr;
7488
7489   attr = dwarf2_attr (die, DW_AT_name, cu);
7490   if (attr && DW_STRING (attr))
7491     return DW_STRING (attr);
7492   return NULL;
7493 }
7494
7495 /* Return the die that this die in an extension of, or NULL if there
7496    is none.  */
7497
7498 static struct die_info *
7499 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
7500 {
7501   struct attribute *attr;
7502
7503   attr = dwarf2_attr (die, DW_AT_extension, cu);
7504   if (attr == NULL)
7505     return NULL;
7506
7507   return follow_die_ref (die, attr, cu);
7508 }
7509
7510 /* Convert a DIE tag into its string name.  */
7511
7512 static char *
7513 dwarf_tag_name (unsigned tag)
7514 {
7515   switch (tag)
7516     {
7517     case DW_TAG_padding:
7518       return "DW_TAG_padding";
7519     case DW_TAG_array_type:
7520       return "DW_TAG_array_type";
7521     case DW_TAG_class_type:
7522       return "DW_TAG_class_type";
7523     case DW_TAG_entry_point:
7524       return "DW_TAG_entry_point";
7525     case DW_TAG_enumeration_type:
7526       return "DW_TAG_enumeration_type";
7527     case DW_TAG_formal_parameter:
7528       return "DW_TAG_formal_parameter";
7529     case DW_TAG_imported_declaration:
7530       return "DW_TAG_imported_declaration";
7531     case DW_TAG_label:
7532       return "DW_TAG_label";
7533     case DW_TAG_lexical_block:
7534       return "DW_TAG_lexical_block";
7535     case DW_TAG_member:
7536       return "DW_TAG_member";
7537     case DW_TAG_pointer_type:
7538       return "DW_TAG_pointer_type";
7539     case DW_TAG_reference_type:
7540       return "DW_TAG_reference_type";
7541     case DW_TAG_compile_unit:
7542       return "DW_TAG_compile_unit";
7543     case DW_TAG_string_type:
7544       return "DW_TAG_string_type";
7545     case DW_TAG_structure_type:
7546       return "DW_TAG_structure_type";
7547     case DW_TAG_subroutine_type:
7548       return "DW_TAG_subroutine_type";
7549     case DW_TAG_typedef:
7550       return "DW_TAG_typedef";
7551     case DW_TAG_union_type:
7552       return "DW_TAG_union_type";
7553     case DW_TAG_unspecified_parameters:
7554       return "DW_TAG_unspecified_parameters";
7555     case DW_TAG_variant:
7556       return "DW_TAG_variant";
7557     case DW_TAG_common_block:
7558       return "DW_TAG_common_block";
7559     case DW_TAG_common_inclusion:
7560       return "DW_TAG_common_inclusion";
7561     case DW_TAG_inheritance:
7562       return "DW_TAG_inheritance";
7563     case DW_TAG_inlined_subroutine:
7564       return "DW_TAG_inlined_subroutine";
7565     case DW_TAG_module:
7566       return "DW_TAG_module";
7567     case DW_TAG_ptr_to_member_type:
7568       return "DW_TAG_ptr_to_member_type";
7569     case DW_TAG_set_type:
7570       return "DW_TAG_set_type";
7571     case DW_TAG_subrange_type:
7572       return "DW_TAG_subrange_type";
7573     case DW_TAG_with_stmt:
7574       return "DW_TAG_with_stmt";
7575     case DW_TAG_access_declaration:
7576       return "DW_TAG_access_declaration";
7577     case DW_TAG_base_type:
7578       return "DW_TAG_base_type";
7579     case DW_TAG_catch_block:
7580       return "DW_TAG_catch_block";
7581     case DW_TAG_const_type:
7582       return "DW_TAG_const_type";
7583     case DW_TAG_constant:
7584       return "DW_TAG_constant";
7585     case DW_TAG_enumerator:
7586       return "DW_TAG_enumerator";
7587     case DW_TAG_file_type:
7588       return "DW_TAG_file_type";
7589     case DW_TAG_friend:
7590       return "DW_TAG_friend";
7591     case DW_TAG_namelist:
7592       return "DW_TAG_namelist";
7593     case DW_TAG_namelist_item:
7594       return "DW_TAG_namelist_item";
7595     case DW_TAG_packed_type:
7596       return "DW_TAG_packed_type";
7597     case DW_TAG_subprogram:
7598       return "DW_TAG_subprogram";
7599     case DW_TAG_template_type_param:
7600       return "DW_TAG_template_type_param";
7601     case DW_TAG_template_value_param:
7602       return "DW_TAG_template_value_param";
7603     case DW_TAG_thrown_type:
7604       return "DW_TAG_thrown_type";
7605     case DW_TAG_try_block:
7606       return "DW_TAG_try_block";
7607     case DW_TAG_variant_part:
7608       return "DW_TAG_variant_part";
7609     case DW_TAG_variable:
7610       return "DW_TAG_variable";
7611     case DW_TAG_volatile_type:
7612       return "DW_TAG_volatile_type";
7613     case DW_TAG_dwarf_procedure:
7614       return "DW_TAG_dwarf_procedure";
7615     case DW_TAG_restrict_type:
7616       return "DW_TAG_restrict_type";
7617     case DW_TAG_interface_type:
7618       return "DW_TAG_interface_type";
7619     case DW_TAG_namespace:
7620       return "DW_TAG_namespace";
7621     case DW_TAG_imported_module:
7622       return "DW_TAG_imported_module";
7623     case DW_TAG_unspecified_type:
7624       return "DW_TAG_unspecified_type";
7625     case DW_TAG_partial_unit:
7626       return "DW_TAG_partial_unit";
7627     case DW_TAG_imported_unit:
7628       return "DW_TAG_imported_unit";
7629     case DW_TAG_MIPS_loop:
7630       return "DW_TAG_MIPS_loop";
7631     case DW_TAG_format_label:
7632       return "DW_TAG_format_label";
7633     case DW_TAG_function_template:
7634       return "DW_TAG_function_template";
7635     case DW_TAG_class_template:
7636       return "DW_TAG_class_template";
7637     default:
7638       return "DW_TAG_<unknown>";
7639     }
7640 }
7641
7642 /* Convert a DWARF attribute code into its string name.  */
7643
7644 static char *
7645 dwarf_attr_name (unsigned attr)
7646 {
7647   switch (attr)
7648     {
7649     case DW_AT_sibling:
7650       return "DW_AT_sibling";
7651     case DW_AT_location:
7652       return "DW_AT_location";
7653     case DW_AT_name:
7654       return "DW_AT_name";
7655     case DW_AT_ordering:
7656       return "DW_AT_ordering";
7657     case DW_AT_subscr_data:
7658       return "DW_AT_subscr_data";
7659     case DW_AT_byte_size:
7660       return "DW_AT_byte_size";
7661     case DW_AT_bit_offset:
7662       return "DW_AT_bit_offset";
7663     case DW_AT_bit_size:
7664       return "DW_AT_bit_size";
7665     case DW_AT_element_list:
7666       return "DW_AT_element_list";
7667     case DW_AT_stmt_list:
7668       return "DW_AT_stmt_list";
7669     case DW_AT_low_pc:
7670       return "DW_AT_low_pc";
7671     case DW_AT_high_pc:
7672       return "DW_AT_high_pc";
7673     case DW_AT_language:
7674       return "DW_AT_language";
7675     case DW_AT_member:
7676       return "DW_AT_member";
7677     case DW_AT_discr:
7678       return "DW_AT_discr";
7679     case DW_AT_discr_value:
7680       return "DW_AT_discr_value";
7681     case DW_AT_visibility:
7682       return "DW_AT_visibility";
7683     case DW_AT_import:
7684       return "DW_AT_import";
7685     case DW_AT_string_length:
7686       return "DW_AT_string_length";
7687     case DW_AT_common_reference:
7688       return "DW_AT_common_reference";
7689     case DW_AT_comp_dir:
7690       return "DW_AT_comp_dir";
7691     case DW_AT_const_value:
7692       return "DW_AT_const_value";
7693     case DW_AT_containing_type:
7694       return "DW_AT_containing_type";
7695     case DW_AT_default_value:
7696       return "DW_AT_default_value";
7697     case DW_AT_inline:
7698       return "DW_AT_inline";
7699     case DW_AT_is_optional:
7700       return "DW_AT_is_optional";
7701     case DW_AT_lower_bound:
7702       return "DW_AT_lower_bound";
7703     case DW_AT_producer:
7704       return "DW_AT_producer";
7705     case DW_AT_prototyped:
7706       return "DW_AT_prototyped";
7707     case DW_AT_return_addr:
7708       return "DW_AT_return_addr";
7709     case DW_AT_start_scope:
7710       return "DW_AT_start_scope";
7711     case DW_AT_stride_size:
7712       return "DW_AT_stride_size";
7713     case DW_AT_upper_bound:
7714       return "DW_AT_upper_bound";
7715     case DW_AT_abstract_origin:
7716       return "DW_AT_abstract_origin";
7717     case DW_AT_accessibility:
7718       return "DW_AT_accessibility";
7719     case DW_AT_address_class:
7720       return "DW_AT_address_class";
7721     case DW_AT_artificial:
7722       return "DW_AT_artificial";
7723     case DW_AT_base_types:
7724       return "DW_AT_base_types";
7725     case DW_AT_calling_convention:
7726       return "DW_AT_calling_convention";
7727     case DW_AT_count:
7728       return "DW_AT_count";
7729     case DW_AT_data_member_location:
7730       return "DW_AT_data_member_location";
7731     case DW_AT_decl_column:
7732       return "DW_AT_decl_column";
7733     case DW_AT_decl_file:
7734       return "DW_AT_decl_file";
7735     case DW_AT_decl_line:
7736       return "DW_AT_decl_line";
7737     case DW_AT_declaration:
7738       return "DW_AT_declaration";
7739     case DW_AT_discr_list:
7740       return "DW_AT_discr_list";
7741     case DW_AT_encoding:
7742       return "DW_AT_encoding";
7743     case DW_AT_external:
7744       return "DW_AT_external";
7745     case DW_AT_frame_base:
7746       return "DW_AT_frame_base";
7747     case DW_AT_friend:
7748       return "DW_AT_friend";
7749     case DW_AT_identifier_case:
7750       return "DW_AT_identifier_case";
7751     case DW_AT_macro_info:
7752       return "DW_AT_macro_info";
7753     case DW_AT_namelist_items:
7754       return "DW_AT_namelist_items";
7755     case DW_AT_priority:
7756       return "DW_AT_priority";
7757     case DW_AT_segment:
7758       return "DW_AT_segment";
7759     case DW_AT_specification:
7760       return "DW_AT_specification";
7761     case DW_AT_static_link:
7762       return "DW_AT_static_link";
7763     case DW_AT_type:
7764       return "DW_AT_type";
7765     case DW_AT_use_location:
7766       return "DW_AT_use_location";
7767     case DW_AT_variable_parameter:
7768       return "DW_AT_variable_parameter";
7769     case DW_AT_virtuality:
7770       return "DW_AT_virtuality";
7771     case DW_AT_vtable_elem_location:
7772       return "DW_AT_vtable_elem_location";
7773     case DW_AT_allocated:
7774       return "DW_AT_allocated";
7775     case DW_AT_associated:
7776       return "DW_AT_associated";
7777     case DW_AT_data_location:
7778       return "DW_AT_data_location";
7779     case DW_AT_stride:
7780       return "DW_AT_stride";
7781     case DW_AT_entry_pc:
7782       return "DW_AT_entry_pc";
7783     case DW_AT_use_UTF8:
7784       return "DW_AT_use_UTF8";
7785     case DW_AT_extension:
7786       return "DW_AT_extension";
7787     case DW_AT_ranges:
7788       return "DW_AT_ranges";
7789     case DW_AT_trampoline:
7790       return "DW_AT_trampoline";
7791     case DW_AT_call_column:
7792       return "DW_AT_call_column";
7793     case DW_AT_call_file:
7794       return "DW_AT_call_file";
7795     case DW_AT_call_line:
7796       return "DW_AT_call_line";
7797 #ifdef MIPS
7798     case DW_AT_MIPS_fde:
7799       return "DW_AT_MIPS_fde";
7800     case DW_AT_MIPS_loop_begin:
7801       return "DW_AT_MIPS_loop_begin";
7802     case DW_AT_MIPS_tail_loop_begin:
7803       return "DW_AT_MIPS_tail_loop_begin";
7804     case DW_AT_MIPS_epilog_begin:
7805       return "DW_AT_MIPS_epilog_begin";
7806     case DW_AT_MIPS_loop_unroll_factor:
7807       return "DW_AT_MIPS_loop_unroll_factor";
7808     case DW_AT_MIPS_software_pipeline_depth:
7809       return "DW_AT_MIPS_software_pipeline_depth";
7810 #endif
7811     case DW_AT_MIPS_linkage_name:
7812       return "DW_AT_MIPS_linkage_name";
7813
7814     case DW_AT_sf_names:
7815       return "DW_AT_sf_names";
7816     case DW_AT_src_info:
7817       return "DW_AT_src_info";
7818     case DW_AT_mac_info:
7819       return "DW_AT_mac_info";
7820     case DW_AT_src_coords:
7821       return "DW_AT_src_coords";
7822     case DW_AT_body_begin:
7823       return "DW_AT_body_begin";
7824     case DW_AT_body_end:
7825       return "DW_AT_body_end";
7826     case DW_AT_GNU_vector:
7827       return "DW_AT_GNU_vector";
7828     default:
7829       return "DW_AT_<unknown>";
7830     }
7831 }
7832
7833 /* Convert a DWARF value form code into its string name.  */
7834
7835 static char *
7836 dwarf_form_name (unsigned form)
7837 {
7838   switch (form)
7839     {
7840     case DW_FORM_addr:
7841       return "DW_FORM_addr";
7842     case DW_FORM_block2:
7843       return "DW_FORM_block2";
7844     case DW_FORM_block4:
7845       return "DW_FORM_block4";
7846     case DW_FORM_data2:
7847       return "DW_FORM_data2";
7848     case DW_FORM_data4:
7849       return "DW_FORM_data4";
7850     case DW_FORM_data8:
7851       return "DW_FORM_data8";
7852     case DW_FORM_string:
7853       return "DW_FORM_string";
7854     case DW_FORM_block:
7855       return "DW_FORM_block";
7856     case DW_FORM_block1:
7857       return "DW_FORM_block1";
7858     case DW_FORM_data1:
7859       return "DW_FORM_data1";
7860     case DW_FORM_flag:
7861       return "DW_FORM_flag";
7862     case DW_FORM_sdata:
7863       return "DW_FORM_sdata";
7864     case DW_FORM_strp:
7865       return "DW_FORM_strp";
7866     case DW_FORM_udata:
7867       return "DW_FORM_udata";
7868     case DW_FORM_ref_addr:
7869       return "DW_FORM_ref_addr";
7870     case DW_FORM_ref1:
7871       return "DW_FORM_ref1";
7872     case DW_FORM_ref2:
7873       return "DW_FORM_ref2";
7874     case DW_FORM_ref4:
7875       return "DW_FORM_ref4";
7876     case DW_FORM_ref8:
7877       return "DW_FORM_ref8";
7878     case DW_FORM_ref_udata:
7879       return "DW_FORM_ref_udata";
7880     case DW_FORM_indirect:
7881       return "DW_FORM_indirect";
7882     default:
7883       return "DW_FORM_<unknown>";
7884     }
7885 }
7886
7887 /* Convert a DWARF stack opcode into its string name.  */
7888
7889 static char *
7890 dwarf_stack_op_name (unsigned op)
7891 {
7892   switch (op)
7893     {
7894     case DW_OP_addr:
7895       return "DW_OP_addr";
7896     case DW_OP_deref:
7897       return "DW_OP_deref";
7898     case DW_OP_const1u:
7899       return "DW_OP_const1u";
7900     case DW_OP_const1s:
7901       return "DW_OP_const1s";
7902     case DW_OP_const2u:
7903       return "DW_OP_const2u";
7904     case DW_OP_const2s:
7905       return "DW_OP_const2s";
7906     case DW_OP_const4u:
7907       return "DW_OP_const4u";
7908     case DW_OP_const4s:
7909       return "DW_OP_const4s";
7910     case DW_OP_const8u:
7911       return "DW_OP_const8u";
7912     case DW_OP_const8s:
7913       return "DW_OP_const8s";
7914     case DW_OP_constu:
7915       return "DW_OP_constu";
7916     case DW_OP_consts:
7917       return "DW_OP_consts";
7918     case DW_OP_dup:
7919       return "DW_OP_dup";
7920     case DW_OP_drop:
7921       return "DW_OP_drop";
7922     case DW_OP_over:
7923       return "DW_OP_over";
7924     case DW_OP_pick:
7925       return "DW_OP_pick";
7926     case DW_OP_swap:
7927       return "DW_OP_swap";
7928     case DW_OP_rot:
7929       return "DW_OP_rot";
7930     case DW_OP_xderef:
7931       return "DW_OP_xderef";
7932     case DW_OP_abs:
7933       return "DW_OP_abs";
7934     case DW_OP_and:
7935       return "DW_OP_and";
7936     case DW_OP_div:
7937       return "DW_OP_div";
7938     case DW_OP_minus:
7939       return "DW_OP_minus";
7940     case DW_OP_mod:
7941       return "DW_OP_mod";
7942     case DW_OP_mul:
7943       return "DW_OP_mul";
7944     case DW_OP_neg:
7945       return "DW_OP_neg";
7946     case DW_OP_not:
7947       return "DW_OP_not";
7948     case DW_OP_or:
7949       return "DW_OP_or";
7950     case DW_OP_plus:
7951       return "DW_OP_plus";
7952     case DW_OP_plus_uconst:
7953       return "DW_OP_plus_uconst";
7954     case DW_OP_shl:
7955       return "DW_OP_shl";
7956     case DW_OP_shr:
7957       return "DW_OP_shr";
7958     case DW_OP_shra:
7959       return "DW_OP_shra";
7960     case DW_OP_xor:
7961       return "DW_OP_xor";
7962     case DW_OP_bra:
7963       return "DW_OP_bra";
7964     case DW_OP_eq:
7965       return "DW_OP_eq";
7966     case DW_OP_ge:
7967       return "DW_OP_ge";
7968     case DW_OP_gt:
7969       return "DW_OP_gt";
7970     case DW_OP_le:
7971       return "DW_OP_le";
7972     case DW_OP_lt:
7973       return "DW_OP_lt";
7974     case DW_OP_ne:
7975       return "DW_OP_ne";
7976     case DW_OP_skip:
7977       return "DW_OP_skip";
7978     case DW_OP_lit0:
7979       return "DW_OP_lit0";
7980     case DW_OP_lit1:
7981       return "DW_OP_lit1";
7982     case DW_OP_lit2:
7983       return "DW_OP_lit2";
7984     case DW_OP_lit3:
7985       return "DW_OP_lit3";
7986     case DW_OP_lit4:
7987       return "DW_OP_lit4";
7988     case DW_OP_lit5:
7989       return "DW_OP_lit5";
7990     case DW_OP_lit6:
7991       return "DW_OP_lit6";
7992     case DW_OP_lit7:
7993       return "DW_OP_lit7";
7994     case DW_OP_lit8:
7995       return "DW_OP_lit8";
7996     case DW_OP_lit9:
7997       return "DW_OP_lit9";
7998     case DW_OP_lit10:
7999       return "DW_OP_lit10";
8000     case DW_OP_lit11:
8001       return "DW_OP_lit11";
8002     case DW_OP_lit12:
8003       return "DW_OP_lit12";
8004     case DW_OP_lit13:
8005       return "DW_OP_lit13";
8006     case DW_OP_lit14:
8007       return "DW_OP_lit14";
8008     case DW_OP_lit15:
8009       return "DW_OP_lit15";
8010     case DW_OP_lit16:
8011       return "DW_OP_lit16";
8012     case DW_OP_lit17:
8013       return "DW_OP_lit17";
8014     case DW_OP_lit18:
8015       return "DW_OP_lit18";
8016     case DW_OP_lit19:
8017       return "DW_OP_lit19";
8018     case DW_OP_lit20:
8019       return "DW_OP_lit20";
8020     case DW_OP_lit21:
8021       return "DW_OP_lit21";
8022     case DW_OP_lit22:
8023       return "DW_OP_lit22";
8024     case DW_OP_lit23:
8025       return "DW_OP_lit23";
8026     case DW_OP_lit24:
8027       return "DW_OP_lit24";
8028     case DW_OP_lit25:
8029       return "DW_OP_lit25";
8030     case DW_OP_lit26:
8031       return "DW_OP_lit26";
8032     case DW_OP_lit27:
8033       return "DW_OP_lit27";
8034     case DW_OP_lit28:
8035       return "DW_OP_lit28";
8036     case DW_OP_lit29:
8037       return "DW_OP_lit29";
8038     case DW_OP_lit30:
8039       return "DW_OP_lit30";
8040     case DW_OP_lit31:
8041       return "DW_OP_lit31";
8042     case DW_OP_reg0:
8043       return "DW_OP_reg0";
8044     case DW_OP_reg1:
8045       return "DW_OP_reg1";
8046     case DW_OP_reg2:
8047       return "DW_OP_reg2";
8048     case DW_OP_reg3:
8049       return "DW_OP_reg3";
8050     case DW_OP_reg4:
8051       return "DW_OP_reg4";
8052     case DW_OP_reg5:
8053       return "DW_OP_reg5";
8054     case DW_OP_reg6:
8055       return "DW_OP_reg6";
8056     case DW_OP_reg7:
8057       return "DW_OP_reg7";
8058     case DW_OP_reg8:
8059       return "DW_OP_reg8";
8060     case DW_OP_reg9:
8061       return "DW_OP_reg9";
8062     case DW_OP_reg10:
8063       return "DW_OP_reg10";
8064     case DW_OP_reg11:
8065       return "DW_OP_reg11";
8066     case DW_OP_reg12:
8067       return "DW_OP_reg12";
8068     case DW_OP_reg13:
8069       return "DW_OP_reg13";
8070     case DW_OP_reg14:
8071       return "DW_OP_reg14";
8072     case DW_OP_reg15:
8073       return "DW_OP_reg15";
8074     case DW_OP_reg16:
8075       return "DW_OP_reg16";
8076     case DW_OP_reg17:
8077       return "DW_OP_reg17";
8078     case DW_OP_reg18:
8079       return "DW_OP_reg18";
8080     case DW_OP_reg19:
8081       return "DW_OP_reg19";
8082     case DW_OP_reg20:
8083       return "DW_OP_reg20";
8084     case DW_OP_reg21:
8085       return "DW_OP_reg21";
8086     case DW_OP_reg22:
8087       return "DW_OP_reg22";
8088     case DW_OP_reg23:
8089       return "DW_OP_reg23";
8090     case DW_OP_reg24:
8091       return "DW_OP_reg24";
8092     case DW_OP_reg25:
8093       return "DW_OP_reg25";
8094     case DW_OP_reg26:
8095       return "DW_OP_reg26";
8096     case DW_OP_reg27:
8097       return "DW_OP_reg27";
8098     case DW_OP_reg28:
8099       return "DW_OP_reg28";
8100     case DW_OP_reg29:
8101       return "DW_OP_reg29";
8102     case DW_OP_reg30:
8103       return "DW_OP_reg30";
8104     case DW_OP_reg31:
8105       return "DW_OP_reg31";
8106     case DW_OP_breg0:
8107       return "DW_OP_breg0";
8108     case DW_OP_breg1:
8109       return "DW_OP_breg1";
8110     case DW_OP_breg2:
8111       return "DW_OP_breg2";
8112     case DW_OP_breg3:
8113       return "DW_OP_breg3";
8114     case DW_OP_breg4:
8115       return "DW_OP_breg4";
8116     case DW_OP_breg5:
8117       return "DW_OP_breg5";
8118     case DW_OP_breg6:
8119       return "DW_OP_breg6";
8120     case DW_OP_breg7:
8121       return "DW_OP_breg7";
8122     case DW_OP_breg8:
8123       return "DW_OP_breg8";
8124     case DW_OP_breg9:
8125       return "DW_OP_breg9";
8126     case DW_OP_breg10:
8127       return "DW_OP_breg10";
8128     case DW_OP_breg11:
8129       return "DW_OP_breg11";
8130     case DW_OP_breg12:
8131       return "DW_OP_breg12";
8132     case DW_OP_breg13:
8133       return "DW_OP_breg13";
8134     case DW_OP_breg14:
8135       return "DW_OP_breg14";
8136     case DW_OP_breg15:
8137       return "DW_OP_breg15";
8138     case DW_OP_breg16:
8139       return "DW_OP_breg16";
8140     case DW_OP_breg17:
8141       return "DW_OP_breg17";
8142     case DW_OP_breg18:
8143       return "DW_OP_breg18";
8144     case DW_OP_breg19:
8145       return "DW_OP_breg19";
8146     case DW_OP_breg20:
8147       return "DW_OP_breg20";
8148     case DW_OP_breg21:
8149       return "DW_OP_breg21";
8150     case DW_OP_breg22:
8151       return "DW_OP_breg22";
8152     case DW_OP_breg23:
8153       return "DW_OP_breg23";
8154     case DW_OP_breg24:
8155       return "DW_OP_breg24";
8156     case DW_OP_breg25:
8157       return "DW_OP_breg25";
8158     case DW_OP_breg26:
8159       return "DW_OP_breg26";
8160     case DW_OP_breg27:
8161       return "DW_OP_breg27";
8162     case DW_OP_breg28:
8163       return "DW_OP_breg28";
8164     case DW_OP_breg29:
8165       return "DW_OP_breg29";
8166     case DW_OP_breg30:
8167       return "DW_OP_breg30";
8168     case DW_OP_breg31:
8169       return "DW_OP_breg31";
8170     case DW_OP_regx:
8171       return "DW_OP_regx";
8172     case DW_OP_fbreg:
8173       return "DW_OP_fbreg";
8174     case DW_OP_bregx:
8175       return "DW_OP_bregx";
8176     case DW_OP_piece:
8177       return "DW_OP_piece";
8178     case DW_OP_deref_size:
8179       return "DW_OP_deref_size";
8180     case DW_OP_xderef_size:
8181       return "DW_OP_xderef_size";
8182     case DW_OP_nop:
8183       return "DW_OP_nop";
8184       /* DWARF 3 extensions.  */
8185     case DW_OP_push_object_address:
8186       return "DW_OP_push_object_address";
8187     case DW_OP_call2:
8188       return "DW_OP_call2";
8189     case DW_OP_call4:
8190       return "DW_OP_call4";
8191     case DW_OP_call_ref:
8192       return "DW_OP_call_ref";
8193       /* GNU extensions.  */
8194     case DW_OP_GNU_push_tls_address:
8195       return "DW_OP_GNU_push_tls_address";
8196     default:
8197       return "OP_<unknown>";
8198     }
8199 }
8200
8201 static char *
8202 dwarf_bool_name (unsigned mybool)
8203 {
8204   if (mybool)
8205     return "TRUE";
8206   else
8207     return "FALSE";
8208 }
8209
8210 /* Convert a DWARF type code into its string name.  */
8211
8212 static char *
8213 dwarf_type_encoding_name (unsigned enc)
8214 {
8215   switch (enc)
8216     {
8217     case DW_ATE_address:
8218       return "DW_ATE_address";
8219     case DW_ATE_boolean:
8220       return "DW_ATE_boolean";
8221     case DW_ATE_complex_float:
8222       return "DW_ATE_complex_float";
8223     case DW_ATE_float:
8224       return "DW_ATE_float";
8225     case DW_ATE_signed:
8226       return "DW_ATE_signed";
8227     case DW_ATE_signed_char:
8228       return "DW_ATE_signed_char";
8229     case DW_ATE_unsigned:
8230       return "DW_ATE_unsigned";
8231     case DW_ATE_unsigned_char:
8232       return "DW_ATE_unsigned_char";
8233     case DW_ATE_imaginary_float:
8234       return "DW_ATE_imaginary_float";
8235     default:
8236       return "DW_ATE_<unknown>";
8237     }
8238 }
8239
8240 /* Convert a DWARF call frame info operation to its string name. */
8241
8242 #if 0
8243 static char *
8244 dwarf_cfi_name (unsigned cfi_opc)
8245 {
8246   switch (cfi_opc)
8247     {
8248     case DW_CFA_advance_loc:
8249       return "DW_CFA_advance_loc";
8250     case DW_CFA_offset:
8251       return "DW_CFA_offset";
8252     case DW_CFA_restore:
8253       return "DW_CFA_restore";
8254     case DW_CFA_nop:
8255       return "DW_CFA_nop";
8256     case DW_CFA_set_loc:
8257       return "DW_CFA_set_loc";
8258     case DW_CFA_advance_loc1:
8259       return "DW_CFA_advance_loc1";
8260     case DW_CFA_advance_loc2:
8261       return "DW_CFA_advance_loc2";
8262     case DW_CFA_advance_loc4:
8263       return "DW_CFA_advance_loc4";
8264     case DW_CFA_offset_extended:
8265       return "DW_CFA_offset_extended";
8266     case DW_CFA_restore_extended:
8267       return "DW_CFA_restore_extended";
8268     case DW_CFA_undefined:
8269       return "DW_CFA_undefined";
8270     case DW_CFA_same_value:
8271       return "DW_CFA_same_value";
8272     case DW_CFA_register:
8273       return "DW_CFA_register";
8274     case DW_CFA_remember_state:
8275       return "DW_CFA_remember_state";
8276     case DW_CFA_restore_state:
8277       return "DW_CFA_restore_state";
8278     case DW_CFA_def_cfa:
8279       return "DW_CFA_def_cfa";
8280     case DW_CFA_def_cfa_register:
8281       return "DW_CFA_def_cfa_register";
8282     case DW_CFA_def_cfa_offset:
8283       return "DW_CFA_def_cfa_offset";
8284
8285     /* DWARF 3 */
8286     case DW_CFA_def_cfa_expression:
8287       return "DW_CFA_def_cfa_expression";
8288     case DW_CFA_expression:
8289       return "DW_CFA_expression";
8290     case DW_CFA_offset_extended_sf:
8291       return "DW_CFA_offset_extended_sf";
8292     case DW_CFA_def_cfa_sf:
8293       return "DW_CFA_def_cfa_sf";
8294     case DW_CFA_def_cfa_offset_sf:
8295       return "DW_CFA_def_cfa_offset_sf";
8296
8297       /* SGI/MIPS specific */
8298     case DW_CFA_MIPS_advance_loc8:
8299       return "DW_CFA_MIPS_advance_loc8";
8300
8301     /* GNU extensions */
8302     case DW_CFA_GNU_window_save:
8303       return "DW_CFA_GNU_window_save";
8304     case DW_CFA_GNU_args_size:
8305       return "DW_CFA_GNU_args_size";
8306     case DW_CFA_GNU_negative_offset_extended:
8307       return "DW_CFA_GNU_negative_offset_extended";
8308
8309     default:
8310       return "DW_CFA_<unknown>";
8311     }
8312 }
8313 #endif
8314
8315 static void
8316 dump_die (struct die_info *die)
8317 {
8318   unsigned int i;
8319
8320   fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
8321            dwarf_tag_name (die->tag), die->abbrev, die->offset);
8322   fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
8323            dwarf_bool_name (die->child != NULL));
8324
8325   fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
8326   for (i = 0; i < die->num_attrs; ++i)
8327     {
8328       fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
8329                dwarf_attr_name (die->attrs[i].name),
8330                dwarf_form_name (die->attrs[i].form));
8331       switch (die->attrs[i].form)
8332         {
8333         case DW_FORM_ref_addr:
8334         case DW_FORM_addr:
8335           fprintf_unfiltered (gdb_stderr, "address: ");
8336           print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
8337           break;
8338         case DW_FORM_block2:
8339         case DW_FORM_block4:
8340         case DW_FORM_block:
8341         case DW_FORM_block1:
8342           fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
8343           break;
8344         case DW_FORM_ref1:
8345         case DW_FORM_ref2:
8346         case DW_FORM_ref4:
8347           fprintf_unfiltered (gdb_stderr, "constant ref: %ld (adjusted)",
8348                               (long) (DW_ADDR (&die->attrs[i])));
8349           break;
8350         case DW_FORM_data1:
8351         case DW_FORM_data2:
8352         case DW_FORM_data4:
8353         case DW_FORM_data8:
8354         case DW_FORM_udata:
8355         case DW_FORM_sdata:
8356           fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
8357           break;
8358         case DW_FORM_string:
8359         case DW_FORM_strp:
8360           fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
8361                    DW_STRING (&die->attrs[i])
8362                    ? DW_STRING (&die->attrs[i]) : "");
8363           break;
8364         case DW_FORM_flag:
8365           if (DW_UNSND (&die->attrs[i]))
8366             fprintf_unfiltered (gdb_stderr, "flag: TRUE");
8367           else
8368             fprintf_unfiltered (gdb_stderr, "flag: FALSE");
8369           break;
8370         case DW_FORM_indirect:
8371           /* the reader will have reduced the indirect form to
8372              the "base form" so this form should not occur */
8373           fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
8374           break;
8375         default:
8376           fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
8377                    die->attrs[i].form);
8378         }
8379       fprintf_unfiltered (gdb_stderr, "\n");
8380     }
8381 }
8382
8383 static void
8384 dump_die_list (struct die_info *die)
8385 {
8386   while (die)
8387     {
8388       dump_die (die);
8389       if (die->child != NULL)
8390         dump_die_list (die->child);
8391       if (die->sibling != NULL)
8392         dump_die_list (die->sibling);
8393     }
8394 }
8395
8396 static void
8397 store_in_ref_table (unsigned int offset, struct die_info *die,
8398                     struct dwarf2_cu *cu)
8399 {
8400   int h;
8401   struct die_info *old;
8402
8403   h = (offset % REF_HASH_SIZE);
8404   old = cu->die_ref_table[h];
8405   die->next_ref = old;
8406   cu->die_ref_table[h] = die;
8407 }
8408
8409 static unsigned int
8410 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
8411 {
8412   unsigned int result = 0;
8413
8414   switch (attr->form)
8415     {
8416     case DW_FORM_ref_addr:
8417     case DW_FORM_ref1:
8418     case DW_FORM_ref2:
8419     case DW_FORM_ref4:
8420     case DW_FORM_ref8:
8421     case DW_FORM_ref_udata:
8422       result = DW_ADDR (attr);
8423       break;
8424     default:
8425       complaint (&symfile_complaints,
8426                  "unsupported die ref attribute form: '%s'",
8427                  dwarf_form_name (attr->form));
8428     }
8429   return result;
8430 }
8431
8432 /* Return the constant value held by the given attribute.  Return -1
8433    if the value held by the attribute is not constant.  */
8434
8435 static int
8436 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
8437 {
8438   if (attr->form == DW_FORM_sdata)
8439     return DW_SND (attr);
8440   else if (attr->form == DW_FORM_udata
8441            || attr->form == DW_FORM_data1
8442            || attr->form == DW_FORM_data2
8443            || attr->form == DW_FORM_data4
8444            || attr->form == DW_FORM_data8)
8445     return DW_UNSND (attr);
8446   else
8447     {
8448       complaint (&symfile_complaints, "Attribute value is not a constant (%s)",
8449                  dwarf_form_name (attr->form));
8450       return default_value;
8451     }
8452 }
8453
8454 static struct die_info *
8455 follow_die_ref (struct die_info *src_die, struct attribute *attr,
8456                 struct dwarf2_cu *cu)
8457 {
8458   struct die_info *die;
8459   unsigned int offset;
8460   int h;
8461   struct die_info temp_die;
8462   struct dwarf2_cu *target_cu;
8463
8464   offset = dwarf2_get_ref_die_offset (attr, cu);
8465
8466   if (DW_ADDR (attr) < cu->header.offset
8467       || DW_ADDR (attr) >= cu->header.offset + cu->header.length)
8468     {
8469       struct dwarf2_per_cu_data *per_cu;
8470       per_cu = dwarf2_find_containing_comp_unit (DW_ADDR (attr),
8471                                                  cu->objfile);
8472       target_cu = per_cu->cu;
8473     }
8474   else
8475     target_cu = cu;
8476
8477   h = (offset % REF_HASH_SIZE);
8478   die = target_cu->die_ref_table[h];
8479   while (die)
8480     {
8481       if (die->offset == offset)
8482         return die;
8483       die = die->next_ref;
8484     }
8485
8486   error ("Dwarf Error: Cannot find DIE at 0x%lx referenced from DIE "
8487          "at 0x%lx [in module %s]",
8488          (long) src_die->offset, (long) offset, cu->objfile->name);
8489
8490   return NULL;
8491 }
8492
8493 static struct type *
8494 dwarf2_fundamental_type (struct objfile *objfile, int typeid,
8495                          struct dwarf2_cu *cu)
8496 {
8497   if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
8498     {
8499       error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
8500              typeid, objfile->name);
8501     }
8502
8503   /* Look for this particular type in the fundamental type vector.  If
8504      one is not found, create and install one appropriate for the
8505      current language and the current target machine. */
8506
8507   if (cu->ftypes[typeid] == NULL)
8508     {
8509       cu->ftypes[typeid] = cu->language_defn->la_fund_type (objfile, typeid);
8510     }
8511
8512   return (cu->ftypes[typeid]);
8513 }
8514
8515 /* Decode simple location descriptions.
8516    Given a pointer to a dwarf block that defines a location, compute
8517    the location and return the value.
8518
8519    NOTE drow/2003-11-18: This function is called in two situations
8520    now: for the address of static or global variables (partial symbols
8521    only) and for offsets into structures which are expected to be
8522    (more or less) constant.  The partial symbol case should go away,
8523    and only the constant case should remain.  That will let this
8524    function complain more accurately.  A few special modes are allowed
8525    without complaint for global variables (for instance, global
8526    register values and thread-local values).
8527
8528    A location description containing no operations indicates that the
8529    object is optimized out.  The return value is 0 for that case.
8530    FIXME drow/2003-11-16: No callers check for this case any more; soon all
8531    callers will only want a very basic result and this can become a
8532    complaint.
8533
8534    When the result is a register number, the global isreg flag is set,
8535    otherwise it is cleared.
8536
8537    Note that stack[0] is unused except as a default error return.
8538    Note that stack overflow is not yet handled.  */
8539
8540 static CORE_ADDR
8541 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
8542 {
8543   struct objfile *objfile = cu->objfile;
8544   struct comp_unit_head *cu_header = &cu->header;
8545   int i;
8546   int size = blk->size;
8547   char *data = blk->data;
8548   CORE_ADDR stack[64];
8549   int stacki;
8550   unsigned int bytes_read, unsnd;
8551   unsigned char op;
8552
8553   i = 0;
8554   stacki = 0;
8555   stack[stacki] = 0;
8556   isreg = 0;
8557
8558   while (i < size)
8559     {
8560       op = data[i++];
8561       switch (op)
8562         {
8563         case DW_OP_lit0:
8564         case DW_OP_lit1:
8565         case DW_OP_lit2:
8566         case DW_OP_lit3:
8567         case DW_OP_lit4:
8568         case DW_OP_lit5:
8569         case DW_OP_lit6:
8570         case DW_OP_lit7:
8571         case DW_OP_lit8:
8572         case DW_OP_lit9:
8573         case DW_OP_lit10:
8574         case DW_OP_lit11:
8575         case DW_OP_lit12:
8576         case DW_OP_lit13:
8577         case DW_OP_lit14:
8578         case DW_OP_lit15:
8579         case DW_OP_lit16:
8580         case DW_OP_lit17:
8581         case DW_OP_lit18:
8582         case DW_OP_lit19:
8583         case DW_OP_lit20:
8584         case DW_OP_lit21:
8585         case DW_OP_lit22:
8586         case DW_OP_lit23:
8587         case DW_OP_lit24:
8588         case DW_OP_lit25:
8589         case DW_OP_lit26:
8590         case DW_OP_lit27:
8591         case DW_OP_lit28:
8592         case DW_OP_lit29:
8593         case DW_OP_lit30:
8594         case DW_OP_lit31:
8595           stack[++stacki] = op - DW_OP_lit0;
8596           break;
8597
8598         case DW_OP_reg0:
8599         case DW_OP_reg1:
8600         case DW_OP_reg2:
8601         case DW_OP_reg3:
8602         case DW_OP_reg4:
8603         case DW_OP_reg5:
8604         case DW_OP_reg6:
8605         case DW_OP_reg7:
8606         case DW_OP_reg8:
8607         case DW_OP_reg9:
8608         case DW_OP_reg10:
8609         case DW_OP_reg11:
8610         case DW_OP_reg12:
8611         case DW_OP_reg13:
8612         case DW_OP_reg14:
8613         case DW_OP_reg15:
8614         case DW_OP_reg16:
8615         case DW_OP_reg17:
8616         case DW_OP_reg18:
8617         case DW_OP_reg19:
8618         case DW_OP_reg20:
8619         case DW_OP_reg21:
8620         case DW_OP_reg22:
8621         case DW_OP_reg23:
8622         case DW_OP_reg24:
8623         case DW_OP_reg25:
8624         case DW_OP_reg26:
8625         case DW_OP_reg27:
8626         case DW_OP_reg28:
8627         case DW_OP_reg29:
8628         case DW_OP_reg30:
8629         case DW_OP_reg31:
8630           isreg = 1;
8631           stack[++stacki] = op - DW_OP_reg0;
8632           if (i < size)
8633             dwarf2_complex_location_expr_complaint ();
8634           break;
8635
8636         case DW_OP_regx:
8637           isreg = 1;
8638           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8639           i += bytes_read;
8640           stack[++stacki] = unsnd;
8641           if (i < size)
8642             dwarf2_complex_location_expr_complaint ();
8643           break;
8644
8645         case DW_OP_addr:
8646           stack[++stacki] = read_address (objfile->obfd, &data[i],
8647                                           cu, &bytes_read);
8648           i += bytes_read;
8649           break;
8650
8651         case DW_OP_const1u:
8652           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
8653           i += 1;
8654           break;
8655
8656         case DW_OP_const1s:
8657           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
8658           i += 1;
8659           break;
8660
8661         case DW_OP_const2u:
8662           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
8663           i += 2;
8664           break;
8665
8666         case DW_OP_const2s:
8667           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
8668           i += 2;
8669           break;
8670
8671         case DW_OP_const4u:
8672           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
8673           i += 4;
8674           break;
8675
8676         case DW_OP_const4s:
8677           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
8678           i += 4;
8679           break;
8680
8681         case DW_OP_constu:
8682           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
8683                                                   &bytes_read);
8684           i += bytes_read;
8685           break;
8686
8687         case DW_OP_consts:
8688           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
8689           i += bytes_read;
8690           break;
8691
8692         case DW_OP_dup:
8693           stack[stacki + 1] = stack[stacki];
8694           stacki++;
8695           break;
8696
8697         case DW_OP_plus:
8698           stack[stacki - 1] += stack[stacki];
8699           stacki--;
8700           break;
8701
8702         case DW_OP_plus_uconst:
8703           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8704           i += bytes_read;
8705           break;
8706
8707         case DW_OP_minus:
8708           stack[stacki - 1] -= stack[stacki];
8709           stacki--;
8710           break;
8711
8712         case DW_OP_deref:
8713           /* If we're not the last op, then we definitely can't encode
8714              this using GDB's address_class enum.  This is valid for partial
8715              global symbols, although the variable's address will be bogus
8716              in the psymtab.  */
8717           if (i < size)
8718             dwarf2_complex_location_expr_complaint ();
8719           break;
8720
8721         case DW_OP_GNU_push_tls_address:
8722           /* The top of the stack has the offset from the beginning
8723              of the thread control block at which the variable is located.  */
8724           /* Nothing should follow this operator, so the top of stack would
8725              be returned.  */
8726           /* This is valid for partial global symbols, but the variable's
8727              address will be bogus in the psymtab.  */
8728           if (i < size)
8729             dwarf2_complex_location_expr_complaint ();
8730           break;
8731
8732         default:
8733           complaint (&symfile_complaints, "unsupported stack op: '%s'",
8734                      dwarf_stack_op_name (op));
8735           return (stack[stacki]);
8736         }
8737     }
8738   return (stack[stacki]);
8739 }
8740
8741 /* memory allocation interface */
8742
8743 static struct dwarf_block *
8744 dwarf_alloc_block (struct dwarf2_cu *cu)
8745 {
8746   struct dwarf_block *blk;
8747
8748   blk = (struct dwarf_block *)
8749     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
8750   return (blk);
8751 }
8752
8753 static struct abbrev_info *
8754 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
8755 {
8756   struct abbrev_info *abbrev;
8757
8758   abbrev = (struct abbrev_info *)
8759     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
8760   memset (abbrev, 0, sizeof (struct abbrev_info));
8761   return (abbrev);
8762 }
8763
8764 static struct die_info *
8765 dwarf_alloc_die (void)
8766 {
8767   struct die_info *die;
8768
8769   die = (struct die_info *) xmalloc (sizeof (struct die_info));
8770   memset (die, 0, sizeof (struct die_info));
8771   return (die);
8772 }
8773
8774 \f
8775 /* Macro support.  */
8776
8777
8778 /* Return the full name of file number I in *LH's file name table.
8779    Use COMP_DIR as the name of the current directory of the
8780    compilation.  The result is allocated using xmalloc; the caller is
8781    responsible for freeing it.  */
8782 static char *
8783 file_full_name (int file, struct line_header *lh, const char *comp_dir)
8784 {
8785   struct file_entry *fe = &lh->file_names[file - 1];
8786   
8787   if (IS_ABSOLUTE_PATH (fe->name))
8788     return xstrdup (fe->name);
8789   else
8790     {
8791       const char *dir;
8792       int dir_len;
8793       char *full_name;
8794
8795       if (fe->dir_index)
8796         dir = lh->include_dirs[fe->dir_index - 1];
8797       else
8798         dir = comp_dir;
8799
8800       if (dir)
8801         {
8802           dir_len = strlen (dir);
8803           full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
8804           strcpy (full_name, dir);
8805           full_name[dir_len] = '/';
8806           strcpy (full_name + dir_len + 1, fe->name);
8807           return full_name;
8808         }
8809       else
8810         return xstrdup (fe->name);
8811     }
8812 }
8813
8814
8815 static struct macro_source_file *
8816 macro_start_file (int file, int line,
8817                   struct macro_source_file *current_file,
8818                   const char *comp_dir,
8819                   struct line_header *lh, struct objfile *objfile)
8820 {
8821   /* The full name of this source file.  */
8822   char *full_name = file_full_name (file, lh, comp_dir);
8823
8824   /* We don't create a macro table for this compilation unit
8825      at all until we actually get a filename.  */
8826   if (! pending_macros)
8827     pending_macros = new_macro_table (&objfile->objfile_obstack,
8828                                       objfile->macro_cache);
8829
8830   if (! current_file)
8831     /* If we have no current file, then this must be the start_file
8832        directive for the compilation unit's main source file.  */
8833     current_file = macro_set_main (pending_macros, full_name);
8834   else
8835     current_file = macro_include (current_file, line, full_name);
8836
8837   xfree (full_name);
8838               
8839   return current_file;
8840 }
8841
8842
8843 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
8844    followed by a null byte.  */
8845 static char *
8846 copy_string (const char *buf, int len)
8847 {
8848   char *s = xmalloc (len + 1);
8849   memcpy (s, buf, len);
8850   s[len] = '\0';
8851
8852   return s;
8853 }
8854
8855
8856 static const char *
8857 consume_improper_spaces (const char *p, const char *body)
8858 {
8859   if (*p == ' ')
8860     {
8861       complaint (&symfile_complaints,
8862                  "macro definition contains spaces in formal argument list:\n`%s'",
8863                  body);
8864
8865       while (*p == ' ')
8866         p++;
8867     }
8868
8869   return p;
8870 }
8871
8872
8873 static void
8874 parse_macro_definition (struct macro_source_file *file, int line,
8875                         const char *body)
8876 {
8877   const char *p;
8878
8879   /* The body string takes one of two forms.  For object-like macro
8880      definitions, it should be:
8881
8882         <macro name> " " <definition>
8883
8884      For function-like macro definitions, it should be:
8885
8886         <macro name> "() " <definition>
8887      or
8888         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
8889
8890      Spaces may appear only where explicitly indicated, and in the
8891      <definition>.
8892
8893      The Dwarf 2 spec says that an object-like macro's name is always
8894      followed by a space, but versions of GCC around March 2002 omit
8895      the space when the macro's definition is the empty string. 
8896
8897      The Dwarf 2 spec says that there should be no spaces between the
8898      formal arguments in a function-like macro's formal argument list,
8899      but versions of GCC around March 2002 include spaces after the
8900      commas.  */
8901
8902
8903   /* Find the extent of the macro name.  The macro name is terminated
8904      by either a space or null character (for an object-like macro) or
8905      an opening paren (for a function-like macro).  */
8906   for (p = body; *p; p++)
8907     if (*p == ' ' || *p == '(')
8908       break;
8909
8910   if (*p == ' ' || *p == '\0')
8911     {
8912       /* It's an object-like macro.  */
8913       int name_len = p - body;
8914       char *name = copy_string (body, name_len);
8915       const char *replacement;
8916
8917       if (*p == ' ')
8918         replacement = body + name_len + 1;
8919       else
8920         {
8921           dwarf2_macro_malformed_definition_complaint (body);
8922           replacement = body + name_len;
8923         }
8924       
8925       macro_define_object (file, line, name, replacement);
8926
8927       xfree (name);
8928     }
8929   else if (*p == '(')
8930     {
8931       /* It's a function-like macro.  */
8932       char *name = copy_string (body, p - body);
8933       int argc = 0;
8934       int argv_size = 1;
8935       char **argv = xmalloc (argv_size * sizeof (*argv));
8936
8937       p++;
8938
8939       p = consume_improper_spaces (p, body);
8940
8941       /* Parse the formal argument list.  */
8942       while (*p && *p != ')')
8943         {
8944           /* Find the extent of the current argument name.  */
8945           const char *arg_start = p;
8946
8947           while (*p && *p != ',' && *p != ')' && *p != ' ')
8948             p++;
8949
8950           if (! *p || p == arg_start)
8951             dwarf2_macro_malformed_definition_complaint (body);
8952           else
8953             {
8954               /* Make sure argv has room for the new argument.  */
8955               if (argc >= argv_size)
8956                 {
8957                   argv_size *= 2;
8958                   argv = xrealloc (argv, argv_size * sizeof (*argv));
8959                 }
8960
8961               argv[argc++] = copy_string (arg_start, p - arg_start);
8962             }
8963
8964           p = consume_improper_spaces (p, body);
8965
8966           /* Consume the comma, if present.  */
8967           if (*p == ',')
8968             {
8969               p++;
8970
8971               p = consume_improper_spaces (p, body);
8972             }
8973         }
8974
8975       if (*p == ')')
8976         {
8977           p++;
8978
8979           if (*p == ' ')
8980             /* Perfectly formed definition, no complaints.  */
8981             macro_define_function (file, line, name,
8982                                    argc, (const char **) argv, 
8983                                    p + 1);
8984           else if (*p == '\0')
8985             {
8986               /* Complain, but do define it.  */
8987               dwarf2_macro_malformed_definition_complaint (body);
8988               macro_define_function (file, line, name,
8989                                      argc, (const char **) argv, 
8990                                      p);
8991             }
8992           else
8993             /* Just complain.  */
8994             dwarf2_macro_malformed_definition_complaint (body);
8995         }
8996       else
8997         /* Just complain.  */
8998         dwarf2_macro_malformed_definition_complaint (body);
8999
9000       xfree (name);
9001       {
9002         int i;
9003
9004         for (i = 0; i < argc; i++)
9005           xfree (argv[i]);
9006       }
9007       xfree (argv);
9008     }
9009   else
9010     dwarf2_macro_malformed_definition_complaint (body);
9011 }
9012
9013
9014 static void
9015 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
9016                      char *comp_dir, bfd *abfd,
9017                      struct dwarf2_cu *cu)
9018 {
9019   char *mac_ptr, *mac_end;
9020   struct macro_source_file *current_file = 0;
9021
9022   if (dwarf2_per_objfile->macinfo_buffer == NULL)
9023     {
9024       complaint (&symfile_complaints, "missing .debug_macinfo section");
9025       return;
9026     }
9027
9028   mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
9029   mac_end = dwarf2_per_objfile->macinfo_buffer
9030     + dwarf2_per_objfile->macinfo_size;
9031
9032   for (;;)
9033     {
9034       enum dwarf_macinfo_record_type macinfo_type;
9035
9036       /* Do we at least have room for a macinfo type byte?  */
9037       if (mac_ptr >= mac_end)
9038         {
9039           dwarf2_macros_too_long_complaint ();
9040           return;
9041         }
9042
9043       macinfo_type = read_1_byte (abfd, mac_ptr);
9044       mac_ptr++;
9045
9046       switch (macinfo_type)
9047         {
9048           /* A zero macinfo type indicates the end of the macro
9049              information.  */
9050         case 0:
9051           return;
9052
9053         case DW_MACINFO_define:
9054         case DW_MACINFO_undef:
9055           {
9056             int bytes_read;
9057             int line;
9058             char *body;
9059
9060             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9061             mac_ptr += bytes_read;
9062             body = read_string (abfd, mac_ptr, &bytes_read);
9063             mac_ptr += bytes_read;
9064
9065             if (! current_file)
9066               complaint (&symfile_complaints,
9067                          "debug info gives macro %s outside of any file: %s",
9068                          macinfo_type ==
9069                          DW_MACINFO_define ? "definition" : macinfo_type ==
9070                          DW_MACINFO_undef ? "undefinition" :
9071                          "something-or-other", body);
9072             else
9073               {
9074                 if (macinfo_type == DW_MACINFO_define)
9075                   parse_macro_definition (current_file, line, body);
9076                 else if (macinfo_type == DW_MACINFO_undef)
9077                   macro_undef (current_file, line, body);
9078               }
9079           }
9080           break;
9081
9082         case DW_MACINFO_start_file:
9083           {
9084             int bytes_read;
9085             int line, file;
9086
9087             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9088             mac_ptr += bytes_read;
9089             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9090             mac_ptr += bytes_read;
9091
9092             current_file = macro_start_file (file, line,
9093                                              current_file, comp_dir,
9094                                              lh, cu->objfile);
9095           }
9096           break;
9097
9098         case DW_MACINFO_end_file:
9099           if (! current_file)
9100             complaint (&symfile_complaints,
9101                        "macro debug info has an unmatched `close_file' directive");
9102           else
9103             {
9104               current_file = current_file->included_by;
9105               if (! current_file)
9106                 {
9107                   enum dwarf_macinfo_record_type next_type;
9108
9109                   /* GCC circa March 2002 doesn't produce the zero
9110                      type byte marking the end of the compilation
9111                      unit.  Complain if it's not there, but exit no
9112                      matter what.  */
9113
9114                   /* Do we at least have room for a macinfo type byte?  */
9115                   if (mac_ptr >= mac_end)
9116                     {
9117                       dwarf2_macros_too_long_complaint ();
9118                       return;
9119                     }
9120
9121                   /* We don't increment mac_ptr here, so this is just
9122                      a look-ahead.  */
9123                   next_type = read_1_byte (abfd, mac_ptr);
9124                   if (next_type != 0)
9125                     complaint (&symfile_complaints,
9126                                "no terminating 0-type entry for macros in `.debug_macinfo' section");
9127
9128                   return;
9129                 }
9130             }
9131           break;
9132
9133         case DW_MACINFO_vendor_ext:
9134           {
9135             int bytes_read;
9136             int constant;
9137             char *string;
9138
9139             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9140             mac_ptr += bytes_read;
9141             string = read_string (abfd, mac_ptr, &bytes_read);
9142             mac_ptr += bytes_read;
9143
9144             /* We don't recognize any vendor extensions.  */
9145           }
9146           break;
9147         }
9148     }
9149 }
9150
9151 /* Check if the attribute's form is a DW_FORM_block*
9152    if so return true else false. */
9153 static int
9154 attr_form_is_block (struct attribute *attr)
9155 {
9156   return (attr == NULL ? 0 :
9157       attr->form == DW_FORM_block1
9158       || attr->form == DW_FORM_block2
9159       || attr->form == DW_FORM_block4
9160       || attr->form == DW_FORM_block);
9161 }
9162
9163 static void
9164 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
9165                              struct dwarf2_cu *cu)
9166 {
9167   if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
9168     {
9169       struct dwarf2_loclist_baton *baton;
9170
9171       baton = obstack_alloc (&cu->objfile->objfile_obstack,
9172                              sizeof (struct dwarf2_loclist_baton));
9173       baton->objfile = cu->objfile;
9174
9175       /* We don't know how long the location list is, but make sure we
9176          don't run off the edge of the section.  */
9177       baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
9178       baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
9179       baton->base_address = cu->header.base_address;
9180       if (cu->header.base_known == 0)
9181         complaint (&symfile_complaints,
9182                    "Location list used without specifying the CU base address.");
9183
9184       SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
9185       SYMBOL_LOCATION_BATON (sym) = baton;
9186     }
9187   else
9188     {
9189       struct dwarf2_locexpr_baton *baton;
9190
9191       baton = obstack_alloc (&cu->objfile->objfile_obstack,
9192                              sizeof (struct dwarf2_locexpr_baton));
9193       baton->objfile = cu->objfile;
9194
9195       if (attr_form_is_block (attr))
9196         {
9197           /* Note that we're just copying the block's data pointer
9198              here, not the actual data.  We're still pointing into the
9199              info_buffer for SYM's objfile; right now we never release
9200              that buffer, but when we do clean up properly this may
9201              need to change.  */
9202           baton->size = DW_BLOCK (attr)->size;
9203           baton->data = DW_BLOCK (attr)->data;
9204         }
9205       else
9206         {
9207           dwarf2_invalid_attrib_class_complaint ("location description",
9208                                                  SYMBOL_NATURAL_NAME (sym));
9209           baton->size = 0;
9210           baton->data = NULL;
9211         }
9212       
9213       SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
9214       SYMBOL_LOCATION_BATON (sym) = baton;
9215     }
9216 }
9217
9218 /* Locate the compilation unit from CU's objfile which contains the
9219    DIE at OFFSET.  Raises an error on failure.  */
9220
9221 static struct dwarf2_per_cu_data *
9222 dwarf2_find_containing_comp_unit (unsigned long offset,
9223                                   struct objfile *objfile)
9224 {
9225   struct dwarf2_per_cu_data *this_cu;
9226   int low, high;
9227
9228   low = 0;
9229   high = dwarf2_per_objfile->n_comp_units - 1;
9230   while (high > low)
9231     {
9232       int mid = low + (high - low) / 2;
9233       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
9234         high = mid;
9235       else
9236         low = mid + 1;
9237     }
9238   gdb_assert (low == high);
9239   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
9240     {
9241       if (low == 0)
9242         error ("Dwarf Error: could not find partial DIE containing "
9243                "offset 0x%lx [in module %s]",
9244                (long) offset, bfd_get_filename (objfile->obfd));
9245
9246       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
9247       return dwarf2_per_objfile->all_comp_units[low-1];
9248     }
9249   else
9250     {
9251       this_cu = dwarf2_per_objfile->all_comp_units[low];
9252       if (low == dwarf2_per_objfile->n_comp_units - 1
9253           && offset >= this_cu->offset + this_cu->length)
9254         error ("invalid dwarf2 offset %ld", offset);
9255       gdb_assert (offset < this_cu->offset + this_cu->length);
9256       return this_cu;
9257     }
9258 }
9259
9260 /* Locate the compilation unit from OBJFILE which is located at exactly
9261    OFFSET.  Raises an error on failure.  */
9262
9263 static struct dwarf2_per_cu_data *
9264 dwarf2_find_comp_unit (unsigned long offset, struct objfile *objfile)
9265 {
9266   struct dwarf2_per_cu_data *this_cu;
9267   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9268   if (this_cu->offset != offset)
9269     error ("no compilation unit with offset %ld\n", offset);
9270   return this_cu;
9271 }
9272
9273 /* Release one cached compilation unit, CU.  We unlink it from the tree
9274    of compilation units, but we don't remove it from the read_in_chain;
9275    the caller is responsible for that.  */
9276
9277 static void
9278 free_one_comp_unit (void *data)
9279 {
9280   struct dwarf2_cu *cu = data;
9281
9282   if (cu->per_cu != NULL)
9283     cu->per_cu->cu = NULL;
9284   cu->per_cu = NULL;
9285
9286   obstack_free (&cu->comp_unit_obstack, NULL);
9287   if (cu->dies)
9288     free_die_list (cu->dies);
9289
9290   xfree (cu);
9291 }
9292
9293 /* This cleanup function is passed the address of a dwarf2_cu on the stack
9294    when we're finished with it.  We can't free the pointer itself, but be
9295    sure to unlink it from the cache.  Also release any associated storage
9296    and perform cache maintenance.
9297
9298    Only used during partial symbol parsing.  */
9299
9300 static void
9301 free_stack_comp_unit (void *data)
9302 {
9303   struct dwarf2_cu *cu = data;
9304
9305   obstack_free (&cu->comp_unit_obstack, NULL);
9306   cu->partial_dies = NULL;
9307
9308   if (cu->per_cu != NULL)
9309     {
9310       /* This compilation unit is on the stack in our caller, so we
9311          should not xfree it.  Just unlink it.  */
9312       cu->per_cu->cu = NULL;
9313       cu->per_cu = NULL;
9314
9315       /* If we had a per-cu pointer, then we may have other compilation
9316          units loaded, so age them now.  */
9317       age_cached_comp_units ();
9318     }
9319 }
9320
9321 /* Free all cached compilation units.  */
9322
9323 static void
9324 free_cached_comp_units (void *data)
9325 {
9326   struct dwarf2_per_cu_data *per_cu, **last_chain;
9327
9328   per_cu = dwarf2_per_objfile->read_in_chain;
9329   last_chain = &dwarf2_per_objfile->read_in_chain;
9330   while (per_cu != NULL)
9331     {
9332       struct dwarf2_per_cu_data *next_cu;
9333
9334       next_cu = per_cu->cu->read_in_chain;
9335
9336       free_one_comp_unit (per_cu->cu);
9337       *last_chain = next_cu;
9338
9339       per_cu = next_cu;
9340     }
9341 }
9342
9343 /* Increase the age counter on each cached compilation unit, and free
9344    any that are too old.  */
9345
9346 static void
9347 age_cached_comp_units (void)
9348 {
9349   struct dwarf2_per_cu_data *per_cu, **last_chain;
9350
9351   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
9352   per_cu = dwarf2_per_objfile->read_in_chain;
9353   while (per_cu != NULL)
9354     {
9355       per_cu->cu->last_used ++;
9356       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
9357         dwarf2_mark (per_cu->cu);
9358       per_cu = per_cu->cu->read_in_chain;
9359     }
9360
9361   per_cu = dwarf2_per_objfile->read_in_chain;
9362   last_chain = &dwarf2_per_objfile->read_in_chain;
9363   while (per_cu != NULL)
9364     {
9365       struct dwarf2_per_cu_data *next_cu;
9366
9367       next_cu = per_cu->cu->read_in_chain;
9368
9369       if (!per_cu->cu->mark)
9370         {
9371           free_one_comp_unit (per_cu->cu);
9372           *last_chain = next_cu;
9373         }
9374       else
9375         last_chain = &per_cu->cu->read_in_chain;
9376
9377       per_cu = next_cu;
9378     }
9379 }
9380
9381 /* Remove a single compilation unit from the cache.  */
9382
9383 static void
9384 free_one_cached_comp_unit (void *target_cu)
9385 {
9386   struct dwarf2_per_cu_data *per_cu, **last_chain;
9387
9388   per_cu = dwarf2_per_objfile->read_in_chain;
9389   last_chain = &dwarf2_per_objfile->read_in_chain;
9390   while (per_cu != NULL)
9391     {
9392       struct dwarf2_per_cu_data *next_cu;
9393
9394       next_cu = per_cu->cu->read_in_chain;
9395
9396       if (per_cu->cu == target_cu)
9397         {
9398           free_one_comp_unit (per_cu->cu);
9399           *last_chain = next_cu;
9400           break;
9401         }
9402       else
9403         last_chain = &per_cu->cu->read_in_chain;
9404
9405       per_cu = next_cu;
9406     }
9407 }
9408
9409 /* A pair of DIE offset and GDB type pointer.  We store these
9410    in a hash table separate from the DIEs, and preserve them
9411    when the DIEs are flushed out of cache.  */
9412
9413 struct dwarf2_offset_and_type
9414 {
9415   unsigned int offset;
9416   struct type *type;
9417 };
9418
9419 /* Hash function for a dwarf2_offset_and_type.  */
9420
9421 static hashval_t
9422 offset_and_type_hash (const void *item)
9423 {
9424   const struct dwarf2_offset_and_type *ofs = item;
9425   return ofs->offset;
9426 }
9427
9428 /* Equality function for a dwarf2_offset_and_type.  */
9429
9430 static int
9431 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
9432 {
9433   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
9434   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9435   return ofs_lhs->offset == ofs_rhs->offset;
9436 }
9437
9438 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
9439    table if necessary.  */
9440
9441 static void
9442 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
9443 {
9444   struct dwarf2_offset_and_type **slot, ofs;
9445
9446   die->type = type;
9447
9448   if (cu->per_cu == NULL)
9449     return;
9450
9451   if (cu->per_cu->type_hash == NULL)
9452     cu->per_cu->type_hash
9453       = htab_create_alloc_ex (cu->header.length / 24,
9454                               offset_and_type_hash,
9455                               offset_and_type_eq,
9456                               NULL,
9457                               &cu->objfile->objfile_obstack,
9458                               hashtab_obstack_allocate,
9459                               dummy_obstack_deallocate);
9460
9461   ofs.offset = die->offset;
9462   ofs.type = type;
9463   slot = (struct dwarf2_offset_and_type **)
9464     htab_find_slot_with_hash (cu->per_cu->type_hash, &ofs, ofs.offset, INSERT);
9465   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
9466   **slot = ofs;
9467 }
9468
9469 /* Find the type for DIE in TYPE_HASH, or return NULL if DIE does not
9470    have a saved type.  */
9471
9472 static struct type *
9473 get_die_type (struct die_info *die, htab_t type_hash)
9474 {
9475   struct dwarf2_offset_and_type *slot, ofs;
9476
9477   ofs.offset = die->offset;
9478   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
9479   if (slot)
9480     return slot->type;
9481   else
9482     return NULL;
9483 }
9484
9485 /* Restore the types of the DIE tree starting at START_DIE from the hash
9486    table saved in CU.  */
9487
9488 static void
9489 reset_die_and_siblings_types (struct die_info *start_die, struct dwarf2_cu *cu)
9490 {
9491   struct die_info *die;
9492
9493   if (cu->per_cu->type_hash == NULL)
9494     return;
9495
9496   for (die = start_die; die != NULL; die = die->sibling)
9497     {
9498       die->type = get_die_type (die, cu->per_cu->type_hash);
9499       if (die->child != NULL)
9500         reset_die_and_siblings_types (die->child, cu);
9501     }
9502 }
9503
9504 /* Set the mark field in CU and in every other compilation unit in the
9505    cache that we must keep because we are keeping CU.  */
9506
9507 /* Add a dependence relationship from CU to REF_PER_CU.  */
9508
9509 static void
9510 dwarf2_add_dependence (struct dwarf2_cu *cu,
9511                        struct dwarf2_per_cu_data *ref_per_cu)
9512 {
9513   void **slot;
9514
9515   if (cu->dependencies == NULL)
9516     cu->dependencies
9517       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
9518                               NULL, &cu->comp_unit_obstack,
9519                               hashtab_obstack_allocate,
9520                               dummy_obstack_deallocate);
9521
9522   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
9523   if (*slot == NULL)
9524     *slot = ref_per_cu;
9525 }
9526
9527 /* Set the mark field in CU and in every other compilation unit in the
9528    cache that we must keep because we are keeping CU.  */
9529
9530 static int
9531 dwarf2_mark_helper (void **slot, void *data)
9532 {
9533   struct dwarf2_per_cu_data *per_cu;
9534
9535   per_cu = (struct dwarf2_per_cu_data *) *slot;
9536   if (per_cu->cu->mark)
9537     return 1;
9538   per_cu->cu->mark = 1;
9539
9540   if (per_cu->cu->dependencies != NULL)
9541     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
9542
9543   return 1;
9544 }
9545
9546 static void
9547 dwarf2_mark (struct dwarf2_cu *cu)
9548 {
9549   if (cu->mark)
9550     return;
9551   cu->mark = 1;
9552   if (cu->dependencies != NULL)
9553     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
9554 }
9555
9556 static void
9557 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
9558 {
9559   while (per_cu)
9560     {
9561       per_cu->cu->mark = 0;
9562       per_cu = per_cu->cu->read_in_chain;
9563     }
9564 }
9565
9566 /* Allocation function for the libiberty hash table which uses an
9567    obstack.  */
9568
9569 static void *
9570 hashtab_obstack_allocate (void *data, size_t size, size_t count)
9571 {
9572   unsigned int total = size * count;
9573   void *ptr = obstack_alloc ((struct obstack *) data, total);
9574   memset (ptr, 0, total);
9575   return ptr;
9576 }
9577
9578 /* Trivial deallocation function for the libiberty splay tree and hash
9579    table - don't deallocate anything.  Rely on later deletion of the
9580    obstack.  */
9581
9582 static void
9583 dummy_obstack_deallocate (void *object, void *data)
9584 {
9585   return;
9586 }
9587
9588 /* Trivial hash function for partial_die_info: the hash value of a DIE
9589    is its offset in .debug_info for this objfile.  */
9590
9591 static hashval_t
9592 partial_die_hash (const void *item)
9593 {
9594   const struct partial_die_info *part_die = item;
9595   return part_die->offset;
9596 }
9597
9598 /* Trivial comparison function for partial_die_info structures: two DIEs
9599    are equal if they have the same offset.  */
9600
9601 static int
9602 partial_die_eq (const void *item_lhs, const void *item_rhs)
9603 {
9604   const struct partial_die_info *part_die_lhs = item_lhs;
9605   const struct partial_die_info *part_die_rhs = item_rhs;
9606   return part_die_lhs->offset == part_die_rhs->offset;
9607 }
9608
9609 static struct cmd_list_element *set_dwarf2_cmdlist;
9610 static struct cmd_list_element *show_dwarf2_cmdlist;
9611
9612 static void
9613 set_dwarf2_cmd (char *args, int from_tty)
9614 {
9615   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
9616 }
9617
9618 static void
9619 show_dwarf2_cmd (char *args, int from_tty)
9620
9621   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
9622 }
9623
9624 void _initialize_dwarf2_read (void);
9625
9626 void
9627 _initialize_dwarf2_read (void)
9628 {
9629   dwarf2_objfile_data_key = register_objfile_data ();
9630
9631   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd,
9632                   "Set DWARF 2 specific variables.\n"
9633                   "Configure DWARF 2 variables such as the cache size",
9634                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
9635                   0/*allow-unknown*/, &maintenance_set_cmdlist);
9636
9637   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd,
9638                   "Show DWARF 2 specific variables\n"
9639                   "Show DWARF 2 variables such as the cache size",
9640                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
9641                   0/*allow-unknown*/, &maintenance_show_cmdlist);
9642
9643   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
9644                             &dwarf2_max_cache_age,
9645                             "Set the upper bound on the age of cached "
9646                             "dwarf2 compilation units.",
9647                             "Show the upper bound on the age of cached "
9648                             "dwarf2 compilation units.",
9649                             "A higher limit means that cached "
9650                             "compilation units will be stored\n"
9651                             "in memory longer, and more total memory will "
9652                             "be used.  Zero disables\n"
9653                             "caching, which can slow down startup.",
9654                             "The upper bound on the age of cached "
9655                             "dwarf2 compilation units is %d.",
9656                             NULL, NULL, &set_dwarf2_cmdlist,
9657                             &show_dwarf2_cmdlist);
9658 }