OSDN Git Service

DWARF: Add support for DW_TAG_constant DIEs
[pf3gnuchains/sourceware.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 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80    Because of alignment constraints, this structure has padding and cannot
81    be mapped directly onto the beginning of the .debug_info section.  */
82 typedef struct comp_unit_header
83   {
84     unsigned int length;        /* length of the .debug_info
85                                    contribution */
86     unsigned short version;     /* version number -- 2 for DWARF
87                                    version 2 */
88     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89     unsigned char addr_size;    /* byte size of an address -- 4 */
90   }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96    Because of alignment constraints, this structure has padding and cannot
97    be mapped directly onto the beginning of the .debug_info section.  */
98 typedef struct statement_prologue
99   {
100     unsigned int total_length;  /* byte length of the statement
101                                    information */
102     unsigned short version;     /* version number -- 2 for DWARF
103                                    version 2 */
104     unsigned int prologue_length;       /* # bytes between prologue &
105                                            stmt program */
106     unsigned char minimum_instruction_length;   /* byte size of
107                                                    smallest instr */
108     unsigned char default_is_stmt;      /* initial value of is_stmt
109                                            register */
110     char line_base;
111     unsigned char line_range;
112     unsigned char opcode_base;  /* number assigned to first special
113                                    opcode */
114     unsigned char *standard_opcode_lengths;
115   }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in.  */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124    info for C++ namespaces.  GCC 3.3.x did not produce this information,
125    but later versions do.  */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133   asection *asection;
134   gdb_byte *buffer;
135   bfd_size_type size;
136   int was_mmapped;
137   /* True if we have tried to read this section.  */
138   int readin;
139 };
140
141 /* All offsets in the index are of this type.  It must be
142    architecture-independent.  */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index.  The file format is described in
148    a comment by the code that writes the index.  */
149 struct mapped_index
150 {
151   /* The total length of the buffer.  */
152   off_t total_size;
153   /* A pointer to the address table data.  */
154   const gdb_byte *address_table;
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157   /* The hash table.  */
158   const offset_type *index_table;
159   /* Size in slots, each slot is 2 offset_types.  */
160   offset_type index_table_slots;
161   /* A pointer to the constant pool.  */
162   const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167   struct dwarf2_section_info info;
168   struct dwarf2_section_info abbrev;
169   struct dwarf2_section_info line;
170   struct dwarf2_section_info loc;
171   struct dwarf2_section_info macinfo;
172   struct dwarf2_section_info str;
173   struct dwarf2_section_info ranges;
174   struct dwarf2_section_info types;
175   struct dwarf2_section_info frame;
176   struct dwarf2_section_info eh_frame;
177   struct dwarf2_section_info gdb_index;
178
179   /* Back link.  */
180   struct objfile *objfile;
181
182   /* A list of all the compilation units.  This is used to locate
183      the target compilation unit of a particular reference.  */
184   struct dwarf2_per_cu_data **all_comp_units;
185
186   /* The number of compilation units in ALL_COMP_UNITS.  */
187   int n_comp_units;
188
189   /* The number of .debug_types-related CUs.  */
190   int n_type_comp_units;
191
192   /* The .debug_types-related CUs.  */
193   struct dwarf2_per_cu_data **type_comp_units;
194
195   /* A chain of compilation units that are currently read in, so that
196      they can be freed later.  */
197   struct dwarf2_per_cu_data *read_in_chain;
198
199   /* A table mapping .debug_types signatures to its signatured_type entry.
200      This is NULL if the .debug_types section hasn't been read in yet.  */
201   htab_t signatured_types;
202
203   /* A flag indicating wether this objfile has a section loaded at a
204      VMA of 0.  */
205   int has_section_at_zero;
206
207   /* True if we are using the mapped index.  */
208   unsigned char using_index;
209
210   /* The mapped index.  */
211   struct mapped_index *index_table;
212
213   /* Set during partial symbol reading, to prevent queueing of full
214      symbols.  */
215   int reading_partial_symbols;
216
217   /* Table mapping type .debug_info DIE offsets to types.
218      This is NULL if not allocated yet.
219      It (currently) makes sense to allocate debug_types_type_hash lazily.
220      To keep things simple we allocate both lazily.  */
221   htab_t debug_info_type_hash;
222
223   /* Table mapping type .debug_types DIE offsets to types.
224      This is NULL if not allocated yet.  */
225   htab_t debug_types_type_hash;
226 };
227
228 static struct dwarf2_per_objfile *dwarf2_per_objfile;
229
230 /* names of the debugging sections */
231
232 /* Note that if the debugging section has been compressed, it might
233    have a name like .zdebug_info.  */
234
235 #define INFO_SECTION     "debug_info"
236 #define ABBREV_SECTION   "debug_abbrev"
237 #define LINE_SECTION     "debug_line"
238 #define LOC_SECTION      "debug_loc"
239 #define MACINFO_SECTION  "debug_macinfo"
240 #define STR_SECTION      "debug_str"
241 #define RANGES_SECTION   "debug_ranges"
242 #define TYPES_SECTION    "debug_types"
243 #define FRAME_SECTION    "debug_frame"
244 #define EH_FRAME_SECTION "eh_frame"
245 #define GDB_INDEX_SECTION "gdb_index"
246
247 /* local data types */
248
249 /* We hold several abbreviation tables in memory at the same time. */
250 #ifndef ABBREV_HASH_SIZE
251 #define ABBREV_HASH_SIZE 121
252 #endif
253
254 /* The data in a compilation unit header, after target2host
255    translation, looks like this.  */
256 struct comp_unit_head
257 {
258   unsigned int length;
259   short version;
260   unsigned char addr_size;
261   unsigned char signed_addr_p;
262   unsigned int abbrev_offset;
263
264   /* Size of file offsets; either 4 or 8.  */
265   unsigned int offset_size;
266
267   /* Size of the length field; either 4 or 12.  */
268   unsigned int initial_length_size;
269
270   /* Offset to the first byte of this compilation unit header in the
271      .debug_info section, for resolving relative reference dies.  */
272   unsigned int offset;
273
274   /* Offset to first die in this cu from the start of the cu.
275      This will be the first byte following the compilation unit header.  */
276   unsigned int first_die_offset;
277 };
278
279 /* Type used for delaying computation of method physnames.
280    See comments for compute_delayed_physnames.  */
281 struct delayed_method_info
282 {
283   /* The type to which the method is attached, i.e., its parent class.  */
284   struct type *type;
285
286   /* The index of the method in the type's function fieldlists.  */
287   int fnfield_index;
288
289   /* The index of the method in the fieldlist.  */
290   int index;
291
292   /* The name of the DIE.  */
293   const char *name;
294
295   /*  The DIE associated with this method.  */
296   struct die_info *die;
297 };
298
299 typedef struct delayed_method_info delayed_method_info;
300 DEF_VEC_O (delayed_method_info);
301
302 /* Internal state when decoding a particular compilation unit.  */
303 struct dwarf2_cu
304 {
305   /* The objfile containing this compilation unit.  */
306   struct objfile *objfile;
307
308   /* The header of the compilation unit.  */
309   struct comp_unit_head header;
310
311   /* Base address of this compilation unit.  */
312   CORE_ADDR base_address;
313
314   /* Non-zero if base_address has been set.  */
315   int base_known;
316
317   struct function_range *first_fn, *last_fn, *cached_fn;
318
319   /* The language we are debugging.  */
320   enum language language;
321   const struct language_defn *language_defn;
322
323   const char *producer;
324
325   /* The generic symbol table building routines have separate lists for
326      file scope symbols and all all other scopes (local scopes).  So
327      we need to select the right one to pass to add_symbol_to_list().
328      We do it by keeping a pointer to the correct list in list_in_scope.
329
330      FIXME: The original dwarf code just treated the file scope as the
331      first local scope, and all other local scopes as nested local
332      scopes, and worked fine.  Check to see if we really need to
333      distinguish these in buildsym.c.  */
334   struct pending **list_in_scope;
335
336   /* DWARF abbreviation table associated with this compilation unit.  */
337   struct abbrev_info **dwarf2_abbrevs;
338
339   /* Storage for the abbrev table.  */
340   struct obstack abbrev_obstack;
341
342   /* Hash table holding all the loaded partial DIEs.  */
343   htab_t partial_dies;
344
345   /* Storage for things with the same lifetime as this read-in compilation
346      unit, including partial DIEs.  */
347   struct obstack comp_unit_obstack;
348
349   /* When multiple dwarf2_cu structures are living in memory, this field
350      chains them all together, so that they can be released efficiently.
351      We will probably also want a generation counter so that most-recently-used
352      compilation units are cached...  */
353   struct dwarf2_per_cu_data *read_in_chain;
354
355   /* Backchain to our per_cu entry if the tree has been built.  */
356   struct dwarf2_per_cu_data *per_cu;
357
358   /* How many compilation units ago was this CU last referenced?  */
359   int last_used;
360
361   /* A hash table of die offsets for following references.  */
362   htab_t die_hash;
363
364   /* Full DIEs if read in.  */
365   struct die_info *dies;
366
367   /* A set of pointers to dwarf2_per_cu_data objects for compilation
368      units referenced by this one.  Only set during full symbol processing;
369      partial symbol tables do not have dependencies.  */
370   htab_t dependencies;
371
372   /* Header data from the line table, during full symbol processing.  */
373   struct line_header *line_header;
374
375   /* A list of methods which need to have physnames computed
376      after all type information has been read.  */
377   VEC (delayed_method_info) *method_list;
378
379   /* Mark used when releasing cached dies.  */
380   unsigned int mark : 1;
381
382   /* This flag will be set if this compilation unit might include
383      inter-compilation-unit references.  */
384   unsigned int has_form_ref_addr : 1;
385
386   /* This flag will be set if this compilation unit includes any
387      DW_TAG_namespace DIEs.  If we know that there are explicit
388      DIEs for namespaces, we don't need to try to infer them
389      from mangled names.  */
390   unsigned int has_namespace_info : 1;
391 };
392
393 /* When using the index (and thus not using psymtabs), each CU has an
394    object of this type.  This is used to hold information needed by
395    the various "quick" methods.  */
396 struct dwarf2_per_cu_quick_data
397 {
398   /* The line table.  This can be NULL if there was no line table.  */
399   struct line_header *lines;
400
401   /* The file names from the line table.  */
402   const char **file_names;
403   /* The file names from the line table after being run through
404      gdb_realpath.  */
405   const char **full_names;
406
407   /* The corresponding symbol table.  This is NULL if symbols for this
408      CU have not yet been read.  */
409   struct symtab *symtab;
410
411   /* A temporary mark bit used when iterating over all CUs in
412      expand_symtabs_matching.  */
413   unsigned int mark : 1;
414
415   /* True if we've tried to read the line table.  */
416   unsigned int read_lines : 1;
417 };
418
419 /* Persistent data held for a compilation unit, even when not
420    processing it.  We put a pointer to this structure in the
421    read_symtab_private field of the psymtab.  If we encounter
422    inter-compilation-unit references, we also maintain a sorted
423    list of all compilation units.  */
424
425 struct dwarf2_per_cu_data
426 {
427   /* The start offset and length of this compilation unit.  2**29-1
428      bytes should suffice to store the length of any compilation unit
429      - if it doesn't, GDB will fall over anyway.
430      NOTE: Unlike comp_unit_head.length, this length includes
431      initial_length_size.  */
432   unsigned int offset;
433   unsigned int length : 29;
434
435   /* Flag indicating this compilation unit will be read in before
436      any of the current compilation units are processed.  */
437   unsigned int queued : 1;
438
439   /* This flag will be set if we need to load absolutely all DIEs
440      for this compilation unit, instead of just the ones we think
441      are interesting.  It gets set if we look for a DIE in the
442      hash table and don't find it.  */
443   unsigned int load_all_dies : 1;
444
445   /* Non-zero if this CU is from .debug_types.
446      Otherwise it's from .debug_info.  */
447   unsigned int from_debug_types : 1;
448
449   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
450      of the CU cache it gets reset to NULL again.  */
451   struct dwarf2_cu *cu;
452
453   /* The corresponding objfile.  */
454   struct objfile *objfile;
455
456   /* When using partial symbol tables, the 'psymtab' field is active.
457      Otherwise the 'quick' field is active.  */
458   union
459   {
460     /* The partial symbol table associated with this compilation unit,
461        or NULL for partial units (which do not have an associated
462        symtab).  */
463     struct partial_symtab *psymtab;
464
465     /* Data needed by the "quick" functions.  */
466     struct dwarf2_per_cu_quick_data *quick;
467   } v;
468 };
469
470 /* Entry in the signatured_types hash table.  */
471
472 struct signatured_type
473 {
474   ULONGEST signature;
475
476   /* Offset in .debug_types of the TU (type_unit) for this type.  */
477   unsigned int offset;
478
479   /* Offset in .debug_types of the type defined by this TU.  */
480   unsigned int type_offset;
481
482   /* The CU(/TU) of this type.  */
483   struct dwarf2_per_cu_data per_cu;
484 };
485
486 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
487    which are used for both .debug_info and .debug_types dies.
488    All parameters here are unchanging for the life of the call.
489    This struct exists to abstract away the constant parameters of
490    die reading.  */
491
492 struct die_reader_specs
493 {
494   /* The bfd of this objfile.  */
495   bfd* abfd;
496
497   /* The CU of the DIE we are parsing.  */
498   struct dwarf2_cu *cu;
499
500   /* Pointer to start of section buffer.
501      This is either the start of .debug_info or .debug_types.  */
502   const gdb_byte *buffer;
503 };
504
505 /* The line number information for a compilation unit (found in the
506    .debug_line section) begins with a "statement program header",
507    which contains the following information.  */
508 struct line_header
509 {
510   unsigned int total_length;
511   unsigned short version;
512   unsigned int header_length;
513   unsigned char minimum_instruction_length;
514   unsigned char maximum_ops_per_instruction;
515   unsigned char default_is_stmt;
516   int line_base;
517   unsigned char line_range;
518   unsigned char opcode_base;
519
520   /* standard_opcode_lengths[i] is the number of operands for the
521      standard opcode whose value is i.  This means that
522      standard_opcode_lengths[0] is unused, and the last meaningful
523      element is standard_opcode_lengths[opcode_base - 1].  */
524   unsigned char *standard_opcode_lengths;
525
526   /* The include_directories table.  NOTE!  These strings are not
527      allocated with xmalloc; instead, they are pointers into
528      debug_line_buffer.  If you try to free them, `free' will get
529      indigestion.  */
530   unsigned int num_include_dirs, include_dirs_size;
531   char **include_dirs;
532
533   /* The file_names table.  NOTE!  These strings are not allocated
534      with xmalloc; instead, they are pointers into debug_line_buffer.
535      Don't try to free them directly.  */
536   unsigned int num_file_names, file_names_size;
537   struct file_entry
538   {
539     char *name;
540     unsigned int dir_index;
541     unsigned int mod_time;
542     unsigned int length;
543     int included_p; /* Non-zero if referenced by the Line Number Program.  */
544     struct symtab *symtab; /* The associated symbol table, if any.  */
545   } *file_names;
546
547   /* The start and end of the statement program following this
548      header.  These point into dwarf2_per_objfile->line_buffer.  */
549   gdb_byte *statement_program_start, *statement_program_end;
550 };
551
552 /* When we construct a partial symbol table entry we only
553    need this much information. */
554 struct partial_die_info
555   {
556     /* Offset of this DIE.  */
557     unsigned int offset;
558
559     /* DWARF-2 tag for this DIE.  */
560     ENUM_BITFIELD(dwarf_tag) tag : 16;
561
562     /* Assorted flags describing the data found in this DIE.  */
563     unsigned int has_children : 1;
564     unsigned int is_external : 1;
565     unsigned int is_declaration : 1;
566     unsigned int has_type : 1;
567     unsigned int has_specification : 1;
568     unsigned int has_pc_info : 1;
569
570     /* Flag set if the SCOPE field of this structure has been
571        computed.  */
572     unsigned int scope_set : 1;
573
574     /* Flag set if the DIE has a byte_size attribute.  */
575     unsigned int has_byte_size : 1;
576
577     /* Flag set if any of the DIE's children are template arguments.  */
578     unsigned int has_template_arguments : 1;
579
580     /* The name of this DIE.  Normally the value of DW_AT_name, but
581        sometimes a default name for unnamed DIEs.  */
582     char *name;
583
584     /* The scope to prepend to our children.  This is generally
585        allocated on the comp_unit_obstack, so will disappear
586        when this compilation unit leaves the cache.  */
587     char *scope;
588
589     /* The location description associated with this DIE, if any.  */
590     struct dwarf_block *locdesc;
591
592     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
593     CORE_ADDR lowpc;
594     CORE_ADDR highpc;
595
596     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
597        DW_AT_sibling, if any.  */
598     gdb_byte *sibling;
599
600     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601        DW_AT_specification (or DW_AT_abstract_origin or
602        DW_AT_extension).  */
603     unsigned int spec_offset;
604
605     /* Pointers to this DIE's parent, first child, and next sibling,
606        if any.  */
607     struct partial_die_info *die_parent, *die_child, *die_sibling;
608   };
609
610 /* This data structure holds the information of an abbrev. */
611 struct abbrev_info
612   {
613     unsigned int number;        /* number identifying abbrev */
614     enum dwarf_tag tag;         /* dwarf tag */
615     unsigned short has_children;                /* boolean */
616     unsigned short num_attrs;   /* number of attributes */
617     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
618     struct abbrev_info *next;   /* next in chain */
619   };
620
621 struct attr_abbrev
622   {
623     ENUM_BITFIELD(dwarf_attribute) name : 16;
624     ENUM_BITFIELD(dwarf_form) form : 16;
625   };
626
627 /* Attributes have a name and a value */
628 struct attribute
629   {
630     ENUM_BITFIELD(dwarf_attribute) name : 16;
631     ENUM_BITFIELD(dwarf_form) form : 15;
632
633     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
634        field should be in u.str (existing only for DW_STRING) but it is kept
635        here for better struct attribute alignment.  */
636     unsigned int string_is_canonical : 1;
637
638     union
639       {
640         char *str;
641         struct dwarf_block *blk;
642         ULONGEST unsnd;
643         LONGEST snd;
644         CORE_ADDR addr;
645         struct signatured_type *signatured_type;
646       }
647     u;
648   };
649
650 /* This data structure holds a complete die structure. */
651 struct die_info
652   {
653     /* DWARF-2 tag for this DIE.  */
654     ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656     /* Number of attributes */
657     unsigned char num_attrs;
658
659     /* True if we're presently building the full type name for the
660        type derived from this DIE.  */
661     unsigned char building_fullname : 1;
662
663     /* Abbrev number */
664     unsigned int abbrev;
665
666     /* Offset in .debug_info or .debug_types section.  */
667     unsigned int offset;
668
669     /* The dies in a compilation unit form an n-ary tree.  PARENT
670        points to this die's parent; CHILD points to the first child of
671        this node; and all the children of a given node are chained
672        together via their SIBLING fields.  */
673     struct die_info *child;     /* Its first child, if any.  */
674     struct die_info *sibling;   /* Its next sibling, if any.  */
675     struct die_info *parent;    /* Its parent, if any.  */
676
677     /* An array of attributes, with NUM_ATTRS elements.  There may be
678        zero, but it's not common and zero-sized arrays are not
679        sufficiently portable C.  */
680     struct attribute attrs[1];
681   };
682
683 struct function_range
684 {
685   const char *name;
686   CORE_ADDR lowpc, highpc;
687   int seen_line;
688   struct function_range *next;
689 };
690
691 /* Get at parts of an attribute structure */
692
693 #define DW_STRING(attr)    ((attr)->u.str)
694 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
695 #define DW_UNSND(attr)     ((attr)->u.unsnd)
696 #define DW_BLOCK(attr)     ((attr)->u.blk)
697 #define DW_SND(attr)       ((attr)->u.snd)
698 #define DW_ADDR(attr)      ((attr)->u.addr)
699 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
700
701 /* Blocks are a bunch of untyped bytes. */
702 struct dwarf_block
703   {
704     unsigned int size;
705     gdb_byte *data;
706   };
707
708 #ifndef ATTR_ALLOC_CHUNK
709 #define ATTR_ALLOC_CHUNK 4
710 #endif
711
712 /* Allocate fields for structs, unions and enums in this size.  */
713 #ifndef DW_FIELD_ALLOC_CHUNK
714 #define DW_FIELD_ALLOC_CHUNK 4
715 #endif
716
717 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
718    but this would require a corresponding change in unpack_field_as_long
719    and friends.  */
720 static int bits_per_byte = 8;
721
722 /* The routines that read and process dies for a C struct or C++ class
723    pass lists of data member fields and lists of member function fields
724    in an instance of a field_info structure, as defined below.  */
725 struct field_info
726   {
727     /* List of data member and baseclasses fields. */
728     struct nextfield
729       {
730         struct nextfield *next;
731         int accessibility;
732         int virtuality;
733         struct field field;
734       }
735      *fields, *baseclasses;
736
737     /* Number of fields (including baseclasses).  */
738     int nfields;
739
740     /* Number of baseclasses.  */
741     int nbaseclasses;
742
743     /* Set if the accesibility of one of the fields is not public.  */
744     int non_public_fields;
745
746     /* Member function fields array, entries are allocated in the order they
747        are encountered in the object file.  */
748     struct nextfnfield
749       {
750         struct nextfnfield *next;
751         struct fn_field fnfield;
752       }
753      *fnfields;
754
755     /* Member function fieldlist array, contains name of possibly overloaded
756        member function, number of overloaded member functions and a pointer
757        to the head of the member function field chain.  */
758     struct fnfieldlist
759       {
760         char *name;
761         int length;
762         struct nextfnfield *head;
763       }
764      *fnfieldlists;
765
766     /* Number of entries in the fnfieldlists array.  */
767     int nfnfields;
768
769     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
770        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
771     struct typedef_field_list
772       {
773         struct typedef_field field;
774         struct typedef_field_list *next;
775       }
776     *typedef_field_list;
777     unsigned typedef_field_list_count;
778   };
779
780 /* One item on the queue of compilation units to read in full symbols
781    for.  */
782 struct dwarf2_queue_item
783 {
784   struct dwarf2_per_cu_data *per_cu;
785   struct dwarf2_queue_item *next;
786 };
787
788 /* The current queue.  */
789 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
790
791 /* Loaded secondary compilation units are kept in memory until they
792    have not been referenced for the processing of this many
793    compilation units.  Set this to zero to disable caching.  Cache
794    sizes of up to at least twenty will improve startup time for
795    typical inter-CU-reference binaries, at an obvious memory cost.  */
796 static int dwarf2_max_cache_age = 5;
797 static void
798 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
799                            struct cmd_list_element *c, const char *value)
800 {
801   fprintf_filtered (file, _("\
802 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
803                     value);
804 }
805
806
807 /* Various complaints about symbol reading that don't abort the process */
808
809 static void
810 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
811 {
812   complaint (&symfile_complaints,
813              _("statement list doesn't fit in .debug_line section"));
814 }
815
816 static void
817 dwarf2_debug_line_missing_file_complaint (void)
818 {
819   complaint (&symfile_complaints,
820              _(".debug_line section has line data without a file"));
821 }
822
823 static void
824 dwarf2_debug_line_missing_end_sequence_complaint (void)
825 {
826   complaint (&symfile_complaints,
827              _(".debug_line section has line program sequence without an end"));
828 }
829
830 static void
831 dwarf2_complex_location_expr_complaint (void)
832 {
833   complaint (&symfile_complaints, _("location expression too complex"));
834 }
835
836 static void
837 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
838                                               int arg3)
839 {
840   complaint (&symfile_complaints,
841              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
842              arg2, arg3);
843 }
844
845 static void
846 dwarf2_macros_too_long_complaint (void)
847 {
848   complaint (&symfile_complaints,
849              _("macro info runs off end of `.debug_macinfo' section"));
850 }
851
852 static void
853 dwarf2_macro_malformed_definition_complaint (const char *arg1)
854 {
855   complaint (&symfile_complaints,
856              _("macro debug info contains a malformed macro definition:\n`%s'"),
857              arg1);
858 }
859
860 static void
861 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
862 {
863   complaint (&symfile_complaints,
864              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
865 }
866
867 /* local function prototypes */
868
869 static void dwarf2_locate_sections (bfd *, asection *, void *);
870
871 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
872                                            struct objfile *);
873
874 static void dwarf2_build_psymtabs_hard (struct objfile *);
875
876 static void scan_partial_symbols (struct partial_die_info *,
877                                   CORE_ADDR *, CORE_ADDR *,
878                                   int, struct dwarf2_cu *);
879
880 static void add_partial_symbol (struct partial_die_info *,
881                                 struct dwarf2_cu *);
882
883 static void add_partial_namespace (struct partial_die_info *pdi,
884                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
885                                    int need_pc, struct dwarf2_cu *cu);
886
887 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
888                                 CORE_ADDR *highpc, int need_pc,
889                                 struct dwarf2_cu *cu);
890
891 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
892                                      struct dwarf2_cu *cu);
893
894 static void add_partial_subprogram (struct partial_die_info *pdi,
895                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
896                                     int need_pc, struct dwarf2_cu *cu);
897
898 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
899                                      gdb_byte *buffer, gdb_byte *info_ptr,
900                                      bfd *abfd, struct dwarf2_cu *cu);
901
902 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
903
904 static void psymtab_to_symtab_1 (struct partial_symtab *);
905
906 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
907
908 static void dwarf2_free_abbrev_table (void *);
909
910 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
911                                             struct dwarf2_cu *);
912
913 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
914                                                  struct dwarf2_cu *);
915
916 static struct partial_die_info *load_partial_dies (bfd *,
917                                                    gdb_byte *, gdb_byte *,
918                                                    int, struct dwarf2_cu *);
919
920 static gdb_byte *read_partial_die (struct partial_die_info *,
921                                    struct abbrev_info *abbrev,
922                                    unsigned int, bfd *,
923                                    gdb_byte *, gdb_byte *,
924                                    struct dwarf2_cu *);
925
926 static struct partial_die_info *find_partial_die (unsigned int,
927                                                   struct dwarf2_cu *);
928
929 static void fixup_partial_die (struct partial_die_info *,
930                                struct dwarf2_cu *);
931
932 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
933                                  bfd *, gdb_byte *, struct dwarf2_cu *);
934
935 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
936                                        bfd *, gdb_byte *, struct dwarf2_cu *);
937
938 static unsigned int read_1_byte (bfd *, gdb_byte *);
939
940 static int read_1_signed_byte (bfd *, gdb_byte *);
941
942 static unsigned int read_2_bytes (bfd *, gdb_byte *);
943
944 static unsigned int read_4_bytes (bfd *, gdb_byte *);
945
946 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
947
948 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
949                                unsigned int *);
950
951 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
952
953 static LONGEST read_checked_initial_length_and_offset
954   (bfd *, gdb_byte *, const struct comp_unit_head *,
955    unsigned int *, unsigned int *);
956
957 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
958                             unsigned int *);
959
960 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
961
962 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
963
964 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
965
966 static char *read_indirect_string (bfd *, gdb_byte *,
967                                    const struct comp_unit_head *,
968                                    unsigned int *);
969
970 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
971
972 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
973
974 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
975
976 static void set_cu_language (unsigned int, struct dwarf2_cu *);
977
978 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
979                                       struct dwarf2_cu *);
980
981 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
982                                                 unsigned int,
983                                                 struct dwarf2_cu *);
984
985 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
986                                struct dwarf2_cu *cu);
987
988 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
989
990 static struct die_info *die_specification (struct die_info *die,
991                                            struct dwarf2_cu **);
992
993 static void free_line_header (struct line_header *lh);
994
995 static void add_file_name (struct line_header *, char *, unsigned int,
996                            unsigned int, unsigned int);
997
998 static struct line_header *(dwarf_decode_line_header
999                             (unsigned int offset,
1000                              bfd *abfd, struct dwarf2_cu *cu));
1001
1002 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1003                                 struct dwarf2_cu *, struct partial_symtab *);
1004
1005 static void dwarf2_start_subfile (char *, char *, char *);
1006
1007 static struct symbol *new_symbol (struct die_info *, struct type *,
1008                                   struct dwarf2_cu *);
1009
1010 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1011                                        struct dwarf2_cu *, struct symbol *);
1012
1013 static void dwarf2_const_value (struct attribute *, struct symbol *,
1014                                 struct dwarf2_cu *);
1015
1016 static void dwarf2_const_value_attr (struct attribute *attr,
1017                                      struct type *type,
1018                                      const char *name,
1019                                      struct obstack *obstack,
1020                                      struct dwarf2_cu *cu, long *value,
1021                                      gdb_byte **bytes,
1022                                      struct dwarf2_locexpr_baton **baton);
1023
1024 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1025
1026 static int need_gnat_info (struct dwarf2_cu *);
1027
1028 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1029
1030 static void set_descriptive_type (struct type *, struct die_info *,
1031                                   struct dwarf2_cu *);
1032
1033 static struct type *die_containing_type (struct die_info *,
1034                                          struct dwarf2_cu *);
1035
1036 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1037                                      struct dwarf2_cu *);
1038
1039 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1040
1041 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1042
1043 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1044
1045 static char *typename_concat (struct obstack *obs, const char *prefix,
1046                               const char *suffix, int physname,
1047                               struct dwarf2_cu *cu);
1048
1049 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1050
1051 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1052
1053 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1054
1055 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1056
1057 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1058                                struct dwarf2_cu *, struct partial_symtab *);
1059
1060 static int dwarf2_get_pc_bounds (struct die_info *,
1061                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1062                                  struct partial_symtab *);
1063
1064 static void get_scope_pc_bounds (struct die_info *,
1065                                  CORE_ADDR *, CORE_ADDR *,
1066                                  struct dwarf2_cu *);
1067
1068 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1069                                         CORE_ADDR, struct dwarf2_cu *);
1070
1071 static void dwarf2_add_field (struct field_info *, struct die_info *,
1072                               struct dwarf2_cu *);
1073
1074 static void dwarf2_attach_fields_to_type (struct field_info *,
1075                                           struct type *, struct dwarf2_cu *);
1076
1077 static void dwarf2_add_member_fn (struct field_info *,
1078                                   struct die_info *, struct type *,
1079                                   struct dwarf2_cu *);
1080
1081 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1082                                              struct type *, struct dwarf2_cu *);
1083
1084 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1085
1086 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1087
1088 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1089
1090 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1091
1092 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1093
1094 static struct type *read_module_type (struct die_info *die,
1095                                       struct dwarf2_cu *cu);
1096
1097 static const char *namespace_name (struct die_info *die,
1098                                    int *is_anonymous, struct dwarf2_cu *);
1099
1100 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1101
1102 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1103
1104 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1105                                                        struct dwarf2_cu *);
1106
1107 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1108
1109 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1110                                                  gdb_byte *info_ptr,
1111                                                  gdb_byte **new_info_ptr,
1112                                                  struct die_info *parent);
1113
1114 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1115                                                gdb_byte *info_ptr,
1116                                                gdb_byte **new_info_ptr,
1117                                                struct die_info *parent);
1118
1119 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1120                                                gdb_byte *info_ptr,
1121                                                gdb_byte **new_info_ptr,
1122                                                struct die_info *parent);
1123
1124 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1125                                 struct die_info **, gdb_byte *,
1126                                 int *);
1127
1128 static void process_die (struct die_info *, struct dwarf2_cu *);
1129
1130 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1131                                        struct obstack *);
1132
1133 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1134
1135 static const char *dwarf2_full_name (char *name,
1136                                      struct die_info *die,
1137                                      struct dwarf2_cu *cu);
1138
1139 static struct die_info *dwarf2_extension (struct die_info *die,
1140                                           struct dwarf2_cu **);
1141
1142 static char *dwarf_tag_name (unsigned int);
1143
1144 static char *dwarf_attr_name (unsigned int);
1145
1146 static char *dwarf_form_name (unsigned int);
1147
1148 static char *dwarf_bool_name (unsigned int);
1149
1150 static char *dwarf_type_encoding_name (unsigned int);
1151
1152 #if 0
1153 static char *dwarf_cfi_name (unsigned int);
1154 #endif
1155
1156 static struct die_info *sibling_die (struct die_info *);
1157
1158 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1159
1160 static void dump_die_for_error (struct die_info *);
1161
1162 static void dump_die_1 (struct ui_file *, int level, int max_level,
1163                         struct die_info *);
1164
1165 /*static*/ void dump_die (struct die_info *, int max_level);
1166
1167 static void store_in_ref_table (struct die_info *,
1168                                 struct dwarf2_cu *);
1169
1170 static int is_ref_attr (struct attribute *);
1171
1172 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1173
1174 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1175
1176 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1177                                                struct attribute *,
1178                                                struct dwarf2_cu **);
1179
1180 static struct die_info *follow_die_ref (struct die_info *,
1181                                         struct attribute *,
1182                                         struct dwarf2_cu **);
1183
1184 static struct die_info *follow_die_sig (struct die_info *,
1185                                         struct attribute *,
1186                                         struct dwarf2_cu **);
1187
1188 static void read_signatured_type_at_offset (struct objfile *objfile,
1189                                             unsigned int offset);
1190
1191 static void read_signatured_type (struct objfile *,
1192                                   struct signatured_type *type_sig);
1193
1194 /* memory allocation interface */
1195
1196 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1197
1198 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1199
1200 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1201
1202 static void initialize_cu_func_list (struct dwarf2_cu *);
1203
1204 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1205                                  struct dwarf2_cu *);
1206
1207 static void dwarf_decode_macros (struct line_header *, unsigned int,
1208                                  char *, bfd *, struct dwarf2_cu *);
1209
1210 static int attr_form_is_block (struct attribute *);
1211
1212 static int attr_form_is_section_offset (struct attribute *);
1213
1214 static int attr_form_is_constant (struct attribute *);
1215
1216 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1217                                          struct symbol *sym,
1218                                          struct dwarf2_cu *cu);
1219
1220 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1221                                struct abbrev_info *abbrev,
1222                                struct dwarf2_cu *cu);
1223
1224 static void free_stack_comp_unit (void *);
1225
1226 static hashval_t partial_die_hash (const void *item);
1227
1228 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1229
1230 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1231   (unsigned int offset, struct objfile *objfile);
1232
1233 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1234   (unsigned int offset, struct objfile *objfile);
1235
1236 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1237
1238 static void free_one_comp_unit (void *);
1239
1240 static void free_cached_comp_units (void *);
1241
1242 static void age_cached_comp_units (void);
1243
1244 static void free_one_cached_comp_unit (void *);
1245
1246 static struct type *set_die_type (struct die_info *, struct type *,
1247                                   struct dwarf2_cu *);
1248
1249 static void create_all_comp_units (struct objfile *);
1250
1251 static int create_debug_types_hash_table (struct objfile *objfile);
1252
1253 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1254                                  struct objfile *);
1255
1256 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1257
1258 static void dwarf2_add_dependence (struct dwarf2_cu *,
1259                                    struct dwarf2_per_cu_data *);
1260
1261 static void dwarf2_mark (struct dwarf2_cu *);
1262
1263 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1264
1265 static struct type *get_die_type_at_offset (unsigned int,
1266                                             struct dwarf2_per_cu_data *per_cu);
1267
1268 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void dwarf2_release_queue (void *dummy);
1271
1272 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1273                              struct objfile *objfile);
1274
1275 static void process_queue (struct objfile *objfile);
1276
1277 static void find_file_and_directory (struct die_info *die,
1278                                      struct dwarf2_cu *cu,
1279                                      char **name, char **comp_dir);
1280
1281 static char *file_full_name (int file, struct line_header *lh,
1282                              const char *comp_dir);
1283
1284 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1285                                               gdb_byte *info_ptr,
1286                                               gdb_byte *buffer,
1287                                               unsigned int buffer_size,
1288                                               bfd *abfd);
1289
1290 static void init_cu_die_reader (struct die_reader_specs *reader,
1291                                 struct dwarf2_cu *cu);
1292
1293 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1294
1295 #if WORDS_BIGENDIAN
1296
1297 /* Convert VALUE between big- and little-endian.  */
1298 static offset_type
1299 byte_swap (offset_type value)
1300 {
1301   offset_type result;
1302
1303   result = (value & 0xff) << 24;
1304   result |= (value & 0xff00) << 8;
1305   result |= (value & 0xff0000) >> 8;
1306   result |= (value & 0xff000000) >> 24;
1307   return result;
1308 }
1309
1310 #define MAYBE_SWAP(V)  byte_swap (V)
1311
1312 #else
1313 #define MAYBE_SWAP(V) (V)
1314 #endif /* WORDS_BIGENDIAN */
1315
1316 /* The suffix for an index file.  */
1317 #define INDEX_SUFFIX ".gdb-index"
1318
1319 static const char *dwarf2_physname (char *name, struct die_info *die,
1320                                     struct dwarf2_cu *cu);
1321
1322 /* Try to locate the sections we need for DWARF 2 debugging
1323    information and return true if we have enough to do something.  */
1324
1325 int
1326 dwarf2_has_info (struct objfile *objfile)
1327 {
1328   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329   if (!dwarf2_per_objfile)
1330     {
1331       /* Initialize per-objfile state.  */
1332       struct dwarf2_per_objfile *data
1333         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1334
1335       memset (data, 0, sizeof (*data));
1336       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337       dwarf2_per_objfile = data;
1338
1339       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1340       dwarf2_per_objfile->objfile = objfile;
1341     }
1342   return (dwarf2_per_objfile->info.asection != NULL
1343           && dwarf2_per_objfile->abbrev.asection != NULL);
1344 }
1345
1346 /* When loading sections, we can either look for ".<name>", or for
1347  * ".z<name>", which indicates a compressed section.  */
1348
1349 static int
1350 section_is_p (const char *section_name, const char *name)
1351 {
1352   return (section_name[0] == '.'
1353           && (strcmp (section_name + 1, name) == 0
1354               || (section_name[1] == 'z'
1355                   && strcmp (section_name + 2, name) == 0)));
1356 }
1357
1358 /* This function is mapped across the sections and remembers the
1359    offset and size of each of the debugging sections we are interested
1360    in.  */
1361
1362 static void
1363 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1364 {
1365   if (section_is_p (sectp->name, INFO_SECTION))
1366     {
1367       dwarf2_per_objfile->info.asection = sectp;
1368       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1369     }
1370   else if (section_is_p (sectp->name, ABBREV_SECTION))
1371     {
1372       dwarf2_per_objfile->abbrev.asection = sectp;
1373       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1374     }
1375   else if (section_is_p (sectp->name, LINE_SECTION))
1376     {
1377       dwarf2_per_objfile->line.asection = sectp;
1378       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1379     }
1380   else if (section_is_p (sectp->name, LOC_SECTION))
1381     {
1382       dwarf2_per_objfile->loc.asection = sectp;
1383       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1384     }
1385   else if (section_is_p (sectp->name, MACINFO_SECTION))
1386     {
1387       dwarf2_per_objfile->macinfo.asection = sectp;
1388       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1389     }
1390   else if (section_is_p (sectp->name, STR_SECTION))
1391     {
1392       dwarf2_per_objfile->str.asection = sectp;
1393       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1394     }
1395   else if (section_is_p (sectp->name, FRAME_SECTION))
1396     {
1397       dwarf2_per_objfile->frame.asection = sectp;
1398       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1399     }
1400   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1401     {
1402       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1403
1404       if (aflag & SEC_HAS_CONTENTS)
1405         {
1406           dwarf2_per_objfile->eh_frame.asection = sectp;
1407           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1408         }
1409     }
1410   else if (section_is_p (sectp->name, RANGES_SECTION))
1411     {
1412       dwarf2_per_objfile->ranges.asection = sectp;
1413       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1414     }
1415   else if (section_is_p (sectp->name, TYPES_SECTION))
1416     {
1417       dwarf2_per_objfile->types.asection = sectp;
1418       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1419     }
1420   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1421     {
1422       dwarf2_per_objfile->gdb_index.asection = sectp;
1423       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1424     }
1425
1426   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1427       && bfd_section_vma (abfd, sectp) == 0)
1428     dwarf2_per_objfile->has_section_at_zero = 1;
1429 }
1430
1431 /* Decompress a section that was compressed using zlib.  Store the
1432    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1433
1434 static void
1435 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1436                          gdb_byte **outbuf, bfd_size_type *outsize)
1437 {
1438   bfd *abfd = objfile->obfd;
1439 #ifndef HAVE_ZLIB_H
1440   error (_("Support for zlib-compressed DWARF data (from '%s') "
1441            "is disabled in this copy of GDB"),
1442          bfd_get_filename (abfd));
1443 #else
1444   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1445   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1446   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1447   bfd_size_type uncompressed_size;
1448   gdb_byte *uncompressed_buffer;
1449   z_stream strm;
1450   int rc;
1451   int header_size = 12;
1452
1453   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1454       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1455     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1456            bfd_get_filename (abfd));
1457
1458   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1459      by the uncompressed section size, 8 bytes in big-endian order.  */
1460   if (compressed_size < header_size
1461       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1462     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1463            bfd_get_filename (abfd));
1464   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1465   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1466   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1467   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1468   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1469   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1470   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1471   uncompressed_size += compressed_buffer[11];
1472
1473   /* It is possible the section consists of several compressed
1474      buffers concatenated together, so we uncompress in a loop.  */
1475   strm.zalloc = NULL;
1476   strm.zfree = NULL;
1477   strm.opaque = NULL;
1478   strm.avail_in = compressed_size - header_size;
1479   strm.next_in = (Bytef*) compressed_buffer + header_size;
1480   strm.avail_out = uncompressed_size;
1481   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1482                                        uncompressed_size);
1483   rc = inflateInit (&strm);
1484   while (strm.avail_in > 0)
1485     {
1486       if (rc != Z_OK)
1487         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1488                bfd_get_filename (abfd), rc);
1489       strm.next_out = ((Bytef*) uncompressed_buffer
1490                        + (uncompressed_size - strm.avail_out));
1491       rc = inflate (&strm, Z_FINISH);
1492       if (rc != Z_STREAM_END)
1493         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1494                bfd_get_filename (abfd), rc);
1495       rc = inflateReset (&strm);
1496     }
1497   rc = inflateEnd (&strm);
1498   if (rc != Z_OK
1499       || strm.avail_out != 0)
1500     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1501            bfd_get_filename (abfd), rc);
1502
1503   do_cleanups (cleanup);
1504   *outbuf = uncompressed_buffer;
1505   *outsize = uncompressed_size;
1506 #endif
1507 }
1508
1509 /* Read the contents of the section SECTP from object file specified by
1510    OBJFILE, store info about the section into INFO.
1511    If the section is compressed, uncompress it before returning.  */
1512
1513 static void
1514 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1515 {
1516   bfd *abfd = objfile->obfd;
1517   asection *sectp = info->asection;
1518   gdb_byte *buf, *retbuf;
1519   unsigned char header[4];
1520
1521   if (info->readin)
1522     return;
1523   info->buffer = NULL;
1524   info->was_mmapped = 0;
1525   info->readin = 1;
1526
1527   if (info->asection == NULL || info->size == 0)
1528     return;
1529
1530   /* Check if the file has a 4-byte header indicating compression.  */
1531   if (info->size > sizeof (header)
1532       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1533       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1534     {
1535       /* Upon decompression, update the buffer and its size.  */
1536       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1537         {
1538           zlib_decompress_section (objfile, sectp, &info->buffer,
1539                                    &info->size);
1540           return;
1541         }
1542     }
1543
1544 #ifdef HAVE_MMAP
1545   if (pagesize == 0)
1546     pagesize = getpagesize ();
1547
1548   /* Only try to mmap sections which are large enough: we don't want to
1549      waste space due to fragmentation.  Also, only try mmap for sections
1550      without relocations.  */
1551
1552   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1553     {
1554       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1555       size_t map_length = info->size + sectp->filepos - pg_offset;
1556       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1557                                  MAP_PRIVATE, pg_offset);
1558
1559       if (retbuf != MAP_FAILED)
1560         {
1561           info->was_mmapped = 1;
1562           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1563 #if HAVE_POSIX_MADVISE
1564           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1565 #endif
1566           return;
1567         }
1568     }
1569 #endif
1570
1571   /* If we get here, we are a normal, not-compressed section.  */
1572   info->buffer = buf
1573     = obstack_alloc (&objfile->objfile_obstack, info->size);
1574
1575   /* When debugging .o files, we may need to apply relocations; see
1576      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1577      We never compress sections in .o files, so we only need to
1578      try this when the section is not compressed.  */
1579   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1580   if (retbuf != NULL)
1581     {
1582       info->buffer = retbuf;
1583       return;
1584     }
1585
1586   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1587       || bfd_bread (buf, info->size, abfd) != info->size)
1588     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1589            bfd_get_filename (abfd));
1590 }
1591
1592 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1593    SECTION_NAME. */
1594
1595 void
1596 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1597                          asection **sectp, gdb_byte **bufp,
1598                          bfd_size_type *sizep)
1599 {
1600   struct dwarf2_per_objfile *data
1601     = objfile_data (objfile, dwarf2_objfile_data_key);
1602   struct dwarf2_section_info *info;
1603
1604   /* We may see an objfile without any DWARF, in which case we just
1605      return nothing.  */
1606   if (data == NULL)
1607     {
1608       *sectp = NULL;
1609       *bufp = NULL;
1610       *sizep = 0;
1611       return;
1612     }
1613   if (section_is_p (section_name, EH_FRAME_SECTION))
1614     info = &data->eh_frame;
1615   else if (section_is_p (section_name, FRAME_SECTION))
1616     info = &data->frame;
1617   else
1618     gdb_assert_not_reached ("unexpected section");
1619
1620   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1621     /* We haven't read this section in yet.  Do it now.  */
1622     dwarf2_read_section (objfile, info);
1623
1624   *sectp = info->asection;
1625   *bufp = info->buffer;
1626   *sizep = info->size;
1627 }
1628
1629 \f
1630
1631 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1632    this CU came.  */
1633 static void
1634 dw2_do_instantiate_symtab (struct objfile *objfile,
1635                            struct dwarf2_per_cu_data *per_cu)
1636 {
1637   struct cleanup *back_to;
1638
1639   back_to = make_cleanup (dwarf2_release_queue, NULL);
1640
1641   queue_comp_unit (per_cu, objfile);
1642
1643   if (per_cu->from_debug_types)
1644     read_signatured_type_at_offset (objfile, per_cu->offset);
1645   else
1646     load_full_comp_unit (per_cu, objfile);
1647
1648   process_queue (objfile);
1649
1650   /* Age the cache, releasing compilation units that have not
1651      been used recently.  */
1652   age_cached_comp_units ();
1653
1654   do_cleanups (back_to);
1655 }
1656
1657 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1658    the objfile from which this CU came.  Returns the resulting symbol
1659    table.  */
1660 static struct symtab *
1661 dw2_instantiate_symtab (struct objfile *objfile,
1662                         struct dwarf2_per_cu_data *per_cu)
1663 {
1664   if (!per_cu->v.quick->symtab)
1665     {
1666       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1667       increment_reading_symtab ();
1668       dw2_do_instantiate_symtab (objfile, per_cu);
1669       do_cleanups (back_to);
1670     }
1671   return per_cu->v.quick->symtab;
1672 }
1673
1674 /* Return the CU given its index.  */
1675 static struct dwarf2_per_cu_data *
1676 dw2_get_cu (int index)
1677 {
1678   if (index >= dwarf2_per_objfile->n_comp_units)
1679     {
1680       index -= dwarf2_per_objfile->n_comp_units;
1681       return dwarf2_per_objfile->type_comp_units[index];
1682     }
1683   return dwarf2_per_objfile->all_comp_units[index];
1684 }
1685
1686 /* A helper function that knows how to read a 64-bit value in a way
1687    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1688    otherwise.  */
1689 static int
1690 extract_cu_value (const char *bytes, ULONGEST *result)
1691 {
1692   if (sizeof (ULONGEST) < 8)
1693     {
1694       int i;
1695
1696       /* Ignore the upper 4 bytes if they are all zero.  */
1697       for (i = 0; i < 4; ++i)
1698         if (bytes[i + 4] != 0)
1699           return 0;
1700
1701       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1702     }
1703   else
1704     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1705   return 1;
1706 }
1707
1708 /* Read the CU list from the mapped index, and use it to create all
1709    the CU objects for this objfile.  Return 0 if something went wrong,
1710    1 if everything went ok.  */
1711 static int
1712 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1713                        offset_type cu_list_elements)
1714 {
1715   offset_type i;
1716
1717   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1718   dwarf2_per_objfile->all_comp_units
1719     = obstack_alloc (&objfile->objfile_obstack,
1720                      dwarf2_per_objfile->n_comp_units
1721                      * sizeof (struct dwarf2_per_cu_data *));
1722
1723   for (i = 0; i < cu_list_elements; i += 2)
1724     {
1725       struct dwarf2_per_cu_data *the_cu;
1726       ULONGEST offset, length;
1727
1728       if (!extract_cu_value (cu_list, &offset)
1729           || !extract_cu_value (cu_list + 8, &length))
1730         return 0;
1731       cu_list += 2 * 8;
1732
1733       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734                                struct dwarf2_per_cu_data);
1735       the_cu->offset = offset;
1736       the_cu->length = length;
1737       the_cu->objfile = objfile;
1738       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739                                         struct dwarf2_per_cu_quick_data);
1740       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1741     }
1742
1743   return 1;
1744 }
1745
1746 /* Create the signatured type hash table from the index.  */
1747
1748 static int
1749 create_signatured_type_table_from_index (struct objfile *objfile,
1750                                          const gdb_byte *bytes,
1751                                          offset_type elements)
1752 {
1753   offset_type i;
1754   htab_t sig_types_hash;
1755
1756   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1757   dwarf2_per_objfile->type_comp_units
1758     = obstack_alloc (&objfile->objfile_obstack,
1759                      dwarf2_per_objfile->n_type_comp_units
1760                      * sizeof (struct dwarf2_per_cu_data *));
1761
1762   sig_types_hash = allocate_signatured_type_table (objfile);
1763
1764   for (i = 0; i < elements; i += 3)
1765     {
1766       struct signatured_type *type_sig;
1767       ULONGEST offset, type_offset, signature;
1768       void **slot;
1769
1770       if (!extract_cu_value (bytes, &offset)
1771           || !extract_cu_value (bytes + 8, &type_offset))
1772         return 0;
1773       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1774       bytes += 3 * 8;
1775
1776       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1777                                  struct signatured_type);
1778       type_sig->signature = signature;
1779       type_sig->offset = offset;
1780       type_sig->type_offset = type_offset;
1781       type_sig->per_cu.from_debug_types = 1;
1782       type_sig->per_cu.offset = offset;
1783       type_sig->per_cu.objfile = objfile;
1784       type_sig->per_cu.v.quick
1785         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1786                           struct dwarf2_per_cu_quick_data);
1787
1788       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1789       *slot = type_sig;
1790
1791       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1792     }
1793
1794   dwarf2_per_objfile->signatured_types = sig_types_hash;
1795
1796   return 1;
1797 }
1798
1799 /* Read the address map data from the mapped index, and use it to
1800    populate the objfile's psymtabs_addrmap.  */
1801 static void
1802 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1803 {
1804   const gdb_byte *iter, *end;
1805   struct obstack temp_obstack;
1806   struct addrmap *mutable_map;
1807   struct cleanup *cleanup;
1808   CORE_ADDR baseaddr;
1809
1810   obstack_init (&temp_obstack);
1811   cleanup = make_cleanup_obstack_free (&temp_obstack);
1812   mutable_map = addrmap_create_mutable (&temp_obstack);
1813
1814   iter = index->address_table;
1815   end = iter + index->address_table_size;
1816
1817   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1818
1819   while (iter < end)
1820     {
1821       ULONGEST hi, lo, cu_index;
1822       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1823       iter += 8;
1824       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1825       iter += 8;
1826       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1827       iter += 4;
1828       
1829       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1830                          dw2_get_cu (cu_index));
1831     }
1832
1833   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1834                                                     &objfile->objfile_obstack);
1835   do_cleanups (cleanup);
1836 }
1837
1838 /* The hash function for strings in the mapped index.  This is the
1839    same as the hashtab.c hash function, but we keep a separate copy to
1840    maintain control over the implementation.  This is necessary
1841    because the hash function is tied to the format of the mapped index
1842    file.  */
1843 static hashval_t
1844 mapped_index_string_hash (const void *p)
1845 {
1846   const unsigned char *str = (const unsigned char *) p;
1847   hashval_t r = 0;
1848   unsigned char c;
1849
1850   while ((c = *str++) != 0)
1851     r = r * 67 + c - 113;
1852
1853   return r;
1854 }
1855
1856 /* Find a slot in the mapped index INDEX for the object named NAME.
1857    If NAME is found, set *VEC_OUT to point to the CU vector in the
1858    constant pool and return 1.  If NAME cannot be found, return 0.  */
1859 static int
1860 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1861                           offset_type **vec_out)
1862 {
1863   offset_type hash = mapped_index_string_hash (name);
1864   offset_type slot, step;
1865
1866   slot = hash & (index->index_table_slots - 1);
1867   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1868
1869   for (;;)
1870     {
1871       /* Convert a slot number to an offset into the table.  */
1872       offset_type i = 2 * slot;
1873       const char *str;
1874       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1875         return 0;
1876
1877       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1878       if (!strcmp (name, str))
1879         {
1880           *vec_out = (offset_type *) (index->constant_pool
1881                                       + MAYBE_SWAP (index->index_table[i + 1]));
1882           return 1;
1883         }
1884
1885       slot = (slot + step) & (index->index_table_slots - 1);
1886     }
1887 }
1888
1889 /* Read the index file.  If everything went ok, initialize the "quick"
1890    elements of all the CUs and return 1.  Otherwise, return 0.  */
1891 static int
1892 dwarf2_read_index (struct objfile *objfile)
1893 {
1894   char *addr;
1895   struct mapped_index *map;
1896   offset_type *metadata;
1897   const gdb_byte *cu_list;
1898   const gdb_byte *types_list = NULL;
1899   offset_type version, cu_list_elements;
1900   offset_type types_list_elements = 0;
1901   int i;
1902
1903   if (dwarf2_per_objfile->gdb_index.asection == NULL
1904       || dwarf2_per_objfile->gdb_index.size == 0)
1905     return 0;
1906
1907   /* Older elfutils strip versions could keep the section in the main
1908      executable while splitting it for the separate debug info file.  */
1909   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1910        & SEC_HAS_CONTENTS) == 0)
1911     return 0;
1912
1913   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1914
1915   addr = dwarf2_per_objfile->gdb_index.buffer;
1916   /* Version check.  */
1917   version = MAYBE_SWAP (*(offset_type *) addr);
1918   if (version == 1)
1919     {
1920       /* Index version 1 neglected to account for .debug_types.  So,
1921          if we see .debug_types, we cannot use this index.  */
1922       if (dwarf2_per_objfile->types.asection != NULL
1923           && dwarf2_per_objfile->types.size != 0)
1924         return 0;
1925     }
1926   else if (version != 2)
1927     return 0;
1928
1929   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1930   map->total_size = dwarf2_per_objfile->gdb_index.size;
1931
1932   metadata = (offset_type *) (addr + sizeof (offset_type));
1933
1934   i = 0;
1935   cu_list = addr + MAYBE_SWAP (metadata[i]);
1936   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1937                       / 8);
1938   ++i;
1939
1940   if (version == 2)
1941     {
1942       types_list = addr + MAYBE_SWAP (metadata[i]);
1943       types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1944                               - MAYBE_SWAP (metadata[i]))
1945                              / 8);
1946       ++i;
1947     }
1948
1949   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1950   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1951                              - MAYBE_SWAP (metadata[i]));
1952   ++i;
1953
1954   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1955   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1956                              - MAYBE_SWAP (metadata[i]))
1957                             / (2 * sizeof (offset_type)));
1958   ++i;
1959
1960   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1961
1962   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1963     return 0;
1964
1965   if (version == 2
1966       && types_list_elements
1967       && !create_signatured_type_table_from_index (objfile, types_list,
1968                                                    types_list_elements))
1969     return 0;
1970
1971   create_addrmap_from_index (objfile, map);
1972
1973   dwarf2_per_objfile->index_table = map;
1974   dwarf2_per_objfile->using_index = 1;
1975
1976   return 1;
1977 }
1978
1979 /* A helper for the "quick" functions which sets the global
1980    dwarf2_per_objfile according to OBJFILE.  */
1981 static void
1982 dw2_setup (struct objfile *objfile)
1983 {
1984   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1985   gdb_assert (dwarf2_per_objfile);
1986 }
1987
1988 /* A helper for the "quick" functions which attempts to read the line
1989    table for THIS_CU.  */
1990 static void
1991 dw2_require_line_header (struct objfile *objfile,
1992                          struct dwarf2_per_cu_data *this_cu)
1993 {
1994   bfd *abfd = objfile->obfd;
1995   struct line_header *lh = NULL;
1996   struct attribute *attr;
1997   struct cleanup *cleanups;
1998   struct die_info *comp_unit_die;
1999   struct dwarf2_section_info* sec;
2000   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2001   int has_children, i;
2002   struct dwarf2_cu cu;
2003   unsigned int bytes_read, buffer_size;
2004   struct die_reader_specs reader_specs;
2005   char *name, *comp_dir;
2006
2007   if (this_cu->v.quick->read_lines)
2008     return;
2009   this_cu->v.quick->read_lines = 1;
2010
2011   memset (&cu, 0, sizeof (cu));
2012   cu.objfile = objfile;
2013   obstack_init (&cu.comp_unit_obstack);
2014
2015   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2016
2017   if (this_cu->from_debug_types)
2018     sec = &dwarf2_per_objfile->types;
2019   else
2020     sec = &dwarf2_per_objfile->info;
2021   dwarf2_read_section (objfile, sec);
2022   buffer_size = sec->size;
2023   buffer = sec->buffer;
2024   info_ptr = buffer + this_cu->offset;
2025   beg_of_comp_unit = info_ptr;
2026
2027   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2028                                           buffer, buffer_size,
2029                                           abfd);
2030
2031   /* Complete the cu_header.  */
2032   cu.header.offset = beg_of_comp_unit - buffer;
2033   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2034
2035   this_cu->cu = &cu;
2036   cu.per_cu = this_cu;
2037
2038   dwarf2_read_abbrevs (abfd, &cu);
2039   make_cleanup (dwarf2_free_abbrev_table, &cu);
2040
2041   if (this_cu->from_debug_types)
2042     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2043   init_cu_die_reader (&reader_specs, &cu);
2044   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2045                             &has_children);
2046
2047   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2048   if (attr)
2049     {
2050       unsigned int line_offset = DW_UNSND (attr);
2051       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2052     }
2053   if (lh == NULL)
2054     {
2055       do_cleanups (cleanups);
2056       return;
2057     }
2058
2059   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2060
2061   this_cu->v.quick->lines = lh;
2062
2063   this_cu->v.quick->file_names
2064     = obstack_alloc (&objfile->objfile_obstack,
2065                      lh->num_file_names * sizeof (char *));
2066   for (i = 0; i < lh->num_file_names; ++i)
2067     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2068
2069   do_cleanups (cleanups);
2070 }
2071
2072 /* A helper for the "quick" functions which computes and caches the
2073    real path for a given file name from the line table.
2074    dw2_require_line_header must have been called before this is
2075    invoked.  */
2076 static const char *
2077 dw2_require_full_path (struct objfile *objfile,
2078                        struct dwarf2_per_cu_data *per_cu,
2079                        int index)
2080 {
2081   if (!per_cu->v.quick->full_names)
2082     per_cu->v.quick->full_names
2083       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2084                         per_cu->v.quick->lines->num_file_names,
2085                         sizeof (char *));
2086
2087   if (!per_cu->v.quick->full_names[index])
2088     per_cu->v.quick->full_names[index]
2089       = gdb_realpath (per_cu->v.quick->file_names[index]);
2090
2091   return per_cu->v.quick->full_names[index];
2092 }
2093
2094 static struct symtab *
2095 dw2_find_last_source_symtab (struct objfile *objfile)
2096 {
2097   int index;
2098   dw2_setup (objfile);
2099   index = dwarf2_per_objfile->n_comp_units - 1;
2100   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2101 }
2102
2103 static void
2104 dw2_forget_cached_source_info (struct objfile *objfile)
2105 {
2106   int i;
2107
2108   dw2_setup (objfile);
2109   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2110                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2111     {
2112       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2113
2114       if (per_cu->v.quick->full_names)
2115         {
2116           int j;
2117
2118           for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2119             xfree ((void *) per_cu->v.quick->full_names[j]);
2120         }
2121     }
2122 }
2123
2124 static int
2125 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2126                    const char *full_path, const char *real_path,
2127                    struct symtab **result)
2128 {
2129   int i;
2130   int check_basename = lbasename (name) == name;
2131   struct dwarf2_per_cu_data *base_cu = NULL;
2132
2133   dw2_setup (objfile);
2134   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2135                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2136     {
2137       int j;
2138       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2139
2140       if (per_cu->v.quick->symtab)
2141         continue;
2142
2143       dw2_require_line_header (objfile, per_cu);
2144       if (!per_cu->v.quick->lines)
2145         continue;
2146
2147       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2148         {
2149           const char *this_name = per_cu->v.quick->file_names[j];
2150
2151           if (FILENAME_CMP (name, this_name) == 0)
2152             {
2153               *result = dw2_instantiate_symtab (objfile, per_cu);
2154               return 1;
2155             }
2156
2157           if (check_basename && ! base_cu
2158               && FILENAME_CMP (lbasename (this_name), name) == 0)
2159             base_cu = per_cu;
2160
2161           if (full_path != NULL)
2162             {
2163               const char *this_full_name = dw2_require_full_path (objfile,
2164                                                                   per_cu, j);
2165
2166               if (this_full_name
2167                   && FILENAME_CMP (full_path, this_full_name) == 0)
2168                 {
2169                   *result = dw2_instantiate_symtab (objfile, per_cu);
2170                   return 1;
2171                 }
2172             }
2173
2174           if (real_path != NULL)
2175             {
2176               const char *this_full_name = dw2_require_full_path (objfile,
2177                                                                   per_cu, j);
2178
2179               if (this_full_name != NULL)
2180                 {
2181                   char *rp = gdb_realpath (this_full_name);
2182                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2183                     {
2184                       xfree (rp);
2185                       *result = dw2_instantiate_symtab (objfile, per_cu);
2186                       return 1;
2187                     }
2188                   xfree (rp);
2189                 }
2190             }
2191         }
2192     }
2193
2194   if (base_cu)
2195     {
2196       *result = dw2_instantiate_symtab (objfile, base_cu);
2197       return 1;
2198     }
2199
2200   return 0;
2201 }
2202
2203 static struct symtab *
2204 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2205                    const char *name, domain_enum domain)
2206 {
2207   /* We do all the work in the pre_expand_symtabs_matching hook
2208      instead.  */
2209   return NULL;
2210 }
2211
2212 /* A helper function that expands all symtabs that hold an object
2213    named NAME.  */
2214 static void
2215 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2216 {
2217   dw2_setup (objfile);
2218
2219   if (dwarf2_per_objfile->index_table)
2220     {
2221       offset_type *vec;
2222
2223       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2224                                     name, &vec))
2225         {
2226           offset_type i, len = MAYBE_SWAP (*vec);
2227           for (i = 0; i < len; ++i)
2228             {
2229               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2230               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2231
2232               dw2_instantiate_symtab (objfile, per_cu);
2233             }
2234         }
2235     }
2236 }
2237
2238 static void
2239 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2240                                  int kind, const char *name,
2241                                  domain_enum domain)
2242 {
2243   dw2_do_expand_symtabs_matching (objfile, name);
2244 }
2245
2246 static void
2247 dw2_print_stats (struct objfile *objfile)
2248 {
2249   int i, count;
2250
2251   dw2_setup (objfile);
2252   count = 0;
2253   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2254                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2255     {
2256       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2257
2258       if (!per_cu->v.quick->symtab)
2259         ++count;
2260     }
2261   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2262 }
2263
2264 static void
2265 dw2_dump (struct objfile *objfile)
2266 {
2267   /* Nothing worth printing.  */
2268 }
2269
2270 static void
2271 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2272               struct section_offsets *delta)
2273 {
2274   /* There's nothing to relocate here.  */
2275 }
2276
2277 static void
2278 dw2_expand_symtabs_for_function (struct objfile *objfile,
2279                                  const char *func_name)
2280 {
2281   dw2_do_expand_symtabs_matching (objfile, func_name);
2282 }
2283
2284 static void
2285 dw2_expand_all_symtabs (struct objfile *objfile)
2286 {
2287   int i;
2288
2289   dw2_setup (objfile);
2290
2291   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2292                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2293     {
2294       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2295
2296       dw2_instantiate_symtab (objfile, per_cu);
2297     }
2298 }
2299
2300 static void
2301 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2302                                   const char *filename)
2303 {
2304   int i;
2305
2306   dw2_setup (objfile);
2307   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2308                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2309     {
2310       int j;
2311       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2312
2313       if (per_cu->v.quick->symtab)
2314         continue;
2315
2316       dw2_require_line_header (objfile, per_cu);
2317       if (!per_cu->v.quick->lines)
2318         continue;
2319
2320       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2321         {
2322           const char *this_name = per_cu->v.quick->file_names[j];
2323           if (strcmp (this_name, filename) == 0)
2324             {
2325               dw2_instantiate_symtab (objfile, per_cu);
2326               break;
2327             }
2328         }
2329     }
2330 }
2331
2332 static const char *
2333 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2334 {
2335   struct dwarf2_per_cu_data *per_cu;
2336   offset_type *vec;
2337
2338   dw2_setup (objfile);
2339
2340   if (!dwarf2_per_objfile->index_table)
2341     return NULL;
2342
2343   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2344                                  name, &vec))
2345     return NULL;
2346
2347   /* Note that this just looks at the very first one named NAME -- but
2348      actually we are looking for a function.  find_main_filename
2349      should be rewritten so that it doesn't require a custom hook.  It
2350      could just use the ordinary symbol tables.  */
2351   /* vec[0] is the length, which must always be >0.  */
2352   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2353
2354   dw2_require_line_header (objfile, per_cu);
2355   if (!per_cu->v.quick->lines)
2356     return NULL;
2357
2358   return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2359 }
2360
2361 static void
2362 dw2_map_ada_symtabs (struct objfile *objfile,
2363                      int (*wild_match) (const char *, int, const char *),
2364                      int (*is_name_suffix) (const char *),
2365                      void (*callback) (struct objfile *,
2366                                        struct symtab *, void *),
2367                      const char *name, int global,
2368                      domain_enum namespace, int wild,
2369                      void *data)
2370 {
2371   /* For now, we don't support Ada.  Still the function can be called if the
2372      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2373      does not look for non-Ada symbols this function should just return.  */
2374 }
2375
2376 static void
2377 dw2_expand_symtabs_matching (struct objfile *objfile,
2378                              int (*file_matcher) (const char *, void *),
2379                              int (*name_matcher) (const char *, void *),
2380                              domain_enum kind,
2381                              void *data)
2382 {
2383   int i;
2384   offset_type iter;
2385
2386   dw2_setup (objfile);
2387   if (!dwarf2_per_objfile->index_table)
2388     return;
2389
2390   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2391                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2392     {
2393       int j;
2394       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2395
2396       per_cu->v.quick->mark = 0;
2397       if (per_cu->v.quick->symtab)
2398         continue;
2399
2400       dw2_require_line_header (objfile, per_cu);
2401       if (!per_cu->v.quick->lines)
2402         continue;
2403
2404       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2405         {
2406           if (file_matcher (per_cu->v.quick->file_names[j], data))
2407             {
2408               per_cu->v.quick->mark = 1;
2409               break;
2410             }
2411         }
2412     }
2413
2414   for (iter = 0;
2415        iter < dwarf2_per_objfile->index_table->index_table_slots;
2416        ++iter)
2417     {
2418       offset_type idx = 2 * iter;
2419       const char *name;
2420       offset_type *vec, vec_len, vec_idx;
2421
2422       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2423           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2424         continue;
2425
2426       name = (dwarf2_per_objfile->index_table->constant_pool
2427               + dwarf2_per_objfile->index_table->index_table[idx]);
2428
2429       if (! (*name_matcher) (name, data))
2430         continue;
2431
2432       /* The name was matched, now expand corresponding CUs that were
2433          marked.  */
2434       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2435                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2436       vec_len = MAYBE_SWAP (vec[0]);
2437       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2438         {
2439           struct dwarf2_per_cu_data *per_cu;
2440
2441           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2442           if (per_cu->v.quick->mark)
2443             dw2_instantiate_symtab (objfile, per_cu);
2444         }
2445     }
2446 }
2447
2448 static struct symtab *
2449 dw2_find_pc_sect_symtab (struct objfile *objfile,
2450                          struct minimal_symbol *msymbol,
2451                          CORE_ADDR pc,
2452                          struct obj_section *section,
2453                          int warn_if_readin)
2454 {
2455   struct dwarf2_per_cu_data *data;
2456
2457   dw2_setup (objfile);
2458
2459   if (!objfile->psymtabs_addrmap)
2460     return NULL;
2461
2462   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2463   if (!data)
2464     return NULL;
2465
2466   if (warn_if_readin && data->v.quick->symtab)
2467     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2468              paddress (get_objfile_arch (objfile), pc));
2469
2470   return dw2_instantiate_symtab (objfile, data);
2471 }
2472
2473 static void
2474 dw2_map_symbol_names (struct objfile *objfile,
2475                       void (*fun) (const char *, void *),
2476                       void *data)
2477 {
2478   offset_type iter;
2479   dw2_setup (objfile);
2480
2481   if (!dwarf2_per_objfile->index_table)
2482     return;
2483
2484   for (iter = 0;
2485        iter < dwarf2_per_objfile->index_table->index_table_slots;
2486        ++iter)
2487     {
2488       offset_type idx = 2 * iter;
2489       const char *name;
2490       offset_type *vec, vec_len, vec_idx;
2491
2492       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2493           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2494         continue;
2495
2496       name = (dwarf2_per_objfile->index_table->constant_pool
2497               + dwarf2_per_objfile->index_table->index_table[idx]);
2498
2499       (*fun) (name, data);
2500     }
2501 }
2502
2503 static void
2504 dw2_map_symbol_filenames (struct objfile *objfile,
2505                           void (*fun) (const char *, const char *, void *),
2506                           void *data)
2507 {
2508   int i;
2509
2510   dw2_setup (objfile);
2511   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2512                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2513     {
2514       int j;
2515       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2516
2517       if (per_cu->v.quick->symtab)
2518         continue;
2519
2520       dw2_require_line_header (objfile, per_cu);
2521       if (!per_cu->v.quick->lines)
2522         continue;
2523
2524       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2525         {
2526           const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2527                                                               j);
2528           (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2529         }
2530     }
2531 }
2532
2533 static int
2534 dw2_has_symbols (struct objfile *objfile)
2535 {
2536   return 1;
2537 }
2538
2539 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2540 {
2541   dw2_has_symbols,
2542   dw2_find_last_source_symtab,
2543   dw2_forget_cached_source_info,
2544   dw2_lookup_symtab,
2545   dw2_lookup_symbol,
2546   dw2_pre_expand_symtabs_matching,
2547   dw2_print_stats,
2548   dw2_dump,
2549   dw2_relocate,
2550   dw2_expand_symtabs_for_function,
2551   dw2_expand_all_symtabs,
2552   dw2_expand_symtabs_with_filename,
2553   dw2_find_symbol_file,
2554   dw2_map_ada_symtabs,
2555   dw2_expand_symtabs_matching,
2556   dw2_find_pc_sect_symtab,
2557   dw2_map_symbol_names,
2558   dw2_map_symbol_filenames
2559 };
2560
2561 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2562    file will use psymtabs, or 1 if using the GNU index.  */
2563
2564 int
2565 dwarf2_initialize_objfile (struct objfile *objfile)
2566 {
2567   /* If we're about to read full symbols, don't bother with the
2568      indices.  In this case we also don't care if some other debug
2569      format is making psymtabs, because they are all about to be
2570      expanded anyway.  */
2571   if ((objfile->flags & OBJF_READNOW))
2572     {
2573       int i;
2574
2575       dwarf2_per_objfile->using_index = 1;
2576       create_all_comp_units (objfile);
2577       create_debug_types_hash_table (objfile);
2578
2579       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2580                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2581         {
2582           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2583
2584           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2585                                             struct dwarf2_per_cu_quick_data);
2586         }
2587
2588       /* Return 1 so that gdb sees the "quick" functions.  However,
2589          these functions will be no-ops because we will have expanded
2590          all symtabs.  */
2591       return 1;
2592     }
2593
2594   if (dwarf2_read_index (objfile))
2595     return 1;
2596
2597   dwarf2_build_psymtabs (objfile);
2598   return 0;
2599 }
2600
2601 \f
2602
2603 /* Build a partial symbol table.  */
2604
2605 void
2606 dwarf2_build_psymtabs (struct objfile *objfile)
2607 {
2608   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2609     {
2610       init_psymbol_list (objfile, 1024);
2611     }
2612
2613   dwarf2_build_psymtabs_hard (objfile);
2614 }
2615
2616 /* Return TRUE if OFFSET is within CU_HEADER.  */
2617
2618 static inline int
2619 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2620 {
2621   unsigned int bottom = cu_header->offset;
2622   unsigned int top = (cu_header->offset
2623                       + cu_header->length
2624                       + cu_header->initial_length_size);
2625
2626   return (offset >= bottom && offset < top);
2627 }
2628
2629 /* Read in the comp unit header information from the debug_info at info_ptr.
2630    NOTE: This leaves members offset, first_die_offset to be filled in
2631    by the caller.  */
2632
2633 static gdb_byte *
2634 read_comp_unit_head (struct comp_unit_head *cu_header,
2635                      gdb_byte *info_ptr, bfd *abfd)
2636 {
2637   int signed_addr;
2638   unsigned int bytes_read;
2639
2640   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2641   cu_header->initial_length_size = bytes_read;
2642   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2643   info_ptr += bytes_read;
2644   cu_header->version = read_2_bytes (abfd, info_ptr);
2645   info_ptr += 2;
2646   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2647                                           &bytes_read);
2648   info_ptr += bytes_read;
2649   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2650   info_ptr += 1;
2651   signed_addr = bfd_get_sign_extend_vma (abfd);
2652   if (signed_addr < 0)
2653     internal_error (__FILE__, __LINE__,
2654                     _("read_comp_unit_head: dwarf from non elf file"));
2655   cu_header->signed_addr_p = signed_addr;
2656
2657   return info_ptr;
2658 }
2659
2660 static gdb_byte *
2661 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2662                              gdb_byte *buffer, unsigned int buffer_size,
2663                              bfd *abfd)
2664 {
2665   gdb_byte *beg_of_comp_unit = info_ptr;
2666
2667   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2668
2669   if (header->version != 2 && header->version != 3 && header->version != 4)
2670     error (_("Dwarf Error: wrong version in compilation unit header "
2671            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2672            bfd_get_filename (abfd));
2673
2674   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2675     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2676            "(offset 0x%lx + 6) [in module %s]"),
2677            (long) header->abbrev_offset,
2678            (long) (beg_of_comp_unit - buffer),
2679            bfd_get_filename (abfd));
2680
2681   if (beg_of_comp_unit + header->length + header->initial_length_size
2682       > buffer + buffer_size)
2683     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2684            "(offset 0x%lx + 0) [in module %s]"),
2685            (long) header->length,
2686            (long) (beg_of_comp_unit - buffer),
2687            bfd_get_filename (abfd));
2688
2689   return info_ptr;
2690 }
2691
2692 /* Read in the types comp unit header information from .debug_types entry at
2693    types_ptr.  The result is a pointer to one past the end of the header.  */
2694
2695 static gdb_byte *
2696 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2697                           ULONGEST *signature,
2698                           gdb_byte *types_ptr, bfd *abfd)
2699 {
2700   gdb_byte *initial_types_ptr = types_ptr;
2701
2702   dwarf2_read_section (dwarf2_per_objfile->objfile,
2703                        &dwarf2_per_objfile->types);
2704   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2705
2706   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2707
2708   *signature = read_8_bytes (abfd, types_ptr);
2709   types_ptr += 8;
2710   types_ptr += cu_header->offset_size;
2711   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2712
2713   return types_ptr;
2714 }
2715
2716 /* Allocate a new partial symtab for file named NAME and mark this new
2717    partial symtab as being an include of PST.  */
2718
2719 static void
2720 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2721                                struct objfile *objfile)
2722 {
2723   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2724
2725   subpst->section_offsets = pst->section_offsets;
2726   subpst->textlow = 0;
2727   subpst->texthigh = 0;
2728
2729   subpst->dependencies = (struct partial_symtab **)
2730     obstack_alloc (&objfile->objfile_obstack,
2731                    sizeof (struct partial_symtab *));
2732   subpst->dependencies[0] = pst;
2733   subpst->number_of_dependencies = 1;
2734
2735   subpst->globals_offset = 0;
2736   subpst->n_global_syms = 0;
2737   subpst->statics_offset = 0;
2738   subpst->n_static_syms = 0;
2739   subpst->symtab = NULL;
2740   subpst->read_symtab = pst->read_symtab;
2741   subpst->readin = 0;
2742
2743   /* No private part is necessary for include psymtabs.  This property
2744      can be used to differentiate between such include psymtabs and
2745      the regular ones.  */
2746   subpst->read_symtab_private = NULL;
2747 }
2748
2749 /* Read the Line Number Program data and extract the list of files
2750    included by the source file represented by PST.  Build an include
2751    partial symtab for each of these included files.  */
2752
2753 static void
2754 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2755                                struct die_info *die,
2756                                struct partial_symtab *pst)
2757 {
2758   struct objfile *objfile = cu->objfile;
2759   bfd *abfd = objfile->obfd;
2760   struct line_header *lh = NULL;
2761   struct attribute *attr;
2762
2763   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2764   if (attr)
2765     {
2766       unsigned int line_offset = DW_UNSND (attr);
2767
2768       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2769     }
2770   if (lh == NULL)
2771     return;  /* No linetable, so no includes.  */
2772
2773   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2774   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2775
2776   free_line_header (lh);
2777 }
2778
2779 static hashval_t
2780 hash_type_signature (const void *item)
2781 {
2782   const struct signatured_type *type_sig = item;
2783
2784   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2785   return type_sig->signature;
2786 }
2787
2788 static int
2789 eq_type_signature (const void *item_lhs, const void *item_rhs)
2790 {
2791   const struct signatured_type *lhs = item_lhs;
2792   const struct signatured_type *rhs = item_rhs;
2793
2794   return lhs->signature == rhs->signature;
2795 }
2796
2797 /* Allocate a hash table for signatured types.  */
2798
2799 static htab_t
2800 allocate_signatured_type_table (struct objfile *objfile)
2801 {
2802   return htab_create_alloc_ex (41,
2803                                hash_type_signature,
2804                                eq_type_signature,
2805                                NULL,
2806                                &objfile->objfile_obstack,
2807                                hashtab_obstack_allocate,
2808                                dummy_obstack_deallocate);
2809 }
2810
2811 /* A helper function to add a signatured type CU to a list.  */
2812
2813 static int
2814 add_signatured_type_cu_to_list (void **slot, void *datum)
2815 {
2816   struct signatured_type *sigt = *slot;
2817   struct dwarf2_per_cu_data ***datap = datum;
2818
2819   **datap = &sigt->per_cu;
2820   ++*datap;
2821
2822   return 1;
2823 }
2824
2825 /* Create the hash table of all entries in the .debug_types section.
2826    The result is zero if there is an error (e.g. missing .debug_types section),
2827    otherwise non-zero.  */
2828
2829 static int
2830 create_debug_types_hash_table (struct objfile *objfile)
2831 {
2832   gdb_byte *info_ptr;
2833   htab_t types_htab;
2834   struct dwarf2_per_cu_data **iter;
2835
2836   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2837   info_ptr = dwarf2_per_objfile->types.buffer;
2838
2839   if (info_ptr == NULL)
2840     {
2841       dwarf2_per_objfile->signatured_types = NULL;
2842       return 0;
2843     }
2844
2845   types_htab = allocate_signatured_type_table (objfile);
2846
2847   if (dwarf2_die_debug)
2848     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2849
2850   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2851     {
2852       unsigned int offset;
2853       unsigned int offset_size;
2854       unsigned int type_offset;
2855       unsigned int length, initial_length_size;
2856       unsigned short version;
2857       ULONGEST signature;
2858       struct signatured_type *type_sig;
2859       void **slot;
2860       gdb_byte *ptr = info_ptr;
2861
2862       offset = ptr - dwarf2_per_objfile->types.buffer;
2863
2864       /* We need to read the type's signature in order to build the hash
2865          table, but we don't need to read anything else just yet.  */
2866
2867       /* Sanity check to ensure entire cu is present.  */
2868       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2869       if (ptr + length + initial_length_size
2870           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2871         {
2872           complaint (&symfile_complaints,
2873                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2874           break;
2875         }
2876
2877       offset_size = initial_length_size == 4 ? 4 : 8;
2878       ptr += initial_length_size;
2879       version = bfd_get_16 (objfile->obfd, ptr);
2880       ptr += 2;
2881       ptr += offset_size; /* abbrev offset */
2882       ptr += 1; /* address size */
2883       signature = bfd_get_64 (objfile->obfd, ptr);
2884       ptr += 8;
2885       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2886
2887       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2888       memset (type_sig, 0, sizeof (*type_sig));
2889       type_sig->signature = signature;
2890       type_sig->offset = offset;
2891       type_sig->type_offset = type_offset;
2892       type_sig->per_cu.objfile = objfile;
2893       type_sig->per_cu.from_debug_types = 1;
2894
2895       slot = htab_find_slot (types_htab, type_sig, INSERT);
2896       gdb_assert (slot != NULL);
2897       *slot = type_sig;
2898
2899       if (dwarf2_die_debug)
2900         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2901                             offset, phex (signature, sizeof (signature)));
2902
2903       info_ptr = info_ptr + initial_length_size + length;
2904     }
2905
2906   dwarf2_per_objfile->signatured_types = types_htab;
2907
2908   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2909   dwarf2_per_objfile->type_comp_units
2910     = obstack_alloc (&objfile->objfile_obstack,
2911                      dwarf2_per_objfile->n_type_comp_units
2912                      * sizeof (struct dwarf2_per_cu_data *));
2913   iter = &dwarf2_per_objfile->type_comp_units[0];
2914   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2915   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2916               == dwarf2_per_objfile->n_type_comp_units);
2917
2918   return 1;
2919 }
2920
2921 /* Lookup a signature based type.
2922    Returns NULL if SIG is not present in the table.  */
2923
2924 static struct signatured_type *
2925 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2926 {
2927   struct signatured_type find_entry, *entry;
2928
2929   if (dwarf2_per_objfile->signatured_types == NULL)
2930     {
2931       complaint (&symfile_complaints,
2932                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2933       return 0;
2934     }
2935
2936   find_entry.signature = sig;
2937   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2938   return entry;
2939 }
2940
2941 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2942
2943 static void
2944 init_cu_die_reader (struct die_reader_specs *reader,
2945                     struct dwarf2_cu *cu)
2946 {
2947   reader->abfd = cu->objfile->obfd;
2948   reader->cu = cu;
2949   if (cu->per_cu->from_debug_types)
2950     {
2951       gdb_assert (dwarf2_per_objfile->types.readin);
2952       reader->buffer = dwarf2_per_objfile->types.buffer;
2953     }
2954   else
2955     {
2956       gdb_assert (dwarf2_per_objfile->info.readin);
2957       reader->buffer = dwarf2_per_objfile->info.buffer;
2958     }
2959 }
2960
2961 /* Find the base address of the compilation unit for range lists and
2962    location lists.  It will normally be specified by DW_AT_low_pc.
2963    In DWARF-3 draft 4, the base address could be overridden by
2964    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2965    compilation units with discontinuous ranges.  */
2966
2967 static void
2968 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2969 {
2970   struct attribute *attr;
2971
2972   cu->base_known = 0;
2973   cu->base_address = 0;
2974
2975   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2976   if (attr)
2977     {
2978       cu->base_address = DW_ADDR (attr);
2979       cu->base_known = 1;
2980     }
2981   else
2982     {
2983       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2984       if (attr)
2985         {
2986           cu->base_address = DW_ADDR (attr);
2987           cu->base_known = 1;
2988         }
2989     }
2990 }
2991
2992 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2993    to combine the common parts.
2994    Process a compilation unit for a psymtab.
2995    BUFFER is a pointer to the beginning of the dwarf section buffer,
2996    either .debug_info or debug_types.
2997    INFO_PTR is a pointer to the start of the CU.
2998    Returns a pointer to the next CU.  */
2999
3000 static gdb_byte *
3001 process_psymtab_comp_unit (struct objfile *objfile,
3002                            struct dwarf2_per_cu_data *this_cu,
3003                            gdb_byte *buffer, gdb_byte *info_ptr,
3004                            unsigned int buffer_size)
3005 {
3006   bfd *abfd = objfile->obfd;
3007   gdb_byte *beg_of_comp_unit = info_ptr;
3008   struct die_info *comp_unit_die;
3009   struct partial_symtab *pst;
3010   CORE_ADDR baseaddr;
3011   struct cleanup *back_to_inner;
3012   struct dwarf2_cu cu;
3013   int has_children, has_pc_info;
3014   struct attribute *attr;
3015   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3016   struct die_reader_specs reader_specs;
3017
3018   memset (&cu, 0, sizeof (cu));
3019   cu.objfile = objfile;
3020   obstack_init (&cu.comp_unit_obstack);
3021
3022   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3023
3024   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3025                                           buffer, buffer_size,
3026                                           abfd);
3027
3028   /* Complete the cu_header.  */
3029   cu.header.offset = beg_of_comp_unit - buffer;
3030   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3031
3032   cu.list_in_scope = &file_symbols;
3033
3034   /* If this compilation unit was already read in, free the
3035      cached copy in order to read it in again.  This is
3036      necessary because we skipped some symbols when we first
3037      read in the compilation unit (see load_partial_dies).
3038      This problem could be avoided, but the benefit is
3039      unclear.  */
3040   if (this_cu->cu != NULL)
3041     free_one_cached_comp_unit (this_cu->cu);
3042
3043   /* Note that this is a pointer to our stack frame, being
3044      added to a global data structure.  It will be cleaned up
3045      in free_stack_comp_unit when we finish with this
3046      compilation unit.  */
3047   this_cu->cu = &cu;
3048   cu.per_cu = this_cu;
3049
3050   /* Read the abbrevs for this compilation unit into a table.  */
3051   dwarf2_read_abbrevs (abfd, &cu);
3052   make_cleanup (dwarf2_free_abbrev_table, &cu);
3053
3054   /* Read the compilation unit die.  */
3055   if (this_cu->from_debug_types)
3056     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3057   init_cu_die_reader (&reader_specs, &cu);
3058   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3059                             &has_children);
3060
3061   if (this_cu->from_debug_types)
3062     {
3063       /* offset,length haven't been set yet for type units.  */
3064       this_cu->offset = cu.header.offset;
3065       this_cu->length = cu.header.length + cu.header.initial_length_size;
3066     }
3067   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3068     {
3069       info_ptr = (beg_of_comp_unit + cu.header.length
3070                   + cu.header.initial_length_size);
3071       do_cleanups (back_to_inner);
3072       return info_ptr;
3073     }
3074
3075   /* Set the language we're debugging.  */
3076   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3077   if (attr)
3078     set_cu_language (DW_UNSND (attr), &cu);
3079   else
3080     set_cu_language (language_minimal, &cu);
3081
3082   /* Allocate a new partial symbol table structure.  */
3083   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3084   pst = start_psymtab_common (objfile, objfile->section_offsets,
3085                               (attr != NULL) ? DW_STRING (attr) : "",
3086                               /* TEXTLOW and TEXTHIGH are set below.  */
3087                               0,
3088                               objfile->global_psymbols.next,
3089                               objfile->static_psymbols.next);
3090
3091   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3092   if (attr != NULL)
3093     pst->dirname = DW_STRING (attr);
3094
3095   pst->read_symtab_private = this_cu;
3096
3097   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3098
3099   /* Store the function that reads in the rest of the symbol table */
3100   pst->read_symtab = dwarf2_psymtab_to_symtab;
3101
3102   this_cu->v.psymtab = pst;
3103
3104   dwarf2_find_base_address (comp_unit_die, &cu);
3105
3106   /* Possibly set the default values of LOWPC and HIGHPC from
3107      `DW_AT_ranges'.  */
3108   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3109                                       &best_highpc, &cu, pst);
3110   if (has_pc_info == 1 && best_lowpc < best_highpc)
3111     /* Store the contiguous range if it is not empty; it can be empty for
3112        CUs with no code.  */
3113     addrmap_set_empty (objfile->psymtabs_addrmap,
3114                        best_lowpc + baseaddr,
3115                        best_highpc + baseaddr - 1, pst);
3116
3117   /* Check if comp unit has_children.
3118      If so, read the rest of the partial symbols from this comp unit.
3119      If not, there's no more debug_info for this comp unit. */
3120   if (has_children)
3121     {
3122       struct partial_die_info *first_die;
3123       CORE_ADDR lowpc, highpc;
3124
3125       lowpc = ((CORE_ADDR) -1);
3126       highpc = ((CORE_ADDR) 0);
3127
3128       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3129
3130       scan_partial_symbols (first_die, &lowpc, &highpc,
3131                             ! has_pc_info, &cu);
3132
3133       /* If we didn't find a lowpc, set it to highpc to avoid
3134          complaints from `maint check'.  */
3135       if (lowpc == ((CORE_ADDR) -1))
3136         lowpc = highpc;
3137
3138       /* If the compilation unit didn't have an explicit address range,
3139          then use the information extracted from its child dies.  */
3140       if (! has_pc_info)
3141         {
3142           best_lowpc = lowpc;
3143           best_highpc = highpc;
3144         }
3145     }
3146   pst->textlow = best_lowpc + baseaddr;
3147   pst->texthigh = best_highpc + baseaddr;
3148
3149   pst->n_global_syms = objfile->global_psymbols.next -
3150     (objfile->global_psymbols.list + pst->globals_offset);
3151   pst->n_static_syms = objfile->static_psymbols.next -
3152     (objfile->static_psymbols.list + pst->statics_offset);
3153   sort_pst_symbols (pst);
3154
3155   info_ptr = (beg_of_comp_unit + cu.header.length
3156               + cu.header.initial_length_size);
3157
3158   if (this_cu->from_debug_types)
3159     {
3160       /* It's not clear we want to do anything with stmt lists here.
3161          Waiting to see what gcc ultimately does.  */
3162     }
3163   else
3164     {
3165       /* Get the list of files included in the current compilation unit,
3166          and build a psymtab for each of them.  */
3167       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3168     }
3169
3170   do_cleanups (back_to_inner);
3171
3172   return info_ptr;
3173 }
3174
3175 /* Traversal function for htab_traverse_noresize.
3176    Process one .debug_types comp-unit.  */
3177
3178 static int
3179 process_type_comp_unit (void **slot, void *info)
3180 {
3181   struct signatured_type *entry = (struct signatured_type *) *slot;
3182   struct objfile *objfile = (struct objfile *) info;
3183   struct dwarf2_per_cu_data *this_cu;
3184
3185   this_cu = &entry->per_cu;
3186
3187   gdb_assert (dwarf2_per_objfile->types.readin);
3188   process_psymtab_comp_unit (objfile, this_cu,
3189                              dwarf2_per_objfile->types.buffer,
3190                              dwarf2_per_objfile->types.buffer + entry->offset,
3191                              dwarf2_per_objfile->types.size);
3192
3193   return 1;
3194 }
3195
3196 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3197    Build partial symbol tables for the .debug_types comp-units.  */
3198
3199 static void
3200 build_type_psymtabs (struct objfile *objfile)
3201 {
3202   if (! create_debug_types_hash_table (objfile))
3203     return;
3204
3205   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3206                           process_type_comp_unit, objfile);
3207 }
3208
3209 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3210
3211 static void
3212 psymtabs_addrmap_cleanup (void *o)
3213 {
3214   struct objfile *objfile = o;
3215
3216   objfile->psymtabs_addrmap = NULL;
3217 }
3218
3219 /* Build the partial symbol table by doing a quick pass through the
3220    .debug_info and .debug_abbrev sections.  */
3221
3222 static void
3223 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3224 {
3225   gdb_byte *info_ptr;
3226   struct cleanup *back_to, *addrmap_cleanup;
3227   struct obstack temp_obstack;
3228
3229   dwarf2_per_objfile->reading_partial_symbols = 1;
3230
3231   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3232   info_ptr = dwarf2_per_objfile->info.buffer;
3233
3234   /* Any cached compilation units will be linked by the per-objfile
3235      read_in_chain.  Make sure to free them when we're done.  */
3236   back_to = make_cleanup (free_cached_comp_units, NULL);
3237
3238   build_type_psymtabs (objfile);
3239
3240   create_all_comp_units (objfile);
3241
3242   /* Create a temporary address map on a temporary obstack.  We later
3243      copy this to the final obstack.  */
3244   obstack_init (&temp_obstack);
3245   make_cleanup_obstack_free (&temp_obstack);
3246   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3247   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3248
3249   /* Since the objects we're extracting from .debug_info vary in
3250      length, only the individual functions to extract them (like
3251      read_comp_unit_head and load_partial_die) can really know whether
3252      the buffer is large enough to hold another complete object.
3253
3254      At the moment, they don't actually check that.  If .debug_info
3255      holds just one extra byte after the last compilation unit's dies,
3256      then read_comp_unit_head will happily read off the end of the
3257      buffer.  read_partial_die is similarly casual.  Those functions
3258      should be fixed.
3259
3260      For this loop condition, simply checking whether there's any data
3261      left at all should be sufficient.  */
3262
3263   while (info_ptr < (dwarf2_per_objfile->info.buffer
3264                      + dwarf2_per_objfile->info.size))
3265     {
3266       struct dwarf2_per_cu_data *this_cu;
3267
3268       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3269                                        objfile);
3270
3271       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3272                                             dwarf2_per_objfile->info.buffer,
3273                                             info_ptr,
3274                                             dwarf2_per_objfile->info.size);
3275     }
3276
3277   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3278                                                     &objfile->objfile_obstack);
3279   discard_cleanups (addrmap_cleanup);
3280
3281   do_cleanups (back_to);
3282 }
3283
3284 /* Load the partial DIEs for a secondary CU into memory.  */
3285
3286 static void
3287 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3288                         struct objfile *objfile)
3289 {
3290   bfd *abfd = objfile->obfd;
3291   gdb_byte *info_ptr, *beg_of_comp_unit;
3292   struct die_info *comp_unit_die;
3293   struct dwarf2_cu *cu;
3294   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3295   struct attribute *attr;
3296   int has_children;
3297   struct die_reader_specs reader_specs;
3298   int read_cu = 0;
3299
3300   gdb_assert (! this_cu->from_debug_types);
3301
3302   gdb_assert (dwarf2_per_objfile->info.readin);
3303   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3304   beg_of_comp_unit = info_ptr;
3305
3306   if (this_cu->cu == NULL)
3307     {
3308       cu = alloc_one_comp_unit (objfile);
3309
3310       read_cu = 1;
3311
3312       /* If an error occurs while loading, release our storage.  */
3313       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3314
3315       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3316                                               dwarf2_per_objfile->info.buffer,
3317                                               dwarf2_per_objfile->info.size,
3318                                               abfd);
3319
3320       /* Complete the cu_header.  */
3321       cu->header.offset = this_cu->offset;
3322       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3323
3324       /* Link this compilation unit into the compilation unit tree.  */
3325       this_cu->cu = cu;
3326       cu->per_cu = this_cu;
3327
3328       /* Link this CU into read_in_chain.  */
3329       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3330       dwarf2_per_objfile->read_in_chain = this_cu;
3331     }
3332   else
3333     {
3334       cu = this_cu->cu;
3335       info_ptr += cu->header.first_die_offset;
3336     }
3337
3338   /* Read the abbrevs for this compilation unit into a table.  */
3339   gdb_assert (cu->dwarf2_abbrevs == NULL);
3340   dwarf2_read_abbrevs (abfd, cu);
3341   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3342
3343   /* Read the compilation unit die.  */
3344   init_cu_die_reader (&reader_specs, cu);
3345   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3346                             &has_children);
3347
3348   /* Set the language we're debugging.  */
3349   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3350   if (attr)
3351     set_cu_language (DW_UNSND (attr), cu);
3352   else
3353     set_cu_language (language_minimal, cu);
3354
3355   /* Check if comp unit has_children.
3356      If so, read the rest of the partial symbols from this comp unit.
3357      If not, there's no more debug_info for this comp unit. */
3358   if (has_children)
3359     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3360
3361   do_cleanups (free_abbrevs_cleanup);
3362
3363   if (read_cu)
3364     {
3365       /* We've successfully allocated this compilation unit.  Let our
3366          caller clean it up when finished with it.  */
3367       discard_cleanups (free_cu_cleanup);
3368     }
3369 }
3370
3371 /* Create a list of all compilation units in OBJFILE.  We do this only
3372    if an inter-comp-unit reference is found; presumably if there is one,
3373    there will be many, and one will occur early in the .debug_info section.
3374    So there's no point in building this list incrementally.  */
3375
3376 static void
3377 create_all_comp_units (struct objfile *objfile)
3378 {
3379   int n_allocated;
3380   int n_comp_units;
3381   struct dwarf2_per_cu_data **all_comp_units;
3382   gdb_byte *info_ptr;
3383
3384   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3385   info_ptr = dwarf2_per_objfile->info.buffer;
3386
3387   n_comp_units = 0;
3388   n_allocated = 10;
3389   all_comp_units = xmalloc (n_allocated
3390                             * sizeof (struct dwarf2_per_cu_data *));
3391
3392   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3393     {
3394       unsigned int length, initial_length_size;
3395       struct dwarf2_per_cu_data *this_cu;
3396       unsigned int offset;
3397
3398       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3399
3400       /* Read just enough information to find out where the next
3401          compilation unit is.  */
3402       length = read_initial_length (objfile->obfd, info_ptr,
3403                                     &initial_length_size);
3404
3405       /* Save the compilation unit for later lookup.  */
3406       this_cu = obstack_alloc (&objfile->objfile_obstack,
3407                                sizeof (struct dwarf2_per_cu_data));
3408       memset (this_cu, 0, sizeof (*this_cu));
3409       this_cu->offset = offset;
3410       this_cu->length = length + initial_length_size;
3411       this_cu->objfile = objfile;
3412
3413       if (n_comp_units == n_allocated)
3414         {
3415           n_allocated *= 2;
3416           all_comp_units = xrealloc (all_comp_units,
3417                                      n_allocated
3418                                      * sizeof (struct dwarf2_per_cu_data *));
3419         }
3420       all_comp_units[n_comp_units++] = this_cu;
3421
3422       info_ptr = info_ptr + this_cu->length;
3423     }
3424
3425   dwarf2_per_objfile->all_comp_units
3426     = obstack_alloc (&objfile->objfile_obstack,
3427                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3428   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3429           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3430   xfree (all_comp_units);
3431   dwarf2_per_objfile->n_comp_units = n_comp_units;
3432 }
3433
3434 /* Process all loaded DIEs for compilation unit CU, starting at
3435    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3436    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3437    DW_AT_ranges).  If NEED_PC is set, then this function will set
3438    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3439    and record the covered ranges in the addrmap.  */
3440
3441 static void
3442 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3443                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3444 {
3445   struct partial_die_info *pdi;
3446
3447   /* Now, march along the PDI's, descending into ones which have
3448      interesting children but skipping the children of the other ones,
3449      until we reach the end of the compilation unit.  */
3450
3451   pdi = first_die;
3452
3453   while (pdi != NULL)
3454     {
3455       fixup_partial_die (pdi, cu);
3456
3457       /* Anonymous namespaces or modules have no name but have interesting
3458          children, so we need to look at them.  Ditto for anonymous
3459          enums.  */
3460
3461       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3462           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3463         {
3464           switch (pdi->tag)
3465             {
3466             case DW_TAG_subprogram:
3467               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3468               break;
3469             case DW_TAG_constant:
3470             case DW_TAG_variable:
3471             case DW_TAG_typedef:
3472             case DW_TAG_union_type:
3473               if (!pdi->is_declaration)
3474                 {
3475                   add_partial_symbol (pdi, cu);
3476                 }
3477               break;
3478             case DW_TAG_class_type:
3479             case DW_TAG_interface_type:
3480             case DW_TAG_structure_type:
3481               if (!pdi->is_declaration)
3482                 {
3483                   add_partial_symbol (pdi, cu);
3484                 }
3485               break;
3486             case DW_TAG_enumeration_type:
3487               if (!pdi->is_declaration)
3488                 add_partial_enumeration (pdi, cu);
3489               break;
3490             case DW_TAG_base_type:
3491             case DW_TAG_subrange_type:
3492               /* File scope base type definitions are added to the partial
3493                  symbol table.  */
3494               add_partial_symbol (pdi, cu);
3495               break;
3496             case DW_TAG_namespace:
3497               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3498               break;
3499             case DW_TAG_module:
3500               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3501               break;
3502             default:
3503               break;
3504             }
3505         }
3506
3507       /* If the die has a sibling, skip to the sibling.  */
3508
3509       pdi = pdi->die_sibling;
3510     }
3511 }
3512
3513 /* Functions used to compute the fully scoped name of a partial DIE.
3514
3515    Normally, this is simple.  For C++, the parent DIE's fully scoped
3516    name is concatenated with "::" and the partial DIE's name.  For
3517    Java, the same thing occurs except that "." is used instead of "::".
3518    Enumerators are an exception; they use the scope of their parent
3519    enumeration type, i.e. the name of the enumeration type is not
3520    prepended to the enumerator.
3521
3522    There are two complexities.  One is DW_AT_specification; in this
3523    case "parent" means the parent of the target of the specification,
3524    instead of the direct parent of the DIE.  The other is compilers
3525    which do not emit DW_TAG_namespace; in this case we try to guess
3526    the fully qualified name of structure types from their members'
3527    linkage names.  This must be done using the DIE's children rather
3528    than the children of any DW_AT_specification target.  We only need
3529    to do this for structures at the top level, i.e. if the target of
3530    any DW_AT_specification (if any; otherwise the DIE itself) does not
3531    have a parent.  */
3532
3533 /* Compute the scope prefix associated with PDI's parent, in
3534    compilation unit CU.  The result will be allocated on CU's
3535    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3536    field.  NULL is returned if no prefix is necessary.  */
3537 static char *
3538 partial_die_parent_scope (struct partial_die_info *pdi,
3539                           struct dwarf2_cu *cu)
3540 {
3541   char *grandparent_scope;
3542   struct partial_die_info *parent, *real_pdi;
3543
3544   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3545      then this means the parent of the specification DIE.  */
3546
3547   real_pdi = pdi;
3548   while (real_pdi->has_specification)
3549     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3550
3551   parent = real_pdi->die_parent;
3552   if (parent == NULL)
3553     return NULL;
3554
3555   if (parent->scope_set)
3556     return parent->scope;
3557
3558   fixup_partial_die (parent, cu);
3559
3560   grandparent_scope = partial_die_parent_scope (parent, cu);
3561
3562   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3563      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3564      Work around this problem here.  */
3565   if (cu->language == language_cplus
3566       && parent->tag == DW_TAG_namespace
3567       && strcmp (parent->name, "::") == 0
3568       && grandparent_scope == NULL)
3569     {
3570       parent->scope = NULL;
3571       parent->scope_set = 1;
3572       return NULL;
3573     }
3574
3575   if (parent->tag == DW_TAG_namespace
3576       || parent->tag == DW_TAG_module
3577       || parent->tag == DW_TAG_structure_type
3578       || parent->tag == DW_TAG_class_type
3579       || parent->tag == DW_TAG_interface_type
3580       || parent->tag == DW_TAG_union_type
3581       || parent->tag == DW_TAG_enumeration_type)
3582     {
3583       if (grandparent_scope == NULL)
3584         parent->scope = parent->name;
3585       else
3586         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3587                                          parent->name, 0, cu);
3588     }
3589   else if (parent->tag == DW_TAG_enumerator)
3590     /* Enumerators should not get the name of the enumeration as a prefix.  */
3591     parent->scope = grandparent_scope;
3592   else
3593     {
3594       /* FIXME drow/2004-04-01: What should we be doing with
3595          function-local names?  For partial symbols, we should probably be
3596          ignoring them.  */
3597       complaint (&symfile_complaints,
3598                  _("unhandled containing DIE tag %d for DIE at %d"),
3599                  parent->tag, pdi->offset);
3600       parent->scope = grandparent_scope;
3601     }
3602
3603   parent->scope_set = 1;
3604   return parent->scope;
3605 }
3606
3607 /* Return the fully scoped name associated with PDI, from compilation unit
3608    CU.  The result will be allocated with malloc.  */
3609 static char *
3610 partial_die_full_name (struct partial_die_info *pdi,
3611                        struct dwarf2_cu *cu)
3612 {
3613   char *parent_scope;
3614
3615   /* If this is a template instantiation, we can not work out the
3616      template arguments from partial DIEs.  So, unfortunately, we have
3617      to go through the full DIEs.  At least any work we do building
3618      types here will be reused if full symbols are loaded later.  */
3619   if (pdi->has_template_arguments)
3620     {
3621       fixup_partial_die (pdi, cu);
3622
3623       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3624         {
3625           struct die_info *die;
3626           struct attribute attr;
3627           struct dwarf2_cu *ref_cu = cu;
3628
3629           attr.name = 0;
3630           attr.form = DW_FORM_ref_addr;
3631           attr.u.addr = pdi->offset;
3632           die = follow_die_ref (NULL, &attr, &ref_cu);
3633
3634           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3635         }
3636     }
3637
3638   parent_scope = partial_die_parent_scope (pdi, cu);
3639   if (parent_scope == NULL)
3640     return NULL;
3641   else
3642     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3643 }
3644
3645 static void
3646 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3647 {
3648   struct objfile *objfile = cu->objfile;
3649   CORE_ADDR addr = 0;
3650   char *actual_name = NULL;
3651   const struct partial_symbol *psym = NULL;
3652   CORE_ADDR baseaddr;
3653   int built_actual_name = 0;
3654
3655   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3656
3657   actual_name = partial_die_full_name (pdi, cu);
3658   if (actual_name)
3659     built_actual_name = 1;
3660
3661   if (actual_name == NULL)
3662     actual_name = pdi->name;
3663
3664   switch (pdi->tag)
3665     {
3666     case DW_TAG_subprogram:
3667       if (pdi->is_external || cu->language == language_ada)
3668         {
3669           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3670              of the global scope.  But in Ada, we want to be able to access
3671              nested procedures globally.  So all Ada subprograms are stored
3672              in the global scope.  */
3673           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3674              mst_text, objfile); */
3675           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3676                                       built_actual_name,
3677                                       VAR_DOMAIN, LOC_BLOCK,
3678                                       &objfile->global_psymbols,
3679                                       0, pdi->lowpc + baseaddr,
3680                                       cu->language, objfile);
3681         }
3682       else
3683         {
3684           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3685              mst_file_text, objfile); */
3686           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3687                                       built_actual_name,
3688                                       VAR_DOMAIN, LOC_BLOCK,
3689                                       &objfile->static_psymbols,
3690                                       0, pdi->lowpc + baseaddr,
3691                                       cu->language, objfile);
3692         }
3693       break;
3694     case DW_TAG_constant:
3695       {
3696         struct psymbol_allocation_list *list;
3697
3698         if (pdi->is_external)
3699           list = &objfile->global_psymbols;
3700         else
3701           list = &objfile->static_psymbols;
3702         psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3703                                     built_actual_name, VAR_DOMAIN, LOC_STATIC,
3704                                     list, 0, 0, cu->language, objfile);
3705
3706       }
3707       break;
3708     case DW_TAG_variable:
3709       if (pdi->locdesc)
3710         addr = decode_locdesc (pdi->locdesc, cu);
3711
3712       if (pdi->locdesc
3713           && addr == 0
3714           && !dwarf2_per_objfile->has_section_at_zero)
3715         {
3716           /* A global or static variable may also have been stripped
3717              out by the linker if unused, in which case its address
3718              will be nullified; do not add such variables into partial
3719              symbol table then.  */
3720         }
3721       else if (pdi->is_external)
3722         {
3723           /* Global Variable.
3724              Don't enter into the minimal symbol tables as there is
3725              a minimal symbol table entry from the ELF symbols already.
3726              Enter into partial symbol table if it has a location
3727              descriptor or a type.
3728              If the location descriptor is missing, new_symbol will create
3729              a LOC_UNRESOLVED symbol, the address of the variable will then
3730              be determined from the minimal symbol table whenever the variable
3731              is referenced.
3732              The address for the partial symbol table entry is not
3733              used by GDB, but it comes in handy for debugging partial symbol
3734              table building.  */
3735
3736           if (pdi->locdesc || pdi->has_type)
3737             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3738                                         built_actual_name,
3739                                         VAR_DOMAIN, LOC_STATIC,
3740                                         &objfile->global_psymbols,
3741                                         0, addr + baseaddr,
3742                                         cu->language, objfile);
3743         }
3744       else
3745         {
3746           /* Static Variable. Skip symbols without location descriptors.  */
3747           if (pdi->locdesc == NULL)
3748             {
3749               if (built_actual_name)
3750                 xfree (actual_name);
3751               return;
3752             }
3753           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3754              mst_file_data, objfile); */
3755           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3756                                       built_actual_name,
3757                                       VAR_DOMAIN, LOC_STATIC,
3758                                       &objfile->static_psymbols,
3759                                       0, addr + baseaddr,
3760                                       cu->language, objfile);
3761         }
3762       break;
3763     case DW_TAG_typedef:
3764     case DW_TAG_base_type:
3765     case DW_TAG_subrange_type:
3766       add_psymbol_to_list (actual_name, strlen (actual_name),
3767                            built_actual_name,
3768                            VAR_DOMAIN, LOC_TYPEDEF,
3769                            &objfile->static_psymbols,
3770                            0, (CORE_ADDR) 0, cu->language, objfile);
3771       break;
3772     case DW_TAG_namespace:
3773       add_psymbol_to_list (actual_name, strlen (actual_name),
3774                            built_actual_name,
3775                            VAR_DOMAIN, LOC_TYPEDEF,
3776                            &objfile->global_psymbols,
3777                            0, (CORE_ADDR) 0, cu->language, objfile);
3778       break;
3779     case DW_TAG_class_type:
3780     case DW_TAG_interface_type:
3781     case DW_TAG_structure_type:
3782     case DW_TAG_union_type:
3783     case DW_TAG_enumeration_type:
3784       /* Skip external references.  The DWARF standard says in the section
3785          about "Structure, Union, and Class Type Entries": "An incomplete
3786          structure, union or class type is represented by a structure,
3787          union or class entry that does not have a byte size attribute
3788          and that has a DW_AT_declaration attribute."  */
3789       if (!pdi->has_byte_size && pdi->is_declaration)
3790         {
3791           if (built_actual_name)
3792             xfree (actual_name);
3793           return;
3794         }
3795
3796       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3797          static vs. global.  */
3798       add_psymbol_to_list (actual_name, strlen (actual_name),
3799                            built_actual_name,
3800                            STRUCT_DOMAIN, LOC_TYPEDEF,
3801                            (cu->language == language_cplus
3802                             || cu->language == language_java)
3803                            ? &objfile->global_psymbols
3804                            : &objfile->static_psymbols,
3805                            0, (CORE_ADDR) 0, cu->language, objfile);
3806
3807       break;
3808     case DW_TAG_enumerator:
3809       add_psymbol_to_list (actual_name, strlen (actual_name),
3810                            built_actual_name,
3811                            VAR_DOMAIN, LOC_CONST,
3812                            (cu->language == language_cplus
3813                             || cu->language == language_java)
3814                            ? &objfile->global_psymbols
3815                            : &objfile->static_psymbols,
3816                            0, (CORE_ADDR) 0, cu->language, objfile);
3817       break;
3818     default:
3819       break;
3820     }
3821
3822   if (built_actual_name)
3823     xfree (actual_name);
3824 }
3825
3826 /* Read a partial die corresponding to a namespace; also, add a symbol
3827    corresponding to that namespace to the symbol table.  NAMESPACE is
3828    the name of the enclosing namespace.  */
3829
3830 static void
3831 add_partial_namespace (struct partial_die_info *pdi,
3832                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3833                        int need_pc, struct dwarf2_cu *cu)
3834 {
3835   /* Add a symbol for the namespace.  */
3836
3837   add_partial_symbol (pdi, cu);
3838
3839   /* Now scan partial symbols in that namespace.  */
3840
3841   if (pdi->has_children)
3842     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3843 }
3844
3845 /* Read a partial die corresponding to a Fortran module.  */
3846
3847 static void
3848 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3849                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3850 {
3851   /* Now scan partial symbols in that module.  */
3852
3853   if (pdi->has_children)
3854     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3855 }
3856
3857 /* Read a partial die corresponding to a subprogram and create a partial
3858    symbol for that subprogram.  When the CU language allows it, this
3859    routine also defines a partial symbol for each nested subprogram
3860    that this subprogram contains.
3861
3862    DIE my also be a lexical block, in which case we simply search
3863    recursively for suprograms defined inside that lexical block.
3864    Again, this is only performed when the CU language allows this
3865    type of definitions.  */
3866
3867 static void
3868 add_partial_subprogram (struct partial_die_info *pdi,
3869                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3870                         int need_pc, struct dwarf2_cu *cu)
3871 {
3872   if (pdi->tag == DW_TAG_subprogram)
3873     {
3874       if (pdi->has_pc_info)
3875         {
3876           if (pdi->lowpc < *lowpc)
3877             *lowpc = pdi->lowpc;
3878           if (pdi->highpc > *highpc)
3879             *highpc = pdi->highpc;
3880           if (need_pc)
3881             {
3882               CORE_ADDR baseaddr;
3883               struct objfile *objfile = cu->objfile;
3884
3885               baseaddr = ANOFFSET (objfile->section_offsets,
3886                                    SECT_OFF_TEXT (objfile));
3887               addrmap_set_empty (objfile->psymtabs_addrmap,
3888                                  pdi->lowpc + baseaddr,
3889                                  pdi->highpc - 1 + baseaddr,
3890                                  cu->per_cu->v.psymtab);
3891             }
3892           if (!pdi->is_declaration)
3893             /* Ignore subprogram DIEs that do not have a name, they are
3894                illegal.  Do not emit a complaint at this point, we will
3895                do so when we convert this psymtab into a symtab.  */
3896             if (pdi->name)
3897               add_partial_symbol (pdi, cu);
3898         }
3899     }
3900
3901   if (! pdi->has_children)
3902     return;
3903
3904   if (cu->language == language_ada)
3905     {
3906       pdi = pdi->die_child;
3907       while (pdi != NULL)
3908         {
3909           fixup_partial_die (pdi, cu);
3910           if (pdi->tag == DW_TAG_subprogram
3911               || pdi->tag == DW_TAG_lexical_block)
3912             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3913           pdi = pdi->die_sibling;
3914         }
3915     }
3916 }
3917
3918 /* See if we can figure out if the class lives in a namespace.  We do
3919    this by looking for a member function; its demangled name will
3920    contain namespace info, if there is any.  */
3921
3922 static void
3923 guess_structure_name (struct partial_die_info *struct_pdi,
3924                       struct dwarf2_cu *cu)
3925 {
3926   if ((cu->language == language_cplus
3927        || cu->language == language_java)
3928       && cu->has_namespace_info == 0
3929       && struct_pdi->has_children)
3930     {
3931       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3932          what template types look like, because the demangler
3933          frequently doesn't give the same name as the debug info.  We
3934          could fix this by only using the demangled name to get the
3935          prefix (but see comment in read_structure_type).  */
3936
3937       struct partial_die_info *real_pdi;
3938
3939       /* If this DIE (this DIE's specification, if any) has a parent, then
3940          we should not do this.  We'll prepend the parent's fully qualified
3941          name when we create the partial symbol.  */
3942
3943       real_pdi = struct_pdi;
3944       while (real_pdi->has_specification)
3945         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3946
3947       if (real_pdi->die_parent != NULL)
3948         return;
3949     }
3950 }
3951
3952 /* Read a partial die corresponding to an enumeration type.  */
3953
3954 static void
3955 add_partial_enumeration (struct partial_die_info *enum_pdi,
3956                          struct dwarf2_cu *cu)
3957 {
3958   struct partial_die_info *pdi;
3959
3960   if (enum_pdi->name != NULL)
3961     add_partial_symbol (enum_pdi, cu);
3962
3963   pdi = enum_pdi->die_child;
3964   while (pdi)
3965     {
3966       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3967         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3968       else
3969         add_partial_symbol (pdi, cu);
3970       pdi = pdi->die_sibling;
3971     }
3972 }
3973
3974 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3975    Return the corresponding abbrev, or NULL if the number is zero (indicating
3976    an empty DIE).  In either case *BYTES_READ will be set to the length of
3977    the initial number.  */
3978
3979 static struct abbrev_info *
3980 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3981                  struct dwarf2_cu *cu)
3982 {
3983   bfd *abfd = cu->objfile->obfd;
3984   unsigned int abbrev_number;
3985   struct abbrev_info *abbrev;
3986
3987   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3988
3989   if (abbrev_number == 0)
3990     return NULL;
3991
3992   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3993   if (!abbrev)
3994     {
3995       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3996                       bfd_get_filename (abfd));
3997     }
3998
3999   return abbrev;
4000 }
4001
4002 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4003    Returns a pointer to the end of a series of DIEs, terminated by an empty
4004    DIE.  Any children of the skipped DIEs will also be skipped.  */
4005
4006 static gdb_byte *
4007 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4008 {
4009   struct abbrev_info *abbrev;
4010   unsigned int bytes_read;
4011
4012   while (1)
4013     {
4014       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4015       if (abbrev == NULL)
4016         return info_ptr + bytes_read;
4017       else
4018         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4019     }
4020 }
4021
4022 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4023    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4024    abbrev corresponding to that skipped uleb128 should be passed in
4025    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4026    children.  */
4027
4028 static gdb_byte *
4029 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4030               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4031 {
4032   unsigned int bytes_read;
4033   struct attribute attr;
4034   bfd *abfd = cu->objfile->obfd;
4035   unsigned int form, i;
4036
4037   for (i = 0; i < abbrev->num_attrs; i++)
4038     {
4039       /* The only abbrev we care about is DW_AT_sibling.  */
4040       if (abbrev->attrs[i].name == DW_AT_sibling)
4041         {
4042           read_attribute (&attr, &abbrev->attrs[i],
4043                           abfd, info_ptr, cu);
4044           if (attr.form == DW_FORM_ref_addr)
4045             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4046           else
4047             return buffer + dwarf2_get_ref_die_offset (&attr);
4048         }
4049
4050       /* If it isn't DW_AT_sibling, skip this attribute.  */
4051       form = abbrev->attrs[i].form;
4052     skip_attribute:
4053       switch (form)
4054         {
4055         case DW_FORM_ref_addr:
4056           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4057              and later it is offset sized.  */
4058           if (cu->header.version == 2)
4059             info_ptr += cu->header.addr_size;
4060           else
4061             info_ptr += cu->header.offset_size;
4062           break;
4063         case DW_FORM_addr:
4064           info_ptr += cu->header.addr_size;
4065           break;
4066         case DW_FORM_data1:
4067         case DW_FORM_ref1:
4068         case DW_FORM_flag:
4069           info_ptr += 1;
4070           break;
4071         case DW_FORM_flag_present:
4072           break;
4073         case DW_FORM_data2:
4074         case DW_FORM_ref2:
4075           info_ptr += 2;
4076           break;
4077         case DW_FORM_data4:
4078         case DW_FORM_ref4:
4079           info_ptr += 4;
4080           break;
4081         case DW_FORM_data8:
4082         case DW_FORM_ref8:
4083         case DW_FORM_sig8:
4084           info_ptr += 8;
4085           break;
4086         case DW_FORM_string:
4087           read_direct_string (abfd, info_ptr, &bytes_read);
4088           info_ptr += bytes_read;
4089           break;
4090         case DW_FORM_sec_offset:
4091         case DW_FORM_strp:
4092           info_ptr += cu->header.offset_size;
4093           break;
4094         case DW_FORM_exprloc:
4095         case DW_FORM_block:
4096           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4097           info_ptr += bytes_read;
4098           break;
4099         case DW_FORM_block1:
4100           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4101           break;
4102         case DW_FORM_block2:
4103           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4104           break;
4105         case DW_FORM_block4:
4106           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4107           break;
4108         case DW_FORM_sdata:
4109         case DW_FORM_udata:
4110         case DW_FORM_ref_udata:
4111           info_ptr = skip_leb128 (abfd, info_ptr);
4112           break;
4113         case DW_FORM_indirect:
4114           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4115           info_ptr += bytes_read;
4116           /* We need to continue parsing from here, so just go back to
4117              the top.  */
4118           goto skip_attribute;
4119
4120         default:
4121           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4122                  dwarf_form_name (form),
4123                  bfd_get_filename (abfd));
4124         }
4125     }
4126
4127   if (abbrev->has_children)
4128     return skip_children (buffer, info_ptr, cu);
4129   else
4130     return info_ptr;
4131 }
4132
4133 /* Locate ORIG_PDI's sibling.
4134    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4135    in BUFFER.  */
4136
4137 static gdb_byte *
4138 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4139                     gdb_byte *buffer, gdb_byte *info_ptr,
4140                     bfd *abfd, struct dwarf2_cu *cu)
4141 {
4142   /* Do we know the sibling already?  */
4143
4144   if (orig_pdi->sibling)
4145     return orig_pdi->sibling;
4146
4147   /* Are there any children to deal with?  */
4148
4149   if (!orig_pdi->has_children)
4150     return info_ptr;
4151
4152   /* Skip the children the long way.  */
4153
4154   return skip_children (buffer, info_ptr, cu);
4155 }
4156
4157 /* Expand this partial symbol table into a full symbol table.  */
4158
4159 static void
4160 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4161 {
4162   if (pst != NULL)
4163     {
4164       if (pst->readin)
4165         {
4166           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4167         }
4168       else
4169         {
4170           if (info_verbose)
4171             {
4172               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4173               gdb_flush (gdb_stdout);
4174             }
4175
4176           /* Restore our global data.  */
4177           dwarf2_per_objfile = objfile_data (pst->objfile,
4178                                              dwarf2_objfile_data_key);
4179
4180           /* If this psymtab is constructed from a debug-only objfile, the
4181              has_section_at_zero flag will not necessarily be correct.  We
4182              can get the correct value for this flag by looking at the data
4183              associated with the (presumably stripped) associated objfile.  */
4184           if (pst->objfile->separate_debug_objfile_backlink)
4185             {
4186               struct dwarf2_per_objfile *dpo_backlink
4187                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4188                                 dwarf2_objfile_data_key);
4189
4190               dwarf2_per_objfile->has_section_at_zero
4191                 = dpo_backlink->has_section_at_zero;
4192             }
4193
4194           dwarf2_per_objfile->reading_partial_symbols = 0;
4195
4196           psymtab_to_symtab_1 (pst);
4197
4198           /* Finish up the debug error message.  */
4199           if (info_verbose)
4200             printf_filtered (_("done.\n"));
4201         }
4202     }
4203 }
4204
4205 /* Add PER_CU to the queue.  */
4206
4207 static void
4208 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4209 {
4210   struct dwarf2_queue_item *item;
4211
4212   per_cu->queued = 1;
4213   item = xmalloc (sizeof (*item));
4214   item->per_cu = per_cu;
4215   item->next = NULL;
4216
4217   if (dwarf2_queue == NULL)
4218     dwarf2_queue = item;
4219   else
4220     dwarf2_queue_tail->next = item;
4221
4222   dwarf2_queue_tail = item;
4223 }
4224
4225 /* Process the queue.  */
4226
4227 static void
4228 process_queue (struct objfile *objfile)
4229 {
4230   struct dwarf2_queue_item *item, *next_item;
4231
4232   /* The queue starts out with one item, but following a DIE reference
4233      may load a new CU, adding it to the end of the queue.  */
4234   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4235     {
4236       if (dwarf2_per_objfile->using_index
4237           ? !item->per_cu->v.quick->symtab
4238           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4239         process_full_comp_unit (item->per_cu);
4240
4241       item->per_cu->queued = 0;
4242       next_item = item->next;
4243       xfree (item);
4244     }
4245
4246   dwarf2_queue_tail = NULL;
4247 }
4248
4249 /* Free all allocated queue entries.  This function only releases anything if
4250    an error was thrown; if the queue was processed then it would have been
4251    freed as we went along.  */
4252
4253 static void
4254 dwarf2_release_queue (void *dummy)
4255 {
4256   struct dwarf2_queue_item *item, *last;
4257
4258   item = dwarf2_queue;
4259   while (item)
4260     {
4261       /* Anything still marked queued is likely to be in an
4262          inconsistent state, so discard it.  */
4263       if (item->per_cu->queued)
4264         {
4265           if (item->per_cu->cu != NULL)
4266             free_one_cached_comp_unit (item->per_cu->cu);
4267           item->per_cu->queued = 0;
4268         }
4269
4270       last = item;
4271       item = item->next;
4272       xfree (last);
4273     }
4274
4275   dwarf2_queue = dwarf2_queue_tail = NULL;
4276 }
4277
4278 /* Read in full symbols for PST, and anything it depends on.  */
4279
4280 static void
4281 psymtab_to_symtab_1 (struct partial_symtab *pst)
4282 {
4283   struct dwarf2_per_cu_data *per_cu;
4284   struct cleanup *back_to;
4285   int i;
4286
4287   for (i = 0; i < pst->number_of_dependencies; i++)
4288     if (!pst->dependencies[i]->readin)
4289       {
4290         /* Inform about additional files that need to be read in.  */
4291         if (info_verbose)
4292           {
4293             /* FIXME: i18n: Need to make this a single string.  */
4294             fputs_filtered (" ", gdb_stdout);
4295             wrap_here ("");
4296             fputs_filtered ("and ", gdb_stdout);
4297             wrap_here ("");
4298             printf_filtered ("%s...", pst->dependencies[i]->filename);
4299             wrap_here ("");     /* Flush output */
4300             gdb_flush (gdb_stdout);
4301           }
4302         psymtab_to_symtab_1 (pst->dependencies[i]);
4303       }
4304
4305   per_cu = pst->read_symtab_private;
4306
4307   if (per_cu == NULL)
4308     {
4309       /* It's an include file, no symbols to read for it.
4310          Everything is in the parent symtab.  */
4311       pst->readin = 1;
4312       return;
4313     }
4314
4315   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4316 }
4317
4318 /* Load the DIEs associated with PER_CU into memory.  */
4319
4320 static void
4321 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4322 {
4323   bfd *abfd = objfile->obfd;
4324   struct dwarf2_cu *cu;
4325   unsigned int offset;
4326   gdb_byte *info_ptr, *beg_of_comp_unit;
4327   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4328   struct attribute *attr;
4329   int read_cu = 0;
4330
4331   gdb_assert (! per_cu->from_debug_types);
4332
4333   /* Set local variables from the partial symbol table info.  */
4334   offset = per_cu->offset;
4335
4336   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4337   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4338   beg_of_comp_unit = info_ptr;
4339
4340   if (per_cu->cu == NULL)
4341     {
4342       cu = alloc_one_comp_unit (objfile);
4343
4344       read_cu = 1;
4345
4346       /* If an error occurs while loading, release our storage.  */
4347       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4348
4349       /* Read in the comp_unit header.  */
4350       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4351
4352       /* Complete the cu_header.  */
4353       cu->header.offset = offset;
4354       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4355
4356       /* Read the abbrevs for this compilation unit.  */
4357       dwarf2_read_abbrevs (abfd, cu);
4358       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4359
4360       /* Link this compilation unit into the compilation unit tree.  */
4361       per_cu->cu = cu;
4362       cu->per_cu = per_cu;
4363
4364       /* Link this CU into read_in_chain.  */
4365       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4366       dwarf2_per_objfile->read_in_chain = per_cu;
4367     }
4368   else
4369     {
4370       cu = per_cu->cu;
4371       info_ptr += cu->header.first_die_offset;
4372     }
4373
4374   cu->dies = read_comp_unit (info_ptr, cu);
4375
4376   /* We try not to read any attributes in this function, because not
4377      all objfiles needed for references have been loaded yet, and symbol
4378      table processing isn't initialized.  But we have to set the CU language,
4379      or we won't be able to build types correctly.  */
4380   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4381   if (attr)
4382     set_cu_language (DW_UNSND (attr), cu);
4383   else
4384     set_cu_language (language_minimal, cu);
4385
4386   /* Similarly, if we do not read the producer, we can not apply
4387      producer-specific interpretation.  */
4388   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4389   if (attr)
4390     cu->producer = DW_STRING (attr);
4391
4392   if (read_cu)
4393     {
4394       do_cleanups (free_abbrevs_cleanup);
4395
4396       /* We've successfully allocated this compilation unit.  Let our
4397          caller clean it up when finished with it.  */
4398       discard_cleanups (free_cu_cleanup);
4399     }
4400 }
4401
4402 /* Add a DIE to the delayed physname list.  */
4403
4404 static void
4405 add_to_method_list (struct type *type, int fnfield_index, int index,
4406                     const char *name, struct die_info *die,
4407                     struct dwarf2_cu *cu)
4408 {
4409   struct delayed_method_info mi;
4410   mi.type = type;
4411   mi.fnfield_index = fnfield_index;
4412   mi.index = index;
4413   mi.name = name;
4414   mi.die = die;
4415   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4416 }
4417
4418 /* A cleanup for freeing the delayed method list.  */
4419
4420 static void
4421 free_delayed_list (void *ptr)
4422 {
4423   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4424   if (cu->method_list != NULL)
4425     {
4426       VEC_free (delayed_method_info, cu->method_list);
4427       cu->method_list = NULL;
4428     }
4429 }
4430
4431 /* Compute the physnames of any methods on the CU's method list.
4432
4433    The computation of method physnames is delayed in order to avoid the
4434    (bad) condition that one of the method's formal parameters is of an as yet
4435    incomplete type.  */
4436
4437 static void
4438 compute_delayed_physnames (struct dwarf2_cu *cu)
4439 {
4440   int i;
4441   struct delayed_method_info *mi;
4442   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4443     {
4444       char *physname;
4445       struct fn_fieldlist *fn_flp
4446         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4447       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4448       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4449     }
4450 }
4451
4452 /* Generate full symbol information for PST and CU, whose DIEs have
4453    already been loaded into memory.  */
4454
4455 static void
4456 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4457 {
4458   struct dwarf2_cu *cu = per_cu->cu;
4459   struct objfile *objfile = per_cu->objfile;
4460   CORE_ADDR lowpc, highpc;
4461   struct symtab *symtab;
4462   struct cleanup *back_to, *delayed_list_cleanup;
4463   CORE_ADDR baseaddr;
4464
4465   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4466
4467   buildsym_init ();
4468   back_to = make_cleanup (really_free_pendings, NULL);
4469   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4470
4471   cu->list_in_scope = &file_symbols;
4472
4473   dwarf2_find_base_address (cu->dies, cu);
4474
4475   /* Do line number decoding in read_file_scope () */
4476   process_die (cu->dies, cu);
4477
4478   /* Now that we have processed all the DIEs in the CU, all the types 
4479      should be complete, and it should now be safe to compute all of the
4480      physnames.  */
4481   compute_delayed_physnames (cu);
4482   do_cleanups (delayed_list_cleanup);
4483
4484   /* Some compilers don't define a DW_AT_high_pc attribute for the
4485      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4486      it, by scanning the DIE's below the compilation unit.  */
4487   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4488
4489   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4490
4491   /* Set symtab language to language from DW_AT_language.
4492      If the compilation is from a C file generated by language preprocessors,
4493      do not set the language if it was already deduced by start_subfile.  */
4494   if (symtab != NULL
4495       && !(cu->language == language_c && symtab->language != language_c))
4496     {
4497       symtab->language = cu->language;
4498     }
4499
4500   if (dwarf2_per_objfile->using_index)
4501     per_cu->v.quick->symtab = symtab;
4502   else
4503     {
4504       struct partial_symtab *pst = per_cu->v.psymtab;
4505       pst->symtab = symtab;
4506       pst->readin = 1;
4507     }
4508
4509   do_cleanups (back_to);
4510 }
4511
4512 /* Process a die and its children.  */
4513
4514 static void
4515 process_die (struct die_info *die, struct dwarf2_cu *cu)
4516 {
4517   switch (die->tag)
4518     {
4519     case DW_TAG_padding:
4520       break;
4521     case DW_TAG_compile_unit:
4522       read_file_scope (die, cu);
4523       break;
4524     case DW_TAG_type_unit:
4525       read_type_unit_scope (die, cu);
4526       break;
4527     case DW_TAG_subprogram:
4528     case DW_TAG_inlined_subroutine:
4529       read_func_scope (die, cu);
4530       break;
4531     case DW_TAG_lexical_block:
4532     case DW_TAG_try_block:
4533     case DW_TAG_catch_block:
4534       read_lexical_block_scope (die, cu);
4535       break;
4536     case DW_TAG_class_type:
4537     case DW_TAG_interface_type:
4538     case DW_TAG_structure_type:
4539     case DW_TAG_union_type:
4540       process_structure_scope (die, cu);
4541       break;
4542     case DW_TAG_enumeration_type:
4543       process_enumeration_scope (die, cu);
4544       break;
4545
4546     /* These dies have a type, but processing them does not create
4547        a symbol or recurse to process the children.  Therefore we can
4548        read them on-demand through read_type_die.  */
4549     case DW_TAG_subroutine_type:
4550     case DW_TAG_set_type:
4551     case DW_TAG_array_type:
4552     case DW_TAG_pointer_type:
4553     case DW_TAG_ptr_to_member_type:
4554     case DW_TAG_reference_type:
4555     case DW_TAG_string_type:
4556       break;
4557
4558     case DW_TAG_base_type:
4559     case DW_TAG_subrange_type:
4560     case DW_TAG_typedef:
4561       /* Add a typedef symbol for the type definition, if it has a
4562          DW_AT_name.  */
4563       new_symbol (die, read_type_die (die, cu), cu);
4564       break;
4565     case DW_TAG_common_block:
4566       read_common_block (die, cu);
4567       break;
4568     case DW_TAG_common_inclusion:
4569       break;
4570     case DW_TAG_namespace:
4571       processing_has_namespace_info = 1;
4572       read_namespace (die, cu);
4573       break;
4574     case DW_TAG_module:
4575       processing_has_namespace_info = 1;
4576       read_module (die, cu);
4577       break;
4578     case DW_TAG_imported_declaration:
4579     case DW_TAG_imported_module:
4580       processing_has_namespace_info = 1;
4581       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4582                                  || cu->language != language_fortran))
4583         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4584                    dwarf_tag_name (die->tag));
4585       read_import_statement (die, cu);
4586       break;
4587     default:
4588       new_symbol (die, NULL, cu);
4589       break;
4590     }
4591 }
4592
4593 /* A helper function for dwarf2_compute_name which determines whether DIE
4594    needs to have the name of the scope prepended to the name listed in the
4595    die.  */
4596
4597 static int
4598 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4599 {
4600   struct attribute *attr;
4601
4602   switch (die->tag)
4603     {
4604     case DW_TAG_namespace:
4605     case DW_TAG_typedef:
4606     case DW_TAG_class_type:
4607     case DW_TAG_interface_type:
4608     case DW_TAG_structure_type:
4609     case DW_TAG_union_type:
4610     case DW_TAG_enumeration_type:
4611     case DW_TAG_enumerator:
4612     case DW_TAG_subprogram:
4613     case DW_TAG_member:
4614       return 1;
4615
4616     case DW_TAG_variable:
4617       /* We only need to prefix "globally" visible variables.  These include
4618          any variable marked with DW_AT_external or any variable that
4619          lives in a namespace.  [Variables in anonymous namespaces
4620          require prefixing, but they are not DW_AT_external.]  */
4621
4622       if (dwarf2_attr (die, DW_AT_specification, cu))
4623         {
4624           struct dwarf2_cu *spec_cu = cu;
4625
4626           return die_needs_namespace (die_specification (die, &spec_cu),
4627                                       spec_cu);
4628         }
4629
4630       attr = dwarf2_attr (die, DW_AT_external, cu);
4631       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4632           && die->parent->tag != DW_TAG_module)
4633         return 0;
4634       /* A variable in a lexical block of some kind does not need a
4635          namespace, even though in C++ such variables may be external
4636          and have a mangled name.  */
4637       if (die->parent->tag ==  DW_TAG_lexical_block
4638           || die->parent->tag ==  DW_TAG_try_block
4639           || die->parent->tag ==  DW_TAG_catch_block
4640           || die->parent->tag == DW_TAG_subprogram)
4641         return 0;
4642       return 1;
4643
4644     default:
4645       return 0;
4646     }
4647 }
4648
4649 /* Retrieve the last character from a mem_file.  */
4650
4651 static void
4652 do_ui_file_peek_last (void *object, const char *buffer, long length)
4653 {
4654   char *last_char_p = (char *) object;
4655
4656   if (length > 0)
4657     *last_char_p = buffer[length - 1];
4658 }
4659
4660 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4661    compute the physname for the object, which include a method's
4662    formal parameters (C++/Java) and return type (Java).
4663
4664    For Ada, return the DIE's linkage name rather than the fully qualified
4665    name.  PHYSNAME is ignored..
4666
4667    The result is allocated on the objfile_obstack and canonicalized.  */
4668
4669 static const char *
4670 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4671                      int physname)
4672 {
4673   if (name == NULL)
4674     name = dwarf2_name (die, cu);
4675
4676   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4677      compute it by typename_concat inside GDB.  */
4678   if (cu->language == language_ada
4679       || (cu->language == language_fortran && physname))
4680     {
4681       /* For Ada unit, we prefer the linkage name over the name, as
4682          the former contains the exported name, which the user expects
4683          to be able to reference.  Ideally, we want the user to be able
4684          to reference this entity using either natural or linkage name,
4685          but we haven't started looking at this enhancement yet.  */
4686       struct attribute *attr;
4687
4688       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4689       if (attr == NULL)
4690         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4691       if (attr && DW_STRING (attr))
4692         return DW_STRING (attr);
4693     }
4694
4695   /* These are the only languages we know how to qualify names in.  */
4696   if (name != NULL
4697       && (cu->language == language_cplus || cu->language == language_java
4698           || cu->language == language_fortran))
4699     {
4700       if (die_needs_namespace (die, cu))
4701         {
4702           long length;
4703           char *prefix;
4704           struct ui_file *buf;
4705
4706           prefix = determine_prefix (die, cu);
4707           buf = mem_fileopen ();
4708           if (*prefix != '\0')
4709             {
4710               char *prefixed_name = typename_concat (NULL, prefix, name,
4711                                                      physname, cu);
4712
4713               fputs_unfiltered (prefixed_name, buf);
4714               xfree (prefixed_name);
4715             }
4716           else
4717             fputs_unfiltered (name ? name : "", buf);
4718
4719           /* Template parameters may be specified in the DIE's DW_AT_name, or
4720              as children with DW_TAG_template_type_param or
4721              DW_TAG_value_type_param.  If the latter, add them to the name
4722              here.  If the name already has template parameters, then
4723              skip this step; some versions of GCC emit both, and
4724              it is more efficient to use the pre-computed name.
4725
4726              Something to keep in mind about this process: it is very
4727              unlikely, or in some cases downright impossible, to produce
4728              something that will match the mangled name of a function.
4729              If the definition of the function has the same debug info,
4730              we should be able to match up with it anyway.  But fallbacks
4731              using the minimal symbol, for instance to find a method
4732              implemented in a stripped copy of libstdc++, will not work.
4733              If we do not have debug info for the definition, we will have to
4734              match them up some other way.
4735
4736              When we do name matching there is a related problem with function
4737              templates; two instantiated function templates are allowed to
4738              differ only by their return types, which we do not add here.  */
4739
4740           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4741             {
4742               struct attribute *attr;
4743               struct die_info *child;
4744               int first = 1;
4745
4746               die->building_fullname = 1;
4747
4748               for (child = die->child; child != NULL; child = child->sibling)
4749                 {
4750                   struct type *type;
4751                   long value;
4752                   gdb_byte *bytes;
4753                   struct dwarf2_locexpr_baton *baton;
4754                   struct value *v;
4755
4756                   if (child->tag != DW_TAG_template_type_param
4757                       && child->tag != DW_TAG_template_value_param)
4758                     continue;
4759
4760                   if (first)
4761                     {
4762                       fputs_unfiltered ("<", buf);
4763                       first = 0;
4764                     }
4765                   else
4766                     fputs_unfiltered (", ", buf);
4767
4768                   attr = dwarf2_attr (child, DW_AT_type, cu);
4769                   if (attr == NULL)
4770                     {
4771                       complaint (&symfile_complaints,
4772                                  _("template parameter missing DW_AT_type"));
4773                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4774                       continue;
4775                     }
4776                   type = die_type (child, cu);
4777
4778                   if (child->tag == DW_TAG_template_type_param)
4779                     {
4780                       c_print_type (type, "", buf, -1, 0);
4781                       continue;
4782                     }
4783
4784                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4785                   if (attr == NULL)
4786                     {
4787                       complaint (&symfile_complaints,
4788                                  _("template parameter missing DW_AT_const_value"));
4789                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4790                       continue;
4791                     }
4792
4793                   dwarf2_const_value_attr (attr, type, name,
4794                                            &cu->comp_unit_obstack, cu,
4795                                            &value, &bytes, &baton);
4796
4797                   if (TYPE_NOSIGN (type))
4798                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4799                        changed, this can use value_print instead.  */
4800                     c_printchar (value, type, buf);
4801                   else
4802                     {
4803                       struct value_print_options opts;
4804
4805                       if (baton != NULL)
4806                         v = dwarf2_evaluate_loc_desc (type, NULL,
4807                                                       baton->data,
4808                                                       baton->size,
4809                                                       baton->per_cu);
4810                       else if (bytes != NULL)
4811                         {
4812                           v = allocate_value (type);
4813                           memcpy (value_contents_writeable (v), bytes,
4814                                   TYPE_LENGTH (type));
4815                         }
4816                       else
4817                         v = value_from_longest (type, value);
4818
4819                       /* Specify decimal so that we do not depend on the radix.  */
4820                       get_formatted_print_options (&opts, 'd');
4821                       opts.raw = 1;
4822                       value_print (v, buf, &opts);
4823                       release_value (v);
4824                       value_free (v);
4825                     }
4826                 }
4827
4828               die->building_fullname = 0;
4829
4830               if (!first)
4831                 {
4832                   /* Close the argument list, with a space if necessary
4833                      (nested templates).  */
4834                   char last_char = '\0';
4835                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4836                   if (last_char == '>')
4837                     fputs_unfiltered (" >", buf);
4838                   else
4839                     fputs_unfiltered (">", buf);
4840                 }
4841             }
4842
4843           /* For Java and C++ methods, append formal parameter type
4844              information, if PHYSNAME.  */
4845
4846           if (physname && die->tag == DW_TAG_subprogram
4847               && (cu->language == language_cplus
4848                   || cu->language == language_java))
4849             {
4850               struct type *type = read_type_die (die, cu);
4851
4852               c_type_print_args (type, buf, 0, cu->language);
4853
4854               if (cu->language == language_java)
4855                 {
4856                   /* For java, we must append the return type to method
4857                      names. */
4858                   if (die->tag == DW_TAG_subprogram)
4859                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4860                                      0, 0);
4861                 }
4862               else if (cu->language == language_cplus)
4863                 {
4864                   /* Assume that an artificial first parameter is
4865                      "this", but do not crash if it is not.  RealView
4866                      marks unnamed (and thus unused) parameters as
4867                      artificial; there is no way to differentiate
4868                      the two cases.  */
4869                   if (TYPE_NFIELDS (type) > 0
4870                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4871                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4872                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4873                     fputs_unfiltered (" const", buf);
4874                 }
4875             }
4876
4877           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4878                                        &length);
4879           ui_file_delete (buf);
4880
4881           if (cu->language == language_cplus)
4882             {
4883               char *cname
4884                 = dwarf2_canonicalize_name (name, cu,
4885                                             &cu->objfile->objfile_obstack);
4886
4887               if (cname != NULL)
4888                 name = cname;
4889             }
4890         }
4891     }
4892
4893   return name;
4894 }
4895
4896 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4897    If scope qualifiers are appropriate they will be added.  The result
4898    will be allocated on the objfile_obstack, or NULL if the DIE does
4899    not have a name.  NAME may either be from a previous call to
4900    dwarf2_name or NULL.
4901
4902    The output string will be canonicalized (if C++/Java). */
4903
4904 static const char *
4905 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4906 {
4907   return dwarf2_compute_name (name, die, cu, 0);
4908 }
4909
4910 /* Construct a physname for the given DIE in CU.  NAME may either be
4911    from a previous call to dwarf2_name or NULL.  The result will be
4912    allocated on the objfile_objstack or NULL if the DIE does not have a
4913    name.
4914
4915    The output string will be canonicalized (if C++/Java).  */
4916
4917 static const char *
4918 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4919 {
4920   return dwarf2_compute_name (name, die, cu, 1);
4921 }
4922
4923 /* Read the import statement specified by the given die and record it.  */
4924
4925 static void
4926 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4927 {
4928   struct attribute *import_attr;
4929   struct die_info *imported_die;
4930   struct dwarf2_cu *imported_cu;
4931   const char *imported_name;
4932   const char *imported_name_prefix;
4933   const char *canonical_name;
4934   const char *import_alias;
4935   const char *imported_declaration = NULL;
4936   const char *import_prefix;
4937
4938   char *temp;
4939
4940   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4941   if (import_attr == NULL)
4942     {
4943       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4944                  dwarf_tag_name (die->tag));
4945       return;
4946     }
4947
4948   imported_cu = cu;
4949   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4950   imported_name = dwarf2_name (imported_die, imported_cu);
4951   if (imported_name == NULL)
4952     {
4953       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4954
4955         The import in the following code:
4956         namespace A
4957           {
4958             typedef int B;
4959           }
4960
4961         int main ()
4962           {
4963             using A::B;
4964             B b;
4965             return b;
4966           }
4967
4968         ...
4969          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4970             <52>   DW_AT_decl_file   : 1
4971             <53>   DW_AT_decl_line   : 6
4972             <54>   DW_AT_import      : <0x75>
4973          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4974             <59>   DW_AT_name        : B
4975             <5b>   DW_AT_decl_file   : 1
4976             <5c>   DW_AT_decl_line   : 2
4977             <5d>   DW_AT_type        : <0x6e>
4978         ...
4979          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4980             <76>   DW_AT_byte_size   : 4
4981             <77>   DW_AT_encoding    : 5        (signed)
4982
4983         imports the wrong die ( 0x75 instead of 0x58 ).
4984         This case will be ignored until the gcc bug is fixed.  */
4985       return;
4986     }
4987
4988   /* Figure out the local name after import.  */
4989   import_alias = dwarf2_name (die, cu);
4990
4991   /* Figure out where the statement is being imported to.  */
4992   import_prefix = determine_prefix (die, cu);
4993
4994   /* Figure out what the scope of the imported die is and prepend it
4995      to the name of the imported die.  */
4996   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4997
4998   if (imported_die->tag != DW_TAG_namespace
4999       && imported_die->tag != DW_TAG_module)
5000     {
5001       imported_declaration = imported_name;
5002       canonical_name = imported_name_prefix;
5003     }
5004   else if (strlen (imported_name_prefix) > 0)
5005     {
5006       temp = alloca (strlen (imported_name_prefix)
5007                      + 2 + strlen (imported_name) + 1);
5008       strcpy (temp, imported_name_prefix);
5009       strcat (temp, "::");
5010       strcat (temp, imported_name);
5011       canonical_name = temp;
5012     }
5013   else
5014     canonical_name = imported_name;
5015
5016   cp_add_using_directive (import_prefix,
5017                           canonical_name,
5018                           import_alias,
5019                           imported_declaration,
5020                           &cu->objfile->objfile_obstack);
5021 }
5022
5023 static void
5024 initialize_cu_func_list (struct dwarf2_cu *cu)
5025 {
5026   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5027 }
5028
5029 static void
5030 free_cu_line_header (void *arg)
5031 {
5032   struct dwarf2_cu *cu = arg;
5033
5034   free_line_header (cu->line_header);
5035   cu->line_header = NULL;
5036 }
5037
5038 static void
5039 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5040                          char **name, char **comp_dir)
5041 {
5042   struct attribute *attr;
5043
5044   *name = NULL;
5045   *comp_dir = NULL;
5046
5047   /* Find the filename.  Do not use dwarf2_name here, since the filename
5048      is not a source language identifier.  */
5049   attr = dwarf2_attr (die, DW_AT_name, cu);
5050   if (attr)
5051     {
5052       *name = DW_STRING (attr);
5053     }
5054
5055   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5056   if (attr)
5057     *comp_dir = DW_STRING (attr);
5058   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5059     {
5060       *comp_dir = ldirname (*name);
5061       if (*comp_dir != NULL)
5062         make_cleanup (xfree, *comp_dir);
5063     }
5064   if (*comp_dir != NULL)
5065     {
5066       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5067          directory, get rid of it.  */
5068       char *cp = strchr (*comp_dir, ':');
5069
5070       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5071         *comp_dir = cp + 1;
5072     }
5073
5074   if (*name == NULL)
5075     *name = "<unknown>";
5076 }
5077
5078 static void
5079 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5080 {
5081   struct objfile *objfile = cu->objfile;
5082   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5083   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5084   CORE_ADDR highpc = ((CORE_ADDR) 0);
5085   struct attribute *attr;
5086   char *name = NULL;
5087   char *comp_dir = NULL;
5088   struct die_info *child_die;
5089   bfd *abfd = objfile->obfd;
5090   struct line_header *line_header = 0;
5091   CORE_ADDR baseaddr;
5092
5093   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5094
5095   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5096
5097   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5098      from finish_block.  */
5099   if (lowpc == ((CORE_ADDR) -1))
5100     lowpc = highpc;
5101   lowpc += baseaddr;
5102   highpc += baseaddr;
5103
5104   find_file_and_directory (die, cu, &name, &comp_dir);
5105
5106   attr = dwarf2_attr (die, DW_AT_language, cu);
5107   if (attr)
5108     {
5109       set_cu_language (DW_UNSND (attr), cu);
5110     }
5111
5112   attr = dwarf2_attr (die, DW_AT_producer, cu);
5113   if (attr)
5114     cu->producer = DW_STRING (attr);
5115
5116   /* We assume that we're processing GCC output. */
5117   processing_gcc_compilation = 2;
5118
5119   processing_has_namespace_info = 0;
5120
5121   start_symtab (name, comp_dir, lowpc);
5122   record_debugformat ("DWARF 2");
5123   record_producer (cu->producer);
5124
5125   initialize_cu_func_list (cu);
5126
5127   /* Decode line number information if present.  We do this before
5128      processing child DIEs, so that the line header table is available
5129      for DW_AT_decl_file.  */
5130   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5131   if (attr)
5132     {
5133       unsigned int line_offset = DW_UNSND (attr);
5134       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5135       if (line_header)
5136         {
5137           cu->line_header = line_header;
5138           make_cleanup (free_cu_line_header, cu);
5139           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5140         }
5141     }
5142
5143   /* Process all dies in compilation unit.  */
5144   if (die->child != NULL)
5145     {
5146       child_die = die->child;
5147       while (child_die && child_die->tag)
5148         {
5149           process_die (child_die, cu);
5150           child_die = sibling_die (child_die);
5151         }
5152     }
5153
5154   /* Decode macro information, if present.  Dwarf 2 macro information
5155      refers to information in the line number info statement program
5156      header, so we can only read it if we've read the header
5157      successfully.  */
5158   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5159   if (attr && line_header)
5160     {
5161       unsigned int macro_offset = DW_UNSND (attr);
5162
5163       dwarf_decode_macros (line_header, macro_offset,
5164                            comp_dir, abfd, cu);
5165     }
5166   do_cleanups (back_to);
5167 }
5168
5169 /* For TUs we want to skip the first top level sibling if it's not the
5170    actual type being defined by this TU.  In this case the first top
5171    level sibling is there to provide context only.  */
5172
5173 static void
5174 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5175 {
5176   struct objfile *objfile = cu->objfile;
5177   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5178   CORE_ADDR lowpc;
5179   struct attribute *attr;
5180   char *name = NULL;
5181   char *comp_dir = NULL;
5182   struct die_info *child_die;
5183   bfd *abfd = objfile->obfd;
5184
5185   /* start_symtab needs a low pc, but we don't really have one.
5186      Do what read_file_scope would do in the absence of such info.  */
5187   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5188
5189   /* Find the filename.  Do not use dwarf2_name here, since the filename
5190      is not a source language identifier.  */
5191   attr = dwarf2_attr (die, DW_AT_name, cu);
5192   if (attr)
5193     name = DW_STRING (attr);
5194
5195   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5196   if (attr)
5197     comp_dir = DW_STRING (attr);
5198   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5199     {
5200       comp_dir = ldirname (name);
5201       if (comp_dir != NULL)
5202         make_cleanup (xfree, comp_dir);
5203     }
5204
5205   if (name == NULL)
5206     name = "<unknown>";
5207
5208   attr = dwarf2_attr (die, DW_AT_language, cu);
5209   if (attr)
5210     set_cu_language (DW_UNSND (attr), cu);
5211
5212   /* This isn't technically needed today.  It is done for symmetry
5213      with read_file_scope.  */
5214   attr = dwarf2_attr (die, DW_AT_producer, cu);
5215   if (attr)
5216     cu->producer = DW_STRING (attr);
5217
5218   /* We assume that we're processing GCC output. */
5219   processing_gcc_compilation = 2;
5220
5221   processing_has_namespace_info = 0;
5222
5223   start_symtab (name, comp_dir, lowpc);
5224   record_debugformat ("DWARF 2");
5225   record_producer (cu->producer);
5226
5227   /* Process the dies in the type unit.  */
5228   if (die->child == NULL)
5229     {
5230       dump_die_for_error (die);
5231       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5232              bfd_get_filename (abfd));
5233     }
5234
5235   child_die = die->child;
5236
5237   while (child_die && child_die->tag)
5238     {
5239       process_die (child_die, cu);
5240
5241       child_die = sibling_die (child_die);
5242     }
5243
5244   do_cleanups (back_to);
5245 }
5246
5247 static void
5248 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5249                      struct dwarf2_cu *cu)
5250 {
5251   struct function_range *thisfn;
5252
5253   thisfn = (struct function_range *)
5254     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5255   thisfn->name = name;
5256   thisfn->lowpc = lowpc;
5257   thisfn->highpc = highpc;
5258   thisfn->seen_line = 0;
5259   thisfn->next = NULL;
5260
5261   if (cu->last_fn == NULL)
5262       cu->first_fn = thisfn;
5263   else
5264       cu->last_fn->next = thisfn;
5265
5266   cu->last_fn = thisfn;
5267 }
5268
5269 /* qsort helper for inherit_abstract_dies.  */
5270
5271 static int
5272 unsigned_int_compar (const void *ap, const void *bp)
5273 {
5274   unsigned int a = *(unsigned int *) ap;
5275   unsigned int b = *(unsigned int *) bp;
5276
5277   return (a > b) - (b > a);
5278 }
5279
5280 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5281    Inherit only the children of the DW_AT_abstract_origin DIE not being already
5282    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
5283
5284 static void
5285 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5286 {
5287   struct die_info *child_die;
5288   unsigned die_children_count;
5289   /* CU offsets which were referenced by children of the current DIE.  */
5290   unsigned *offsets;
5291   unsigned *offsets_end, *offsetp;
5292   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5293   struct die_info *origin_die;
5294   /* Iterator of the ORIGIN_DIE children.  */
5295   struct die_info *origin_child_die;
5296   struct cleanup *cleanups;
5297   struct attribute *attr;
5298   struct dwarf2_cu *origin_cu;
5299   struct pending **origin_previous_list_in_scope;
5300
5301   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5302   if (!attr)
5303     return;
5304
5305   /* Note that following die references may follow to a die in a
5306      different cu.  */
5307
5308   origin_cu = cu;
5309   origin_die = follow_die_ref (die, attr, &origin_cu);
5310
5311   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5312      symbols in.  */
5313   origin_previous_list_in_scope = origin_cu->list_in_scope;
5314   origin_cu->list_in_scope = cu->list_in_scope;
5315
5316   if (die->tag != origin_die->tag
5317       && !(die->tag == DW_TAG_inlined_subroutine
5318            && origin_die->tag == DW_TAG_subprogram))
5319     complaint (&symfile_complaints,
5320                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5321                die->offset, origin_die->offset);
5322
5323   child_die = die->child;
5324   die_children_count = 0;
5325   while (child_die && child_die->tag)
5326     {
5327       child_die = sibling_die (child_die);
5328       die_children_count++;
5329     }
5330   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5331   cleanups = make_cleanup (xfree, offsets);
5332
5333   offsets_end = offsets;
5334   child_die = die->child;
5335   while (child_die && child_die->tag)
5336     {
5337       /* For each CHILD_DIE, find the corresponding child of
5338          ORIGIN_DIE.  If there is more than one layer of
5339          DW_AT_abstract_origin, follow them all; there shouldn't be,
5340          but GCC versions at least through 4.4 generate this (GCC PR
5341          40573).  */
5342       struct die_info *child_origin_die = child_die;
5343       struct dwarf2_cu *child_origin_cu = cu;
5344
5345       while (1)
5346         {
5347           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5348                               child_origin_cu);
5349           if (attr == NULL)
5350             break;
5351           child_origin_die = follow_die_ref (child_origin_die, attr,
5352                                              &child_origin_cu);
5353         }
5354
5355       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5356          counterpart may exist.  */
5357       if (child_origin_die != child_die)
5358         {
5359           if (child_die->tag != child_origin_die->tag
5360               && !(child_die->tag == DW_TAG_inlined_subroutine
5361                    && child_origin_die->tag == DW_TAG_subprogram))
5362             complaint (&symfile_complaints,
5363                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5364                          "different tags"), child_die->offset,
5365                        child_origin_die->offset);
5366           if (child_origin_die->parent != origin_die)
5367             complaint (&symfile_complaints,
5368                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5369                          "different parents"), child_die->offset,
5370                        child_origin_die->offset);
5371           else
5372             *offsets_end++ = child_origin_die->offset;
5373         }
5374       child_die = sibling_die (child_die);
5375     }
5376   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5377          unsigned_int_compar);
5378   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5379     if (offsetp[-1] == *offsetp)
5380       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5381                                         "to DIE 0x%x as their abstract origin"),
5382                  die->offset, *offsetp);
5383
5384   offsetp = offsets;
5385   origin_child_die = origin_die->child;
5386   while (origin_child_die && origin_child_die->tag)
5387     {
5388       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5389       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5390         offsetp++;
5391       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5392         {
5393           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5394           process_die (origin_child_die, origin_cu);
5395         }
5396       origin_child_die = sibling_die (origin_child_die);
5397     }
5398   origin_cu->list_in_scope = origin_previous_list_in_scope;
5399
5400   do_cleanups (cleanups);
5401 }
5402
5403 static void
5404 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5405 {
5406   struct objfile *objfile = cu->objfile;
5407   struct context_stack *new;
5408   CORE_ADDR lowpc;
5409   CORE_ADDR highpc;
5410   struct die_info *child_die;
5411   struct attribute *attr, *call_line, *call_file;
5412   char *name;
5413   CORE_ADDR baseaddr;
5414   struct block *block;
5415   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5416   VEC (symbolp) *template_args = NULL;
5417   struct template_symbol *templ_func = NULL;
5418
5419   if (inlined_func)
5420     {
5421       /* If we do not have call site information, we can't show the
5422          caller of this inlined function.  That's too confusing, so
5423          only use the scope for local variables.  */
5424       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5425       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5426       if (call_line == NULL || call_file == NULL)
5427         {
5428           read_lexical_block_scope (die, cu);
5429           return;
5430         }
5431     }
5432
5433   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5434
5435   name = dwarf2_name (die, cu);
5436
5437   /* Ignore functions with missing or empty names.  These are actually
5438      illegal according to the DWARF standard.  */
5439   if (name == NULL)
5440     {
5441       complaint (&symfile_complaints,
5442                  _("missing name for subprogram DIE at %d"), die->offset);
5443       return;
5444     }
5445
5446   /* Ignore functions with missing or invalid low and high pc attributes.  */
5447   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5448     {
5449       attr = dwarf2_attr (die, DW_AT_external, cu);
5450       if (!attr || !DW_UNSND (attr))
5451         complaint (&symfile_complaints,
5452                    _("cannot get low and high bounds for subprogram DIE at %d"),
5453                    die->offset);
5454       return;
5455     }
5456
5457   lowpc += baseaddr;
5458   highpc += baseaddr;
5459
5460   /* Record the function range for dwarf_decode_lines.  */
5461   add_to_cu_func_list (name, lowpc, highpc, cu);
5462
5463   /* If we have any template arguments, then we must allocate a
5464      different sort of symbol.  */
5465   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5466     {
5467       if (child_die->tag == DW_TAG_template_type_param
5468           || child_die->tag == DW_TAG_template_value_param)
5469         {
5470           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5471                                        struct template_symbol);
5472           templ_func->base.is_cplus_template_function = 1;
5473           break;
5474         }
5475     }
5476
5477   new = push_context (0, lowpc);
5478   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5479                                (struct symbol *) templ_func);
5480
5481   /* If there is a location expression for DW_AT_frame_base, record
5482      it.  */
5483   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5484   if (attr)
5485     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5486        expression is being recorded directly in the function's symbol
5487        and not in a separate frame-base object.  I guess this hack is
5488        to avoid adding some sort of frame-base adjunct/annex to the
5489        function's symbol :-(.  The problem with doing this is that it
5490        results in a function symbol with a location expression that
5491        has nothing to do with the location of the function, ouch!  The
5492        relationship should be: a function's symbol has-a frame base; a
5493        frame-base has-a location expression.  */
5494     dwarf2_symbol_mark_computed (attr, new->name, cu);
5495
5496   cu->list_in_scope = &local_symbols;
5497
5498   if (die->child != NULL)
5499     {
5500       child_die = die->child;
5501       while (child_die && child_die->tag)
5502         {
5503           if (child_die->tag == DW_TAG_template_type_param
5504               || child_die->tag == DW_TAG_template_value_param)
5505             {
5506               struct symbol *arg = new_symbol (child_die, NULL, cu);
5507
5508               if (arg != NULL)
5509                 VEC_safe_push (symbolp, template_args, arg);
5510             }
5511           else
5512             process_die (child_die, cu);
5513           child_die = sibling_die (child_die);
5514         }
5515     }
5516
5517   inherit_abstract_dies (die, cu);
5518
5519   /* If we have a DW_AT_specification, we might need to import using
5520      directives from the context of the specification DIE.  See the
5521      comment in determine_prefix.  */
5522   if (cu->language == language_cplus
5523       && dwarf2_attr (die, DW_AT_specification, cu))
5524     {
5525       struct dwarf2_cu *spec_cu = cu;
5526       struct die_info *spec_die = die_specification (die, &spec_cu);
5527
5528       while (spec_die)
5529         {
5530           child_die = spec_die->child;
5531           while (child_die && child_die->tag)
5532             {
5533               if (child_die->tag == DW_TAG_imported_module)
5534                 process_die (child_die, spec_cu);
5535               child_die = sibling_die (child_die);
5536             }
5537
5538           /* In some cases, GCC generates specification DIEs that
5539              themselves contain DW_AT_specification attributes.  */
5540           spec_die = die_specification (spec_die, &spec_cu);
5541         }
5542     }
5543
5544   new = pop_context ();
5545   /* Make a block for the local symbols within.  */
5546   block = finish_block (new->name, &local_symbols, new->old_blocks,
5547                         lowpc, highpc, objfile);
5548
5549   /* For C++, set the block's scope.  */
5550   if (cu->language == language_cplus || cu->language == language_fortran)
5551     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5552                         determine_prefix (die, cu),
5553                         processing_has_namespace_info);
5554
5555   /* If we have address ranges, record them.  */
5556   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5557
5558   /* Attach template arguments to function.  */
5559   if (! VEC_empty (symbolp, template_args))
5560     {
5561       gdb_assert (templ_func != NULL);
5562
5563       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5564       templ_func->template_arguments
5565         = obstack_alloc (&objfile->objfile_obstack,
5566                          (templ_func->n_template_arguments
5567                           * sizeof (struct symbol *)));
5568       memcpy (templ_func->template_arguments,
5569               VEC_address (symbolp, template_args),
5570               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5571       VEC_free (symbolp, template_args);
5572     }
5573
5574   /* In C++, we can have functions nested inside functions (e.g., when
5575      a function declares a class that has methods).  This means that
5576      when we finish processing a function scope, we may need to go
5577      back to building a containing block's symbol lists.  */
5578   local_symbols = new->locals;
5579   param_symbols = new->params;
5580   using_directives = new->using_directives;
5581
5582   /* If we've finished processing a top-level function, subsequent
5583      symbols go in the file symbol list.  */
5584   if (outermost_context_p ())
5585     cu->list_in_scope = &file_symbols;
5586 }
5587
5588 /* Process all the DIES contained within a lexical block scope.  Start
5589    a new scope, process the dies, and then close the scope.  */
5590
5591 static void
5592 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5593 {
5594   struct objfile *objfile = cu->objfile;
5595   struct context_stack *new;
5596   CORE_ADDR lowpc, highpc;
5597   struct die_info *child_die;
5598   CORE_ADDR baseaddr;
5599
5600   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5601
5602   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5603   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5604      as multiple lexical blocks?  Handling children in a sane way would
5605      be nasty.  Might be easier to properly extend generic blocks to
5606      describe ranges.  */
5607   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5608     return;
5609   lowpc += baseaddr;
5610   highpc += baseaddr;
5611
5612   push_context (0, lowpc);
5613   if (die->child != NULL)
5614     {
5615       child_die = die->child;
5616       while (child_die && child_die->tag)
5617         {
5618           process_die (child_die, cu);
5619           child_die = sibling_die (child_die);
5620         }
5621     }
5622   new = pop_context ();
5623
5624   if (local_symbols != NULL || using_directives != NULL)
5625     {
5626       struct block *block
5627         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5628                         highpc, objfile);
5629
5630       /* Note that recording ranges after traversing children, as we
5631          do here, means that recording a parent's ranges entails
5632          walking across all its children's ranges as they appear in
5633          the address map, which is quadratic behavior.
5634
5635          It would be nicer to record the parent's ranges before
5636          traversing its children, simply overriding whatever you find
5637          there.  But since we don't even decide whether to create a
5638          block until after we've traversed its children, that's hard
5639          to do.  */
5640       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5641     }
5642   local_symbols = new->locals;
5643   using_directives = new->using_directives;
5644 }
5645
5646 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5647    Return 1 if the attributes are present and valid, otherwise, return 0.
5648    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5649
5650 static int
5651 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5652                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5653                     struct partial_symtab *ranges_pst)
5654 {
5655   struct objfile *objfile = cu->objfile;
5656   struct comp_unit_head *cu_header = &cu->header;
5657   bfd *obfd = objfile->obfd;
5658   unsigned int addr_size = cu_header->addr_size;
5659   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5660   /* Base address selection entry.  */
5661   CORE_ADDR base;
5662   int found_base;
5663   unsigned int dummy;
5664   gdb_byte *buffer;
5665   CORE_ADDR marker;
5666   int low_set;
5667   CORE_ADDR low = 0;
5668   CORE_ADDR high = 0;
5669   CORE_ADDR baseaddr;
5670
5671   found_base = cu->base_known;
5672   base = cu->base_address;
5673
5674   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5675   if (offset >= dwarf2_per_objfile->ranges.size)
5676     {
5677       complaint (&symfile_complaints,
5678                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5679                  offset);
5680       return 0;
5681     }
5682   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5683
5684   /* Read in the largest possible address.  */
5685   marker = read_address (obfd, buffer, cu, &dummy);
5686   if ((marker & mask) == mask)
5687     {
5688       /* If we found the largest possible address, then
5689          read the base address.  */
5690       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5691       buffer += 2 * addr_size;
5692       offset += 2 * addr_size;
5693       found_base = 1;
5694     }
5695
5696   low_set = 0;
5697
5698   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5699
5700   while (1)
5701     {
5702       CORE_ADDR range_beginning, range_end;
5703
5704       range_beginning = read_address (obfd, buffer, cu, &dummy);
5705       buffer += addr_size;
5706       range_end = read_address (obfd, buffer, cu, &dummy);
5707       buffer += addr_size;
5708       offset += 2 * addr_size;
5709
5710       /* An end of list marker is a pair of zero addresses.  */
5711       if (range_beginning == 0 && range_end == 0)
5712         /* Found the end of list entry.  */
5713         break;
5714
5715       /* Each base address selection entry is a pair of 2 values.
5716          The first is the largest possible address, the second is
5717          the base address.  Check for a base address here.  */
5718       if ((range_beginning & mask) == mask)
5719         {
5720           /* If we found the largest possible address, then
5721              read the base address.  */
5722           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5723           found_base = 1;
5724           continue;
5725         }
5726
5727       if (!found_base)
5728         {
5729           /* We have no valid base address for the ranges
5730              data.  */
5731           complaint (&symfile_complaints,
5732                      _("Invalid .debug_ranges data (no base address)"));
5733           return 0;
5734         }
5735
5736       range_beginning += base;
5737       range_end += base;
5738
5739       if (ranges_pst != NULL && range_beginning < range_end)
5740         addrmap_set_empty (objfile->psymtabs_addrmap,
5741                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5742                            ranges_pst);
5743
5744       /* FIXME: This is recording everything as a low-high
5745          segment of consecutive addresses.  We should have a
5746          data structure for discontiguous block ranges
5747          instead.  */
5748       if (! low_set)
5749         {
5750           low = range_beginning;
5751           high = range_end;
5752           low_set = 1;
5753         }
5754       else
5755         {
5756           if (range_beginning < low)
5757             low = range_beginning;
5758           if (range_end > high)
5759             high = range_end;
5760         }
5761     }
5762
5763   if (! low_set)
5764     /* If the first entry is an end-of-list marker, the range
5765        describes an empty scope, i.e. no instructions.  */
5766     return 0;
5767
5768   if (low_return)
5769     *low_return = low;
5770   if (high_return)
5771     *high_return = high;
5772   return 1;
5773 }
5774
5775 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5776    are present and valid, otherwise, return 0.  Return -1 if the range is
5777    discontinuous, i.e. derived from DW_AT_ranges information.  */
5778 static int
5779 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5780                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5781                       struct partial_symtab *pst)
5782 {
5783   struct attribute *attr;
5784   CORE_ADDR low = 0;
5785   CORE_ADDR high = 0;
5786   int ret = 0;
5787
5788   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5789   if (attr)
5790     {
5791       high = DW_ADDR (attr);
5792       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5793       if (attr)
5794         low = DW_ADDR (attr);
5795       else
5796         /* Found high w/o low attribute.  */
5797         return 0;
5798
5799       /* Found consecutive range of addresses.  */
5800       ret = 1;
5801     }
5802   else
5803     {
5804       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5805       if (attr != NULL)
5806         {
5807           /* Value of the DW_AT_ranges attribute is the offset in the
5808              .debug_ranges section.  */
5809           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5810             return 0;
5811           /* Found discontinuous range of addresses.  */
5812           ret = -1;
5813         }
5814     }
5815
5816   if (high < low)
5817     return 0;
5818
5819   /* When using the GNU linker, .gnu.linkonce. sections are used to
5820      eliminate duplicate copies of functions and vtables and such.
5821      The linker will arbitrarily choose one and discard the others.
5822      The AT_*_pc values for such functions refer to local labels in
5823      these sections.  If the section from that file was discarded, the
5824      labels are not in the output, so the relocs get a value of 0.
5825      If this is a discarded function, mark the pc bounds as invalid,
5826      so that GDB will ignore it.  */
5827   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5828     return 0;
5829
5830   *lowpc = low;
5831   *highpc = high;
5832   return ret;
5833 }
5834
5835 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5836    its low and high PC addresses.  Do nothing if these addresses could not
5837    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5838    and HIGHPC to the high address if greater than HIGHPC.  */
5839
5840 static void
5841 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5842                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5843                                  struct dwarf2_cu *cu)
5844 {
5845   CORE_ADDR low, high;
5846   struct die_info *child = die->child;
5847
5848   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5849     {
5850       *lowpc = min (*lowpc, low);
5851       *highpc = max (*highpc, high);
5852     }
5853
5854   /* If the language does not allow nested subprograms (either inside
5855      subprograms or lexical blocks), we're done.  */
5856   if (cu->language != language_ada)
5857     return;
5858
5859   /* Check all the children of the given DIE.  If it contains nested
5860      subprograms, then check their pc bounds.  Likewise, we need to
5861      check lexical blocks as well, as they may also contain subprogram
5862      definitions.  */
5863   while (child && child->tag)
5864     {
5865       if (child->tag == DW_TAG_subprogram
5866           || child->tag == DW_TAG_lexical_block)
5867         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5868       child = sibling_die (child);
5869     }
5870 }
5871
5872 /* Get the low and high pc's represented by the scope DIE, and store
5873    them in *LOWPC and *HIGHPC.  If the correct values can't be
5874    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5875
5876 static void
5877 get_scope_pc_bounds (struct die_info *die,
5878                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5879                      struct dwarf2_cu *cu)
5880 {
5881   CORE_ADDR best_low = (CORE_ADDR) -1;
5882   CORE_ADDR best_high = (CORE_ADDR) 0;
5883   CORE_ADDR current_low, current_high;
5884
5885   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5886     {
5887       best_low = current_low;
5888       best_high = current_high;
5889     }
5890   else
5891     {
5892       struct die_info *child = die->child;
5893
5894       while (child && child->tag)
5895         {
5896           switch (child->tag) {
5897           case DW_TAG_subprogram:
5898             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5899             break;
5900           case DW_TAG_namespace:
5901           case DW_TAG_module:
5902             /* FIXME: carlton/2004-01-16: Should we do this for
5903                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5904                that current GCC's always emit the DIEs corresponding
5905                to definitions of methods of classes as children of a
5906                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5907                the DIEs giving the declarations, which could be
5908                anywhere).  But I don't see any reason why the
5909                standards says that they have to be there.  */
5910             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5911
5912             if (current_low != ((CORE_ADDR) -1))
5913               {
5914                 best_low = min (best_low, current_low);
5915                 best_high = max (best_high, current_high);
5916               }
5917             break;
5918           default:
5919             /* Ignore. */
5920             break;
5921           }
5922
5923           child = sibling_die (child);
5924         }
5925     }
5926
5927   *lowpc = best_low;
5928   *highpc = best_high;
5929 }
5930
5931 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5932    in DIE.  */
5933 static void
5934 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5935                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5936 {
5937   struct attribute *attr;
5938
5939   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5940   if (attr)
5941     {
5942       CORE_ADDR high = DW_ADDR (attr);
5943
5944       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5945       if (attr)
5946         {
5947           CORE_ADDR low = DW_ADDR (attr);
5948
5949           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5950         }
5951     }
5952
5953   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5954   if (attr)
5955     {
5956       bfd *obfd = cu->objfile->obfd;
5957
5958       /* The value of the DW_AT_ranges attribute is the offset of the
5959          address range list in the .debug_ranges section.  */
5960       unsigned long offset = DW_UNSND (attr);
5961       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5962
5963       /* For some target architectures, but not others, the
5964          read_address function sign-extends the addresses it returns.
5965          To recognize base address selection entries, we need a
5966          mask.  */
5967       unsigned int addr_size = cu->header.addr_size;
5968       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5969
5970       /* The base address, to which the next pair is relative.  Note
5971          that this 'base' is a DWARF concept: most entries in a range
5972          list are relative, to reduce the number of relocs against the
5973          debugging information.  This is separate from this function's
5974          'baseaddr' argument, which GDB uses to relocate debugging
5975          information from a shared library based on the address at
5976          which the library was loaded.  */
5977       CORE_ADDR base = cu->base_address;
5978       int base_known = cu->base_known;
5979
5980       gdb_assert (dwarf2_per_objfile->ranges.readin);
5981       if (offset >= dwarf2_per_objfile->ranges.size)
5982         {
5983           complaint (&symfile_complaints,
5984                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5985                      offset);
5986           return;
5987         }
5988
5989       for (;;)
5990         {
5991           unsigned int bytes_read;
5992           CORE_ADDR start, end;
5993
5994           start = read_address (obfd, buffer, cu, &bytes_read);
5995           buffer += bytes_read;
5996           end = read_address (obfd, buffer, cu, &bytes_read);
5997           buffer += bytes_read;
5998
5999           /* Did we find the end of the range list?  */
6000           if (start == 0 && end == 0)
6001             break;
6002
6003           /* Did we find a base address selection entry?  */
6004           else if ((start & base_select_mask) == base_select_mask)
6005             {
6006               base = end;
6007               base_known = 1;
6008             }
6009
6010           /* We found an ordinary address range.  */
6011           else
6012             {
6013               if (!base_known)
6014                 {
6015                   complaint (&symfile_complaints,
6016                              _("Invalid .debug_ranges data (no base address)"));
6017                   return;
6018                 }
6019
6020               record_block_range (block,
6021                                   baseaddr + base + start,
6022                                   baseaddr + base + end - 1);
6023             }
6024         }
6025     }
6026 }
6027
6028 /* Add an aggregate field to the field list.  */
6029
6030 static void
6031 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6032                   struct dwarf2_cu *cu)
6033 {
6034   struct objfile *objfile = cu->objfile;
6035   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6036   struct nextfield *new_field;
6037   struct attribute *attr;
6038   struct field *fp;
6039   char *fieldname = "";
6040
6041   /* Allocate a new field list entry and link it in.  */
6042   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6043   make_cleanup (xfree, new_field);
6044   memset (new_field, 0, sizeof (struct nextfield));
6045
6046   if (die->tag == DW_TAG_inheritance)
6047     {
6048       new_field->next = fip->baseclasses;
6049       fip->baseclasses = new_field;
6050     }
6051   else
6052     {
6053       new_field->next = fip->fields;
6054       fip->fields = new_field;
6055     }
6056   fip->nfields++;
6057
6058   /* Handle accessibility and virtuality of field.
6059      The default accessibility for members is public, the default
6060      accessibility for inheritance is private.  */
6061   if (die->tag != DW_TAG_inheritance)
6062     new_field->accessibility = DW_ACCESS_public;
6063   else
6064     new_field->accessibility = DW_ACCESS_private;
6065   new_field->virtuality = DW_VIRTUALITY_none;
6066
6067   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6068   if (attr)
6069     new_field->accessibility = DW_UNSND (attr);
6070   if (new_field->accessibility != DW_ACCESS_public)
6071     fip->non_public_fields = 1;
6072   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6073   if (attr)
6074     new_field->virtuality = DW_UNSND (attr);
6075
6076   fp = &new_field->field;
6077
6078   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6079     {
6080       /* Data member other than a C++ static data member.  */
6081
6082       /* Get type of field.  */
6083       fp->type = die_type (die, cu);
6084
6085       SET_FIELD_BITPOS (*fp, 0);
6086
6087       /* Get bit size of field (zero if none).  */
6088       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6089       if (attr)
6090         {
6091           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6092         }
6093       else
6094         {
6095           FIELD_BITSIZE (*fp) = 0;
6096         }
6097
6098       /* Get bit offset of field.  */
6099       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6100       if (attr)
6101         {
6102           int byte_offset = 0;
6103
6104           if (attr_form_is_section_offset (attr))
6105             dwarf2_complex_location_expr_complaint ();
6106           else if (attr_form_is_constant (attr))
6107             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6108           else if (attr_form_is_block (attr))
6109             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6110           else
6111             dwarf2_complex_location_expr_complaint ();
6112
6113           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6114         }
6115       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6116       if (attr)
6117         {
6118           if (gdbarch_bits_big_endian (gdbarch))
6119             {
6120               /* For big endian bits, the DW_AT_bit_offset gives the
6121                  additional bit offset from the MSB of the containing
6122                  anonymous object to the MSB of the field.  We don't
6123                  have to do anything special since we don't need to
6124                  know the size of the anonymous object.  */
6125               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6126             }
6127           else
6128             {
6129               /* For little endian bits, compute the bit offset to the
6130                  MSB of the anonymous object, subtract off the number of
6131                  bits from the MSB of the field to the MSB of the
6132                  object, and then subtract off the number of bits of
6133                  the field itself.  The result is the bit offset of
6134                  the LSB of the field.  */
6135               int anonymous_size;
6136               int bit_offset = DW_UNSND (attr);
6137
6138               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6139               if (attr)
6140                 {
6141                   /* The size of the anonymous object containing
6142                      the bit field is explicit, so use the
6143                      indicated size (in bytes).  */
6144                   anonymous_size = DW_UNSND (attr);
6145                 }
6146               else
6147                 {
6148                   /* The size of the anonymous object containing
6149                      the bit field must be inferred from the type
6150                      attribute of the data member containing the
6151                      bit field.  */
6152                   anonymous_size = TYPE_LENGTH (fp->type);
6153                 }
6154               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6155                 - bit_offset - FIELD_BITSIZE (*fp);
6156             }
6157         }
6158
6159       /* Get name of field.  */
6160       fieldname = dwarf2_name (die, cu);
6161       if (fieldname == NULL)
6162         fieldname = "";
6163
6164       /* The name is already allocated along with this objfile, so we don't
6165          need to duplicate it for the type.  */
6166       fp->name = fieldname;
6167
6168       /* Change accessibility for artificial fields (e.g. virtual table
6169          pointer or virtual base class pointer) to private.  */
6170       if (dwarf2_attr (die, DW_AT_artificial, cu))
6171         {
6172           FIELD_ARTIFICIAL (*fp) = 1;
6173           new_field->accessibility = DW_ACCESS_private;
6174           fip->non_public_fields = 1;
6175         }
6176     }
6177   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6178     {
6179       /* C++ static member.  */
6180
6181       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6182          is a declaration, but all versions of G++ as of this writing
6183          (so through at least 3.2.1) incorrectly generate
6184          DW_TAG_variable tags.  */
6185
6186       char *physname;
6187
6188       /* Get name of field.  */
6189       fieldname = dwarf2_name (die, cu);
6190       if (fieldname == NULL)
6191         return;
6192
6193       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6194       if (attr
6195           /* Only create a symbol if this is an external value.
6196              new_symbol checks this and puts the value in the global symbol
6197              table, which we want.  If it is not external, new_symbol
6198              will try to put the value in cu->list_in_scope which is wrong.  */
6199           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6200         {
6201           /* A static const member, not much different than an enum as far as
6202              we're concerned, except that we can support more types.  */
6203           new_symbol (die, NULL, cu);
6204         }
6205
6206       /* Get physical name.  */
6207       physname = (char *) dwarf2_physname (fieldname, die, cu);
6208
6209       /* The name is already allocated along with this objfile, so we don't
6210          need to duplicate it for the type.  */
6211       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6212       FIELD_TYPE (*fp) = die_type (die, cu);
6213       FIELD_NAME (*fp) = fieldname;
6214     }
6215   else if (die->tag == DW_TAG_inheritance)
6216     {
6217       /* C++ base class field.  */
6218       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6219       if (attr)
6220         {
6221           int byte_offset = 0;
6222
6223           if (attr_form_is_section_offset (attr))
6224             dwarf2_complex_location_expr_complaint ();
6225           else if (attr_form_is_constant (attr))
6226             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6227           else if (attr_form_is_block (attr))
6228             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6229           else
6230             dwarf2_complex_location_expr_complaint ();
6231
6232           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6233         }
6234       FIELD_BITSIZE (*fp) = 0;
6235       FIELD_TYPE (*fp) = die_type (die, cu);
6236       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6237       fip->nbaseclasses++;
6238     }
6239 }
6240
6241 /* Add a typedef defined in the scope of the FIP's class.  */
6242
6243 static void
6244 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6245                     struct dwarf2_cu *cu)
6246 {
6247   struct objfile *objfile = cu->objfile;
6248   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6249   struct typedef_field_list *new_field;
6250   struct attribute *attr;
6251   struct typedef_field *fp;
6252   char *fieldname = "";
6253
6254   /* Allocate a new field list entry and link it in.  */
6255   new_field = xzalloc (sizeof (*new_field));
6256   make_cleanup (xfree, new_field);
6257
6258   gdb_assert (die->tag == DW_TAG_typedef);
6259
6260   fp = &new_field->field;
6261
6262   /* Get name of field.  */
6263   fp->name = dwarf2_name (die, cu);
6264   if (fp->name == NULL)
6265     return;
6266
6267   fp->type = read_type_die (die, cu);
6268
6269   new_field->next = fip->typedef_field_list;
6270   fip->typedef_field_list = new_field;
6271   fip->typedef_field_list_count++;
6272 }
6273
6274 /* Create the vector of fields, and attach it to the type.  */
6275
6276 static void
6277 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6278                               struct dwarf2_cu *cu)
6279 {
6280   int nfields = fip->nfields;
6281
6282   /* Record the field count, allocate space for the array of fields,
6283      and create blank accessibility bitfields if necessary.  */
6284   TYPE_NFIELDS (type) = nfields;
6285   TYPE_FIELDS (type) = (struct field *)
6286     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6287   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6288
6289   if (fip->non_public_fields && cu->language != language_ada)
6290     {
6291       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6292
6293       TYPE_FIELD_PRIVATE_BITS (type) =
6294         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6295       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6296
6297       TYPE_FIELD_PROTECTED_BITS (type) =
6298         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6299       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6300
6301       TYPE_FIELD_IGNORE_BITS (type) =
6302         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6303       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6304     }
6305
6306   /* If the type has baseclasses, allocate and clear a bit vector for
6307      TYPE_FIELD_VIRTUAL_BITS.  */
6308   if (fip->nbaseclasses && cu->language != language_ada)
6309     {
6310       int num_bytes = B_BYTES (fip->nbaseclasses);
6311       unsigned char *pointer;
6312
6313       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6314       pointer = TYPE_ALLOC (type, num_bytes);
6315       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6316       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6317       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6318     }
6319
6320   /* Copy the saved-up fields into the field vector.  Start from the head
6321      of the list, adding to the tail of the field array, so that they end
6322      up in the same order in the array in which they were added to the list.  */
6323   while (nfields-- > 0)
6324     {
6325       struct nextfield *fieldp;
6326
6327       if (fip->fields)
6328         {
6329           fieldp = fip->fields;
6330           fip->fields = fieldp->next;
6331         }
6332       else
6333         {
6334           fieldp = fip->baseclasses;
6335           fip->baseclasses = fieldp->next;
6336         }
6337
6338       TYPE_FIELD (type, nfields) = fieldp->field;
6339       switch (fieldp->accessibility)
6340         {
6341         case DW_ACCESS_private:
6342           if (cu->language != language_ada)
6343             SET_TYPE_FIELD_PRIVATE (type, nfields);
6344           break;
6345
6346         case DW_ACCESS_protected:
6347           if (cu->language != language_ada)
6348             SET_TYPE_FIELD_PROTECTED (type, nfields);
6349           break;
6350
6351         case DW_ACCESS_public:
6352           break;
6353
6354         default:
6355           /* Unknown accessibility.  Complain and treat it as public.  */
6356           {
6357             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6358                        fieldp->accessibility);
6359           }
6360           break;
6361         }
6362       if (nfields < fip->nbaseclasses)
6363         {
6364           switch (fieldp->virtuality)
6365             {
6366             case DW_VIRTUALITY_virtual:
6367             case DW_VIRTUALITY_pure_virtual:
6368               if (cu->language == language_ada)
6369                 error ("unexpected virtuality in component of Ada type");
6370               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6371               break;
6372             }
6373         }
6374     }
6375 }
6376
6377 /* Add a member function to the proper fieldlist.  */
6378
6379 static void
6380 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6381                       struct type *type, struct dwarf2_cu *cu)
6382 {
6383   struct objfile *objfile = cu->objfile;
6384   struct attribute *attr;
6385   struct fnfieldlist *flp;
6386   int i;
6387   struct fn_field *fnp;
6388   char *fieldname;
6389   struct nextfnfield *new_fnfield;
6390   struct type *this_type;
6391
6392   if (cu->language == language_ada)
6393     error ("unexpected member function in Ada type");
6394
6395   /* Get name of member function.  */
6396   fieldname = dwarf2_name (die, cu);
6397   if (fieldname == NULL)
6398     return;
6399
6400   /* Look up member function name in fieldlist.  */
6401   for (i = 0; i < fip->nfnfields; i++)
6402     {
6403       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6404         break;
6405     }
6406
6407   /* Create new list element if necessary.  */
6408   if (i < fip->nfnfields)
6409     flp = &fip->fnfieldlists[i];
6410   else
6411     {
6412       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6413         {
6414           fip->fnfieldlists = (struct fnfieldlist *)
6415             xrealloc (fip->fnfieldlists,
6416                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6417                       * sizeof (struct fnfieldlist));
6418           if (fip->nfnfields == 0)
6419             make_cleanup (free_current_contents, &fip->fnfieldlists);
6420         }
6421       flp = &fip->fnfieldlists[fip->nfnfields];
6422       flp->name = fieldname;
6423       flp->length = 0;
6424       flp->head = NULL;
6425       i = fip->nfnfields++;
6426     }
6427
6428   /* Create a new member function field and chain it to the field list
6429      entry. */
6430   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6431   make_cleanup (xfree, new_fnfield);
6432   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6433   new_fnfield->next = flp->head;
6434   flp->head = new_fnfield;
6435   flp->length++;
6436
6437   /* Fill in the member function field info.  */
6438   fnp = &new_fnfield->fnfield;
6439
6440   /* Delay processing of the physname until later.  */
6441   if (cu->language == language_cplus || cu->language == language_java)
6442     {
6443       add_to_method_list (type, i, flp->length - 1, fieldname,
6444                           die, cu);
6445     }
6446   else
6447     {
6448       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6449       fnp->physname = physname ? physname : "";
6450     }
6451
6452   fnp->type = alloc_type (objfile);
6453   this_type = read_type_die (die, cu);
6454   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6455     {
6456       int nparams = TYPE_NFIELDS (this_type);
6457
6458       /* TYPE is the domain of this method, and THIS_TYPE is the type
6459            of the method itself (TYPE_CODE_METHOD).  */
6460       smash_to_method_type (fnp->type, type,
6461                             TYPE_TARGET_TYPE (this_type),
6462                             TYPE_FIELDS (this_type),
6463                             TYPE_NFIELDS (this_type),
6464                             TYPE_VARARGS (this_type));
6465
6466       /* Handle static member functions.
6467          Dwarf2 has no clean way to discern C++ static and non-static
6468          member functions. G++ helps GDB by marking the first
6469          parameter for non-static member functions (which is the
6470          this pointer) as artificial. We obtain this information
6471          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6472       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6473         fnp->voffset = VOFFSET_STATIC;
6474     }
6475   else
6476     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6477                dwarf2_full_name (fieldname, die, cu));
6478
6479   /* Get fcontext from DW_AT_containing_type if present.  */
6480   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6481     fnp->fcontext = die_containing_type (die, cu);
6482
6483   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6484      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6485
6486   /* Get accessibility.  */
6487   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6488   if (attr)
6489     {
6490       switch (DW_UNSND (attr))
6491         {
6492         case DW_ACCESS_private:
6493           fnp->is_private = 1;
6494           break;
6495         case DW_ACCESS_protected:
6496           fnp->is_protected = 1;
6497           break;
6498         }
6499     }
6500
6501   /* Check for artificial methods.  */
6502   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6503   if (attr && DW_UNSND (attr) != 0)
6504     fnp->is_artificial = 1;
6505
6506   /* Get index in virtual function table if it is a virtual member
6507      function.  For older versions of GCC, this is an offset in the
6508      appropriate virtual table, as specified by DW_AT_containing_type.
6509      For everyone else, it is an expression to be evaluated relative
6510      to the object address.  */
6511
6512   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6513   if (attr)
6514     {
6515       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6516         {
6517           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6518             {
6519               /* Old-style GCC.  */
6520               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6521             }
6522           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6523                    || (DW_BLOCK (attr)->size > 1
6524                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6525                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6526             {
6527               struct dwarf_block blk;
6528               int offset;
6529
6530               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6531                         ? 1 : 2);
6532               blk.size = DW_BLOCK (attr)->size - offset;
6533               blk.data = DW_BLOCK (attr)->data + offset;
6534               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6535               if ((fnp->voffset % cu->header.addr_size) != 0)
6536                 dwarf2_complex_location_expr_complaint ();
6537               else
6538                 fnp->voffset /= cu->header.addr_size;
6539               fnp->voffset += 2;
6540             }
6541           else
6542             dwarf2_complex_location_expr_complaint ();
6543
6544           if (!fnp->fcontext)
6545             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6546         }
6547       else if (attr_form_is_section_offset (attr))
6548         {
6549           dwarf2_complex_location_expr_complaint ();
6550         }
6551       else
6552         {
6553           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6554                                                  fieldname);
6555         }
6556     }
6557   else
6558     {
6559       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6560       if (attr && DW_UNSND (attr))
6561         {
6562           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6563           complaint (&symfile_complaints,
6564                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6565                      fieldname, die->offset);
6566           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6567           TYPE_CPLUS_DYNAMIC (type) = 1;
6568         }
6569     }
6570 }
6571
6572 /* Create the vector of member function fields, and attach it to the type.  */
6573
6574 static void
6575 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6576                                  struct dwarf2_cu *cu)
6577 {
6578   struct fnfieldlist *flp;
6579   int total_length = 0;
6580   int i;
6581
6582   if (cu->language == language_ada)
6583     error ("unexpected member functions in Ada type");
6584
6585   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6586   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6587     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6588
6589   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6590     {
6591       struct nextfnfield *nfp = flp->head;
6592       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6593       int k;
6594
6595       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6596       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6597       fn_flp->fn_fields = (struct fn_field *)
6598         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6599       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6600         fn_flp->fn_fields[k] = nfp->fnfield;
6601
6602       total_length += flp->length;
6603     }
6604
6605   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6606   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6607 }
6608
6609 /* Returns non-zero if NAME is the name of a vtable member in CU's
6610    language, zero otherwise.  */
6611 static int
6612 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6613 {
6614   static const char vptr[] = "_vptr";
6615   static const char vtable[] = "vtable";
6616
6617   /* Look for the C++ and Java forms of the vtable.  */
6618   if ((cu->language == language_java
6619        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6620        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6621        && is_cplus_marker (name[sizeof (vptr) - 1])))
6622     return 1;
6623
6624   return 0;
6625 }
6626
6627 /* GCC outputs unnamed structures that are really pointers to member
6628    functions, with the ABI-specified layout.  If TYPE describes
6629    such a structure, smash it into a member function type.
6630
6631    GCC shouldn't do this; it should just output pointer to member DIEs.
6632    This is GCC PR debug/28767.  */
6633
6634 static void
6635 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6636 {
6637   struct type *pfn_type, *domain_type, *new_type;
6638
6639   /* Check for a structure with no name and two children.  */
6640   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6641     return;
6642
6643   /* Check for __pfn and __delta members.  */
6644   if (TYPE_FIELD_NAME (type, 0) == NULL
6645       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6646       || TYPE_FIELD_NAME (type, 1) == NULL
6647       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6648     return;
6649
6650   /* Find the type of the method.  */
6651   pfn_type = TYPE_FIELD_TYPE (type, 0);
6652   if (pfn_type == NULL
6653       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6654       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6655     return;
6656
6657   /* Look for the "this" argument.  */
6658   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6659   if (TYPE_NFIELDS (pfn_type) == 0
6660       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6661       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6662     return;
6663
6664   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6665   new_type = alloc_type (objfile);
6666   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6667                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6668                         TYPE_VARARGS (pfn_type));
6669   smash_to_methodptr_type (type, new_type);
6670 }
6671
6672 /* Called when we find the DIE that starts a structure or union scope
6673    (definition) to create a type for the structure or union.  Fill in
6674    the type's name and general properties; the members will not be
6675    processed until process_structure_type.
6676
6677    NOTE: we need to call these functions regardless of whether or not the
6678    DIE has a DW_AT_name attribute, since it might be an anonymous
6679    structure or union.  This gets the type entered into our set of
6680    user defined types.
6681
6682    However, if the structure is incomplete (an opaque struct/union)
6683    then suppress creating a symbol table entry for it since gdb only
6684    wants to find the one with the complete definition.  Note that if
6685    it is complete, we just call new_symbol, which does it's own
6686    checking about whether the struct/union is anonymous or not (and
6687    suppresses creating a symbol table entry itself).  */
6688
6689 static struct type *
6690 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6691 {
6692   struct objfile *objfile = cu->objfile;
6693   struct type *type;
6694   struct attribute *attr;
6695   char *name;
6696
6697   /* If the definition of this type lives in .debug_types, read that type.
6698      Don't follow DW_AT_specification though, that will take us back up
6699      the chain and we want to go down.  */
6700   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6701   if (attr)
6702     {
6703       struct dwarf2_cu *type_cu = cu;
6704       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6705
6706       /* We could just recurse on read_structure_type, but we need to call
6707          get_die_type to ensure only one type for this DIE is created.
6708          This is important, for example, because for c++ classes we need
6709          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6710       type = read_type_die (type_die, type_cu);
6711
6712       /* TYPE_CU may not be the same as CU.
6713          Ensure TYPE is recorded in CU's type_hash table.  */
6714       return set_die_type (die, type, cu);
6715     }
6716
6717   type = alloc_type (objfile);
6718   INIT_CPLUS_SPECIFIC (type);
6719
6720   name = dwarf2_name (die, cu);
6721   if (name != NULL)
6722     {
6723       if (cu->language == language_cplus
6724           || cu->language == language_java)
6725         {
6726           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6727
6728           /* dwarf2_full_name might have already finished building the DIE's
6729              type.  If so, there is no need to continue.  */
6730           if (get_die_type (die, cu) != NULL)
6731             return get_die_type (die, cu);
6732
6733           TYPE_TAG_NAME (type) = full_name;
6734           if (die->tag == DW_TAG_structure_type
6735               || die->tag == DW_TAG_class_type)
6736             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6737         }
6738       else
6739         {
6740           /* The name is already allocated along with this objfile, so
6741              we don't need to duplicate it for the type.  */
6742           TYPE_TAG_NAME (type) = (char *) name;
6743           if (die->tag == DW_TAG_class_type)
6744             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6745         }
6746     }
6747
6748   if (die->tag == DW_TAG_structure_type)
6749     {
6750       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6751     }
6752   else if (die->tag == DW_TAG_union_type)
6753     {
6754       TYPE_CODE (type) = TYPE_CODE_UNION;
6755     }
6756   else
6757     {
6758       TYPE_CODE (type) = TYPE_CODE_CLASS;
6759     }
6760
6761   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6762     TYPE_DECLARED_CLASS (type) = 1;
6763
6764   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6765   if (attr)
6766     {
6767       TYPE_LENGTH (type) = DW_UNSND (attr);
6768     }
6769   else
6770     {
6771       TYPE_LENGTH (type) = 0;
6772     }
6773
6774   TYPE_STUB_SUPPORTED (type) = 1;
6775   if (die_is_declaration (die, cu))
6776     TYPE_STUB (type) = 1;
6777   else if (attr == NULL && die->child == NULL
6778            && producer_is_realview (cu->producer))
6779     /* RealView does not output the required DW_AT_declaration
6780        on incomplete types.  */
6781     TYPE_STUB (type) = 1;
6782
6783   /* We need to add the type field to the die immediately so we don't
6784      infinitely recurse when dealing with pointers to the structure
6785      type within the structure itself. */
6786   set_die_type (die, type, cu);
6787
6788   /* set_die_type should be already done.  */
6789   set_descriptive_type (type, die, cu);
6790
6791   return type;
6792 }
6793
6794 /* Finish creating a structure or union type, including filling in
6795    its members and creating a symbol for it.  */
6796
6797 static void
6798 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6799 {
6800   struct objfile *objfile = cu->objfile;
6801   struct die_info *child_die = die->child;
6802   struct type *type;
6803
6804   type = get_die_type (die, cu);
6805   if (type == NULL)
6806     type = read_structure_type (die, cu);
6807
6808   if (die->child != NULL && ! die_is_declaration (die, cu))
6809     {
6810       struct field_info fi;
6811       struct die_info *child_die;
6812       VEC (symbolp) *template_args = NULL;
6813       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6814
6815       memset (&fi, 0, sizeof (struct field_info));
6816
6817       child_die = die->child;
6818
6819       while (child_die && child_die->tag)
6820         {
6821           if (child_die->tag == DW_TAG_member
6822               || child_die->tag == DW_TAG_variable)
6823             {
6824               /* NOTE: carlton/2002-11-05: A C++ static data member
6825                  should be a DW_TAG_member that is a declaration, but
6826                  all versions of G++ as of this writing (so through at
6827                  least 3.2.1) incorrectly generate DW_TAG_variable
6828                  tags for them instead.  */
6829               dwarf2_add_field (&fi, child_die, cu);
6830             }
6831           else if (child_die->tag == DW_TAG_subprogram)
6832             {
6833               /* C++ member function. */
6834               dwarf2_add_member_fn (&fi, child_die, type, cu);
6835             }
6836           else if (child_die->tag == DW_TAG_inheritance)
6837             {
6838               /* C++ base class field.  */
6839               dwarf2_add_field (&fi, child_die, cu);
6840             }
6841           else if (child_die->tag == DW_TAG_typedef)
6842             dwarf2_add_typedef (&fi, child_die, cu);
6843           else if (child_die->tag == DW_TAG_template_type_param
6844                    || child_die->tag == DW_TAG_template_value_param)
6845             {
6846               struct symbol *arg = new_symbol (child_die, NULL, cu);
6847
6848               if (arg != NULL)
6849                 VEC_safe_push (symbolp, template_args, arg);
6850             }
6851
6852           child_die = sibling_die (child_die);
6853         }
6854
6855       /* Attach template arguments to type.  */
6856       if (! VEC_empty (symbolp, template_args))
6857         {
6858           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6859           TYPE_N_TEMPLATE_ARGUMENTS (type)
6860             = VEC_length (symbolp, template_args);
6861           TYPE_TEMPLATE_ARGUMENTS (type)
6862             = obstack_alloc (&objfile->objfile_obstack,
6863                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
6864                               * sizeof (struct symbol *)));
6865           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6866                   VEC_address (symbolp, template_args),
6867                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
6868                    * sizeof (struct symbol *)));
6869           VEC_free (symbolp, template_args);
6870         }
6871
6872       /* Attach fields and member functions to the type.  */
6873       if (fi.nfields)
6874         dwarf2_attach_fields_to_type (&fi, type, cu);
6875       if (fi.nfnfields)
6876         {
6877           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6878
6879           /* Get the type which refers to the base class (possibly this
6880              class itself) which contains the vtable pointer for the current
6881              class from the DW_AT_containing_type attribute.  This use of
6882              DW_AT_containing_type is a GNU extension.  */
6883
6884           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6885             {
6886               struct type *t = die_containing_type (die, cu);
6887
6888               TYPE_VPTR_BASETYPE (type) = t;
6889               if (type == t)
6890                 {
6891                   int i;
6892
6893                   /* Our own class provides vtbl ptr.  */
6894                   for (i = TYPE_NFIELDS (t) - 1;
6895                        i >= TYPE_N_BASECLASSES (t);
6896                        --i)
6897                     {
6898                       char *fieldname = TYPE_FIELD_NAME (t, i);
6899
6900                       if (is_vtable_name (fieldname, cu))
6901                         {
6902                           TYPE_VPTR_FIELDNO (type) = i;
6903                           break;
6904                         }
6905                     }
6906
6907                   /* Complain if virtual function table field not found.  */
6908                   if (i < TYPE_N_BASECLASSES (t))
6909                     complaint (&symfile_complaints,
6910                                _("virtual function table pointer not found when defining class '%s'"),
6911                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6912                                "");
6913                 }
6914               else
6915                 {
6916                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6917                 }
6918             }
6919           else if (cu->producer
6920                    && strncmp (cu->producer,
6921                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6922             {
6923               /* The IBM XLC compiler does not provide direct indication
6924                  of the containing type, but the vtable pointer is
6925                  always named __vfp.  */
6926
6927               int i;
6928
6929               for (i = TYPE_NFIELDS (type) - 1;
6930                    i >= TYPE_N_BASECLASSES (type);
6931                    --i)
6932                 {
6933                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6934                     {
6935                       TYPE_VPTR_FIELDNO (type) = i;
6936                       TYPE_VPTR_BASETYPE (type) = type;
6937                       break;
6938                     }
6939                 }
6940             }
6941         }
6942
6943       /* Copy fi.typedef_field_list linked list elements content into the
6944          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6945       if (fi.typedef_field_list)
6946         {
6947           int i = fi.typedef_field_list_count;
6948
6949           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6950           TYPE_TYPEDEF_FIELD_ARRAY (type)
6951             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6952           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6953
6954           /* Reverse the list order to keep the debug info elements order.  */
6955           while (--i >= 0)
6956             {
6957               struct typedef_field *dest, *src;
6958
6959               dest = &TYPE_TYPEDEF_FIELD (type, i);
6960               src = &fi.typedef_field_list->field;
6961               fi.typedef_field_list = fi.typedef_field_list->next;
6962               *dest = *src;
6963             }
6964         }
6965
6966       do_cleanups (back_to);
6967     }
6968
6969   quirk_gcc_member_function_pointer (type, cu->objfile);
6970
6971   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6972      snapshots) has been known to create a die giving a declaration
6973      for a class that has, as a child, a die giving a definition for a
6974      nested class.  So we have to process our children even if the
6975      current die is a declaration.  Normally, of course, a declaration
6976      won't have any children at all.  */
6977
6978   while (child_die != NULL && child_die->tag)
6979     {
6980       if (child_die->tag == DW_TAG_member
6981           || child_die->tag == DW_TAG_variable
6982           || child_die->tag == DW_TAG_inheritance
6983           || child_die->tag == DW_TAG_template_value_param
6984           || child_die->tag == DW_TAG_template_type_param)
6985         {
6986           /* Do nothing.  */
6987         }
6988       else
6989         process_die (child_die, cu);
6990
6991       child_die = sibling_die (child_die);
6992     }
6993
6994   /* Do not consider external references.  According to the DWARF standard,
6995      these DIEs are identified by the fact that they have no byte_size
6996      attribute, and a declaration attribute.  */
6997   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6998       || !die_is_declaration (die, cu))
6999     new_symbol (die, type, cu);
7000 }
7001
7002 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7003    complete the type's fields yet, or create any symbols.  */
7004
7005 static struct type *
7006 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7007 {
7008   struct objfile *objfile = cu->objfile;
7009   struct type *type;
7010   struct attribute *attr;
7011   const char *name;
7012
7013   /* If the definition of this type lives in .debug_types, read that type.
7014      Don't follow DW_AT_specification though, that will take us back up
7015      the chain and we want to go down.  */
7016   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7017   if (attr)
7018     {
7019       struct dwarf2_cu *type_cu = cu;
7020       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7021
7022       type = read_type_die (type_die, type_cu);
7023
7024       /* TYPE_CU may not be the same as CU.
7025          Ensure TYPE is recorded in CU's type_hash table.  */
7026       return set_die_type (die, type, cu);
7027     }
7028
7029   type = alloc_type (objfile);
7030
7031   TYPE_CODE (type) = TYPE_CODE_ENUM;
7032   name = dwarf2_full_name (NULL, die, cu);
7033   if (name != NULL)
7034     TYPE_TAG_NAME (type) = (char *) name;
7035
7036   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7037   if (attr)
7038     {
7039       TYPE_LENGTH (type) = DW_UNSND (attr);
7040     }
7041   else
7042     {
7043       TYPE_LENGTH (type) = 0;
7044     }
7045
7046   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7047      declared as private in the package spec, and then defined only
7048      inside the package body.  Such types are known as Taft Amendment
7049      Types.  When another package uses such a type, an incomplete DIE
7050      may be generated by the compiler.  */
7051   if (die_is_declaration (die, cu))
7052     TYPE_STUB (type) = 1;
7053
7054   return set_die_type (die, type, cu);
7055 }
7056
7057 /* Given a pointer to a die which begins an enumeration, process all
7058    the dies that define the members of the enumeration, and create the
7059    symbol for the enumeration type.
7060
7061    NOTE: We reverse the order of the element list.  */
7062
7063 static void
7064 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7065 {
7066   struct type *this_type;
7067
7068   this_type = get_die_type (die, cu);
7069   if (this_type == NULL)
7070     this_type = read_enumeration_type (die, cu);
7071
7072   if (die->child != NULL)
7073     {
7074       struct die_info *child_die;
7075       struct symbol *sym;
7076       struct field *fields = NULL;
7077       int num_fields = 0;
7078       int unsigned_enum = 1;
7079       char *name;
7080
7081       child_die = die->child;
7082       while (child_die && child_die->tag)
7083         {
7084           if (child_die->tag != DW_TAG_enumerator)
7085             {
7086               process_die (child_die, cu);
7087             }
7088           else
7089             {
7090               name = dwarf2_name (child_die, cu);
7091               if (name)
7092                 {
7093                   sym = new_symbol (child_die, this_type, cu);
7094                   if (SYMBOL_VALUE (sym) < 0)
7095                     unsigned_enum = 0;
7096
7097                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7098                     {
7099                       fields = (struct field *)
7100                         xrealloc (fields,
7101                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7102                                   * sizeof (struct field));
7103                     }
7104
7105                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7106                   FIELD_TYPE (fields[num_fields]) = NULL;
7107                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7108                   FIELD_BITSIZE (fields[num_fields]) = 0;
7109
7110                   num_fields++;
7111                 }
7112             }
7113
7114           child_die = sibling_die (child_die);
7115         }
7116
7117       if (num_fields)
7118         {
7119           TYPE_NFIELDS (this_type) = num_fields;
7120           TYPE_FIELDS (this_type) = (struct field *)
7121             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7122           memcpy (TYPE_FIELDS (this_type), fields,
7123                   sizeof (struct field) * num_fields);
7124           xfree (fields);
7125         }
7126       if (unsigned_enum)
7127         TYPE_UNSIGNED (this_type) = 1;
7128     }
7129
7130   new_symbol (die, this_type, cu);
7131 }
7132
7133 /* Extract all information from a DW_TAG_array_type DIE and put it in
7134    the DIE's type field.  For now, this only handles one dimensional
7135    arrays.  */
7136
7137 static struct type *
7138 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7139 {
7140   struct objfile *objfile = cu->objfile;
7141   struct die_info *child_die;
7142   struct type *type;
7143   struct type *element_type, *range_type, *index_type;
7144   struct type **range_types = NULL;
7145   struct attribute *attr;
7146   int ndim = 0;
7147   struct cleanup *back_to;
7148   char *name;
7149
7150   element_type = die_type (die, cu);
7151
7152   /* The die_type call above may have already set the type for this DIE.  */
7153   type = get_die_type (die, cu);
7154   if (type)
7155     return type;
7156
7157   /* Irix 6.2 native cc creates array types without children for
7158      arrays with unspecified length.  */
7159   if (die->child == NULL)
7160     {
7161       index_type = objfile_type (objfile)->builtin_int;
7162       range_type = create_range_type (NULL, index_type, 0, -1);
7163       type = create_array_type (NULL, element_type, range_type);
7164       return set_die_type (die, type, cu);
7165     }
7166
7167   back_to = make_cleanup (null_cleanup, NULL);
7168   child_die = die->child;
7169   while (child_die && child_die->tag)
7170     {
7171       if (child_die->tag == DW_TAG_subrange_type)
7172         {
7173           struct type *child_type = read_type_die (child_die, cu);
7174
7175           if (child_type != NULL)
7176             {
7177               /* The range type was succesfully read. Save it for
7178                  the array type creation.  */
7179               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7180                 {
7181                   range_types = (struct type **)
7182                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7183                               * sizeof (struct type *));
7184                   if (ndim == 0)
7185                     make_cleanup (free_current_contents, &range_types);
7186                 }
7187               range_types[ndim++] = child_type;
7188             }
7189         }
7190       child_die = sibling_die (child_die);
7191     }
7192
7193   /* Dwarf2 dimensions are output from left to right, create the
7194      necessary array types in backwards order.  */
7195
7196   type = element_type;
7197
7198   if (read_array_order (die, cu) == DW_ORD_col_major)
7199     {
7200       int i = 0;
7201
7202       while (i < ndim)
7203         type = create_array_type (NULL, type, range_types[i++]);
7204     }
7205   else
7206     {
7207       while (ndim-- > 0)
7208         type = create_array_type (NULL, type, range_types[ndim]);
7209     }
7210
7211   /* Understand Dwarf2 support for vector types (like they occur on
7212      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7213      array type.  This is not part of the Dwarf2/3 standard yet, but a
7214      custom vendor extension.  The main difference between a regular
7215      array and the vector variant is that vectors are passed by value
7216      to functions.  */
7217   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7218   if (attr)
7219     make_vector_type (type);
7220
7221   name = dwarf2_name (die, cu);
7222   if (name)
7223     TYPE_NAME (type) = name;
7224
7225   /* Install the type in the die. */
7226   set_die_type (die, type, cu);
7227
7228   /* set_die_type should be already done.  */
7229   set_descriptive_type (type, die, cu);
7230
7231   do_cleanups (back_to);
7232
7233   return type;
7234 }
7235
7236 static enum dwarf_array_dim_ordering
7237 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7238 {
7239   struct attribute *attr;
7240
7241   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7242
7243   if (attr) return DW_SND (attr);
7244
7245   /*
7246     GNU F77 is a special case, as at 08/2004 array type info is the
7247     opposite order to the dwarf2 specification, but data is still
7248     laid out as per normal fortran.
7249
7250     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7251     version checking.
7252   */
7253
7254   if (cu->language == language_fortran
7255       && cu->producer && strstr (cu->producer, "GNU F77"))
7256     {
7257       return DW_ORD_row_major;
7258     }
7259
7260   switch (cu->language_defn->la_array_ordering)
7261     {
7262     case array_column_major:
7263       return DW_ORD_col_major;
7264     case array_row_major:
7265     default:
7266       return DW_ORD_row_major;
7267     };
7268 }
7269
7270 /* Extract all information from a DW_TAG_set_type DIE and put it in
7271    the DIE's type field. */
7272
7273 static struct type *
7274 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7275 {
7276   struct type *domain_type, *set_type;
7277   struct attribute *attr;
7278
7279   domain_type = die_type (die, cu);
7280
7281   /* The die_type call above may have already set the type for this DIE.  */
7282   set_type = get_die_type (die, cu);
7283   if (set_type)
7284     return set_type;
7285
7286   set_type = create_set_type (NULL, domain_type);
7287
7288   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7289   if (attr)
7290     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7291
7292   return set_die_type (die, set_type, cu);
7293 }
7294
7295 /* First cut: install each common block member as a global variable.  */
7296
7297 static void
7298 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7299 {
7300   struct die_info *child_die;
7301   struct attribute *attr;
7302   struct symbol *sym;
7303   CORE_ADDR base = (CORE_ADDR) 0;
7304
7305   attr = dwarf2_attr (die, DW_AT_location, cu);
7306   if (attr)
7307     {
7308       /* Support the .debug_loc offsets */
7309       if (attr_form_is_block (attr))
7310         {
7311           base = decode_locdesc (DW_BLOCK (attr), cu);
7312         }
7313       else if (attr_form_is_section_offset (attr))
7314         {
7315           dwarf2_complex_location_expr_complaint ();
7316         }
7317       else
7318         {
7319           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7320                                                  "common block member");
7321         }
7322     }
7323   if (die->child != NULL)
7324     {
7325       child_die = die->child;
7326       while (child_die && child_die->tag)
7327         {
7328           sym = new_symbol (child_die, NULL, cu);
7329           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7330           if (sym != NULL && attr != NULL)
7331             {
7332               CORE_ADDR byte_offset = 0;
7333
7334               if (attr_form_is_section_offset (attr))
7335                 dwarf2_complex_location_expr_complaint ();
7336               else if (attr_form_is_constant (attr))
7337                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7338               else if (attr_form_is_block (attr))
7339                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7340               else
7341                 dwarf2_complex_location_expr_complaint ();
7342
7343               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7344               add_symbol_to_list (sym, &global_symbols);
7345             }
7346           child_die = sibling_die (child_die);
7347         }
7348     }
7349 }
7350
7351 /* Create a type for a C++ namespace.  */
7352
7353 static struct type *
7354 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7355 {
7356   struct objfile *objfile = cu->objfile;
7357   const char *previous_prefix, *name;
7358   int is_anonymous;
7359   struct type *type;
7360
7361   /* For extensions, reuse the type of the original namespace.  */
7362   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7363     {
7364       struct die_info *ext_die;
7365       struct dwarf2_cu *ext_cu = cu;
7366
7367       ext_die = dwarf2_extension (die, &ext_cu);
7368       type = read_type_die (ext_die, ext_cu);
7369
7370       /* EXT_CU may not be the same as CU.
7371          Ensure TYPE is recorded in CU's type_hash table.  */
7372       return set_die_type (die, type, cu);
7373     }
7374
7375   name = namespace_name (die, &is_anonymous, cu);
7376
7377   /* Now build the name of the current namespace.  */
7378
7379   previous_prefix = determine_prefix (die, cu);
7380   if (previous_prefix[0] != '\0')
7381     name = typename_concat (&objfile->objfile_obstack,
7382                             previous_prefix, name, 0, cu);
7383
7384   /* Create the type.  */
7385   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7386                     objfile);
7387   TYPE_NAME (type) = (char *) name;
7388   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7389
7390   return set_die_type (die, type, cu);
7391 }
7392
7393 /* Read a C++ namespace.  */
7394
7395 static void
7396 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7397 {
7398   struct objfile *objfile = cu->objfile;
7399   const char *name;
7400   int is_anonymous;
7401
7402   /* Add a symbol associated to this if we haven't seen the namespace
7403      before.  Also, add a using directive if it's an anonymous
7404      namespace.  */
7405
7406   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7407     {
7408       struct type *type;
7409
7410       type = read_type_die (die, cu);
7411       new_symbol (die, type, cu);
7412
7413       name = namespace_name (die, &is_anonymous, cu);
7414       if (is_anonymous)
7415         {
7416           const char *previous_prefix = determine_prefix (die, cu);
7417
7418           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7419                                   NULL, &objfile->objfile_obstack);
7420         }
7421     }
7422
7423   if (die->child != NULL)
7424     {
7425       struct die_info *child_die = die->child;
7426
7427       while (child_die && child_die->tag)
7428         {
7429           process_die (child_die, cu);
7430           child_die = sibling_die (child_die);
7431         }
7432     }
7433 }
7434
7435 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7436    imported module.  Still we need that type as local Fortran "use ... only"
7437    declaration imports depend on the created type in determine_prefix.  */
7438
7439 static struct type *
7440 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7441 {
7442   struct objfile *objfile = cu->objfile;
7443   char *module_name;
7444   struct type *type;
7445
7446   module_name = dwarf2_name (die, cu);
7447   if (!module_name)
7448     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7449                die->offset);
7450   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7451
7452   /* determine_prefix uses TYPE_TAG_NAME.  */
7453   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7454
7455   return set_die_type (die, type, cu);
7456 }
7457
7458 /* Read a Fortran module.  */
7459
7460 static void
7461 read_module (struct die_info *die, struct dwarf2_cu *cu)
7462 {
7463   struct die_info *child_die = die->child;
7464
7465   while (child_die && child_die->tag)
7466     {
7467       process_die (child_die, cu);
7468       child_die = sibling_die (child_die);
7469     }
7470 }
7471
7472 /* Return the name of the namespace represented by DIE.  Set
7473    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7474    namespace.  */
7475
7476 static const char *
7477 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7478 {
7479   struct die_info *current_die;
7480   const char *name = NULL;
7481
7482   /* Loop through the extensions until we find a name.  */
7483
7484   for (current_die = die;
7485        current_die != NULL;
7486        current_die = dwarf2_extension (die, &cu))
7487     {
7488       name = dwarf2_name (current_die, cu);
7489       if (name != NULL)
7490         break;
7491     }
7492
7493   /* Is it an anonymous namespace?  */
7494
7495   *is_anonymous = (name == NULL);
7496   if (*is_anonymous)
7497     name = "(anonymous namespace)";
7498
7499   return name;
7500 }
7501
7502 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7503    the user defined type vector.  */
7504
7505 static struct type *
7506 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7507 {
7508   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7509   struct comp_unit_head *cu_header = &cu->header;
7510   struct type *type;
7511   struct attribute *attr_byte_size;
7512   struct attribute *attr_address_class;
7513   int byte_size, addr_class;
7514   struct type *target_type;
7515
7516   target_type = die_type (die, cu);
7517
7518   /* The die_type call above may have already set the type for this DIE.  */
7519   type = get_die_type (die, cu);
7520   if (type)
7521     return type;
7522
7523   type = lookup_pointer_type (target_type);
7524
7525   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7526   if (attr_byte_size)
7527     byte_size = DW_UNSND (attr_byte_size);
7528   else
7529     byte_size = cu_header->addr_size;
7530
7531   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7532   if (attr_address_class)
7533     addr_class = DW_UNSND (attr_address_class);
7534   else
7535     addr_class = DW_ADDR_none;
7536
7537   /* If the pointer size or address class is different than the
7538      default, create a type variant marked as such and set the
7539      length accordingly.  */
7540   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7541     {
7542       if (gdbarch_address_class_type_flags_p (gdbarch))
7543         {
7544           int type_flags;
7545
7546           type_flags = gdbarch_address_class_type_flags
7547                          (gdbarch, byte_size, addr_class);
7548           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7549                       == 0);
7550           type = make_type_with_address_space (type, type_flags);
7551         }
7552       else if (TYPE_LENGTH (type) != byte_size)
7553         {
7554           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7555         }
7556       else
7557         {
7558           /* Should we also complain about unhandled address classes?  */
7559         }
7560     }
7561
7562   TYPE_LENGTH (type) = byte_size;
7563   return set_die_type (die, type, cu);
7564 }
7565
7566 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7567    the user defined type vector.  */
7568
7569 static struct type *
7570 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7571 {
7572   struct type *type;
7573   struct type *to_type;
7574   struct type *domain;
7575
7576   to_type = die_type (die, cu);
7577   domain = die_containing_type (die, cu);
7578
7579   /* The calls above may have already set the type for this DIE.  */
7580   type = get_die_type (die, cu);
7581   if (type)
7582     return type;
7583
7584   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7585     type = lookup_methodptr_type (to_type);
7586   else
7587     type = lookup_memberptr_type (to_type, domain);
7588
7589   return set_die_type (die, type, cu);
7590 }
7591
7592 /* Extract all information from a DW_TAG_reference_type DIE and add to
7593    the user defined type vector.  */
7594
7595 static struct type *
7596 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7597 {
7598   struct comp_unit_head *cu_header = &cu->header;
7599   struct type *type, *target_type;
7600   struct attribute *attr;
7601
7602   target_type = die_type (die, cu);
7603
7604   /* The die_type call above may have already set the type for this DIE.  */
7605   type = get_die_type (die, cu);
7606   if (type)
7607     return type;
7608
7609   type = lookup_reference_type (target_type);
7610   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7611   if (attr)
7612     {
7613       TYPE_LENGTH (type) = DW_UNSND (attr);
7614     }
7615   else
7616     {
7617       TYPE_LENGTH (type) = cu_header->addr_size;
7618     }
7619   return set_die_type (die, type, cu);
7620 }
7621
7622 static struct type *
7623 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7624 {
7625   struct type *base_type, *cv_type;
7626
7627   base_type = die_type (die, cu);
7628
7629   /* The die_type call above may have already set the type for this DIE.  */
7630   cv_type = get_die_type (die, cu);
7631   if (cv_type)
7632     return cv_type;
7633
7634   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7635   return set_die_type (die, cv_type, cu);
7636 }
7637
7638 static struct type *
7639 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7640 {
7641   struct type *base_type, *cv_type;
7642
7643   base_type = die_type (die, cu);
7644
7645   /* The die_type call above may have already set the type for this DIE.  */
7646   cv_type = get_die_type (die, cu);
7647   if (cv_type)
7648     return cv_type;
7649
7650   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7651   return set_die_type (die, cv_type, cu);
7652 }
7653
7654 /* Extract all information from a DW_TAG_string_type DIE and add to
7655    the user defined type vector.  It isn't really a user defined type,
7656    but it behaves like one, with other DIE's using an AT_user_def_type
7657    attribute to reference it.  */
7658
7659 static struct type *
7660 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7661 {
7662   struct objfile *objfile = cu->objfile;
7663   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7664   struct type *type, *range_type, *index_type, *char_type;
7665   struct attribute *attr;
7666   unsigned int length;
7667
7668   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7669   if (attr)
7670     {
7671       length = DW_UNSND (attr);
7672     }
7673   else
7674     {
7675       /* check for the DW_AT_byte_size attribute */
7676       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7677       if (attr)
7678         {
7679           length = DW_UNSND (attr);
7680         }
7681       else
7682         {
7683           length = 1;
7684         }
7685     }
7686
7687   index_type = objfile_type (objfile)->builtin_int;
7688   range_type = create_range_type (NULL, index_type, 1, length);
7689   char_type = language_string_char_type (cu->language_defn, gdbarch);
7690   type = create_string_type (NULL, char_type, range_type);
7691
7692   return set_die_type (die, type, cu);
7693 }
7694
7695 /* Handle DIES due to C code like:
7696
7697    struct foo
7698    {
7699    int (*funcp)(int a, long l);
7700    int b;
7701    };
7702
7703    ('funcp' generates a DW_TAG_subroutine_type DIE)
7704  */
7705
7706 static struct type *
7707 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7708 {
7709   struct type *type;            /* Type that this function returns */
7710   struct type *ftype;           /* Function that returns above type */
7711   struct attribute *attr;
7712
7713   type = die_type (die, cu);
7714
7715   /* The die_type call above may have already set the type for this DIE.  */
7716   ftype = get_die_type (die, cu);
7717   if (ftype)
7718     return ftype;
7719
7720   ftype = lookup_function_type (type);
7721
7722   /* All functions in C++, Pascal and Java have prototypes.  */
7723   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7724   if ((attr && (DW_UNSND (attr) != 0))
7725       || cu->language == language_cplus
7726       || cu->language == language_java
7727       || cu->language == language_pascal)
7728     TYPE_PROTOTYPED (ftype) = 1;
7729   else if (producer_is_realview (cu->producer))
7730     /* RealView does not emit DW_AT_prototyped.  We can not
7731        distinguish prototyped and unprototyped functions; default to
7732        prototyped, since that is more common in modern code (and
7733        RealView warns about unprototyped functions).  */
7734     TYPE_PROTOTYPED (ftype) = 1;
7735
7736   /* Store the calling convention in the type if it's available in
7737      the subroutine die.  Otherwise set the calling convention to
7738      the default value DW_CC_normal.  */
7739   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7740   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7741
7742   /* We need to add the subroutine type to the die immediately so
7743      we don't infinitely recurse when dealing with parameters
7744      declared as the same subroutine type. */
7745   set_die_type (die, ftype, cu);
7746
7747   if (die->child != NULL)
7748     {
7749       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7750       struct die_info *child_die;
7751       int nparams, iparams;
7752
7753       /* Count the number of parameters.
7754          FIXME: GDB currently ignores vararg functions, but knows about
7755          vararg member functions.  */
7756       nparams = 0;
7757       child_die = die->child;
7758       while (child_die && child_die->tag)
7759         {
7760           if (child_die->tag == DW_TAG_formal_parameter)
7761             nparams++;
7762           else if (child_die->tag == DW_TAG_unspecified_parameters)
7763             TYPE_VARARGS (ftype) = 1;
7764           child_die = sibling_die (child_die);
7765         }
7766
7767       /* Allocate storage for parameters and fill them in.  */
7768       TYPE_NFIELDS (ftype) = nparams;
7769       TYPE_FIELDS (ftype) = (struct field *)
7770         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7771
7772       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7773          even if we error out during the parameters reading below.  */
7774       for (iparams = 0; iparams < nparams; iparams++)
7775         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7776
7777       iparams = 0;
7778       child_die = die->child;
7779       while (child_die && child_die->tag)
7780         {
7781           if (child_die->tag == DW_TAG_formal_parameter)
7782             {
7783               struct type *arg_type;
7784
7785               /* DWARF version 2 has no clean way to discern C++
7786                  static and non-static member functions.  G++ helps
7787                  GDB by marking the first parameter for non-static
7788                  member functions (which is the this pointer) as
7789                  artificial.  We pass this information to
7790                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7791
7792                  DWARF version 3 added DW_AT_object_pointer, which GCC
7793                  4.5 does not yet generate.  */
7794               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7795               if (attr)
7796                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7797               else
7798                 {
7799                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7800
7801                   /* GCC/43521: In java, the formal parameter
7802                      "this" is sometimes not marked with DW_AT_artificial.  */
7803                   if (cu->language == language_java)
7804                     {
7805                       const char *name = dwarf2_name (child_die, cu);
7806
7807                       if (name && !strcmp (name, "this"))
7808                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7809                     }
7810                 }
7811               arg_type = die_type (child_die, cu);
7812
7813               /* RealView does not mark THIS as const, which the testsuite
7814                  expects.  GCC marks THIS as const in method definitions,
7815                  but not in the class specifications (GCC PR 43053).  */
7816               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7817                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7818                 {
7819                   int is_this = 0;
7820                   struct dwarf2_cu *arg_cu = cu;
7821                   const char *name = dwarf2_name (child_die, cu);
7822
7823                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7824                   if (attr)
7825                     {
7826                       /* If the compiler emits this, use it.  */
7827                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
7828                         is_this = 1;
7829                     }
7830                   else if (name && strcmp (name, "this") == 0)
7831                     /* Function definitions will have the argument names.  */
7832                     is_this = 1;
7833                   else if (name == NULL && iparams == 0)
7834                     /* Declarations may not have the names, so like
7835                        elsewhere in GDB, assume an artificial first
7836                        argument is "this".  */
7837                     is_this = 1;
7838
7839                   if (is_this)
7840                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7841                                              arg_type, 0);
7842                 }
7843
7844               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7845               iparams++;
7846             }
7847           child_die = sibling_die (child_die);
7848         }
7849     }
7850
7851   return ftype;
7852 }
7853
7854 static struct type *
7855 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7856 {
7857   struct objfile *objfile = cu->objfile;
7858   const char *name = NULL;
7859   struct type *this_type;
7860
7861   name = dwarf2_full_name (NULL, die, cu);
7862   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7863                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7864   TYPE_NAME (this_type) = (char *) name;
7865   set_die_type (die, this_type, cu);
7866   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7867   return this_type;
7868 }
7869
7870 /* Find a representation of a given base type and install
7871    it in the TYPE field of the die.  */
7872
7873 static struct type *
7874 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7875 {
7876   struct objfile *objfile = cu->objfile;
7877   struct type *type;
7878   struct attribute *attr;
7879   int encoding = 0, size = 0;
7880   char *name;
7881   enum type_code code = TYPE_CODE_INT;
7882   int type_flags = 0;
7883   struct type *target_type = NULL;
7884
7885   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7886   if (attr)
7887     {
7888       encoding = DW_UNSND (attr);
7889     }
7890   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7891   if (attr)
7892     {
7893       size = DW_UNSND (attr);
7894     }
7895   name = dwarf2_name (die, cu);
7896   if (!name)
7897     {
7898       complaint (&symfile_complaints,
7899                  _("DW_AT_name missing from DW_TAG_base_type"));
7900     }
7901
7902   switch (encoding)
7903     {
7904       case DW_ATE_address:
7905         /* Turn DW_ATE_address into a void * pointer.  */
7906         code = TYPE_CODE_PTR;
7907         type_flags |= TYPE_FLAG_UNSIGNED;
7908         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7909         break;
7910       case DW_ATE_boolean:
7911         code = TYPE_CODE_BOOL;
7912         type_flags |= TYPE_FLAG_UNSIGNED;
7913         break;
7914       case DW_ATE_complex_float:
7915         code = TYPE_CODE_COMPLEX;
7916         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7917         break;
7918       case DW_ATE_decimal_float:
7919         code = TYPE_CODE_DECFLOAT;
7920         break;
7921       case DW_ATE_float:
7922         code = TYPE_CODE_FLT;
7923         break;
7924       case DW_ATE_signed:
7925         break;
7926       case DW_ATE_unsigned:
7927         type_flags |= TYPE_FLAG_UNSIGNED;
7928         break;
7929       case DW_ATE_signed_char:
7930         if (cu->language == language_ada || cu->language == language_m2
7931             || cu->language == language_pascal)
7932           code = TYPE_CODE_CHAR;
7933         break;
7934       case DW_ATE_unsigned_char:
7935         if (cu->language == language_ada || cu->language == language_m2
7936             || cu->language == language_pascal)
7937           code = TYPE_CODE_CHAR;
7938         type_flags |= TYPE_FLAG_UNSIGNED;
7939         break;
7940       case DW_ATE_UTF:
7941         /* We just treat this as an integer and then recognize the
7942            type by name elsewhere.  */
7943         break;
7944
7945       default:
7946         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7947                    dwarf_type_encoding_name (encoding));
7948         break;
7949     }
7950
7951   type = init_type (code, size, type_flags, NULL, objfile);
7952   TYPE_NAME (type) = name;
7953   TYPE_TARGET_TYPE (type) = target_type;
7954
7955   if (name && strcmp (name, "char") == 0)
7956     TYPE_NOSIGN (type) = 1;
7957
7958   return set_die_type (die, type, cu);
7959 }
7960
7961 /* Read the given DW_AT_subrange DIE.  */
7962
7963 static struct type *
7964 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7965 {
7966   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7967   struct type *base_type;
7968   struct type *range_type;
7969   struct attribute *attr;
7970   LONGEST low = 0;
7971   LONGEST high = -1;
7972   char *name;
7973   LONGEST negative_mask;
7974
7975   base_type = die_type (die, cu);
7976   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7977   check_typedef (base_type);
7978
7979   /* The die_type call above may have already set the type for this DIE.  */
7980   range_type = get_die_type (die, cu);
7981   if (range_type)
7982     return range_type;
7983
7984   if (cu->language == language_fortran)
7985     {
7986       /* FORTRAN implies a lower bound of 1, if not given.  */
7987       low = 1;
7988     }
7989
7990   /* FIXME: For variable sized arrays either of these could be
7991      a variable rather than a constant value.  We'll allow it,
7992      but we don't know how to handle it.  */
7993   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7994   if (attr)
7995     low = dwarf2_get_attr_constant_value (attr, 0);
7996
7997   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7998   if (attr)
7999     {
8000       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8001         {
8002           /* GCC encodes arrays with unspecified or dynamic length
8003              with a DW_FORM_block1 attribute or a reference attribute.
8004              FIXME: GDB does not yet know how to handle dynamic
8005              arrays properly, treat them as arrays with unspecified
8006              length for now.
8007
8008              FIXME: jimb/2003-09-22: GDB does not really know
8009              how to handle arrays of unspecified length
8010              either; we just represent them as zero-length
8011              arrays.  Choose an appropriate upper bound given
8012              the lower bound we've computed above.  */
8013           high = low - 1;
8014         }
8015       else
8016         high = dwarf2_get_attr_constant_value (attr, 1);
8017     }
8018   else
8019     {
8020       attr = dwarf2_attr (die, DW_AT_count, cu);
8021       if (attr)
8022         {
8023           int count = dwarf2_get_attr_constant_value (attr, 1);
8024           high = low + count - 1;
8025         }
8026     }
8027
8028   /* Dwarf-2 specifications explicitly allows to create subrange types
8029      without specifying a base type.
8030      In that case, the base type must be set to the type of
8031      the lower bound, upper bound or count, in that order, if any of these
8032      three attributes references an object that has a type.
8033      If no base type is found, the Dwarf-2 specifications say that
8034      a signed integer type of size equal to the size of an address should
8035      be used.
8036      For the following C code: `extern char gdb_int [];'
8037      GCC produces an empty range DIE.
8038      FIXME: muller/2010-05-28: Possible references to object for low bound,
8039      high bound or count are not yet handled by this code.
8040   */
8041   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8042     {
8043       struct objfile *objfile = cu->objfile;
8044       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8045       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8046       struct type *int_type = objfile_type (objfile)->builtin_int;
8047
8048       /* Test "int", "long int", and "long long int" objfile types,
8049          and select the first one having a size above or equal to the
8050          architecture address size.  */
8051       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8052         base_type = int_type;
8053       else
8054         {
8055           int_type = objfile_type (objfile)->builtin_long;
8056           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8057             base_type = int_type;
8058           else
8059             {
8060               int_type = objfile_type (objfile)->builtin_long_long;
8061               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8062                 base_type = int_type;
8063             }
8064         }
8065     }
8066
8067   negative_mask =
8068     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8069   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8070     low |= negative_mask;
8071   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8072     high |= negative_mask;
8073
8074   range_type = create_range_type (NULL, base_type, low, high);
8075
8076   /* Mark arrays with dynamic length at least as an array of unspecified
8077      length.  GDB could check the boundary but before it gets implemented at
8078      least allow accessing the array elements.  */
8079   if (attr && attr->form == DW_FORM_block1)
8080     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8081
8082   name = dwarf2_name (die, cu);
8083   if (name)
8084     TYPE_NAME (range_type) = name;
8085
8086   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8087   if (attr)
8088     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8089
8090   set_die_type (die, range_type, cu);
8091
8092   /* set_die_type should be already done.  */
8093   set_descriptive_type (range_type, die, cu);
8094
8095   return range_type;
8096 }
8097
8098 static struct type *
8099 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8100 {
8101   struct type *type;
8102
8103   /* For now, we only support the C meaning of an unspecified type: void.  */
8104
8105   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8106   TYPE_NAME (type) = dwarf2_name (die, cu);
8107
8108   return set_die_type (die, type, cu);
8109 }
8110
8111 /* Trivial hash function for die_info: the hash value of a DIE
8112    is its offset in .debug_info for this objfile.  */
8113
8114 static hashval_t
8115 die_hash (const void *item)
8116 {
8117   const struct die_info *die = item;
8118
8119   return die->offset;
8120 }
8121
8122 /* Trivial comparison function for die_info structures: two DIEs
8123    are equal if they have the same offset.  */
8124
8125 static int
8126 die_eq (const void *item_lhs, const void *item_rhs)
8127 {
8128   const struct die_info *die_lhs = item_lhs;
8129   const struct die_info *die_rhs = item_rhs;
8130
8131   return die_lhs->offset == die_rhs->offset;
8132 }
8133
8134 /* Read a whole compilation unit into a linked list of dies.  */
8135
8136 static struct die_info *
8137 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8138 {
8139   struct die_reader_specs reader_specs;
8140   int read_abbrevs = 0;
8141   struct cleanup *back_to = NULL;
8142   struct die_info *die;
8143
8144   if (cu->dwarf2_abbrevs == NULL)
8145     {
8146       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8147       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8148       read_abbrevs = 1;
8149     }
8150
8151   gdb_assert (cu->die_hash == NULL);
8152   cu->die_hash
8153     = htab_create_alloc_ex (cu->header.length / 12,
8154                             die_hash,
8155                             die_eq,
8156                             NULL,
8157                             &cu->comp_unit_obstack,
8158                             hashtab_obstack_allocate,
8159                             dummy_obstack_deallocate);
8160
8161   init_cu_die_reader (&reader_specs, cu);
8162
8163   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8164
8165   if (read_abbrevs)
8166     do_cleanups (back_to);
8167
8168   return die;
8169 }
8170
8171 /* Main entry point for reading a DIE and all children.
8172    Read the DIE and dump it if requested.  */
8173
8174 static struct die_info *
8175 read_die_and_children (const struct die_reader_specs *reader,
8176                        gdb_byte *info_ptr,
8177                        gdb_byte **new_info_ptr,
8178                        struct die_info *parent)
8179 {
8180   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8181                                                      new_info_ptr, parent);
8182
8183   if (dwarf2_die_debug)
8184     {
8185       fprintf_unfiltered (gdb_stdlog,
8186                           "\nRead die from %s of %s:\n",
8187                           reader->buffer == dwarf2_per_objfile->info.buffer
8188                           ? ".debug_info"
8189                           : reader->buffer == dwarf2_per_objfile->types.buffer
8190                           ? ".debug_types"
8191                           : "unknown section",
8192                           reader->abfd->filename);
8193       dump_die (result, dwarf2_die_debug);
8194     }
8195
8196   return result;
8197 }
8198
8199 /* Read a single die and all its descendents.  Set the die's sibling
8200    field to NULL; set other fields in the die correctly, and set all
8201    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8202    location of the info_ptr after reading all of those dies.  PARENT
8203    is the parent of the die in question.  */
8204
8205 static struct die_info *
8206 read_die_and_children_1 (const struct die_reader_specs *reader,
8207                          gdb_byte *info_ptr,
8208                          gdb_byte **new_info_ptr,
8209                          struct die_info *parent)
8210 {
8211   struct die_info *die;
8212   gdb_byte *cur_ptr;
8213   int has_children;
8214
8215   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8216   if (die == NULL)
8217     {
8218       *new_info_ptr = cur_ptr;
8219       return NULL;
8220     }
8221   store_in_ref_table (die, reader->cu);
8222
8223   if (has_children)
8224     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8225   else
8226     {
8227       die->child = NULL;
8228       *new_info_ptr = cur_ptr;
8229     }
8230
8231   die->sibling = NULL;
8232   die->parent = parent;
8233   return die;
8234 }
8235
8236 /* Read a die, all of its descendents, and all of its siblings; set
8237    all of the fields of all of the dies correctly.  Arguments are as
8238    in read_die_and_children.  */
8239
8240 static struct die_info *
8241 read_die_and_siblings (const struct die_reader_specs *reader,
8242                        gdb_byte *info_ptr,
8243                        gdb_byte **new_info_ptr,
8244                        struct die_info *parent)
8245 {
8246   struct die_info *first_die, *last_sibling;
8247   gdb_byte *cur_ptr;
8248
8249   cur_ptr = info_ptr;
8250   first_die = last_sibling = NULL;
8251
8252   while (1)
8253     {
8254       struct die_info *die
8255         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8256
8257       if (die == NULL)
8258         {
8259           *new_info_ptr = cur_ptr;
8260           return first_die;
8261         }
8262
8263       if (!first_die)
8264         first_die = die;
8265       else
8266         last_sibling->sibling = die;
8267
8268       last_sibling = die;
8269     }
8270 }
8271
8272 /* Read the die from the .debug_info section buffer.  Set DIEP to
8273    point to a newly allocated die with its information, except for its
8274    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8275    whether the die has children or not.  */
8276
8277 static gdb_byte *
8278 read_full_die (const struct die_reader_specs *reader,
8279                struct die_info **diep, gdb_byte *info_ptr,
8280                int *has_children)
8281 {
8282   unsigned int abbrev_number, bytes_read, i, offset;
8283   struct abbrev_info *abbrev;
8284   struct die_info *die;
8285   struct dwarf2_cu *cu = reader->cu;
8286   bfd *abfd = reader->abfd;
8287
8288   offset = info_ptr - reader->buffer;
8289   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8290   info_ptr += bytes_read;
8291   if (!abbrev_number)
8292     {
8293       *diep = NULL;
8294       *has_children = 0;
8295       return info_ptr;
8296     }
8297
8298   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8299   if (!abbrev)
8300     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8301            abbrev_number,
8302            bfd_get_filename (abfd));
8303
8304   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8305   die->offset = offset;
8306   die->tag = abbrev->tag;
8307   die->abbrev = abbrev_number;
8308
8309   die->num_attrs = abbrev->num_attrs;
8310
8311   for (i = 0; i < abbrev->num_attrs; ++i)
8312     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8313                                abfd, info_ptr, cu);
8314
8315   *diep = die;
8316   *has_children = abbrev->has_children;
8317   return info_ptr;
8318 }
8319
8320 /* In DWARF version 2, the description of the debugging information is
8321    stored in a separate .debug_abbrev section.  Before we read any
8322    dies from a section we read in all abbreviations and install them
8323    in a hash table.  This function also sets flags in CU describing
8324    the data found in the abbrev table.  */
8325
8326 static void
8327 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8328 {
8329   struct comp_unit_head *cu_header = &cu->header;
8330   gdb_byte *abbrev_ptr;
8331   struct abbrev_info *cur_abbrev;
8332   unsigned int abbrev_number, bytes_read, abbrev_name;
8333   unsigned int abbrev_form, hash_number;
8334   struct attr_abbrev *cur_attrs;
8335   unsigned int allocated_attrs;
8336
8337   /* Initialize dwarf2 abbrevs */
8338   obstack_init (&cu->abbrev_obstack);
8339   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8340                                       (ABBREV_HASH_SIZE
8341                                        * sizeof (struct abbrev_info *)));
8342   memset (cu->dwarf2_abbrevs, 0,
8343           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8344
8345   dwarf2_read_section (dwarf2_per_objfile->objfile,
8346                        &dwarf2_per_objfile->abbrev);
8347   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8348   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8349   abbrev_ptr += bytes_read;
8350
8351   allocated_attrs = ATTR_ALLOC_CHUNK;
8352   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8353
8354   /* loop until we reach an abbrev number of 0 */
8355   while (abbrev_number)
8356     {
8357       cur_abbrev = dwarf_alloc_abbrev (cu);
8358
8359       /* read in abbrev header */
8360       cur_abbrev->number = abbrev_number;
8361       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8362       abbrev_ptr += bytes_read;
8363       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8364       abbrev_ptr += 1;
8365
8366       if (cur_abbrev->tag == DW_TAG_namespace)
8367         cu->has_namespace_info = 1;
8368
8369       /* now read in declarations */
8370       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8371       abbrev_ptr += bytes_read;
8372       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8373       abbrev_ptr += bytes_read;
8374       while (abbrev_name)
8375         {
8376           if (cur_abbrev->num_attrs == allocated_attrs)
8377             {
8378               allocated_attrs += ATTR_ALLOC_CHUNK;
8379               cur_attrs
8380                 = xrealloc (cur_attrs, (allocated_attrs
8381                                         * sizeof (struct attr_abbrev)));
8382             }
8383
8384           /* Record whether this compilation unit might have
8385              inter-compilation-unit references.  If we don't know what form
8386              this attribute will have, then it might potentially be a
8387              DW_FORM_ref_addr, so we conservatively expect inter-CU
8388              references.  */
8389
8390           if (abbrev_form == DW_FORM_ref_addr
8391               || abbrev_form == DW_FORM_indirect)
8392             cu->has_form_ref_addr = 1;
8393
8394           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8395           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8396           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8397           abbrev_ptr += bytes_read;
8398           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8399           abbrev_ptr += bytes_read;
8400         }
8401
8402       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8403                                          (cur_abbrev->num_attrs
8404                                           * sizeof (struct attr_abbrev)));
8405       memcpy (cur_abbrev->attrs, cur_attrs,
8406               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8407
8408       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8409       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8410       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8411
8412       /* Get next abbreviation.
8413          Under Irix6 the abbreviations for a compilation unit are not
8414          always properly terminated with an abbrev number of 0.
8415          Exit loop if we encounter an abbreviation which we have
8416          already read (which means we are about to read the abbreviations
8417          for the next compile unit) or if the end of the abbreviation
8418          table is reached.  */
8419       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8420           >= dwarf2_per_objfile->abbrev.size)
8421         break;
8422       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8423       abbrev_ptr += bytes_read;
8424       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8425         break;
8426     }
8427
8428   xfree (cur_attrs);
8429 }
8430
8431 /* Release the memory used by the abbrev table for a compilation unit.  */
8432
8433 static void
8434 dwarf2_free_abbrev_table (void *ptr_to_cu)
8435 {
8436   struct dwarf2_cu *cu = ptr_to_cu;
8437
8438   obstack_free (&cu->abbrev_obstack, NULL);
8439   cu->dwarf2_abbrevs = NULL;
8440 }
8441
8442 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8443
8444 static struct abbrev_info *
8445 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8446 {
8447   unsigned int hash_number;
8448   struct abbrev_info *abbrev;
8449
8450   hash_number = number % ABBREV_HASH_SIZE;
8451   abbrev = cu->dwarf2_abbrevs[hash_number];
8452
8453   while (abbrev)
8454     {
8455       if (abbrev->number == number)
8456         return abbrev;
8457       else
8458         abbrev = abbrev->next;
8459     }
8460   return NULL;
8461 }
8462
8463 /* Returns nonzero if TAG represents a type that we might generate a partial
8464    symbol for.  */
8465
8466 static int
8467 is_type_tag_for_partial (int tag)
8468 {
8469   switch (tag)
8470     {
8471 #if 0
8472     /* Some types that would be reasonable to generate partial symbols for,
8473        that we don't at present.  */
8474     case DW_TAG_array_type:
8475     case DW_TAG_file_type:
8476     case DW_TAG_ptr_to_member_type:
8477     case DW_TAG_set_type:
8478     case DW_TAG_string_type:
8479     case DW_TAG_subroutine_type:
8480 #endif
8481     case DW_TAG_base_type:
8482     case DW_TAG_class_type:
8483     case DW_TAG_interface_type:
8484     case DW_TAG_enumeration_type:
8485     case DW_TAG_structure_type:
8486     case DW_TAG_subrange_type:
8487     case DW_TAG_typedef:
8488     case DW_TAG_union_type:
8489       return 1;
8490     default:
8491       return 0;
8492     }
8493 }
8494
8495 /* Load all DIEs that are interesting for partial symbols into memory.  */
8496
8497 static struct partial_die_info *
8498 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8499                    int building_psymtab, struct dwarf2_cu *cu)
8500 {
8501   struct partial_die_info *part_die;
8502   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8503   struct abbrev_info *abbrev;
8504   unsigned int bytes_read;
8505   unsigned int load_all = 0;
8506
8507   int nesting_level = 1;
8508
8509   parent_die = NULL;
8510   last_die = NULL;
8511
8512   if (cu->per_cu && cu->per_cu->load_all_dies)
8513     load_all = 1;
8514
8515   cu->partial_dies
8516     = htab_create_alloc_ex (cu->header.length / 12,
8517                             partial_die_hash,
8518                             partial_die_eq,
8519                             NULL,
8520                             &cu->comp_unit_obstack,
8521                             hashtab_obstack_allocate,
8522                             dummy_obstack_deallocate);
8523
8524   part_die = obstack_alloc (&cu->comp_unit_obstack,
8525                             sizeof (struct partial_die_info));
8526
8527   while (1)
8528     {
8529       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8530
8531       /* A NULL abbrev means the end of a series of children.  */
8532       if (abbrev == NULL)
8533         {
8534           if (--nesting_level == 0)
8535             {
8536               /* PART_DIE was probably the last thing allocated on the
8537                  comp_unit_obstack, so we could call obstack_free
8538                  here.  We don't do that because the waste is small,
8539                  and will be cleaned up when we're done with this
8540                  compilation unit.  This way, we're also more robust
8541                  against other users of the comp_unit_obstack.  */
8542               return first_die;
8543             }
8544           info_ptr += bytes_read;
8545           last_die = parent_die;
8546           parent_die = parent_die->die_parent;
8547           continue;
8548         }
8549
8550       /* Check for template arguments.  We never save these; if
8551          they're seen, we just mark the parent, and go on our way.  */
8552       if (parent_die != NULL
8553           && cu->language == language_cplus
8554           && (abbrev->tag == DW_TAG_template_type_param
8555               || abbrev->tag == DW_TAG_template_value_param))
8556         {
8557           parent_die->has_template_arguments = 1;
8558
8559           if (!load_all)
8560             {
8561               /* We don't need a partial DIE for the template argument.  */
8562               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8563                                        cu);
8564               continue;
8565             }
8566         }
8567
8568       /* We only recurse into subprograms looking for template arguments.
8569          Skip their other children.  */
8570       if (!load_all
8571           && cu->language == language_cplus
8572           && parent_die != NULL
8573           && parent_die->tag == DW_TAG_subprogram)
8574         {
8575           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8576           continue;
8577         }
8578
8579       /* Check whether this DIE is interesting enough to save.  Normally
8580          we would not be interested in members here, but there may be
8581          later variables referencing them via DW_AT_specification (for
8582          static members).  */
8583       if (!load_all
8584           && !is_type_tag_for_partial (abbrev->tag)
8585           && abbrev->tag != DW_TAG_constant
8586           && abbrev->tag != DW_TAG_enumerator
8587           && abbrev->tag != DW_TAG_subprogram
8588           && abbrev->tag != DW_TAG_lexical_block
8589           && abbrev->tag != DW_TAG_variable
8590           && abbrev->tag != DW_TAG_namespace
8591           && abbrev->tag != DW_TAG_module
8592           && abbrev->tag != DW_TAG_member)
8593         {
8594           /* Otherwise we skip to the next sibling, if any.  */
8595           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8596           continue;
8597         }
8598
8599       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8600                                    buffer, info_ptr, cu);
8601
8602       /* This two-pass algorithm for processing partial symbols has a
8603          high cost in cache pressure.  Thus, handle some simple cases
8604          here which cover the majority of C partial symbols.  DIEs
8605          which neither have specification tags in them, nor could have
8606          specification tags elsewhere pointing at them, can simply be
8607          processed and discarded.
8608
8609          This segment is also optional; scan_partial_symbols and
8610          add_partial_symbol will handle these DIEs if we chain
8611          them in normally.  When compilers which do not emit large
8612          quantities of duplicate debug information are more common,
8613          this code can probably be removed.  */
8614
8615       /* Any complete simple types at the top level (pretty much all
8616          of them, for a language without namespaces), can be processed
8617          directly.  */
8618       if (parent_die == NULL
8619           && part_die->has_specification == 0
8620           && part_die->is_declaration == 0
8621           && (part_die->tag == DW_TAG_typedef
8622               || part_die->tag == DW_TAG_base_type
8623               || part_die->tag == DW_TAG_subrange_type))
8624         {
8625           if (building_psymtab && part_die->name != NULL)
8626             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8627                                  VAR_DOMAIN, LOC_TYPEDEF,
8628                                  &cu->objfile->static_psymbols,
8629                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8630           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8631           continue;
8632         }
8633
8634       /* If we're at the second level, and we're an enumerator, and
8635          our parent has no specification (meaning possibly lives in a
8636          namespace elsewhere), then we can add the partial symbol now
8637          instead of queueing it.  */
8638       if (part_die->tag == DW_TAG_enumerator
8639           && parent_die != NULL
8640           && parent_die->die_parent == NULL
8641           && parent_die->tag == DW_TAG_enumeration_type
8642           && parent_die->has_specification == 0)
8643         {
8644           if (part_die->name == NULL)
8645             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8646           else if (building_psymtab)
8647             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8648                                  VAR_DOMAIN, LOC_CONST,
8649                                  (cu->language == language_cplus
8650                                   || cu->language == language_java)
8651                                  ? &cu->objfile->global_psymbols
8652                                  : &cu->objfile->static_psymbols,
8653                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8654
8655           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8656           continue;
8657         }
8658
8659       /* We'll save this DIE so link it in.  */
8660       part_die->die_parent = parent_die;
8661       part_die->die_sibling = NULL;
8662       part_die->die_child = NULL;
8663
8664       if (last_die && last_die == parent_die)
8665         last_die->die_child = part_die;
8666       else if (last_die)
8667         last_die->die_sibling = part_die;
8668
8669       last_die = part_die;
8670
8671       if (first_die == NULL)
8672         first_die = part_die;
8673
8674       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8675          find interesting need to be in the hash table, because we
8676          also have the parent/sibling/child chains; only those that we
8677          might refer to by offset later during partial symbol reading.
8678
8679          For now this means things that might have be the target of a
8680          DW_AT_specification, DW_AT_abstract_origin, or
8681          DW_AT_extension.  DW_AT_extension will refer only to
8682          namespaces; DW_AT_abstract_origin refers to functions (and
8683          many things under the function DIE, but we do not recurse
8684          into function DIEs during partial symbol reading) and
8685          possibly variables as well; DW_AT_specification refers to
8686          declarations.  Declarations ought to have the DW_AT_declaration
8687          flag.  It happens that GCC forgets to put it in sometimes, but
8688          only for functions, not for types.
8689
8690          Adding more things than necessary to the hash table is harmless
8691          except for the performance cost.  Adding too few will result in
8692          wasted time in find_partial_die, when we reread the compilation
8693          unit with load_all_dies set.  */
8694
8695       if (load_all
8696           || abbrev->tag == DW_TAG_constant
8697           || abbrev->tag == DW_TAG_subprogram
8698           || abbrev->tag == DW_TAG_variable
8699           || abbrev->tag == DW_TAG_namespace
8700           || part_die->is_declaration)
8701         {
8702           void **slot;
8703
8704           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8705                                            part_die->offset, INSERT);
8706           *slot = part_die;
8707         }
8708
8709       part_die = obstack_alloc (&cu->comp_unit_obstack,
8710                                 sizeof (struct partial_die_info));
8711
8712       /* For some DIEs we want to follow their children (if any).  For C
8713          we have no reason to follow the children of structures; for other
8714          languages we have to, so that we can get at method physnames
8715          to infer fully qualified class names, for DW_AT_specification,
8716          and for C++ template arguments.  For C++, we also look one level
8717          inside functions to find template arguments (if the name of the
8718          function does not already contain the template arguments).
8719
8720          For Ada, we need to scan the children of subprograms and lexical
8721          blocks as well because Ada allows the definition of nested
8722          entities that could be interesting for the debugger, such as
8723          nested subprograms for instance.  */
8724       if (last_die->has_children
8725           && (load_all
8726               || last_die->tag == DW_TAG_namespace
8727               || last_die->tag == DW_TAG_module
8728               || last_die->tag == DW_TAG_enumeration_type
8729               || (cu->language == language_cplus
8730                   && last_die->tag == DW_TAG_subprogram
8731                   && (last_die->name == NULL
8732                       || strchr (last_die->name, '<') == NULL))
8733               || (cu->language != language_c
8734                   && (last_die->tag == DW_TAG_class_type
8735                       || last_die->tag == DW_TAG_interface_type
8736                       || last_die->tag == DW_TAG_structure_type
8737                       || last_die->tag == DW_TAG_union_type))
8738               || (cu->language == language_ada
8739                   && (last_die->tag == DW_TAG_subprogram
8740                       || last_die->tag == DW_TAG_lexical_block))))
8741         {
8742           nesting_level++;
8743           parent_die = last_die;
8744           continue;
8745         }
8746
8747       /* Otherwise we skip to the next sibling, if any.  */
8748       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8749
8750       /* Back to the top, do it again.  */
8751     }
8752 }
8753
8754 /* Read a minimal amount of information into the minimal die structure.  */
8755
8756 static gdb_byte *
8757 read_partial_die (struct partial_die_info *part_die,
8758                   struct abbrev_info *abbrev,
8759                   unsigned int abbrev_len, bfd *abfd,
8760                   gdb_byte *buffer, gdb_byte *info_ptr,
8761                   struct dwarf2_cu *cu)
8762 {
8763   unsigned int i;
8764   struct attribute attr;
8765   int has_low_pc_attr = 0;
8766   int has_high_pc_attr = 0;
8767
8768   memset (part_die, 0, sizeof (struct partial_die_info));
8769
8770   part_die->offset = info_ptr - buffer;
8771
8772   info_ptr += abbrev_len;
8773
8774   if (abbrev == NULL)
8775     return info_ptr;
8776
8777   part_die->tag = abbrev->tag;
8778   part_die->has_children = abbrev->has_children;
8779
8780   for (i = 0; i < abbrev->num_attrs; ++i)
8781     {
8782       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8783
8784       /* Store the data if it is of an attribute we want to keep in a
8785          partial symbol table.  */
8786       switch (attr.name)
8787         {
8788         case DW_AT_name:
8789           switch (part_die->tag)
8790             {
8791             case DW_TAG_compile_unit:
8792             case DW_TAG_type_unit:
8793               /* Compilation units have a DW_AT_name that is a filename, not
8794                  a source language identifier.  */
8795             case DW_TAG_enumeration_type:
8796             case DW_TAG_enumerator:
8797               /* These tags always have simple identifiers already; no need
8798                  to canonicalize them.  */
8799               part_die->name = DW_STRING (&attr);
8800               break;
8801             default:
8802               part_die->name
8803                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8804                                             &cu->objfile->objfile_obstack);
8805               break;
8806             }
8807           break;
8808         case DW_AT_linkage_name:
8809         case DW_AT_MIPS_linkage_name:
8810           /* Note that both forms of linkage name might appear.  We
8811              assume they will be the same, and we only store the last
8812              one we see.  */
8813           if (cu->language == language_ada)
8814             part_die->name = DW_STRING (&attr);
8815           break;
8816         case DW_AT_low_pc:
8817           has_low_pc_attr = 1;
8818           part_die->lowpc = DW_ADDR (&attr);
8819           break;
8820         case DW_AT_high_pc:
8821           has_high_pc_attr = 1;
8822           part_die->highpc = DW_ADDR (&attr);
8823           break;
8824         case DW_AT_location:
8825           /* Support the .debug_loc offsets */
8826           if (attr_form_is_block (&attr))
8827             {
8828                part_die->locdesc = DW_BLOCK (&attr);
8829             }
8830           else if (attr_form_is_section_offset (&attr))
8831             {
8832               dwarf2_complex_location_expr_complaint ();
8833             }
8834           else
8835             {
8836               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8837                                                      "partial symbol information");
8838             }
8839           break;
8840         case DW_AT_external:
8841           part_die->is_external = DW_UNSND (&attr);
8842           break;
8843         case DW_AT_declaration:
8844           part_die->is_declaration = DW_UNSND (&attr);
8845           break;
8846         case DW_AT_type:
8847           part_die->has_type = 1;
8848           break;
8849         case DW_AT_abstract_origin:
8850         case DW_AT_specification:
8851         case DW_AT_extension:
8852           part_die->has_specification = 1;
8853           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8854           break;
8855         case DW_AT_sibling:
8856           /* Ignore absolute siblings, they might point outside of
8857              the current compile unit.  */
8858           if (attr.form == DW_FORM_ref_addr)
8859             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8860           else
8861             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8862           break;
8863         case DW_AT_byte_size:
8864           part_die->has_byte_size = 1;
8865           break;
8866         case DW_AT_calling_convention:
8867           /* DWARF doesn't provide a way to identify a program's source-level
8868              entry point.  DW_AT_calling_convention attributes are only meant
8869              to describe functions' calling conventions.
8870
8871              However, because it's a necessary piece of information in
8872              Fortran, and because DW_CC_program is the only piece of debugging
8873              information whose definition refers to a 'main program' at all,
8874              several compilers have begun marking Fortran main programs with
8875              DW_CC_program --- even when those functions use the standard
8876              calling conventions.
8877
8878              So until DWARF specifies a way to provide this information and
8879              compilers pick up the new representation, we'll support this
8880              practice.  */
8881           if (DW_UNSND (&attr) == DW_CC_program
8882               && cu->language == language_fortran)
8883             set_main_name (part_die->name);
8884           break;
8885         default:
8886           break;
8887         }
8888     }
8889
8890   /* When using the GNU linker, .gnu.linkonce. sections are used to
8891      eliminate duplicate copies of functions and vtables and such.
8892      The linker will arbitrarily choose one and discard the others.
8893      The AT_*_pc values for such functions refer to local labels in
8894      these sections.  If the section from that file was discarded, the
8895      labels are not in the output, so the relocs get a value of 0.
8896      If this is a discarded function, mark the pc bounds as invalid,
8897      so that GDB will ignore it.  */
8898   if (has_low_pc_attr && has_high_pc_attr
8899       && part_die->lowpc < part_die->highpc
8900       && (part_die->lowpc != 0
8901           || dwarf2_per_objfile->has_section_at_zero))
8902     part_die->has_pc_info = 1;
8903
8904   return info_ptr;
8905 }
8906
8907 /* Find a cached partial DIE at OFFSET in CU.  */
8908
8909 static struct partial_die_info *
8910 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8911 {
8912   struct partial_die_info *lookup_die = NULL;
8913   struct partial_die_info part_die;
8914
8915   part_die.offset = offset;
8916   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8917
8918   return lookup_die;
8919 }
8920
8921 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8922    except in the case of .debug_types DIEs which do not reference
8923    outside their CU (they do however referencing other types via
8924    DW_FORM_sig8).  */
8925
8926 static struct partial_die_info *
8927 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8928 {
8929   struct dwarf2_per_cu_data *per_cu = NULL;
8930   struct partial_die_info *pd = NULL;
8931
8932   if (cu->per_cu->from_debug_types)
8933     {
8934       pd = find_partial_die_in_comp_unit (offset, cu);
8935       if (pd != NULL)
8936         return pd;
8937       goto not_found;
8938     }
8939
8940   if (offset_in_cu_p (&cu->header, offset))
8941     {
8942       pd = find_partial_die_in_comp_unit (offset, cu);
8943       if (pd != NULL)
8944         return pd;
8945     }
8946
8947   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8948
8949   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8950     load_partial_comp_unit (per_cu, cu->objfile);
8951
8952   per_cu->cu->last_used = 0;
8953   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8954
8955   if (pd == NULL && per_cu->load_all_dies == 0)
8956     {
8957       struct cleanup *back_to;
8958       struct partial_die_info comp_unit_die;
8959       struct abbrev_info *abbrev;
8960       unsigned int bytes_read;
8961       char *info_ptr;
8962
8963       per_cu->load_all_dies = 1;
8964
8965       /* Re-read the DIEs.  */
8966       back_to = make_cleanup (null_cleanup, 0);
8967       if (per_cu->cu->dwarf2_abbrevs == NULL)
8968         {
8969           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8970           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8971         }
8972       info_ptr = (dwarf2_per_objfile->info.buffer
8973                   + per_cu->cu->header.offset
8974                   + per_cu->cu->header.first_die_offset);
8975       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8976       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8977                                    per_cu->cu->objfile->obfd,
8978                                    dwarf2_per_objfile->info.buffer, info_ptr,
8979                                    per_cu->cu);
8980       if (comp_unit_die.has_children)
8981         load_partial_dies (per_cu->cu->objfile->obfd,
8982                            dwarf2_per_objfile->info.buffer, info_ptr,
8983                            0, per_cu->cu);
8984       do_cleanups (back_to);
8985
8986       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8987     }
8988
8989  not_found:
8990
8991   if (pd == NULL)
8992     internal_error (__FILE__, __LINE__,
8993                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8994                     offset, bfd_get_filename (cu->objfile->obfd));
8995   return pd;
8996 }
8997
8998 /* Adjust PART_DIE before generating a symbol for it.  This function
8999    may set the is_external flag or change the DIE's name.  */
9000
9001 static void
9002 fixup_partial_die (struct partial_die_info *part_die,
9003                    struct dwarf2_cu *cu)
9004 {
9005   /* If we found a reference attribute and the DIE has no name, try
9006      to find a name in the referred to DIE.  */
9007
9008   if (part_die->name == NULL && part_die->has_specification)
9009     {
9010       struct partial_die_info *spec_die;
9011
9012       spec_die = find_partial_die (part_die->spec_offset, cu);
9013
9014       fixup_partial_die (spec_die, cu);
9015
9016       if (spec_die->name)
9017         {
9018           part_die->name = spec_die->name;
9019
9020           /* Copy DW_AT_external attribute if it is set.  */
9021           if (spec_die->is_external)
9022             part_die->is_external = spec_die->is_external;
9023         }
9024     }
9025
9026   /* Set default names for some unnamed DIEs.  */
9027
9028   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9029     part_die->name = "(anonymous namespace)";
9030
9031   if (part_die->tag == DW_TAG_structure_type
9032       || part_die->tag == DW_TAG_class_type
9033       || part_die->tag == DW_TAG_union_type)
9034     guess_structure_name (part_die, cu);
9035 }
9036
9037 /* Read an attribute value described by an attribute form.  */
9038
9039 static gdb_byte *
9040 read_attribute_value (struct attribute *attr, unsigned form,
9041                       bfd *abfd, gdb_byte *info_ptr,
9042                       struct dwarf2_cu *cu)
9043 {
9044   struct comp_unit_head *cu_header = &cu->header;
9045   unsigned int bytes_read;
9046   struct dwarf_block *blk;
9047
9048   attr->form = form;
9049   switch (form)
9050     {
9051     case DW_FORM_ref_addr:
9052       if (cu->header.version == 2)
9053         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9054       else
9055         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9056       info_ptr += bytes_read;
9057       break;
9058     case DW_FORM_addr:
9059       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9060       info_ptr += bytes_read;
9061       break;
9062     case DW_FORM_block2:
9063       blk = dwarf_alloc_block (cu);
9064       blk->size = read_2_bytes (abfd, info_ptr);
9065       info_ptr += 2;
9066       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9067       info_ptr += blk->size;
9068       DW_BLOCK (attr) = blk;
9069       break;
9070     case DW_FORM_block4:
9071       blk = dwarf_alloc_block (cu);
9072       blk->size = read_4_bytes (abfd, info_ptr);
9073       info_ptr += 4;
9074       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9075       info_ptr += blk->size;
9076       DW_BLOCK (attr) = blk;
9077       break;
9078     case DW_FORM_data2:
9079       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9080       info_ptr += 2;
9081       break;
9082     case DW_FORM_data4:
9083       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9084       info_ptr += 4;
9085       break;
9086     case DW_FORM_data8:
9087       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9088       info_ptr += 8;
9089       break;
9090     case DW_FORM_sec_offset:
9091       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9092       info_ptr += bytes_read;
9093       break;
9094     case DW_FORM_string:
9095       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9096       DW_STRING_IS_CANONICAL (attr) = 0;
9097       info_ptr += bytes_read;
9098       break;
9099     case DW_FORM_strp:
9100       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9101                                                &bytes_read);
9102       DW_STRING_IS_CANONICAL (attr) = 0;
9103       info_ptr += bytes_read;
9104       break;
9105     case DW_FORM_exprloc:
9106     case DW_FORM_block:
9107       blk = dwarf_alloc_block (cu);
9108       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9109       info_ptr += bytes_read;
9110       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9111       info_ptr += blk->size;
9112       DW_BLOCK (attr) = blk;
9113       break;
9114     case DW_FORM_block1:
9115       blk = dwarf_alloc_block (cu);
9116       blk->size = read_1_byte (abfd, info_ptr);
9117       info_ptr += 1;
9118       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9119       info_ptr += blk->size;
9120       DW_BLOCK (attr) = blk;
9121       break;
9122     case DW_FORM_data1:
9123       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9124       info_ptr += 1;
9125       break;
9126     case DW_FORM_flag:
9127       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9128       info_ptr += 1;
9129       break;
9130     case DW_FORM_flag_present:
9131       DW_UNSND (attr) = 1;
9132       break;
9133     case DW_FORM_sdata:
9134       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9135       info_ptr += bytes_read;
9136       break;
9137     case DW_FORM_udata:
9138       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9139       info_ptr += bytes_read;
9140       break;
9141     case DW_FORM_ref1:
9142       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9143       info_ptr += 1;
9144       break;
9145     case DW_FORM_ref2:
9146       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9147       info_ptr += 2;
9148       break;
9149     case DW_FORM_ref4:
9150       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9151       info_ptr += 4;
9152       break;
9153     case DW_FORM_ref8:
9154       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9155       info_ptr += 8;
9156       break;
9157     case DW_FORM_sig8:
9158       /* Convert the signature to something we can record in DW_UNSND
9159          for later lookup.
9160          NOTE: This is NULL if the type wasn't found.  */
9161       DW_SIGNATURED_TYPE (attr) =
9162         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9163       info_ptr += 8;
9164       break;
9165     case DW_FORM_ref_udata:
9166       DW_ADDR (attr) = (cu->header.offset
9167                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9168       info_ptr += bytes_read;
9169       break;
9170     case DW_FORM_indirect:
9171       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9172       info_ptr += bytes_read;
9173       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9174       break;
9175     default:
9176       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9177              dwarf_form_name (form),
9178              bfd_get_filename (abfd));
9179     }
9180
9181   /* We have seen instances where the compiler tried to emit a byte
9182      size attribute of -1 which ended up being encoded as an unsigned
9183      0xffffffff.  Although 0xffffffff is technically a valid size value,
9184      an object of this size seems pretty unlikely so we can relatively
9185      safely treat these cases as if the size attribute was invalid and
9186      treat them as zero by default.  */
9187   if (attr->name == DW_AT_byte_size
9188       && form == DW_FORM_data4
9189       && DW_UNSND (attr) >= 0xffffffff)
9190     {
9191       complaint
9192         (&symfile_complaints,
9193          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9194          hex_string (DW_UNSND (attr)));
9195       DW_UNSND (attr) = 0;
9196     }
9197
9198   return info_ptr;
9199 }
9200
9201 /* Read an attribute described by an abbreviated attribute.  */
9202
9203 static gdb_byte *
9204 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9205                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9206 {
9207   attr->name = abbrev->name;
9208   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9209 }
9210
9211 /* read dwarf information from a buffer */
9212
9213 static unsigned int
9214 read_1_byte (bfd *abfd, gdb_byte *buf)
9215 {
9216   return bfd_get_8 (abfd, buf);
9217 }
9218
9219 static int
9220 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9221 {
9222   return bfd_get_signed_8 (abfd, buf);
9223 }
9224
9225 static unsigned int
9226 read_2_bytes (bfd *abfd, gdb_byte *buf)
9227 {
9228   return bfd_get_16 (abfd, buf);
9229 }
9230
9231 static int
9232 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9233 {
9234   return bfd_get_signed_16 (abfd, buf);
9235 }
9236
9237 static unsigned int
9238 read_4_bytes (bfd *abfd, gdb_byte *buf)
9239 {
9240   return bfd_get_32 (abfd, buf);
9241 }
9242
9243 static int
9244 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9245 {
9246   return bfd_get_signed_32 (abfd, buf);
9247 }
9248
9249 static ULONGEST
9250 read_8_bytes (bfd *abfd, gdb_byte *buf)
9251 {
9252   return bfd_get_64 (abfd, buf);
9253 }
9254
9255 static CORE_ADDR
9256 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9257               unsigned int *bytes_read)
9258 {
9259   struct comp_unit_head *cu_header = &cu->header;
9260   CORE_ADDR retval = 0;
9261
9262   if (cu_header->signed_addr_p)
9263     {
9264       switch (cu_header->addr_size)
9265         {
9266         case 2:
9267           retval = bfd_get_signed_16 (abfd, buf);
9268           break;
9269         case 4:
9270           retval = bfd_get_signed_32 (abfd, buf);
9271           break;
9272         case 8:
9273           retval = bfd_get_signed_64 (abfd, buf);
9274           break;
9275         default:
9276           internal_error (__FILE__, __LINE__,
9277                           _("read_address: bad switch, signed [in module %s]"),
9278                           bfd_get_filename (abfd));
9279         }
9280     }
9281   else
9282     {
9283       switch (cu_header->addr_size)
9284         {
9285         case 2:
9286           retval = bfd_get_16 (abfd, buf);
9287           break;
9288         case 4:
9289           retval = bfd_get_32 (abfd, buf);
9290           break;
9291         case 8:
9292           retval = bfd_get_64 (abfd, buf);
9293           break;
9294         default:
9295           internal_error (__FILE__, __LINE__,
9296                           _("read_address: bad switch, unsigned [in module %s]"),
9297                           bfd_get_filename (abfd));
9298         }
9299     }
9300
9301   *bytes_read = cu_header->addr_size;
9302   return retval;
9303 }
9304
9305 /* Read the initial length from a section.  The (draft) DWARF 3
9306    specification allows the initial length to take up either 4 bytes
9307    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9308    bytes describe the length and all offsets will be 8 bytes in length
9309    instead of 4.
9310
9311    An older, non-standard 64-bit format is also handled by this
9312    function.  The older format in question stores the initial length
9313    as an 8-byte quantity without an escape value.  Lengths greater
9314    than 2^32 aren't very common which means that the initial 4 bytes
9315    is almost always zero.  Since a length value of zero doesn't make
9316    sense for the 32-bit format, this initial zero can be considered to
9317    be an escape value which indicates the presence of the older 64-bit
9318    format.  As written, the code can't detect (old format) lengths
9319    greater than 4GB.  If it becomes necessary to handle lengths
9320    somewhat larger than 4GB, we could allow other small values (such
9321    as the non-sensical values of 1, 2, and 3) to also be used as
9322    escape values indicating the presence of the old format.
9323
9324    The value returned via bytes_read should be used to increment the
9325    relevant pointer after calling read_initial_length().
9326
9327    [ Note:  read_initial_length() and read_offset() are based on the
9328      document entitled "DWARF Debugging Information Format", revision
9329      3, draft 8, dated November 19, 2001.  This document was obtained
9330      from:
9331
9332         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9333
9334      This document is only a draft and is subject to change.  (So beware.)
9335
9336      Details regarding the older, non-standard 64-bit format were
9337      determined empirically by examining 64-bit ELF files produced by
9338      the SGI toolchain on an IRIX 6.5 machine.
9339
9340      - Kevin, July 16, 2002
9341    ] */
9342
9343 static LONGEST
9344 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9345 {
9346   LONGEST length = bfd_get_32 (abfd, buf);
9347
9348   if (length == 0xffffffff)
9349     {
9350       length = bfd_get_64 (abfd, buf + 4);
9351       *bytes_read = 12;
9352     }
9353   else if (length == 0)
9354     {
9355       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9356       length = bfd_get_64 (abfd, buf);
9357       *bytes_read = 8;
9358     }
9359   else
9360     {
9361       *bytes_read = 4;
9362     }
9363
9364   return length;
9365 }
9366
9367 /* Cover function for read_initial_length.
9368    Returns the length of the object at BUF, and stores the size of the
9369    initial length in *BYTES_READ and stores the size that offsets will be in
9370    *OFFSET_SIZE.
9371    If the initial length size is not equivalent to that specified in
9372    CU_HEADER then issue a complaint.
9373    This is useful when reading non-comp-unit headers.  */
9374
9375 static LONGEST
9376 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9377                                         const struct comp_unit_head *cu_header,
9378                                         unsigned int *bytes_read,
9379                                         unsigned int *offset_size)
9380 {
9381   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9382
9383   gdb_assert (cu_header->initial_length_size == 4
9384               || cu_header->initial_length_size == 8
9385               || cu_header->initial_length_size == 12);
9386
9387   if (cu_header->initial_length_size != *bytes_read)
9388     complaint (&symfile_complaints,
9389                _("intermixed 32-bit and 64-bit DWARF sections"));
9390
9391   *offset_size = (*bytes_read == 4) ? 4 : 8;
9392   return length;
9393 }
9394
9395 /* Read an offset from the data stream.  The size of the offset is
9396    given by cu_header->offset_size.  */
9397
9398 static LONGEST
9399 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9400              unsigned int *bytes_read)
9401 {
9402   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9403
9404   *bytes_read = cu_header->offset_size;
9405   return offset;
9406 }
9407
9408 /* Read an offset from the data stream.  */
9409
9410 static LONGEST
9411 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9412 {
9413   LONGEST retval = 0;
9414
9415   switch (offset_size)
9416     {
9417     case 4:
9418       retval = bfd_get_32 (abfd, buf);
9419       break;
9420     case 8:
9421       retval = bfd_get_64 (abfd, buf);
9422       break;
9423     default:
9424       internal_error (__FILE__, __LINE__,
9425                       _("read_offset_1: bad switch [in module %s]"),
9426                       bfd_get_filename (abfd));
9427     }
9428
9429   return retval;
9430 }
9431
9432 static gdb_byte *
9433 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9434 {
9435   /* If the size of a host char is 8 bits, we can return a pointer
9436      to the buffer, otherwise we have to copy the data to a buffer
9437      allocated on the temporary obstack.  */
9438   gdb_assert (HOST_CHAR_BIT == 8);
9439   return buf;
9440 }
9441
9442 static char *
9443 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9444 {
9445   /* If the size of a host char is 8 bits, we can return a pointer
9446      to the string, otherwise we have to copy the string to a buffer
9447      allocated on the temporary obstack.  */
9448   gdb_assert (HOST_CHAR_BIT == 8);
9449   if (*buf == '\0')
9450     {
9451       *bytes_read_ptr = 1;
9452       return NULL;
9453     }
9454   *bytes_read_ptr = strlen ((char *) buf) + 1;
9455   return (char *) buf;
9456 }
9457
9458 static char *
9459 read_indirect_string (bfd *abfd, gdb_byte *buf,
9460                       const struct comp_unit_head *cu_header,
9461                       unsigned int *bytes_read_ptr)
9462 {
9463   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9464
9465   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9466   if (dwarf2_per_objfile->str.buffer == NULL)
9467     {
9468       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9469                       bfd_get_filename (abfd));
9470       return NULL;
9471     }
9472   if (str_offset >= dwarf2_per_objfile->str.size)
9473     {
9474       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9475                       bfd_get_filename (abfd));
9476       return NULL;
9477     }
9478   gdb_assert (HOST_CHAR_BIT == 8);
9479   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9480     return NULL;
9481   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9482 }
9483
9484 static unsigned long
9485 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9486 {
9487   unsigned long result;
9488   unsigned int num_read;
9489   int i, shift;
9490   unsigned char byte;
9491
9492   result = 0;
9493   shift = 0;
9494   num_read = 0;
9495   i = 0;
9496   while (1)
9497     {
9498       byte = bfd_get_8 (abfd, buf);
9499       buf++;
9500       num_read++;
9501       result |= ((unsigned long)(byte & 127) << shift);
9502       if ((byte & 128) == 0)
9503         {
9504           break;
9505         }
9506       shift += 7;
9507     }
9508   *bytes_read_ptr = num_read;
9509   return result;
9510 }
9511
9512 static long
9513 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9514 {
9515   long result;
9516   int i, shift, num_read;
9517   unsigned char byte;
9518
9519   result = 0;
9520   shift = 0;
9521   num_read = 0;
9522   i = 0;
9523   while (1)
9524     {
9525       byte = bfd_get_8 (abfd, buf);
9526       buf++;
9527       num_read++;
9528       result |= ((long)(byte & 127) << shift);
9529       shift += 7;
9530       if ((byte & 128) == 0)
9531         {
9532           break;
9533         }
9534     }
9535   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9536     result |= -(((long)1) << shift);
9537   *bytes_read_ptr = num_read;
9538   return result;
9539 }
9540
9541 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9542
9543 static gdb_byte *
9544 skip_leb128 (bfd *abfd, gdb_byte *buf)
9545 {
9546   int byte;
9547
9548   while (1)
9549     {
9550       byte = bfd_get_8 (abfd, buf);
9551       buf++;
9552       if ((byte & 128) == 0)
9553         return buf;
9554     }
9555 }
9556
9557 static void
9558 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9559 {
9560   switch (lang)
9561     {
9562     case DW_LANG_C89:
9563     case DW_LANG_C99:
9564     case DW_LANG_C:
9565       cu->language = language_c;
9566       break;
9567     case DW_LANG_C_plus_plus:
9568       cu->language = language_cplus;
9569       break;
9570     case DW_LANG_D:
9571       cu->language = language_d;
9572       break;
9573     case DW_LANG_Fortran77:
9574     case DW_LANG_Fortran90:
9575     case DW_LANG_Fortran95:
9576       cu->language = language_fortran;
9577       break;
9578     case DW_LANG_Mips_Assembler:
9579       cu->language = language_asm;
9580       break;
9581     case DW_LANG_Java:
9582       cu->language = language_java;
9583       break;
9584     case DW_LANG_Ada83:
9585     case DW_LANG_Ada95:
9586       cu->language = language_ada;
9587       break;
9588     case DW_LANG_Modula2:
9589       cu->language = language_m2;
9590       break;
9591     case DW_LANG_Pascal83:
9592       cu->language = language_pascal;
9593       break;
9594     case DW_LANG_ObjC:
9595       cu->language = language_objc;
9596       break;
9597     case DW_LANG_Cobol74:
9598     case DW_LANG_Cobol85:
9599     default:
9600       cu->language = language_minimal;
9601       break;
9602     }
9603   cu->language_defn = language_def (cu->language);
9604 }
9605
9606 /* Return the named attribute or NULL if not there.  */
9607
9608 static struct attribute *
9609 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9610 {
9611   unsigned int i;
9612   struct attribute *spec = NULL;
9613
9614   for (i = 0; i < die->num_attrs; ++i)
9615     {
9616       if (die->attrs[i].name == name)
9617         return &die->attrs[i];
9618       if (die->attrs[i].name == DW_AT_specification
9619           || die->attrs[i].name == DW_AT_abstract_origin)
9620         spec = &die->attrs[i];
9621     }
9622
9623   if (spec)
9624     {
9625       die = follow_die_ref (die, spec, &cu);
9626       return dwarf2_attr (die, name, cu);
9627     }
9628
9629   return NULL;
9630 }
9631
9632 /* Return the named attribute or NULL if not there,
9633    but do not follow DW_AT_specification, etc.
9634    This is for use in contexts where we're reading .debug_types dies.
9635    Following DW_AT_specification, DW_AT_abstract_origin will take us
9636    back up the chain, and we want to go down.  */
9637
9638 static struct attribute *
9639 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9640                        struct dwarf2_cu *cu)
9641 {
9642   unsigned int i;
9643
9644   for (i = 0; i < die->num_attrs; ++i)
9645     if (die->attrs[i].name == name)
9646       return &die->attrs[i];
9647
9648   return NULL;
9649 }
9650
9651 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9652    and holds a non-zero value.  This function should only be used for
9653    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9654
9655 static int
9656 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9657 {
9658   struct attribute *attr = dwarf2_attr (die, name, cu);
9659
9660   return (attr && DW_UNSND (attr));
9661 }
9662
9663 static int
9664 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9665 {
9666   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9667      which value is non-zero.  However, we have to be careful with
9668      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9669      (via dwarf2_flag_true_p) follows this attribute.  So we may
9670      end up accidently finding a declaration attribute that belongs
9671      to a different DIE referenced by the specification attribute,
9672      even though the given DIE does not have a declaration attribute.  */
9673   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9674           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9675 }
9676
9677 /* Return the die giving the specification for DIE, if there is
9678    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9679    containing the return value on output.  If there is no
9680    specification, but there is an abstract origin, that is
9681    returned.  */
9682
9683 static struct die_info *
9684 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9685 {
9686   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9687                                              *spec_cu);
9688
9689   if (spec_attr == NULL)
9690     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9691
9692   if (spec_attr == NULL)
9693     return NULL;
9694   else
9695     return follow_die_ref (die, spec_attr, spec_cu);
9696 }
9697
9698 /* Free the line_header structure *LH, and any arrays and strings it
9699    refers to.  */
9700 static void
9701 free_line_header (struct line_header *lh)
9702 {
9703   if (lh->standard_opcode_lengths)
9704     xfree (lh->standard_opcode_lengths);
9705
9706   /* Remember that all the lh->file_names[i].name pointers are
9707      pointers into debug_line_buffer, and don't need to be freed.  */
9708   if (lh->file_names)
9709     xfree (lh->file_names);
9710
9711   /* Similarly for the include directory names.  */
9712   if (lh->include_dirs)
9713     xfree (lh->include_dirs);
9714
9715   xfree (lh);
9716 }
9717
9718
9719 /* Add an entry to LH's include directory table.  */
9720 static void
9721 add_include_dir (struct line_header *lh, char *include_dir)
9722 {
9723   /* Grow the array if necessary.  */
9724   if (lh->include_dirs_size == 0)
9725     {
9726       lh->include_dirs_size = 1; /* for testing */
9727       lh->include_dirs = xmalloc (lh->include_dirs_size
9728                                   * sizeof (*lh->include_dirs));
9729     }
9730   else if (lh->num_include_dirs >= lh->include_dirs_size)
9731     {
9732       lh->include_dirs_size *= 2;
9733       lh->include_dirs = xrealloc (lh->include_dirs,
9734                                    (lh->include_dirs_size
9735                                     * sizeof (*lh->include_dirs)));
9736     }
9737
9738   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9739 }
9740
9741
9742 /* Add an entry to LH's file name table.  */
9743 static void
9744 add_file_name (struct line_header *lh,
9745                char *name,
9746                unsigned int dir_index,
9747                unsigned int mod_time,
9748                unsigned int length)
9749 {
9750   struct file_entry *fe;
9751
9752   /* Grow the array if necessary.  */
9753   if (lh->file_names_size == 0)
9754     {
9755       lh->file_names_size = 1; /* for testing */
9756       lh->file_names = xmalloc (lh->file_names_size
9757                                 * sizeof (*lh->file_names));
9758     }
9759   else if (lh->num_file_names >= lh->file_names_size)
9760     {
9761       lh->file_names_size *= 2;
9762       lh->file_names = xrealloc (lh->file_names,
9763                                  (lh->file_names_size
9764                                   * sizeof (*lh->file_names)));
9765     }
9766
9767   fe = &lh->file_names[lh->num_file_names++];
9768   fe->name = name;
9769   fe->dir_index = dir_index;
9770   fe->mod_time = mod_time;
9771   fe->length = length;
9772   fe->included_p = 0;
9773   fe->symtab = NULL;
9774 }
9775
9776
9777 /* Read the statement program header starting at OFFSET in
9778    .debug_line, according to the endianness of ABFD.  Return a pointer
9779    to a struct line_header, allocated using xmalloc.
9780
9781    NOTE: the strings in the include directory and file name tables of
9782    the returned object point into debug_line_buffer, and must not be
9783    freed.  */
9784 static struct line_header *
9785 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9786                           struct dwarf2_cu *cu)
9787 {
9788   struct cleanup *back_to;
9789   struct line_header *lh;
9790   gdb_byte *line_ptr;
9791   unsigned int bytes_read, offset_size;
9792   int i;
9793   char *cur_dir, *cur_file;
9794
9795   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9796   if (dwarf2_per_objfile->line.buffer == NULL)
9797     {
9798       complaint (&symfile_complaints, _("missing .debug_line section"));
9799       return 0;
9800     }
9801
9802   /* Make sure that at least there's room for the total_length field.
9803      That could be 12 bytes long, but we're just going to fudge that.  */
9804   if (offset + 4 >= dwarf2_per_objfile->line.size)
9805     {
9806       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9807       return 0;
9808     }
9809
9810   lh = xmalloc (sizeof (*lh));
9811   memset (lh, 0, sizeof (*lh));
9812   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9813                           (void *) lh);
9814
9815   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9816
9817   /* Read in the header.  */
9818   lh->total_length =
9819     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9820                                             &bytes_read, &offset_size);
9821   line_ptr += bytes_read;
9822   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9823                                      + dwarf2_per_objfile->line.size))
9824     {
9825       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9826       return 0;
9827     }
9828   lh->statement_program_end = line_ptr + lh->total_length;
9829   lh->version = read_2_bytes (abfd, line_ptr);
9830   line_ptr += 2;
9831   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9832   line_ptr += offset_size;
9833   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9834   line_ptr += 1;
9835   if (lh->version >= 4)
9836     {
9837       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9838       line_ptr += 1;
9839     }
9840   else
9841     lh->maximum_ops_per_instruction = 1;
9842
9843   if (lh->maximum_ops_per_instruction == 0)
9844     {
9845       lh->maximum_ops_per_instruction = 1;
9846       complaint (&symfile_complaints,
9847                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9848     }
9849
9850   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9851   line_ptr += 1;
9852   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9853   line_ptr += 1;
9854   lh->line_range = read_1_byte (abfd, line_ptr);
9855   line_ptr += 1;
9856   lh->opcode_base = read_1_byte (abfd, line_ptr);
9857   line_ptr += 1;
9858   lh->standard_opcode_lengths
9859     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9860
9861   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9862   for (i = 1; i < lh->opcode_base; ++i)
9863     {
9864       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9865       line_ptr += 1;
9866     }
9867
9868   /* Read directory table.  */
9869   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9870     {
9871       line_ptr += bytes_read;
9872       add_include_dir (lh, cur_dir);
9873     }
9874   line_ptr += bytes_read;
9875
9876   /* Read file name table.  */
9877   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9878     {
9879       unsigned int dir_index, mod_time, length;
9880
9881       line_ptr += bytes_read;
9882       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9883       line_ptr += bytes_read;
9884       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9885       line_ptr += bytes_read;
9886       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9887       line_ptr += bytes_read;
9888
9889       add_file_name (lh, cur_file, dir_index, mod_time, length);
9890     }
9891   line_ptr += bytes_read;
9892   lh->statement_program_start = line_ptr;
9893
9894   if (line_ptr > (dwarf2_per_objfile->line.buffer
9895                   + dwarf2_per_objfile->line.size))
9896     complaint (&symfile_complaints,
9897                _("line number info header doesn't fit in `.debug_line' section"));
9898
9899   discard_cleanups (back_to);
9900   return lh;
9901 }
9902
9903 /* This function exists to work around a bug in certain compilers
9904    (particularly GCC 2.95), in which the first line number marker of a
9905    function does not show up until after the prologue, right before
9906    the second line number marker.  This function shifts ADDRESS down
9907    to the beginning of the function if necessary, and is called on
9908    addresses passed to record_line.  */
9909
9910 static CORE_ADDR
9911 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9912 {
9913   struct function_range *fn;
9914
9915   /* Find the function_range containing address.  */
9916   if (!cu->first_fn)
9917     return address;
9918
9919   if (!cu->cached_fn)
9920     cu->cached_fn = cu->first_fn;
9921
9922   fn = cu->cached_fn;
9923   while (fn)
9924     if (fn->lowpc <= address && fn->highpc > address)
9925       goto found;
9926     else
9927       fn = fn->next;
9928
9929   fn = cu->first_fn;
9930   while (fn && fn != cu->cached_fn)
9931     if (fn->lowpc <= address && fn->highpc > address)
9932       goto found;
9933     else
9934       fn = fn->next;
9935
9936   return address;
9937
9938  found:
9939   if (fn->seen_line)
9940     return address;
9941   if (address != fn->lowpc)
9942     complaint (&symfile_complaints,
9943                _("misplaced first line number at 0x%lx for '%s'"),
9944                (unsigned long) address, fn->name);
9945   fn->seen_line = 1;
9946   return fn->lowpc;
9947 }
9948
9949 /* Subroutine of dwarf_decode_lines to simplify it.
9950    Return the file name of the psymtab for included file FILE_INDEX
9951    in line header LH of PST.
9952    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9953    If space for the result is malloc'd, it will be freed by a cleanup.
9954    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
9955
9956 static char *
9957 psymtab_include_file_name (const struct line_header *lh, int file_index,
9958                            const struct partial_symtab *pst,
9959                            const char *comp_dir)
9960 {
9961   const struct file_entry fe = lh->file_names [file_index];
9962   char *include_name = fe.name;
9963   char *include_name_to_compare = include_name;
9964   char *dir_name = NULL;
9965   char *pst_filename;
9966   int file_is_pst;
9967
9968   if (fe.dir_index)
9969     dir_name = lh->include_dirs[fe.dir_index - 1];
9970
9971   if (!IS_ABSOLUTE_PATH (include_name)
9972       && (dir_name != NULL || comp_dir != NULL))
9973     {
9974       /* Avoid creating a duplicate psymtab for PST.
9975          We do this by comparing INCLUDE_NAME and PST_FILENAME.
9976          Before we do the comparison, however, we need to account
9977          for DIR_NAME and COMP_DIR.
9978          First prepend dir_name (if non-NULL).  If we still don't
9979          have an absolute path prepend comp_dir (if non-NULL).
9980          However, the directory we record in the include-file's
9981          psymtab does not contain COMP_DIR (to match the
9982          corresponding symtab(s)).
9983
9984          Example:
9985
9986          bash$ cd /tmp
9987          bash$ gcc -g ./hello.c
9988          include_name = "hello.c"
9989          dir_name = "."
9990          DW_AT_comp_dir = comp_dir = "/tmp"
9991          DW_AT_name = "./hello.c"  */
9992
9993       if (dir_name != NULL)
9994         {
9995           include_name = concat (dir_name, SLASH_STRING,
9996                                  include_name, (char *)NULL);
9997           include_name_to_compare = include_name;
9998           make_cleanup (xfree, include_name);
9999         }
10000       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10001         {
10002           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10003                                             include_name, (char *)NULL);
10004         }
10005     }
10006
10007   pst_filename = pst->filename;
10008   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10009     {
10010       pst_filename = concat (pst->dirname, SLASH_STRING,
10011                              pst_filename, (char *)NULL);
10012     }
10013
10014   file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
10015
10016   if (include_name_to_compare != include_name)
10017     xfree (include_name_to_compare);
10018   if (pst_filename != pst->filename)
10019     xfree (pst_filename);
10020
10021   if (file_is_pst)
10022     return NULL;
10023   return include_name;
10024 }
10025
10026 /* Decode the Line Number Program (LNP) for the given line_header
10027    structure and CU.  The actual information extracted and the type
10028    of structures created from the LNP depends on the value of PST.
10029
10030    1. If PST is NULL, then this procedure uses the data from the program
10031       to create all necessary symbol tables, and their linetables.
10032
10033    2. If PST is not NULL, this procedure reads the program to determine
10034       the list of files included by the unit represented by PST, and
10035       builds all the associated partial symbol tables.
10036
10037    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10038    It is used for relative paths in the line table.
10039    NOTE: When processing partial symtabs (pst != NULL),
10040    comp_dir == pst->dirname.
10041
10042    NOTE: It is important that psymtabs have the same file name (via strcmp)
10043    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10044    symtab we don't use it in the name of the psymtabs we create.
10045    E.g. expand_line_sal requires this when finding psymtabs to expand.
10046    A good testcase for this is mb-inline.exp.  */
10047
10048 static void
10049 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
10050                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10051 {
10052   gdb_byte *line_ptr, *extended_end;
10053   gdb_byte *line_end;
10054   unsigned int bytes_read, extended_len;
10055   unsigned char op_code, extended_op, adj_opcode;
10056   CORE_ADDR baseaddr;
10057   struct objfile *objfile = cu->objfile;
10058   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10059   const int decode_for_pst_p = (pst != NULL);
10060   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10061
10062   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10063
10064   line_ptr = lh->statement_program_start;
10065   line_end = lh->statement_program_end;
10066
10067   /* Read the statement sequences until there's nothing left.  */
10068   while (line_ptr < line_end)
10069     {
10070       /* state machine registers  */
10071       CORE_ADDR address = 0;
10072       unsigned int file = 1;
10073       unsigned int line = 1;
10074       unsigned int column = 0;
10075       int is_stmt = lh->default_is_stmt;
10076       int basic_block = 0;
10077       int end_sequence = 0;
10078       CORE_ADDR addr;
10079       unsigned char op_index = 0;
10080
10081       if (!decode_for_pst_p && lh->num_file_names >= file)
10082         {
10083           /* Start a subfile for the current file of the state machine.  */
10084           /* lh->include_dirs and lh->file_names are 0-based, but the
10085              directory and file name numbers in the statement program
10086              are 1-based.  */
10087           struct file_entry *fe = &lh->file_names[file - 1];
10088           char *dir = NULL;
10089
10090           if (fe->dir_index)
10091             dir = lh->include_dirs[fe->dir_index - 1];
10092
10093           dwarf2_start_subfile (fe->name, dir, comp_dir);
10094         }
10095
10096       /* Decode the table.  */
10097       while (!end_sequence)
10098         {
10099           op_code = read_1_byte (abfd, line_ptr);
10100           line_ptr += 1;
10101           if (line_ptr > line_end)
10102             {
10103               dwarf2_debug_line_missing_end_sequence_complaint ();
10104               break;
10105             }
10106
10107           if (op_code >= lh->opcode_base)
10108             {
10109               /* Special operand.  */
10110               adj_opcode = op_code - lh->opcode_base;
10111               address += (((op_index + (adj_opcode / lh->line_range))
10112                            / lh->maximum_ops_per_instruction)
10113                           * lh->minimum_instruction_length);
10114               op_index = ((op_index + (adj_opcode / lh->line_range))
10115                           % lh->maximum_ops_per_instruction);
10116               line += lh->line_base + (adj_opcode % lh->line_range);
10117               if (lh->num_file_names < file || file == 0)
10118                 dwarf2_debug_line_missing_file_complaint ();
10119               /* For now we ignore lines not starting on an
10120                  instruction boundary.  */
10121               else if (op_index == 0)
10122                 {
10123                   lh->file_names[file - 1].included_p = 1;
10124                   if (!decode_for_pst_p && is_stmt)
10125                     {
10126                       if (last_subfile != current_subfile)
10127                         {
10128                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10129                           if (last_subfile)
10130                             record_line (last_subfile, 0, addr);
10131                           last_subfile = current_subfile;
10132                         }
10133                       /* Append row to matrix using current values.  */
10134                       addr = check_cu_functions (address, cu);
10135                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10136                       record_line (current_subfile, line, addr);
10137                     }
10138                 }
10139               basic_block = 0;
10140             }
10141           else switch (op_code)
10142             {
10143             case DW_LNS_extended_op:
10144               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10145               line_ptr += bytes_read;
10146               extended_end = line_ptr + extended_len;
10147               extended_op = read_1_byte (abfd, line_ptr);
10148               line_ptr += 1;
10149               switch (extended_op)
10150                 {
10151                 case DW_LNE_end_sequence:
10152                   end_sequence = 1;
10153                   break;
10154                 case DW_LNE_set_address:
10155                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10156                   op_index = 0;
10157                   line_ptr += bytes_read;
10158                   address += baseaddr;
10159                   break;
10160                 case DW_LNE_define_file:
10161                   {
10162                     char *cur_file;
10163                     unsigned int dir_index, mod_time, length;
10164
10165                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10166                     line_ptr += bytes_read;
10167                     dir_index =
10168                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10169                     line_ptr += bytes_read;
10170                     mod_time =
10171                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10172                     line_ptr += bytes_read;
10173                     length =
10174                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10175                     line_ptr += bytes_read;
10176                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10177                   }
10178                   break;
10179                 case DW_LNE_set_discriminator:
10180                   /* The discriminator is not interesting to the debugger;
10181                      just ignore it.  */
10182                   line_ptr = extended_end;
10183                   break;
10184                 default:
10185                   complaint (&symfile_complaints,
10186                              _("mangled .debug_line section"));
10187                   return;
10188                 }
10189               /* Make sure that we parsed the extended op correctly.  If e.g.
10190                  we expected a different address size than the producer used,
10191                  we may have read the wrong number of bytes.  */
10192               if (line_ptr != extended_end)
10193                 {
10194                   complaint (&symfile_complaints,
10195                              _("mangled .debug_line section"));
10196                   return;
10197                 }
10198               break;
10199             case DW_LNS_copy:
10200               if (lh->num_file_names < file || file == 0)
10201                 dwarf2_debug_line_missing_file_complaint ();
10202               else
10203                 {
10204                   lh->file_names[file - 1].included_p = 1;
10205                   if (!decode_for_pst_p && is_stmt)
10206                     {
10207                       if (last_subfile != current_subfile)
10208                         {
10209                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10210                           if (last_subfile)
10211                             record_line (last_subfile, 0, addr);
10212                           last_subfile = current_subfile;
10213                         }
10214                       addr = check_cu_functions (address, cu);
10215                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10216                       record_line (current_subfile, line, addr);
10217                     }
10218                 }
10219               basic_block = 0;
10220               break;
10221             case DW_LNS_advance_pc:
10222               {
10223                 CORE_ADDR adjust
10224                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10225
10226                 address += (((op_index + adjust)
10227                              / lh->maximum_ops_per_instruction)
10228                             * lh->minimum_instruction_length);
10229                 op_index = ((op_index + adjust)
10230                             % lh->maximum_ops_per_instruction);
10231                 line_ptr += bytes_read;
10232               }
10233               break;
10234             case DW_LNS_advance_line:
10235               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10236               line_ptr += bytes_read;
10237               break;
10238             case DW_LNS_set_file:
10239               {
10240                 /* The arrays lh->include_dirs and lh->file_names are
10241                    0-based, but the directory and file name numbers in
10242                    the statement program are 1-based.  */
10243                 struct file_entry *fe;
10244                 char *dir = NULL;
10245
10246                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10247                 line_ptr += bytes_read;
10248                 if (lh->num_file_names < file || file == 0)
10249                   dwarf2_debug_line_missing_file_complaint ();
10250                 else
10251                   {
10252                     fe = &lh->file_names[file - 1];
10253                     if (fe->dir_index)
10254                       dir = lh->include_dirs[fe->dir_index - 1];
10255                     if (!decode_for_pst_p)
10256                       {
10257                         last_subfile = current_subfile;
10258                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10259                       }
10260                   }
10261               }
10262               break;
10263             case DW_LNS_set_column:
10264               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10265               line_ptr += bytes_read;
10266               break;
10267             case DW_LNS_negate_stmt:
10268               is_stmt = (!is_stmt);
10269               break;
10270             case DW_LNS_set_basic_block:
10271               basic_block = 1;
10272               break;
10273             /* Add to the address register of the state machine the
10274                address increment value corresponding to special opcode
10275                255.  I.e., this value is scaled by the minimum
10276                instruction length since special opcode 255 would have
10277                scaled the the increment.  */
10278             case DW_LNS_const_add_pc:
10279               {
10280                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10281
10282                 address += (((op_index + adjust)
10283                              / lh->maximum_ops_per_instruction)
10284                             * lh->minimum_instruction_length);
10285                 op_index = ((op_index + adjust)
10286                             % lh->maximum_ops_per_instruction);
10287               }
10288               break;
10289             case DW_LNS_fixed_advance_pc:
10290               address += read_2_bytes (abfd, line_ptr);
10291               op_index = 0;
10292               line_ptr += 2;
10293               break;
10294             default:
10295               {
10296                 /* Unknown standard opcode, ignore it.  */
10297                 int i;
10298
10299                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10300                   {
10301                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10302                     line_ptr += bytes_read;
10303                   }
10304               }
10305             }
10306         }
10307       if (lh->num_file_names < file || file == 0)
10308         dwarf2_debug_line_missing_file_complaint ();
10309       else
10310         {
10311           lh->file_names[file - 1].included_p = 1;
10312           if (!decode_for_pst_p)
10313             {
10314               addr = gdbarch_addr_bits_remove (gdbarch, address);
10315               record_line (current_subfile, 0, addr);
10316             }
10317         }
10318     }
10319
10320   if (decode_for_pst_p)
10321     {
10322       int file_index;
10323
10324       /* Now that we're done scanning the Line Header Program, we can
10325          create the psymtab of each included file.  */
10326       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10327         if (lh->file_names[file_index].included_p == 1)
10328           {
10329             char *include_name =
10330               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10331             if (include_name != NULL)
10332               dwarf2_create_include_psymtab (include_name, pst, objfile);
10333           }
10334     }
10335   else
10336     {
10337       /* Make sure a symtab is created for every file, even files
10338          which contain only variables (i.e. no code with associated
10339          line numbers).  */
10340
10341       int i;
10342       struct file_entry *fe;
10343
10344       for (i = 0; i < lh->num_file_names; i++)
10345         {
10346           char *dir = NULL;
10347
10348           fe = &lh->file_names[i];
10349           if (fe->dir_index)
10350             dir = lh->include_dirs[fe->dir_index - 1];
10351           dwarf2_start_subfile (fe->name, dir, comp_dir);
10352
10353           /* Skip the main file; we don't need it, and it must be
10354              allocated last, so that it will show up before the
10355              non-primary symtabs in the objfile's symtab list.  */
10356           if (current_subfile == first_subfile)
10357             continue;
10358
10359           if (current_subfile->symtab == NULL)
10360             current_subfile->symtab = allocate_symtab (current_subfile->name,
10361                                                        cu->objfile);
10362           fe->symtab = current_subfile->symtab;
10363         }
10364     }
10365 }
10366
10367 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10368    DIRNAME the name of the source directory which contains FILENAME
10369    or NULL if not known.  COMP_DIR is the compilation directory for the
10370    linetable's compilation unit or NULL if not known.
10371    This routine tries to keep line numbers from identical absolute and
10372    relative file names in a common subfile.
10373
10374    Using the `list' example from the GDB testsuite, which resides in
10375    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10376    of /srcdir/list0.c yields the following debugging information for list0.c:
10377
10378    DW_AT_name:          /srcdir/list0.c
10379    DW_AT_comp_dir:              /compdir
10380    files.files[0].name: list0.h
10381    files.files[0].dir:  /srcdir
10382    files.files[1].name: list0.c
10383    files.files[1].dir:  /srcdir
10384
10385    The line number information for list0.c has to end up in a single
10386    subfile, so that `break /srcdir/list0.c:1' works as expected.
10387    start_subfile will ensure that this happens provided that we pass the
10388    concatenation of files.files[1].dir and files.files[1].name as the
10389    subfile's name.  */
10390
10391 static void
10392 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10393 {
10394   char *fullname;
10395
10396   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10397      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10398      second argument to start_subfile.  To be consistent, we do the
10399      same here.  In order not to lose the line information directory,
10400      we concatenate it to the filename when it makes sense.
10401      Note that the Dwarf3 standard says (speaking of filenames in line
10402      information): ``The directory index is ignored for file names
10403      that represent full path names''.  Thus ignoring dirname in the
10404      `else' branch below isn't an issue.  */
10405
10406   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10407     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10408   else
10409     fullname = filename;
10410
10411   start_subfile (fullname, comp_dir);
10412
10413   if (fullname != filename)
10414     xfree (fullname);
10415 }
10416
10417 static void
10418 var_decode_location (struct attribute *attr, struct symbol *sym,
10419                      struct dwarf2_cu *cu)
10420 {
10421   struct objfile *objfile = cu->objfile;
10422   struct comp_unit_head *cu_header = &cu->header;
10423
10424   /* NOTE drow/2003-01-30: There used to be a comment and some special
10425      code here to turn a symbol with DW_AT_external and a
10426      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10427      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10428      with some versions of binutils) where shared libraries could have
10429      relocations against symbols in their debug information - the
10430      minimal symbol would have the right address, but the debug info
10431      would not.  It's no longer necessary, because we will explicitly
10432      apply relocations when we read in the debug information now.  */
10433
10434   /* A DW_AT_location attribute with no contents indicates that a
10435      variable has been optimized away.  */
10436   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10437     {
10438       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10439       return;
10440     }
10441
10442   /* Handle one degenerate form of location expression specially, to
10443      preserve GDB's previous behavior when section offsets are
10444      specified.  If this is just a DW_OP_addr then mark this symbol
10445      as LOC_STATIC.  */
10446
10447   if (attr_form_is_block (attr)
10448       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10449       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10450     {
10451       unsigned int dummy;
10452
10453       SYMBOL_VALUE_ADDRESS (sym) =
10454         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10455       SYMBOL_CLASS (sym) = LOC_STATIC;
10456       fixup_symbol_section (sym, objfile);
10457       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10458                                               SYMBOL_SECTION (sym));
10459       return;
10460     }
10461
10462   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10463      expression evaluator, and use LOC_COMPUTED only when necessary
10464      (i.e. when the value of a register or memory location is
10465      referenced, or a thread-local block, etc.).  Then again, it might
10466      not be worthwhile.  I'm assuming that it isn't unless performance
10467      or memory numbers show me otherwise.  */
10468
10469   dwarf2_symbol_mark_computed (attr, sym, cu);
10470   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10471 }
10472
10473 /* Given a pointer to a DWARF information entry, figure out if we need
10474    to make a symbol table entry for it, and if so, create a new entry
10475    and return a pointer to it.
10476    If TYPE is NULL, determine symbol type from the die, otherwise
10477    used the passed type.
10478    If SPACE is not NULL, use it to hold the new symbol.  If it is
10479    NULL, allocate a new symbol on the objfile's obstack.  */
10480
10481 static struct symbol *
10482 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10483                  struct symbol *space)
10484 {
10485   struct objfile *objfile = cu->objfile;
10486   struct symbol *sym = NULL;
10487   char *name;
10488   struct attribute *attr = NULL;
10489   struct attribute *attr2 = NULL;
10490   CORE_ADDR baseaddr;
10491   struct pending **list_to_add = NULL;
10492
10493   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10494
10495   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10496
10497   name = dwarf2_name (die, cu);
10498   if (name)
10499     {
10500       const char *linkagename;
10501       int suppress_add = 0;
10502
10503       if (space)
10504         sym = space;
10505       else
10506         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10507       OBJSTAT (objfile, n_syms++);
10508
10509       /* Cache this symbol's name and the name's demangled form (if any).  */
10510       SYMBOL_SET_LANGUAGE (sym, cu->language);
10511       linkagename = dwarf2_physname (name, die, cu);
10512       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10513
10514       /* Fortran does not have mangling standard and the mangling does differ
10515          between gfortran, iFort etc.  */
10516       if (cu->language == language_fortran
10517           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10518         symbol_set_demangled_name (&(sym->ginfo),
10519                                    (char *) dwarf2_full_name (name, die, cu),
10520                                    NULL);
10521
10522       /* Default assumptions.
10523          Use the passed type or decode it from the die.  */
10524       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10525       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10526       if (type != NULL)
10527         SYMBOL_TYPE (sym) = type;
10528       else
10529         SYMBOL_TYPE (sym) = die_type (die, cu);
10530       attr = dwarf2_attr (die,
10531                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10532                           cu);
10533       if (attr)
10534         {
10535           SYMBOL_LINE (sym) = DW_UNSND (attr);
10536         }
10537
10538       attr = dwarf2_attr (die,
10539                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10540                           cu);
10541       if (attr)
10542         {
10543           int file_index = DW_UNSND (attr);
10544
10545           if (cu->line_header == NULL
10546               || file_index > cu->line_header->num_file_names)
10547             complaint (&symfile_complaints,
10548                        _("file index out of range"));
10549           else if (file_index > 0)
10550             {
10551               struct file_entry *fe;
10552
10553               fe = &cu->line_header->file_names[file_index - 1];
10554               SYMBOL_SYMTAB (sym) = fe->symtab;
10555             }
10556         }
10557
10558       switch (die->tag)
10559         {
10560         case DW_TAG_label:
10561           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10562           if (attr)
10563             {
10564               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10565             }
10566           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10567           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10568           SYMBOL_CLASS (sym) = LOC_LABEL;
10569           add_symbol_to_list (sym, cu->list_in_scope);
10570           break;
10571         case DW_TAG_subprogram:
10572           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10573              finish_block.  */
10574           SYMBOL_CLASS (sym) = LOC_BLOCK;
10575           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10576           if ((attr2 && (DW_UNSND (attr2) != 0))
10577               || cu->language == language_ada)
10578             {
10579               /* Subprograms marked external are stored as a global symbol.
10580                  Ada subprograms, whether marked external or not, are always
10581                  stored as a global symbol, because we want to be able to
10582                  access them globally.  For instance, we want to be able
10583                  to break on a nested subprogram without having to
10584                  specify the context.  */
10585               list_to_add = &global_symbols;
10586             }
10587           else
10588             {
10589               list_to_add = cu->list_in_scope;
10590             }
10591           break;
10592         case DW_TAG_inlined_subroutine:
10593           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10594              finish_block.  */
10595           SYMBOL_CLASS (sym) = LOC_BLOCK;
10596           SYMBOL_INLINED (sym) = 1;
10597           /* Do not add the symbol to any lists.  It will be found via
10598              BLOCK_FUNCTION from the blockvector.  */
10599           break;
10600         case DW_TAG_template_value_param:
10601           suppress_add = 1;
10602           /* Fall through.  */
10603         case DW_TAG_constant:
10604         case DW_TAG_variable:
10605         case DW_TAG_member:
10606           /* Compilation with minimal debug info may result in variables
10607              with missing type entries. Change the misleading `void' type
10608              to something sensible.  */
10609           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10610             SYMBOL_TYPE (sym)
10611               = objfile_type (objfile)->nodebug_data_symbol;
10612
10613           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10614           /* In the case of DW_TAG_member, we should only be called for
10615              static const members.  */
10616           if (die->tag == DW_TAG_member)
10617             {
10618               /* dwarf2_add_field uses die_is_declaration,
10619                  so we do the same.  */
10620               gdb_assert (die_is_declaration (die, cu));
10621               gdb_assert (attr);
10622             }
10623           if (attr)
10624             {
10625               dwarf2_const_value (attr, sym, cu);
10626               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10627               if (!suppress_add)
10628                 {
10629                   if (attr2 && (DW_UNSND (attr2) != 0))
10630                     list_to_add = &global_symbols;
10631                   else
10632                     list_to_add = cu->list_in_scope;
10633                 }
10634               break;
10635             }
10636           attr = dwarf2_attr (die, DW_AT_location, cu);
10637           if (attr)
10638             {
10639               var_decode_location (attr, sym, cu);
10640               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10641               if (SYMBOL_CLASS (sym) == LOC_STATIC
10642                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10643                   && !dwarf2_per_objfile->has_section_at_zero)
10644                 {
10645                   /* When a static variable is eliminated by the linker,
10646                      the corresponding debug information is not stripped
10647                      out, but the variable address is set to null;
10648                      do not add such variables into symbol table.  */
10649                 }
10650               else if (attr2 && (DW_UNSND (attr2) != 0))
10651                 {
10652                   /* Workaround gfortran PR debug/40040 - it uses
10653                      DW_AT_location for variables in -fPIC libraries which may
10654                      get overriden by other libraries/executable and get
10655                      a different address.  Resolve it by the minimal symbol
10656                      which may come from inferior's executable using copy
10657                      relocation.  Make this workaround only for gfortran as for
10658                      other compilers GDB cannot guess the minimal symbol
10659                      Fortran mangling kind.  */
10660                   if (cu->language == language_fortran && die->parent
10661                       && die->parent->tag == DW_TAG_module
10662                       && cu->producer
10663                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10664                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10665
10666                   /* A variable with DW_AT_external is never static,
10667                      but it may be block-scoped.  */
10668                   list_to_add = (cu->list_in_scope == &file_symbols
10669                                  ? &global_symbols : cu->list_in_scope);
10670                 }
10671               else
10672                 list_to_add = cu->list_in_scope;
10673             }
10674           else
10675             {
10676               /* We do not know the address of this symbol.
10677                  If it is an external symbol and we have type information
10678                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10679                  The address of the variable will then be determined from
10680                  the minimal symbol table whenever the variable is
10681                  referenced.  */
10682               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10683               if (attr2 && (DW_UNSND (attr2) != 0)
10684                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10685                 {
10686                   /* A variable with DW_AT_external is never static, but it
10687                      may be block-scoped.  */
10688                   list_to_add = (cu->list_in_scope == &file_symbols
10689                                  ? &global_symbols : cu->list_in_scope);
10690
10691                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10692                 }
10693               else if (!die_is_declaration (die, cu))
10694                 {
10695                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10696                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10697                   if (!suppress_add)
10698                     list_to_add = cu->list_in_scope;
10699                 }
10700             }
10701           break;
10702         case DW_TAG_formal_parameter:
10703           /* If we are inside a function, mark this as an argument.  If
10704              not, we might be looking at an argument to an inlined function
10705              when we do not have enough information to show inlined frames;
10706              pretend it's a local variable in that case so that the user can
10707              still see it.  */
10708           if (context_stack_depth > 0
10709               && context_stack[context_stack_depth - 1].name != NULL)
10710             SYMBOL_IS_ARGUMENT (sym) = 1;
10711           attr = dwarf2_attr (die, DW_AT_location, cu);
10712           if (attr)
10713             {
10714               var_decode_location (attr, sym, cu);
10715             }
10716           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10717           if (attr)
10718             {
10719               dwarf2_const_value (attr, sym, cu);
10720             }
10721           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10722           if (attr && DW_UNSND (attr))
10723             {
10724               struct type *ref_type;
10725
10726               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10727               SYMBOL_TYPE (sym) = ref_type;
10728             }
10729
10730           list_to_add = cu->list_in_scope;
10731           break;
10732         case DW_TAG_unspecified_parameters:
10733           /* From varargs functions; gdb doesn't seem to have any
10734              interest in this information, so just ignore it for now.
10735              (FIXME?) */
10736           break;
10737         case DW_TAG_template_type_param:
10738           suppress_add = 1;
10739           /* Fall through.  */
10740         case DW_TAG_class_type:
10741         case DW_TAG_interface_type:
10742         case DW_TAG_structure_type:
10743         case DW_TAG_union_type:
10744         case DW_TAG_set_type:
10745         case DW_TAG_enumeration_type:
10746           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10747           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10748
10749           {
10750             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10751                really ever be static objects: otherwise, if you try
10752                to, say, break of a class's method and you're in a file
10753                which doesn't mention that class, it won't work unless
10754                the check for all static symbols in lookup_symbol_aux
10755                saves you.  See the OtherFileClass tests in
10756                gdb.c++/namespace.exp.  */
10757
10758             if (!suppress_add)
10759               {
10760                 list_to_add = (cu->list_in_scope == &file_symbols
10761                                && (cu->language == language_cplus
10762                                    || cu->language == language_java)
10763                                ? &global_symbols : cu->list_in_scope);
10764
10765                 /* The semantics of C++ state that "struct foo {
10766                    ... }" also defines a typedef for "foo".  A Java
10767                    class declaration also defines a typedef for the
10768                    class.  */
10769                 if (cu->language == language_cplus
10770                     || cu->language == language_java
10771                     || cu->language == language_ada)
10772                   {
10773                     /* The symbol's name is already allocated along
10774                        with this objfile, so we don't need to
10775                        duplicate it for the type.  */
10776                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10777                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10778                   }
10779               }
10780           }
10781           break;
10782         case DW_TAG_typedef:
10783           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10784           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10785           list_to_add = cu->list_in_scope;
10786           break;
10787         case DW_TAG_base_type:
10788         case DW_TAG_subrange_type:
10789           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10790           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10791           list_to_add = cu->list_in_scope;
10792           break;
10793         case DW_TAG_enumerator:
10794           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10795           if (attr)
10796             {
10797               dwarf2_const_value (attr, sym, cu);
10798             }
10799           {
10800             /* NOTE: carlton/2003-11-10: See comment above in the
10801                DW_TAG_class_type, etc. block.  */
10802
10803             list_to_add = (cu->list_in_scope == &file_symbols
10804                            && (cu->language == language_cplus
10805                                || cu->language == language_java)
10806                            ? &global_symbols : cu->list_in_scope);
10807           }
10808           break;
10809         case DW_TAG_namespace:
10810           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10811           list_to_add = &global_symbols;
10812           break;
10813         default:
10814           /* Not a tag we recognize.  Hopefully we aren't processing
10815              trash data, but since we must specifically ignore things
10816              we don't recognize, there is nothing else we should do at
10817              this point. */
10818           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10819                      dwarf_tag_name (die->tag));
10820           break;
10821         }
10822
10823       if (suppress_add)
10824         {
10825           sym->hash_next = objfile->template_symbols;
10826           objfile->template_symbols = sym;
10827           list_to_add = NULL;
10828         }
10829
10830       if (list_to_add != NULL)
10831         add_symbol_to_list (sym, list_to_add);
10832
10833       /* For the benefit of old versions of GCC, check for anonymous
10834          namespaces based on the demangled name.  */
10835       if (!processing_has_namespace_info
10836           && cu->language == language_cplus)
10837         cp_scan_for_anonymous_namespaces (sym);
10838     }
10839   return (sym);
10840 }
10841
10842 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
10843
10844 static struct symbol *
10845 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10846 {
10847   return new_symbol_full (die, type, cu, NULL);
10848 }
10849
10850 /* Given an attr with a DW_FORM_dataN value in host byte order,
10851    zero-extend it as appropriate for the symbol's type.  The DWARF
10852    standard (v4) is not entirely clear about the meaning of using
10853    DW_FORM_dataN for a constant with a signed type, where the type is
10854    wider than the data.  The conclusion of a discussion on the DWARF
10855    list was that this is unspecified.  We choose to always zero-extend
10856    because that is the interpretation long in use by GCC.  */
10857
10858 static gdb_byte *
10859 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10860                          const char *name, struct obstack *obstack,
10861                          struct dwarf2_cu *cu, long *value, int bits)
10862 {
10863   struct objfile *objfile = cu->objfile;
10864   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10865                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10866   LONGEST l = DW_UNSND (attr);
10867
10868   if (bits < sizeof (*value) * 8)
10869     {
10870       l &= ((LONGEST) 1 << bits) - 1;
10871       *value = l;
10872     }
10873   else if (bits == sizeof (*value) * 8)
10874     *value = l;
10875   else
10876     {
10877       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10878       store_unsigned_integer (bytes, bits / 8, byte_order, l);
10879       return bytes;
10880     }
10881
10882   return NULL;
10883 }
10884
10885 /* Read a constant value from an attribute.  Either set *VALUE, or if
10886    the value does not fit in *VALUE, set *BYTES - either already
10887    allocated on the objfile obstack, or newly allocated on OBSTACK,
10888    or, set *BATON, if we translated the constant to a location
10889    expression.  */
10890
10891 static void
10892 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10893                          const char *name, struct obstack *obstack,
10894                          struct dwarf2_cu *cu,
10895                          long *value, gdb_byte **bytes,
10896                          struct dwarf2_locexpr_baton **baton)
10897 {
10898   struct objfile *objfile = cu->objfile;
10899   struct comp_unit_head *cu_header = &cu->header;
10900   struct dwarf_block *blk;
10901   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10902                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10903
10904   *value = 0;
10905   *bytes = NULL;
10906   *baton = NULL;
10907
10908   switch (attr->form)
10909     {
10910     case DW_FORM_addr:
10911       {
10912         gdb_byte *data;
10913
10914         if (TYPE_LENGTH (type) != cu_header->addr_size)
10915           dwarf2_const_value_length_mismatch_complaint (name,
10916                                                         cu_header->addr_size,
10917                                                         TYPE_LENGTH (type));
10918         /* Symbols of this form are reasonably rare, so we just
10919            piggyback on the existing location code rather than writing
10920            a new implementation of symbol_computed_ops.  */
10921         *baton = obstack_alloc (&objfile->objfile_obstack,
10922                                 sizeof (struct dwarf2_locexpr_baton));
10923         (*baton)->per_cu = cu->per_cu;
10924         gdb_assert ((*baton)->per_cu);
10925
10926         (*baton)->size = 2 + cu_header->addr_size;
10927         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10928         (*baton)->data = data;
10929
10930         data[0] = DW_OP_addr;
10931         store_unsigned_integer (&data[1], cu_header->addr_size,
10932                                 byte_order, DW_ADDR (attr));
10933         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10934       }
10935       break;
10936     case DW_FORM_string:
10937     case DW_FORM_strp:
10938       /* DW_STRING is already allocated on the objfile obstack, point
10939          directly to it.  */
10940       *bytes = (gdb_byte *) DW_STRING (attr);
10941       break;
10942     case DW_FORM_block1:
10943     case DW_FORM_block2:
10944     case DW_FORM_block4:
10945     case DW_FORM_block:
10946     case DW_FORM_exprloc:
10947       blk = DW_BLOCK (attr);
10948       if (TYPE_LENGTH (type) != blk->size)
10949         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10950                                                       TYPE_LENGTH (type));
10951       *bytes = blk->data;
10952       break;
10953
10954       /* The DW_AT_const_value attributes are supposed to carry the
10955          symbol's value "represented as it would be on the target
10956          architecture."  By the time we get here, it's already been
10957          converted to host endianness, so we just need to sign- or
10958          zero-extend it as appropriate.  */
10959     case DW_FORM_data1:
10960       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10961       break;
10962     case DW_FORM_data2:
10963       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10964       break;
10965     case DW_FORM_data4:
10966       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10967       break;
10968     case DW_FORM_data8:
10969       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10970       break;
10971
10972     case DW_FORM_sdata:
10973       *value = DW_SND (attr);
10974       break;
10975
10976     case DW_FORM_udata:
10977       *value = DW_UNSND (attr);
10978       break;
10979
10980     default:
10981       complaint (&symfile_complaints,
10982                  _("unsupported const value attribute form: '%s'"),
10983                  dwarf_form_name (attr->form));
10984       *value = 0;
10985       break;
10986     }
10987 }
10988
10989
10990 /* Copy constant value from an attribute to a symbol.  */
10991
10992 static void
10993 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10994                     struct dwarf2_cu *cu)
10995 {
10996   struct objfile *objfile = cu->objfile;
10997   struct comp_unit_head *cu_header = &cu->header;
10998   long value;
10999   gdb_byte *bytes;
11000   struct dwarf2_locexpr_baton *baton;
11001
11002   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11003                            SYMBOL_PRINT_NAME (sym),
11004                            &objfile->objfile_obstack, cu,
11005                            &value, &bytes, &baton);
11006
11007   if (baton != NULL)
11008     {
11009       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11010       SYMBOL_LOCATION_BATON (sym) = baton;
11011       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11012     }
11013   else if (bytes != NULL)
11014      {
11015       SYMBOL_VALUE_BYTES (sym) = bytes;
11016       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11017     }
11018   else
11019     {
11020       SYMBOL_VALUE (sym) = value;
11021       SYMBOL_CLASS (sym) = LOC_CONST;
11022     }
11023 }
11024
11025 /* Return the type of the die in question using its DW_AT_type attribute.  */
11026
11027 static struct type *
11028 die_type (struct die_info *die, struct dwarf2_cu *cu)
11029 {
11030   struct attribute *type_attr;
11031
11032   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11033   if (!type_attr)
11034     {
11035       /* A missing DW_AT_type represents a void type.  */
11036       return objfile_type (cu->objfile)->builtin_void;
11037     }
11038
11039   return lookup_die_type (die, type_attr, cu);
11040 }
11041
11042 /* True iff CU's producer generates GNAT Ada auxiliary information
11043    that allows to find parallel types through that information instead
11044    of having to do expensive parallel lookups by type name.  */
11045
11046 static int
11047 need_gnat_info (struct dwarf2_cu *cu)
11048 {
11049   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11050      of GNAT produces this auxiliary information, without any indication
11051      that it is produced.  Part of enhancing the FSF version of GNAT
11052      to produce that information will be to put in place an indicator
11053      that we can use in order to determine whether the descriptive type
11054      info is available or not.  One suggestion that has been made is
11055      to use a new attribute, attached to the CU die.  For now, assume
11056      that the descriptive type info is not available.  */
11057   return 0;
11058 }
11059
11060 /* Return the auxiliary type of the die in question using its
11061    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11062    attribute is not present.  */
11063
11064 static struct type *
11065 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11066 {
11067   struct attribute *type_attr;
11068
11069   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11070   if (!type_attr)
11071     return NULL;
11072
11073   return lookup_die_type (die, type_attr, cu);
11074 }
11075
11076 /* If DIE has a descriptive_type attribute, then set the TYPE's
11077    descriptive type accordingly.  */
11078
11079 static void
11080 set_descriptive_type (struct type *type, struct die_info *die,
11081                       struct dwarf2_cu *cu)
11082 {
11083   struct type *descriptive_type = die_descriptive_type (die, cu);
11084
11085   if (descriptive_type)
11086     {
11087       ALLOCATE_GNAT_AUX_TYPE (type);
11088       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11089     }
11090 }
11091
11092 /* Return the containing type of the die in question using its
11093    DW_AT_containing_type attribute.  */
11094
11095 static struct type *
11096 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11097 {
11098   struct attribute *type_attr;
11099
11100   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11101   if (!type_attr)
11102     error (_("Dwarf Error: Problem turning containing type into gdb type "
11103              "[in module %s]"), cu->objfile->name);
11104
11105   return lookup_die_type (die, type_attr, cu);
11106 }
11107
11108 /* Look up the type of DIE in CU using its type attribute ATTR.
11109    If there is no type substitute an error marker.  */
11110
11111 static struct type *
11112 lookup_die_type (struct die_info *die, struct attribute *attr,
11113                  struct dwarf2_cu *cu)
11114 {
11115   struct type *this_type;
11116
11117   /* First see if we have it cached.  */
11118
11119   if (is_ref_attr (attr))
11120     {
11121       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11122
11123       this_type = get_die_type_at_offset (offset, cu->per_cu);
11124     }
11125   else if (attr->form == DW_FORM_sig8)
11126     {
11127       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11128       struct dwarf2_cu *sig_cu;
11129       unsigned int offset;
11130
11131       /* sig_type will be NULL if the signatured type is missing from
11132          the debug info.  */
11133       if (sig_type == NULL)
11134         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11135                  "at 0x%x [in module %s]"),
11136                die->offset, cu->objfile->name);
11137
11138       gdb_assert (sig_type->per_cu.from_debug_types);
11139       offset = sig_type->offset + sig_type->type_offset;
11140       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11141     }
11142   else
11143     {
11144       dump_die_for_error (die);
11145       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11146              dwarf_attr_name (attr->name), cu->objfile->name);
11147     }
11148
11149   /* If not cached we need to read it in.  */
11150
11151   if (this_type == NULL)
11152     {
11153       struct die_info *type_die;
11154       struct dwarf2_cu *type_cu = cu;
11155
11156       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11157       /* If the type is cached, we should have found it above.  */
11158       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11159       this_type = read_type_die_1 (type_die, type_cu);
11160     }
11161
11162   /* If we still don't have a type use an error marker.  */
11163
11164   if (this_type == NULL)
11165     {
11166       char *message, *saved;
11167
11168       /* read_type_die already issued a complaint.  */
11169       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11170                             cu->objfile->name,
11171                             cu->header.offset,
11172                             die->offset);
11173       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11174                              message, strlen (message));
11175       xfree (message);
11176
11177       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11178     }
11179
11180   return this_type;
11181 }
11182
11183 /* Return the type in DIE, CU.
11184    Returns NULL for invalid types.
11185
11186    This first does a lookup in the appropriate type_hash table,
11187    and only reads the die in if necessary.
11188
11189    NOTE: This can be called when reading in partial or full symbols.  */
11190
11191 static struct type *
11192 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11193 {
11194   struct type *this_type;
11195
11196   this_type = get_die_type (die, cu);
11197   if (this_type)
11198     return this_type;
11199
11200   return read_type_die_1 (die, cu);
11201 }
11202
11203 /* Read the type in DIE, CU.
11204    Returns NULL for invalid types.  */
11205
11206 static struct type *
11207 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11208 {
11209   struct type *this_type = NULL;
11210
11211   switch (die->tag)
11212     {
11213     case DW_TAG_class_type:
11214     case DW_TAG_interface_type:
11215     case DW_TAG_structure_type:
11216     case DW_TAG_union_type:
11217       this_type = read_structure_type (die, cu);
11218       break;
11219     case DW_TAG_enumeration_type:
11220       this_type = read_enumeration_type (die, cu);
11221       break;
11222     case DW_TAG_subprogram:
11223     case DW_TAG_subroutine_type:
11224     case DW_TAG_inlined_subroutine:
11225       this_type = read_subroutine_type (die, cu);
11226       break;
11227     case DW_TAG_array_type:
11228       this_type = read_array_type (die, cu);
11229       break;
11230     case DW_TAG_set_type:
11231       this_type = read_set_type (die, cu);
11232       break;
11233     case DW_TAG_pointer_type:
11234       this_type = read_tag_pointer_type (die, cu);
11235       break;
11236     case DW_TAG_ptr_to_member_type:
11237       this_type = read_tag_ptr_to_member_type (die, cu);
11238       break;
11239     case DW_TAG_reference_type:
11240       this_type = read_tag_reference_type (die, cu);
11241       break;
11242     case DW_TAG_const_type:
11243       this_type = read_tag_const_type (die, cu);
11244       break;
11245     case DW_TAG_volatile_type:
11246       this_type = read_tag_volatile_type (die, cu);
11247       break;
11248     case DW_TAG_string_type:
11249       this_type = read_tag_string_type (die, cu);
11250       break;
11251     case DW_TAG_typedef:
11252       this_type = read_typedef (die, cu);
11253       break;
11254     case DW_TAG_subrange_type:
11255       this_type = read_subrange_type (die, cu);
11256       break;
11257     case DW_TAG_base_type:
11258       this_type = read_base_type (die, cu);
11259       break;
11260     case DW_TAG_unspecified_type:
11261       this_type = read_unspecified_type (die, cu);
11262       break;
11263     case DW_TAG_namespace:
11264       this_type = read_namespace_type (die, cu);
11265       break;
11266     case DW_TAG_module:
11267       this_type = read_module_type (die, cu);
11268       break;
11269     default:
11270       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11271                  dwarf_tag_name (die->tag));
11272       break;
11273     }
11274
11275   return this_type;
11276 }
11277
11278 /* Return the name of the namespace/class that DIE is defined within,
11279    or "" if we can't tell.  The caller should not xfree the result.
11280
11281    For example, if we're within the method foo() in the following
11282    code:
11283
11284    namespace N {
11285      class C {
11286        void foo () {
11287        }
11288      };
11289    }
11290
11291    then determine_prefix on foo's die will return "N::C".  */
11292
11293 static char *
11294 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11295 {
11296   struct die_info *parent, *spec_die;
11297   struct dwarf2_cu *spec_cu;
11298   struct type *parent_type;
11299
11300   if (cu->language != language_cplus && cu->language != language_java
11301       && cu->language != language_fortran)
11302     return "";
11303
11304   /* We have to be careful in the presence of DW_AT_specification.
11305      For example, with GCC 3.4, given the code
11306
11307      namespace N {
11308        void foo() {
11309          // Definition of N::foo.
11310        }
11311      }
11312
11313      then we'll have a tree of DIEs like this:
11314
11315      1: DW_TAG_compile_unit
11316        2: DW_TAG_namespace        // N
11317          3: DW_TAG_subprogram     // declaration of N::foo
11318        4: DW_TAG_subprogram       // definition of N::foo
11319             DW_AT_specification   // refers to die #3
11320
11321      Thus, when processing die #4, we have to pretend that we're in
11322      the context of its DW_AT_specification, namely the contex of die
11323      #3.  */
11324   spec_cu = cu;
11325   spec_die = die_specification (die, &spec_cu);
11326   if (spec_die == NULL)
11327     parent = die->parent;
11328   else
11329     {
11330       parent = spec_die->parent;
11331       cu = spec_cu;
11332     }
11333
11334   if (parent == NULL)
11335     return "";
11336   else if (parent->building_fullname)
11337     {
11338       const char *name;
11339       const char *parent_name;
11340
11341       /* It has been seen on RealView 2.2 built binaries,
11342          DW_TAG_template_type_param types actually _defined_ as
11343          children of the parent class:
11344
11345          enum E {};
11346          template class <class Enum> Class{};
11347          Class<enum E> class_e;
11348
11349          1: DW_TAG_class_type (Class)
11350            2: DW_TAG_enumeration_type (E)
11351              3: DW_TAG_enumerator (enum1:0)
11352              3: DW_TAG_enumerator (enum2:1)
11353              ...
11354            2: DW_TAG_template_type_param
11355               DW_AT_type  DW_FORM_ref_udata (E)
11356
11357          Besides being broken debug info, it can put GDB into an
11358          infinite loop.  Consider:
11359
11360          When we're building the full name for Class<E>, we'll start
11361          at Class, and go look over its template type parameters,
11362          finding E.  We'll then try to build the full name of E, and
11363          reach here.  We're now trying to build the full name of E,
11364          and look over the parent DIE for containing scope.  In the
11365          broken case, if we followed the parent DIE of E, we'd again
11366          find Class, and once again go look at its template type
11367          arguments, etc., etc.  Simply don't consider such parent die
11368          as source-level parent of this die (it can't be, the language
11369          doesn't allow it), and break the loop here.  */
11370       name = dwarf2_name (die, cu);
11371       parent_name = dwarf2_name (parent, cu);
11372       complaint (&symfile_complaints,
11373                  _("template param type '%s' defined within parent '%s'"),
11374                  name ? name : "<unknown>",
11375                  parent_name ? parent_name : "<unknown>");
11376       return "";
11377     }
11378   else
11379     switch (parent->tag)
11380       {
11381       case DW_TAG_namespace:
11382         parent_type = read_type_die (parent, cu);
11383         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11384            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11385            Work around this problem here.  */
11386         if (cu->language == language_cplus
11387             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11388           return "";
11389         /* We give a name to even anonymous namespaces.  */
11390         return TYPE_TAG_NAME (parent_type);
11391       case DW_TAG_class_type:
11392       case DW_TAG_interface_type:
11393       case DW_TAG_structure_type:
11394       case DW_TAG_union_type:
11395       case DW_TAG_module:
11396         parent_type = read_type_die (parent, cu);
11397         if (TYPE_TAG_NAME (parent_type) != NULL)
11398           return TYPE_TAG_NAME (parent_type);
11399         else
11400           /* An anonymous structure is only allowed non-static data
11401              members; no typedefs, no member functions, et cetera.
11402              So it does not need a prefix.  */
11403           return "";
11404       default:
11405         return determine_prefix (parent, cu);
11406       }
11407 }
11408
11409 /* Return a newly-allocated string formed by concatenating PREFIX and
11410    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11411    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
11412    perform an obconcat, otherwise allocate storage for the result.  The CU argument
11413    is used to determine the language and hence, the appropriate separator.  */
11414
11415 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11416
11417 static char *
11418 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11419                  int physname, struct dwarf2_cu *cu)
11420 {
11421   const char *lead = "";
11422   const char *sep;
11423
11424   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11425     sep = "";
11426   else if (cu->language == language_java)
11427     sep = ".";
11428   else if (cu->language == language_fortran && physname)
11429     {
11430       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11431          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11432
11433       lead = "__";
11434       sep = "_MOD_";
11435     }
11436   else
11437     sep = "::";
11438
11439   if (prefix == NULL)
11440     prefix = "";
11441   if (suffix == NULL)
11442     suffix = "";
11443
11444   if (obs == NULL)
11445     {
11446       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11447
11448       strcpy (retval, lead);
11449       strcat (retval, prefix);
11450       strcat (retval, sep);
11451       strcat (retval, suffix);
11452       return retval;
11453     }
11454   else
11455     {
11456       /* We have an obstack.  */
11457       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11458     }
11459 }
11460
11461 /* Return sibling of die, NULL if no sibling.  */
11462
11463 static struct die_info *
11464 sibling_die (struct die_info *die)
11465 {
11466   return die->sibling;
11467 }
11468
11469 /* Get name of a die, return NULL if not found.  */
11470
11471 static char *
11472 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11473                           struct obstack *obstack)
11474 {
11475   if (name && cu->language == language_cplus)
11476     {
11477       char *canon_name = cp_canonicalize_string (name);
11478
11479       if (canon_name != NULL)
11480         {
11481           if (strcmp (canon_name, name) != 0)
11482             name = obsavestring (canon_name, strlen (canon_name),
11483                                  obstack);
11484           xfree (canon_name);
11485         }
11486     }
11487
11488   return name;
11489 }
11490
11491 /* Get name of a die, return NULL if not found.  */
11492
11493 static char *
11494 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11495 {
11496   struct attribute *attr;
11497
11498   attr = dwarf2_attr (die, DW_AT_name, cu);
11499   if (!attr || !DW_STRING (attr))
11500     return NULL;
11501
11502   switch (die->tag)
11503     {
11504     case DW_TAG_compile_unit:
11505       /* Compilation units have a DW_AT_name that is a filename, not
11506          a source language identifier.  */
11507     case DW_TAG_enumeration_type:
11508     case DW_TAG_enumerator:
11509       /* These tags always have simple identifiers already; no need
11510          to canonicalize them.  */
11511       return DW_STRING (attr);
11512
11513     case DW_TAG_subprogram:
11514       /* Java constructors will all be named "<init>", so return
11515          the class name when we see this special case.  */
11516       if (cu->language == language_java
11517           && DW_STRING (attr) != NULL
11518           && strcmp (DW_STRING (attr), "<init>") == 0)
11519         {
11520           struct dwarf2_cu *spec_cu = cu;
11521           struct die_info *spec_die;
11522
11523           /* GCJ will output '<init>' for Java constructor names.
11524              For this special case, return the name of the parent class.  */
11525
11526           /* GCJ may output suprogram DIEs with AT_specification set.
11527              If so, use the name of the specified DIE.  */
11528           spec_die = die_specification (die, &spec_cu);
11529           if (spec_die != NULL)
11530             return dwarf2_name (spec_die, spec_cu);
11531
11532           do
11533             {
11534               die = die->parent;
11535               if (die->tag == DW_TAG_class_type)
11536                 return dwarf2_name (die, cu);
11537             }
11538           while (die->tag != DW_TAG_compile_unit);
11539         }
11540       break;
11541
11542     case DW_TAG_class_type:
11543     case DW_TAG_interface_type:
11544     case DW_TAG_structure_type:
11545     case DW_TAG_union_type:
11546       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11547          structures or unions.  These were of the form "._%d" in GCC 4.1,
11548          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11549          and GCC 4.4.  We work around this problem by ignoring these.  */
11550       if (strncmp (DW_STRING (attr), "._", 2) == 0
11551           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11552         return NULL;
11553       break;
11554
11555     default:
11556       break;
11557     }
11558
11559   if (!DW_STRING_IS_CANONICAL (attr))
11560     {
11561       DW_STRING (attr)
11562         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11563                                     &cu->objfile->objfile_obstack);
11564       DW_STRING_IS_CANONICAL (attr) = 1;
11565     }
11566   return DW_STRING (attr);
11567 }
11568
11569 /* Return the die that this die in an extension of, or NULL if there
11570    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11571    containing the return value on output.  */
11572
11573 static struct die_info *
11574 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11575 {
11576   struct attribute *attr;
11577
11578   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11579   if (attr == NULL)
11580     return NULL;
11581
11582   return follow_die_ref (die, attr, ext_cu);
11583 }
11584
11585 /* Convert a DIE tag into its string name.  */
11586
11587 static char *
11588 dwarf_tag_name (unsigned tag)
11589 {
11590   switch (tag)
11591     {
11592     case DW_TAG_padding:
11593       return "DW_TAG_padding";
11594     case DW_TAG_array_type:
11595       return "DW_TAG_array_type";
11596     case DW_TAG_class_type:
11597       return "DW_TAG_class_type";
11598     case DW_TAG_entry_point:
11599       return "DW_TAG_entry_point";
11600     case DW_TAG_enumeration_type:
11601       return "DW_TAG_enumeration_type";
11602     case DW_TAG_formal_parameter:
11603       return "DW_TAG_formal_parameter";
11604     case DW_TAG_imported_declaration:
11605       return "DW_TAG_imported_declaration";
11606     case DW_TAG_label:
11607       return "DW_TAG_label";
11608     case DW_TAG_lexical_block:
11609       return "DW_TAG_lexical_block";
11610     case DW_TAG_member:
11611       return "DW_TAG_member";
11612     case DW_TAG_pointer_type:
11613       return "DW_TAG_pointer_type";
11614     case DW_TAG_reference_type:
11615       return "DW_TAG_reference_type";
11616     case DW_TAG_compile_unit:
11617       return "DW_TAG_compile_unit";
11618     case DW_TAG_string_type:
11619       return "DW_TAG_string_type";
11620     case DW_TAG_structure_type:
11621       return "DW_TAG_structure_type";
11622     case DW_TAG_subroutine_type:
11623       return "DW_TAG_subroutine_type";
11624     case DW_TAG_typedef:
11625       return "DW_TAG_typedef";
11626     case DW_TAG_union_type:
11627       return "DW_TAG_union_type";
11628     case DW_TAG_unspecified_parameters:
11629       return "DW_TAG_unspecified_parameters";
11630     case DW_TAG_variant:
11631       return "DW_TAG_variant";
11632     case DW_TAG_common_block:
11633       return "DW_TAG_common_block";
11634     case DW_TAG_common_inclusion:
11635       return "DW_TAG_common_inclusion";
11636     case DW_TAG_inheritance:
11637       return "DW_TAG_inheritance";
11638     case DW_TAG_inlined_subroutine:
11639       return "DW_TAG_inlined_subroutine";
11640     case DW_TAG_module:
11641       return "DW_TAG_module";
11642     case DW_TAG_ptr_to_member_type:
11643       return "DW_TAG_ptr_to_member_type";
11644     case DW_TAG_set_type:
11645       return "DW_TAG_set_type";
11646     case DW_TAG_subrange_type:
11647       return "DW_TAG_subrange_type";
11648     case DW_TAG_with_stmt:
11649       return "DW_TAG_with_stmt";
11650     case DW_TAG_access_declaration:
11651       return "DW_TAG_access_declaration";
11652     case DW_TAG_base_type:
11653       return "DW_TAG_base_type";
11654     case DW_TAG_catch_block:
11655       return "DW_TAG_catch_block";
11656     case DW_TAG_const_type:
11657       return "DW_TAG_const_type";
11658     case DW_TAG_constant:
11659       return "DW_TAG_constant";
11660     case DW_TAG_enumerator:
11661       return "DW_TAG_enumerator";
11662     case DW_TAG_file_type:
11663       return "DW_TAG_file_type";
11664     case DW_TAG_friend:
11665       return "DW_TAG_friend";
11666     case DW_TAG_namelist:
11667       return "DW_TAG_namelist";
11668     case DW_TAG_namelist_item:
11669       return "DW_TAG_namelist_item";
11670     case DW_TAG_packed_type:
11671       return "DW_TAG_packed_type";
11672     case DW_TAG_subprogram:
11673       return "DW_TAG_subprogram";
11674     case DW_TAG_template_type_param:
11675       return "DW_TAG_template_type_param";
11676     case DW_TAG_template_value_param:
11677       return "DW_TAG_template_value_param";
11678     case DW_TAG_thrown_type:
11679       return "DW_TAG_thrown_type";
11680     case DW_TAG_try_block:
11681       return "DW_TAG_try_block";
11682     case DW_TAG_variant_part:
11683       return "DW_TAG_variant_part";
11684     case DW_TAG_variable:
11685       return "DW_TAG_variable";
11686     case DW_TAG_volatile_type:
11687       return "DW_TAG_volatile_type";
11688     case DW_TAG_dwarf_procedure:
11689       return "DW_TAG_dwarf_procedure";
11690     case DW_TAG_restrict_type:
11691       return "DW_TAG_restrict_type";
11692     case DW_TAG_interface_type:
11693       return "DW_TAG_interface_type";
11694     case DW_TAG_namespace:
11695       return "DW_TAG_namespace";
11696     case DW_TAG_imported_module:
11697       return "DW_TAG_imported_module";
11698     case DW_TAG_unspecified_type:
11699       return "DW_TAG_unspecified_type";
11700     case DW_TAG_partial_unit:
11701       return "DW_TAG_partial_unit";
11702     case DW_TAG_imported_unit:
11703       return "DW_TAG_imported_unit";
11704     case DW_TAG_condition:
11705       return "DW_TAG_condition";
11706     case DW_TAG_shared_type:
11707       return "DW_TAG_shared_type";
11708     case DW_TAG_type_unit:
11709       return "DW_TAG_type_unit";
11710     case DW_TAG_MIPS_loop:
11711       return "DW_TAG_MIPS_loop";
11712     case DW_TAG_HP_array_descriptor:
11713       return "DW_TAG_HP_array_descriptor";
11714     case DW_TAG_format_label:
11715       return "DW_TAG_format_label";
11716     case DW_TAG_function_template:
11717       return "DW_TAG_function_template";
11718     case DW_TAG_class_template:
11719       return "DW_TAG_class_template";
11720     case DW_TAG_GNU_BINCL:
11721       return "DW_TAG_GNU_BINCL";
11722     case DW_TAG_GNU_EINCL:
11723       return "DW_TAG_GNU_EINCL";
11724     case DW_TAG_upc_shared_type:
11725       return "DW_TAG_upc_shared_type";
11726     case DW_TAG_upc_strict_type:
11727       return "DW_TAG_upc_strict_type";
11728     case DW_TAG_upc_relaxed_type:
11729       return "DW_TAG_upc_relaxed_type";
11730     case DW_TAG_PGI_kanji_type:
11731       return "DW_TAG_PGI_kanji_type";
11732     case DW_TAG_PGI_interface_block:
11733       return "DW_TAG_PGI_interface_block";
11734     default:
11735       return "DW_TAG_<unknown>";
11736     }
11737 }
11738
11739 /* Convert a DWARF attribute code into its string name.  */
11740
11741 static char *
11742 dwarf_attr_name (unsigned attr)
11743 {
11744   switch (attr)
11745     {
11746     case DW_AT_sibling:
11747       return "DW_AT_sibling";
11748     case DW_AT_location:
11749       return "DW_AT_location";
11750     case DW_AT_name:
11751       return "DW_AT_name";
11752     case DW_AT_ordering:
11753       return "DW_AT_ordering";
11754     case DW_AT_subscr_data:
11755       return "DW_AT_subscr_data";
11756     case DW_AT_byte_size:
11757       return "DW_AT_byte_size";
11758     case DW_AT_bit_offset:
11759       return "DW_AT_bit_offset";
11760     case DW_AT_bit_size:
11761       return "DW_AT_bit_size";
11762     case DW_AT_element_list:
11763       return "DW_AT_element_list";
11764     case DW_AT_stmt_list:
11765       return "DW_AT_stmt_list";
11766     case DW_AT_low_pc:
11767       return "DW_AT_low_pc";
11768     case DW_AT_high_pc:
11769       return "DW_AT_high_pc";
11770     case DW_AT_language:
11771       return "DW_AT_language";
11772     case DW_AT_member:
11773       return "DW_AT_member";
11774     case DW_AT_discr:
11775       return "DW_AT_discr";
11776     case DW_AT_discr_value:
11777       return "DW_AT_discr_value";
11778     case DW_AT_visibility:
11779       return "DW_AT_visibility";
11780     case DW_AT_import:
11781       return "DW_AT_import";
11782     case DW_AT_string_length:
11783       return "DW_AT_string_length";
11784     case DW_AT_common_reference:
11785       return "DW_AT_common_reference";
11786     case DW_AT_comp_dir:
11787       return "DW_AT_comp_dir";
11788     case DW_AT_const_value:
11789       return "DW_AT_const_value";
11790     case DW_AT_containing_type:
11791       return "DW_AT_containing_type";
11792     case DW_AT_default_value:
11793       return "DW_AT_default_value";
11794     case DW_AT_inline:
11795       return "DW_AT_inline";
11796     case DW_AT_is_optional:
11797       return "DW_AT_is_optional";
11798     case DW_AT_lower_bound:
11799       return "DW_AT_lower_bound";
11800     case DW_AT_producer:
11801       return "DW_AT_producer";
11802     case DW_AT_prototyped:
11803       return "DW_AT_prototyped";
11804     case DW_AT_return_addr:
11805       return "DW_AT_return_addr";
11806     case DW_AT_start_scope:
11807       return "DW_AT_start_scope";
11808     case DW_AT_bit_stride:
11809       return "DW_AT_bit_stride";
11810     case DW_AT_upper_bound:
11811       return "DW_AT_upper_bound";
11812     case DW_AT_abstract_origin:
11813       return "DW_AT_abstract_origin";
11814     case DW_AT_accessibility:
11815       return "DW_AT_accessibility";
11816     case DW_AT_address_class:
11817       return "DW_AT_address_class";
11818     case DW_AT_artificial:
11819       return "DW_AT_artificial";
11820     case DW_AT_base_types:
11821       return "DW_AT_base_types";
11822     case DW_AT_calling_convention:
11823       return "DW_AT_calling_convention";
11824     case DW_AT_count:
11825       return "DW_AT_count";
11826     case DW_AT_data_member_location:
11827       return "DW_AT_data_member_location";
11828     case DW_AT_decl_column:
11829       return "DW_AT_decl_column";
11830     case DW_AT_decl_file:
11831       return "DW_AT_decl_file";
11832     case DW_AT_decl_line:
11833       return "DW_AT_decl_line";
11834     case DW_AT_declaration:
11835       return "DW_AT_declaration";
11836     case DW_AT_discr_list:
11837       return "DW_AT_discr_list";
11838     case DW_AT_encoding:
11839       return "DW_AT_encoding";
11840     case DW_AT_external:
11841       return "DW_AT_external";
11842     case DW_AT_frame_base:
11843       return "DW_AT_frame_base";
11844     case DW_AT_friend:
11845       return "DW_AT_friend";
11846     case DW_AT_identifier_case:
11847       return "DW_AT_identifier_case";
11848     case DW_AT_macro_info:
11849       return "DW_AT_macro_info";
11850     case DW_AT_namelist_items:
11851       return "DW_AT_namelist_items";
11852     case DW_AT_priority:
11853       return "DW_AT_priority";
11854     case DW_AT_segment:
11855       return "DW_AT_segment";
11856     case DW_AT_specification:
11857       return "DW_AT_specification";
11858     case DW_AT_static_link:
11859       return "DW_AT_static_link";
11860     case DW_AT_type:
11861       return "DW_AT_type";
11862     case DW_AT_use_location:
11863       return "DW_AT_use_location";
11864     case DW_AT_variable_parameter:
11865       return "DW_AT_variable_parameter";
11866     case DW_AT_virtuality:
11867       return "DW_AT_virtuality";
11868     case DW_AT_vtable_elem_location:
11869       return "DW_AT_vtable_elem_location";
11870     /* DWARF 3 values.  */
11871     case DW_AT_allocated:
11872       return "DW_AT_allocated";
11873     case DW_AT_associated:
11874       return "DW_AT_associated";
11875     case DW_AT_data_location:
11876       return "DW_AT_data_location";
11877     case DW_AT_byte_stride:
11878       return "DW_AT_byte_stride";
11879     case DW_AT_entry_pc:
11880       return "DW_AT_entry_pc";
11881     case DW_AT_use_UTF8:
11882       return "DW_AT_use_UTF8";
11883     case DW_AT_extension:
11884       return "DW_AT_extension";
11885     case DW_AT_ranges:
11886       return "DW_AT_ranges";
11887     case DW_AT_trampoline:
11888       return "DW_AT_trampoline";
11889     case DW_AT_call_column:
11890       return "DW_AT_call_column";
11891     case DW_AT_call_file:
11892       return "DW_AT_call_file";
11893     case DW_AT_call_line:
11894       return "DW_AT_call_line";
11895     case DW_AT_description:
11896       return "DW_AT_description";
11897     case DW_AT_binary_scale:
11898       return "DW_AT_binary_scale";
11899     case DW_AT_decimal_scale:
11900       return "DW_AT_decimal_scale";
11901     case DW_AT_small:
11902       return "DW_AT_small";
11903     case DW_AT_decimal_sign:
11904       return "DW_AT_decimal_sign";
11905     case DW_AT_digit_count:
11906       return "DW_AT_digit_count";
11907     case DW_AT_picture_string:
11908       return "DW_AT_picture_string";
11909     case DW_AT_mutable:
11910       return "DW_AT_mutable";
11911     case DW_AT_threads_scaled:
11912       return "DW_AT_threads_scaled";
11913     case DW_AT_explicit:
11914       return "DW_AT_explicit";
11915     case DW_AT_object_pointer:
11916       return "DW_AT_object_pointer";
11917     case DW_AT_endianity:
11918       return "DW_AT_endianity";
11919     case DW_AT_elemental:
11920       return "DW_AT_elemental";
11921     case DW_AT_pure:
11922       return "DW_AT_pure";
11923     case DW_AT_recursive:
11924       return "DW_AT_recursive";
11925     /* DWARF 4 values.  */
11926     case DW_AT_signature:
11927       return "DW_AT_signature";
11928     case DW_AT_linkage_name:
11929       return "DW_AT_linkage_name";
11930     /* SGI/MIPS extensions.  */
11931 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11932     case DW_AT_MIPS_fde:
11933       return "DW_AT_MIPS_fde";
11934 #endif
11935     case DW_AT_MIPS_loop_begin:
11936       return "DW_AT_MIPS_loop_begin";
11937     case DW_AT_MIPS_tail_loop_begin:
11938       return "DW_AT_MIPS_tail_loop_begin";
11939     case DW_AT_MIPS_epilog_begin:
11940       return "DW_AT_MIPS_epilog_begin";
11941     case DW_AT_MIPS_loop_unroll_factor:
11942       return "DW_AT_MIPS_loop_unroll_factor";
11943     case DW_AT_MIPS_software_pipeline_depth:
11944       return "DW_AT_MIPS_software_pipeline_depth";
11945     case DW_AT_MIPS_linkage_name:
11946       return "DW_AT_MIPS_linkage_name";
11947     case DW_AT_MIPS_stride:
11948       return "DW_AT_MIPS_stride";
11949     case DW_AT_MIPS_abstract_name:
11950       return "DW_AT_MIPS_abstract_name";
11951     case DW_AT_MIPS_clone_origin:
11952       return "DW_AT_MIPS_clone_origin";
11953     case DW_AT_MIPS_has_inlines:
11954       return "DW_AT_MIPS_has_inlines";
11955     /* HP extensions.  */
11956 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11957     case DW_AT_HP_block_index:
11958       return "DW_AT_HP_block_index";
11959 #endif
11960     case DW_AT_HP_unmodifiable:
11961       return "DW_AT_HP_unmodifiable";
11962     case DW_AT_HP_actuals_stmt_list:
11963       return "DW_AT_HP_actuals_stmt_list";
11964     case DW_AT_HP_proc_per_section:
11965       return "DW_AT_HP_proc_per_section";
11966     case DW_AT_HP_raw_data_ptr:
11967       return "DW_AT_HP_raw_data_ptr";
11968     case DW_AT_HP_pass_by_reference:
11969       return "DW_AT_HP_pass_by_reference";
11970     case DW_AT_HP_opt_level:
11971       return "DW_AT_HP_opt_level";
11972     case DW_AT_HP_prof_version_id:
11973       return "DW_AT_HP_prof_version_id";
11974     case DW_AT_HP_opt_flags:
11975       return "DW_AT_HP_opt_flags";
11976     case DW_AT_HP_cold_region_low_pc:
11977       return "DW_AT_HP_cold_region_low_pc";
11978     case DW_AT_HP_cold_region_high_pc:
11979       return "DW_AT_HP_cold_region_high_pc";
11980     case DW_AT_HP_all_variables_modifiable:
11981       return "DW_AT_HP_all_variables_modifiable";
11982     case DW_AT_HP_linkage_name:
11983       return "DW_AT_HP_linkage_name";
11984     case DW_AT_HP_prof_flags:
11985       return "DW_AT_HP_prof_flags";
11986     /* GNU extensions.  */
11987     case DW_AT_sf_names:
11988       return "DW_AT_sf_names";
11989     case DW_AT_src_info:
11990       return "DW_AT_src_info";
11991     case DW_AT_mac_info:
11992       return "DW_AT_mac_info";
11993     case DW_AT_src_coords:
11994       return "DW_AT_src_coords";
11995     case DW_AT_body_begin:
11996       return "DW_AT_body_begin";
11997     case DW_AT_body_end:
11998       return "DW_AT_body_end";
11999     case DW_AT_GNU_vector:
12000       return "DW_AT_GNU_vector";
12001     case DW_AT_GNU_odr_signature:
12002       return "DW_AT_GNU_odr_signature";
12003     /* VMS extensions.  */
12004     case DW_AT_VMS_rtnbeg_pd_address:
12005       return "DW_AT_VMS_rtnbeg_pd_address";
12006     /* UPC extension.  */
12007     case DW_AT_upc_threads_scaled:
12008       return "DW_AT_upc_threads_scaled";
12009     /* PGI (STMicroelectronics) extensions.  */
12010     case DW_AT_PGI_lbase:
12011       return "DW_AT_PGI_lbase";
12012     case DW_AT_PGI_soffset:
12013       return "DW_AT_PGI_soffset";
12014     case DW_AT_PGI_lstride:
12015       return "DW_AT_PGI_lstride";
12016     default:
12017       return "DW_AT_<unknown>";
12018     }
12019 }
12020
12021 /* Convert a DWARF value form code into its string name.  */
12022
12023 static char *
12024 dwarf_form_name (unsigned form)
12025 {
12026   switch (form)
12027     {
12028     case DW_FORM_addr:
12029       return "DW_FORM_addr";
12030     case DW_FORM_block2:
12031       return "DW_FORM_block2";
12032     case DW_FORM_block4:
12033       return "DW_FORM_block4";
12034     case DW_FORM_data2:
12035       return "DW_FORM_data2";
12036     case DW_FORM_data4:
12037       return "DW_FORM_data4";
12038     case DW_FORM_data8:
12039       return "DW_FORM_data8";
12040     case DW_FORM_string:
12041       return "DW_FORM_string";
12042     case DW_FORM_block:
12043       return "DW_FORM_block";
12044     case DW_FORM_block1:
12045       return "DW_FORM_block1";
12046     case DW_FORM_data1:
12047       return "DW_FORM_data1";
12048     case DW_FORM_flag:
12049       return "DW_FORM_flag";
12050     case DW_FORM_sdata:
12051       return "DW_FORM_sdata";
12052     case DW_FORM_strp:
12053       return "DW_FORM_strp";
12054     case DW_FORM_udata:
12055       return "DW_FORM_udata";
12056     case DW_FORM_ref_addr:
12057       return "DW_FORM_ref_addr";
12058     case DW_FORM_ref1:
12059       return "DW_FORM_ref1";
12060     case DW_FORM_ref2:
12061       return "DW_FORM_ref2";
12062     case DW_FORM_ref4:
12063       return "DW_FORM_ref4";
12064     case DW_FORM_ref8:
12065       return "DW_FORM_ref8";
12066     case DW_FORM_ref_udata:
12067       return "DW_FORM_ref_udata";
12068     case DW_FORM_indirect:
12069       return "DW_FORM_indirect";
12070     case DW_FORM_sec_offset:
12071       return "DW_FORM_sec_offset";
12072     case DW_FORM_exprloc:
12073       return "DW_FORM_exprloc";
12074     case DW_FORM_flag_present:
12075       return "DW_FORM_flag_present";
12076     case DW_FORM_sig8:
12077       return "DW_FORM_sig8";
12078     default:
12079       return "DW_FORM_<unknown>";
12080     }
12081 }
12082
12083 /* Convert a DWARF stack opcode into its string name.  */
12084
12085 const char *
12086 dwarf_stack_op_name (unsigned op, int def)
12087 {
12088   switch (op)
12089     {
12090     case DW_OP_addr:
12091       return "DW_OP_addr";
12092     case DW_OP_deref:
12093       return "DW_OP_deref";
12094     case DW_OP_const1u:
12095       return "DW_OP_const1u";
12096     case DW_OP_const1s:
12097       return "DW_OP_const1s";
12098     case DW_OP_const2u:
12099       return "DW_OP_const2u";
12100     case DW_OP_const2s:
12101       return "DW_OP_const2s";
12102     case DW_OP_const4u:
12103       return "DW_OP_const4u";
12104     case DW_OP_const4s:
12105       return "DW_OP_const4s";
12106     case DW_OP_const8u:
12107       return "DW_OP_const8u";
12108     case DW_OP_const8s:
12109       return "DW_OP_const8s";
12110     case DW_OP_constu:
12111       return "DW_OP_constu";
12112     case DW_OP_consts:
12113       return "DW_OP_consts";
12114     case DW_OP_dup:
12115       return "DW_OP_dup";
12116     case DW_OP_drop:
12117       return "DW_OP_drop";
12118     case DW_OP_over:
12119       return "DW_OP_over";
12120     case DW_OP_pick:
12121       return "DW_OP_pick";
12122     case DW_OP_swap:
12123       return "DW_OP_swap";
12124     case DW_OP_rot:
12125       return "DW_OP_rot";
12126     case DW_OP_xderef:
12127       return "DW_OP_xderef";
12128     case DW_OP_abs:
12129       return "DW_OP_abs";
12130     case DW_OP_and:
12131       return "DW_OP_and";
12132     case DW_OP_div:
12133       return "DW_OP_div";
12134     case DW_OP_minus:
12135       return "DW_OP_minus";
12136     case DW_OP_mod:
12137       return "DW_OP_mod";
12138     case DW_OP_mul:
12139       return "DW_OP_mul";
12140     case DW_OP_neg:
12141       return "DW_OP_neg";
12142     case DW_OP_not:
12143       return "DW_OP_not";
12144     case DW_OP_or:
12145       return "DW_OP_or";
12146     case DW_OP_plus:
12147       return "DW_OP_plus";
12148     case DW_OP_plus_uconst:
12149       return "DW_OP_plus_uconst";
12150     case DW_OP_shl:
12151       return "DW_OP_shl";
12152     case DW_OP_shr:
12153       return "DW_OP_shr";
12154     case DW_OP_shra:
12155       return "DW_OP_shra";
12156     case DW_OP_xor:
12157       return "DW_OP_xor";
12158     case DW_OP_bra:
12159       return "DW_OP_bra";
12160     case DW_OP_eq:
12161       return "DW_OP_eq";
12162     case DW_OP_ge:
12163       return "DW_OP_ge";
12164     case DW_OP_gt:
12165       return "DW_OP_gt";
12166     case DW_OP_le:
12167       return "DW_OP_le";
12168     case DW_OP_lt:
12169       return "DW_OP_lt";
12170     case DW_OP_ne:
12171       return "DW_OP_ne";
12172     case DW_OP_skip:
12173       return "DW_OP_skip";
12174     case DW_OP_lit0:
12175       return "DW_OP_lit0";
12176     case DW_OP_lit1:
12177       return "DW_OP_lit1";
12178     case DW_OP_lit2:
12179       return "DW_OP_lit2";
12180     case DW_OP_lit3:
12181       return "DW_OP_lit3";
12182     case DW_OP_lit4:
12183       return "DW_OP_lit4";
12184     case DW_OP_lit5:
12185       return "DW_OP_lit5";
12186     case DW_OP_lit6:
12187       return "DW_OP_lit6";
12188     case DW_OP_lit7:
12189       return "DW_OP_lit7";
12190     case DW_OP_lit8:
12191       return "DW_OP_lit8";
12192     case DW_OP_lit9:
12193       return "DW_OP_lit9";
12194     case DW_OP_lit10:
12195       return "DW_OP_lit10";
12196     case DW_OP_lit11:
12197       return "DW_OP_lit11";
12198     case DW_OP_lit12:
12199       return "DW_OP_lit12";
12200     case DW_OP_lit13:
12201       return "DW_OP_lit13";
12202     case DW_OP_lit14:
12203       return "DW_OP_lit14";
12204     case DW_OP_lit15:
12205       return "DW_OP_lit15";
12206     case DW_OP_lit16:
12207       return "DW_OP_lit16";
12208     case DW_OP_lit17:
12209       return "DW_OP_lit17";
12210     case DW_OP_lit18:
12211       return "DW_OP_lit18";
12212     case DW_OP_lit19:
12213       return "DW_OP_lit19";
12214     case DW_OP_lit20:
12215       return "DW_OP_lit20";
12216     case DW_OP_lit21:
12217       return "DW_OP_lit21";
12218     case DW_OP_lit22:
12219       return "DW_OP_lit22";
12220     case DW_OP_lit23:
12221       return "DW_OP_lit23";
12222     case DW_OP_lit24:
12223       return "DW_OP_lit24";
12224     case DW_OP_lit25:
12225       return "DW_OP_lit25";
12226     case DW_OP_lit26:
12227       return "DW_OP_lit26";
12228     case DW_OP_lit27:
12229       return "DW_OP_lit27";
12230     case DW_OP_lit28:
12231       return "DW_OP_lit28";
12232     case DW_OP_lit29:
12233       return "DW_OP_lit29";
12234     case DW_OP_lit30:
12235       return "DW_OP_lit30";
12236     case DW_OP_lit31:
12237       return "DW_OP_lit31";
12238     case DW_OP_reg0:
12239       return "DW_OP_reg0";
12240     case DW_OP_reg1:
12241       return "DW_OP_reg1";
12242     case DW_OP_reg2:
12243       return "DW_OP_reg2";
12244     case DW_OP_reg3:
12245       return "DW_OP_reg3";
12246     case DW_OP_reg4:
12247       return "DW_OP_reg4";
12248     case DW_OP_reg5:
12249       return "DW_OP_reg5";
12250     case DW_OP_reg6:
12251       return "DW_OP_reg6";
12252     case DW_OP_reg7:
12253       return "DW_OP_reg7";
12254     case DW_OP_reg8:
12255       return "DW_OP_reg8";
12256     case DW_OP_reg9:
12257       return "DW_OP_reg9";
12258     case DW_OP_reg10:
12259       return "DW_OP_reg10";
12260     case DW_OP_reg11:
12261       return "DW_OP_reg11";
12262     case DW_OP_reg12:
12263       return "DW_OP_reg12";
12264     case DW_OP_reg13:
12265       return "DW_OP_reg13";
12266     case DW_OP_reg14:
12267       return "DW_OP_reg14";
12268     case DW_OP_reg15:
12269       return "DW_OP_reg15";
12270     case DW_OP_reg16:
12271       return "DW_OP_reg16";
12272     case DW_OP_reg17:
12273       return "DW_OP_reg17";
12274     case DW_OP_reg18:
12275       return "DW_OP_reg18";
12276     case DW_OP_reg19:
12277       return "DW_OP_reg19";
12278     case DW_OP_reg20:
12279       return "DW_OP_reg20";
12280     case DW_OP_reg21:
12281       return "DW_OP_reg21";
12282     case DW_OP_reg22:
12283       return "DW_OP_reg22";
12284     case DW_OP_reg23:
12285       return "DW_OP_reg23";
12286     case DW_OP_reg24:
12287       return "DW_OP_reg24";
12288     case DW_OP_reg25:
12289       return "DW_OP_reg25";
12290     case DW_OP_reg26:
12291       return "DW_OP_reg26";
12292     case DW_OP_reg27:
12293       return "DW_OP_reg27";
12294     case DW_OP_reg28:
12295       return "DW_OP_reg28";
12296     case DW_OP_reg29:
12297       return "DW_OP_reg29";
12298     case DW_OP_reg30:
12299       return "DW_OP_reg30";
12300     case DW_OP_reg31:
12301       return "DW_OP_reg31";
12302     case DW_OP_breg0:
12303       return "DW_OP_breg0";
12304     case DW_OP_breg1:
12305       return "DW_OP_breg1";
12306     case DW_OP_breg2:
12307       return "DW_OP_breg2";
12308     case DW_OP_breg3:
12309       return "DW_OP_breg3";
12310     case DW_OP_breg4:
12311       return "DW_OP_breg4";
12312     case DW_OP_breg5:
12313       return "DW_OP_breg5";
12314     case DW_OP_breg6:
12315       return "DW_OP_breg6";
12316     case DW_OP_breg7:
12317       return "DW_OP_breg7";
12318     case DW_OP_breg8:
12319       return "DW_OP_breg8";
12320     case DW_OP_breg9:
12321       return "DW_OP_breg9";
12322     case DW_OP_breg10:
12323       return "DW_OP_breg10";
12324     case DW_OP_breg11:
12325       return "DW_OP_breg11";
12326     case DW_OP_breg12:
12327       return "DW_OP_breg12";
12328     case DW_OP_breg13:
12329       return "DW_OP_breg13";
12330     case DW_OP_breg14:
12331       return "DW_OP_breg14";
12332     case DW_OP_breg15:
12333       return "DW_OP_breg15";
12334     case DW_OP_breg16:
12335       return "DW_OP_breg16";
12336     case DW_OP_breg17:
12337       return "DW_OP_breg17";
12338     case DW_OP_breg18:
12339       return "DW_OP_breg18";
12340     case DW_OP_breg19:
12341       return "DW_OP_breg19";
12342     case DW_OP_breg20:
12343       return "DW_OP_breg20";
12344     case DW_OP_breg21:
12345       return "DW_OP_breg21";
12346     case DW_OP_breg22:
12347       return "DW_OP_breg22";
12348     case DW_OP_breg23:
12349       return "DW_OP_breg23";
12350     case DW_OP_breg24:
12351       return "DW_OP_breg24";
12352     case DW_OP_breg25:
12353       return "DW_OP_breg25";
12354     case DW_OP_breg26:
12355       return "DW_OP_breg26";
12356     case DW_OP_breg27:
12357       return "DW_OP_breg27";
12358     case DW_OP_breg28:
12359       return "DW_OP_breg28";
12360     case DW_OP_breg29:
12361       return "DW_OP_breg29";
12362     case DW_OP_breg30:
12363       return "DW_OP_breg30";
12364     case DW_OP_breg31:
12365       return "DW_OP_breg31";
12366     case DW_OP_regx:
12367       return "DW_OP_regx";
12368     case DW_OP_fbreg:
12369       return "DW_OP_fbreg";
12370     case DW_OP_bregx:
12371       return "DW_OP_bregx";
12372     case DW_OP_piece:
12373       return "DW_OP_piece";
12374     case DW_OP_deref_size:
12375       return "DW_OP_deref_size";
12376     case DW_OP_xderef_size:
12377       return "DW_OP_xderef_size";
12378     case DW_OP_nop:
12379       return "DW_OP_nop";
12380     /* DWARF 3 extensions.  */
12381     case DW_OP_push_object_address:
12382       return "DW_OP_push_object_address";
12383     case DW_OP_call2:
12384       return "DW_OP_call2";
12385     case DW_OP_call4:
12386       return "DW_OP_call4";
12387     case DW_OP_call_ref:
12388       return "DW_OP_call_ref";
12389     case DW_OP_form_tls_address:
12390       return "DW_OP_form_tls_address";
12391     case DW_OP_call_frame_cfa:
12392       return "DW_OP_call_frame_cfa";
12393     case DW_OP_bit_piece:
12394       return "DW_OP_bit_piece";
12395     /* DWARF 4 extensions.  */
12396     case DW_OP_implicit_value:
12397       return "DW_OP_implicit_value";
12398     case DW_OP_stack_value:
12399       return "DW_OP_stack_value";
12400     /* GNU extensions.  */
12401     case DW_OP_GNU_push_tls_address:
12402       return "DW_OP_GNU_push_tls_address";
12403     case DW_OP_GNU_uninit:
12404       return "DW_OP_GNU_uninit";
12405     default:
12406       return def ? "OP_<unknown>" : NULL;
12407     }
12408 }
12409
12410 static char *
12411 dwarf_bool_name (unsigned mybool)
12412 {
12413   if (mybool)
12414     return "TRUE";
12415   else
12416     return "FALSE";
12417 }
12418
12419 /* Convert a DWARF type code into its string name.  */
12420
12421 static char *
12422 dwarf_type_encoding_name (unsigned enc)
12423 {
12424   switch (enc)
12425     {
12426     case DW_ATE_void:
12427       return "DW_ATE_void";
12428     case DW_ATE_address:
12429       return "DW_ATE_address";
12430     case DW_ATE_boolean:
12431       return "DW_ATE_boolean";
12432     case DW_ATE_complex_float:
12433       return "DW_ATE_complex_float";
12434     case DW_ATE_float:
12435       return "DW_ATE_float";
12436     case DW_ATE_signed:
12437       return "DW_ATE_signed";
12438     case DW_ATE_signed_char:
12439       return "DW_ATE_signed_char";
12440     case DW_ATE_unsigned:
12441       return "DW_ATE_unsigned";
12442     case DW_ATE_unsigned_char:
12443       return "DW_ATE_unsigned_char";
12444     /* DWARF 3.  */
12445     case DW_ATE_imaginary_float:
12446       return "DW_ATE_imaginary_float";
12447     case DW_ATE_packed_decimal:
12448       return "DW_ATE_packed_decimal";
12449     case DW_ATE_numeric_string:
12450       return "DW_ATE_numeric_string";
12451     case DW_ATE_edited:
12452       return "DW_ATE_edited";
12453     case DW_ATE_signed_fixed:
12454       return "DW_ATE_signed_fixed";
12455     case DW_ATE_unsigned_fixed:
12456       return "DW_ATE_unsigned_fixed";
12457     case DW_ATE_decimal_float:
12458       return "DW_ATE_decimal_float";
12459     /* DWARF 4.  */
12460     case DW_ATE_UTF:
12461       return "DW_ATE_UTF";
12462     /* HP extensions.  */
12463     case DW_ATE_HP_float80:
12464       return "DW_ATE_HP_float80";
12465     case DW_ATE_HP_complex_float80:
12466       return "DW_ATE_HP_complex_float80";
12467     case DW_ATE_HP_float128:
12468       return "DW_ATE_HP_float128";
12469     case DW_ATE_HP_complex_float128:
12470       return "DW_ATE_HP_complex_float128";
12471     case DW_ATE_HP_floathpintel:
12472       return "DW_ATE_HP_floathpintel";
12473     case DW_ATE_HP_imaginary_float80:
12474       return "DW_ATE_HP_imaginary_float80";
12475     case DW_ATE_HP_imaginary_float128:
12476       return "DW_ATE_HP_imaginary_float128";
12477     default:
12478       return "DW_ATE_<unknown>";
12479     }
12480 }
12481
12482 /* Convert a DWARF call frame info operation to its string name. */
12483
12484 #if 0
12485 static char *
12486 dwarf_cfi_name (unsigned cfi_opc)
12487 {
12488   switch (cfi_opc)
12489     {
12490     case DW_CFA_advance_loc:
12491       return "DW_CFA_advance_loc";
12492     case DW_CFA_offset:
12493       return "DW_CFA_offset";
12494     case DW_CFA_restore:
12495       return "DW_CFA_restore";
12496     case DW_CFA_nop:
12497       return "DW_CFA_nop";
12498     case DW_CFA_set_loc:
12499       return "DW_CFA_set_loc";
12500     case DW_CFA_advance_loc1:
12501       return "DW_CFA_advance_loc1";
12502     case DW_CFA_advance_loc2:
12503       return "DW_CFA_advance_loc2";
12504     case DW_CFA_advance_loc4:
12505       return "DW_CFA_advance_loc4";
12506     case DW_CFA_offset_extended:
12507       return "DW_CFA_offset_extended";
12508     case DW_CFA_restore_extended:
12509       return "DW_CFA_restore_extended";
12510     case DW_CFA_undefined:
12511       return "DW_CFA_undefined";
12512     case DW_CFA_same_value:
12513       return "DW_CFA_same_value";
12514     case DW_CFA_register:
12515       return "DW_CFA_register";
12516     case DW_CFA_remember_state:
12517       return "DW_CFA_remember_state";
12518     case DW_CFA_restore_state:
12519       return "DW_CFA_restore_state";
12520     case DW_CFA_def_cfa:
12521       return "DW_CFA_def_cfa";
12522     case DW_CFA_def_cfa_register:
12523       return "DW_CFA_def_cfa_register";
12524     case DW_CFA_def_cfa_offset:
12525       return "DW_CFA_def_cfa_offset";
12526     /* DWARF 3.  */
12527     case DW_CFA_def_cfa_expression:
12528       return "DW_CFA_def_cfa_expression";
12529     case DW_CFA_expression:
12530       return "DW_CFA_expression";
12531     case DW_CFA_offset_extended_sf:
12532       return "DW_CFA_offset_extended_sf";
12533     case DW_CFA_def_cfa_sf:
12534       return "DW_CFA_def_cfa_sf";
12535     case DW_CFA_def_cfa_offset_sf:
12536       return "DW_CFA_def_cfa_offset_sf";
12537     case DW_CFA_val_offset:
12538       return "DW_CFA_val_offset";
12539     case DW_CFA_val_offset_sf:
12540       return "DW_CFA_val_offset_sf";
12541     case DW_CFA_val_expression:
12542       return "DW_CFA_val_expression";
12543     /* SGI/MIPS specific.  */
12544     case DW_CFA_MIPS_advance_loc8:
12545       return "DW_CFA_MIPS_advance_loc8";
12546     /* GNU extensions.  */
12547     case DW_CFA_GNU_window_save:
12548       return "DW_CFA_GNU_window_save";
12549     case DW_CFA_GNU_args_size:
12550       return "DW_CFA_GNU_args_size";
12551     case DW_CFA_GNU_negative_offset_extended:
12552       return "DW_CFA_GNU_negative_offset_extended";
12553     default:
12554       return "DW_CFA_<unknown>";
12555     }
12556 }
12557 #endif
12558
12559 static void
12560 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12561 {
12562   unsigned int i;
12563
12564   print_spaces (indent, f);
12565   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12566            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12567
12568   if (die->parent != NULL)
12569     {
12570       print_spaces (indent, f);
12571       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12572                           die->parent->offset);
12573     }
12574
12575   print_spaces (indent, f);
12576   fprintf_unfiltered (f, "  has children: %s\n",
12577            dwarf_bool_name (die->child != NULL));
12578
12579   print_spaces (indent, f);
12580   fprintf_unfiltered (f, "  attributes:\n");
12581
12582   for (i = 0; i < die->num_attrs; ++i)
12583     {
12584       print_spaces (indent, f);
12585       fprintf_unfiltered (f, "    %s (%s) ",
12586                dwarf_attr_name (die->attrs[i].name),
12587                dwarf_form_name (die->attrs[i].form));
12588
12589       switch (die->attrs[i].form)
12590         {
12591         case DW_FORM_ref_addr:
12592         case DW_FORM_addr:
12593           fprintf_unfiltered (f, "address: ");
12594           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12595           break;
12596         case DW_FORM_block2:
12597         case DW_FORM_block4:
12598         case DW_FORM_block:
12599         case DW_FORM_block1:
12600           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12601           break;
12602         case DW_FORM_exprloc:
12603           fprintf_unfiltered (f, "expression: size %u",
12604                               DW_BLOCK (&die->attrs[i])->size);
12605           break;
12606         case DW_FORM_ref1:
12607         case DW_FORM_ref2:
12608         case DW_FORM_ref4:
12609           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12610                               (long) (DW_ADDR (&die->attrs[i])));
12611           break;
12612         case DW_FORM_data1:
12613         case DW_FORM_data2:
12614         case DW_FORM_data4:
12615         case DW_FORM_data8:
12616         case DW_FORM_udata:
12617         case DW_FORM_sdata:
12618           fprintf_unfiltered (f, "constant: %s",
12619                               pulongest (DW_UNSND (&die->attrs[i])));
12620           break;
12621         case DW_FORM_sec_offset:
12622           fprintf_unfiltered (f, "section offset: %s",
12623                               pulongest (DW_UNSND (&die->attrs[i])));
12624           break;
12625         case DW_FORM_sig8:
12626           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12627             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12628                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12629           else
12630             fprintf_unfiltered (f, "signatured type, offset: unknown");
12631           break;
12632         case DW_FORM_string:
12633         case DW_FORM_strp:
12634           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12635                    DW_STRING (&die->attrs[i])
12636                    ? DW_STRING (&die->attrs[i]) : "",
12637                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12638           break;
12639         case DW_FORM_flag:
12640           if (DW_UNSND (&die->attrs[i]))
12641             fprintf_unfiltered (f, "flag: TRUE");
12642           else
12643             fprintf_unfiltered (f, "flag: FALSE");
12644           break;
12645         case DW_FORM_flag_present:
12646           fprintf_unfiltered (f, "flag: TRUE");
12647           break;
12648         case DW_FORM_indirect:
12649           /* the reader will have reduced the indirect form to
12650              the "base form" so this form should not occur */
12651           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12652           break;
12653         default:
12654           fprintf_unfiltered (f, "unsupported attribute form: %d.",
12655                    die->attrs[i].form);
12656           break;
12657         }
12658       fprintf_unfiltered (f, "\n");
12659     }
12660 }
12661
12662 static void
12663 dump_die_for_error (struct die_info *die)
12664 {
12665   dump_die_shallow (gdb_stderr, 0, die);
12666 }
12667
12668 static void
12669 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12670 {
12671   int indent = level * 4;
12672
12673   gdb_assert (die != NULL);
12674
12675   if (level >= max_level)
12676     return;
12677
12678   dump_die_shallow (f, indent, die);
12679
12680   if (die->child != NULL)
12681     {
12682       print_spaces (indent, f);
12683       fprintf_unfiltered (f, "  Children:");
12684       if (level + 1 < max_level)
12685         {
12686           fprintf_unfiltered (f, "\n");
12687           dump_die_1 (f, level + 1, max_level, die->child);
12688         }
12689       else
12690         {
12691           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12692         }
12693     }
12694
12695   if (die->sibling != NULL && level > 0)
12696     {
12697       dump_die_1 (f, level, max_level, die->sibling);
12698     }
12699 }
12700
12701 /* This is called from the pdie macro in gdbinit.in.
12702    It's not static so gcc will keep a copy callable from gdb.  */
12703
12704 void
12705 dump_die (struct die_info *die, int max_level)
12706 {
12707   dump_die_1 (gdb_stdlog, 0, max_level, die);
12708 }
12709
12710 static void
12711 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12712 {
12713   void **slot;
12714
12715   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12716
12717   *slot = die;
12718 }
12719
12720 static int
12721 is_ref_attr (struct attribute *attr)
12722 {
12723   switch (attr->form)
12724     {
12725     case DW_FORM_ref_addr:
12726     case DW_FORM_ref1:
12727     case DW_FORM_ref2:
12728     case DW_FORM_ref4:
12729     case DW_FORM_ref8:
12730     case DW_FORM_ref_udata:
12731       return 1;
12732     default:
12733       return 0;
12734     }
12735 }
12736
12737 static unsigned int
12738 dwarf2_get_ref_die_offset (struct attribute *attr)
12739 {
12740   if (is_ref_attr (attr))
12741     return DW_ADDR (attr);
12742
12743   complaint (&symfile_complaints,
12744              _("unsupported die ref attribute form: '%s'"),
12745              dwarf_form_name (attr->form));
12746   return 0;
12747 }
12748
12749 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
12750  * the value held by the attribute is not constant.  */
12751
12752 static LONGEST
12753 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12754 {
12755   if (attr->form == DW_FORM_sdata)
12756     return DW_SND (attr);
12757   else if (attr->form == DW_FORM_udata
12758            || attr->form == DW_FORM_data1
12759            || attr->form == DW_FORM_data2
12760            || attr->form == DW_FORM_data4
12761            || attr->form == DW_FORM_data8)
12762     return DW_UNSND (attr);
12763   else
12764     {
12765       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12766                  dwarf_form_name (attr->form));
12767       return default_value;
12768     }
12769 }
12770
12771 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
12772    unit and add it to our queue.
12773    The result is non-zero if PER_CU was queued, otherwise the result is zero
12774    meaning either PER_CU is already queued or it is already loaded.  */
12775
12776 static int
12777 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12778                        struct dwarf2_per_cu_data *per_cu)
12779 {
12780   /* We may arrive here during partial symbol reading, if we need full
12781      DIEs to process an unusual case (e.g. template arguments).  Do
12782      not queue PER_CU, just tell our caller to load its DIEs.  */
12783   if (dwarf2_per_objfile->reading_partial_symbols)
12784     {
12785       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12786         return 1;
12787       return 0;
12788     }
12789
12790   /* Mark the dependence relation so that we don't flush PER_CU
12791      too early.  */
12792   dwarf2_add_dependence (this_cu, per_cu);
12793
12794   /* If it's already on the queue, we have nothing to do.  */
12795   if (per_cu->queued)
12796     return 0;
12797
12798   /* If the compilation unit is already loaded, just mark it as
12799      used.  */
12800   if (per_cu->cu != NULL)
12801     {
12802       per_cu->cu->last_used = 0;
12803       return 0;
12804     }
12805
12806   /* Add it to the queue.  */
12807   queue_comp_unit (per_cu, this_cu->objfile);
12808
12809   return 1;
12810 }
12811
12812 /* Follow reference or signature attribute ATTR of SRC_DIE.
12813    On entry *REF_CU is the CU of SRC_DIE.
12814    On exit *REF_CU is the CU of the result.  */
12815
12816 static struct die_info *
12817 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12818                        struct dwarf2_cu **ref_cu)
12819 {
12820   struct die_info *die;
12821
12822   if (is_ref_attr (attr))
12823     die = follow_die_ref (src_die, attr, ref_cu);
12824   else if (attr->form == DW_FORM_sig8)
12825     die = follow_die_sig (src_die, attr, ref_cu);
12826   else
12827     {
12828       dump_die_for_error (src_die);
12829       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12830              (*ref_cu)->objfile->name);
12831     }
12832
12833   return die;
12834 }
12835
12836 /* Follow reference OFFSET.
12837    On entry *REF_CU is the CU of the source die referencing OFFSET.
12838    On exit *REF_CU is the CU of the result.
12839    Returns NULL if OFFSET is invalid.  */
12840
12841 static struct die_info *
12842 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12843 {
12844   struct die_info temp_die;
12845   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12846
12847   gdb_assert (cu->per_cu != NULL);
12848
12849   target_cu = cu;
12850
12851   if (cu->per_cu->from_debug_types)
12852     {
12853       /* .debug_types CUs cannot reference anything outside their CU.
12854          If they need to, they have to reference a signatured type via
12855          DW_FORM_sig8.  */
12856       if (! offset_in_cu_p (&cu->header, offset))
12857         return NULL;
12858     }
12859   else if (! offset_in_cu_p (&cu->header, offset))
12860     {
12861       struct dwarf2_per_cu_data *per_cu;
12862
12863       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12864
12865       /* If necessary, add it to the queue and load its DIEs.  */
12866       if (maybe_queue_comp_unit (cu, per_cu))
12867         load_full_comp_unit (per_cu, cu->objfile);
12868
12869       target_cu = per_cu->cu;
12870     }
12871   else if (cu->dies == NULL)
12872     {
12873       /* We're loading full DIEs during partial symbol reading.  */
12874       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12875       load_full_comp_unit (cu->per_cu, cu->objfile);
12876     }
12877
12878   *ref_cu = target_cu;
12879   temp_die.offset = offset;
12880   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12881 }
12882
12883 /* Follow reference attribute ATTR of SRC_DIE.
12884    On entry *REF_CU is the CU of SRC_DIE.
12885    On exit *REF_CU is the CU of the result.  */
12886
12887 static struct die_info *
12888 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12889                 struct dwarf2_cu **ref_cu)
12890 {
12891   unsigned int offset = dwarf2_get_ref_die_offset (attr);
12892   struct dwarf2_cu *cu = *ref_cu;
12893   struct die_info *die;
12894
12895   die = follow_die_offset (offset, ref_cu);
12896   if (!die)
12897     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12898            "at 0x%x [in module %s]"),
12899            offset, src_die->offset, cu->objfile->name);
12900
12901   return die;
12902 }
12903
12904 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
12905    value is intended for DW_OP_call*.  */
12906
12907 struct dwarf2_locexpr_baton
12908 dwarf2_fetch_die_location_block (unsigned int offset,
12909                                  struct dwarf2_per_cu_data *per_cu)
12910 {
12911   struct dwarf2_cu *cu = per_cu->cu;
12912   struct die_info *die;
12913   struct attribute *attr;
12914   struct dwarf2_locexpr_baton retval;
12915
12916   die = follow_die_offset (offset, &cu);
12917   if (!die)
12918     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12919            offset, per_cu->cu->objfile->name);
12920
12921   attr = dwarf2_attr (die, DW_AT_location, cu);
12922   if (!attr)
12923     {
12924       /* DWARF: "If there is no such attribute, then there is no effect.".  */
12925
12926       retval.data = NULL;
12927       retval.size = 0;
12928     }
12929   else
12930     {
12931       if (!attr_form_is_block (attr))
12932         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12933                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12934                offset, per_cu->cu->objfile->name);
12935
12936       retval.data = DW_BLOCK (attr)->data;
12937       retval.size = DW_BLOCK (attr)->size;
12938     }
12939   retval.per_cu = cu->per_cu;
12940   return retval;
12941 }
12942
12943 /* Follow the signature attribute ATTR in SRC_DIE.
12944    On entry *REF_CU is the CU of SRC_DIE.
12945    On exit *REF_CU is the CU of the result.  */
12946
12947 static struct die_info *
12948 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12949                 struct dwarf2_cu **ref_cu)
12950 {
12951   struct objfile *objfile = (*ref_cu)->objfile;
12952   struct die_info temp_die;
12953   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12954   struct dwarf2_cu *sig_cu;
12955   struct die_info *die;
12956
12957   /* sig_type will be NULL if the signatured type is missing from
12958      the debug info.  */
12959   if (sig_type == NULL)
12960     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12961              "at 0x%x [in module %s]"),
12962            src_die->offset, objfile->name);
12963
12964   /* If necessary, add it to the queue and load its DIEs.  */
12965
12966   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12967     read_signatured_type (objfile, sig_type);
12968
12969   gdb_assert (sig_type->per_cu.cu != NULL);
12970
12971   sig_cu = sig_type->per_cu.cu;
12972   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12973   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12974   if (die)
12975     {
12976       *ref_cu = sig_cu;
12977       return die;
12978     }
12979
12980   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12981          "at 0x%x [in module %s]"),
12982          sig_type->type_offset, src_die->offset, objfile->name);
12983 }
12984
12985 /* Given an offset of a signatured type, return its signatured_type.  */
12986
12987 static struct signatured_type *
12988 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12989 {
12990   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12991   unsigned int length, initial_length_size;
12992   unsigned int sig_offset;
12993   struct signatured_type find_entry, *type_sig;
12994
12995   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12996   sig_offset = (initial_length_size
12997                 + 2 /*version*/
12998                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12999                 + 1 /*address_size*/);
13000   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13001   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13002
13003   /* This is only used to lookup previously recorded types.
13004      If we didn't find it, it's our bug.  */
13005   gdb_assert (type_sig != NULL);
13006   gdb_assert (offset == type_sig->offset);
13007
13008   return type_sig;
13009 }
13010
13011 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13012
13013 static void
13014 read_signatured_type_at_offset (struct objfile *objfile,
13015                                 unsigned int offset)
13016 {
13017   struct signatured_type *type_sig;
13018
13019   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13020
13021   /* We have the section offset, but we need the signature to do the
13022      hash table lookup.  */
13023   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13024
13025   gdb_assert (type_sig->per_cu.cu == NULL);
13026
13027   read_signatured_type (objfile, type_sig);
13028
13029   gdb_assert (type_sig->per_cu.cu != NULL);
13030 }
13031
13032 /* Read in a signatured type and build its CU and DIEs.  */
13033
13034 static void
13035 read_signatured_type (struct objfile *objfile,
13036                       struct signatured_type *type_sig)
13037 {
13038   gdb_byte *types_ptr;
13039   struct die_reader_specs reader_specs;
13040   struct dwarf2_cu *cu;
13041   ULONGEST signature;
13042   struct cleanup *back_to, *free_cu_cleanup;
13043   struct attribute *attr;
13044
13045   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13046   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13047
13048   gdb_assert (type_sig->per_cu.cu == NULL);
13049
13050   cu = xmalloc (sizeof (struct dwarf2_cu));
13051   memset (cu, 0, sizeof (struct dwarf2_cu));
13052   obstack_init (&cu->comp_unit_obstack);
13053   cu->objfile = objfile;
13054   type_sig->per_cu.cu = cu;
13055   cu->per_cu = &type_sig->per_cu;
13056
13057   /* If an error occurs while loading, release our storage.  */
13058   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13059
13060   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13061                                         types_ptr, objfile->obfd);
13062   gdb_assert (signature == type_sig->signature);
13063
13064   cu->die_hash
13065     = htab_create_alloc_ex (cu->header.length / 12,
13066                             die_hash,
13067                             die_eq,
13068                             NULL,
13069                             &cu->comp_unit_obstack,
13070                             hashtab_obstack_allocate,
13071                             dummy_obstack_deallocate);
13072
13073   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13074   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13075
13076   init_cu_die_reader (&reader_specs, cu);
13077
13078   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13079                                     NULL /*parent*/);
13080
13081   /* We try not to read any attributes in this function, because not
13082      all objfiles needed for references have been loaded yet, and symbol
13083      table processing isn't initialized.  But we have to set the CU language,
13084      or we won't be able to build types correctly.  */
13085   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13086   if (attr)
13087     set_cu_language (DW_UNSND (attr), cu);
13088   else
13089     set_cu_language (language_minimal, cu);
13090
13091   do_cleanups (back_to);
13092
13093   /* We've successfully allocated this compilation unit.  Let our caller
13094      clean it up when finished with it.  */
13095   discard_cleanups (free_cu_cleanup);
13096
13097   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13098   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13099 }
13100
13101 /* Decode simple location descriptions.
13102    Given a pointer to a dwarf block that defines a location, compute
13103    the location and return the value.
13104
13105    NOTE drow/2003-11-18: This function is called in two situations
13106    now: for the address of static or global variables (partial symbols
13107    only) and for offsets into structures which are expected to be
13108    (more or less) constant.  The partial symbol case should go away,
13109    and only the constant case should remain.  That will let this
13110    function complain more accurately.  A few special modes are allowed
13111    without complaint for global variables (for instance, global
13112    register values and thread-local values).
13113
13114    A location description containing no operations indicates that the
13115    object is optimized out.  The return value is 0 for that case.
13116    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13117    callers will only want a very basic result and this can become a
13118    complaint.
13119
13120    Note that stack[0] is unused except as a default error return.
13121    Note that stack overflow is not yet handled.  */
13122
13123 static CORE_ADDR
13124 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13125 {
13126   struct objfile *objfile = cu->objfile;
13127   int i;
13128   int size = blk->size;
13129   gdb_byte *data = blk->data;
13130   CORE_ADDR stack[64];
13131   int stacki;
13132   unsigned int bytes_read, unsnd;
13133   gdb_byte op;
13134
13135   i = 0;
13136   stacki = 0;
13137   stack[stacki] = 0;
13138
13139   while (i < size)
13140     {
13141       op = data[i++];
13142       switch (op)
13143         {
13144         case DW_OP_lit0:
13145         case DW_OP_lit1:
13146         case DW_OP_lit2:
13147         case DW_OP_lit3:
13148         case DW_OP_lit4:
13149         case DW_OP_lit5:
13150         case DW_OP_lit6:
13151         case DW_OP_lit7:
13152         case DW_OP_lit8:
13153         case DW_OP_lit9:
13154         case DW_OP_lit10:
13155         case DW_OP_lit11:
13156         case DW_OP_lit12:
13157         case DW_OP_lit13:
13158         case DW_OP_lit14:
13159         case DW_OP_lit15:
13160         case DW_OP_lit16:
13161         case DW_OP_lit17:
13162         case DW_OP_lit18:
13163         case DW_OP_lit19:
13164         case DW_OP_lit20:
13165         case DW_OP_lit21:
13166         case DW_OP_lit22:
13167         case DW_OP_lit23:
13168         case DW_OP_lit24:
13169         case DW_OP_lit25:
13170         case DW_OP_lit26:
13171         case DW_OP_lit27:
13172         case DW_OP_lit28:
13173         case DW_OP_lit29:
13174         case DW_OP_lit30:
13175         case DW_OP_lit31:
13176           stack[++stacki] = op - DW_OP_lit0;
13177           break;
13178
13179         case DW_OP_reg0:
13180         case DW_OP_reg1:
13181         case DW_OP_reg2:
13182         case DW_OP_reg3:
13183         case DW_OP_reg4:
13184         case DW_OP_reg5:
13185         case DW_OP_reg6:
13186         case DW_OP_reg7:
13187         case DW_OP_reg8:
13188         case DW_OP_reg9:
13189         case DW_OP_reg10:
13190         case DW_OP_reg11:
13191         case DW_OP_reg12:
13192         case DW_OP_reg13:
13193         case DW_OP_reg14:
13194         case DW_OP_reg15:
13195         case DW_OP_reg16:
13196         case DW_OP_reg17:
13197         case DW_OP_reg18:
13198         case DW_OP_reg19:
13199         case DW_OP_reg20:
13200         case DW_OP_reg21:
13201         case DW_OP_reg22:
13202         case DW_OP_reg23:
13203         case DW_OP_reg24:
13204         case DW_OP_reg25:
13205         case DW_OP_reg26:
13206         case DW_OP_reg27:
13207         case DW_OP_reg28:
13208         case DW_OP_reg29:
13209         case DW_OP_reg30:
13210         case DW_OP_reg31:
13211           stack[++stacki] = op - DW_OP_reg0;
13212           if (i < size)
13213             dwarf2_complex_location_expr_complaint ();
13214           break;
13215
13216         case DW_OP_regx:
13217           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13218           i += bytes_read;
13219           stack[++stacki] = unsnd;
13220           if (i < size)
13221             dwarf2_complex_location_expr_complaint ();
13222           break;
13223
13224         case DW_OP_addr:
13225           stack[++stacki] = read_address (objfile->obfd, &data[i],
13226                                           cu, &bytes_read);
13227           i += bytes_read;
13228           break;
13229
13230         case DW_OP_const1u:
13231           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13232           i += 1;
13233           break;
13234
13235         case DW_OP_const1s:
13236           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13237           i += 1;
13238           break;
13239
13240         case DW_OP_const2u:
13241           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13242           i += 2;
13243           break;
13244
13245         case DW_OP_const2s:
13246           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13247           i += 2;
13248           break;
13249
13250         case DW_OP_const4u:
13251           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13252           i += 4;
13253           break;
13254
13255         case DW_OP_const4s:
13256           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13257           i += 4;
13258           break;
13259
13260         case DW_OP_constu:
13261           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13262                                                   &bytes_read);
13263           i += bytes_read;
13264           break;
13265
13266         case DW_OP_consts:
13267           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13268           i += bytes_read;
13269           break;
13270
13271         case DW_OP_dup:
13272           stack[stacki + 1] = stack[stacki];
13273           stacki++;
13274           break;
13275
13276         case DW_OP_plus:
13277           stack[stacki - 1] += stack[stacki];
13278           stacki--;
13279           break;
13280
13281         case DW_OP_plus_uconst:
13282           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13283           i += bytes_read;
13284           break;
13285
13286         case DW_OP_minus:
13287           stack[stacki - 1] -= stack[stacki];
13288           stacki--;
13289           break;
13290
13291         case DW_OP_deref:
13292           /* If we're not the last op, then we definitely can't encode
13293              this using GDB's address_class enum.  This is valid for partial
13294              global symbols, although the variable's address will be bogus
13295              in the psymtab.  */
13296           if (i < size)
13297             dwarf2_complex_location_expr_complaint ();
13298           break;
13299
13300         case DW_OP_GNU_push_tls_address:
13301           /* The top of the stack has the offset from the beginning
13302              of the thread control block at which the variable is located.  */
13303           /* Nothing should follow this operator, so the top of stack would
13304              be returned.  */
13305           /* This is valid for partial global symbols, but the variable's
13306              address will be bogus in the psymtab.  */
13307           if (i < size)
13308             dwarf2_complex_location_expr_complaint ();
13309           break;
13310
13311         case DW_OP_GNU_uninit:
13312           break;
13313
13314         default:
13315           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13316                      dwarf_stack_op_name (op, 1));
13317           return (stack[stacki]);
13318         }
13319     }
13320   return (stack[stacki]);
13321 }
13322
13323 /* memory allocation interface */
13324
13325 static struct dwarf_block *
13326 dwarf_alloc_block (struct dwarf2_cu *cu)
13327 {
13328   struct dwarf_block *blk;
13329
13330   blk = (struct dwarf_block *)
13331     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13332   return (blk);
13333 }
13334
13335 static struct abbrev_info *
13336 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13337 {
13338   struct abbrev_info *abbrev;
13339
13340   abbrev = (struct abbrev_info *)
13341     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13342   memset (abbrev, 0, sizeof (struct abbrev_info));
13343   return (abbrev);
13344 }
13345
13346 static struct die_info *
13347 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13348 {
13349   struct die_info *die;
13350   size_t size = sizeof (struct die_info);
13351
13352   if (num_attrs > 1)
13353     size += (num_attrs - 1) * sizeof (struct attribute);
13354
13355   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13356   memset (die, 0, sizeof (struct die_info));
13357   return (die);
13358 }
13359
13360 \f
13361 /* Macro support.  */
13362
13363
13364 /* Return the full name of file number I in *LH's file name table.
13365    Use COMP_DIR as the name of the current directory of the
13366    compilation.  The result is allocated using xmalloc; the caller is
13367    responsible for freeing it.  */
13368 static char *
13369 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13370 {
13371   /* Is the file number a valid index into the line header's file name
13372      table?  Remember that file numbers start with one, not zero.  */
13373   if (1 <= file && file <= lh->num_file_names)
13374     {
13375       struct file_entry *fe = &lh->file_names[file - 1];
13376
13377       if (IS_ABSOLUTE_PATH (fe->name))
13378         return xstrdup (fe->name);
13379       else
13380         {
13381           const char *dir;
13382           int dir_len;
13383           char *full_name;
13384
13385           if (fe->dir_index)
13386             dir = lh->include_dirs[fe->dir_index - 1];
13387           else
13388             dir = comp_dir;
13389
13390           if (dir)
13391             {
13392               dir_len = strlen (dir);
13393               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13394               strcpy (full_name, dir);
13395               full_name[dir_len] = '/';
13396               strcpy (full_name + dir_len + 1, fe->name);
13397               return full_name;
13398             }
13399           else
13400             return xstrdup (fe->name);
13401         }
13402     }
13403   else
13404     {
13405       /* The compiler produced a bogus file number.  We can at least
13406          record the macro definitions made in the file, even if we
13407          won't be able to find the file by name.  */
13408       char fake_name[80];
13409
13410       sprintf (fake_name, "<bad macro file number %d>", file);
13411
13412       complaint (&symfile_complaints,
13413                  _("bad file number in macro information (%d)"),
13414                  file);
13415
13416       return xstrdup (fake_name);
13417     }
13418 }
13419
13420
13421 static struct macro_source_file *
13422 macro_start_file (int file, int line,
13423                   struct macro_source_file *current_file,
13424                   const char *comp_dir,
13425                   struct line_header *lh, struct objfile *objfile)
13426 {
13427   /* The full name of this source file.  */
13428   char *full_name = file_full_name (file, lh, comp_dir);
13429
13430   /* We don't create a macro table for this compilation unit
13431      at all until we actually get a filename.  */
13432   if (! pending_macros)
13433     pending_macros = new_macro_table (&objfile->objfile_obstack,
13434                                       objfile->macro_cache);
13435
13436   if (! current_file)
13437     /* If we have no current file, then this must be the start_file
13438        directive for the compilation unit's main source file.  */
13439     current_file = macro_set_main (pending_macros, full_name);
13440   else
13441     current_file = macro_include (current_file, line, full_name);
13442
13443   xfree (full_name);
13444
13445   return current_file;
13446 }
13447
13448
13449 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13450    followed by a null byte.  */
13451 static char *
13452 copy_string (const char *buf, int len)
13453 {
13454   char *s = xmalloc (len + 1);
13455
13456   memcpy (s, buf, len);
13457   s[len] = '\0';
13458   return s;
13459 }
13460
13461
13462 static const char *
13463 consume_improper_spaces (const char *p, const char *body)
13464 {
13465   if (*p == ' ')
13466     {
13467       complaint (&symfile_complaints,
13468                  _("macro definition contains spaces in formal argument list:\n`%s'"),
13469                  body);
13470
13471       while (*p == ' ')
13472         p++;
13473     }
13474
13475   return p;
13476 }
13477
13478
13479 static void
13480 parse_macro_definition (struct macro_source_file *file, int line,
13481                         const char *body)
13482 {
13483   const char *p;
13484
13485   /* The body string takes one of two forms.  For object-like macro
13486      definitions, it should be:
13487
13488         <macro name> " " <definition>
13489
13490      For function-like macro definitions, it should be:
13491
13492         <macro name> "() " <definition>
13493      or
13494         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13495
13496      Spaces may appear only where explicitly indicated, and in the
13497      <definition>.
13498
13499      The Dwarf 2 spec says that an object-like macro's name is always
13500      followed by a space, but versions of GCC around March 2002 omit
13501      the space when the macro's definition is the empty string.
13502
13503      The Dwarf 2 spec says that there should be no spaces between the
13504      formal arguments in a function-like macro's formal argument list,
13505      but versions of GCC around March 2002 include spaces after the
13506      commas.  */
13507
13508
13509   /* Find the extent of the macro name.  The macro name is terminated
13510      by either a space or null character (for an object-like macro) or
13511      an opening paren (for a function-like macro).  */
13512   for (p = body; *p; p++)
13513     if (*p == ' ' || *p == '(')
13514       break;
13515
13516   if (*p == ' ' || *p == '\0')
13517     {
13518       /* It's an object-like macro.  */
13519       int name_len = p - body;
13520       char *name = copy_string (body, name_len);
13521       const char *replacement;
13522
13523       if (*p == ' ')
13524         replacement = body + name_len + 1;
13525       else
13526         {
13527           dwarf2_macro_malformed_definition_complaint (body);
13528           replacement = body + name_len;
13529         }
13530
13531       macro_define_object (file, line, name, replacement);
13532
13533       xfree (name);
13534     }
13535   else if (*p == '(')
13536     {
13537       /* It's a function-like macro.  */
13538       char *name = copy_string (body, p - body);
13539       int argc = 0;
13540       int argv_size = 1;
13541       char **argv = xmalloc (argv_size * sizeof (*argv));
13542
13543       p++;
13544
13545       p = consume_improper_spaces (p, body);
13546
13547       /* Parse the formal argument list.  */
13548       while (*p && *p != ')')
13549         {
13550           /* Find the extent of the current argument name.  */
13551           const char *arg_start = p;
13552
13553           while (*p && *p != ',' && *p != ')' && *p != ' ')
13554             p++;
13555
13556           if (! *p || p == arg_start)
13557             dwarf2_macro_malformed_definition_complaint (body);
13558           else
13559             {
13560               /* Make sure argv has room for the new argument.  */
13561               if (argc >= argv_size)
13562                 {
13563                   argv_size *= 2;
13564                   argv = xrealloc (argv, argv_size * sizeof (*argv));
13565                 }
13566
13567               argv[argc++] = copy_string (arg_start, p - arg_start);
13568             }
13569
13570           p = consume_improper_spaces (p, body);
13571
13572           /* Consume the comma, if present.  */
13573           if (*p == ',')
13574             {
13575               p++;
13576
13577               p = consume_improper_spaces (p, body);
13578             }
13579         }
13580
13581       if (*p == ')')
13582         {
13583           p++;
13584
13585           if (*p == ' ')
13586             /* Perfectly formed definition, no complaints.  */
13587             macro_define_function (file, line, name,
13588                                    argc, (const char **) argv,
13589                                    p + 1);
13590           else if (*p == '\0')
13591             {
13592               /* Complain, but do define it.  */
13593               dwarf2_macro_malformed_definition_complaint (body);
13594               macro_define_function (file, line, name,
13595                                      argc, (const char **) argv,
13596                                      p);
13597             }
13598           else
13599             /* Just complain.  */
13600             dwarf2_macro_malformed_definition_complaint (body);
13601         }
13602       else
13603         /* Just complain.  */
13604         dwarf2_macro_malformed_definition_complaint (body);
13605
13606       xfree (name);
13607       {
13608         int i;
13609
13610         for (i = 0; i < argc; i++)
13611           xfree (argv[i]);
13612       }
13613       xfree (argv);
13614     }
13615   else
13616     dwarf2_macro_malformed_definition_complaint (body);
13617 }
13618
13619
13620 static void
13621 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13622                      char *comp_dir, bfd *abfd,
13623                      struct dwarf2_cu *cu)
13624 {
13625   gdb_byte *mac_ptr, *mac_end;
13626   struct macro_source_file *current_file = 0;
13627   enum dwarf_macinfo_record_type macinfo_type;
13628   int at_commandline;
13629
13630   dwarf2_read_section (dwarf2_per_objfile->objfile,
13631                        &dwarf2_per_objfile->macinfo);
13632   if (dwarf2_per_objfile->macinfo.buffer == NULL)
13633     {
13634       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13635       return;
13636     }
13637
13638   /* First pass: Find the name of the base filename.
13639      This filename is needed in order to process all macros whose definition
13640      (or undefinition) comes from the command line.  These macros are defined
13641      before the first DW_MACINFO_start_file entry, and yet still need to be
13642      associated to the base file.
13643
13644      To determine the base file name, we scan the macro definitions until we
13645      reach the first DW_MACINFO_start_file entry.  We then initialize
13646      CURRENT_FILE accordingly so that any macro definition found before the
13647      first DW_MACINFO_start_file can still be associated to the base file.  */
13648
13649   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13650   mac_end = dwarf2_per_objfile->macinfo.buffer
13651     + dwarf2_per_objfile->macinfo.size;
13652
13653   do
13654     {
13655       /* Do we at least have room for a macinfo type byte?  */
13656       if (mac_ptr >= mac_end)
13657         {
13658           /* Complaint is printed during the second pass as GDB will probably
13659              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
13660           break;
13661         }
13662
13663       macinfo_type = read_1_byte (abfd, mac_ptr);
13664       mac_ptr++;
13665
13666       switch (macinfo_type)
13667         {
13668           /* A zero macinfo type indicates the end of the macro
13669              information.  */
13670         case 0:
13671           break;
13672
13673         case DW_MACINFO_define:
13674         case DW_MACINFO_undef:
13675           /* Only skip the data by MAC_PTR.  */
13676           {
13677             unsigned int bytes_read;
13678
13679             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13680             mac_ptr += bytes_read;
13681             read_direct_string (abfd, mac_ptr, &bytes_read);
13682             mac_ptr += bytes_read;
13683           }
13684           break;
13685
13686         case DW_MACINFO_start_file:
13687           {
13688             unsigned int bytes_read;
13689             int line, file;
13690
13691             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13692             mac_ptr += bytes_read;
13693             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13694             mac_ptr += bytes_read;
13695
13696             current_file = macro_start_file (file, line, current_file, comp_dir,
13697                                              lh, cu->objfile);
13698           }
13699           break;
13700
13701         case DW_MACINFO_end_file:
13702           /* No data to skip by MAC_PTR.  */
13703           break;
13704
13705         case DW_MACINFO_vendor_ext:
13706           /* Only skip the data by MAC_PTR.  */
13707           {
13708             unsigned int bytes_read;
13709
13710             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13711             mac_ptr += bytes_read;
13712             read_direct_string (abfd, mac_ptr, &bytes_read);
13713             mac_ptr += bytes_read;
13714           }
13715           break;
13716
13717         default:
13718           break;
13719         }
13720     } while (macinfo_type != 0 && current_file == NULL);
13721
13722   /* Second pass: Process all entries.
13723
13724      Use the AT_COMMAND_LINE flag to determine whether we are still processing
13725      command-line macro definitions/undefinitions.  This flag is unset when we
13726      reach the first DW_MACINFO_start_file entry.  */
13727
13728   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13729
13730   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
13731      GDB is still reading the definitions from command line.  First
13732      DW_MACINFO_start_file will need to be ignored as it was already executed
13733      to create CURRENT_FILE for the main source holding also the command line
13734      definitions.  On first met DW_MACINFO_start_file this flag is reset to
13735      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
13736
13737   at_commandline = 1;
13738
13739   do
13740     {
13741       /* Do we at least have room for a macinfo type byte?  */
13742       if (mac_ptr >= mac_end)
13743         {
13744           dwarf2_macros_too_long_complaint ();
13745           break;
13746         }
13747
13748       macinfo_type = read_1_byte (abfd, mac_ptr);
13749       mac_ptr++;
13750
13751       switch (macinfo_type)
13752         {
13753           /* A zero macinfo type indicates the end of the macro
13754              information.  */
13755         case 0:
13756           break;
13757
13758         case DW_MACINFO_define:
13759         case DW_MACINFO_undef:
13760           {
13761             unsigned int bytes_read;
13762             int line;
13763             char *body;
13764
13765             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13766             mac_ptr += bytes_read;
13767             body = read_direct_string (abfd, mac_ptr, &bytes_read);
13768             mac_ptr += bytes_read;
13769
13770             if (! current_file)
13771               {
13772                 /* DWARF violation as no main source is present.  */
13773                 complaint (&symfile_complaints,
13774                            _("debug info with no main source gives macro %s "
13775                              "on line %d: %s"),
13776                            macinfo_type == DW_MACINFO_define ?
13777                              _("definition") :
13778                                macinfo_type == DW_MACINFO_undef ?
13779                                  _("undefinition") :
13780                                  _("something-or-other"), line, body);
13781                 break;
13782               }
13783             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13784               complaint (&symfile_complaints,
13785                          _("debug info gives %s macro %s with %s line %d: %s"),
13786                          at_commandline ? _("command-line") : _("in-file"),
13787                          macinfo_type == DW_MACINFO_define ?
13788                            _("definition") :
13789                              macinfo_type == DW_MACINFO_undef ?
13790                                _("undefinition") :
13791                                _("something-or-other"),
13792                          line == 0 ? _("zero") : _("non-zero"), line, body);
13793
13794             if (macinfo_type == DW_MACINFO_define)
13795               parse_macro_definition (current_file, line, body);
13796             else if (macinfo_type == DW_MACINFO_undef)
13797               macro_undef (current_file, line, body);
13798           }
13799           break;
13800
13801         case DW_MACINFO_start_file:
13802           {
13803             unsigned int bytes_read;
13804             int line, file;
13805
13806             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13807             mac_ptr += bytes_read;
13808             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13809             mac_ptr += bytes_read;
13810
13811             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13812               complaint (&symfile_complaints,
13813                          _("debug info gives source %d included "
13814                            "from %s at %s line %d"),
13815                          file, at_commandline ? _("command-line") : _("file"),
13816                          line == 0 ? _("zero") : _("non-zero"), line);
13817
13818             if (at_commandline)
13819               {
13820                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13821                 at_commandline = 0;
13822               }
13823             else
13824               current_file = macro_start_file (file, line,
13825                                                current_file, comp_dir,
13826                                                lh, cu->objfile);
13827           }
13828           break;
13829
13830         case DW_MACINFO_end_file:
13831           if (! current_file)
13832             complaint (&symfile_complaints,
13833                        _("macro debug info has an unmatched `close_file' directive"));
13834           else
13835             {
13836               current_file = current_file->included_by;
13837               if (! current_file)
13838                 {
13839                   enum dwarf_macinfo_record_type next_type;
13840
13841                   /* GCC circa March 2002 doesn't produce the zero
13842                      type byte marking the end of the compilation
13843                      unit.  Complain if it's not there, but exit no
13844                      matter what.  */
13845
13846                   /* Do we at least have room for a macinfo type byte?  */
13847                   if (mac_ptr >= mac_end)
13848                     {
13849                       dwarf2_macros_too_long_complaint ();
13850                       return;
13851                     }
13852
13853                   /* We don't increment mac_ptr here, so this is just
13854                      a look-ahead.  */
13855                   next_type = read_1_byte (abfd, mac_ptr);
13856                   if (next_type != 0)
13857                     complaint (&symfile_complaints,
13858                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13859
13860                   return;
13861                 }
13862             }
13863           break;
13864
13865         case DW_MACINFO_vendor_ext:
13866           {
13867             unsigned int bytes_read;
13868             int constant;
13869             char *string;
13870
13871             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13872             mac_ptr += bytes_read;
13873             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13874             mac_ptr += bytes_read;
13875
13876             /* We don't recognize any vendor extensions.  */
13877           }
13878           break;
13879         }
13880     } while (macinfo_type != 0);
13881 }
13882
13883 /* Check if the attribute's form is a DW_FORM_block*
13884    if so return true else false. */
13885 static int
13886 attr_form_is_block (struct attribute *attr)
13887 {
13888   return (attr == NULL ? 0 :
13889       attr->form == DW_FORM_block1
13890       || attr->form == DW_FORM_block2
13891       || attr->form == DW_FORM_block4
13892       || attr->form == DW_FORM_block
13893       || attr->form == DW_FORM_exprloc);
13894 }
13895
13896 /* Return non-zero if ATTR's value is a section offset --- classes
13897    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13898    You may use DW_UNSND (attr) to retrieve such offsets.
13899
13900    Section 7.5.4, "Attribute Encodings", explains that no attribute
13901    may have a value that belongs to more than one of these classes; it
13902    would be ambiguous if we did, because we use the same forms for all
13903    of them.  */
13904 static int
13905 attr_form_is_section_offset (struct attribute *attr)
13906 {
13907   return (attr->form == DW_FORM_data4
13908           || attr->form == DW_FORM_data8
13909           || attr->form == DW_FORM_sec_offset);
13910 }
13911
13912
13913 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13914    zero otherwise.  When this function returns true, you can apply
13915    dwarf2_get_attr_constant_value to it.
13916
13917    However, note that for some attributes you must check
13918    attr_form_is_section_offset before using this test.  DW_FORM_data4
13919    and DW_FORM_data8 are members of both the constant class, and of
13920    the classes that contain offsets into other debug sections
13921    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
13922    that, if an attribute's can be either a constant or one of the
13923    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13924    taken as section offsets, not constants.  */
13925 static int
13926 attr_form_is_constant (struct attribute *attr)
13927 {
13928   switch (attr->form)
13929     {
13930     case DW_FORM_sdata:
13931     case DW_FORM_udata:
13932     case DW_FORM_data1:
13933     case DW_FORM_data2:
13934     case DW_FORM_data4:
13935     case DW_FORM_data8:
13936       return 1;
13937     default:
13938       return 0;
13939     }
13940 }
13941
13942 static void
13943 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13944                              struct dwarf2_cu *cu)
13945 {
13946   if (attr_form_is_section_offset (attr)
13947       /* ".debug_loc" may not exist at all, or the offset may be outside
13948          the section.  If so, fall through to the complaint in the
13949          other branch.  */
13950       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13951     {
13952       struct dwarf2_loclist_baton *baton;
13953
13954       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13955                              sizeof (struct dwarf2_loclist_baton));
13956       baton->per_cu = cu->per_cu;
13957       gdb_assert (baton->per_cu);
13958
13959       dwarf2_read_section (dwarf2_per_objfile->objfile,
13960                            &dwarf2_per_objfile->loc);
13961
13962       /* We don't know how long the location list is, but make sure we
13963          don't run off the edge of the section.  */
13964       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13965       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13966       baton->base_address = cu->base_address;
13967       if (cu->base_known == 0)
13968         complaint (&symfile_complaints,
13969                    _("Location list used without specifying the CU base address."));
13970
13971       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13972       SYMBOL_LOCATION_BATON (sym) = baton;
13973     }
13974   else
13975     {
13976       struct dwarf2_locexpr_baton *baton;
13977
13978       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13979                              sizeof (struct dwarf2_locexpr_baton));
13980       baton->per_cu = cu->per_cu;
13981       gdb_assert (baton->per_cu);
13982
13983       if (attr_form_is_block (attr))
13984         {
13985           /* Note that we're just copying the block's data pointer
13986              here, not the actual data.  We're still pointing into the
13987              info_buffer for SYM's objfile; right now we never release
13988              that buffer, but when we do clean up properly this may
13989              need to change.  */
13990           baton->size = DW_BLOCK (attr)->size;
13991           baton->data = DW_BLOCK (attr)->data;
13992         }
13993       else
13994         {
13995           dwarf2_invalid_attrib_class_complaint ("location description",
13996                                                  SYMBOL_NATURAL_NAME (sym));
13997           baton->size = 0;
13998           baton->data = NULL;
13999         }
14000
14001       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14002       SYMBOL_LOCATION_BATON (sym) = baton;
14003     }
14004 }
14005
14006 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14007    came from a separate debuginfo file, then the master objfile is
14008    returned.  */
14009
14010 struct objfile *
14011 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14012 {
14013   struct objfile *objfile = per_cu->objfile;
14014
14015   /* Return the master objfile, so that we can report and look up the
14016      correct file containing this variable.  */
14017   if (objfile->separate_debug_objfile_backlink)
14018     objfile = objfile->separate_debug_objfile_backlink;
14019
14020   return objfile;
14021 }
14022
14023 /* Return the address size given in the compilation unit header for CU.  */
14024
14025 CORE_ADDR
14026 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14027 {
14028   if (per_cu->cu)
14029     return per_cu->cu->header.addr_size;
14030   else
14031     {
14032       /* If the CU is not currently read in, we re-read its header.  */
14033       struct objfile *objfile = per_cu->objfile;
14034       struct dwarf2_per_objfile *per_objfile
14035         = objfile_data (objfile, dwarf2_objfile_data_key);
14036       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14037       struct comp_unit_head cu_header;
14038
14039       memset (&cu_header, 0, sizeof cu_header);
14040       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14041       return cu_header.addr_size;
14042     }
14043 }
14044
14045 /* Return the offset size given in the compilation unit header for CU.  */
14046
14047 int
14048 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14049 {
14050   if (per_cu->cu)
14051     return per_cu->cu->header.offset_size;
14052   else
14053     {
14054       /* If the CU is not currently read in, we re-read its header.  */
14055       struct objfile *objfile = per_cu->objfile;
14056       struct dwarf2_per_objfile *per_objfile
14057         = objfile_data (objfile, dwarf2_objfile_data_key);
14058       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14059       struct comp_unit_head cu_header;
14060
14061       memset (&cu_header, 0, sizeof cu_header);
14062       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14063       return cu_header.offset_size;
14064     }
14065 }
14066
14067 /* Return the text offset of the CU.  The returned offset comes from
14068    this CU's objfile.  If this objfile came from a separate debuginfo
14069    file, then the offset may be different from the corresponding
14070    offset in the parent objfile.  */
14071
14072 CORE_ADDR
14073 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14074 {
14075   struct objfile *objfile = per_cu->objfile;
14076
14077   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14078 }
14079
14080 /* Locate the .debug_info compilation unit from CU's objfile which contains
14081    the DIE at OFFSET.  Raises an error on failure.  */
14082
14083 static struct dwarf2_per_cu_data *
14084 dwarf2_find_containing_comp_unit (unsigned int offset,
14085                                   struct objfile *objfile)
14086 {
14087   struct dwarf2_per_cu_data *this_cu;
14088   int low, high;
14089
14090   low = 0;
14091   high = dwarf2_per_objfile->n_comp_units - 1;
14092   while (high > low)
14093     {
14094       int mid = low + (high - low) / 2;
14095
14096       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14097         high = mid;
14098       else
14099         low = mid + 1;
14100     }
14101   gdb_assert (low == high);
14102   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14103     {
14104       if (low == 0)
14105         error (_("Dwarf Error: could not find partial DIE containing "
14106                "offset 0x%lx [in module %s]"),
14107                (long) offset, bfd_get_filename (objfile->obfd));
14108
14109       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14110       return dwarf2_per_objfile->all_comp_units[low-1];
14111     }
14112   else
14113     {
14114       this_cu = dwarf2_per_objfile->all_comp_units[low];
14115       if (low == dwarf2_per_objfile->n_comp_units - 1
14116           && offset >= this_cu->offset + this_cu->length)
14117         error (_("invalid dwarf2 offset %u"), offset);
14118       gdb_assert (offset < this_cu->offset + this_cu->length);
14119       return this_cu;
14120     }
14121 }
14122
14123 /* Locate the compilation unit from OBJFILE which is located at exactly
14124    OFFSET.  Raises an error on failure.  */
14125
14126 static struct dwarf2_per_cu_data *
14127 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14128 {
14129   struct dwarf2_per_cu_data *this_cu;
14130
14131   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14132   if (this_cu->offset != offset)
14133     error (_("no compilation unit with offset %u."), offset);
14134   return this_cu;
14135 }
14136
14137 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
14138
14139 static struct dwarf2_cu *
14140 alloc_one_comp_unit (struct objfile *objfile)
14141 {
14142   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14143   cu->objfile = objfile;
14144   obstack_init (&cu->comp_unit_obstack);
14145   return cu;
14146 }
14147
14148 /* Release one cached compilation unit, CU.  We unlink it from the tree
14149    of compilation units, but we don't remove it from the read_in_chain;
14150    the caller is responsible for that.
14151    NOTE: DATA is a void * because this function is also used as a
14152    cleanup routine.  */
14153
14154 static void
14155 free_one_comp_unit (void *data)
14156 {
14157   struct dwarf2_cu *cu = data;
14158
14159   if (cu->per_cu != NULL)
14160     cu->per_cu->cu = NULL;
14161   cu->per_cu = NULL;
14162
14163   obstack_free (&cu->comp_unit_obstack, NULL);
14164
14165   xfree (cu);
14166 }
14167
14168 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14169    when we're finished with it.  We can't free the pointer itself, but be
14170    sure to unlink it from the cache.  Also release any associated storage
14171    and perform cache maintenance.
14172
14173    Only used during partial symbol parsing.  */
14174
14175 static void
14176 free_stack_comp_unit (void *data)
14177 {
14178   struct dwarf2_cu *cu = data;
14179
14180   obstack_free (&cu->comp_unit_obstack, NULL);
14181   cu->partial_dies = NULL;
14182
14183   if (cu->per_cu != NULL)
14184     {
14185       /* This compilation unit is on the stack in our caller, so we
14186          should not xfree it.  Just unlink it.  */
14187       cu->per_cu->cu = NULL;
14188       cu->per_cu = NULL;
14189
14190       /* If we had a per-cu pointer, then we may have other compilation
14191          units loaded, so age them now.  */
14192       age_cached_comp_units ();
14193     }
14194 }
14195
14196 /* Free all cached compilation units.  */
14197
14198 static void
14199 free_cached_comp_units (void *data)
14200 {
14201   struct dwarf2_per_cu_data *per_cu, **last_chain;
14202
14203   per_cu = dwarf2_per_objfile->read_in_chain;
14204   last_chain = &dwarf2_per_objfile->read_in_chain;
14205   while (per_cu != NULL)
14206     {
14207       struct dwarf2_per_cu_data *next_cu;
14208
14209       next_cu = per_cu->cu->read_in_chain;
14210
14211       free_one_comp_unit (per_cu->cu);
14212       *last_chain = next_cu;
14213
14214       per_cu = next_cu;
14215     }
14216 }
14217
14218 /* Increase the age counter on each cached compilation unit, and free
14219    any that are too old.  */
14220
14221 static void
14222 age_cached_comp_units (void)
14223 {
14224   struct dwarf2_per_cu_data *per_cu, **last_chain;
14225
14226   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14227   per_cu = dwarf2_per_objfile->read_in_chain;
14228   while (per_cu != NULL)
14229     {
14230       per_cu->cu->last_used ++;
14231       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14232         dwarf2_mark (per_cu->cu);
14233       per_cu = per_cu->cu->read_in_chain;
14234     }
14235
14236   per_cu = dwarf2_per_objfile->read_in_chain;
14237   last_chain = &dwarf2_per_objfile->read_in_chain;
14238   while (per_cu != NULL)
14239     {
14240       struct dwarf2_per_cu_data *next_cu;
14241
14242       next_cu = per_cu->cu->read_in_chain;
14243
14244       if (!per_cu->cu->mark)
14245         {
14246           free_one_comp_unit (per_cu->cu);
14247           *last_chain = next_cu;
14248         }
14249       else
14250         last_chain = &per_cu->cu->read_in_chain;
14251
14252       per_cu = next_cu;
14253     }
14254 }
14255
14256 /* Remove a single compilation unit from the cache.  */
14257
14258 static void
14259 free_one_cached_comp_unit (void *target_cu)
14260 {
14261   struct dwarf2_per_cu_data *per_cu, **last_chain;
14262
14263   per_cu = dwarf2_per_objfile->read_in_chain;
14264   last_chain = &dwarf2_per_objfile->read_in_chain;
14265   while (per_cu != NULL)
14266     {
14267       struct dwarf2_per_cu_data *next_cu;
14268
14269       next_cu = per_cu->cu->read_in_chain;
14270
14271       if (per_cu->cu == target_cu)
14272         {
14273           free_one_comp_unit (per_cu->cu);
14274           *last_chain = next_cu;
14275           break;
14276         }
14277       else
14278         last_chain = &per_cu->cu->read_in_chain;
14279
14280       per_cu = next_cu;
14281     }
14282 }
14283
14284 /* Release all extra memory associated with OBJFILE.  */
14285
14286 void
14287 dwarf2_free_objfile (struct objfile *objfile)
14288 {
14289   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14290
14291   if (dwarf2_per_objfile == NULL)
14292     return;
14293
14294   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14295   free_cached_comp_units (NULL);
14296
14297   if (dwarf2_per_objfile->using_index)
14298     {
14299       int i;
14300
14301       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14302         {
14303           int j;
14304           struct dwarf2_per_cu_data *per_cu =
14305             dwarf2_per_objfile->all_comp_units[i];
14306
14307           if (!per_cu->v.quick->lines)
14308             continue;
14309
14310           for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14311             {
14312               if (per_cu->v.quick->file_names)
14313                 xfree ((void *) per_cu->v.quick->file_names[j]);
14314               if (per_cu->v.quick->full_names)
14315                 xfree ((void *) per_cu->v.quick->full_names[j]);
14316             }
14317
14318           free_line_header (per_cu->v.quick->lines);
14319         }
14320     }
14321
14322   /* Everything else should be on the objfile obstack.  */
14323 }
14324
14325 /* A pair of DIE offset and GDB type pointer.  We store these
14326    in a hash table separate from the DIEs, and preserve them
14327    when the DIEs are flushed out of cache.  */
14328
14329 struct dwarf2_offset_and_type
14330 {
14331   unsigned int offset;
14332   struct type *type;
14333 };
14334
14335 /* Hash function for a dwarf2_offset_and_type.  */
14336
14337 static hashval_t
14338 offset_and_type_hash (const void *item)
14339 {
14340   const struct dwarf2_offset_and_type *ofs = item;
14341
14342   return ofs->offset;
14343 }
14344
14345 /* Equality function for a dwarf2_offset_and_type.  */
14346
14347 static int
14348 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14349 {
14350   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14351   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14352
14353   return ofs_lhs->offset == ofs_rhs->offset;
14354 }
14355
14356 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14357    table if necessary.  For convenience, return TYPE.
14358
14359    The DIEs reading must have careful ordering to:
14360     * Not cause infite loops trying to read in DIEs as a prerequisite for
14361       reading current DIE.
14362     * Not trying to dereference contents of still incompletely read in types
14363       while reading in other DIEs.
14364     * Enable referencing still incompletely read in types just by a pointer to
14365       the type without accessing its fields.
14366
14367    Therefore caller should follow these rules:
14368      * Try to fetch any prerequisite types we may need to build this DIE type
14369        before building the type and calling set_die_type.
14370      * After building type call set_die_type for current DIE as soon as
14371        possible before fetching more types to complete the current type.
14372      * Make the type as complete as possible before fetching more types.  */
14373
14374 static struct type *
14375 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14376 {
14377   struct dwarf2_offset_and_type **slot, ofs;
14378   struct objfile *objfile = cu->objfile;
14379   htab_t *type_hash_ptr;
14380
14381   /* For Ada types, make sure that the gnat-specific data is always
14382      initialized (if not already set).  There are a few types where
14383      we should not be doing so, because the type-specific area is
14384      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14385      where the type-specific area is used to store the floatformat).
14386      But this is not a problem, because the gnat-specific information
14387      is actually not needed for these types.  */
14388   if (need_gnat_info (cu)
14389       && TYPE_CODE (type) != TYPE_CODE_FUNC
14390       && TYPE_CODE (type) != TYPE_CODE_FLT
14391       && !HAVE_GNAT_AUX_INFO (type))
14392     INIT_GNAT_SPECIFIC (type);
14393
14394   if (cu->per_cu->from_debug_types)
14395     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14396   else
14397     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14398
14399   if (*type_hash_ptr == NULL)
14400     {
14401       *type_hash_ptr
14402         = htab_create_alloc_ex (127,
14403                                 offset_and_type_hash,
14404                                 offset_and_type_eq,
14405                                 NULL,
14406                                 &objfile->objfile_obstack,
14407                                 hashtab_obstack_allocate,
14408                                 dummy_obstack_deallocate);
14409     }
14410
14411   ofs.offset = die->offset;
14412   ofs.type = type;
14413   slot = (struct dwarf2_offset_and_type **)
14414     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14415   if (*slot)
14416     complaint (&symfile_complaints,
14417                _("A problem internal to GDB: DIE 0x%x has type already set"),
14418                die->offset);
14419   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14420   **slot = ofs;
14421   return type;
14422 }
14423
14424 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14425    table, or return NULL if the die does not have a saved type.  */
14426
14427 static struct type *
14428 get_die_type_at_offset (unsigned int offset,
14429                         struct dwarf2_per_cu_data *per_cu)
14430 {
14431   struct dwarf2_offset_and_type *slot, ofs;
14432   htab_t type_hash;
14433
14434   if (per_cu->from_debug_types)
14435     type_hash = dwarf2_per_objfile->debug_types_type_hash;
14436   else
14437     type_hash = dwarf2_per_objfile->debug_info_type_hash;
14438   if (type_hash == NULL)
14439     return NULL;
14440
14441   ofs.offset = offset;
14442   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14443   if (slot)
14444     return slot->type;
14445   else
14446     return NULL;
14447 }
14448
14449 /* Look up the type for DIE in the appropriate type_hash table,
14450    or return NULL if DIE does not have a saved type.  */
14451
14452 static struct type *
14453 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14454 {
14455   return get_die_type_at_offset (die->offset, cu->per_cu);
14456 }
14457
14458 /* Add a dependence relationship from CU to REF_PER_CU.  */
14459
14460 static void
14461 dwarf2_add_dependence (struct dwarf2_cu *cu,
14462                        struct dwarf2_per_cu_data *ref_per_cu)
14463 {
14464   void **slot;
14465
14466   if (cu->dependencies == NULL)
14467     cu->dependencies
14468       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14469                               NULL, &cu->comp_unit_obstack,
14470                               hashtab_obstack_allocate,
14471                               dummy_obstack_deallocate);
14472
14473   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14474   if (*slot == NULL)
14475     *slot = ref_per_cu;
14476 }
14477
14478 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14479    Set the mark field in every compilation unit in the
14480    cache that we must keep because we are keeping CU.  */
14481
14482 static int
14483 dwarf2_mark_helper (void **slot, void *data)
14484 {
14485   struct dwarf2_per_cu_data *per_cu;
14486
14487   per_cu = (struct dwarf2_per_cu_data *) *slot;
14488   if (per_cu->cu->mark)
14489     return 1;
14490   per_cu->cu->mark = 1;
14491
14492   if (per_cu->cu->dependencies != NULL)
14493     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14494
14495   return 1;
14496 }
14497
14498 /* Set the mark field in CU and in every other compilation unit in the
14499    cache that we must keep because we are keeping CU.  */
14500
14501 static void
14502 dwarf2_mark (struct dwarf2_cu *cu)
14503 {
14504   if (cu->mark)
14505     return;
14506   cu->mark = 1;
14507   if (cu->dependencies != NULL)
14508     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14509 }
14510
14511 static void
14512 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14513 {
14514   while (per_cu)
14515     {
14516       per_cu->cu->mark = 0;
14517       per_cu = per_cu->cu->read_in_chain;
14518     }
14519 }
14520
14521 /* Trivial hash function for partial_die_info: the hash value of a DIE
14522    is its offset in .debug_info for this objfile.  */
14523
14524 static hashval_t
14525 partial_die_hash (const void *item)
14526 {
14527   const struct partial_die_info *part_die = item;
14528
14529   return part_die->offset;
14530 }
14531
14532 /* Trivial comparison function for partial_die_info structures: two DIEs
14533    are equal if they have the same offset.  */
14534
14535 static int
14536 partial_die_eq (const void *item_lhs, const void *item_rhs)
14537 {
14538   const struct partial_die_info *part_die_lhs = item_lhs;
14539   const struct partial_die_info *part_die_rhs = item_rhs;
14540
14541   return part_die_lhs->offset == part_die_rhs->offset;
14542 }
14543
14544 static struct cmd_list_element *set_dwarf2_cmdlist;
14545 static struct cmd_list_element *show_dwarf2_cmdlist;
14546
14547 static void
14548 set_dwarf2_cmd (char *args, int from_tty)
14549 {
14550   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14551 }
14552
14553 static void
14554 show_dwarf2_cmd (char *args, int from_tty)
14555 {
14556   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14557 }
14558
14559 /* If section described by INFO was mmapped, munmap it now.  */
14560
14561 static void
14562 munmap_section_buffer (struct dwarf2_section_info *info)
14563 {
14564   if (info->was_mmapped)
14565     {
14566 #ifdef HAVE_MMAP
14567       intptr_t begin = (intptr_t) info->buffer;
14568       intptr_t map_begin = begin & ~(pagesize - 1);
14569       size_t map_length = info->size + begin - map_begin;
14570
14571       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14572 #else
14573       /* Without HAVE_MMAP, we should never be here to begin with.  */
14574       gdb_assert_not_reached ("no mmap support");
14575 #endif
14576     }
14577 }
14578
14579 /* munmap debug sections for OBJFILE, if necessary.  */
14580
14581 static void
14582 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14583 {
14584   struct dwarf2_per_objfile *data = d;
14585
14586   /* This is sorted according to the order they're defined in to make it easier
14587      to keep in sync.  */
14588   munmap_section_buffer (&data->info);
14589   munmap_section_buffer (&data->abbrev);
14590   munmap_section_buffer (&data->line);
14591   munmap_section_buffer (&data->loc);
14592   munmap_section_buffer (&data->macinfo);
14593   munmap_section_buffer (&data->str);
14594   munmap_section_buffer (&data->ranges);
14595   munmap_section_buffer (&data->types);
14596   munmap_section_buffer (&data->frame);
14597   munmap_section_buffer (&data->eh_frame);
14598   munmap_section_buffer (&data->gdb_index);
14599 }
14600
14601 \f
14602
14603 /* The contents of the hash table we create when building the string
14604    table.  */
14605 struct strtab_entry
14606 {
14607   offset_type offset;
14608   const char *str;
14609 };
14610
14611 /* Hash function for a strtab_entry.  */
14612 static hashval_t
14613 hash_strtab_entry (const void *e)
14614 {
14615   const struct strtab_entry *entry = e;
14616   return mapped_index_string_hash (entry->str);
14617 }
14618
14619 /* Equality function for a strtab_entry.  */
14620 static int
14621 eq_strtab_entry (const void *a, const void *b)
14622 {
14623   const struct strtab_entry *ea = a;
14624   const struct strtab_entry *eb = b;
14625   return !strcmp (ea->str, eb->str);
14626 }
14627
14628 /* Create a strtab_entry hash table.  */
14629 static htab_t
14630 create_strtab (void)
14631 {
14632   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14633                             xfree, xcalloc, xfree);
14634 }
14635
14636 /* Add a string to the constant pool.  Return the string's offset in
14637    host order.  */
14638 static offset_type
14639 add_string (htab_t table, struct obstack *cpool, const char *str)
14640 {
14641   void **slot;
14642   struct strtab_entry entry;
14643   struct strtab_entry *result;
14644
14645   entry.str = str;
14646   slot = htab_find_slot (table, &entry, INSERT);
14647   if (*slot)
14648     result = *slot;
14649   else
14650     {
14651       result = XNEW (struct strtab_entry);
14652       result->offset = obstack_object_size (cpool);
14653       result->str = str;
14654       obstack_grow_str0 (cpool, str);
14655       *slot = result;
14656     }
14657   return result->offset;
14658 }
14659
14660 /* An entry in the symbol table.  */
14661 struct symtab_index_entry
14662 {
14663   /* The name of the symbol.  */
14664   const char *name;
14665   /* The offset of the name in the constant pool.  */
14666   offset_type index_offset;
14667   /* A sorted vector of the indices of all the CUs that hold an object
14668      of this name.  */
14669   VEC (offset_type) *cu_indices;
14670 };
14671
14672 /* The symbol table.  This is a power-of-2-sized hash table.  */
14673 struct mapped_symtab
14674 {
14675   offset_type n_elements;
14676   offset_type size;
14677   struct symtab_index_entry **data;
14678 };
14679
14680 /* Hash function for a symtab_index_entry.  */
14681 static hashval_t
14682 hash_symtab_entry (const void *e)
14683 {
14684   const struct symtab_index_entry *entry = e;
14685   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14686                          sizeof (offset_type) * VEC_length (offset_type,
14687                                                             entry->cu_indices),
14688                          0);
14689 }
14690
14691 /* Equality function for a symtab_index_entry.  */
14692 static int
14693 eq_symtab_entry (const void *a, const void *b)
14694 {
14695   const struct symtab_index_entry *ea = a;
14696   const struct symtab_index_entry *eb = b;
14697   int len = VEC_length (offset_type, ea->cu_indices);
14698   if (len != VEC_length (offset_type, eb->cu_indices))
14699     return 0;
14700   return !memcmp (VEC_address (offset_type, ea->cu_indices),
14701                   VEC_address (offset_type, eb->cu_indices),
14702                   sizeof (offset_type) * len);
14703 }
14704
14705 /* Destroy a symtab_index_entry.  */
14706 static void
14707 delete_symtab_entry (void *p)
14708 {
14709   struct symtab_index_entry *entry = p;
14710   VEC_free (offset_type, entry->cu_indices);
14711   xfree (entry);
14712 }
14713
14714 /* Create a hash table holding symtab_index_entry objects.  */
14715 static htab_t
14716 create_index_table (void)
14717 {
14718   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14719                             delete_symtab_entry, xcalloc, xfree);
14720 }
14721
14722 /* Create a new mapped symtab object.  */
14723 static struct mapped_symtab *
14724 create_mapped_symtab (void)
14725 {
14726   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14727   symtab->n_elements = 0;
14728   symtab->size = 1024;
14729   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14730   return symtab;
14731 }
14732
14733 /* Destroy a mapped_symtab.  */
14734 static void
14735 cleanup_mapped_symtab (void *p)
14736 {
14737   struct mapped_symtab *symtab = p;
14738   /* The contents of the array are freed when the other hash table is
14739      destroyed.  */
14740   xfree (symtab->data);
14741   xfree (symtab);
14742 }
14743
14744 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
14745    the slot.  */
14746 static struct symtab_index_entry **
14747 find_slot (struct mapped_symtab *symtab, const char *name)
14748 {
14749   offset_type index, step, hash = mapped_index_string_hash (name);
14750
14751   index = hash & (symtab->size - 1);
14752   step = ((hash * 17) & (symtab->size - 1)) | 1;
14753
14754   for (;;)
14755     {
14756       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14757         return &symtab->data[index];
14758       index = (index + step) & (symtab->size - 1);
14759     }
14760 }
14761
14762 /* Expand SYMTAB's hash table.  */
14763 static void
14764 hash_expand (struct mapped_symtab *symtab)
14765 {
14766   offset_type old_size = symtab->size;
14767   offset_type i;
14768   struct symtab_index_entry **old_entries = symtab->data;
14769
14770   symtab->size *= 2;
14771   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14772
14773   for (i = 0; i < old_size; ++i)
14774     {
14775       if (old_entries[i])
14776         {
14777           struct symtab_index_entry **slot = find_slot (symtab,
14778                                                         old_entries[i]->name);
14779           *slot = old_entries[i];
14780         }
14781     }
14782
14783   xfree (old_entries);
14784 }
14785
14786 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
14787    is the index of the CU in which the symbol appears.  */
14788 static void
14789 add_index_entry (struct mapped_symtab *symtab, const char *name,
14790                  offset_type cu_index)
14791 {
14792   struct symtab_index_entry **slot;
14793
14794   ++symtab->n_elements;
14795   if (4 * symtab->n_elements / 3 >= symtab->size)
14796     hash_expand (symtab);
14797
14798   slot = find_slot (symtab, name);
14799   if (!*slot)
14800     {
14801       *slot = XNEW (struct symtab_index_entry);
14802       (*slot)->name = name;
14803       (*slot)->cu_indices = NULL;
14804     }
14805   /* Don't push an index twice.  Due to how we add entries we only
14806      have to check the last one.  */ 
14807   if (VEC_empty (offset_type, (*slot)->cu_indices)
14808       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14809     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14810 }
14811
14812 /* Add a vector of indices to the constant pool.  */
14813 static offset_type
14814 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14815                       struct symtab_index_entry *entry)
14816 {
14817   void **slot;
14818
14819   slot = htab_find_slot (index_table, entry, INSERT);
14820   if (!*slot)
14821     {
14822       offset_type len = VEC_length (offset_type, entry->cu_indices);
14823       offset_type val = MAYBE_SWAP (len);
14824       offset_type iter;
14825       int i;
14826
14827       *slot = entry;
14828       entry->index_offset = obstack_object_size (cpool);
14829
14830       obstack_grow (cpool, &val, sizeof (val));
14831       for (i = 0;
14832            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14833            ++i)
14834         {
14835           val = MAYBE_SWAP (iter);
14836           obstack_grow (cpool, &val, sizeof (val));
14837         }
14838     }
14839   else
14840     {
14841       struct symtab_index_entry *old_entry = *slot;
14842       entry->index_offset = old_entry->index_offset;
14843       entry = old_entry;
14844     }
14845   return entry->index_offset;
14846 }
14847
14848 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14849    constant pool entries going into the obstack CPOOL.  */
14850 static void
14851 write_hash_table (struct mapped_symtab *symtab,
14852                   struct obstack *output, struct obstack *cpool)
14853 {
14854   offset_type i;
14855   htab_t index_table;
14856   htab_t str_table;
14857
14858   index_table = create_index_table ();
14859   str_table = create_strtab ();
14860   /* We add all the index vectors to the constant pool first, to
14861      ensure alignment is ok.  */
14862   for (i = 0; i < symtab->size; ++i)
14863     {
14864       if (symtab->data[i])
14865         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14866     }
14867
14868   /* Now write out the hash table.  */
14869   for (i = 0; i < symtab->size; ++i)
14870     {
14871       offset_type str_off, vec_off;
14872
14873       if (symtab->data[i])
14874         {
14875           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14876           vec_off = symtab->data[i]->index_offset;
14877         }
14878       else
14879         {
14880           /* While 0 is a valid constant pool index, it is not valid
14881              to have 0 for both offsets.  */
14882           str_off = 0;
14883           vec_off = 0;
14884         }
14885
14886       str_off = MAYBE_SWAP (str_off);
14887       vec_off = MAYBE_SWAP (vec_off);
14888
14889       obstack_grow (output, &str_off, sizeof (str_off));
14890       obstack_grow (output, &vec_off, sizeof (vec_off));
14891     }
14892
14893   htab_delete (str_table);
14894   htab_delete (index_table);
14895 }
14896
14897 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
14898    from PST; CU_INDEX is the index of the CU in the vector of all
14899    CUs.  */
14900 static void
14901 add_address_entry (struct objfile *objfile,
14902                    struct obstack *addr_obstack, struct partial_symtab *pst,
14903                    unsigned int cu_index)
14904 {
14905   offset_type offset;
14906   char addr[8];
14907   CORE_ADDR baseaddr;
14908
14909   /* Don't bother recording empty ranges.  */
14910   if (pst->textlow == pst->texthigh)
14911     return;
14912
14913   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14914
14915   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14916   obstack_grow (addr_obstack, addr, 8);
14917   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14918   obstack_grow (addr_obstack, addr, 8);
14919   offset = MAYBE_SWAP (cu_index);
14920   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14921 }
14922
14923 /* Add a list of partial symbols to SYMTAB.  */
14924 static void
14925 write_psymbols (struct mapped_symtab *symtab,
14926                 struct partial_symbol **psymp,
14927                 int count,
14928                 offset_type cu_index)
14929 {
14930   for (; count-- > 0; ++psymp)
14931     {
14932       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14933         error (_("Ada is not currently supported by the index"));
14934       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14935     }
14936 }
14937
14938 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
14939    exception if there is an error.  */
14940 static void
14941 write_obstack (FILE *file, struct obstack *obstack)
14942 {
14943   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14944               file)
14945       != obstack_object_size (obstack))
14946     error (_("couldn't data write to file"));
14947 }
14948
14949 /* Unlink a file if the argument is not NULL.  */
14950 static void
14951 unlink_if_set (void *p)
14952 {
14953   char **filename = p;
14954   if (*filename)
14955     unlink (*filename);
14956 }
14957
14958 /* A helper struct used when iterating over debug_types.  */
14959 struct signatured_type_index_data
14960 {
14961   struct objfile *objfile;
14962   struct mapped_symtab *symtab;
14963   struct obstack *types_list;
14964   int cu_index;
14965 };
14966
14967 /* A helper function that writes a single signatured_type to an
14968    obstack.  */
14969 static int
14970 write_one_signatured_type (void **slot, void *d)
14971 {
14972   struct signatured_type_index_data *info = d;
14973   struct signatured_type *entry = (struct signatured_type *) *slot;
14974   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14975   struct partial_symtab *psymtab = per_cu->v.psymtab;
14976   gdb_byte val[8];
14977
14978   write_psymbols (info->symtab,
14979                   info->objfile->global_psymbols.list + psymtab->globals_offset,
14980                   psymtab->n_global_syms, info->cu_index);
14981   write_psymbols (info->symtab,
14982                   info->objfile->static_psymbols.list + psymtab->statics_offset,
14983                   psymtab->n_static_syms, info->cu_index);
14984
14985   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14986   obstack_grow (info->types_list, val, 8);
14987   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14988   obstack_grow (info->types_list, val, 8);
14989   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14990   obstack_grow (info->types_list, val, 8);
14991
14992   ++info->cu_index;
14993
14994   return 1;
14995 }
14996
14997 /* Create an index file for OBJFILE in the directory DIR.  */
14998 static void
14999 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15000 {
15001   struct cleanup *cleanup;
15002   char *filename, *cleanup_filename;
15003   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15004   struct obstack cu_list, types_cu_list;
15005   int i;
15006   FILE *out_file;
15007   struct mapped_symtab *symtab;
15008   offset_type val, size_of_contents, total_len;
15009   struct stat st;
15010   char buf[8];
15011
15012   if (!objfile->psymtabs)
15013     return;
15014   if (dwarf2_per_objfile->using_index)
15015     error (_("Cannot use an index to create the index"));
15016
15017   if (stat (objfile->name, &st) < 0)
15018     perror_with_name (_("Could not stat"));
15019
15020   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15021                      INDEX_SUFFIX, (char *) NULL);
15022   cleanup = make_cleanup (xfree, filename);
15023
15024   out_file = fopen (filename, "wb");
15025   if (!out_file)
15026     error (_("Can't open `%s' for writing"), filename);
15027
15028   cleanup_filename = filename;
15029   make_cleanup (unlink_if_set, &cleanup_filename);
15030
15031   symtab = create_mapped_symtab ();
15032   make_cleanup (cleanup_mapped_symtab, symtab);
15033
15034   obstack_init (&addr_obstack);
15035   make_cleanup_obstack_free (&addr_obstack);
15036
15037   obstack_init (&cu_list);
15038   make_cleanup_obstack_free (&cu_list);
15039
15040   obstack_init (&types_cu_list);
15041   make_cleanup_obstack_free (&types_cu_list);
15042
15043   /* The list is already sorted, so we don't need to do additional
15044      work here.  Also, the debug_types entries do not appear in
15045      all_comp_units, but only in their own hash table.  */
15046   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15047     {
15048       struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15049       struct partial_symtab *psymtab = per_cu->v.psymtab;
15050       gdb_byte val[8];
15051
15052       write_psymbols (symtab,
15053                       objfile->global_psymbols.list + psymtab->globals_offset,
15054                       psymtab->n_global_syms, i);
15055       write_psymbols (symtab,
15056                       objfile->static_psymbols.list + psymtab->statics_offset,
15057                       psymtab->n_static_syms, i);
15058
15059       add_address_entry (objfile, &addr_obstack, psymtab, i);
15060
15061       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15062       obstack_grow (&cu_list, val, 8);
15063       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15064       obstack_grow (&cu_list, val, 8);
15065     }
15066
15067   /* Write out the .debug_type entries, if any.  */
15068   if (dwarf2_per_objfile->signatured_types)
15069     {
15070       struct signatured_type_index_data sig_data;
15071
15072       sig_data.objfile = objfile;
15073       sig_data.symtab = symtab;
15074       sig_data.types_list = &types_cu_list;
15075       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15076       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15077                               write_one_signatured_type, &sig_data);
15078     }
15079
15080   obstack_init (&constant_pool);
15081   make_cleanup_obstack_free (&constant_pool);
15082   obstack_init (&symtab_obstack);
15083   make_cleanup_obstack_free (&symtab_obstack);
15084   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15085
15086   obstack_init (&contents);
15087   make_cleanup_obstack_free (&contents);
15088   size_of_contents = 6 * sizeof (offset_type);
15089   total_len = size_of_contents;
15090
15091   /* The version number.  */
15092   val = MAYBE_SWAP (2);
15093   obstack_grow (&contents, &val, sizeof (val));
15094
15095   /* The offset of the CU list from the start of the file.  */
15096   val = MAYBE_SWAP (total_len);
15097   obstack_grow (&contents, &val, sizeof (val));
15098   total_len += obstack_object_size (&cu_list);
15099
15100   /* The offset of the types CU list from the start of the file.  */
15101   val = MAYBE_SWAP (total_len);
15102   obstack_grow (&contents, &val, sizeof (val));
15103   total_len += obstack_object_size (&types_cu_list);
15104
15105   /* The offset of the address table from the start of the file.  */
15106   val = MAYBE_SWAP (total_len);
15107   obstack_grow (&contents, &val, sizeof (val));
15108   total_len += obstack_object_size (&addr_obstack);
15109
15110   /* The offset of the symbol table from the start of the file.  */
15111   val = MAYBE_SWAP (total_len);
15112   obstack_grow (&contents, &val, sizeof (val));
15113   total_len += obstack_object_size (&symtab_obstack);
15114
15115   /* The offset of the constant pool from the start of the file.  */
15116   val = MAYBE_SWAP (total_len);
15117   obstack_grow (&contents, &val, sizeof (val));
15118   total_len += obstack_object_size (&constant_pool);
15119
15120   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15121
15122   write_obstack (out_file, &contents);
15123   write_obstack (out_file, &cu_list);
15124   write_obstack (out_file, &types_cu_list);
15125   write_obstack (out_file, &addr_obstack);
15126   write_obstack (out_file, &symtab_obstack);
15127   write_obstack (out_file, &constant_pool);
15128
15129   fclose (out_file);
15130
15131   /* We want to keep the file, so we set cleanup_filename to NULL
15132      here.  See unlink_if_set.  */
15133   cleanup_filename = NULL;
15134
15135   do_cleanups (cleanup);
15136 }
15137
15138 /* The mapped index file format is designed to be directly mmap()able
15139    on any architecture.  In most cases, a datum is represented using a
15140    little-endian 32-bit integer value, called an offset_type.  Big
15141    endian machines must byte-swap the values before using them.
15142    Exceptions to this rule are noted.  The data is laid out such that
15143    alignment is always respected.
15144
15145    A mapped index consists of several sections.
15146
15147    1. The file header.  This is a sequence of values, of offset_type
15148    unless otherwise noted:
15149    [0] The version number.  Currently 1 or 2.  The differences are
15150    noted below.  Version 1 did not account for .debug_types sections;
15151    the presence of a .debug_types section invalidates any version 1
15152    index that may exist.
15153    [1] The offset, from the start of the file, of the CU list.
15154    [1.5] In version 2, the offset, from the start of the file, of the
15155    types CU list.  This offset does not appear in version 1.  Note
15156    that this can be empty, in which case this offset will be equal to
15157    the next offset.
15158    [2] The offset, from the start of the file, of the address section.
15159    [3] The offset, from the start of the file, of the symbol table.
15160    [4] The offset, from the start of the file, of the constant pool.
15161
15162    2. The CU list.  This is a sequence of pairs of 64-bit
15163    little-endian values, sorted by the CU offset.  The first element
15164    in each pair is the offset of a CU in the .debug_info section.  The
15165    second element in each pair is the length of that CU.  References
15166    to a CU elsewhere in the map are done using a CU index, which is
15167    just the 0-based index into this table.  Note that if there are
15168    type CUs, then conceptually CUs and type CUs form a single list for
15169    the purposes of CU indices.
15170
15171    2.5 The types CU list.  This does not appear in a version 1 index.
15172    This is a sequence of triplets of 64-bit little-endian values.  In
15173    a triplet, the first value is the CU offset, the second value is
15174    the type offset in the CU, and the third value is the type
15175    signature.  The types CU list is not sorted.
15176
15177    3. The address section.  The address section consists of a sequence
15178    of address entries.  Each address entry has three elements.
15179    [0] The low address.  This is a 64-bit little-endian value.
15180    [1] The high address.  This is a 64-bit little-endian value.
15181    [2] The CU index.  This is an offset_type value.
15182
15183    4. The symbol table.  This is a hash table.  The size of the hash
15184    table is always a power of 2.  The initial hash and the step are
15185    currently defined by the `find_slot' function.
15186
15187    Each slot in the hash table consists of a pair of offset_type
15188    values.  The first value is the offset of the symbol's name in the
15189    constant pool.  The second value is the offset of the CU vector in
15190    the constant pool.
15191
15192    If both values are 0, then this slot in the hash table is empty.
15193    This is ok because while 0 is a valid constant pool index, it
15194    cannot be a valid index for both a string and a CU vector.
15195
15196    A string in the constant pool is stored as a \0-terminated string,
15197    as you'd expect.
15198
15199    A CU vector in the constant pool is a sequence of offset_type
15200    values.  The first value is the number of CU indices in the vector.
15201    Each subsequent value is the index of a CU in the CU list.  This
15202    element in the hash table is used to indicate which CUs define the
15203    symbol.
15204
15205    5. The constant pool.  This is simply a bunch of bytes.  It is
15206    organized so that alignment is correct: CU vectors are stored
15207    first, followed by strings.  */
15208 static void
15209 save_gdb_index_command (char *arg, int from_tty)
15210 {
15211   struct objfile *objfile;
15212
15213   if (!arg || !*arg)
15214     error (_("usage: save gdb-index DIRECTORY"));
15215
15216   ALL_OBJFILES (objfile)
15217   {
15218     struct stat st;
15219
15220     /* If the objfile does not correspond to an actual file, skip it.  */
15221     if (stat (objfile->name, &st) < 0)
15222       continue;
15223
15224     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15225     if (dwarf2_per_objfile)
15226       {
15227         volatile struct gdb_exception except;
15228
15229         TRY_CATCH (except, RETURN_MASK_ERROR)
15230           {
15231             write_psymtabs_to_index (objfile, arg);
15232           }
15233         if (except.reason < 0)
15234           exception_fprintf (gdb_stderr, except,
15235                              _("Error while writing index for `%s': "),
15236                              objfile->name);
15237       }
15238   }
15239 }
15240
15241 \f
15242
15243 int dwarf2_always_disassemble;
15244
15245 static void
15246 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15247                                 struct cmd_list_element *c, const char *value)
15248 {
15249   fprintf_filtered (file, _("\
15250 Whether to always disassemble DWARF expressions is %s.\n"),
15251                     value);
15252 }
15253
15254 void _initialize_dwarf2_read (void);
15255
15256 void
15257 _initialize_dwarf2_read (void)
15258 {
15259   struct cmd_list_element *c;
15260
15261   dwarf2_objfile_data_key
15262     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15263
15264   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15265 Set DWARF 2 specific variables.\n\
15266 Configure DWARF 2 variables such as the cache size"),
15267                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15268                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15269
15270   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15271 Show DWARF 2 specific variables\n\
15272 Show DWARF 2 variables such as the cache size"),
15273                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15274                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15275
15276   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15277                             &dwarf2_max_cache_age, _("\
15278 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15279 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15280 A higher limit means that cached compilation units will be stored\n\
15281 in memory longer, and more total memory will be used.  Zero disables\n\
15282 caching, which can slow down startup."),
15283                             NULL,
15284                             show_dwarf2_max_cache_age,
15285                             &set_dwarf2_cmdlist,
15286                             &show_dwarf2_cmdlist);
15287
15288   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15289                            &dwarf2_always_disassemble, _("\
15290 Set whether `info address' always disassembles DWARF expressions."), _("\
15291 Show whether `info address' always disassembles DWARF expressions."), _("\
15292 When enabled, DWARF expressions are always printed in an assembly-like\n\
15293 syntax.  When disabled, expressions will be printed in a more\n\
15294 conversational style, when possible."),
15295                            NULL,
15296                            show_dwarf2_always_disassemble,
15297                            &set_dwarf2_cmdlist,
15298                            &show_dwarf2_cmdlist);
15299
15300   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15301 Set debugging of the dwarf2 DIE reader."), _("\
15302 Show debugging of the dwarf2 DIE reader."), _("\
15303 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15304 The value is the maximum depth to print."),
15305                             NULL,
15306                             NULL,
15307                             &setdebuglist, &showdebuglist);
15308
15309   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15310                _("Save a .gdb-index file"),
15311                &save_cmdlist);
15312   set_cmd_completer (c, filename_completer);
15313 }