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