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