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