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