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