OSDN Git Service

gdb/
[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, terminated by a die whose
673        tag is zero.  */
674     struct die_info *child;     /* Its first child, if any.  */
675     struct die_info *sibling;   /* Its next sibling, if any.  */
676     struct die_info *parent;    /* Its parent, if any.  */
677
678     /* An array of attributes, with NUM_ATTRS elements.  There may be
679        zero, but it's not common and zero-sized arrays are not
680        sufficiently portable C.  */
681     struct attribute attrs[1];
682   };
683
684 struct function_range
685 {
686   const char *name;
687   CORE_ADDR lowpc, highpc;
688   int seen_line;
689   struct function_range *next;
690 };
691
692 /* Get at parts of an attribute structure */
693
694 #define DW_STRING(attr)    ((attr)->u.str)
695 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
696 #define DW_UNSND(attr)     ((attr)->u.unsnd)
697 #define DW_BLOCK(attr)     ((attr)->u.blk)
698 #define DW_SND(attr)       ((attr)->u.snd)
699 #define DW_ADDR(attr)      ((attr)->u.addr)
700 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
701
702 /* Blocks are a bunch of untyped bytes. */
703 struct dwarf_block
704   {
705     unsigned int size;
706     gdb_byte *data;
707   };
708
709 #ifndef ATTR_ALLOC_CHUNK
710 #define ATTR_ALLOC_CHUNK 4
711 #endif
712
713 /* Allocate fields for structs, unions and enums in this size.  */
714 #ifndef DW_FIELD_ALLOC_CHUNK
715 #define DW_FIELD_ALLOC_CHUNK 4
716 #endif
717
718 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
719    but this would require a corresponding change in unpack_field_as_long
720    and friends.  */
721 static int bits_per_byte = 8;
722
723 /* The routines that read and process dies for a C struct or C++ class
724    pass lists of data member fields and lists of member function fields
725    in an instance of a field_info structure, as defined below.  */
726 struct field_info
727   {
728     /* List of data member and baseclasses fields. */
729     struct nextfield
730       {
731         struct nextfield *next;
732         int accessibility;
733         int virtuality;
734         struct field field;
735       }
736      *fields, *baseclasses;
737
738     /* Number of fields (including baseclasses).  */
739     int nfields;
740
741     /* Number of baseclasses.  */
742     int nbaseclasses;
743
744     /* Set if the accesibility of one of the fields is not public.  */
745     int non_public_fields;
746
747     /* Member function fields array, entries are allocated in the order they
748        are encountered in the object file.  */
749     struct nextfnfield
750       {
751         struct nextfnfield *next;
752         struct fn_field fnfield;
753       }
754      *fnfields;
755
756     /* Member function fieldlist array, contains name of possibly overloaded
757        member function, number of overloaded member functions and a pointer
758        to the head of the member function field chain.  */
759     struct fnfieldlist
760       {
761         char *name;
762         int length;
763         struct nextfnfield *head;
764       }
765      *fnfieldlists;
766
767     /* Number of entries in the fnfieldlists array.  */
768     int nfnfields;
769
770     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
771        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
772     struct typedef_field_list
773       {
774         struct typedef_field field;
775         struct typedef_field_list *next;
776       }
777     *typedef_field_list;
778     unsigned typedef_field_list_count;
779   };
780
781 /* One item on the queue of compilation units to read in full symbols
782    for.  */
783 struct dwarf2_queue_item
784 {
785   struct dwarf2_per_cu_data *per_cu;
786   struct dwarf2_queue_item *next;
787 };
788
789 /* The current queue.  */
790 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
791
792 /* Loaded secondary compilation units are kept in memory until they
793    have not been referenced for the processing of this many
794    compilation units.  Set this to zero to disable caching.  Cache
795    sizes of up to at least twenty will improve startup time for
796    typical inter-CU-reference binaries, at an obvious memory cost.  */
797 static int dwarf2_max_cache_age = 5;
798 static void
799 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
800                            struct cmd_list_element *c, const char *value)
801 {
802   fprintf_filtered (file, _("\
803 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
804                     value);
805 }
806
807
808 /* Various complaints about symbol reading that don't abort the process */
809
810 static void
811 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
812 {
813   complaint (&symfile_complaints,
814              _("statement list doesn't fit in .debug_line section"));
815 }
816
817 static void
818 dwarf2_debug_line_missing_file_complaint (void)
819 {
820   complaint (&symfile_complaints,
821              _(".debug_line section has line data without a file"));
822 }
823
824 static void
825 dwarf2_debug_line_missing_end_sequence_complaint (void)
826 {
827   complaint (&symfile_complaints,
828              _(".debug_line section has line program sequence without an end"));
829 }
830
831 static void
832 dwarf2_complex_location_expr_complaint (void)
833 {
834   complaint (&symfile_complaints, _("location expression too complex"));
835 }
836
837 static void
838 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839                                               int arg3)
840 {
841   complaint (&symfile_complaints,
842              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
843              arg2, arg3);
844 }
845
846 static void
847 dwarf2_macros_too_long_complaint (void)
848 {
849   complaint (&symfile_complaints,
850              _("macro info runs off end of `.debug_macinfo' section"));
851 }
852
853 static void
854 dwarf2_macro_malformed_definition_complaint (const char *arg1)
855 {
856   complaint (&symfile_complaints,
857              _("macro debug info contains a malformed macro definition:\n`%s'"),
858              arg1);
859 }
860
861 static void
862 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
863 {
864   complaint (&symfile_complaints,
865              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
866 }
867
868 /* local function prototypes */
869
870 static void dwarf2_locate_sections (bfd *, asection *, void *);
871
872 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873                                            struct objfile *);
874
875 static void dwarf2_build_psymtabs_hard (struct objfile *);
876
877 static void scan_partial_symbols (struct partial_die_info *,
878                                   CORE_ADDR *, CORE_ADDR *,
879                                   int, struct dwarf2_cu *);
880
881 static void add_partial_symbol (struct partial_die_info *,
882                                 struct dwarf2_cu *);
883
884 static void add_partial_namespace (struct partial_die_info *pdi,
885                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
886                                    int need_pc, struct dwarf2_cu *cu);
887
888 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
889                                 CORE_ADDR *highpc, int need_pc,
890                                 struct dwarf2_cu *cu);
891
892 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
893                                      struct dwarf2_cu *cu);
894
895 static void add_partial_subprogram (struct partial_die_info *pdi,
896                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
897                                     int need_pc, struct dwarf2_cu *cu);
898
899 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
900                                      gdb_byte *buffer, gdb_byte *info_ptr,
901                                      bfd *abfd, struct dwarf2_cu *cu);
902
903 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
904
905 static void psymtab_to_symtab_1 (struct partial_symtab *);
906
907 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
908
909 static void dwarf2_free_abbrev_table (void *);
910
911 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
912                                             struct dwarf2_cu *);
913
914 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
915                                                  struct dwarf2_cu *);
916
917 static struct partial_die_info *load_partial_dies (bfd *,
918                                                    gdb_byte *, gdb_byte *,
919                                                    int, struct dwarf2_cu *);
920
921 static gdb_byte *read_partial_die (struct partial_die_info *,
922                                    struct abbrev_info *abbrev,
923                                    unsigned int, bfd *,
924                                    gdb_byte *, gdb_byte *,
925                                    struct dwarf2_cu *);
926
927 static struct partial_die_info *find_partial_die (unsigned int,
928                                                   struct dwarf2_cu *);
929
930 static void fixup_partial_die (struct partial_die_info *,
931                                struct dwarf2_cu *);
932
933 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
934                                  bfd *, gdb_byte *, struct dwarf2_cu *);
935
936 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
937                                        bfd *, gdb_byte *, struct dwarf2_cu *);
938
939 static unsigned int read_1_byte (bfd *, gdb_byte *);
940
941 static int read_1_signed_byte (bfd *, gdb_byte *);
942
943 static unsigned int read_2_bytes (bfd *, gdb_byte *);
944
945 static unsigned int read_4_bytes (bfd *, gdb_byte *);
946
947 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
948
949 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
950                                unsigned int *);
951
952 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
953
954 static LONGEST read_checked_initial_length_and_offset
955   (bfd *, gdb_byte *, const struct comp_unit_head *,
956    unsigned int *, unsigned int *);
957
958 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
959                             unsigned int *);
960
961 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
962
963 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
964
965 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
966
967 static char *read_indirect_string (bfd *, gdb_byte *,
968                                    const struct comp_unit_head *,
969                                    unsigned int *);
970
971 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
972
973 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
974
975 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
976
977 static void set_cu_language (unsigned int, struct dwarf2_cu *);
978
979 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
980                                       struct dwarf2_cu *);
981
982 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
983                                                 unsigned int,
984                                                 struct dwarf2_cu *);
985
986 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
987                                struct dwarf2_cu *cu);
988
989 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
990
991 static struct die_info *die_specification (struct die_info *die,
992                                            struct dwarf2_cu **);
993
994 static void free_line_header (struct line_header *lh);
995
996 static void add_file_name (struct line_header *, char *, unsigned int,
997                            unsigned int, unsigned int);
998
999 static struct line_header *(dwarf_decode_line_header
1000                             (unsigned int offset,
1001                              bfd *abfd, struct dwarf2_cu *cu));
1002
1003 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1004                                 struct dwarf2_cu *, struct partial_symtab *);
1005
1006 static void dwarf2_start_subfile (char *, char *, char *);
1007
1008 static struct symbol *new_symbol (struct die_info *, struct type *,
1009                                   struct dwarf2_cu *);
1010
1011 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1012                                        struct dwarf2_cu *, struct symbol *);
1013
1014 static void dwarf2_const_value (struct attribute *, struct symbol *,
1015                                 struct dwarf2_cu *);
1016
1017 static void dwarf2_const_value_attr (struct attribute *attr,
1018                                      struct type *type,
1019                                      const char *name,
1020                                      struct obstack *obstack,
1021                                      struct dwarf2_cu *cu, long *value,
1022                                      gdb_byte **bytes,
1023                                      struct dwarf2_locexpr_baton **baton);
1024
1025 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1026
1027 static int need_gnat_info (struct dwarf2_cu *);
1028
1029 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1030
1031 static void set_descriptive_type (struct type *, struct die_info *,
1032                                   struct dwarf2_cu *);
1033
1034 static struct type *die_containing_type (struct die_info *,
1035                                          struct dwarf2_cu *);
1036
1037 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1038                                      struct dwarf2_cu *);
1039
1040 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1041
1042 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1043
1044 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1045
1046 static char *typename_concat (struct obstack *obs, const char *prefix,
1047                               const char *suffix, int physname,
1048                               struct dwarf2_cu *cu);
1049
1050 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1051
1052 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1053
1054 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1055
1056 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1057
1058 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1059                                struct dwarf2_cu *, struct partial_symtab *);
1060
1061 static int dwarf2_get_pc_bounds (struct die_info *,
1062                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1063                                  struct partial_symtab *);
1064
1065 static void get_scope_pc_bounds (struct die_info *,
1066                                  CORE_ADDR *, CORE_ADDR *,
1067                                  struct dwarf2_cu *);
1068
1069 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1070                                         CORE_ADDR, struct dwarf2_cu *);
1071
1072 static void dwarf2_add_field (struct field_info *, struct die_info *,
1073                               struct dwarf2_cu *);
1074
1075 static void dwarf2_attach_fields_to_type (struct field_info *,
1076                                           struct type *, struct dwarf2_cu *);
1077
1078 static void dwarf2_add_member_fn (struct field_info *,
1079                                   struct die_info *, struct type *,
1080                                   struct dwarf2_cu *);
1081
1082 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1083                                              struct type *, struct dwarf2_cu *);
1084
1085 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1086
1087 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1090
1091 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1092
1093 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1094
1095 static struct type *read_module_type (struct die_info *die,
1096                                       struct dwarf2_cu *cu);
1097
1098 static const char *namespace_name (struct die_info *die,
1099                                    int *is_anonymous, struct dwarf2_cu *);
1100
1101 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1102
1103 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1104
1105 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1106                                                        struct dwarf2_cu *);
1107
1108 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1109
1110 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1111                                                  gdb_byte *info_ptr,
1112                                                  gdb_byte **new_info_ptr,
1113                                                  struct die_info *parent);
1114
1115 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1116                                                gdb_byte *info_ptr,
1117                                                gdb_byte **new_info_ptr,
1118                                                struct die_info *parent);
1119
1120 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1121                                                gdb_byte *info_ptr,
1122                                                gdb_byte **new_info_ptr,
1123                                                struct die_info *parent);
1124
1125 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1126                                 struct die_info **, gdb_byte *,
1127                                 int *);
1128
1129 static void process_die (struct die_info *, struct dwarf2_cu *);
1130
1131 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1132                                        struct obstack *);
1133
1134 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1135
1136 static const char *dwarf2_full_name (char *name,
1137                                      struct die_info *die,
1138                                      struct dwarf2_cu *cu);
1139
1140 static struct die_info *dwarf2_extension (struct die_info *die,
1141                                           struct dwarf2_cu **);
1142
1143 static char *dwarf_tag_name (unsigned int);
1144
1145 static char *dwarf_attr_name (unsigned int);
1146
1147 static char *dwarf_form_name (unsigned int);
1148
1149 static char *dwarf_bool_name (unsigned int);
1150
1151 static char *dwarf_type_encoding_name (unsigned int);
1152
1153 #if 0
1154 static char *dwarf_cfi_name (unsigned int);
1155 #endif
1156
1157 static struct die_info *sibling_die (struct die_info *);
1158
1159 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1160
1161 static void dump_die_for_error (struct die_info *);
1162
1163 static void dump_die_1 (struct ui_file *, int level, int max_level,
1164                         struct die_info *);
1165
1166 /*static*/ void dump_die (struct die_info *, int max_level);
1167
1168 static void store_in_ref_table (struct die_info *,
1169                                 struct dwarf2_cu *);
1170
1171 static int is_ref_attr (struct attribute *);
1172
1173 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1174
1175 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1176
1177 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1178                                                struct attribute *,
1179                                                struct dwarf2_cu **);
1180
1181 static struct die_info *follow_die_ref (struct die_info *,
1182                                         struct attribute *,
1183                                         struct dwarf2_cu **);
1184
1185 static struct die_info *follow_die_sig (struct die_info *,
1186                                         struct attribute *,
1187                                         struct dwarf2_cu **);
1188
1189 static void read_signatured_type_at_offset (struct objfile *objfile,
1190                                             unsigned int offset);
1191
1192 static void read_signatured_type (struct objfile *,
1193                                   struct signatured_type *type_sig);
1194
1195 /* memory allocation interface */
1196
1197 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1198
1199 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1200
1201 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1202
1203 static void initialize_cu_func_list (struct dwarf2_cu *);
1204
1205 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1206                                  struct dwarf2_cu *);
1207
1208 static void dwarf_decode_macros (struct line_header *, unsigned int,
1209                                  char *, bfd *, struct dwarf2_cu *);
1210
1211 static int attr_form_is_block (struct attribute *);
1212
1213 static int attr_form_is_section_offset (struct attribute *);
1214
1215 static int attr_form_is_constant (struct attribute *);
1216
1217 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1218                                          struct symbol *sym,
1219                                          struct dwarf2_cu *cu);
1220
1221 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1222                                struct abbrev_info *abbrev,
1223                                struct dwarf2_cu *cu);
1224
1225 static void free_stack_comp_unit (void *);
1226
1227 static hashval_t partial_die_hash (const void *item);
1228
1229 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1230
1231 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1232   (unsigned int offset, struct objfile *objfile);
1233
1234 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1235   (unsigned int offset, struct objfile *objfile);
1236
1237 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1238
1239 static void free_one_comp_unit (void *);
1240
1241 static void free_cached_comp_units (void *);
1242
1243 static void age_cached_comp_units (void);
1244
1245 static void free_one_cached_comp_unit (void *);
1246
1247 static struct type *set_die_type (struct die_info *, struct type *,
1248                                   struct dwarf2_cu *);
1249
1250 static void create_all_comp_units (struct objfile *);
1251
1252 static int create_debug_types_hash_table (struct objfile *objfile);
1253
1254 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1255                                  struct objfile *);
1256
1257 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1258
1259 static void dwarf2_add_dependence (struct dwarf2_cu *,
1260                                    struct dwarf2_per_cu_data *);
1261
1262 static void dwarf2_mark (struct dwarf2_cu *);
1263
1264 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1265
1266 static struct type *get_die_type_at_offset (unsigned int,
1267                                             struct dwarf2_per_cu_data *per_cu);
1268
1269 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1270
1271 static void dwarf2_release_queue (void *dummy);
1272
1273 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1274                              struct objfile *objfile);
1275
1276 static void process_queue (struct objfile *objfile);
1277
1278 static void find_file_and_directory (struct die_info *die,
1279                                      struct dwarf2_cu *cu,
1280                                      char **name, char **comp_dir);
1281
1282 static char *file_full_name (int file, struct line_header *lh,
1283                              const char *comp_dir);
1284
1285 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1286                                               gdb_byte *info_ptr,
1287                                               gdb_byte *buffer,
1288                                               unsigned int buffer_size,
1289                                               bfd *abfd);
1290
1291 static void init_cu_die_reader (struct die_reader_specs *reader,
1292                                 struct dwarf2_cu *cu);
1293
1294 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1295
1296 #if WORDS_BIGENDIAN
1297
1298 /* Convert VALUE between big- and little-endian.  */
1299 static offset_type
1300 byte_swap (offset_type value)
1301 {
1302   offset_type result;
1303
1304   result = (value & 0xff) << 24;
1305   result |= (value & 0xff00) << 8;
1306   result |= (value & 0xff0000) >> 8;
1307   result |= (value & 0xff000000) >> 24;
1308   return result;
1309 }
1310
1311 #define MAYBE_SWAP(V)  byte_swap (V)
1312
1313 #else
1314 #define MAYBE_SWAP(V) (V)
1315 #endif /* WORDS_BIGENDIAN */
1316
1317 /* The suffix for an index file.  */
1318 #define INDEX_SUFFIX ".gdb-index"
1319
1320 static const char *dwarf2_physname (char *name, struct die_info *die,
1321                                     struct dwarf2_cu *cu);
1322
1323 /* Try to locate the sections we need for DWARF 2 debugging
1324    information and return true if we have enough to do something.  */
1325
1326 int
1327 dwarf2_has_info (struct objfile *objfile)
1328 {
1329   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1330   if (!dwarf2_per_objfile)
1331     {
1332       /* Initialize per-objfile state.  */
1333       struct dwarf2_per_objfile *data
1334         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1335
1336       memset (data, 0, sizeof (*data));
1337       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1338       dwarf2_per_objfile = data;
1339
1340       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1341       dwarf2_per_objfile->objfile = objfile;
1342     }
1343   return (dwarf2_per_objfile->info.asection != NULL
1344           && dwarf2_per_objfile->abbrev.asection != NULL);
1345 }
1346
1347 /* When loading sections, we can either look for ".<name>", or for
1348  * ".z<name>", which indicates a compressed section.  */
1349
1350 static int
1351 section_is_p (const char *section_name, const char *name)
1352 {
1353   return (section_name[0] == '.'
1354           && (strcmp (section_name + 1, name) == 0
1355               || (section_name[1] == 'z'
1356                   && strcmp (section_name + 2, name) == 0)));
1357 }
1358
1359 /* This function is mapped across the sections and remembers the
1360    offset and size of each of the debugging sections we are interested
1361    in.  */
1362
1363 static void
1364 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1365 {
1366   if (section_is_p (sectp->name, INFO_SECTION))
1367     {
1368       dwarf2_per_objfile->info.asection = sectp;
1369       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1370     }
1371   else if (section_is_p (sectp->name, ABBREV_SECTION))
1372     {
1373       dwarf2_per_objfile->abbrev.asection = sectp;
1374       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1375     }
1376   else if (section_is_p (sectp->name, LINE_SECTION))
1377     {
1378       dwarf2_per_objfile->line.asection = sectp;
1379       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1380     }
1381   else if (section_is_p (sectp->name, LOC_SECTION))
1382     {
1383       dwarf2_per_objfile->loc.asection = sectp;
1384       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1385     }
1386   else if (section_is_p (sectp->name, MACINFO_SECTION))
1387     {
1388       dwarf2_per_objfile->macinfo.asection = sectp;
1389       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1390     }
1391   else if (section_is_p (sectp->name, STR_SECTION))
1392     {
1393       dwarf2_per_objfile->str.asection = sectp;
1394       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1395     }
1396   else if (section_is_p (sectp->name, FRAME_SECTION))
1397     {
1398       dwarf2_per_objfile->frame.asection = sectp;
1399       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1400     }
1401   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1402     {
1403       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1404
1405       if (aflag & SEC_HAS_CONTENTS)
1406         {
1407           dwarf2_per_objfile->eh_frame.asection = sectp;
1408           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1409         }
1410     }
1411   else if (section_is_p (sectp->name, RANGES_SECTION))
1412     {
1413       dwarf2_per_objfile->ranges.asection = sectp;
1414       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1415     }
1416   else if (section_is_p (sectp->name, TYPES_SECTION))
1417     {
1418       dwarf2_per_objfile->types.asection = sectp;
1419       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1420     }
1421   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1422     {
1423       dwarf2_per_objfile->gdb_index.asection = sectp;
1424       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1425     }
1426
1427   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1428       && bfd_section_vma (abfd, sectp) == 0)
1429     dwarf2_per_objfile->has_section_at_zero = 1;
1430 }
1431
1432 /* Decompress a section that was compressed using zlib.  Store the
1433    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1434
1435 static void
1436 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1437                          gdb_byte **outbuf, bfd_size_type *outsize)
1438 {
1439   bfd *abfd = objfile->obfd;
1440 #ifndef HAVE_ZLIB_H
1441   error (_("Support for zlib-compressed DWARF data (from '%s') "
1442            "is disabled in this copy of GDB"),
1443          bfd_get_filename (abfd));
1444 #else
1445   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1446   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1447   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1448   bfd_size_type uncompressed_size;
1449   gdb_byte *uncompressed_buffer;
1450   z_stream strm;
1451   int rc;
1452   int header_size = 12;
1453
1454   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1455       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1456     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1457            bfd_get_filename (abfd));
1458
1459   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1460      by the uncompressed section size, 8 bytes in big-endian order.  */
1461   if (compressed_size < header_size
1462       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1463     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1464            bfd_get_filename (abfd));
1465   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1466   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1467   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1468   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1469   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1470   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1471   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1472   uncompressed_size += compressed_buffer[11];
1473
1474   /* It is possible the section consists of several compressed
1475      buffers concatenated together, so we uncompress in a loop.  */
1476   strm.zalloc = NULL;
1477   strm.zfree = NULL;
1478   strm.opaque = NULL;
1479   strm.avail_in = compressed_size - header_size;
1480   strm.next_in = (Bytef*) compressed_buffer + header_size;
1481   strm.avail_out = uncompressed_size;
1482   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1483                                        uncompressed_size);
1484   rc = inflateInit (&strm);
1485   while (strm.avail_in > 0)
1486     {
1487       if (rc != Z_OK)
1488         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1489                bfd_get_filename (abfd), rc);
1490       strm.next_out = ((Bytef*) uncompressed_buffer
1491                        + (uncompressed_size - strm.avail_out));
1492       rc = inflate (&strm, Z_FINISH);
1493       if (rc != Z_STREAM_END)
1494         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1495                bfd_get_filename (abfd), rc);
1496       rc = inflateReset (&strm);
1497     }
1498   rc = inflateEnd (&strm);
1499   if (rc != Z_OK
1500       || strm.avail_out != 0)
1501     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1502            bfd_get_filename (abfd), rc);
1503
1504   do_cleanups (cleanup);
1505   *outbuf = uncompressed_buffer;
1506   *outsize = uncompressed_size;
1507 #endif
1508 }
1509
1510 /* Read the contents of the section SECTP from object file specified by
1511    OBJFILE, store info about the section into INFO.
1512    If the section is compressed, uncompress it before returning.  */
1513
1514 static void
1515 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1516 {
1517   bfd *abfd = objfile->obfd;
1518   asection *sectp = info->asection;
1519   gdb_byte *buf, *retbuf;
1520   unsigned char header[4];
1521
1522   if (info->readin)
1523     return;
1524   info->buffer = NULL;
1525   info->was_mmapped = 0;
1526   info->readin = 1;
1527
1528   if (info->asection == NULL || info->size == 0)
1529     return;
1530
1531   /* Check if the file has a 4-byte header indicating compression.  */
1532   if (info->size > sizeof (header)
1533       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1534       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1535     {
1536       /* Upon decompression, update the buffer and its size.  */
1537       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1538         {
1539           zlib_decompress_section (objfile, sectp, &info->buffer,
1540                                    &info->size);
1541           return;
1542         }
1543     }
1544
1545 #ifdef HAVE_MMAP
1546   if (pagesize == 0)
1547     pagesize = getpagesize ();
1548
1549   /* Only try to mmap sections which are large enough: we don't want to
1550      waste space due to fragmentation.  Also, only try mmap for sections
1551      without relocations.  */
1552
1553   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1554     {
1555       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1556       size_t map_length = info->size + sectp->filepos - pg_offset;
1557       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1558                                  MAP_PRIVATE, pg_offset);
1559
1560       if (retbuf != MAP_FAILED)
1561         {
1562           info->was_mmapped = 1;
1563           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1564 #if HAVE_POSIX_MADVISE
1565           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1566 #endif
1567           return;
1568         }
1569     }
1570 #endif
1571
1572   /* If we get here, we are a normal, not-compressed section.  */
1573   info->buffer = buf
1574     = obstack_alloc (&objfile->objfile_obstack, info->size);
1575
1576   /* When debugging .o files, we may need to apply relocations; see
1577      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1578      We never compress sections in .o files, so we only need to
1579      try this when the section is not compressed.  */
1580   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1581   if (retbuf != NULL)
1582     {
1583       info->buffer = retbuf;
1584       return;
1585     }
1586
1587   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1588       || bfd_bread (buf, info->size, abfd) != info->size)
1589     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1590            bfd_get_filename (abfd));
1591 }
1592
1593 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1594    SECTION_NAME. */
1595
1596 void
1597 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1598                          asection **sectp, gdb_byte **bufp,
1599                          bfd_size_type *sizep)
1600 {
1601   struct dwarf2_per_objfile *data
1602     = objfile_data (objfile, dwarf2_objfile_data_key);
1603   struct dwarf2_section_info *info;
1604
1605   /* We may see an objfile without any DWARF, in which case we just
1606      return nothing.  */
1607   if (data == NULL)
1608     {
1609       *sectp = NULL;
1610       *bufp = NULL;
1611       *sizep = 0;
1612       return;
1613     }
1614   if (section_is_p (section_name, EH_FRAME_SECTION))
1615     info = &data->eh_frame;
1616   else if (section_is_p (section_name, FRAME_SECTION))
1617     info = &data->frame;
1618   else
1619     gdb_assert_not_reached ("unexpected section");
1620
1621   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1622     /* We haven't read this section in yet.  Do it now.  */
1623     dwarf2_read_section (objfile, info);
1624
1625   *sectp = info->asection;
1626   *bufp = info->buffer;
1627   *sizep = info->size;
1628 }
1629
1630 \f
1631
1632 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1633    this CU came.  */
1634 static void
1635 dw2_do_instantiate_symtab (struct objfile *objfile,
1636                            struct dwarf2_per_cu_data *per_cu)
1637 {
1638   struct cleanup *back_to;
1639
1640   back_to = make_cleanup (dwarf2_release_queue, NULL);
1641
1642   queue_comp_unit (per_cu, objfile);
1643
1644   if (per_cu->from_debug_types)
1645     read_signatured_type_at_offset (objfile, per_cu->offset);
1646   else
1647     load_full_comp_unit (per_cu, objfile);
1648
1649   process_queue (objfile);
1650
1651   /* Age the cache, releasing compilation units that have not
1652      been used recently.  */
1653   age_cached_comp_units ();
1654
1655   do_cleanups (back_to);
1656 }
1657
1658 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1659    the objfile from which this CU came.  Returns the resulting symbol
1660    table.  */
1661 static struct symtab *
1662 dw2_instantiate_symtab (struct objfile *objfile,
1663                         struct dwarf2_per_cu_data *per_cu)
1664 {
1665   if (!per_cu->v.quick->symtab)
1666     {
1667       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1668       increment_reading_symtab ();
1669       dw2_do_instantiate_symtab (objfile, per_cu);
1670       do_cleanups (back_to);
1671     }
1672   return per_cu->v.quick->symtab;
1673 }
1674
1675 /* Return the CU given its index.  */
1676 static struct dwarf2_per_cu_data *
1677 dw2_get_cu (int index)
1678 {
1679   if (index >= dwarf2_per_objfile->n_comp_units)
1680     {
1681       index -= dwarf2_per_objfile->n_comp_units;
1682       return dwarf2_per_objfile->type_comp_units[index];
1683     }
1684   return dwarf2_per_objfile->all_comp_units[index];
1685 }
1686
1687 /* A helper function that knows how to read a 64-bit value in a way
1688    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1689    otherwise.  */
1690 static int
1691 extract_cu_value (const char *bytes, ULONGEST *result)
1692 {
1693   if (sizeof (ULONGEST) < 8)
1694     {
1695       int i;
1696
1697       /* Ignore the upper 4 bytes if they are all zero.  */
1698       for (i = 0; i < 4; ++i)
1699         if (bytes[i + 4] != 0)
1700           return 0;
1701
1702       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1703     }
1704   else
1705     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1706   return 1;
1707 }
1708
1709 /* Read the CU list from the mapped index, and use it to create all
1710    the CU objects for this objfile.  Return 0 if something went wrong,
1711    1 if everything went ok.  */
1712 static int
1713 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1714                        offset_type cu_list_elements)
1715 {
1716   offset_type i;
1717
1718   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1719   dwarf2_per_objfile->all_comp_units
1720     = obstack_alloc (&objfile->objfile_obstack,
1721                      dwarf2_per_objfile->n_comp_units
1722                      * sizeof (struct dwarf2_per_cu_data *));
1723
1724   for (i = 0; i < cu_list_elements; i += 2)
1725     {
1726       struct dwarf2_per_cu_data *the_cu;
1727       ULONGEST offset, length;
1728
1729       if (!extract_cu_value (cu_list, &offset)
1730           || !extract_cu_value (cu_list + 8, &length))
1731         return 0;
1732       cu_list += 2 * 8;
1733
1734       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1735                                struct dwarf2_per_cu_data);
1736       the_cu->offset = offset;
1737       the_cu->length = length;
1738       the_cu->objfile = objfile;
1739       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1740                                         struct dwarf2_per_cu_quick_data);
1741       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1742     }
1743
1744   return 1;
1745 }
1746
1747 /* Create the signatured type hash table from the index.  */
1748
1749 static int
1750 create_signatured_type_table_from_index (struct objfile *objfile,
1751                                          const gdb_byte *bytes,
1752                                          offset_type elements)
1753 {
1754   offset_type i;
1755   htab_t sig_types_hash;
1756
1757   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1758   dwarf2_per_objfile->type_comp_units
1759     = obstack_alloc (&objfile->objfile_obstack,
1760                      dwarf2_per_objfile->n_type_comp_units
1761                      * sizeof (struct dwarf2_per_cu_data *));
1762
1763   sig_types_hash = allocate_signatured_type_table (objfile);
1764
1765   for (i = 0; i < elements; i += 3)
1766     {
1767       struct signatured_type *type_sig;
1768       ULONGEST offset, type_offset, signature;
1769       void **slot;
1770
1771       if (!extract_cu_value (bytes, &offset)
1772           || !extract_cu_value (bytes + 8, &type_offset))
1773         return 0;
1774       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1775       bytes += 3 * 8;
1776
1777       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1778                                  struct signatured_type);
1779       type_sig->signature = signature;
1780       type_sig->offset = offset;
1781       type_sig->type_offset = type_offset;
1782       type_sig->per_cu.from_debug_types = 1;
1783       type_sig->per_cu.offset = offset;
1784       type_sig->per_cu.objfile = objfile;
1785       type_sig->per_cu.v.quick
1786         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1787                           struct dwarf2_per_cu_quick_data);
1788
1789       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1790       *slot = type_sig;
1791
1792       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1793     }
1794
1795   dwarf2_per_objfile->signatured_types = sig_types_hash;
1796
1797   return 1;
1798 }
1799
1800 /* Read the address map data from the mapped index, and use it to
1801    populate the objfile's psymtabs_addrmap.  */
1802 static void
1803 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1804 {
1805   const gdb_byte *iter, *end;
1806   struct obstack temp_obstack;
1807   struct addrmap *mutable_map;
1808   struct cleanup *cleanup;
1809   CORE_ADDR baseaddr;
1810
1811   obstack_init (&temp_obstack);
1812   cleanup = make_cleanup_obstack_free (&temp_obstack);
1813   mutable_map = addrmap_create_mutable (&temp_obstack);
1814
1815   iter = index->address_table;
1816   end = iter + index->address_table_size;
1817
1818   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1819
1820   while (iter < end)
1821     {
1822       ULONGEST hi, lo, cu_index;
1823       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1824       iter += 8;
1825       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1826       iter += 8;
1827       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1828       iter += 4;
1829       
1830       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1831                          dw2_get_cu (cu_index));
1832     }
1833
1834   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1835                                                     &objfile->objfile_obstack);
1836   do_cleanups (cleanup);
1837 }
1838
1839 /* The hash function for strings in the mapped index.  This is the
1840    same as the hashtab.c hash function, but we keep a separate copy to
1841    maintain control over the implementation.  This is necessary
1842    because the hash function is tied to the format of the mapped index
1843    file.  */
1844 static hashval_t
1845 mapped_index_string_hash (const void *p)
1846 {
1847   const unsigned char *str = (const unsigned char *) p;
1848   hashval_t r = 0;
1849   unsigned char c;
1850
1851   while ((c = *str++) != 0)
1852     r = r * 67 + c - 113;
1853
1854   return r;
1855 }
1856
1857 /* Find a slot in the mapped index INDEX for the object named NAME.
1858    If NAME is found, set *VEC_OUT to point to the CU vector in the
1859    constant pool and return 1.  If NAME cannot be found, return 0.  */
1860 static int
1861 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1862                           offset_type **vec_out)
1863 {
1864   offset_type hash = mapped_index_string_hash (name);
1865   offset_type slot, step;
1866
1867   slot = hash & (index->index_table_slots - 1);
1868   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1869
1870   for (;;)
1871     {
1872       /* Convert a slot number to an offset into the table.  */
1873       offset_type i = 2 * slot;
1874       const char *str;
1875       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1876         return 0;
1877
1878       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1879       if (!strcmp (name, str))
1880         {
1881           *vec_out = (offset_type *) (index->constant_pool
1882                                       + MAYBE_SWAP (index->index_table[i + 1]));
1883           return 1;
1884         }
1885
1886       slot = (slot + step) & (index->index_table_slots - 1);
1887     }
1888 }
1889
1890 /* Read the index file.  If everything went ok, initialize the "quick"
1891    elements of all the CUs and return 1.  Otherwise, return 0.  */
1892 static int
1893 dwarf2_read_index (struct objfile *objfile)
1894 {
1895   char *addr;
1896   struct mapped_index *map;
1897   offset_type *metadata;
1898   const gdb_byte *cu_list;
1899   const gdb_byte *types_list = NULL;
1900   offset_type version, cu_list_elements;
1901   offset_type types_list_elements = 0;
1902   int i;
1903
1904   if (dwarf2_per_objfile->gdb_index.asection == NULL
1905       || dwarf2_per_objfile->gdb_index.size == 0)
1906     return 0;
1907
1908   /* Older elfutils strip versions could keep the section in the main
1909      executable while splitting it for the separate debug info file.  */
1910   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1911        & SEC_HAS_CONTENTS) == 0)
1912     return 0;
1913
1914   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1915
1916   addr = dwarf2_per_objfile->gdb_index.buffer;
1917   /* Version check.  */
1918   version = MAYBE_SWAP (*(offset_type *) addr);
1919   if (version == 1)
1920     {
1921       /* Index version 1 neglected to account for .debug_types.  So,
1922          if we see .debug_types, we cannot use this index.  */
1923       if (dwarf2_per_objfile->types.asection != NULL
1924           && dwarf2_per_objfile->types.size != 0)
1925         return 0;
1926     }
1927   else if (version != 2)
1928     return 0;
1929
1930   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1931   map->total_size = dwarf2_per_objfile->gdb_index.size;
1932
1933   metadata = (offset_type *) (addr + sizeof (offset_type));
1934
1935   i = 0;
1936   cu_list = addr + MAYBE_SWAP (metadata[i]);
1937   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1938                       / 8);
1939   ++i;
1940
1941   if (version == 2)
1942     {
1943       types_list = addr + MAYBE_SWAP (metadata[i]);
1944       types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1945                               - MAYBE_SWAP (metadata[i]))
1946                              / 8);
1947       ++i;
1948     }
1949
1950   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1951   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1952                              - MAYBE_SWAP (metadata[i]));
1953   ++i;
1954
1955   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1956   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1957                              - MAYBE_SWAP (metadata[i]))
1958                             / (2 * sizeof (offset_type)));
1959   ++i;
1960
1961   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1962
1963   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1964     return 0;
1965
1966   if (version == 2
1967       && types_list_elements
1968       && !create_signatured_type_table_from_index (objfile, types_list,
1969                                                    types_list_elements))
1970     return 0;
1971
1972   create_addrmap_from_index (objfile, map);
1973
1974   dwarf2_per_objfile->index_table = map;
1975   dwarf2_per_objfile->using_index = 1;
1976
1977   return 1;
1978 }
1979
1980 /* A helper for the "quick" functions which sets the global
1981    dwarf2_per_objfile according to OBJFILE.  */
1982 static void
1983 dw2_setup (struct objfile *objfile)
1984 {
1985   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1986   gdb_assert (dwarf2_per_objfile);
1987 }
1988
1989 /* A helper for the "quick" functions which attempts to read the line
1990    table for THIS_CU.  */
1991 static void
1992 dw2_require_line_header (struct objfile *objfile,
1993                          struct dwarf2_per_cu_data *this_cu)
1994 {
1995   bfd *abfd = objfile->obfd;
1996   struct line_header *lh = NULL;
1997   struct attribute *attr;
1998   struct cleanup *cleanups;
1999   struct die_info *comp_unit_die;
2000   struct dwarf2_section_info* sec;
2001   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2002   int has_children, i;
2003   struct dwarf2_cu cu;
2004   unsigned int bytes_read, buffer_size;
2005   struct die_reader_specs reader_specs;
2006   char *name, *comp_dir;
2007
2008   if (this_cu->v.quick->read_lines)
2009     return;
2010   this_cu->v.quick->read_lines = 1;
2011
2012   memset (&cu, 0, sizeof (cu));
2013   cu.objfile = objfile;
2014   obstack_init (&cu.comp_unit_obstack);
2015
2016   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2017
2018   if (this_cu->from_debug_types)
2019     sec = &dwarf2_per_objfile->types;
2020   else
2021     sec = &dwarf2_per_objfile->info;
2022   dwarf2_read_section (objfile, sec);
2023   buffer_size = sec->size;
2024   buffer = sec->buffer;
2025   info_ptr = buffer + this_cu->offset;
2026   beg_of_comp_unit = info_ptr;
2027
2028   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2029                                           buffer, buffer_size,
2030                                           abfd);
2031
2032   /* Complete the cu_header.  */
2033   cu.header.offset = beg_of_comp_unit - buffer;
2034   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2035
2036   this_cu->cu = &cu;
2037   cu.per_cu = this_cu;
2038
2039   dwarf2_read_abbrevs (abfd, &cu);
2040   make_cleanup (dwarf2_free_abbrev_table, &cu);
2041
2042   if (this_cu->from_debug_types)
2043     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2044   init_cu_die_reader (&reader_specs, &cu);
2045   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2046                             &has_children);
2047
2048   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2049   if (attr)
2050     {
2051       unsigned int line_offset = DW_UNSND (attr);
2052       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2053     }
2054   if (lh == NULL)
2055     {
2056       do_cleanups (cleanups);
2057       return;
2058     }
2059
2060   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2061
2062   this_cu->v.quick->lines = lh;
2063
2064   this_cu->v.quick->file_names
2065     = obstack_alloc (&objfile->objfile_obstack,
2066                      lh->num_file_names * sizeof (char *));
2067   for (i = 0; i < lh->num_file_names; ++i)
2068     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2069
2070   do_cleanups (cleanups);
2071 }
2072
2073 /* A helper for the "quick" functions which computes and caches the
2074    real path for a given file name from the line table.
2075    dw2_require_line_header must have been called before this is
2076    invoked.  */
2077 static const char *
2078 dw2_require_full_path (struct objfile *objfile,
2079                        struct dwarf2_per_cu_data *per_cu,
2080                        int index)
2081 {
2082   if (!per_cu->v.quick->full_names)
2083     per_cu->v.quick->full_names
2084       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2085                         per_cu->v.quick->lines->num_file_names,
2086                         sizeof (char *));
2087
2088   if (!per_cu->v.quick->full_names[index])
2089     per_cu->v.quick->full_names[index]
2090       = gdb_realpath (per_cu->v.quick->file_names[index]);
2091
2092   return per_cu->v.quick->full_names[index];
2093 }
2094
2095 static struct symtab *
2096 dw2_find_last_source_symtab (struct objfile *objfile)
2097 {
2098   int index;
2099   dw2_setup (objfile);
2100   index = dwarf2_per_objfile->n_comp_units - 1;
2101   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2102 }
2103
2104 static void
2105 dw2_forget_cached_source_info (struct objfile *objfile)
2106 {
2107   int i;
2108
2109   dw2_setup (objfile);
2110   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2111                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2112     {
2113       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2114
2115       if (per_cu->v.quick->full_names)
2116         {
2117           int j;
2118
2119           for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2120             xfree ((void *) per_cu->v.quick->full_names[j]);
2121         }
2122     }
2123 }
2124
2125 static int
2126 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2127                    const char *full_path, const char *real_path,
2128                    struct symtab **result)
2129 {
2130   int i;
2131   int check_basename = lbasename (name) == name;
2132   struct dwarf2_per_cu_data *base_cu = NULL;
2133
2134   dw2_setup (objfile);
2135   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2136                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2137     {
2138       int j;
2139       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2140
2141       if (per_cu->v.quick->symtab)
2142         continue;
2143
2144       dw2_require_line_header (objfile, per_cu);
2145       if (!per_cu->v.quick->lines)
2146         continue;
2147
2148       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2149         {
2150           const char *this_name = per_cu->v.quick->file_names[j];
2151
2152           if (FILENAME_CMP (name, this_name) == 0)
2153             {
2154               *result = dw2_instantiate_symtab (objfile, per_cu);
2155               return 1;
2156             }
2157
2158           if (check_basename && ! base_cu
2159               && FILENAME_CMP (lbasename (this_name), name) == 0)
2160             base_cu = per_cu;
2161
2162           if (full_path != NULL)
2163             {
2164               const char *this_full_name = dw2_require_full_path (objfile,
2165                                                                   per_cu, j);
2166
2167               if (this_full_name
2168                   && FILENAME_CMP (full_path, this_full_name) == 0)
2169                 {
2170                   *result = dw2_instantiate_symtab (objfile, per_cu);
2171                   return 1;
2172                 }
2173             }
2174
2175           if (real_path != NULL)
2176             {
2177               const char *this_full_name = dw2_require_full_path (objfile,
2178                                                                   per_cu, j);
2179
2180               if (this_full_name != NULL)
2181                 {
2182                   char *rp = gdb_realpath (this_full_name);
2183                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2184                     {
2185                       xfree (rp);
2186                       *result = dw2_instantiate_symtab (objfile, per_cu);
2187                       return 1;
2188                     }
2189                   xfree (rp);
2190                 }
2191             }
2192         }
2193     }
2194
2195   if (base_cu)
2196     {
2197       *result = dw2_instantiate_symtab (objfile, base_cu);
2198       return 1;
2199     }
2200
2201   return 0;
2202 }
2203
2204 static struct symtab *
2205 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2206                    const char *name, domain_enum domain)
2207 {
2208   /* We do all the work in the pre_expand_symtabs_matching hook
2209      instead.  */
2210   return NULL;
2211 }
2212
2213 /* A helper function that expands all symtabs that hold an object
2214    named NAME.  */
2215 static void
2216 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2217 {
2218   dw2_setup (objfile);
2219
2220   if (dwarf2_per_objfile->index_table)
2221     {
2222       offset_type *vec;
2223
2224       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2225                                     name, &vec))
2226         {
2227           offset_type i, len = MAYBE_SWAP (*vec);
2228           for (i = 0; i < len; ++i)
2229             {
2230               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2231               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2232
2233               dw2_instantiate_symtab (objfile, per_cu);
2234             }
2235         }
2236     }
2237 }
2238
2239 static void
2240 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2241                                  int kind, const char *name,
2242                                  domain_enum domain)
2243 {
2244   dw2_do_expand_symtabs_matching (objfile, name);
2245 }
2246
2247 static void
2248 dw2_print_stats (struct objfile *objfile)
2249 {
2250   int i, count;
2251
2252   dw2_setup (objfile);
2253   count = 0;
2254   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2255                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2256     {
2257       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2258
2259       if (!per_cu->v.quick->symtab)
2260         ++count;
2261     }
2262   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2263 }
2264
2265 static void
2266 dw2_dump (struct objfile *objfile)
2267 {
2268   /* Nothing worth printing.  */
2269 }
2270
2271 static void
2272 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2273               struct section_offsets *delta)
2274 {
2275   /* There's nothing to relocate here.  */
2276 }
2277
2278 static void
2279 dw2_expand_symtabs_for_function (struct objfile *objfile,
2280                                  const char *func_name)
2281 {
2282   dw2_do_expand_symtabs_matching (objfile, func_name);
2283 }
2284
2285 static void
2286 dw2_expand_all_symtabs (struct objfile *objfile)
2287 {
2288   int i;
2289
2290   dw2_setup (objfile);
2291
2292   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2293                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2294     {
2295       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2296
2297       dw2_instantiate_symtab (objfile, per_cu);
2298     }
2299 }
2300
2301 static void
2302 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2303                                   const char *filename)
2304 {
2305   int i;
2306
2307   dw2_setup (objfile);
2308   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2309                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2310     {
2311       int j;
2312       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2313
2314       if (per_cu->v.quick->symtab)
2315         continue;
2316
2317       dw2_require_line_header (objfile, per_cu);
2318       if (!per_cu->v.quick->lines)
2319         continue;
2320
2321       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2322         {
2323           const char *this_name = per_cu->v.quick->file_names[j];
2324           if (strcmp (this_name, filename) == 0)
2325             {
2326               dw2_instantiate_symtab (objfile, per_cu);
2327               break;
2328             }
2329         }
2330     }
2331 }
2332
2333 static const char *
2334 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2335 {
2336   struct dwarf2_per_cu_data *per_cu;
2337   offset_type *vec;
2338
2339   dw2_setup (objfile);
2340
2341   if (!dwarf2_per_objfile->index_table)
2342     return NULL;
2343
2344   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2345                                  name, &vec))
2346     return NULL;
2347
2348   /* Note that this just looks at the very first one named NAME -- but
2349      actually we are looking for a function.  find_main_filename
2350      should be rewritten so that it doesn't require a custom hook.  It
2351      could just use the ordinary symbol tables.  */
2352   /* vec[0] is the length, which must always be >0.  */
2353   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2354
2355   dw2_require_line_header (objfile, per_cu);
2356   if (!per_cu->v.quick->lines)
2357     return NULL;
2358
2359   return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2360 }
2361
2362 static void
2363 dw2_map_ada_symtabs (struct objfile *objfile,
2364                      int (*wild_match) (const char *, int, const char *),
2365                      int (*is_name_suffix) (const char *),
2366                      void (*callback) (struct objfile *,
2367                                        struct symtab *, void *),
2368                      const char *name, int global,
2369                      domain_enum namespace, int wild,
2370                      void *data)
2371 {
2372   /* For now, we don't support Ada, so this function can't be
2373      reached.  */
2374   internal_error (__FILE__, __LINE__,
2375                   _("map_ada_symtabs called via index method"));
2376 }
2377
2378 static void
2379 dw2_expand_symtabs_matching (struct objfile *objfile,
2380                              int (*file_matcher) (const char *, void *),
2381                              int (*name_matcher) (const char *, void *),
2382                              domain_enum kind,
2383                              void *data)
2384 {
2385   int i;
2386   offset_type iter;
2387
2388   dw2_setup (objfile);
2389   if (!dwarf2_per_objfile->index_table)
2390     return;
2391
2392   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2393                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2394     {
2395       int j;
2396       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2397
2398       per_cu->v.quick->mark = 0;
2399       if (per_cu->v.quick->symtab)
2400         continue;
2401
2402       dw2_require_line_header (objfile, per_cu);
2403       if (!per_cu->v.quick->lines)
2404         continue;
2405
2406       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2407         {
2408           if (file_matcher (per_cu->v.quick->file_names[j], data))
2409             {
2410               per_cu->v.quick->mark = 1;
2411               break;
2412             }
2413         }
2414     }
2415
2416   for (iter = 0;
2417        iter < dwarf2_per_objfile->index_table->index_table_slots;
2418        ++iter)
2419     {
2420       offset_type idx = 2 * iter;
2421       const char *name;
2422       offset_type *vec, vec_len, vec_idx;
2423
2424       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2425           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2426         continue;
2427
2428       name = (dwarf2_per_objfile->index_table->constant_pool
2429               + dwarf2_per_objfile->index_table->index_table[idx]);
2430
2431       if (! (*name_matcher) (name, data))
2432         continue;
2433
2434       /* The name was matched, now expand corresponding CUs that were
2435          marked.  */
2436       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2437                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2438       vec_len = MAYBE_SWAP (vec[0]);
2439       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2440         {
2441           struct dwarf2_per_cu_data *per_cu;
2442
2443           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2444           if (per_cu->v.quick->mark)
2445             dw2_instantiate_symtab (objfile, per_cu);
2446         }
2447     }
2448 }
2449
2450 static struct symtab *
2451 dw2_find_pc_sect_symtab (struct objfile *objfile,
2452                          struct minimal_symbol *msymbol,
2453                          CORE_ADDR pc,
2454                          struct obj_section *section,
2455                          int warn_if_readin)
2456 {
2457   struct dwarf2_per_cu_data *data;
2458
2459   dw2_setup (objfile);
2460
2461   if (!objfile->psymtabs_addrmap)
2462     return NULL;
2463
2464   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2465   if (!data)
2466     return NULL;
2467
2468   if (warn_if_readin && data->v.quick->symtab)
2469     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2470              paddress (get_objfile_arch (objfile), pc));
2471
2472   return dw2_instantiate_symtab (objfile, data);
2473 }
2474
2475 static void
2476 dw2_map_symbol_names (struct objfile *objfile,
2477                       void (*fun) (const char *, void *),
2478                       void *data)
2479 {
2480   offset_type iter;
2481   dw2_setup (objfile);
2482
2483   if (!dwarf2_per_objfile->index_table)
2484     return;
2485
2486   for (iter = 0;
2487        iter < dwarf2_per_objfile->index_table->index_table_slots;
2488        ++iter)
2489     {
2490       offset_type idx = 2 * iter;
2491       const char *name;
2492       offset_type *vec, vec_len, vec_idx;
2493
2494       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2495           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2496         continue;
2497
2498       name = (dwarf2_per_objfile->index_table->constant_pool
2499               + dwarf2_per_objfile->index_table->index_table[idx]);
2500
2501       (*fun) (name, data);
2502     }
2503 }
2504
2505 static void
2506 dw2_map_symbol_filenames (struct objfile *objfile,
2507                           void (*fun) (const char *, const char *, void *),
2508                           void *data)
2509 {
2510   int i;
2511
2512   dw2_setup (objfile);
2513   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2514                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2515     {
2516       int j;
2517       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2518
2519       if (per_cu->v.quick->symtab)
2520         continue;
2521
2522       dw2_require_line_header (objfile, per_cu);
2523       if (!per_cu->v.quick->lines)
2524         continue;
2525
2526       for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2527         {
2528           const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2529                                                               j);
2530           (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2531         }
2532     }
2533 }
2534
2535 static int
2536 dw2_has_symbols (struct objfile *objfile)
2537 {
2538   return 1;
2539 }
2540
2541 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2542 {
2543   dw2_has_symbols,
2544   dw2_find_last_source_symtab,
2545   dw2_forget_cached_source_info,
2546   dw2_lookup_symtab,
2547   dw2_lookup_symbol,
2548   dw2_pre_expand_symtabs_matching,
2549   dw2_print_stats,
2550   dw2_dump,
2551   dw2_relocate,
2552   dw2_expand_symtabs_for_function,
2553   dw2_expand_all_symtabs,
2554   dw2_expand_symtabs_with_filename,
2555   dw2_find_symbol_file,
2556   dw2_map_ada_symtabs,
2557   dw2_expand_symtabs_matching,
2558   dw2_find_pc_sect_symtab,
2559   dw2_map_symbol_names,
2560   dw2_map_symbol_filenames
2561 };
2562
2563 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2564    file will use psymtabs, or 1 if using the GNU index.  */
2565
2566 int
2567 dwarf2_initialize_objfile (struct objfile *objfile)
2568 {
2569   /* If we're about to read full symbols, don't bother with the
2570      indices.  In this case we also don't care if some other debug
2571      format is making psymtabs, because they are all about to be
2572      expanded anyway.  */
2573   if ((objfile->flags & OBJF_READNOW))
2574     {
2575       int i;
2576
2577       dwarf2_per_objfile->using_index = 1;
2578       create_all_comp_units (objfile);
2579       create_debug_types_hash_table (objfile);
2580
2581       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2582                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2583         {
2584           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2585
2586           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2587                                             struct dwarf2_per_cu_quick_data);
2588         }
2589
2590       /* Return 1 so that gdb sees the "quick" functions.  However,
2591          these functions will be no-ops because we will have expanded
2592          all symtabs.  */
2593       return 1;
2594     }
2595
2596   if (dwarf2_read_index (objfile))
2597     return 1;
2598
2599   dwarf2_build_psymtabs (objfile);
2600   return 0;
2601 }
2602
2603 \f
2604
2605 /* Build a partial symbol table.  */
2606
2607 void
2608 dwarf2_build_psymtabs (struct objfile *objfile)
2609 {
2610   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2611     {
2612       init_psymbol_list (objfile, 1024);
2613     }
2614
2615   dwarf2_build_psymtabs_hard (objfile);
2616 }
2617
2618 /* Return TRUE if OFFSET is within CU_HEADER.  */
2619
2620 static inline int
2621 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2622 {
2623   unsigned int bottom = cu_header->offset;
2624   unsigned int top = (cu_header->offset
2625                       + cu_header->length
2626                       + cu_header->initial_length_size);
2627
2628   return (offset >= bottom && offset < top);
2629 }
2630
2631 /* Read in the comp unit header information from the debug_info at info_ptr.
2632    NOTE: This leaves members offset, first_die_offset to be filled in
2633    by the caller.  */
2634
2635 static gdb_byte *
2636 read_comp_unit_head (struct comp_unit_head *cu_header,
2637                      gdb_byte *info_ptr, bfd *abfd)
2638 {
2639   int signed_addr;
2640   unsigned int bytes_read;
2641
2642   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2643   cu_header->initial_length_size = bytes_read;
2644   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2645   info_ptr += bytes_read;
2646   cu_header->version = read_2_bytes (abfd, info_ptr);
2647   info_ptr += 2;
2648   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2649                                           &bytes_read);
2650   info_ptr += bytes_read;
2651   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2652   info_ptr += 1;
2653   signed_addr = bfd_get_sign_extend_vma (abfd);
2654   if (signed_addr < 0)
2655     internal_error (__FILE__, __LINE__,
2656                     _("read_comp_unit_head: dwarf from non elf file"));
2657   cu_header->signed_addr_p = signed_addr;
2658
2659   return info_ptr;
2660 }
2661
2662 static gdb_byte *
2663 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2664                              gdb_byte *buffer, unsigned int buffer_size,
2665                              bfd *abfd)
2666 {
2667   gdb_byte *beg_of_comp_unit = info_ptr;
2668
2669   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2670
2671   if (header->version != 2 && header->version != 3 && header->version != 4)
2672     error (_("Dwarf Error: wrong version in compilation unit header "
2673            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2674            bfd_get_filename (abfd));
2675
2676   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2677     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2678            "(offset 0x%lx + 6) [in module %s]"),
2679            (long) header->abbrev_offset,
2680            (long) (beg_of_comp_unit - buffer),
2681            bfd_get_filename (abfd));
2682
2683   if (beg_of_comp_unit + header->length + header->initial_length_size
2684       > buffer + buffer_size)
2685     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2686            "(offset 0x%lx + 0) [in module %s]"),
2687            (long) header->length,
2688            (long) (beg_of_comp_unit - buffer),
2689            bfd_get_filename (abfd));
2690
2691   return info_ptr;
2692 }
2693
2694 /* Read in the types comp unit header information from .debug_types entry at
2695    types_ptr.  The result is a pointer to one past the end of the header.  */
2696
2697 static gdb_byte *
2698 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2699                           ULONGEST *signature,
2700                           gdb_byte *types_ptr, bfd *abfd)
2701 {
2702   gdb_byte *initial_types_ptr = types_ptr;
2703
2704   dwarf2_read_section (dwarf2_per_objfile->objfile,
2705                        &dwarf2_per_objfile->types);
2706   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2707
2708   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2709
2710   *signature = read_8_bytes (abfd, types_ptr);
2711   types_ptr += 8;
2712   types_ptr += cu_header->offset_size;
2713   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2714
2715   return types_ptr;
2716 }
2717
2718 /* Allocate a new partial symtab for file named NAME and mark this new
2719    partial symtab as being an include of PST.  */
2720
2721 static void
2722 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2723                                struct objfile *objfile)
2724 {
2725   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2726
2727   subpst->section_offsets = pst->section_offsets;
2728   subpst->textlow = 0;
2729   subpst->texthigh = 0;
2730
2731   subpst->dependencies = (struct partial_symtab **)
2732     obstack_alloc (&objfile->objfile_obstack,
2733                    sizeof (struct partial_symtab *));
2734   subpst->dependencies[0] = pst;
2735   subpst->number_of_dependencies = 1;
2736
2737   subpst->globals_offset = 0;
2738   subpst->n_global_syms = 0;
2739   subpst->statics_offset = 0;
2740   subpst->n_static_syms = 0;
2741   subpst->symtab = NULL;
2742   subpst->read_symtab = pst->read_symtab;
2743   subpst->readin = 0;
2744
2745   /* No private part is necessary for include psymtabs.  This property
2746      can be used to differentiate between such include psymtabs and
2747      the regular ones.  */
2748   subpst->read_symtab_private = NULL;
2749 }
2750
2751 /* Read the Line Number Program data and extract the list of files
2752    included by the source file represented by PST.  Build an include
2753    partial symtab for each of these included files.  */
2754
2755 static void
2756 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2757                                struct die_info *die,
2758                                struct partial_symtab *pst)
2759 {
2760   struct objfile *objfile = cu->objfile;
2761   bfd *abfd = objfile->obfd;
2762   struct line_header *lh = NULL;
2763   struct attribute *attr;
2764
2765   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2766   if (attr)
2767     {
2768       unsigned int line_offset = DW_UNSND (attr);
2769
2770       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2771     }
2772   if (lh == NULL)
2773     return;  /* No linetable, so no includes.  */
2774
2775   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2776   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2777
2778   free_line_header (lh);
2779 }
2780
2781 static hashval_t
2782 hash_type_signature (const void *item)
2783 {
2784   const struct signatured_type *type_sig = item;
2785
2786   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2787   return type_sig->signature;
2788 }
2789
2790 static int
2791 eq_type_signature (const void *item_lhs, const void *item_rhs)
2792 {
2793   const struct signatured_type *lhs = item_lhs;
2794   const struct signatured_type *rhs = item_rhs;
2795
2796   return lhs->signature == rhs->signature;
2797 }
2798
2799 /* Allocate a hash table for signatured types.  */
2800
2801 static htab_t
2802 allocate_signatured_type_table (struct objfile *objfile)
2803 {
2804   return htab_create_alloc_ex (41,
2805                                hash_type_signature,
2806                                eq_type_signature,
2807                                NULL,
2808                                &objfile->objfile_obstack,
2809                                hashtab_obstack_allocate,
2810                                dummy_obstack_deallocate);
2811 }
2812
2813 /* A helper function to add a signatured type CU to a list.  */
2814
2815 static int
2816 add_signatured_type_cu_to_list (void **slot, void *datum)
2817 {
2818   struct signatured_type *sigt = *slot;
2819   struct dwarf2_per_cu_data ***datap = datum;
2820
2821   **datap = &sigt->per_cu;
2822   ++*datap;
2823
2824   return 1;
2825 }
2826
2827 /* Create the hash table of all entries in the .debug_types section.
2828    The result is zero if there is an error (e.g. missing .debug_types section),
2829    otherwise non-zero.  */
2830
2831 static int
2832 create_debug_types_hash_table (struct objfile *objfile)
2833 {
2834   gdb_byte *info_ptr;
2835   htab_t types_htab;
2836   struct dwarf2_per_cu_data **iter;
2837
2838   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2839   info_ptr = dwarf2_per_objfile->types.buffer;
2840
2841   if (info_ptr == NULL)
2842     {
2843       dwarf2_per_objfile->signatured_types = NULL;
2844       return 0;
2845     }
2846
2847   types_htab = allocate_signatured_type_table (objfile);
2848
2849   if (dwarf2_die_debug)
2850     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2851
2852   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2853     {
2854       unsigned int offset;
2855       unsigned int offset_size;
2856       unsigned int type_offset;
2857       unsigned int length, initial_length_size;
2858       unsigned short version;
2859       ULONGEST signature;
2860       struct signatured_type *type_sig;
2861       void **slot;
2862       gdb_byte *ptr = info_ptr;
2863
2864       offset = ptr - dwarf2_per_objfile->types.buffer;
2865
2866       /* We need to read the type's signature in order to build the hash
2867          table, but we don't need to read anything else just yet.  */
2868
2869       /* Sanity check to ensure entire cu is present.  */
2870       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2871       if (ptr + length + initial_length_size
2872           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2873         {
2874           complaint (&symfile_complaints,
2875                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2876           break;
2877         }
2878
2879       offset_size = initial_length_size == 4 ? 4 : 8;
2880       ptr += initial_length_size;
2881       version = bfd_get_16 (objfile->obfd, ptr);
2882       ptr += 2;
2883       ptr += offset_size; /* abbrev offset */
2884       ptr += 1; /* address size */
2885       signature = bfd_get_64 (objfile->obfd, ptr);
2886       ptr += 8;
2887       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2888
2889       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2890       memset (type_sig, 0, sizeof (*type_sig));
2891       type_sig->signature = signature;
2892       type_sig->offset = offset;
2893       type_sig->type_offset = type_offset;
2894       type_sig->per_cu.objfile = objfile;
2895       type_sig->per_cu.from_debug_types = 1;
2896
2897       slot = htab_find_slot (types_htab, type_sig, INSERT);
2898       gdb_assert (slot != NULL);
2899       *slot = type_sig;
2900
2901       if (dwarf2_die_debug)
2902         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2903                             offset, phex (signature, sizeof (signature)));
2904
2905       info_ptr = info_ptr + initial_length_size + length;
2906     }
2907
2908   dwarf2_per_objfile->signatured_types = types_htab;
2909
2910   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2911   dwarf2_per_objfile->type_comp_units
2912     = obstack_alloc (&objfile->objfile_obstack,
2913                      dwarf2_per_objfile->n_type_comp_units
2914                      * sizeof (struct dwarf2_per_cu_data *));
2915   iter = &dwarf2_per_objfile->type_comp_units[0];
2916   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2917   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2918               == dwarf2_per_objfile->n_type_comp_units);
2919
2920   return 1;
2921 }
2922
2923 /* Lookup a signature based type.
2924    Returns NULL if SIG is not present in the table.  */
2925
2926 static struct signatured_type *
2927 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2928 {
2929   struct signatured_type find_entry, *entry;
2930
2931   if (dwarf2_per_objfile->signatured_types == NULL)
2932     {
2933       complaint (&symfile_complaints,
2934                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2935       return 0;
2936     }
2937
2938   find_entry.signature = sig;
2939   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2940   return entry;
2941 }
2942
2943 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2944
2945 static void
2946 init_cu_die_reader (struct die_reader_specs *reader,
2947                     struct dwarf2_cu *cu)
2948 {
2949   reader->abfd = cu->objfile->obfd;
2950   reader->cu = cu;
2951   if (cu->per_cu->from_debug_types)
2952     {
2953       gdb_assert (dwarf2_per_objfile->types.readin);
2954       reader->buffer = dwarf2_per_objfile->types.buffer;
2955     }
2956   else
2957     {
2958       gdb_assert (dwarf2_per_objfile->info.readin);
2959       reader->buffer = dwarf2_per_objfile->info.buffer;
2960     }
2961 }
2962
2963 /* Find the base address of the compilation unit for range lists and
2964    location lists.  It will normally be specified by DW_AT_low_pc.
2965    In DWARF-3 draft 4, the base address could be overridden by
2966    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2967    compilation units with discontinuous ranges.  */
2968
2969 static void
2970 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2971 {
2972   struct attribute *attr;
2973
2974   cu->base_known = 0;
2975   cu->base_address = 0;
2976
2977   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2978   if (attr)
2979     {
2980       cu->base_address = DW_ADDR (attr);
2981       cu->base_known = 1;
2982     }
2983   else
2984     {
2985       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2986       if (attr)
2987         {
2988           cu->base_address = DW_ADDR (attr);
2989           cu->base_known = 1;
2990         }
2991     }
2992 }
2993
2994 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2995    to combine the common parts.
2996    Process a compilation unit for a psymtab.
2997    BUFFER is a pointer to the beginning of the dwarf section buffer,
2998    either .debug_info or debug_types.
2999    INFO_PTR is a pointer to the start of the CU.
3000    Returns a pointer to the next CU.  */
3001
3002 static gdb_byte *
3003 process_psymtab_comp_unit (struct objfile *objfile,
3004                            struct dwarf2_per_cu_data *this_cu,
3005                            gdb_byte *buffer, gdb_byte *info_ptr,
3006                            unsigned int buffer_size)
3007 {
3008   bfd *abfd = objfile->obfd;
3009   gdb_byte *beg_of_comp_unit = info_ptr;
3010   struct die_info *comp_unit_die;
3011   struct partial_symtab *pst;
3012   CORE_ADDR baseaddr;
3013   struct cleanup *back_to_inner;
3014   struct dwarf2_cu cu;
3015   int has_children, has_pc_info;
3016   struct attribute *attr;
3017   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3018   struct die_reader_specs reader_specs;
3019
3020   memset (&cu, 0, sizeof (cu));
3021   cu.objfile = objfile;
3022   obstack_init (&cu.comp_unit_obstack);
3023
3024   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3025
3026   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3027                                           buffer, buffer_size,
3028                                           abfd);
3029
3030   /* Complete the cu_header.  */
3031   cu.header.offset = beg_of_comp_unit - buffer;
3032   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3033
3034   cu.list_in_scope = &file_symbols;
3035
3036   /* If this compilation unit was already read in, free the
3037      cached copy in order to read it in again.  This is
3038      necessary because we skipped some symbols when we first
3039      read in the compilation unit (see load_partial_dies).
3040      This problem could be avoided, but the benefit is
3041      unclear.  */
3042   if (this_cu->cu != NULL)
3043     free_one_cached_comp_unit (this_cu->cu);
3044
3045   /* Note that this is a pointer to our stack frame, being
3046      added to a global data structure.  It will be cleaned up
3047      in free_stack_comp_unit when we finish with this
3048      compilation unit.  */
3049   this_cu->cu = &cu;
3050   cu.per_cu = this_cu;
3051
3052   /* Read the abbrevs for this compilation unit into a table.  */
3053   dwarf2_read_abbrevs (abfd, &cu);
3054   make_cleanup (dwarf2_free_abbrev_table, &cu);
3055
3056   /* Read the compilation unit die.  */
3057   if (this_cu->from_debug_types)
3058     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3059   init_cu_die_reader (&reader_specs, &cu);
3060   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3061                             &has_children);
3062
3063   if (this_cu->from_debug_types)
3064     {
3065       /* offset,length haven't been set yet for type units.  */
3066       this_cu->offset = cu.header.offset;
3067       this_cu->length = cu.header.length + cu.header.initial_length_size;
3068     }
3069   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3070     {
3071       info_ptr = (beg_of_comp_unit + cu.header.length
3072                   + cu.header.initial_length_size);
3073       do_cleanups (back_to_inner);
3074       return info_ptr;
3075     }
3076
3077   /* Set the language we're debugging.  */
3078   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3079   if (attr)
3080     set_cu_language (DW_UNSND (attr), &cu);
3081   else
3082     set_cu_language (language_minimal, &cu);
3083
3084   /* Allocate a new partial symbol table structure.  */
3085   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3086   pst = start_psymtab_common (objfile, objfile->section_offsets,
3087                               (attr != NULL) ? DW_STRING (attr) : "",
3088                               /* TEXTLOW and TEXTHIGH are set below.  */
3089                               0,
3090                               objfile->global_psymbols.next,
3091                               objfile->static_psymbols.next);
3092
3093   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3094   if (attr != NULL)
3095     pst->dirname = DW_STRING (attr);
3096
3097   pst->read_symtab_private = this_cu;
3098
3099   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3100
3101   /* Store the function that reads in the rest of the symbol table */
3102   pst->read_symtab = dwarf2_psymtab_to_symtab;
3103
3104   this_cu->v.psymtab = pst;
3105
3106   dwarf2_find_base_address (comp_unit_die, &cu);
3107
3108   /* Possibly set the default values of LOWPC and HIGHPC from
3109      `DW_AT_ranges'.  */
3110   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3111                                       &best_highpc, &cu, pst);
3112   if (has_pc_info == 1 && best_lowpc < best_highpc)
3113     /* Store the contiguous range if it is not empty; it can be empty for
3114        CUs with no code.  */
3115     addrmap_set_empty (objfile->psymtabs_addrmap,
3116                        best_lowpc + baseaddr,
3117                        best_highpc + baseaddr - 1, pst);
3118
3119   /* Check if comp unit has_children.
3120      If so, read the rest of the partial symbols from this comp unit.
3121      If not, there's no more debug_info for this comp unit. */
3122   if (has_children)
3123     {
3124       struct partial_die_info *first_die;
3125       CORE_ADDR lowpc, highpc;
3126
3127       lowpc = ((CORE_ADDR) -1);
3128       highpc = ((CORE_ADDR) 0);
3129
3130       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3131
3132       scan_partial_symbols (first_die, &lowpc, &highpc,
3133                             ! has_pc_info, &cu);
3134
3135       /* If we didn't find a lowpc, set it to highpc to avoid
3136          complaints from `maint check'.  */
3137       if (lowpc == ((CORE_ADDR) -1))
3138         lowpc = highpc;
3139
3140       /* If the compilation unit didn't have an explicit address range,
3141          then use the information extracted from its child dies.  */
3142       if (! has_pc_info)
3143         {
3144           best_lowpc = lowpc;
3145           best_highpc = highpc;
3146         }
3147     }
3148   pst->textlow = best_lowpc + baseaddr;
3149   pst->texthigh = best_highpc + baseaddr;
3150
3151   pst->n_global_syms = objfile->global_psymbols.next -
3152     (objfile->global_psymbols.list + pst->globals_offset);
3153   pst->n_static_syms = objfile->static_psymbols.next -
3154     (objfile->static_psymbols.list + pst->statics_offset);
3155   sort_pst_symbols (pst);
3156
3157   info_ptr = (beg_of_comp_unit + cu.header.length
3158               + cu.header.initial_length_size);
3159
3160   if (this_cu->from_debug_types)
3161     {
3162       /* It's not clear we want to do anything with stmt lists here.
3163          Waiting to see what gcc ultimately does.  */
3164     }
3165   else
3166     {
3167       /* Get the list of files included in the current compilation unit,
3168          and build a psymtab for each of them.  */
3169       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3170     }
3171
3172   do_cleanups (back_to_inner);
3173
3174   return info_ptr;
3175 }
3176
3177 /* Traversal function for htab_traverse_noresize.
3178    Process one .debug_types comp-unit.  */
3179
3180 static int
3181 process_type_comp_unit (void **slot, void *info)
3182 {
3183   struct signatured_type *entry = (struct signatured_type *) *slot;
3184   struct objfile *objfile = (struct objfile *) info;
3185   struct dwarf2_per_cu_data *this_cu;
3186
3187   this_cu = &entry->per_cu;
3188
3189   gdb_assert (dwarf2_per_objfile->types.readin);
3190   process_psymtab_comp_unit (objfile, this_cu,
3191                              dwarf2_per_objfile->types.buffer,
3192                              dwarf2_per_objfile->types.buffer + entry->offset,
3193                              dwarf2_per_objfile->types.size);
3194
3195   return 1;
3196 }
3197
3198 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3199    Build partial symbol tables for the .debug_types comp-units.  */
3200
3201 static void
3202 build_type_psymtabs (struct objfile *objfile)
3203 {
3204   if (! create_debug_types_hash_table (objfile))
3205     return;
3206
3207   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3208                           process_type_comp_unit, objfile);
3209 }
3210
3211 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3212
3213 static void
3214 psymtabs_addrmap_cleanup (void *o)
3215 {
3216   struct objfile *objfile = o;
3217
3218   objfile->psymtabs_addrmap = NULL;
3219 }
3220
3221 /* Build the partial symbol table by doing a quick pass through the
3222    .debug_info and .debug_abbrev sections.  */
3223
3224 static void
3225 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3226 {
3227   gdb_byte *info_ptr;
3228   struct cleanup *back_to, *addrmap_cleanup;
3229   struct obstack temp_obstack;
3230
3231   dwarf2_per_objfile->reading_partial_symbols = 1;
3232
3233   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3234   info_ptr = dwarf2_per_objfile->info.buffer;
3235
3236   /* Any cached compilation units will be linked by the per-objfile
3237      read_in_chain.  Make sure to free them when we're done.  */
3238   back_to = make_cleanup (free_cached_comp_units, NULL);
3239
3240   build_type_psymtabs (objfile);
3241
3242   create_all_comp_units (objfile);
3243
3244   /* Create a temporary address map on a temporary obstack.  We later
3245      copy this to the final obstack.  */
3246   obstack_init (&temp_obstack);
3247   make_cleanup_obstack_free (&temp_obstack);
3248   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3249   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3250
3251   /* Since the objects we're extracting from .debug_info vary in
3252      length, only the individual functions to extract them (like
3253      read_comp_unit_head and load_partial_die) can really know whether
3254      the buffer is large enough to hold another complete object.
3255
3256      At the moment, they don't actually check that.  If .debug_info
3257      holds just one extra byte after the last compilation unit's dies,
3258      then read_comp_unit_head will happily read off the end of the
3259      buffer.  read_partial_die is similarly casual.  Those functions
3260      should be fixed.
3261
3262      For this loop condition, simply checking whether there's any data
3263      left at all should be sufficient.  */
3264
3265   while (info_ptr < (dwarf2_per_objfile->info.buffer
3266                      + dwarf2_per_objfile->info.size))
3267     {
3268       struct dwarf2_per_cu_data *this_cu;
3269
3270       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3271                                        objfile);
3272
3273       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3274                                             dwarf2_per_objfile->info.buffer,
3275                                             info_ptr,
3276                                             dwarf2_per_objfile->info.size);
3277     }
3278
3279   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3280                                                     &objfile->objfile_obstack);
3281   discard_cleanups (addrmap_cleanup);
3282
3283   do_cleanups (back_to);
3284 }
3285
3286 /* Load the partial DIEs for a secondary CU into memory.  */
3287
3288 static void
3289 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3290                         struct objfile *objfile)
3291 {
3292   bfd *abfd = objfile->obfd;
3293   gdb_byte *info_ptr, *beg_of_comp_unit;
3294   struct die_info *comp_unit_die;
3295   struct dwarf2_cu *cu;
3296   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3297   struct attribute *attr;
3298   int has_children;
3299   struct die_reader_specs reader_specs;
3300   int read_cu = 0;
3301
3302   gdb_assert (! this_cu->from_debug_types);
3303
3304   gdb_assert (dwarf2_per_objfile->info.readin);
3305   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3306   beg_of_comp_unit = info_ptr;
3307
3308   if (this_cu->cu == NULL)
3309     {
3310       cu = alloc_one_comp_unit (objfile);
3311
3312       read_cu = 1;
3313
3314       /* If an error occurs while loading, release our storage.  */
3315       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3316
3317       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3318                                               dwarf2_per_objfile->info.buffer,
3319                                               dwarf2_per_objfile->info.size,
3320                                               abfd);
3321
3322       /* Complete the cu_header.  */
3323       cu->header.offset = this_cu->offset;
3324       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3325
3326       /* Link this compilation unit into the compilation unit tree.  */
3327       this_cu->cu = cu;
3328       cu->per_cu = this_cu;
3329
3330       /* Link this CU into read_in_chain.  */
3331       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3332       dwarf2_per_objfile->read_in_chain = this_cu;
3333     }
3334   else
3335     {
3336       cu = this_cu->cu;
3337       info_ptr += cu->header.first_die_offset;
3338     }
3339
3340   /* Read the abbrevs for this compilation unit into a table.  */
3341   gdb_assert (cu->dwarf2_abbrevs == NULL);
3342   dwarf2_read_abbrevs (abfd, cu);
3343   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3344
3345   /* Read the compilation unit die.  */
3346   init_cu_die_reader (&reader_specs, cu);
3347   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3348                             &has_children);
3349
3350   /* Set the language we're debugging.  */
3351   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3352   if (attr)
3353     set_cu_language (DW_UNSND (attr), cu);
3354   else
3355     set_cu_language (language_minimal, cu);
3356
3357   /* Check if comp unit has_children.
3358      If so, read the rest of the partial symbols from this comp unit.
3359      If not, there's no more debug_info for this comp unit. */
3360   if (has_children)
3361     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3362
3363   do_cleanups (free_abbrevs_cleanup);
3364
3365   if (read_cu)
3366     {
3367       /* We've successfully allocated this compilation unit.  Let our
3368          caller clean it up when finished with it.  */
3369       discard_cleanups (free_cu_cleanup);
3370     }
3371 }
3372
3373 /* Create a list of all compilation units in OBJFILE.  We do this only
3374    if an inter-comp-unit reference is found; presumably if there is one,
3375    there will be many, and one will occur early in the .debug_info section.
3376    So there's no point in building this list incrementally.  */
3377
3378 static void
3379 create_all_comp_units (struct objfile *objfile)
3380 {
3381   int n_allocated;
3382   int n_comp_units;
3383   struct dwarf2_per_cu_data **all_comp_units;
3384   gdb_byte *info_ptr;
3385
3386   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3387   info_ptr = dwarf2_per_objfile->info.buffer;
3388
3389   n_comp_units = 0;
3390   n_allocated = 10;
3391   all_comp_units = xmalloc (n_allocated
3392                             * sizeof (struct dwarf2_per_cu_data *));
3393
3394   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3395     {
3396       unsigned int length, initial_length_size;
3397       struct dwarf2_per_cu_data *this_cu;
3398       unsigned int offset;
3399
3400       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3401
3402       /* Read just enough information to find out where the next
3403          compilation unit is.  */
3404       length = read_initial_length (objfile->obfd, info_ptr,
3405                                     &initial_length_size);
3406
3407       /* Save the compilation unit for later lookup.  */
3408       this_cu = obstack_alloc (&objfile->objfile_obstack,
3409                                sizeof (struct dwarf2_per_cu_data));
3410       memset (this_cu, 0, sizeof (*this_cu));
3411       this_cu->offset = offset;
3412       this_cu->length = length + initial_length_size;
3413       this_cu->objfile = objfile;
3414
3415       if (n_comp_units == n_allocated)
3416         {
3417           n_allocated *= 2;
3418           all_comp_units = xrealloc (all_comp_units,
3419                                      n_allocated
3420                                      * sizeof (struct dwarf2_per_cu_data *));
3421         }
3422       all_comp_units[n_comp_units++] = this_cu;
3423
3424       info_ptr = info_ptr + this_cu->length;
3425     }
3426
3427   dwarf2_per_objfile->all_comp_units
3428     = obstack_alloc (&objfile->objfile_obstack,
3429                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3430   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3431           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3432   xfree (all_comp_units);
3433   dwarf2_per_objfile->n_comp_units = n_comp_units;
3434 }
3435
3436 /* Process all loaded DIEs for compilation unit CU, starting at
3437    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3438    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3439    DW_AT_ranges).  If NEED_PC is set, then this function will set
3440    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3441    and record the covered ranges in the addrmap.  */
3442
3443 static void
3444 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3445                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3446 {
3447   struct partial_die_info *pdi;
3448
3449   /* Now, march along the PDI's, descending into ones which have
3450      interesting children but skipping the children of the other ones,
3451      until we reach the end of the compilation unit.  */
3452
3453   pdi = first_die;
3454
3455   while (pdi != NULL)
3456     {
3457       fixup_partial_die (pdi, cu);
3458
3459       /* Anonymous namespaces or modules have no name but have interesting
3460          children, so we need to look at them.  Ditto for anonymous
3461          enums.  */
3462
3463       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3464           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3465         {
3466           switch (pdi->tag)
3467             {
3468             case DW_TAG_subprogram:
3469               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3470               break;
3471             case DW_TAG_variable:
3472             case DW_TAG_typedef:
3473             case DW_TAG_union_type:
3474               if (!pdi->is_declaration)
3475                 {
3476                   add_partial_symbol (pdi, cu);
3477                 }
3478               break;
3479             case DW_TAG_class_type:
3480             case DW_TAG_interface_type:
3481             case DW_TAG_structure_type:
3482               if (!pdi->is_declaration)
3483                 {
3484                   add_partial_symbol (pdi, cu);
3485                 }
3486               break;
3487             case DW_TAG_enumeration_type:
3488               if (!pdi->is_declaration)
3489                 add_partial_enumeration (pdi, cu);
3490               break;
3491             case DW_TAG_base_type:
3492             case DW_TAG_subrange_type:
3493               /* File scope base type definitions are added to the partial
3494                  symbol table.  */
3495               add_partial_symbol (pdi, cu);
3496               break;
3497             case DW_TAG_namespace:
3498               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3499               break;
3500             case DW_TAG_module:
3501               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3502               break;
3503             default:
3504               break;
3505             }
3506         }
3507
3508       /* If the die has a sibling, skip to the sibling.  */
3509
3510       pdi = pdi->die_sibling;
3511     }
3512 }
3513
3514 /* Functions used to compute the fully scoped name of a partial DIE.
3515
3516    Normally, this is simple.  For C++, the parent DIE's fully scoped
3517    name is concatenated with "::" and the partial DIE's name.  For
3518    Java, the same thing occurs except that "." is used instead of "::".
3519    Enumerators are an exception; they use the scope of their parent
3520    enumeration type, i.e. the name of the enumeration type is not
3521    prepended to the enumerator.
3522
3523    There are two complexities.  One is DW_AT_specification; in this
3524    case "parent" means the parent of the target of the specification,
3525    instead of the direct parent of the DIE.  The other is compilers
3526    which do not emit DW_TAG_namespace; in this case we try to guess
3527    the fully qualified name of structure types from their members'
3528    linkage names.  This must be done using the DIE's children rather
3529    than the children of any DW_AT_specification target.  We only need
3530    to do this for structures at the top level, i.e. if the target of
3531    any DW_AT_specification (if any; otherwise the DIE itself) does not
3532    have a parent.  */
3533
3534 /* Compute the scope prefix associated with PDI's parent, in
3535    compilation unit CU.  The result will be allocated on CU's
3536    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3537    field.  NULL is returned if no prefix is necessary.  */
3538 static char *
3539 partial_die_parent_scope (struct partial_die_info *pdi,
3540                           struct dwarf2_cu *cu)
3541 {
3542   char *grandparent_scope;
3543   struct partial_die_info *parent, *real_pdi;
3544
3545   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3546      then this means the parent of the specification DIE.  */
3547
3548   real_pdi = pdi;
3549   while (real_pdi->has_specification)
3550     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3551
3552   parent = real_pdi->die_parent;
3553   if (parent == NULL)
3554     return NULL;
3555
3556   if (parent->scope_set)
3557     return parent->scope;
3558
3559   fixup_partial_die (parent, cu);
3560
3561   grandparent_scope = partial_die_parent_scope (parent, cu);
3562
3563   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3564      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3565      Work around this problem here.  */
3566   if (cu->language == language_cplus
3567       && parent->tag == DW_TAG_namespace
3568       && strcmp (parent->name, "::") == 0
3569       && grandparent_scope == NULL)
3570     {
3571       parent->scope = NULL;
3572       parent->scope_set = 1;
3573       return NULL;
3574     }
3575
3576   if (parent->tag == DW_TAG_namespace
3577       || parent->tag == DW_TAG_module
3578       || parent->tag == DW_TAG_structure_type
3579       || parent->tag == DW_TAG_class_type
3580       || parent->tag == DW_TAG_interface_type
3581       || parent->tag == DW_TAG_union_type
3582       || parent->tag == DW_TAG_enumeration_type)
3583     {
3584       if (grandparent_scope == NULL)
3585         parent->scope = parent->name;
3586       else
3587         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3588                                          parent->name, 0, cu);
3589     }
3590   else if (parent->tag == DW_TAG_enumerator)
3591     /* Enumerators should not get the name of the enumeration as a prefix.  */
3592     parent->scope = grandparent_scope;
3593   else
3594     {
3595       /* FIXME drow/2004-04-01: What should we be doing with
3596          function-local names?  For partial symbols, we should probably be
3597          ignoring them.  */
3598       complaint (&symfile_complaints,
3599                  _("unhandled containing DIE tag %d for DIE at %d"),
3600                  parent->tag, pdi->offset);
3601       parent->scope = grandparent_scope;
3602     }
3603
3604   parent->scope_set = 1;
3605   return parent->scope;
3606 }
3607
3608 /* Return the fully scoped name associated with PDI, from compilation unit
3609    CU.  The result will be allocated with malloc.  */
3610 static char *
3611 partial_die_full_name (struct partial_die_info *pdi,
3612                        struct dwarf2_cu *cu)
3613 {
3614   char *parent_scope;
3615
3616   /* If this is a template instantiation, we can not work out the
3617      template arguments from partial DIEs.  So, unfortunately, we have
3618      to go through the full DIEs.  At least any work we do building
3619      types here will be reused if full symbols are loaded later.  */
3620   if (pdi->has_template_arguments)
3621     {
3622       fixup_partial_die (pdi, cu);
3623
3624       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3625         {
3626           struct die_info *die;
3627           struct attribute attr;
3628           struct dwarf2_cu *ref_cu = cu;
3629
3630           attr.name = 0;
3631           attr.form = DW_FORM_ref_addr;
3632           attr.u.addr = pdi->offset;
3633           die = follow_die_ref (NULL, &attr, &ref_cu);
3634
3635           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3636         }
3637     }
3638
3639   parent_scope = partial_die_parent_scope (pdi, cu);
3640   if (parent_scope == NULL)
3641     return NULL;
3642   else
3643     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3644 }
3645
3646 static void
3647 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3648 {
3649   struct objfile *objfile = cu->objfile;
3650   CORE_ADDR addr = 0;
3651   char *actual_name = NULL;
3652   const struct partial_symbol *psym = NULL;
3653   CORE_ADDR baseaddr;
3654   int built_actual_name = 0;
3655
3656   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3657
3658   actual_name = partial_die_full_name (pdi, cu);
3659   if (actual_name)
3660     built_actual_name = 1;
3661
3662   if (actual_name == NULL)
3663     actual_name = pdi->name;
3664
3665   switch (pdi->tag)
3666     {
3667     case DW_TAG_subprogram:
3668       if (pdi->is_external || cu->language == language_ada)
3669         {
3670           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3671              of the global scope.  But in Ada, we want to be able to access
3672              nested procedures globally.  So all Ada subprograms are stored
3673              in the global scope.  */
3674           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3675              mst_text, objfile); */
3676           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3677                                       built_actual_name,
3678                                       VAR_DOMAIN, LOC_BLOCK,
3679                                       &objfile->global_psymbols,
3680                                       0, pdi->lowpc + baseaddr,
3681                                       cu->language, objfile);
3682         }
3683       else
3684         {
3685           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3686              mst_file_text, objfile); */
3687           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3688                                       built_actual_name,
3689                                       VAR_DOMAIN, LOC_BLOCK,
3690                                       &objfile->static_psymbols,
3691                                       0, pdi->lowpc + baseaddr,
3692                                       cu->language, objfile);
3693         }
3694       break;
3695     case DW_TAG_variable:
3696       if (pdi->locdesc)
3697         addr = decode_locdesc (pdi->locdesc, cu);
3698
3699       if (pdi->locdesc
3700           && addr == 0
3701           && !dwarf2_per_objfile->has_section_at_zero)
3702         {
3703           /* A global or static variable may also have been stripped
3704              out by the linker if unused, in which case its address
3705              will be nullified; do not add such variables into partial
3706              symbol table then.  */
3707         }
3708       else if (pdi->is_external)
3709         {
3710           /* Global Variable.
3711              Don't enter into the minimal symbol tables as there is
3712              a minimal symbol table entry from the ELF symbols already.
3713              Enter into partial symbol table if it has a location
3714              descriptor or a type.
3715              If the location descriptor is missing, new_symbol will create
3716              a LOC_UNRESOLVED symbol, the address of the variable will then
3717              be determined from the minimal symbol table whenever the variable
3718              is referenced.
3719              The address for the partial symbol table entry is not
3720              used by GDB, but it comes in handy for debugging partial symbol
3721              table building.  */
3722
3723           if (pdi->locdesc || pdi->has_type)
3724             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3725                                         built_actual_name,
3726                                         VAR_DOMAIN, LOC_STATIC,
3727                                         &objfile->global_psymbols,
3728                                         0, addr + baseaddr,
3729                                         cu->language, objfile);
3730         }
3731       else
3732         {
3733           /* Static Variable. Skip symbols without location descriptors.  */
3734           if (pdi->locdesc == NULL)
3735             {
3736               if (built_actual_name)
3737                 xfree (actual_name);
3738               return;
3739             }
3740           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3741              mst_file_data, objfile); */
3742           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3743                                       built_actual_name,
3744                                       VAR_DOMAIN, LOC_STATIC,
3745                                       &objfile->static_psymbols,
3746                                       0, addr + baseaddr,
3747                                       cu->language, objfile);
3748         }
3749       break;
3750     case DW_TAG_typedef:
3751     case DW_TAG_base_type:
3752     case DW_TAG_subrange_type:
3753       add_psymbol_to_list (actual_name, strlen (actual_name),
3754                            built_actual_name,
3755                            VAR_DOMAIN, LOC_TYPEDEF,
3756                            &objfile->static_psymbols,
3757                            0, (CORE_ADDR) 0, cu->language, objfile);
3758       break;
3759     case DW_TAG_namespace:
3760       add_psymbol_to_list (actual_name, strlen (actual_name),
3761                            built_actual_name,
3762                            VAR_DOMAIN, LOC_TYPEDEF,
3763                            &objfile->global_psymbols,
3764                            0, (CORE_ADDR) 0, cu->language, objfile);
3765       break;
3766     case DW_TAG_class_type:
3767     case DW_TAG_interface_type:
3768     case DW_TAG_structure_type:
3769     case DW_TAG_union_type:
3770     case DW_TAG_enumeration_type:
3771       /* Skip external references.  The DWARF standard says in the section
3772          about "Structure, Union, and Class Type Entries": "An incomplete
3773          structure, union or class type is represented by a structure,
3774          union or class entry that does not have a byte size attribute
3775          and that has a DW_AT_declaration attribute."  */
3776       if (!pdi->has_byte_size && pdi->is_declaration)
3777         {
3778           if (built_actual_name)
3779             xfree (actual_name);
3780           return;
3781         }
3782
3783       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3784          static vs. global.  */
3785       add_psymbol_to_list (actual_name, strlen (actual_name),
3786                            built_actual_name,
3787                            STRUCT_DOMAIN, LOC_TYPEDEF,
3788                            (cu->language == language_cplus
3789                             || cu->language == language_java)
3790                            ? &objfile->global_psymbols
3791                            : &objfile->static_psymbols,
3792                            0, (CORE_ADDR) 0, cu->language, objfile);
3793
3794       break;
3795     case DW_TAG_enumerator:
3796       add_psymbol_to_list (actual_name, strlen (actual_name),
3797                            built_actual_name,
3798                            VAR_DOMAIN, LOC_CONST,
3799                            (cu->language == language_cplus
3800                             || cu->language == language_java)
3801                            ? &objfile->global_psymbols
3802                            : &objfile->static_psymbols,
3803                            0, (CORE_ADDR) 0, cu->language, objfile);
3804       break;
3805     default:
3806       break;
3807     }
3808
3809   if (built_actual_name)
3810     xfree (actual_name);
3811 }
3812
3813 /* Read a partial die corresponding to a namespace; also, add a symbol
3814    corresponding to that namespace to the symbol table.  NAMESPACE is
3815    the name of the enclosing namespace.  */
3816
3817 static void
3818 add_partial_namespace (struct partial_die_info *pdi,
3819                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3820                        int need_pc, struct dwarf2_cu *cu)
3821 {
3822   /* Add a symbol for the namespace.  */
3823
3824   add_partial_symbol (pdi, cu);
3825
3826   /* Now scan partial symbols in that namespace.  */
3827
3828   if (pdi->has_children)
3829     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3830 }
3831
3832 /* Read a partial die corresponding to a Fortran module.  */
3833
3834 static void
3835 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3836                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3837 {
3838   /* Now scan partial symbols in that module.  */
3839
3840   if (pdi->has_children)
3841     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3842 }
3843
3844 /* Read a partial die corresponding to a subprogram and create a partial
3845    symbol for that subprogram.  When the CU language allows it, this
3846    routine also defines a partial symbol for each nested subprogram
3847    that this subprogram contains.
3848
3849    DIE my also be a lexical block, in which case we simply search
3850    recursively for suprograms defined inside that lexical block.
3851    Again, this is only performed when the CU language allows this
3852    type of definitions.  */
3853
3854 static void
3855 add_partial_subprogram (struct partial_die_info *pdi,
3856                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3857                         int need_pc, struct dwarf2_cu *cu)
3858 {
3859   if (pdi->tag == DW_TAG_subprogram)
3860     {
3861       if (pdi->has_pc_info)
3862         {
3863           if (pdi->lowpc < *lowpc)
3864             *lowpc = pdi->lowpc;
3865           if (pdi->highpc > *highpc)
3866             *highpc = pdi->highpc;
3867           if (need_pc)
3868             {
3869               CORE_ADDR baseaddr;
3870               struct objfile *objfile = cu->objfile;
3871
3872               baseaddr = ANOFFSET (objfile->section_offsets,
3873                                    SECT_OFF_TEXT (objfile));
3874               addrmap_set_empty (objfile->psymtabs_addrmap,
3875                                  pdi->lowpc + baseaddr,
3876                                  pdi->highpc - 1 + baseaddr,
3877                                  cu->per_cu->v.psymtab);
3878             }
3879           if (!pdi->is_declaration)
3880             /* Ignore subprogram DIEs that do not have a name, they are
3881                illegal.  Do not emit a complaint at this point, we will
3882                do so when we convert this psymtab into a symtab.  */
3883             if (pdi->name)
3884               add_partial_symbol (pdi, cu);
3885         }
3886     }
3887
3888   if (! pdi->has_children)
3889     return;
3890
3891   if (cu->language == language_ada)
3892     {
3893       pdi = pdi->die_child;
3894       while (pdi != NULL)
3895         {
3896           fixup_partial_die (pdi, cu);
3897           if (pdi->tag == DW_TAG_subprogram
3898               || pdi->tag == DW_TAG_lexical_block)
3899             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3900           pdi = pdi->die_sibling;
3901         }
3902     }
3903 }
3904
3905 /* See if we can figure out if the class lives in a namespace.  We do
3906    this by looking for a member function; its demangled name will
3907    contain namespace info, if there is any.  */
3908
3909 static void
3910 guess_structure_name (struct partial_die_info *struct_pdi,
3911                       struct dwarf2_cu *cu)
3912 {
3913   if ((cu->language == language_cplus
3914        || cu->language == language_java)
3915       && cu->has_namespace_info == 0
3916       && struct_pdi->has_children)
3917     {
3918       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3919          what template types look like, because the demangler
3920          frequently doesn't give the same name as the debug info.  We
3921          could fix this by only using the demangled name to get the
3922          prefix (but see comment in read_structure_type).  */
3923
3924       struct partial_die_info *real_pdi;
3925
3926       /* If this DIE (this DIE's specification, if any) has a parent, then
3927          we should not do this.  We'll prepend the parent's fully qualified
3928          name when we create the partial symbol.  */
3929
3930       real_pdi = struct_pdi;
3931       while (real_pdi->has_specification)
3932         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3933
3934       if (real_pdi->die_parent != NULL)
3935         return;
3936     }
3937 }
3938
3939 /* Read a partial die corresponding to an enumeration type.  */
3940
3941 static void
3942 add_partial_enumeration (struct partial_die_info *enum_pdi,
3943                          struct dwarf2_cu *cu)
3944 {
3945   struct partial_die_info *pdi;
3946
3947   if (enum_pdi->name != NULL)
3948     add_partial_symbol (enum_pdi, cu);
3949
3950   pdi = enum_pdi->die_child;
3951   while (pdi)
3952     {
3953       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3954         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3955       else
3956         add_partial_symbol (pdi, cu);
3957       pdi = pdi->die_sibling;
3958     }
3959 }
3960
3961 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3962    Return the corresponding abbrev, or NULL if the number is zero (indicating
3963    an empty DIE).  In either case *BYTES_READ will be set to the length of
3964    the initial number.  */
3965
3966 static struct abbrev_info *
3967 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3968                  struct dwarf2_cu *cu)
3969 {
3970   bfd *abfd = cu->objfile->obfd;
3971   unsigned int abbrev_number;
3972   struct abbrev_info *abbrev;
3973
3974   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3975
3976   if (abbrev_number == 0)
3977     return NULL;
3978
3979   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3980   if (!abbrev)
3981     {
3982       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3983                       bfd_get_filename (abfd));
3984     }
3985
3986   return abbrev;
3987 }
3988
3989 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3990    Returns a pointer to the end of a series of DIEs, terminated by an empty
3991    DIE.  Any children of the skipped DIEs will also be skipped.  */
3992
3993 static gdb_byte *
3994 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3995 {
3996   struct abbrev_info *abbrev;
3997   unsigned int bytes_read;
3998
3999   while (1)
4000     {
4001       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4002       if (abbrev == NULL)
4003         return info_ptr + bytes_read;
4004       else
4005         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4006     }
4007 }
4008
4009 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4010    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4011    abbrev corresponding to that skipped uleb128 should be passed in
4012    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4013    children.  */
4014
4015 static gdb_byte *
4016 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4017               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4018 {
4019   unsigned int bytes_read;
4020   struct attribute attr;
4021   bfd *abfd = cu->objfile->obfd;
4022   unsigned int form, i;
4023
4024   for (i = 0; i < abbrev->num_attrs; i++)
4025     {
4026       /* The only abbrev we care about is DW_AT_sibling.  */
4027       if (abbrev->attrs[i].name == DW_AT_sibling)
4028         {
4029           read_attribute (&attr, &abbrev->attrs[i],
4030                           abfd, info_ptr, cu);
4031           if (attr.form == DW_FORM_ref_addr)
4032             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4033           else
4034             return buffer + dwarf2_get_ref_die_offset (&attr);
4035         }
4036
4037       /* If it isn't DW_AT_sibling, skip this attribute.  */
4038       form = abbrev->attrs[i].form;
4039     skip_attribute:
4040       switch (form)
4041         {
4042         case DW_FORM_ref_addr:
4043           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4044              and later it is offset sized.  */
4045           if (cu->header.version == 2)
4046             info_ptr += cu->header.addr_size;
4047           else
4048             info_ptr += cu->header.offset_size;
4049           break;
4050         case DW_FORM_addr:
4051           info_ptr += cu->header.addr_size;
4052           break;
4053         case DW_FORM_data1:
4054         case DW_FORM_ref1:
4055         case DW_FORM_flag:
4056           info_ptr += 1;
4057           break;
4058         case DW_FORM_flag_present:
4059           break;
4060         case DW_FORM_data2:
4061         case DW_FORM_ref2:
4062           info_ptr += 2;
4063           break;
4064         case DW_FORM_data4:
4065         case DW_FORM_ref4:
4066           info_ptr += 4;
4067           break;
4068         case DW_FORM_data8:
4069         case DW_FORM_ref8:
4070         case DW_FORM_sig8:
4071           info_ptr += 8;
4072           break;
4073         case DW_FORM_string:
4074           read_direct_string (abfd, info_ptr, &bytes_read);
4075           info_ptr += bytes_read;
4076           break;
4077         case DW_FORM_sec_offset:
4078         case DW_FORM_strp:
4079           info_ptr += cu->header.offset_size;
4080           break;
4081         case DW_FORM_exprloc:
4082         case DW_FORM_block:
4083           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4084           info_ptr += bytes_read;
4085           break;
4086         case DW_FORM_block1:
4087           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4088           break;
4089         case DW_FORM_block2:
4090           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4091           break;
4092         case DW_FORM_block4:
4093           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4094           break;
4095         case DW_FORM_sdata:
4096         case DW_FORM_udata:
4097         case DW_FORM_ref_udata:
4098           info_ptr = skip_leb128 (abfd, info_ptr);
4099           break;
4100         case DW_FORM_indirect:
4101           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4102           info_ptr += bytes_read;
4103           /* We need to continue parsing from here, so just go back to
4104              the top.  */
4105           goto skip_attribute;
4106
4107         default:
4108           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4109                  dwarf_form_name (form),
4110                  bfd_get_filename (abfd));
4111         }
4112     }
4113
4114   if (abbrev->has_children)
4115     return skip_children (buffer, info_ptr, cu);
4116   else
4117     return info_ptr;
4118 }
4119
4120 /* Locate ORIG_PDI's sibling.
4121    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4122    in BUFFER.  */
4123
4124 static gdb_byte *
4125 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4126                     gdb_byte *buffer, gdb_byte *info_ptr,
4127                     bfd *abfd, struct dwarf2_cu *cu)
4128 {
4129   /* Do we know the sibling already?  */
4130
4131   if (orig_pdi->sibling)
4132     return orig_pdi->sibling;
4133
4134   /* Are there any children to deal with?  */
4135
4136   if (!orig_pdi->has_children)
4137     return info_ptr;
4138
4139   /* Skip the children the long way.  */
4140
4141   return skip_children (buffer, info_ptr, cu);
4142 }
4143
4144 /* Expand this partial symbol table into a full symbol table.  */
4145
4146 static void
4147 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4148 {
4149   if (pst != NULL)
4150     {
4151       if (pst->readin)
4152         {
4153           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4154         }
4155       else
4156         {
4157           if (info_verbose)
4158             {
4159               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4160               gdb_flush (gdb_stdout);
4161             }
4162
4163           /* Restore our global data.  */
4164           dwarf2_per_objfile = objfile_data (pst->objfile,
4165                                              dwarf2_objfile_data_key);
4166
4167           /* If this psymtab is constructed from a debug-only objfile, the
4168              has_section_at_zero flag will not necessarily be correct.  We
4169              can get the correct value for this flag by looking at the data
4170              associated with the (presumably stripped) associated objfile.  */
4171           if (pst->objfile->separate_debug_objfile_backlink)
4172             {
4173               struct dwarf2_per_objfile *dpo_backlink
4174                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4175                                 dwarf2_objfile_data_key);
4176
4177               dwarf2_per_objfile->has_section_at_zero
4178                 = dpo_backlink->has_section_at_zero;
4179             }
4180
4181           dwarf2_per_objfile->reading_partial_symbols = 0;
4182
4183           psymtab_to_symtab_1 (pst);
4184
4185           /* Finish up the debug error message.  */
4186           if (info_verbose)
4187             printf_filtered (_("done.\n"));
4188         }
4189     }
4190 }
4191
4192 /* Add PER_CU to the queue.  */
4193
4194 static void
4195 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4196 {
4197   struct dwarf2_queue_item *item;
4198
4199   per_cu->queued = 1;
4200   item = xmalloc (sizeof (*item));
4201   item->per_cu = per_cu;
4202   item->next = NULL;
4203
4204   if (dwarf2_queue == NULL)
4205     dwarf2_queue = item;
4206   else
4207     dwarf2_queue_tail->next = item;
4208
4209   dwarf2_queue_tail = item;
4210 }
4211
4212 /* Process the queue.  */
4213
4214 static void
4215 process_queue (struct objfile *objfile)
4216 {
4217   struct dwarf2_queue_item *item, *next_item;
4218
4219   /* The queue starts out with one item, but following a DIE reference
4220      may load a new CU, adding it to the end of the queue.  */
4221   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4222     {
4223       if (dwarf2_per_objfile->using_index
4224           ? !item->per_cu->v.quick->symtab
4225           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4226         process_full_comp_unit (item->per_cu);
4227
4228       item->per_cu->queued = 0;
4229       next_item = item->next;
4230       xfree (item);
4231     }
4232
4233   dwarf2_queue_tail = NULL;
4234 }
4235
4236 /* Free all allocated queue entries.  This function only releases anything if
4237    an error was thrown; if the queue was processed then it would have been
4238    freed as we went along.  */
4239
4240 static void
4241 dwarf2_release_queue (void *dummy)
4242 {
4243   struct dwarf2_queue_item *item, *last;
4244
4245   item = dwarf2_queue;
4246   while (item)
4247     {
4248       /* Anything still marked queued is likely to be in an
4249          inconsistent state, so discard it.  */
4250       if (item->per_cu->queued)
4251         {
4252           if (item->per_cu->cu != NULL)
4253             free_one_cached_comp_unit (item->per_cu->cu);
4254           item->per_cu->queued = 0;
4255         }
4256
4257       last = item;
4258       item = item->next;
4259       xfree (last);
4260     }
4261
4262   dwarf2_queue = dwarf2_queue_tail = NULL;
4263 }
4264
4265 /* Read in full symbols for PST, and anything it depends on.  */
4266
4267 static void
4268 psymtab_to_symtab_1 (struct partial_symtab *pst)
4269 {
4270   struct dwarf2_per_cu_data *per_cu;
4271   struct cleanup *back_to;
4272   int i;
4273
4274   for (i = 0; i < pst->number_of_dependencies; i++)
4275     if (!pst->dependencies[i]->readin)
4276       {
4277         /* Inform about additional files that need to be read in.  */
4278         if (info_verbose)
4279           {
4280             /* FIXME: i18n: Need to make this a single string.  */
4281             fputs_filtered (" ", gdb_stdout);
4282             wrap_here ("");
4283             fputs_filtered ("and ", gdb_stdout);
4284             wrap_here ("");
4285             printf_filtered ("%s...", pst->dependencies[i]->filename);
4286             wrap_here ("");     /* Flush output */
4287             gdb_flush (gdb_stdout);
4288           }
4289         psymtab_to_symtab_1 (pst->dependencies[i]);
4290       }
4291
4292   per_cu = pst->read_symtab_private;
4293
4294   if (per_cu == NULL)
4295     {
4296       /* It's an include file, no symbols to read for it.
4297          Everything is in the parent symtab.  */
4298       pst->readin = 1;
4299       return;
4300     }
4301
4302   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4303 }
4304
4305 /* Load the DIEs associated with PER_CU into memory.  */
4306
4307 static void
4308 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4309 {
4310   bfd *abfd = objfile->obfd;
4311   struct dwarf2_cu *cu;
4312   unsigned int offset;
4313   gdb_byte *info_ptr, *beg_of_comp_unit;
4314   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4315   struct attribute *attr;
4316   int read_cu = 0;
4317
4318   gdb_assert (! per_cu->from_debug_types);
4319
4320   /* Set local variables from the partial symbol table info.  */
4321   offset = per_cu->offset;
4322
4323   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4324   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4325   beg_of_comp_unit = info_ptr;
4326
4327   if (per_cu->cu == NULL)
4328     {
4329       cu = alloc_one_comp_unit (objfile);
4330
4331       read_cu = 1;
4332
4333       /* If an error occurs while loading, release our storage.  */
4334       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4335
4336       /* Read in the comp_unit header.  */
4337       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4338
4339       /* Complete the cu_header.  */
4340       cu->header.offset = offset;
4341       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4342
4343       /* Read the abbrevs for this compilation unit.  */
4344       dwarf2_read_abbrevs (abfd, cu);
4345       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4346
4347       /* Link this compilation unit into the compilation unit tree.  */
4348       per_cu->cu = cu;
4349       cu->per_cu = per_cu;
4350
4351       /* Link this CU into read_in_chain.  */
4352       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4353       dwarf2_per_objfile->read_in_chain = per_cu;
4354     }
4355   else
4356     {
4357       cu = per_cu->cu;
4358       info_ptr += cu->header.first_die_offset;
4359     }
4360
4361   cu->dies = read_comp_unit (info_ptr, cu);
4362
4363   /* We try not to read any attributes in this function, because not
4364      all objfiles needed for references have been loaded yet, and symbol
4365      table processing isn't initialized.  But we have to set the CU language,
4366      or we won't be able to build types correctly.  */
4367   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4368   if (attr)
4369     set_cu_language (DW_UNSND (attr), cu);
4370   else
4371     set_cu_language (language_minimal, cu);
4372
4373   /* Similarly, if we do not read the producer, we can not apply
4374      producer-specific interpretation.  */
4375   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4376   if (attr)
4377     cu->producer = DW_STRING (attr);
4378
4379   if (read_cu)
4380     {
4381       do_cleanups (free_abbrevs_cleanup);
4382
4383       /* We've successfully allocated this compilation unit.  Let our
4384          caller clean it up when finished with it.  */
4385       discard_cleanups (free_cu_cleanup);
4386     }
4387 }
4388
4389 /* Add a DIE to the delayed physname list.  */
4390
4391 static void
4392 add_to_method_list (struct type *type, int fnfield_index, int index,
4393                     const char *name, struct die_info *die,
4394                     struct dwarf2_cu *cu)
4395 {
4396   struct delayed_method_info mi;
4397   mi.type = type;
4398   mi.fnfield_index = fnfield_index;
4399   mi.index = index;
4400   mi.name = name;
4401   mi.die = die;
4402   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4403 }
4404
4405 /* A cleanup for freeing the delayed method list.  */
4406
4407 static void
4408 free_delayed_list (void *ptr)
4409 {
4410   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4411   if (cu->method_list != NULL)
4412     {
4413       VEC_free (delayed_method_info, cu->method_list);
4414       cu->method_list = NULL;
4415     }
4416 }
4417
4418 /* Compute the physnames of any methods on the CU's method list.
4419
4420    The computation of method physnames is delayed in order to avoid the
4421    (bad) condition that one of the method's formal parameters is of an as yet
4422    incomplete type.  */
4423
4424 static void
4425 compute_delayed_physnames (struct dwarf2_cu *cu)
4426 {
4427   int i;
4428   struct delayed_method_info *mi;
4429   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4430     {
4431       char *physname;
4432       struct fn_fieldlist *fn_flp
4433         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4434       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4435       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4436     }
4437 }
4438
4439 /* Generate full symbol information for PST and CU, whose DIEs have
4440    already been loaded into memory.  */
4441
4442 static void
4443 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4444 {
4445   struct dwarf2_cu *cu = per_cu->cu;
4446   struct objfile *objfile = per_cu->objfile;
4447   CORE_ADDR lowpc, highpc;
4448   struct symtab *symtab;
4449   struct cleanup *back_to, *delayed_list_cleanup;
4450   CORE_ADDR baseaddr;
4451
4452   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4453
4454   buildsym_init ();
4455   back_to = make_cleanup (really_free_pendings, NULL);
4456   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4457
4458   cu->list_in_scope = &file_symbols;
4459
4460   dwarf2_find_base_address (cu->dies, cu);
4461
4462   /* Do line number decoding in read_file_scope () */
4463   process_die (cu->dies, cu);
4464
4465   /* Now that we have processed all the DIEs in the CU, all the types 
4466      should be complete, and it should now be safe to compute all of the
4467      physnames.  */
4468   compute_delayed_physnames (cu);
4469   do_cleanups (delayed_list_cleanup);
4470
4471   /* Some compilers don't define a DW_AT_high_pc attribute for the
4472      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4473      it, by scanning the DIE's below the compilation unit.  */
4474   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4475
4476   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4477
4478   /* Set symtab language to language from DW_AT_language.
4479      If the compilation is from a C file generated by language preprocessors,
4480      do not set the language if it was already deduced by start_subfile.  */
4481   if (symtab != NULL
4482       && !(cu->language == language_c && symtab->language != language_c))
4483     {
4484       symtab->language = cu->language;
4485     }
4486
4487   if (dwarf2_per_objfile->using_index)
4488     per_cu->v.quick->symtab = symtab;
4489   else
4490     {
4491       struct partial_symtab *pst = per_cu->v.psymtab;
4492       pst->symtab = symtab;
4493       pst->readin = 1;
4494     }
4495
4496   do_cleanups (back_to);
4497 }
4498
4499 /* Process a die and its children.  */
4500
4501 static void
4502 process_die (struct die_info *die, struct dwarf2_cu *cu)
4503 {
4504   switch (die->tag)
4505     {
4506     case DW_TAG_padding:
4507       break;
4508     case DW_TAG_compile_unit:
4509       read_file_scope (die, cu);
4510       break;
4511     case DW_TAG_type_unit:
4512       read_type_unit_scope (die, cu);
4513       break;
4514     case DW_TAG_subprogram:
4515     case DW_TAG_inlined_subroutine:
4516       read_func_scope (die, cu);
4517       break;
4518     case DW_TAG_lexical_block:
4519     case DW_TAG_try_block:
4520     case DW_TAG_catch_block:
4521       read_lexical_block_scope (die, cu);
4522       break;
4523     case DW_TAG_class_type:
4524     case DW_TAG_interface_type:
4525     case DW_TAG_structure_type:
4526     case DW_TAG_union_type:
4527       process_structure_scope (die, cu);
4528       break;
4529     case DW_TAG_enumeration_type:
4530       process_enumeration_scope (die, cu);
4531       break;
4532
4533     /* These dies have a type, but processing them does not create
4534        a symbol or recurse to process the children.  Therefore we can
4535        read them on-demand through read_type_die.  */
4536     case DW_TAG_subroutine_type:
4537     case DW_TAG_set_type:
4538     case DW_TAG_array_type:
4539     case DW_TAG_pointer_type:
4540     case DW_TAG_ptr_to_member_type:
4541     case DW_TAG_reference_type:
4542     case DW_TAG_string_type:
4543       break;
4544
4545     case DW_TAG_base_type:
4546     case DW_TAG_subrange_type:
4547     case DW_TAG_typedef:
4548       /* Add a typedef symbol for the type definition, if it has a
4549          DW_AT_name.  */
4550       new_symbol (die, read_type_die (die, cu), cu);
4551       break;
4552     case DW_TAG_common_block:
4553       read_common_block (die, cu);
4554       break;
4555     case DW_TAG_common_inclusion:
4556       break;
4557     case DW_TAG_namespace:
4558       processing_has_namespace_info = 1;
4559       read_namespace (die, cu);
4560       break;
4561     case DW_TAG_module:
4562       processing_has_namespace_info = 1;
4563       read_module (die, cu);
4564       break;
4565     case DW_TAG_imported_declaration:
4566     case DW_TAG_imported_module:
4567       processing_has_namespace_info = 1;
4568       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4569                                  || cu->language != language_fortran))
4570         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4571                    dwarf_tag_name (die->tag));
4572       read_import_statement (die, cu);
4573       break;
4574     default:
4575       new_symbol (die, NULL, cu);
4576       break;
4577     }
4578 }
4579
4580 /* A helper function for dwarf2_compute_name which determines whether DIE
4581    needs to have the name of the scope prepended to the name listed in the
4582    die.  */
4583
4584 static int
4585 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4586 {
4587   struct attribute *attr;
4588
4589   switch (die->tag)
4590     {
4591     case DW_TAG_namespace:
4592     case DW_TAG_typedef:
4593     case DW_TAG_class_type:
4594     case DW_TAG_interface_type:
4595     case DW_TAG_structure_type:
4596     case DW_TAG_union_type:
4597     case DW_TAG_enumeration_type:
4598     case DW_TAG_enumerator:
4599     case DW_TAG_subprogram:
4600     case DW_TAG_member:
4601       return 1;
4602
4603     case DW_TAG_variable:
4604       /* We only need to prefix "globally" visible variables.  These include
4605          any variable marked with DW_AT_external or any variable that
4606          lives in a namespace.  [Variables in anonymous namespaces
4607          require prefixing, but they are not DW_AT_external.]  */
4608
4609       if (dwarf2_attr (die, DW_AT_specification, cu))
4610         {
4611           struct dwarf2_cu *spec_cu = cu;
4612
4613           return die_needs_namespace (die_specification (die, &spec_cu),
4614                                       spec_cu);
4615         }
4616
4617       attr = dwarf2_attr (die, DW_AT_external, cu);
4618       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4619           && die->parent->tag != DW_TAG_module)
4620         return 0;
4621       /* A variable in a lexical block of some kind does not need a
4622          namespace, even though in C++ such variables may be external
4623          and have a mangled name.  */
4624       if (die->parent->tag ==  DW_TAG_lexical_block
4625           || die->parent->tag ==  DW_TAG_try_block
4626           || die->parent->tag ==  DW_TAG_catch_block
4627           || die->parent->tag == DW_TAG_subprogram)
4628         return 0;
4629       return 1;
4630
4631     default:
4632       return 0;
4633     }
4634 }
4635
4636 /* Retrieve the last character from a mem_file.  */
4637
4638 static void
4639 do_ui_file_peek_last (void *object, const char *buffer, long length)
4640 {
4641   char *last_char_p = (char *) object;
4642
4643   if (length > 0)
4644     *last_char_p = buffer[length - 1];
4645 }
4646
4647 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4648    compute the physname for the object, which include a method's
4649    formal parameters (C++/Java) and return type (Java).
4650
4651    For Ada, return the DIE's linkage name rather than the fully qualified
4652    name.  PHYSNAME is ignored..
4653
4654    The result is allocated on the objfile_obstack and canonicalized.  */
4655
4656 static const char *
4657 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4658                      int physname)
4659 {
4660   if (name == NULL)
4661     name = dwarf2_name (die, cu);
4662
4663   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4664      compute it by typename_concat inside GDB.  */
4665   if (cu->language == language_ada
4666       || (cu->language == language_fortran && physname))
4667     {
4668       /* For Ada unit, we prefer the linkage name over the name, as
4669          the former contains the exported name, which the user expects
4670          to be able to reference.  Ideally, we want the user to be able
4671          to reference this entity using either natural or linkage name,
4672          but we haven't started looking at this enhancement yet.  */
4673       struct attribute *attr;
4674
4675       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4676       if (attr == NULL)
4677         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4678       if (attr && DW_STRING (attr))
4679         return DW_STRING (attr);
4680     }
4681
4682   /* These are the only languages we know how to qualify names in.  */
4683   if (name != NULL
4684       && (cu->language == language_cplus || cu->language == language_java
4685           || cu->language == language_fortran))
4686     {
4687       if (die_needs_namespace (die, cu))
4688         {
4689           long length;
4690           char *prefix;
4691           struct ui_file *buf;
4692
4693           prefix = determine_prefix (die, cu);
4694           buf = mem_fileopen ();
4695           if (*prefix != '\0')
4696             {
4697               char *prefixed_name = typename_concat (NULL, prefix, name,
4698                                                      physname, cu);
4699
4700               fputs_unfiltered (prefixed_name, buf);
4701               xfree (prefixed_name);
4702             }
4703           else
4704             fputs_unfiltered (name ? name : "", buf);
4705
4706           /* Template parameters may be specified in the DIE's DW_AT_name, or
4707              as children with DW_TAG_template_type_param or
4708              DW_TAG_value_type_param.  If the latter, add them to the name
4709              here.  If the name already has template parameters, then
4710              skip this step; some versions of GCC emit both, and
4711              it is more efficient to use the pre-computed name.
4712
4713              Something to keep in mind about this process: it is very
4714              unlikely, or in some cases downright impossible, to produce
4715              something that will match the mangled name of a function.
4716              If the definition of the function has the same debug info,
4717              we should be able to match up with it anyway.  But fallbacks
4718              using the minimal symbol, for instance to find a method
4719              implemented in a stripped copy of libstdc++, will not work.
4720              If we do not have debug info for the definition, we will have to
4721              match them up some other way.
4722
4723              When we do name matching there is a related problem with function
4724              templates; two instantiated function templates are allowed to
4725              differ only by their return types, which we do not add here.  */
4726
4727           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4728             {
4729               struct attribute *attr;
4730               struct die_info *child;
4731               int first = 1;
4732
4733               die->building_fullname = 1;
4734
4735               for (child = die->child; child != NULL; child = child->sibling)
4736                 {
4737                   struct type *type;
4738                   long value;
4739                   gdb_byte *bytes;
4740                   struct dwarf2_locexpr_baton *baton;
4741                   struct value *v;
4742
4743                   if (child->tag != DW_TAG_template_type_param
4744                       && child->tag != DW_TAG_template_value_param)
4745                     continue;
4746
4747                   if (first)
4748                     {
4749                       fputs_unfiltered ("<", buf);
4750                       first = 0;
4751                     }
4752                   else
4753                     fputs_unfiltered (", ", buf);
4754
4755                   attr = dwarf2_attr (child, DW_AT_type, cu);
4756                   if (attr == NULL)
4757                     {
4758                       complaint (&symfile_complaints,
4759                                  _("template parameter missing DW_AT_type"));
4760                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4761                       continue;
4762                     }
4763                   type = die_type (child, cu);
4764
4765                   if (child->tag == DW_TAG_template_type_param)
4766                     {
4767                       c_print_type (type, "", buf, -1, 0);
4768                       continue;
4769                     }
4770
4771                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4772                   if (attr == NULL)
4773                     {
4774                       complaint (&symfile_complaints,
4775                                  _("template parameter missing DW_AT_const_value"));
4776                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4777                       continue;
4778                     }
4779
4780                   dwarf2_const_value_attr (attr, type, name,
4781                                            &cu->comp_unit_obstack, cu,
4782                                            &value, &bytes, &baton);
4783
4784                   if (TYPE_NOSIGN (type))
4785                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4786                        changed, this can use value_print instead.  */
4787                     c_printchar (value, type, buf);
4788                   else
4789                     {
4790                       struct value_print_options opts;
4791
4792                       if (baton != NULL)
4793                         v = dwarf2_evaluate_loc_desc (type, NULL,
4794                                                       baton->data,
4795                                                       baton->size,
4796                                                       baton->per_cu);
4797                       else if (bytes != NULL)
4798                         {
4799                           v = allocate_value (type);
4800                           memcpy (value_contents_writeable (v), bytes,
4801                                   TYPE_LENGTH (type));
4802                         }
4803                       else
4804                         v = value_from_longest (type, value);
4805
4806                       /* Specify decimal so that we do not depend on the radix.  */
4807                       get_formatted_print_options (&opts, 'd');
4808                       opts.raw = 1;
4809                       value_print (v, buf, &opts);
4810                       release_value (v);
4811                       value_free (v);
4812                     }
4813                 }
4814
4815               die->building_fullname = 0;
4816
4817               if (!first)
4818                 {
4819                   /* Close the argument list, with a space if necessary
4820                      (nested templates).  */
4821                   char last_char = '\0';
4822                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4823                   if (last_char == '>')
4824                     fputs_unfiltered (" >", buf);
4825                   else
4826                     fputs_unfiltered (">", buf);
4827                 }
4828             }
4829
4830           /* For Java and C++ methods, append formal parameter type
4831              information, if PHYSNAME.  */
4832
4833           if (physname && die->tag == DW_TAG_subprogram
4834               && (cu->language == language_cplus
4835                   || cu->language == language_java))
4836             {
4837               struct type *type = read_type_die (die, cu);
4838
4839               c_type_print_args (type, buf, 0, cu->language);
4840
4841               if (cu->language == language_java)
4842                 {
4843                   /* For java, we must append the return type to method
4844                      names. */
4845                   if (die->tag == DW_TAG_subprogram)
4846                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4847                                      0, 0);
4848                 }
4849               else if (cu->language == language_cplus)
4850                 {
4851                   if (TYPE_NFIELDS (type) > 0
4852                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4853                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4854                     fputs_unfiltered (" const", buf);
4855                 }
4856             }
4857
4858           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4859                                        &length);
4860           ui_file_delete (buf);
4861
4862           if (cu->language == language_cplus)
4863             {
4864               char *cname
4865                 = dwarf2_canonicalize_name (name, cu,
4866                                             &cu->objfile->objfile_obstack);
4867
4868               if (cname != NULL)
4869                 name = cname;
4870             }
4871         }
4872     }
4873
4874   return name;
4875 }
4876
4877 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4878    If scope qualifiers are appropriate they will be added.  The result
4879    will be allocated on the objfile_obstack, or NULL if the DIE does
4880    not have a name.  NAME may either be from a previous call to
4881    dwarf2_name or NULL.
4882
4883    The output string will be canonicalized (if C++/Java). */
4884
4885 static const char *
4886 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4887 {
4888   return dwarf2_compute_name (name, die, cu, 0);
4889 }
4890
4891 /* Construct a physname for the given DIE in CU.  NAME may either be
4892    from a previous call to dwarf2_name or NULL.  The result will be
4893    allocated on the objfile_objstack or NULL if the DIE does not have a
4894    name.
4895
4896    The output string will be canonicalized (if C++/Java).  */
4897
4898 static const char *
4899 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4900 {
4901   return dwarf2_compute_name (name, die, cu, 1);
4902 }
4903
4904 /* Read the import statement specified by the given die and record it.  */
4905
4906 static void
4907 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4908 {
4909   struct attribute *import_attr;
4910   struct die_info *imported_die;
4911   struct dwarf2_cu *imported_cu;
4912   const char *imported_name;
4913   const char *imported_name_prefix;
4914   const char *canonical_name;
4915   const char *import_alias;
4916   const char *imported_declaration = NULL;
4917   const char *import_prefix;
4918
4919   char *temp;
4920
4921   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4922   if (import_attr == NULL)
4923     {
4924       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4925                  dwarf_tag_name (die->tag));
4926       return;
4927     }
4928
4929   imported_cu = cu;
4930   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4931   imported_name = dwarf2_name (imported_die, imported_cu);
4932   if (imported_name == NULL)
4933     {
4934       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4935
4936         The import in the following code:
4937         namespace A
4938           {
4939             typedef int B;
4940           }
4941
4942         int main ()
4943           {
4944             using A::B;
4945             B b;
4946             return b;
4947           }
4948
4949         ...
4950          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4951             <52>   DW_AT_decl_file   : 1
4952             <53>   DW_AT_decl_line   : 6
4953             <54>   DW_AT_import      : <0x75>
4954          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4955             <59>   DW_AT_name        : B
4956             <5b>   DW_AT_decl_file   : 1
4957             <5c>   DW_AT_decl_line   : 2
4958             <5d>   DW_AT_type        : <0x6e>
4959         ...
4960          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4961             <76>   DW_AT_byte_size   : 4
4962             <77>   DW_AT_encoding    : 5        (signed)
4963
4964         imports the wrong die ( 0x75 instead of 0x58 ).
4965         This case will be ignored until the gcc bug is fixed.  */
4966       return;
4967     }
4968
4969   /* Figure out the local name after import.  */
4970   import_alias = dwarf2_name (die, cu);
4971
4972   /* Figure out where the statement is being imported to.  */
4973   import_prefix = determine_prefix (die, cu);
4974
4975   /* Figure out what the scope of the imported die is and prepend it
4976      to the name of the imported die.  */
4977   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4978
4979   if (imported_die->tag != DW_TAG_namespace
4980       && imported_die->tag != DW_TAG_module)
4981     {
4982       imported_declaration = imported_name;
4983       canonical_name = imported_name_prefix;
4984     }
4985   else if (strlen (imported_name_prefix) > 0)
4986     {
4987       temp = alloca (strlen (imported_name_prefix)
4988                      + 2 + strlen (imported_name) + 1);
4989       strcpy (temp, imported_name_prefix);
4990       strcat (temp, "::");
4991       strcat (temp, imported_name);
4992       canonical_name = temp;
4993     }
4994   else
4995     canonical_name = imported_name;
4996
4997   cp_add_using_directive (import_prefix,
4998                           canonical_name,
4999                           import_alias,
5000                           imported_declaration,
5001                           &cu->objfile->objfile_obstack);
5002 }
5003
5004 static void
5005 initialize_cu_func_list (struct dwarf2_cu *cu)
5006 {
5007   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5008 }
5009
5010 static void
5011 free_cu_line_header (void *arg)
5012 {
5013   struct dwarf2_cu *cu = arg;
5014
5015   free_line_header (cu->line_header);
5016   cu->line_header = NULL;
5017 }
5018
5019 static void
5020 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5021                          char **name, char **comp_dir)
5022 {
5023   struct attribute *attr;
5024
5025   *name = NULL;
5026   *comp_dir = NULL;
5027
5028   /* Find the filename.  Do not use dwarf2_name here, since the filename
5029      is not a source language identifier.  */
5030   attr = dwarf2_attr (die, DW_AT_name, cu);
5031   if (attr)
5032     {
5033       *name = DW_STRING (attr);
5034     }
5035
5036   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5037   if (attr)
5038     *comp_dir = DW_STRING (attr);
5039   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5040     {
5041       *comp_dir = ldirname (*name);
5042       if (*comp_dir != NULL)
5043         make_cleanup (xfree, *comp_dir);
5044     }
5045   if (*comp_dir != NULL)
5046     {
5047       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5048          directory, get rid of it.  */
5049       char *cp = strchr (*comp_dir, ':');
5050
5051       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5052         *comp_dir = cp + 1;
5053     }
5054
5055   if (*name == NULL)
5056     *name = "<unknown>";
5057 }
5058
5059 static void
5060 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5061 {
5062   struct objfile *objfile = cu->objfile;
5063   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5064   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5065   CORE_ADDR highpc = ((CORE_ADDR) 0);
5066   struct attribute *attr;
5067   char *name = NULL;
5068   char *comp_dir = NULL;
5069   struct die_info *child_die;
5070   bfd *abfd = objfile->obfd;
5071   struct line_header *line_header = 0;
5072   CORE_ADDR baseaddr;
5073
5074   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5075
5076   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5077
5078   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5079      from finish_block.  */
5080   if (lowpc == ((CORE_ADDR) -1))
5081     lowpc = highpc;
5082   lowpc += baseaddr;
5083   highpc += baseaddr;
5084
5085   find_file_and_directory (die, cu, &name, &comp_dir);
5086
5087   attr = dwarf2_attr (die, DW_AT_language, cu);
5088   if (attr)
5089     {
5090       set_cu_language (DW_UNSND (attr), cu);
5091     }
5092
5093   attr = dwarf2_attr (die, DW_AT_producer, cu);
5094   if (attr)
5095     cu->producer = DW_STRING (attr);
5096
5097   /* We assume that we're processing GCC output. */
5098   processing_gcc_compilation = 2;
5099
5100   processing_has_namespace_info = 0;
5101
5102   start_symtab (name, comp_dir, lowpc);
5103   record_debugformat ("DWARF 2");
5104   record_producer (cu->producer);
5105
5106   initialize_cu_func_list (cu);
5107
5108   /* Decode line number information if present.  We do this before
5109      processing child DIEs, so that the line header table is available
5110      for DW_AT_decl_file.  */
5111   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5112   if (attr)
5113     {
5114       unsigned int line_offset = DW_UNSND (attr);
5115       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5116       if (line_header)
5117         {
5118           cu->line_header = line_header;
5119           make_cleanup (free_cu_line_header, cu);
5120           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5121         }
5122     }
5123
5124   /* Process all dies in compilation unit.  */
5125   if (die->child != NULL)
5126     {
5127       child_die = die->child;
5128       while (child_die && child_die->tag)
5129         {
5130           process_die (child_die, cu);
5131           child_die = sibling_die (child_die);
5132         }
5133     }
5134
5135   /* Decode macro information, if present.  Dwarf 2 macro information
5136      refers to information in the line number info statement program
5137      header, so we can only read it if we've read the header
5138      successfully.  */
5139   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5140   if (attr && line_header)
5141     {
5142       unsigned int macro_offset = DW_UNSND (attr);
5143
5144       dwarf_decode_macros (line_header, macro_offset,
5145                            comp_dir, abfd, cu);
5146     }
5147   do_cleanups (back_to);
5148 }
5149
5150 /* For TUs we want to skip the first top level sibling if it's not the
5151    actual type being defined by this TU.  In this case the first top
5152    level sibling is there to provide context only.  */
5153
5154 static void
5155 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5156 {
5157   struct objfile *objfile = cu->objfile;
5158   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5159   CORE_ADDR lowpc;
5160   struct attribute *attr;
5161   char *name = NULL;
5162   char *comp_dir = NULL;
5163   struct die_info *child_die;
5164   bfd *abfd = objfile->obfd;
5165
5166   /* start_symtab needs a low pc, but we don't really have one.
5167      Do what read_file_scope would do in the absence of such info.  */
5168   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5169
5170   /* Find the filename.  Do not use dwarf2_name here, since the filename
5171      is not a source language identifier.  */
5172   attr = dwarf2_attr (die, DW_AT_name, cu);
5173   if (attr)
5174     name = DW_STRING (attr);
5175
5176   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5177   if (attr)
5178     comp_dir = DW_STRING (attr);
5179   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5180     {
5181       comp_dir = ldirname (name);
5182       if (comp_dir != NULL)
5183         make_cleanup (xfree, comp_dir);
5184     }
5185
5186   if (name == NULL)
5187     name = "<unknown>";
5188
5189   attr = dwarf2_attr (die, DW_AT_language, cu);
5190   if (attr)
5191     set_cu_language (DW_UNSND (attr), cu);
5192
5193   /* This isn't technically needed today.  It is done for symmetry
5194      with read_file_scope.  */
5195   attr = dwarf2_attr (die, DW_AT_producer, cu);
5196   if (attr)
5197     cu->producer = DW_STRING (attr);
5198
5199   /* We assume that we're processing GCC output. */
5200   processing_gcc_compilation = 2;
5201
5202   processing_has_namespace_info = 0;
5203
5204   start_symtab (name, comp_dir, lowpc);
5205   record_debugformat ("DWARF 2");
5206   record_producer (cu->producer);
5207
5208   /* Process the dies in the type unit.  */
5209   if (die->child == NULL)
5210     {
5211       dump_die_for_error (die);
5212       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5213              bfd_get_filename (abfd));
5214     }
5215
5216   child_die = die->child;
5217
5218   while (child_die && child_die->tag)
5219     {
5220       process_die (child_die, cu);
5221
5222       child_die = sibling_die (child_die);
5223     }
5224
5225   do_cleanups (back_to);
5226 }
5227
5228 static void
5229 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5230                      struct dwarf2_cu *cu)
5231 {
5232   struct function_range *thisfn;
5233
5234   thisfn = (struct function_range *)
5235     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5236   thisfn->name = name;
5237   thisfn->lowpc = lowpc;
5238   thisfn->highpc = highpc;
5239   thisfn->seen_line = 0;
5240   thisfn->next = NULL;
5241
5242   if (cu->last_fn == NULL)
5243       cu->first_fn = thisfn;
5244   else
5245       cu->last_fn->next = thisfn;
5246
5247   cu->last_fn = thisfn;
5248 }
5249
5250 /* qsort helper for inherit_abstract_dies.  */
5251
5252 static int
5253 unsigned_int_compar (const void *ap, const void *bp)
5254 {
5255   unsigned int a = *(unsigned int *) ap;
5256   unsigned int b = *(unsigned int *) bp;
5257
5258   return (a > b) - (b > a);
5259 }
5260
5261 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5262    Inherit only the children of the DW_AT_abstract_origin DIE not being already
5263    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
5264
5265 static void
5266 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5267 {
5268   struct die_info *child_die;
5269   unsigned die_children_count;
5270   /* CU offsets which were referenced by children of the current DIE.  */
5271   unsigned *offsets;
5272   unsigned *offsets_end, *offsetp;
5273   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5274   struct die_info *origin_die;
5275   /* Iterator of the ORIGIN_DIE children.  */
5276   struct die_info *origin_child_die;
5277   struct cleanup *cleanups;
5278   struct attribute *attr;
5279   struct dwarf2_cu *origin_cu;
5280   struct pending **origin_previous_list_in_scope;
5281
5282   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5283   if (!attr)
5284     return;
5285
5286   /* Note that following die references may follow to a die in a
5287      different cu.  */
5288
5289   origin_cu = cu;
5290   origin_die = follow_die_ref (die, attr, &origin_cu);
5291
5292   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5293      symbols in.  */
5294   origin_previous_list_in_scope = origin_cu->list_in_scope;
5295   origin_cu->list_in_scope = cu->list_in_scope;
5296
5297   if (die->tag != origin_die->tag
5298       && !(die->tag == DW_TAG_inlined_subroutine
5299            && origin_die->tag == DW_TAG_subprogram))
5300     complaint (&symfile_complaints,
5301                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5302                die->offset, origin_die->offset);
5303
5304   child_die = die->child;
5305   die_children_count = 0;
5306   while (child_die && child_die->tag)
5307     {
5308       child_die = sibling_die (child_die);
5309       die_children_count++;
5310     }
5311   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5312   cleanups = make_cleanup (xfree, offsets);
5313
5314   offsets_end = offsets;
5315   child_die = die->child;
5316   while (child_die && child_die->tag)
5317     {
5318       /* For each CHILD_DIE, find the corresponding child of
5319          ORIGIN_DIE.  If there is more than one layer of
5320          DW_AT_abstract_origin, follow them all; there shouldn't be,
5321          but GCC versions at least through 4.4 generate this (GCC PR
5322          40573).  */
5323       struct die_info *child_origin_die = child_die;
5324       struct dwarf2_cu *child_origin_cu = cu;
5325
5326       while (1)
5327         {
5328           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5329                               child_origin_cu);
5330           if (attr == NULL)
5331             break;
5332           child_origin_die = follow_die_ref (child_origin_die, attr,
5333                                              &child_origin_cu);
5334         }
5335
5336       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5337          counterpart may exist.  */
5338       if (child_origin_die != child_die)
5339         {
5340           if (child_die->tag != child_origin_die->tag
5341               && !(child_die->tag == DW_TAG_inlined_subroutine
5342                    && child_origin_die->tag == DW_TAG_subprogram))
5343             complaint (&symfile_complaints,
5344                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5345                          "different tags"), child_die->offset,
5346                        child_origin_die->offset);
5347           if (child_origin_die->parent != origin_die)
5348             complaint (&symfile_complaints,
5349                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5350                          "different parents"), child_die->offset,
5351                        child_origin_die->offset);
5352           else
5353             *offsets_end++ = child_origin_die->offset;
5354         }
5355       child_die = sibling_die (child_die);
5356     }
5357   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5358          unsigned_int_compar);
5359   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5360     if (offsetp[-1] == *offsetp)
5361       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5362                                         "to DIE 0x%x as their abstract origin"),
5363                  die->offset, *offsetp);
5364
5365   offsetp = offsets;
5366   origin_child_die = origin_die->child;
5367   while (origin_child_die && origin_child_die->tag)
5368     {
5369       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5370       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5371         offsetp++;
5372       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5373         {
5374           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5375           process_die (origin_child_die, origin_cu);
5376         }
5377       origin_child_die = sibling_die (origin_child_die);
5378     }
5379   origin_cu->list_in_scope = origin_previous_list_in_scope;
5380
5381   do_cleanups (cleanups);
5382 }
5383
5384 static void
5385 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5386 {
5387   struct objfile *objfile = cu->objfile;
5388   struct context_stack *new;
5389   CORE_ADDR lowpc;
5390   CORE_ADDR highpc;
5391   struct die_info *child_die;
5392   struct attribute *attr, *call_line, *call_file;
5393   char *name;
5394   CORE_ADDR baseaddr;
5395   struct block *block;
5396   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5397   VEC (symbolp) *template_args = NULL;
5398   struct template_symbol *templ_func = NULL;
5399
5400   if (inlined_func)
5401     {
5402       /* If we do not have call site information, we can't show the
5403          caller of this inlined function.  That's too confusing, so
5404          only use the scope for local variables.  */
5405       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5406       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5407       if (call_line == NULL || call_file == NULL)
5408         {
5409           read_lexical_block_scope (die, cu);
5410           return;
5411         }
5412     }
5413
5414   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5415
5416   name = dwarf2_name (die, cu);
5417
5418   /* Ignore functions with missing or empty names.  These are actually
5419      illegal according to the DWARF standard.  */
5420   if (name == NULL)
5421     {
5422       complaint (&symfile_complaints,
5423                  _("missing name for subprogram DIE at %d"), die->offset);
5424       return;
5425     }
5426
5427   /* Ignore functions with missing or invalid low and high pc attributes.  */
5428   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5429     {
5430       attr = dwarf2_attr (die, DW_AT_external, cu);
5431       if (!attr || !DW_UNSND (attr))
5432         complaint (&symfile_complaints,
5433                    _("cannot get low and high bounds for subprogram DIE at %d"),
5434                    die->offset);
5435       return;
5436     }
5437
5438   lowpc += baseaddr;
5439   highpc += baseaddr;
5440
5441   /* Record the function range for dwarf_decode_lines.  */
5442   add_to_cu_func_list (name, lowpc, highpc, cu);
5443
5444   /* If we have any template arguments, then we must allocate a
5445      different sort of symbol.  */
5446   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5447     {
5448       if (child_die->tag == DW_TAG_template_type_param
5449           || child_die->tag == DW_TAG_template_value_param)
5450         {
5451           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5452                                        struct template_symbol);
5453           templ_func->base.is_cplus_template_function = 1;
5454           break;
5455         }
5456     }
5457
5458   new = push_context (0, lowpc);
5459   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5460                                (struct symbol *) templ_func);
5461
5462   /* If there is a location expression for DW_AT_frame_base, record
5463      it.  */
5464   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5465   if (attr)
5466     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5467        expression is being recorded directly in the function's symbol
5468        and not in a separate frame-base object.  I guess this hack is
5469        to avoid adding some sort of frame-base adjunct/annex to the
5470        function's symbol :-(.  The problem with doing this is that it
5471        results in a function symbol with a location expression that
5472        has nothing to do with the location of the function, ouch!  The
5473        relationship should be: a function's symbol has-a frame base; a
5474        frame-base has-a location expression.  */
5475     dwarf2_symbol_mark_computed (attr, new->name, cu);
5476
5477   cu->list_in_scope = &local_symbols;
5478
5479   if (die->child != NULL)
5480     {
5481       child_die = die->child;
5482       while (child_die && child_die->tag)
5483         {
5484           if (child_die->tag == DW_TAG_template_type_param
5485               || child_die->tag == DW_TAG_template_value_param)
5486             {
5487               struct symbol *arg = new_symbol (child_die, NULL, cu);
5488
5489               VEC_safe_push (symbolp, template_args, arg);
5490             }
5491           else
5492             process_die (child_die, cu);
5493           child_die = sibling_die (child_die);
5494         }
5495     }
5496
5497   inherit_abstract_dies (die, cu);
5498
5499   /* If we have a DW_AT_specification, we might need to import using
5500      directives from the context of the specification DIE.  See the
5501      comment in determine_prefix.  */
5502   if (cu->language == language_cplus
5503       && dwarf2_attr (die, DW_AT_specification, cu))
5504     {
5505       struct dwarf2_cu *spec_cu = cu;
5506       struct die_info *spec_die = die_specification (die, &spec_cu);
5507
5508       while (spec_die)
5509         {
5510           child_die = spec_die->child;
5511           while (child_die && child_die->tag)
5512             {
5513               if (child_die->tag == DW_TAG_imported_module)
5514                 process_die (child_die, spec_cu);
5515               child_die = sibling_die (child_die);
5516             }
5517
5518           /* In some cases, GCC generates specification DIEs that
5519              themselves contain DW_AT_specification attributes.  */
5520           spec_die = die_specification (spec_die, &spec_cu);
5521         }
5522     }
5523
5524   new = pop_context ();
5525   /* Make a block for the local symbols within.  */
5526   block = finish_block (new->name, &local_symbols, new->old_blocks,
5527                         lowpc, highpc, objfile);
5528
5529   /* For C++, set the block's scope.  */
5530   if (cu->language == language_cplus || cu->language == language_fortran)
5531     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5532                         determine_prefix (die, cu),
5533                         processing_has_namespace_info);
5534
5535   /* If we have address ranges, record them.  */
5536   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5537
5538   /* Attach template arguments to function.  */
5539   if (! VEC_empty (symbolp, template_args))
5540     {
5541       gdb_assert (templ_func != NULL);
5542
5543       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5544       templ_func->template_arguments
5545         = obstack_alloc (&objfile->objfile_obstack,
5546                          (templ_func->n_template_arguments
5547                           * sizeof (struct symbol *)));
5548       memcpy (templ_func->template_arguments,
5549               VEC_address (symbolp, template_args),
5550               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5551       VEC_free (symbolp, template_args);
5552     }
5553
5554   /* In C++, we can have functions nested inside functions (e.g., when
5555      a function declares a class that has methods).  This means that
5556      when we finish processing a function scope, we may need to go
5557      back to building a containing block's symbol lists.  */
5558   local_symbols = new->locals;
5559   param_symbols = new->params;
5560   using_directives = new->using_directives;
5561
5562   /* If we've finished processing a top-level function, subsequent
5563      symbols go in the file symbol list.  */
5564   if (outermost_context_p ())
5565     cu->list_in_scope = &file_symbols;
5566 }
5567
5568 /* Process all the DIES contained within a lexical block scope.  Start
5569    a new scope, process the dies, and then close the scope.  */
5570
5571 static void
5572 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5573 {
5574   struct objfile *objfile = cu->objfile;
5575   struct context_stack *new;
5576   CORE_ADDR lowpc, highpc;
5577   struct die_info *child_die;
5578   CORE_ADDR baseaddr;
5579
5580   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5581
5582   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5583   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5584      as multiple lexical blocks?  Handling children in a sane way would
5585      be nasty.  Might be easier to properly extend generic blocks to
5586      describe ranges.  */
5587   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5588     return;
5589   lowpc += baseaddr;
5590   highpc += baseaddr;
5591
5592   push_context (0, lowpc);
5593   if (die->child != NULL)
5594     {
5595       child_die = die->child;
5596       while (child_die && child_die->tag)
5597         {
5598           process_die (child_die, cu);
5599           child_die = sibling_die (child_die);
5600         }
5601     }
5602   new = pop_context ();
5603
5604   if (local_symbols != NULL || using_directives != NULL)
5605     {
5606       struct block *block
5607         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5608                         highpc, objfile);
5609
5610       /* Note that recording ranges after traversing children, as we
5611          do here, means that recording a parent's ranges entails
5612          walking across all its children's ranges as they appear in
5613          the address map, which is quadratic behavior.
5614
5615          It would be nicer to record the parent's ranges before
5616          traversing its children, simply overriding whatever you find
5617          there.  But since we don't even decide whether to create a
5618          block until after we've traversed its children, that's hard
5619          to do.  */
5620       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5621     }
5622   local_symbols = new->locals;
5623   using_directives = new->using_directives;
5624 }
5625
5626 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5627    Return 1 if the attributes are present and valid, otherwise, return 0.
5628    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5629
5630 static int
5631 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5632                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5633                     struct partial_symtab *ranges_pst)
5634 {
5635   struct objfile *objfile = cu->objfile;
5636   struct comp_unit_head *cu_header = &cu->header;
5637   bfd *obfd = objfile->obfd;
5638   unsigned int addr_size = cu_header->addr_size;
5639   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5640   /* Base address selection entry.  */
5641   CORE_ADDR base;
5642   int found_base;
5643   unsigned int dummy;
5644   gdb_byte *buffer;
5645   CORE_ADDR marker;
5646   int low_set;
5647   CORE_ADDR low = 0;
5648   CORE_ADDR high = 0;
5649   CORE_ADDR baseaddr;
5650
5651   found_base = cu->base_known;
5652   base = cu->base_address;
5653
5654   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5655   if (offset >= dwarf2_per_objfile->ranges.size)
5656     {
5657       complaint (&symfile_complaints,
5658                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5659                  offset);
5660       return 0;
5661     }
5662   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5663
5664   /* Read in the largest possible address.  */
5665   marker = read_address (obfd, buffer, cu, &dummy);
5666   if ((marker & mask) == mask)
5667     {
5668       /* If we found the largest possible address, then
5669          read the base address.  */
5670       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5671       buffer += 2 * addr_size;
5672       offset += 2 * addr_size;
5673       found_base = 1;
5674     }
5675
5676   low_set = 0;
5677
5678   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5679
5680   while (1)
5681     {
5682       CORE_ADDR range_beginning, range_end;
5683
5684       range_beginning = read_address (obfd, buffer, cu, &dummy);
5685       buffer += addr_size;
5686       range_end = read_address (obfd, buffer, cu, &dummy);
5687       buffer += addr_size;
5688       offset += 2 * addr_size;
5689
5690       /* An end of list marker is a pair of zero addresses.  */
5691       if (range_beginning == 0 && range_end == 0)
5692         /* Found the end of list entry.  */
5693         break;
5694
5695       /* Each base address selection entry is a pair of 2 values.
5696          The first is the largest possible address, the second is
5697          the base address.  Check for a base address here.  */
5698       if ((range_beginning & mask) == mask)
5699         {
5700           /* If we found the largest possible address, then
5701              read the base address.  */
5702           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5703           found_base = 1;
5704           continue;
5705         }
5706
5707       if (!found_base)
5708         {
5709           /* We have no valid base address for the ranges
5710              data.  */
5711           complaint (&symfile_complaints,
5712                      _("Invalid .debug_ranges data (no base address)"));
5713           return 0;
5714         }
5715
5716       range_beginning += base;
5717       range_end += base;
5718
5719       if (ranges_pst != NULL && range_beginning < range_end)
5720         addrmap_set_empty (objfile->psymtabs_addrmap,
5721                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5722                            ranges_pst);
5723
5724       /* FIXME: This is recording everything as a low-high
5725          segment of consecutive addresses.  We should have a
5726          data structure for discontiguous block ranges
5727          instead.  */
5728       if (! low_set)
5729         {
5730           low = range_beginning;
5731           high = range_end;
5732           low_set = 1;
5733         }
5734       else
5735         {
5736           if (range_beginning < low)
5737             low = range_beginning;
5738           if (range_end > high)
5739             high = range_end;
5740         }
5741     }
5742
5743   if (! low_set)
5744     /* If the first entry is an end-of-list marker, the range
5745        describes an empty scope, i.e. no instructions.  */
5746     return 0;
5747
5748   if (low_return)
5749     *low_return = low;
5750   if (high_return)
5751     *high_return = high;
5752   return 1;
5753 }
5754
5755 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5756    are present and valid, otherwise, return 0.  Return -1 if the range is
5757    discontinuous, i.e. derived from DW_AT_ranges information.  */
5758 static int
5759 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5760                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5761                       struct partial_symtab *pst)
5762 {
5763   struct attribute *attr;
5764   CORE_ADDR low = 0;
5765   CORE_ADDR high = 0;
5766   int ret = 0;
5767
5768   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5769   if (attr)
5770     {
5771       high = DW_ADDR (attr);
5772       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5773       if (attr)
5774         low = DW_ADDR (attr);
5775       else
5776         /* Found high w/o low attribute.  */
5777         return 0;
5778
5779       /* Found consecutive range of addresses.  */
5780       ret = 1;
5781     }
5782   else
5783     {
5784       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5785       if (attr != NULL)
5786         {
5787           /* Value of the DW_AT_ranges attribute is the offset in the
5788              .debug_ranges section.  */
5789           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5790             return 0;
5791           /* Found discontinuous range of addresses.  */
5792           ret = -1;
5793         }
5794     }
5795
5796   if (high < low)
5797     return 0;
5798
5799   /* When using the GNU linker, .gnu.linkonce. sections are used to
5800      eliminate duplicate copies of functions and vtables and such.
5801      The linker will arbitrarily choose one and discard the others.
5802      The AT_*_pc values for such functions refer to local labels in
5803      these sections.  If the section from that file was discarded, the
5804      labels are not in the output, so the relocs get a value of 0.
5805      If this is a discarded function, mark the pc bounds as invalid,
5806      so that GDB will ignore it.  */
5807   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5808     return 0;
5809
5810   *lowpc = low;
5811   *highpc = high;
5812   return ret;
5813 }
5814
5815 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5816    its low and high PC addresses.  Do nothing if these addresses could not
5817    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5818    and HIGHPC to the high address if greater than HIGHPC.  */
5819
5820 static void
5821 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5822                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5823                                  struct dwarf2_cu *cu)
5824 {
5825   CORE_ADDR low, high;
5826   struct die_info *child = die->child;
5827
5828   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5829     {
5830       *lowpc = min (*lowpc, low);
5831       *highpc = max (*highpc, high);
5832     }
5833
5834   /* If the language does not allow nested subprograms (either inside
5835      subprograms or lexical blocks), we're done.  */
5836   if (cu->language != language_ada)
5837     return;
5838
5839   /* Check all the children of the given DIE.  If it contains nested
5840      subprograms, then check their pc bounds.  Likewise, we need to
5841      check lexical blocks as well, as they may also contain subprogram
5842      definitions.  */
5843   while (child && child->tag)
5844     {
5845       if (child->tag == DW_TAG_subprogram
5846           || child->tag == DW_TAG_lexical_block)
5847         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5848       child = sibling_die (child);
5849     }
5850 }
5851
5852 /* Get the low and high pc's represented by the scope DIE, and store
5853    them in *LOWPC and *HIGHPC.  If the correct values can't be
5854    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5855
5856 static void
5857 get_scope_pc_bounds (struct die_info *die,
5858                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5859                      struct dwarf2_cu *cu)
5860 {
5861   CORE_ADDR best_low = (CORE_ADDR) -1;
5862   CORE_ADDR best_high = (CORE_ADDR) 0;
5863   CORE_ADDR current_low, current_high;
5864
5865   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5866     {
5867       best_low = current_low;
5868       best_high = current_high;
5869     }
5870   else
5871     {
5872       struct die_info *child = die->child;
5873
5874       while (child && child->tag)
5875         {
5876           switch (child->tag) {
5877           case DW_TAG_subprogram:
5878             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5879             break;
5880           case DW_TAG_namespace:
5881           case DW_TAG_module:
5882             /* FIXME: carlton/2004-01-16: Should we do this for
5883                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5884                that current GCC's always emit the DIEs corresponding
5885                to definitions of methods of classes as children of a
5886                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5887                the DIEs giving the declarations, which could be
5888                anywhere).  But I don't see any reason why the
5889                standards says that they have to be there.  */
5890             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5891
5892             if (current_low != ((CORE_ADDR) -1))
5893               {
5894                 best_low = min (best_low, current_low);
5895                 best_high = max (best_high, current_high);
5896               }
5897             break;
5898           default:
5899             /* Ignore. */
5900             break;
5901           }
5902
5903           child = sibling_die (child);
5904         }
5905     }
5906
5907   *lowpc = best_low;
5908   *highpc = best_high;
5909 }
5910
5911 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5912    in DIE.  */
5913 static void
5914 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5915                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5916 {
5917   struct attribute *attr;
5918
5919   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5920   if (attr)
5921     {
5922       CORE_ADDR high = DW_ADDR (attr);
5923
5924       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5925       if (attr)
5926         {
5927           CORE_ADDR low = DW_ADDR (attr);
5928
5929           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5930         }
5931     }
5932
5933   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5934   if (attr)
5935     {
5936       bfd *obfd = cu->objfile->obfd;
5937
5938       /* The value of the DW_AT_ranges attribute is the offset of the
5939          address range list in the .debug_ranges section.  */
5940       unsigned long offset = DW_UNSND (attr);
5941       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5942
5943       /* For some target architectures, but not others, the
5944          read_address function sign-extends the addresses it returns.
5945          To recognize base address selection entries, we need a
5946          mask.  */
5947       unsigned int addr_size = cu->header.addr_size;
5948       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5949
5950       /* The base address, to which the next pair is relative.  Note
5951          that this 'base' is a DWARF concept: most entries in a range
5952          list are relative, to reduce the number of relocs against the
5953          debugging information.  This is separate from this function's
5954          'baseaddr' argument, which GDB uses to relocate debugging
5955          information from a shared library based on the address at
5956          which the library was loaded.  */
5957       CORE_ADDR base = cu->base_address;
5958       int base_known = cu->base_known;
5959
5960       gdb_assert (dwarf2_per_objfile->ranges.readin);
5961       if (offset >= dwarf2_per_objfile->ranges.size)
5962         {
5963           complaint (&symfile_complaints,
5964                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5965                      offset);
5966           return;
5967         }
5968
5969       for (;;)
5970         {
5971           unsigned int bytes_read;
5972           CORE_ADDR start, end;
5973
5974           start = read_address (obfd, buffer, cu, &bytes_read);
5975           buffer += bytes_read;
5976           end = read_address (obfd, buffer, cu, &bytes_read);
5977           buffer += bytes_read;
5978
5979           /* Did we find the end of the range list?  */
5980           if (start == 0 && end == 0)
5981             break;
5982
5983           /* Did we find a base address selection entry?  */
5984           else if ((start & base_select_mask) == base_select_mask)
5985             {
5986               base = end;
5987               base_known = 1;
5988             }
5989
5990           /* We found an ordinary address range.  */
5991           else
5992             {
5993               if (!base_known)
5994                 {
5995                   complaint (&symfile_complaints,
5996                              _("Invalid .debug_ranges data (no base address)"));
5997                   return;
5998                 }
5999
6000               record_block_range (block,
6001                                   baseaddr + base + start,
6002                                   baseaddr + base + end - 1);
6003             }
6004         }
6005     }
6006 }
6007
6008 /* Add an aggregate field to the field list.  */
6009
6010 static void
6011 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6012                   struct dwarf2_cu *cu)
6013 {
6014   struct objfile *objfile = cu->objfile;
6015   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6016   struct nextfield *new_field;
6017   struct attribute *attr;
6018   struct field *fp;
6019   char *fieldname = "";
6020
6021   /* Allocate a new field list entry and link it in.  */
6022   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6023   make_cleanup (xfree, new_field);
6024   memset (new_field, 0, sizeof (struct nextfield));
6025
6026   if (die->tag == DW_TAG_inheritance)
6027     {
6028       new_field->next = fip->baseclasses;
6029       fip->baseclasses = new_field;
6030     }
6031   else
6032     {
6033       new_field->next = fip->fields;
6034       fip->fields = new_field;
6035     }
6036   fip->nfields++;
6037
6038   /* Handle accessibility and virtuality of field.
6039      The default accessibility for members is public, the default
6040      accessibility for inheritance is private.  */
6041   if (die->tag != DW_TAG_inheritance)
6042     new_field->accessibility = DW_ACCESS_public;
6043   else
6044     new_field->accessibility = DW_ACCESS_private;
6045   new_field->virtuality = DW_VIRTUALITY_none;
6046
6047   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6048   if (attr)
6049     new_field->accessibility = DW_UNSND (attr);
6050   if (new_field->accessibility != DW_ACCESS_public)
6051     fip->non_public_fields = 1;
6052   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6053   if (attr)
6054     new_field->virtuality = DW_UNSND (attr);
6055
6056   fp = &new_field->field;
6057
6058   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6059     {
6060       /* Data member other than a C++ static data member.  */
6061
6062       /* Get type of field.  */
6063       fp->type = die_type (die, cu);
6064
6065       SET_FIELD_BITPOS (*fp, 0);
6066
6067       /* Get bit size of field (zero if none).  */
6068       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6069       if (attr)
6070         {
6071           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6072         }
6073       else
6074         {
6075           FIELD_BITSIZE (*fp) = 0;
6076         }
6077
6078       /* Get bit offset of field.  */
6079       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6080       if (attr)
6081         {
6082           int byte_offset = 0;
6083
6084           if (attr_form_is_section_offset (attr))
6085             dwarf2_complex_location_expr_complaint ();
6086           else if (attr_form_is_constant (attr))
6087             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6088           else if (attr_form_is_block (attr))
6089             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6090           else
6091             dwarf2_complex_location_expr_complaint ();
6092
6093           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6094         }
6095       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6096       if (attr)
6097         {
6098           if (gdbarch_bits_big_endian (gdbarch))
6099             {
6100               /* For big endian bits, the DW_AT_bit_offset gives the
6101                  additional bit offset from the MSB of the containing
6102                  anonymous object to the MSB of the field.  We don't
6103                  have to do anything special since we don't need to
6104                  know the size of the anonymous object.  */
6105               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6106             }
6107           else
6108             {
6109               /* For little endian bits, compute the bit offset to the
6110                  MSB of the anonymous object, subtract off the number of
6111                  bits from the MSB of the field to the MSB of the
6112                  object, and then subtract off the number of bits of
6113                  the field itself.  The result is the bit offset of
6114                  the LSB of the field.  */
6115               int anonymous_size;
6116               int bit_offset = DW_UNSND (attr);
6117
6118               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6119               if (attr)
6120                 {
6121                   /* The size of the anonymous object containing
6122                      the bit field is explicit, so use the
6123                      indicated size (in bytes).  */
6124                   anonymous_size = DW_UNSND (attr);
6125                 }
6126               else
6127                 {
6128                   /* The size of the anonymous object containing
6129                      the bit field must be inferred from the type
6130                      attribute of the data member containing the
6131                      bit field.  */
6132                   anonymous_size = TYPE_LENGTH (fp->type);
6133                 }
6134               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6135                 - bit_offset - FIELD_BITSIZE (*fp);
6136             }
6137         }
6138
6139       /* Get name of field.  */
6140       fieldname = dwarf2_name (die, cu);
6141       if (fieldname == NULL)
6142         fieldname = "";
6143
6144       /* The name is already allocated along with this objfile, so we don't
6145          need to duplicate it for the type.  */
6146       fp->name = fieldname;
6147
6148       /* Change accessibility for artificial fields (e.g. virtual table
6149          pointer or virtual base class pointer) to private.  */
6150       if (dwarf2_attr (die, DW_AT_artificial, cu))
6151         {
6152           FIELD_ARTIFICIAL (*fp) = 1;
6153           new_field->accessibility = DW_ACCESS_private;
6154           fip->non_public_fields = 1;
6155         }
6156     }
6157   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6158     {
6159       /* C++ static member.  */
6160
6161       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6162          is a declaration, but all versions of G++ as of this writing
6163          (so through at least 3.2.1) incorrectly generate
6164          DW_TAG_variable tags.  */
6165
6166       char *physname;
6167
6168       /* Get name of field.  */
6169       fieldname = dwarf2_name (die, cu);
6170       if (fieldname == NULL)
6171         return;
6172
6173       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6174       if (attr
6175           /* Only create a symbol if this is an external value.
6176              new_symbol checks this and puts the value in the global symbol
6177              table, which we want.  If it is not external, new_symbol
6178              will try to put the value in cu->list_in_scope which is wrong.  */
6179           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6180         {
6181           /* A static const member, not much different than an enum as far as
6182              we're concerned, except that we can support more types.  */
6183           new_symbol (die, NULL, cu);
6184         }
6185
6186       /* Get physical name.  */
6187       physname = (char *) dwarf2_physname (fieldname, die, cu);
6188
6189       /* The name is already allocated along with this objfile, so we don't
6190          need to duplicate it for the type.  */
6191       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6192       FIELD_TYPE (*fp) = die_type (die, cu);
6193       FIELD_NAME (*fp) = fieldname;
6194     }
6195   else if (die->tag == DW_TAG_inheritance)
6196     {
6197       /* C++ base class field.  */
6198       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6199       if (attr)
6200         {
6201           int byte_offset = 0;
6202
6203           if (attr_form_is_section_offset (attr))
6204             dwarf2_complex_location_expr_complaint ();
6205           else if (attr_form_is_constant (attr))
6206             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6207           else if (attr_form_is_block (attr))
6208             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6209           else
6210             dwarf2_complex_location_expr_complaint ();
6211
6212           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6213         }
6214       FIELD_BITSIZE (*fp) = 0;
6215       FIELD_TYPE (*fp) = die_type (die, cu);
6216       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6217       fip->nbaseclasses++;
6218     }
6219 }
6220
6221 /* Add a typedef defined in the scope of the FIP's class.  */
6222
6223 static void
6224 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6225                     struct dwarf2_cu *cu)
6226 {
6227   struct objfile *objfile = cu->objfile;
6228   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6229   struct typedef_field_list *new_field;
6230   struct attribute *attr;
6231   struct typedef_field *fp;
6232   char *fieldname = "";
6233
6234   /* Allocate a new field list entry and link it in.  */
6235   new_field = xzalloc (sizeof (*new_field));
6236   make_cleanup (xfree, new_field);
6237
6238   gdb_assert (die->tag == DW_TAG_typedef);
6239
6240   fp = &new_field->field;
6241
6242   /* Get name of field.  */
6243   fp->name = dwarf2_name (die, cu);
6244   if (fp->name == NULL)
6245     return;
6246
6247   fp->type = read_type_die (die, cu);
6248
6249   new_field->next = fip->typedef_field_list;
6250   fip->typedef_field_list = new_field;
6251   fip->typedef_field_list_count++;
6252 }
6253
6254 /* Create the vector of fields, and attach it to the type.  */
6255
6256 static void
6257 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6258                               struct dwarf2_cu *cu)
6259 {
6260   int nfields = fip->nfields;
6261
6262   /* Record the field count, allocate space for the array of fields,
6263      and create blank accessibility bitfields if necessary.  */
6264   TYPE_NFIELDS (type) = nfields;
6265   TYPE_FIELDS (type) = (struct field *)
6266     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6267   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6268
6269   if (fip->non_public_fields && cu->language != language_ada)
6270     {
6271       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6272
6273       TYPE_FIELD_PRIVATE_BITS (type) =
6274         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6275       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6276
6277       TYPE_FIELD_PROTECTED_BITS (type) =
6278         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6279       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6280
6281       TYPE_FIELD_IGNORE_BITS (type) =
6282         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6283       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6284     }
6285
6286   /* If the type has baseclasses, allocate and clear a bit vector for
6287      TYPE_FIELD_VIRTUAL_BITS.  */
6288   if (fip->nbaseclasses && cu->language != language_ada)
6289     {
6290       int num_bytes = B_BYTES (fip->nbaseclasses);
6291       unsigned char *pointer;
6292
6293       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6294       pointer = TYPE_ALLOC (type, num_bytes);
6295       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6296       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6297       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6298     }
6299
6300   /* Copy the saved-up fields into the field vector.  Start from the head
6301      of the list, adding to the tail of the field array, so that they end
6302      up in the same order in the array in which they were added to the list.  */
6303   while (nfields-- > 0)
6304     {
6305       struct nextfield *fieldp;
6306
6307       if (fip->fields)
6308         {
6309           fieldp = fip->fields;
6310           fip->fields = fieldp->next;
6311         }
6312       else
6313         {
6314           fieldp = fip->baseclasses;
6315           fip->baseclasses = fieldp->next;
6316         }
6317
6318       TYPE_FIELD (type, nfields) = fieldp->field;
6319       switch (fieldp->accessibility)
6320         {
6321         case DW_ACCESS_private:
6322           if (cu->language != language_ada)
6323             SET_TYPE_FIELD_PRIVATE (type, nfields);
6324           break;
6325
6326         case DW_ACCESS_protected:
6327           if (cu->language != language_ada)
6328             SET_TYPE_FIELD_PROTECTED (type, nfields);
6329           break;
6330
6331         case DW_ACCESS_public:
6332           break;
6333
6334         default:
6335           /* Unknown accessibility.  Complain and treat it as public.  */
6336           {
6337             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6338                        fieldp->accessibility);
6339           }
6340           break;
6341         }
6342       if (nfields < fip->nbaseclasses)
6343         {
6344           switch (fieldp->virtuality)
6345             {
6346             case DW_VIRTUALITY_virtual:
6347             case DW_VIRTUALITY_pure_virtual:
6348               if (cu->language == language_ada)
6349                 error ("unexpected virtuality in component of Ada type");
6350               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6351               break;
6352             }
6353         }
6354     }
6355 }
6356
6357 /* Add a member function to the proper fieldlist.  */
6358
6359 static void
6360 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6361                       struct type *type, struct dwarf2_cu *cu)
6362 {
6363   struct objfile *objfile = cu->objfile;
6364   struct attribute *attr;
6365   struct fnfieldlist *flp;
6366   int i;
6367   struct fn_field *fnp;
6368   char *fieldname;
6369   struct nextfnfield *new_fnfield;
6370   struct type *this_type;
6371
6372   if (cu->language == language_ada)
6373     error ("unexpected member function in Ada type");
6374
6375   /* Get name of member function.  */
6376   fieldname = dwarf2_name (die, cu);
6377   if (fieldname == NULL)
6378     return;
6379
6380   /* Look up member function name in fieldlist.  */
6381   for (i = 0; i < fip->nfnfields; i++)
6382     {
6383       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6384         break;
6385     }
6386
6387   /* Create new list element if necessary.  */
6388   if (i < fip->nfnfields)
6389     flp = &fip->fnfieldlists[i];
6390   else
6391     {
6392       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6393         {
6394           fip->fnfieldlists = (struct fnfieldlist *)
6395             xrealloc (fip->fnfieldlists,
6396                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6397                       * sizeof (struct fnfieldlist));
6398           if (fip->nfnfields == 0)
6399             make_cleanup (free_current_contents, &fip->fnfieldlists);
6400         }
6401       flp = &fip->fnfieldlists[fip->nfnfields];
6402       flp->name = fieldname;
6403       flp->length = 0;
6404       flp->head = NULL;
6405       i = fip->nfnfields++;
6406     }
6407
6408   /* Create a new member function field and chain it to the field list
6409      entry. */
6410   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6411   make_cleanup (xfree, new_fnfield);
6412   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6413   new_fnfield->next = flp->head;
6414   flp->head = new_fnfield;
6415   flp->length++;
6416
6417   /* Fill in the member function field info.  */
6418   fnp = &new_fnfield->fnfield;
6419
6420   /* Delay processing of the physname until later.  */
6421   if (cu->language == language_cplus || cu->language == language_java)
6422     {
6423       add_to_method_list (type, i, flp->length - 1, fieldname,
6424                           die, cu);
6425     }
6426   else
6427     {
6428       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6429       fnp->physname = physname ? physname : "";
6430     }
6431
6432   fnp->type = alloc_type (objfile);
6433   this_type = read_type_die (die, cu);
6434   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6435     {
6436       int nparams = TYPE_NFIELDS (this_type);
6437
6438       /* TYPE is the domain of this method, and THIS_TYPE is the type
6439            of the method itself (TYPE_CODE_METHOD).  */
6440       smash_to_method_type (fnp->type, type,
6441                             TYPE_TARGET_TYPE (this_type),
6442                             TYPE_FIELDS (this_type),
6443                             TYPE_NFIELDS (this_type),
6444                             TYPE_VARARGS (this_type));
6445
6446       /* Handle static member functions.
6447          Dwarf2 has no clean way to discern C++ static and non-static
6448          member functions. G++ helps GDB by marking the first
6449          parameter for non-static member functions (which is the
6450          this pointer) as artificial. We obtain this information
6451          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6452       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6453         fnp->voffset = VOFFSET_STATIC;
6454     }
6455   else
6456     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6457                dwarf2_full_name (fieldname, die, cu));
6458
6459   /* Get fcontext from DW_AT_containing_type if present.  */
6460   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6461     fnp->fcontext = die_containing_type (die, cu);
6462
6463   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6464      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6465
6466   /* Get accessibility.  */
6467   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6468   if (attr)
6469     {
6470       switch (DW_UNSND (attr))
6471         {
6472         case DW_ACCESS_private:
6473           fnp->is_private = 1;
6474           break;
6475         case DW_ACCESS_protected:
6476           fnp->is_protected = 1;
6477           break;
6478         }
6479     }
6480
6481   /* Check for artificial methods.  */
6482   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6483   if (attr && DW_UNSND (attr) != 0)
6484     fnp->is_artificial = 1;
6485
6486   /* Get index in virtual function table if it is a virtual member
6487      function.  For older versions of GCC, this is an offset in the
6488      appropriate virtual table, as specified by DW_AT_containing_type.
6489      For everyone else, it is an expression to be evaluated relative
6490      to the object address.  */
6491
6492   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6493   if (attr)
6494     {
6495       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6496         {
6497           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6498             {
6499               /* Old-style GCC.  */
6500               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6501             }
6502           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6503                    || (DW_BLOCK (attr)->size > 1
6504                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6505                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6506             {
6507               struct dwarf_block blk;
6508               int offset;
6509
6510               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6511                         ? 1 : 2);
6512               blk.size = DW_BLOCK (attr)->size - offset;
6513               blk.data = DW_BLOCK (attr)->data + offset;
6514               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6515               if ((fnp->voffset % cu->header.addr_size) != 0)
6516                 dwarf2_complex_location_expr_complaint ();
6517               else
6518                 fnp->voffset /= cu->header.addr_size;
6519               fnp->voffset += 2;
6520             }
6521           else
6522             dwarf2_complex_location_expr_complaint ();
6523
6524           if (!fnp->fcontext)
6525             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6526         }
6527       else if (attr_form_is_section_offset (attr))
6528         {
6529           dwarf2_complex_location_expr_complaint ();
6530         }
6531       else
6532         {
6533           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6534                                                  fieldname);
6535         }
6536     }
6537   else
6538     {
6539       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6540       if (attr && DW_UNSND (attr))
6541         {
6542           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6543           complaint (&symfile_complaints,
6544                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6545                      fieldname, die->offset);
6546           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6547           TYPE_CPLUS_DYNAMIC (type) = 1;
6548         }
6549     }
6550 }
6551
6552 /* Create the vector of member function fields, and attach it to the type.  */
6553
6554 static void
6555 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6556                                  struct dwarf2_cu *cu)
6557 {
6558   struct fnfieldlist *flp;
6559   int total_length = 0;
6560   int i;
6561
6562   if (cu->language == language_ada)
6563     error ("unexpected member functions in Ada type");
6564
6565   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6566   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6567     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6568
6569   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6570     {
6571       struct nextfnfield *nfp = flp->head;
6572       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6573       int k;
6574
6575       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6576       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6577       fn_flp->fn_fields = (struct fn_field *)
6578         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6579       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6580         fn_flp->fn_fields[k] = nfp->fnfield;
6581
6582       total_length += flp->length;
6583     }
6584
6585   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6586   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6587 }
6588
6589 /* Returns non-zero if NAME is the name of a vtable member in CU's
6590    language, zero otherwise.  */
6591 static int
6592 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6593 {
6594   static const char vptr[] = "_vptr";
6595   static const char vtable[] = "vtable";
6596
6597   /* Look for the C++ and Java forms of the vtable.  */
6598   if ((cu->language == language_java
6599        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6600        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6601        && is_cplus_marker (name[sizeof (vptr) - 1])))
6602     return 1;
6603
6604   return 0;
6605 }
6606
6607 /* GCC outputs unnamed structures that are really pointers to member
6608    functions, with the ABI-specified layout.  If TYPE describes
6609    such a structure, smash it into a member function type.
6610
6611    GCC shouldn't do this; it should just output pointer to member DIEs.
6612    This is GCC PR debug/28767.  */
6613
6614 static void
6615 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6616 {
6617   struct type *pfn_type, *domain_type, *new_type;
6618
6619   /* Check for a structure with no name and two children.  */
6620   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6621     return;
6622
6623   /* Check for __pfn and __delta members.  */
6624   if (TYPE_FIELD_NAME (type, 0) == NULL
6625       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6626       || TYPE_FIELD_NAME (type, 1) == NULL
6627       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6628     return;
6629
6630   /* Find the type of the method.  */
6631   pfn_type = TYPE_FIELD_TYPE (type, 0);
6632   if (pfn_type == NULL
6633       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6634       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6635     return;
6636
6637   /* Look for the "this" argument.  */
6638   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6639   if (TYPE_NFIELDS (pfn_type) == 0
6640       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6641       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6642     return;
6643
6644   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6645   new_type = alloc_type (objfile);
6646   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6647                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6648                         TYPE_VARARGS (pfn_type));
6649   smash_to_methodptr_type (type, new_type);
6650 }
6651
6652 /* Called when we find the DIE that starts a structure or union scope
6653    (definition) to create a type for the structure or union.  Fill in
6654    the type's name and general properties; the members will not be
6655    processed until process_structure_type.
6656
6657    NOTE: we need to call these functions regardless of whether or not the
6658    DIE has a DW_AT_name attribute, since it might be an anonymous
6659    structure or union.  This gets the type entered into our set of
6660    user defined types.
6661
6662    However, if the structure is incomplete (an opaque struct/union)
6663    then suppress creating a symbol table entry for it since gdb only
6664    wants to find the one with the complete definition.  Note that if
6665    it is complete, we just call new_symbol, which does it's own
6666    checking about whether the struct/union is anonymous or not (and
6667    suppresses creating a symbol table entry itself).  */
6668
6669 static struct type *
6670 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6671 {
6672   struct objfile *objfile = cu->objfile;
6673   struct type *type;
6674   struct attribute *attr;
6675   char *name;
6676
6677   /* If the definition of this type lives in .debug_types, read that type.
6678      Don't follow DW_AT_specification though, that will take us back up
6679      the chain and we want to go down.  */
6680   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6681   if (attr)
6682     {
6683       struct dwarf2_cu *type_cu = cu;
6684       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6685
6686       /* We could just recurse on read_structure_type, but we need to call
6687          get_die_type to ensure only one type for this DIE is created.
6688          This is important, for example, because for c++ classes we need
6689          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6690       type = read_type_die (type_die, type_cu);
6691
6692       /* TYPE_CU may not be the same as CU.
6693          Ensure TYPE is recorded in CU's type_hash table.  */
6694       return set_die_type (die, type, cu);
6695     }
6696
6697   type = alloc_type (objfile);
6698   INIT_CPLUS_SPECIFIC (type);
6699
6700   name = dwarf2_name (die, cu);
6701   if (name != NULL)
6702     {
6703       if (cu->language == language_cplus
6704           || cu->language == language_java)
6705         {
6706           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6707
6708           /* dwarf2_full_name might have already finished building the DIE's
6709              type.  If so, there is no need to continue.  */
6710           if (get_die_type (die, cu) != NULL)
6711             return get_die_type (die, cu);
6712
6713           TYPE_TAG_NAME (type) = full_name;
6714           if (die->tag == DW_TAG_structure_type
6715               || die->tag == DW_TAG_class_type)
6716             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6717         }
6718       else
6719         {
6720           /* The name is already allocated along with this objfile, so
6721              we don't need to duplicate it for the type.  */
6722           TYPE_TAG_NAME (type) = (char *) name;
6723           if (die->tag == DW_TAG_class_type)
6724             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6725         }
6726     }
6727
6728   if (die->tag == DW_TAG_structure_type)
6729     {
6730       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6731     }
6732   else if (die->tag == DW_TAG_union_type)
6733     {
6734       TYPE_CODE (type) = TYPE_CODE_UNION;
6735     }
6736   else
6737     {
6738       TYPE_CODE (type) = TYPE_CODE_CLASS;
6739     }
6740
6741   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6742     TYPE_DECLARED_CLASS (type) = 1;
6743
6744   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6745   if (attr)
6746     {
6747       TYPE_LENGTH (type) = DW_UNSND (attr);
6748     }
6749   else
6750     {
6751       TYPE_LENGTH (type) = 0;
6752     }
6753
6754   TYPE_STUB_SUPPORTED (type) = 1;
6755   if (die_is_declaration (die, cu))
6756     TYPE_STUB (type) = 1;
6757   else if (attr == NULL && die->child == NULL
6758            && producer_is_realview (cu->producer))
6759     /* RealView does not output the required DW_AT_declaration
6760        on incomplete types.  */
6761     TYPE_STUB (type) = 1;
6762
6763   /* We need to add the type field to the die immediately so we don't
6764      infinitely recurse when dealing with pointers to the structure
6765      type within the structure itself. */
6766   set_die_type (die, type, cu);
6767
6768   /* set_die_type should be already done.  */
6769   set_descriptive_type (type, die, cu);
6770
6771   return type;
6772 }
6773
6774 /* Finish creating a structure or union type, including filling in
6775    its members and creating a symbol for it.  */
6776
6777 static void
6778 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6779 {
6780   struct objfile *objfile = cu->objfile;
6781   struct die_info *child_die = die->child;
6782   struct type *type;
6783
6784   type = get_die_type (die, cu);
6785   if (type == NULL)
6786     type = read_structure_type (die, cu);
6787
6788   if (die->child != NULL && ! die_is_declaration (die, cu))
6789     {
6790       struct field_info fi;
6791       struct die_info *child_die;
6792       VEC (symbolp) *template_args = NULL;
6793       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6794
6795       memset (&fi, 0, sizeof (struct field_info));
6796
6797       child_die = die->child;
6798
6799       while (child_die && child_die->tag)
6800         {
6801           if (child_die->tag == DW_TAG_member
6802               || child_die->tag == DW_TAG_variable)
6803             {
6804               /* NOTE: carlton/2002-11-05: A C++ static data member
6805                  should be a DW_TAG_member that is a declaration, but
6806                  all versions of G++ as of this writing (so through at
6807                  least 3.2.1) incorrectly generate DW_TAG_variable
6808                  tags for them instead.  */
6809               dwarf2_add_field (&fi, child_die, cu);
6810             }
6811           else if (child_die->tag == DW_TAG_subprogram)
6812             {
6813               /* C++ member function. */
6814               dwarf2_add_member_fn (&fi, child_die, type, cu);
6815             }
6816           else if (child_die->tag == DW_TAG_inheritance)
6817             {
6818               /* C++ base class field.  */
6819               dwarf2_add_field (&fi, child_die, cu);
6820             }
6821           else if (child_die->tag == DW_TAG_typedef)
6822             dwarf2_add_typedef (&fi, child_die, cu);
6823           else if (child_die->tag == DW_TAG_template_type_param
6824                    || child_die->tag == DW_TAG_template_value_param)
6825             {
6826               struct symbol *arg = new_symbol (child_die, NULL, cu);
6827
6828               VEC_safe_push (symbolp, template_args, arg);
6829             }
6830
6831           child_die = sibling_die (child_die);
6832         }
6833
6834       /* Attach template arguments to type.  */
6835       if (! VEC_empty (symbolp, template_args))
6836         {
6837           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6838           TYPE_N_TEMPLATE_ARGUMENTS (type)
6839             = VEC_length (symbolp, template_args);
6840           TYPE_TEMPLATE_ARGUMENTS (type)
6841             = obstack_alloc (&objfile->objfile_obstack,
6842                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
6843                               * sizeof (struct symbol *)));
6844           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6845                   VEC_address (symbolp, template_args),
6846                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
6847                    * sizeof (struct symbol *)));
6848           VEC_free (symbolp, template_args);
6849         }
6850
6851       /* Attach fields and member functions to the type.  */
6852       if (fi.nfields)
6853         dwarf2_attach_fields_to_type (&fi, type, cu);
6854       if (fi.nfnfields)
6855         {
6856           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6857
6858           /* Get the type which refers to the base class (possibly this
6859              class itself) which contains the vtable pointer for the current
6860              class from the DW_AT_containing_type attribute.  This use of
6861              DW_AT_containing_type is a GNU extension.  */
6862
6863           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6864             {
6865               struct type *t = die_containing_type (die, cu);
6866
6867               TYPE_VPTR_BASETYPE (type) = t;
6868               if (type == t)
6869                 {
6870                   int i;
6871
6872                   /* Our own class provides vtbl ptr.  */
6873                   for (i = TYPE_NFIELDS (t) - 1;
6874                        i >= TYPE_N_BASECLASSES (t);
6875                        --i)
6876                     {
6877                       char *fieldname = TYPE_FIELD_NAME (t, i);
6878
6879                       if (is_vtable_name (fieldname, cu))
6880                         {
6881                           TYPE_VPTR_FIELDNO (type) = i;
6882                           break;
6883                         }
6884                     }
6885
6886                   /* Complain if virtual function table field not found.  */
6887                   if (i < TYPE_N_BASECLASSES (t))
6888                     complaint (&symfile_complaints,
6889                                _("virtual function table pointer not found when defining class '%s'"),
6890                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6891                                "");
6892                 }
6893               else
6894                 {
6895                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6896                 }
6897             }
6898           else if (cu->producer
6899                    && strncmp (cu->producer,
6900                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6901             {
6902               /* The IBM XLC compiler does not provide direct indication
6903                  of the containing type, but the vtable pointer is
6904                  always named __vfp.  */
6905
6906               int i;
6907
6908               for (i = TYPE_NFIELDS (type) - 1;
6909                    i >= TYPE_N_BASECLASSES (type);
6910                    --i)
6911                 {
6912                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6913                     {
6914                       TYPE_VPTR_FIELDNO (type) = i;
6915                       TYPE_VPTR_BASETYPE (type) = type;
6916                       break;
6917                     }
6918                 }
6919             }
6920         }
6921
6922       /* Copy fi.typedef_field_list linked list elements content into the
6923          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6924       if (fi.typedef_field_list)
6925         {
6926           int i = fi.typedef_field_list_count;
6927
6928           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6929           TYPE_TYPEDEF_FIELD_ARRAY (type)
6930             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6931           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6932
6933           /* Reverse the list order to keep the debug info elements order.  */
6934           while (--i >= 0)
6935             {
6936               struct typedef_field *dest, *src;
6937
6938               dest = &TYPE_TYPEDEF_FIELD (type, i);
6939               src = &fi.typedef_field_list->field;
6940               fi.typedef_field_list = fi.typedef_field_list->next;
6941               *dest = *src;
6942             }
6943         }
6944
6945       do_cleanups (back_to);
6946     }
6947
6948   quirk_gcc_member_function_pointer (type, cu->objfile);
6949
6950   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6951      snapshots) has been known to create a die giving a declaration
6952      for a class that has, as a child, a die giving a definition for a
6953      nested class.  So we have to process our children even if the
6954      current die is a declaration.  Normally, of course, a declaration
6955      won't have any children at all.  */
6956
6957   while (child_die != NULL && child_die->tag)
6958     {
6959       if (child_die->tag == DW_TAG_member
6960           || child_die->tag == DW_TAG_variable
6961           || child_die->tag == DW_TAG_inheritance
6962           || child_die->tag == DW_TAG_template_value_param
6963           || child_die->tag == DW_TAG_template_type_param)
6964         {
6965           /* Do nothing.  */
6966         }
6967       else
6968         process_die (child_die, cu);
6969
6970       child_die = sibling_die (child_die);
6971     }
6972
6973   /* Do not consider external references.  According to the DWARF standard,
6974      these DIEs are identified by the fact that they have no byte_size
6975      attribute, and a declaration attribute.  */
6976   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6977       || !die_is_declaration (die, cu))
6978     new_symbol (die, type, cu);
6979 }
6980
6981 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6982    complete the type's fields yet, or create any symbols.  */
6983
6984 static struct type *
6985 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6986 {
6987   struct objfile *objfile = cu->objfile;
6988   struct type *type;
6989   struct attribute *attr;
6990   const char *name;
6991
6992   /* If the definition of this type lives in .debug_types, read that type.
6993      Don't follow DW_AT_specification though, that will take us back up
6994      the chain and we want to go down.  */
6995   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6996   if (attr)
6997     {
6998       struct dwarf2_cu *type_cu = cu;
6999       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7000
7001       type = read_type_die (type_die, type_cu);
7002
7003       /* TYPE_CU may not be the same as CU.
7004          Ensure TYPE is recorded in CU's type_hash table.  */
7005       return set_die_type (die, type, cu);
7006     }
7007
7008   type = alloc_type (objfile);
7009
7010   TYPE_CODE (type) = TYPE_CODE_ENUM;
7011   name = dwarf2_full_name (NULL, die, cu);
7012   if (name != NULL)
7013     TYPE_TAG_NAME (type) = (char *) name;
7014
7015   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7016   if (attr)
7017     {
7018       TYPE_LENGTH (type) = DW_UNSND (attr);
7019     }
7020   else
7021     {
7022       TYPE_LENGTH (type) = 0;
7023     }
7024
7025   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7026      declared as private in the package spec, and then defined only
7027      inside the package body.  Such types are known as Taft Amendment
7028      Types.  When another package uses such a type, an incomplete DIE
7029      may be generated by the compiler.  */
7030   if (die_is_declaration (die, cu))
7031     TYPE_STUB (type) = 1;
7032
7033   return set_die_type (die, type, cu);
7034 }
7035
7036 /* Given a pointer to a die which begins an enumeration, process all
7037    the dies that define the members of the enumeration, and create the
7038    symbol for the enumeration type.
7039
7040    NOTE: We reverse the order of the element list.  */
7041
7042 static void
7043 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7044 {
7045   struct type *this_type;
7046
7047   this_type = get_die_type (die, cu);
7048   if (this_type == NULL)
7049     this_type = read_enumeration_type (die, cu);
7050
7051   if (die->child != NULL)
7052     {
7053       struct die_info *child_die;
7054       struct symbol *sym;
7055       struct field *fields = NULL;
7056       int num_fields = 0;
7057       int unsigned_enum = 1;
7058       char *name;
7059
7060       child_die = die->child;
7061       while (child_die && child_die->tag)
7062         {
7063           if (child_die->tag != DW_TAG_enumerator)
7064             {
7065               process_die (child_die, cu);
7066             }
7067           else
7068             {
7069               name = dwarf2_name (child_die, cu);
7070               if (name)
7071                 {
7072                   sym = new_symbol (child_die, this_type, cu);
7073                   if (SYMBOL_VALUE (sym) < 0)
7074                     unsigned_enum = 0;
7075
7076                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7077                     {
7078                       fields = (struct field *)
7079                         xrealloc (fields,
7080                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7081                                   * sizeof (struct field));
7082                     }
7083
7084                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7085                   FIELD_TYPE (fields[num_fields]) = NULL;
7086                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7087                   FIELD_BITSIZE (fields[num_fields]) = 0;
7088
7089                   num_fields++;
7090                 }
7091             }
7092
7093           child_die = sibling_die (child_die);
7094         }
7095
7096       if (num_fields)
7097         {
7098           TYPE_NFIELDS (this_type) = num_fields;
7099           TYPE_FIELDS (this_type) = (struct field *)
7100             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7101           memcpy (TYPE_FIELDS (this_type), fields,
7102                   sizeof (struct field) * num_fields);
7103           xfree (fields);
7104         }
7105       if (unsigned_enum)
7106         TYPE_UNSIGNED (this_type) = 1;
7107     }
7108
7109   new_symbol (die, this_type, cu);
7110 }
7111
7112 /* Extract all information from a DW_TAG_array_type DIE and put it in
7113    the DIE's type field.  For now, this only handles one dimensional
7114    arrays.  */
7115
7116 static struct type *
7117 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7118 {
7119   struct objfile *objfile = cu->objfile;
7120   struct die_info *child_die;
7121   struct type *type;
7122   struct type *element_type, *range_type, *index_type;
7123   struct type **range_types = NULL;
7124   struct attribute *attr;
7125   int ndim = 0;
7126   struct cleanup *back_to;
7127   char *name;
7128
7129   element_type = die_type (die, cu);
7130
7131   /* The die_type call above may have already set the type for this DIE.  */
7132   type = get_die_type (die, cu);
7133   if (type)
7134     return type;
7135
7136   /* Irix 6.2 native cc creates array types without children for
7137      arrays with unspecified length.  */
7138   if (die->child == NULL)
7139     {
7140       index_type = objfile_type (objfile)->builtin_int;
7141       range_type = create_range_type (NULL, index_type, 0, -1);
7142       type = create_array_type (NULL, element_type, range_type);
7143       return set_die_type (die, type, cu);
7144     }
7145
7146   back_to = make_cleanup (null_cleanup, NULL);
7147   child_die = die->child;
7148   while (child_die && child_die->tag)
7149     {
7150       if (child_die->tag == DW_TAG_subrange_type)
7151         {
7152           struct type *child_type = read_type_die (child_die, cu);
7153
7154           if (child_type != NULL)
7155             {
7156               /* The range type was succesfully read. Save it for
7157                  the array type creation.  */
7158               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7159                 {
7160                   range_types = (struct type **)
7161                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7162                               * sizeof (struct type *));
7163                   if (ndim == 0)
7164                     make_cleanup (free_current_contents, &range_types);
7165                 }
7166               range_types[ndim++] = child_type;
7167             }
7168         }
7169       child_die = sibling_die (child_die);
7170     }
7171
7172   /* Dwarf2 dimensions are output from left to right, create the
7173      necessary array types in backwards order.  */
7174
7175   type = element_type;
7176
7177   if (read_array_order (die, cu) == DW_ORD_col_major)
7178     {
7179       int i = 0;
7180
7181       while (i < ndim)
7182         type = create_array_type (NULL, type, range_types[i++]);
7183     }
7184   else
7185     {
7186       while (ndim-- > 0)
7187         type = create_array_type (NULL, type, range_types[ndim]);
7188     }
7189
7190   /* Understand Dwarf2 support for vector types (like they occur on
7191      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7192      array type.  This is not part of the Dwarf2/3 standard yet, but a
7193      custom vendor extension.  The main difference between a regular
7194      array and the vector variant is that vectors are passed by value
7195      to functions.  */
7196   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7197   if (attr)
7198     make_vector_type (type);
7199
7200   name = dwarf2_name (die, cu);
7201   if (name)
7202     TYPE_NAME (type) = name;
7203
7204   /* Install the type in the die. */
7205   set_die_type (die, type, cu);
7206
7207   /* set_die_type should be already done.  */
7208   set_descriptive_type (type, die, cu);
7209
7210   do_cleanups (back_to);
7211
7212   return type;
7213 }
7214
7215 static enum dwarf_array_dim_ordering
7216 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7217 {
7218   struct attribute *attr;
7219
7220   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7221
7222   if (attr) return DW_SND (attr);
7223
7224   /*
7225     GNU F77 is a special case, as at 08/2004 array type info is the
7226     opposite order to the dwarf2 specification, but data is still
7227     laid out as per normal fortran.
7228
7229     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7230     version checking.
7231   */
7232
7233   if (cu->language == language_fortran
7234       && cu->producer && strstr (cu->producer, "GNU F77"))
7235     {
7236       return DW_ORD_row_major;
7237     }
7238
7239   switch (cu->language_defn->la_array_ordering)
7240     {
7241     case array_column_major:
7242       return DW_ORD_col_major;
7243     case array_row_major:
7244     default:
7245       return DW_ORD_row_major;
7246     };
7247 }
7248
7249 /* Extract all information from a DW_TAG_set_type DIE and put it in
7250    the DIE's type field. */
7251
7252 static struct type *
7253 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7254 {
7255   struct type *domain_type, *set_type;
7256   struct attribute *attr;
7257
7258   domain_type = die_type (die, cu);
7259
7260   /* The die_type call above may have already set the type for this DIE.  */
7261   set_type = get_die_type (die, cu);
7262   if (set_type)
7263     return set_type;
7264
7265   set_type = create_set_type (NULL, domain_type);
7266
7267   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7268   if (attr)
7269     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7270
7271   return set_die_type (die, set_type, cu);
7272 }
7273
7274 /* First cut: install each common block member as a global variable.  */
7275
7276 static void
7277 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7278 {
7279   struct die_info *child_die;
7280   struct attribute *attr;
7281   struct symbol *sym;
7282   CORE_ADDR base = (CORE_ADDR) 0;
7283
7284   attr = dwarf2_attr (die, DW_AT_location, cu);
7285   if (attr)
7286     {
7287       /* Support the .debug_loc offsets */
7288       if (attr_form_is_block (attr))
7289         {
7290           base = decode_locdesc (DW_BLOCK (attr), cu);
7291         }
7292       else if (attr_form_is_section_offset (attr))
7293         {
7294           dwarf2_complex_location_expr_complaint ();
7295         }
7296       else
7297         {
7298           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7299                                                  "common block member");
7300         }
7301     }
7302   if (die->child != NULL)
7303     {
7304       child_die = die->child;
7305       while (child_die && child_die->tag)
7306         {
7307           sym = new_symbol (child_die, NULL, cu);
7308           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7309           if (attr)
7310             {
7311               CORE_ADDR byte_offset = 0;
7312
7313               if (attr_form_is_section_offset (attr))
7314                 dwarf2_complex_location_expr_complaint ();
7315               else if (attr_form_is_constant (attr))
7316                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7317               else if (attr_form_is_block (attr))
7318                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7319               else
7320                 dwarf2_complex_location_expr_complaint ();
7321
7322               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7323               add_symbol_to_list (sym, &global_symbols);
7324             }
7325           child_die = sibling_die (child_die);
7326         }
7327     }
7328 }
7329
7330 /* Create a type for a C++ namespace.  */
7331
7332 static struct type *
7333 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7334 {
7335   struct objfile *objfile = cu->objfile;
7336   const char *previous_prefix, *name;
7337   int is_anonymous;
7338   struct type *type;
7339
7340   /* For extensions, reuse the type of the original namespace.  */
7341   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7342     {
7343       struct die_info *ext_die;
7344       struct dwarf2_cu *ext_cu = cu;
7345
7346       ext_die = dwarf2_extension (die, &ext_cu);
7347       type = read_type_die (ext_die, ext_cu);
7348
7349       /* EXT_CU may not be the same as CU.
7350          Ensure TYPE is recorded in CU's type_hash table.  */
7351       return set_die_type (die, type, cu);
7352     }
7353
7354   name = namespace_name (die, &is_anonymous, cu);
7355
7356   /* Now build the name of the current namespace.  */
7357
7358   previous_prefix = determine_prefix (die, cu);
7359   if (previous_prefix[0] != '\0')
7360     name = typename_concat (&objfile->objfile_obstack,
7361                             previous_prefix, name, 0, cu);
7362
7363   /* Create the type.  */
7364   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7365                     objfile);
7366   TYPE_NAME (type) = (char *) name;
7367   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7368
7369   return set_die_type (die, type, cu);
7370 }
7371
7372 /* Read a C++ namespace.  */
7373
7374 static void
7375 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7376 {
7377   struct objfile *objfile = cu->objfile;
7378   const char *name;
7379   int is_anonymous;
7380
7381   /* Add a symbol associated to this if we haven't seen the namespace
7382      before.  Also, add a using directive if it's an anonymous
7383      namespace.  */
7384
7385   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7386     {
7387       struct type *type;
7388
7389       type = read_type_die (die, cu);
7390       new_symbol (die, type, cu);
7391
7392       name = namespace_name (die, &is_anonymous, cu);
7393       if (is_anonymous)
7394         {
7395           const char *previous_prefix = determine_prefix (die, cu);
7396
7397           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7398                                   NULL, &objfile->objfile_obstack);
7399         }
7400     }
7401
7402   if (die->child != NULL)
7403     {
7404       struct die_info *child_die = die->child;
7405
7406       while (child_die && child_die->tag)
7407         {
7408           process_die (child_die, cu);
7409           child_die = sibling_die (child_die);
7410         }
7411     }
7412 }
7413
7414 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7415    imported module.  Still we need that type as local Fortran "use ... only"
7416    declaration imports depend on the created type in determine_prefix.  */
7417
7418 static struct type *
7419 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7420 {
7421   struct objfile *objfile = cu->objfile;
7422   char *module_name;
7423   struct type *type;
7424
7425   module_name = dwarf2_name (die, cu);
7426   if (!module_name)
7427     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7428                die->offset);
7429   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7430
7431   /* determine_prefix uses TYPE_TAG_NAME.  */
7432   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7433
7434   return set_die_type (die, type, cu);
7435 }
7436
7437 /* Read a Fortran module.  */
7438
7439 static void
7440 read_module (struct die_info *die, struct dwarf2_cu *cu)
7441 {
7442   struct die_info *child_die = die->child;
7443
7444   while (child_die && child_die->tag)
7445     {
7446       process_die (child_die, cu);
7447       child_die = sibling_die (child_die);
7448     }
7449 }
7450
7451 /* Return the name of the namespace represented by DIE.  Set
7452    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7453    namespace.  */
7454
7455 static const char *
7456 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7457 {
7458   struct die_info *current_die;
7459   const char *name = NULL;
7460
7461   /* Loop through the extensions until we find a name.  */
7462
7463   for (current_die = die;
7464        current_die != NULL;
7465        current_die = dwarf2_extension (die, &cu))
7466     {
7467       name = dwarf2_name (current_die, cu);
7468       if (name != NULL)
7469         break;
7470     }
7471
7472   /* Is it an anonymous namespace?  */
7473
7474   *is_anonymous = (name == NULL);
7475   if (*is_anonymous)
7476     name = "(anonymous namespace)";
7477
7478   return name;
7479 }
7480
7481 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7482    the user defined type vector.  */
7483
7484 static struct type *
7485 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7486 {
7487   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7488   struct comp_unit_head *cu_header = &cu->header;
7489   struct type *type;
7490   struct attribute *attr_byte_size;
7491   struct attribute *attr_address_class;
7492   int byte_size, addr_class;
7493   struct type *target_type;
7494
7495   target_type = die_type (die, cu);
7496
7497   /* The die_type call above may have already set the type for this DIE.  */
7498   type = get_die_type (die, cu);
7499   if (type)
7500     return type;
7501
7502   type = lookup_pointer_type (target_type);
7503
7504   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7505   if (attr_byte_size)
7506     byte_size = DW_UNSND (attr_byte_size);
7507   else
7508     byte_size = cu_header->addr_size;
7509
7510   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7511   if (attr_address_class)
7512     addr_class = DW_UNSND (attr_address_class);
7513   else
7514     addr_class = DW_ADDR_none;
7515
7516   /* If the pointer size or address class is different than the
7517      default, create a type variant marked as such and set the
7518      length accordingly.  */
7519   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7520     {
7521       if (gdbarch_address_class_type_flags_p (gdbarch))
7522         {
7523           int type_flags;
7524
7525           type_flags = gdbarch_address_class_type_flags
7526                          (gdbarch, byte_size, addr_class);
7527           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7528                       == 0);
7529           type = make_type_with_address_space (type, type_flags);
7530         }
7531       else if (TYPE_LENGTH (type) != byte_size)
7532         {
7533           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7534         }
7535       else
7536         {
7537           /* Should we also complain about unhandled address classes?  */
7538         }
7539     }
7540
7541   TYPE_LENGTH (type) = byte_size;
7542   return set_die_type (die, type, cu);
7543 }
7544
7545 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7546    the user defined type vector.  */
7547
7548 static struct type *
7549 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7550 {
7551   struct type *type;
7552   struct type *to_type;
7553   struct type *domain;
7554
7555   to_type = die_type (die, cu);
7556   domain = die_containing_type (die, cu);
7557
7558   /* The calls above may have already set the type for this DIE.  */
7559   type = get_die_type (die, cu);
7560   if (type)
7561     return type;
7562
7563   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7564     type = lookup_methodptr_type (to_type);
7565   else
7566     type = lookup_memberptr_type (to_type, domain);
7567
7568   return set_die_type (die, type, cu);
7569 }
7570
7571 /* Extract all information from a DW_TAG_reference_type DIE and add to
7572    the user defined type vector.  */
7573
7574 static struct type *
7575 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7576 {
7577   struct comp_unit_head *cu_header = &cu->header;
7578   struct type *type, *target_type;
7579   struct attribute *attr;
7580
7581   target_type = die_type (die, cu);
7582
7583   /* The die_type call above may have already set the type for this DIE.  */
7584   type = get_die_type (die, cu);
7585   if (type)
7586     return type;
7587
7588   type = lookup_reference_type (target_type);
7589   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7590   if (attr)
7591     {
7592       TYPE_LENGTH (type) = DW_UNSND (attr);
7593     }
7594   else
7595     {
7596       TYPE_LENGTH (type) = cu_header->addr_size;
7597     }
7598   return set_die_type (die, type, cu);
7599 }
7600
7601 static struct type *
7602 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7603 {
7604   struct type *base_type, *cv_type;
7605
7606   base_type = die_type (die, cu);
7607
7608   /* The die_type call above may have already set the type for this DIE.  */
7609   cv_type = get_die_type (die, cu);
7610   if (cv_type)
7611     return cv_type;
7612
7613   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7614   return set_die_type (die, cv_type, cu);
7615 }
7616
7617 static struct type *
7618 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7619 {
7620   struct type *base_type, *cv_type;
7621
7622   base_type = die_type (die, cu);
7623
7624   /* The die_type call above may have already set the type for this DIE.  */
7625   cv_type = get_die_type (die, cu);
7626   if (cv_type)
7627     return cv_type;
7628
7629   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7630   return set_die_type (die, cv_type, cu);
7631 }
7632
7633 /* Extract all information from a DW_TAG_string_type DIE and add to
7634    the user defined type vector.  It isn't really a user defined type,
7635    but it behaves like one, with other DIE's using an AT_user_def_type
7636    attribute to reference it.  */
7637
7638 static struct type *
7639 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7640 {
7641   struct objfile *objfile = cu->objfile;
7642   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7643   struct type *type, *range_type, *index_type, *char_type;
7644   struct attribute *attr;
7645   unsigned int length;
7646
7647   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7648   if (attr)
7649     {
7650       length = DW_UNSND (attr);
7651     }
7652   else
7653     {
7654       /* check for the DW_AT_byte_size attribute */
7655       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7656       if (attr)
7657         {
7658           length = DW_UNSND (attr);
7659         }
7660       else
7661         {
7662           length = 1;
7663         }
7664     }
7665
7666   index_type = objfile_type (objfile)->builtin_int;
7667   range_type = create_range_type (NULL, index_type, 1, length);
7668   char_type = language_string_char_type (cu->language_defn, gdbarch);
7669   type = create_string_type (NULL, char_type, range_type);
7670
7671   return set_die_type (die, type, cu);
7672 }
7673
7674 /* Handle DIES due to C code like:
7675
7676    struct foo
7677    {
7678    int (*funcp)(int a, long l);
7679    int b;
7680    };
7681
7682    ('funcp' generates a DW_TAG_subroutine_type DIE)
7683  */
7684
7685 static struct type *
7686 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7687 {
7688   struct type *type;            /* Type that this function returns */
7689   struct type *ftype;           /* Function that returns above type */
7690   struct attribute *attr;
7691
7692   type = die_type (die, cu);
7693
7694   /* The die_type call above may have already set the type for this DIE.  */
7695   ftype = get_die_type (die, cu);
7696   if (ftype)
7697     return ftype;
7698
7699   ftype = lookup_function_type (type);
7700
7701   /* All functions in C++, Pascal and Java have prototypes.  */
7702   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7703   if ((attr && (DW_UNSND (attr) != 0))
7704       || cu->language == language_cplus
7705       || cu->language == language_java
7706       || cu->language == language_pascal)
7707     TYPE_PROTOTYPED (ftype) = 1;
7708   else if (producer_is_realview (cu->producer))
7709     /* RealView does not emit DW_AT_prototyped.  We can not
7710        distinguish prototyped and unprototyped functions; default to
7711        prototyped, since that is more common in modern code (and
7712        RealView warns about unprototyped functions).  */
7713     TYPE_PROTOTYPED (ftype) = 1;
7714
7715   /* Store the calling convention in the type if it's available in
7716      the subroutine die.  Otherwise set the calling convention to
7717      the default value DW_CC_normal.  */
7718   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7719   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7720
7721   /* We need to add the subroutine type to the die immediately so
7722      we don't infinitely recurse when dealing with parameters
7723      declared as the same subroutine type. */
7724   set_die_type (die, ftype, cu);
7725
7726   if (die->child != NULL)
7727     {
7728       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7729       struct die_info *child_die;
7730       int nparams, iparams;
7731
7732       /* Count the number of parameters.
7733          FIXME: GDB currently ignores vararg functions, but knows about
7734          vararg member functions.  */
7735       nparams = 0;
7736       child_die = die->child;
7737       while (child_die && child_die->tag)
7738         {
7739           if (child_die->tag == DW_TAG_formal_parameter)
7740             nparams++;
7741           else if (child_die->tag == DW_TAG_unspecified_parameters)
7742             TYPE_VARARGS (ftype) = 1;
7743           child_die = sibling_die (child_die);
7744         }
7745
7746       /* Allocate storage for parameters and fill them in.  */
7747       TYPE_NFIELDS (ftype) = nparams;
7748       TYPE_FIELDS (ftype) = (struct field *)
7749         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7750
7751       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7752          even if we error out during the parameters reading below.  */
7753       for (iparams = 0; iparams < nparams; iparams++)
7754         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7755
7756       iparams = 0;
7757       child_die = die->child;
7758       while (child_die && child_die->tag)
7759         {
7760           if (child_die->tag == DW_TAG_formal_parameter)
7761             {
7762               struct type *arg_type;
7763
7764               /* DWARF version 2 has no clean way to discern C++
7765                  static and non-static member functions.  G++ helps
7766                  GDB by marking the first parameter for non-static
7767                  member functions (which is the this pointer) as
7768                  artificial.  We pass this information to
7769                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7770
7771                  DWARF version 3 added DW_AT_object_pointer, which GCC
7772                  4.5 does not yet generate.  */
7773               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7774               if (attr)
7775                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7776               else
7777                 {
7778                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7779
7780                   /* GCC/43521: In java, the formal parameter
7781                      "this" is sometimes not marked with DW_AT_artificial.  */
7782                   if (cu->language == language_java)
7783                     {
7784                       const char *name = dwarf2_name (child_die, cu);
7785
7786                       if (name && !strcmp (name, "this"))
7787                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7788                     }
7789                 }
7790               arg_type = die_type (child_die, cu);
7791
7792               /* RealView does not mark THIS as const, which the testsuite
7793                  expects.  GCC marks THIS as const in method definitions,
7794                  but not in the class specifications (GCC PR 43053).  */
7795               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7796                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7797                 {
7798                   int is_this = 0;
7799                   struct dwarf2_cu *arg_cu = cu;
7800                   const char *name = dwarf2_name (child_die, cu);
7801
7802                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7803                   if (attr)
7804                     {
7805                       /* If the compiler emits this, use it.  */
7806                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
7807                         is_this = 1;
7808                     }
7809                   else if (name && strcmp (name, "this") == 0)
7810                     /* Function definitions will have the argument names.  */
7811                     is_this = 1;
7812                   else if (name == NULL && iparams == 0)
7813                     /* Declarations may not have the names, so like
7814                        elsewhere in GDB, assume an artificial first
7815                        argument is "this".  */
7816                     is_this = 1;
7817
7818                   if (is_this)
7819                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7820                                              arg_type, 0);
7821                 }
7822
7823               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7824               iparams++;
7825             }
7826           child_die = sibling_die (child_die);
7827         }
7828     }
7829
7830   return ftype;
7831 }
7832
7833 static struct type *
7834 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7835 {
7836   struct objfile *objfile = cu->objfile;
7837   const char *name = NULL;
7838   struct type *this_type;
7839
7840   name = dwarf2_full_name (NULL, die, cu);
7841   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7842                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7843   TYPE_NAME (this_type) = (char *) name;
7844   set_die_type (die, this_type, cu);
7845   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7846   return this_type;
7847 }
7848
7849 /* Find a representation of a given base type and install
7850    it in the TYPE field of the die.  */
7851
7852 static struct type *
7853 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7854 {
7855   struct objfile *objfile = cu->objfile;
7856   struct type *type;
7857   struct attribute *attr;
7858   int encoding = 0, size = 0;
7859   char *name;
7860   enum type_code code = TYPE_CODE_INT;
7861   int type_flags = 0;
7862   struct type *target_type = NULL;
7863
7864   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7865   if (attr)
7866     {
7867       encoding = DW_UNSND (attr);
7868     }
7869   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7870   if (attr)
7871     {
7872       size = DW_UNSND (attr);
7873     }
7874   name = dwarf2_name (die, cu);
7875   if (!name)
7876     {
7877       complaint (&symfile_complaints,
7878                  _("DW_AT_name missing from DW_TAG_base_type"));
7879     }
7880
7881   switch (encoding)
7882     {
7883       case DW_ATE_address:
7884         /* Turn DW_ATE_address into a void * pointer.  */
7885         code = TYPE_CODE_PTR;
7886         type_flags |= TYPE_FLAG_UNSIGNED;
7887         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7888         break;
7889       case DW_ATE_boolean:
7890         code = TYPE_CODE_BOOL;
7891         type_flags |= TYPE_FLAG_UNSIGNED;
7892         break;
7893       case DW_ATE_complex_float:
7894         code = TYPE_CODE_COMPLEX;
7895         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7896         break;
7897       case DW_ATE_decimal_float:
7898         code = TYPE_CODE_DECFLOAT;
7899         break;
7900       case DW_ATE_float:
7901         code = TYPE_CODE_FLT;
7902         break;
7903       case DW_ATE_signed:
7904         break;
7905       case DW_ATE_unsigned:
7906         type_flags |= TYPE_FLAG_UNSIGNED;
7907         break;
7908       case DW_ATE_signed_char:
7909         if (cu->language == language_ada || cu->language == language_m2
7910             || cu->language == language_pascal)
7911           code = TYPE_CODE_CHAR;
7912         break;
7913       case DW_ATE_unsigned_char:
7914         if (cu->language == language_ada || cu->language == language_m2
7915             || cu->language == language_pascal)
7916           code = TYPE_CODE_CHAR;
7917         type_flags |= TYPE_FLAG_UNSIGNED;
7918         break;
7919       case DW_ATE_UTF:
7920         /* We just treat this as an integer and then recognize the
7921            type by name elsewhere.  */
7922         break;
7923
7924       default:
7925         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7926                    dwarf_type_encoding_name (encoding));
7927         break;
7928     }
7929
7930   type = init_type (code, size, type_flags, NULL, objfile);
7931   TYPE_NAME (type) = name;
7932   TYPE_TARGET_TYPE (type) = target_type;
7933
7934   if (name && strcmp (name, "char") == 0)
7935     TYPE_NOSIGN (type) = 1;
7936
7937   return set_die_type (die, type, cu);
7938 }
7939
7940 /* Read the given DW_AT_subrange DIE.  */
7941
7942 static struct type *
7943 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7944 {
7945   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7946   struct type *base_type;
7947   struct type *range_type;
7948   struct attribute *attr;
7949   LONGEST low = 0;
7950   LONGEST high = -1;
7951   char *name;
7952   LONGEST negative_mask;
7953
7954   base_type = die_type (die, cu);
7955   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7956   check_typedef (base_type);
7957
7958   /* The die_type call above may have already set the type for this DIE.  */
7959   range_type = get_die_type (die, cu);
7960   if (range_type)
7961     return range_type;
7962
7963   if (cu->language == language_fortran)
7964     {
7965       /* FORTRAN implies a lower bound of 1, if not given.  */
7966       low = 1;
7967     }
7968
7969   /* FIXME: For variable sized arrays either of these could be
7970      a variable rather than a constant value.  We'll allow it,
7971      but we don't know how to handle it.  */
7972   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7973   if (attr)
7974     low = dwarf2_get_attr_constant_value (attr, 0);
7975
7976   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7977   if (attr)
7978     {
7979       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7980         {
7981           /* GCC encodes arrays with unspecified or dynamic length
7982              with a DW_FORM_block1 attribute or a reference attribute.
7983              FIXME: GDB does not yet know how to handle dynamic
7984              arrays properly, treat them as arrays with unspecified
7985              length for now.
7986
7987              FIXME: jimb/2003-09-22: GDB does not really know
7988              how to handle arrays of unspecified length
7989              either; we just represent them as zero-length
7990              arrays.  Choose an appropriate upper bound given
7991              the lower bound we've computed above.  */
7992           high = low - 1;
7993         }
7994       else
7995         high = dwarf2_get_attr_constant_value (attr, 1);
7996     }
7997   else
7998     {
7999       attr = dwarf2_attr (die, DW_AT_count, cu);
8000       if (attr)
8001         {
8002           int count = dwarf2_get_attr_constant_value (attr, 1);
8003           high = low + count - 1;
8004         }
8005     }
8006
8007   /* Dwarf-2 specifications explicitly allows to create subrange types
8008      without specifying a base type.
8009      In that case, the base type must be set to the type of
8010      the lower bound, upper bound or count, in that order, if any of these
8011      three attributes references an object that has a type.
8012      If no base type is found, the Dwarf-2 specifications say that
8013      a signed integer type of size equal to the size of an address should
8014      be used.
8015      For the following C code: `extern char gdb_int [];'
8016      GCC produces an empty range DIE.
8017      FIXME: muller/2010-05-28: Possible references to object for low bound,
8018      high bound or count are not yet handled by this code.
8019   */
8020   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8021     {
8022       struct objfile *objfile = cu->objfile;
8023       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8024       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8025       struct type *int_type = objfile_type (objfile)->builtin_int;
8026
8027       /* Test "int", "long int", and "long long int" objfile types,
8028          and select the first one having a size above or equal to the
8029          architecture address size.  */
8030       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8031         base_type = int_type;
8032       else
8033         {
8034           int_type = objfile_type (objfile)->builtin_long;
8035           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8036             base_type = int_type;
8037           else
8038             {
8039               int_type = objfile_type (objfile)->builtin_long_long;
8040               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8041                 base_type = int_type;
8042             }
8043         }
8044     }
8045
8046   negative_mask =
8047     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8048   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8049     low |= negative_mask;
8050   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8051     high |= negative_mask;
8052
8053   range_type = create_range_type (NULL, base_type, low, high);
8054
8055   /* Mark arrays with dynamic length at least as an array of unspecified
8056      length.  GDB could check the boundary but before it gets implemented at
8057      least allow accessing the array elements.  */
8058   if (attr && attr->form == DW_FORM_block1)
8059     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8060
8061   name = dwarf2_name (die, cu);
8062   if (name)
8063     TYPE_NAME (range_type) = name;
8064
8065   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8066   if (attr)
8067     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8068
8069   set_die_type (die, range_type, cu);
8070
8071   /* set_die_type should be already done.  */
8072   set_descriptive_type (range_type, die, cu);
8073
8074   return range_type;
8075 }
8076
8077 static struct type *
8078 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8079 {
8080   struct type *type;
8081
8082   /* For now, we only support the C meaning of an unspecified type: void.  */
8083
8084   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8085   TYPE_NAME (type) = dwarf2_name (die, cu);
8086
8087   return set_die_type (die, type, cu);
8088 }
8089
8090 /* Trivial hash function for die_info: the hash value of a DIE
8091    is its offset in .debug_info for this objfile.  */
8092
8093 static hashval_t
8094 die_hash (const void *item)
8095 {
8096   const struct die_info *die = item;
8097
8098   return die->offset;
8099 }
8100
8101 /* Trivial comparison function for die_info structures: two DIEs
8102    are equal if they have the same offset.  */
8103
8104 static int
8105 die_eq (const void *item_lhs, const void *item_rhs)
8106 {
8107   const struct die_info *die_lhs = item_lhs;
8108   const struct die_info *die_rhs = item_rhs;
8109
8110   return die_lhs->offset == die_rhs->offset;
8111 }
8112
8113 /* Read a whole compilation unit into a linked list of dies.  */
8114
8115 static struct die_info *
8116 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8117 {
8118   struct die_reader_specs reader_specs;
8119   int read_abbrevs = 0;
8120   struct cleanup *back_to = NULL;
8121   struct die_info *die;
8122
8123   if (cu->dwarf2_abbrevs == NULL)
8124     {
8125       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8126       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8127       read_abbrevs = 1;
8128     }
8129
8130   gdb_assert (cu->die_hash == NULL);
8131   cu->die_hash
8132     = htab_create_alloc_ex (cu->header.length / 12,
8133                             die_hash,
8134                             die_eq,
8135                             NULL,
8136                             &cu->comp_unit_obstack,
8137                             hashtab_obstack_allocate,
8138                             dummy_obstack_deallocate);
8139
8140   init_cu_die_reader (&reader_specs, cu);
8141
8142   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8143
8144   if (read_abbrevs)
8145     do_cleanups (back_to);
8146
8147   return die;
8148 }
8149
8150 /* Main entry point for reading a DIE and all children.
8151    Read the DIE and dump it if requested.  */
8152
8153 static struct die_info *
8154 read_die_and_children (const struct die_reader_specs *reader,
8155                        gdb_byte *info_ptr,
8156                        gdb_byte **new_info_ptr,
8157                        struct die_info *parent)
8158 {
8159   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8160                                                      new_info_ptr, parent);
8161
8162   if (dwarf2_die_debug)
8163     {
8164       fprintf_unfiltered (gdb_stdlog,
8165                           "\nRead die from %s of %s:\n",
8166                           reader->buffer == dwarf2_per_objfile->info.buffer
8167                           ? ".debug_info"
8168                           : reader->buffer == dwarf2_per_objfile->types.buffer
8169                           ? ".debug_types"
8170                           : "unknown section",
8171                           reader->abfd->filename);
8172       dump_die (result, dwarf2_die_debug);
8173     }
8174
8175   return result;
8176 }
8177
8178 /* Read a single die and all its descendents.  Set the die's sibling
8179    field to NULL; set other fields in the die correctly, and set all
8180    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8181    location of the info_ptr after reading all of those dies.  PARENT
8182    is the parent of the die in question.  */
8183
8184 static struct die_info *
8185 read_die_and_children_1 (const struct die_reader_specs *reader,
8186                          gdb_byte *info_ptr,
8187                          gdb_byte **new_info_ptr,
8188                          struct die_info *parent)
8189 {
8190   struct die_info *die;
8191   gdb_byte *cur_ptr;
8192   int has_children;
8193
8194   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8195   if (die == NULL)
8196     {
8197       *new_info_ptr = cur_ptr;
8198       return NULL;
8199     }
8200   store_in_ref_table (die, reader->cu);
8201
8202   if (has_children)
8203     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8204   else
8205     {
8206       die->child = NULL;
8207       *new_info_ptr = cur_ptr;
8208     }
8209
8210   die->sibling = NULL;
8211   die->parent = parent;
8212   return die;
8213 }
8214
8215 /* Read a die, all of its descendents, and all of its siblings; set
8216    all of the fields of all of the dies correctly.  Arguments are as
8217    in read_die_and_children.  */
8218
8219 static struct die_info *
8220 read_die_and_siblings (const struct die_reader_specs *reader,
8221                        gdb_byte *info_ptr,
8222                        gdb_byte **new_info_ptr,
8223                        struct die_info *parent)
8224 {
8225   struct die_info *first_die, *last_sibling;
8226   gdb_byte *cur_ptr;
8227
8228   cur_ptr = info_ptr;
8229   first_die = last_sibling = NULL;
8230
8231   while (1)
8232     {
8233       struct die_info *die
8234         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8235
8236       if (die == NULL)
8237         {
8238           *new_info_ptr = cur_ptr;
8239           return first_die;
8240         }
8241
8242       if (!first_die)
8243         first_die = die;
8244       else
8245         last_sibling->sibling = die;
8246
8247       last_sibling = die;
8248     }
8249 }
8250
8251 /* Read the die from the .debug_info section buffer.  Set DIEP to
8252    point to a newly allocated die with its information, except for its
8253    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8254    whether the die has children or not.  */
8255
8256 static gdb_byte *
8257 read_full_die (const struct die_reader_specs *reader,
8258                struct die_info **diep, gdb_byte *info_ptr,
8259                int *has_children)
8260 {
8261   unsigned int abbrev_number, bytes_read, i, offset;
8262   struct abbrev_info *abbrev;
8263   struct die_info *die;
8264   struct dwarf2_cu *cu = reader->cu;
8265   bfd *abfd = reader->abfd;
8266
8267   offset = info_ptr - reader->buffer;
8268   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8269   info_ptr += bytes_read;
8270   if (!abbrev_number)
8271     {
8272       *diep = NULL;
8273       *has_children = 0;
8274       return info_ptr;
8275     }
8276
8277   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8278   if (!abbrev)
8279     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8280            abbrev_number,
8281            bfd_get_filename (abfd));
8282
8283   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8284   die->offset = offset;
8285   die->tag = abbrev->tag;
8286   die->abbrev = abbrev_number;
8287
8288   die->num_attrs = abbrev->num_attrs;
8289
8290   for (i = 0; i < abbrev->num_attrs; ++i)
8291     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8292                                abfd, info_ptr, cu);
8293
8294   *diep = die;
8295   *has_children = abbrev->has_children;
8296   return info_ptr;
8297 }
8298
8299 /* In DWARF version 2, the description of the debugging information is
8300    stored in a separate .debug_abbrev section.  Before we read any
8301    dies from a section we read in all abbreviations and install them
8302    in a hash table.  This function also sets flags in CU describing
8303    the data found in the abbrev table.  */
8304
8305 static void
8306 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8307 {
8308   struct comp_unit_head *cu_header = &cu->header;
8309   gdb_byte *abbrev_ptr;
8310   struct abbrev_info *cur_abbrev;
8311   unsigned int abbrev_number, bytes_read, abbrev_name;
8312   unsigned int abbrev_form, hash_number;
8313   struct attr_abbrev *cur_attrs;
8314   unsigned int allocated_attrs;
8315
8316   /* Initialize dwarf2 abbrevs */
8317   obstack_init (&cu->abbrev_obstack);
8318   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8319                                       (ABBREV_HASH_SIZE
8320                                        * sizeof (struct abbrev_info *)));
8321   memset (cu->dwarf2_abbrevs, 0,
8322           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8323
8324   dwarf2_read_section (dwarf2_per_objfile->objfile,
8325                        &dwarf2_per_objfile->abbrev);
8326   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8327   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8328   abbrev_ptr += bytes_read;
8329
8330   allocated_attrs = ATTR_ALLOC_CHUNK;
8331   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8332
8333   /* loop until we reach an abbrev number of 0 */
8334   while (abbrev_number)
8335     {
8336       cur_abbrev = dwarf_alloc_abbrev (cu);
8337
8338       /* read in abbrev header */
8339       cur_abbrev->number = abbrev_number;
8340       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8341       abbrev_ptr += bytes_read;
8342       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8343       abbrev_ptr += 1;
8344
8345       if (cur_abbrev->tag == DW_TAG_namespace)
8346         cu->has_namespace_info = 1;
8347
8348       /* now read in declarations */
8349       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8350       abbrev_ptr += bytes_read;
8351       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8352       abbrev_ptr += bytes_read;
8353       while (abbrev_name)
8354         {
8355           if (cur_abbrev->num_attrs == allocated_attrs)
8356             {
8357               allocated_attrs += ATTR_ALLOC_CHUNK;
8358               cur_attrs
8359                 = xrealloc (cur_attrs, (allocated_attrs
8360                                         * sizeof (struct attr_abbrev)));
8361             }
8362
8363           /* Record whether this compilation unit might have
8364              inter-compilation-unit references.  If we don't know what form
8365              this attribute will have, then it might potentially be a
8366              DW_FORM_ref_addr, so we conservatively expect inter-CU
8367              references.  */
8368
8369           if (abbrev_form == DW_FORM_ref_addr
8370               || abbrev_form == DW_FORM_indirect)
8371             cu->has_form_ref_addr = 1;
8372
8373           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8374           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8375           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8376           abbrev_ptr += bytes_read;
8377           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8378           abbrev_ptr += bytes_read;
8379         }
8380
8381       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8382                                          (cur_abbrev->num_attrs
8383                                           * sizeof (struct attr_abbrev)));
8384       memcpy (cur_abbrev->attrs, cur_attrs,
8385               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8386
8387       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8388       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8389       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8390
8391       /* Get next abbreviation.
8392          Under Irix6 the abbreviations for a compilation unit are not
8393          always properly terminated with an abbrev number of 0.
8394          Exit loop if we encounter an abbreviation which we have
8395          already read (which means we are about to read the abbreviations
8396          for the next compile unit) or if the end of the abbreviation
8397          table is reached.  */
8398       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8399           >= dwarf2_per_objfile->abbrev.size)
8400         break;
8401       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8402       abbrev_ptr += bytes_read;
8403       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8404         break;
8405     }
8406
8407   xfree (cur_attrs);
8408 }
8409
8410 /* Release the memory used by the abbrev table for a compilation unit.  */
8411
8412 static void
8413 dwarf2_free_abbrev_table (void *ptr_to_cu)
8414 {
8415   struct dwarf2_cu *cu = ptr_to_cu;
8416
8417   obstack_free (&cu->abbrev_obstack, NULL);
8418   cu->dwarf2_abbrevs = NULL;
8419 }
8420
8421 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8422
8423 static struct abbrev_info *
8424 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8425 {
8426   unsigned int hash_number;
8427   struct abbrev_info *abbrev;
8428
8429   hash_number = number % ABBREV_HASH_SIZE;
8430   abbrev = cu->dwarf2_abbrevs[hash_number];
8431
8432   while (abbrev)
8433     {
8434       if (abbrev->number == number)
8435         return abbrev;
8436       else
8437         abbrev = abbrev->next;
8438     }
8439   return NULL;
8440 }
8441
8442 /* Returns nonzero if TAG represents a type that we might generate a partial
8443    symbol for.  */
8444
8445 static int
8446 is_type_tag_for_partial (int tag)
8447 {
8448   switch (tag)
8449     {
8450 #if 0
8451     /* Some types that would be reasonable to generate partial symbols for,
8452        that we don't at present.  */
8453     case DW_TAG_array_type:
8454     case DW_TAG_file_type:
8455     case DW_TAG_ptr_to_member_type:
8456     case DW_TAG_set_type:
8457     case DW_TAG_string_type:
8458     case DW_TAG_subroutine_type:
8459 #endif
8460     case DW_TAG_base_type:
8461     case DW_TAG_class_type:
8462     case DW_TAG_interface_type:
8463     case DW_TAG_enumeration_type:
8464     case DW_TAG_structure_type:
8465     case DW_TAG_subrange_type:
8466     case DW_TAG_typedef:
8467     case DW_TAG_union_type:
8468       return 1;
8469     default:
8470       return 0;
8471     }
8472 }
8473
8474 /* Load all DIEs that are interesting for partial symbols into memory.  */
8475
8476 static struct partial_die_info *
8477 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8478                    int building_psymtab, struct dwarf2_cu *cu)
8479 {
8480   struct partial_die_info *part_die;
8481   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8482   struct abbrev_info *abbrev;
8483   unsigned int bytes_read;
8484   unsigned int load_all = 0;
8485
8486   int nesting_level = 1;
8487
8488   parent_die = NULL;
8489   last_die = NULL;
8490
8491   if (cu->per_cu && cu->per_cu->load_all_dies)
8492     load_all = 1;
8493
8494   cu->partial_dies
8495     = htab_create_alloc_ex (cu->header.length / 12,
8496                             partial_die_hash,
8497                             partial_die_eq,
8498                             NULL,
8499                             &cu->comp_unit_obstack,
8500                             hashtab_obstack_allocate,
8501                             dummy_obstack_deallocate);
8502
8503   part_die = obstack_alloc (&cu->comp_unit_obstack,
8504                             sizeof (struct partial_die_info));
8505
8506   while (1)
8507     {
8508       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8509
8510       /* A NULL abbrev means the end of a series of children.  */
8511       if (abbrev == NULL)
8512         {
8513           if (--nesting_level == 0)
8514             {
8515               /* PART_DIE was probably the last thing allocated on the
8516                  comp_unit_obstack, so we could call obstack_free
8517                  here.  We don't do that because the waste is small,
8518                  and will be cleaned up when we're done with this
8519                  compilation unit.  This way, we're also more robust
8520                  against other users of the comp_unit_obstack.  */
8521               return first_die;
8522             }
8523           info_ptr += bytes_read;
8524           last_die = parent_die;
8525           parent_die = parent_die->die_parent;
8526           continue;
8527         }
8528
8529       /* Check for template arguments.  We never save these; if
8530          they're seen, we just mark the parent, and go on our way.  */
8531       if (parent_die != NULL
8532           && cu->language == language_cplus
8533           && (abbrev->tag == DW_TAG_template_type_param
8534               || abbrev->tag == DW_TAG_template_value_param))
8535         {
8536           parent_die->has_template_arguments = 1;
8537
8538           if (!load_all)
8539             {
8540               /* We don't need a partial DIE for the template argument.  */
8541               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8542                                        cu);
8543               continue;
8544             }
8545         }
8546
8547       /* We only recurse into subprograms looking for template arguments.
8548          Skip their other children.  */
8549       if (!load_all
8550           && cu->language == language_cplus
8551           && parent_die != NULL
8552           && parent_die->tag == DW_TAG_subprogram)
8553         {
8554           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8555           continue;
8556         }
8557
8558       /* Check whether this DIE is interesting enough to save.  Normally
8559          we would not be interested in members here, but there may be
8560          later variables referencing them via DW_AT_specification (for
8561          static members).  */
8562       if (!load_all
8563           && !is_type_tag_for_partial (abbrev->tag)
8564           && abbrev->tag != DW_TAG_enumerator
8565           && abbrev->tag != DW_TAG_subprogram
8566           && abbrev->tag != DW_TAG_lexical_block
8567           && abbrev->tag != DW_TAG_variable
8568           && abbrev->tag != DW_TAG_namespace
8569           && abbrev->tag != DW_TAG_module
8570           && abbrev->tag != DW_TAG_member)
8571         {
8572           /* Otherwise we skip to the next sibling, if any.  */
8573           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8574           continue;
8575         }
8576
8577       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8578                                    buffer, info_ptr, cu);
8579
8580       /* This two-pass algorithm for processing partial symbols has a
8581          high cost in cache pressure.  Thus, handle some simple cases
8582          here which cover the majority of C partial symbols.  DIEs
8583          which neither have specification tags in them, nor could have
8584          specification tags elsewhere pointing at them, can simply be
8585          processed and discarded.
8586
8587          This segment is also optional; scan_partial_symbols and
8588          add_partial_symbol will handle these DIEs if we chain
8589          them in normally.  When compilers which do not emit large
8590          quantities of duplicate debug information are more common,
8591          this code can probably be removed.  */
8592
8593       /* Any complete simple types at the top level (pretty much all
8594          of them, for a language without namespaces), can be processed
8595          directly.  */
8596       if (parent_die == NULL
8597           && part_die->has_specification == 0
8598           && part_die->is_declaration == 0
8599           && (part_die->tag == DW_TAG_typedef
8600               || part_die->tag == DW_TAG_base_type
8601               || part_die->tag == DW_TAG_subrange_type))
8602         {
8603           if (building_psymtab && part_die->name != NULL)
8604             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8605                                  VAR_DOMAIN, LOC_TYPEDEF,
8606                                  &cu->objfile->static_psymbols,
8607                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8608           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8609           continue;
8610         }
8611
8612       /* If we're at the second level, and we're an enumerator, and
8613          our parent has no specification (meaning possibly lives in a
8614          namespace elsewhere), then we can add the partial symbol now
8615          instead of queueing it.  */
8616       if (part_die->tag == DW_TAG_enumerator
8617           && parent_die != NULL
8618           && parent_die->die_parent == NULL
8619           && parent_die->tag == DW_TAG_enumeration_type
8620           && parent_die->has_specification == 0)
8621         {
8622           if (part_die->name == NULL)
8623             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8624           else if (building_psymtab)
8625             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8626                                  VAR_DOMAIN, LOC_CONST,
8627                                  (cu->language == language_cplus
8628                                   || cu->language == language_java)
8629                                  ? &cu->objfile->global_psymbols
8630                                  : &cu->objfile->static_psymbols,
8631                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8632
8633           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8634           continue;
8635         }
8636
8637       /* We'll save this DIE so link it in.  */
8638       part_die->die_parent = parent_die;
8639       part_die->die_sibling = NULL;
8640       part_die->die_child = NULL;
8641
8642       if (last_die && last_die == parent_die)
8643         last_die->die_child = part_die;
8644       else if (last_die)
8645         last_die->die_sibling = part_die;
8646
8647       last_die = part_die;
8648
8649       if (first_die == NULL)
8650         first_die = part_die;
8651
8652       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8653          find interesting need to be in the hash table, because we
8654          also have the parent/sibling/child chains; only those that we
8655          might refer to by offset later during partial symbol reading.
8656
8657          For now this means things that might have be the target of a
8658          DW_AT_specification, DW_AT_abstract_origin, or
8659          DW_AT_extension.  DW_AT_extension will refer only to
8660          namespaces; DW_AT_abstract_origin refers to functions (and
8661          many things under the function DIE, but we do not recurse
8662          into function DIEs during partial symbol reading) and
8663          possibly variables as well; DW_AT_specification refers to
8664          declarations.  Declarations ought to have the DW_AT_declaration
8665          flag.  It happens that GCC forgets to put it in sometimes, but
8666          only for functions, not for types.
8667
8668          Adding more things than necessary to the hash table is harmless
8669          except for the performance cost.  Adding too few will result in
8670          wasted time in find_partial_die, when we reread the compilation
8671          unit with load_all_dies set.  */
8672
8673       if (load_all
8674           || abbrev->tag == DW_TAG_subprogram
8675           || abbrev->tag == DW_TAG_variable
8676           || abbrev->tag == DW_TAG_namespace
8677           || part_die->is_declaration)
8678         {
8679           void **slot;
8680
8681           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8682                                            part_die->offset, INSERT);
8683           *slot = part_die;
8684         }
8685
8686       part_die = obstack_alloc (&cu->comp_unit_obstack,
8687                                 sizeof (struct partial_die_info));
8688
8689       /* For some DIEs we want to follow their children (if any).  For C
8690          we have no reason to follow the children of structures; for other
8691          languages we have to, so that we can get at method physnames
8692          to infer fully qualified class names, for DW_AT_specification,
8693          and for C++ template arguments.  For C++, we also look one level
8694          inside functions to find template arguments (if the name of the
8695          function does not already contain the template arguments).
8696
8697          For Ada, we need to scan the children of subprograms and lexical
8698          blocks as well because Ada allows the definition of nested
8699          entities that could be interesting for the debugger, such as
8700          nested subprograms for instance.  */
8701       if (last_die->has_children
8702           && (load_all
8703               || last_die->tag == DW_TAG_namespace
8704               || last_die->tag == DW_TAG_module
8705               || last_die->tag == DW_TAG_enumeration_type
8706               || (cu->language == language_cplus
8707                   && last_die->tag == DW_TAG_subprogram
8708                   && (last_die->name == NULL
8709                       || strchr (last_die->name, '<') == NULL))
8710               || (cu->language != language_c
8711                   && (last_die->tag == DW_TAG_class_type
8712                       || last_die->tag == DW_TAG_interface_type
8713                       || last_die->tag == DW_TAG_structure_type
8714                       || last_die->tag == DW_TAG_union_type))
8715               || (cu->language == language_ada
8716                   && (last_die->tag == DW_TAG_subprogram
8717                       || last_die->tag == DW_TAG_lexical_block))))
8718         {
8719           nesting_level++;
8720           parent_die = last_die;
8721           continue;
8722         }
8723
8724       /* Otherwise we skip to the next sibling, if any.  */
8725       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8726
8727       /* Back to the top, do it again.  */
8728     }
8729 }
8730
8731 /* Read a minimal amount of information into the minimal die structure.  */
8732
8733 static gdb_byte *
8734 read_partial_die (struct partial_die_info *part_die,
8735                   struct abbrev_info *abbrev,
8736                   unsigned int abbrev_len, bfd *abfd,
8737                   gdb_byte *buffer, gdb_byte *info_ptr,
8738                   struct dwarf2_cu *cu)
8739 {
8740   unsigned int i;
8741   struct attribute attr;
8742   int has_low_pc_attr = 0;
8743   int has_high_pc_attr = 0;
8744
8745   memset (part_die, 0, sizeof (struct partial_die_info));
8746
8747   part_die->offset = info_ptr - buffer;
8748
8749   info_ptr += abbrev_len;
8750
8751   if (abbrev == NULL)
8752     return info_ptr;
8753
8754   part_die->tag = abbrev->tag;
8755   part_die->has_children = abbrev->has_children;
8756
8757   for (i = 0; i < abbrev->num_attrs; ++i)
8758     {
8759       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8760
8761       /* Store the data if it is of an attribute we want to keep in a
8762          partial symbol table.  */
8763       switch (attr.name)
8764         {
8765         case DW_AT_name:
8766           switch (part_die->tag)
8767             {
8768             case DW_TAG_compile_unit:
8769             case DW_TAG_type_unit:
8770               /* Compilation units have a DW_AT_name that is a filename, not
8771                  a source language identifier.  */
8772             case DW_TAG_enumeration_type:
8773             case DW_TAG_enumerator:
8774               /* These tags always have simple identifiers already; no need
8775                  to canonicalize them.  */
8776               part_die->name = DW_STRING (&attr);
8777               break;
8778             default:
8779               part_die->name
8780                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8781                                             &cu->objfile->objfile_obstack);
8782               break;
8783             }
8784           break;
8785         case DW_AT_linkage_name:
8786         case DW_AT_MIPS_linkage_name:
8787           /* Note that both forms of linkage name might appear.  We
8788              assume they will be the same, and we only store the last
8789              one we see.  */
8790           if (cu->language == language_ada)
8791             part_die->name = DW_STRING (&attr);
8792           break;
8793         case DW_AT_low_pc:
8794           has_low_pc_attr = 1;
8795           part_die->lowpc = DW_ADDR (&attr);
8796           break;
8797         case DW_AT_high_pc:
8798           has_high_pc_attr = 1;
8799           part_die->highpc = DW_ADDR (&attr);
8800           break;
8801         case DW_AT_location:
8802           /* Support the .debug_loc offsets */
8803           if (attr_form_is_block (&attr))
8804             {
8805                part_die->locdesc = DW_BLOCK (&attr);
8806             }
8807           else if (attr_form_is_section_offset (&attr))
8808             {
8809               dwarf2_complex_location_expr_complaint ();
8810             }
8811           else
8812             {
8813               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8814                                                      "partial symbol information");
8815             }
8816           break;
8817         case DW_AT_external:
8818           part_die->is_external = DW_UNSND (&attr);
8819           break;
8820         case DW_AT_declaration:
8821           part_die->is_declaration = DW_UNSND (&attr);
8822           break;
8823         case DW_AT_type:
8824           part_die->has_type = 1;
8825           break;
8826         case DW_AT_abstract_origin:
8827         case DW_AT_specification:
8828         case DW_AT_extension:
8829           part_die->has_specification = 1;
8830           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8831           break;
8832         case DW_AT_sibling:
8833           /* Ignore absolute siblings, they might point outside of
8834              the current compile unit.  */
8835           if (attr.form == DW_FORM_ref_addr)
8836             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8837           else
8838             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8839           break;
8840         case DW_AT_byte_size:
8841           part_die->has_byte_size = 1;
8842           break;
8843         case DW_AT_calling_convention:
8844           /* DWARF doesn't provide a way to identify a program's source-level
8845              entry point.  DW_AT_calling_convention attributes are only meant
8846              to describe functions' calling conventions.
8847
8848              However, because it's a necessary piece of information in
8849              Fortran, and because DW_CC_program is the only piece of debugging
8850              information whose definition refers to a 'main program' at all,
8851              several compilers have begun marking Fortran main programs with
8852              DW_CC_program --- even when those functions use the standard
8853              calling conventions.
8854
8855              So until DWARF specifies a way to provide this information and
8856              compilers pick up the new representation, we'll support this
8857              practice.  */
8858           if (DW_UNSND (&attr) == DW_CC_program
8859               && cu->language == language_fortran)
8860             set_main_name (part_die->name);
8861           break;
8862         default:
8863           break;
8864         }
8865     }
8866
8867   /* When using the GNU linker, .gnu.linkonce. sections are used to
8868      eliminate duplicate copies of functions and vtables and such.
8869      The linker will arbitrarily choose one and discard the others.
8870      The AT_*_pc values for such functions refer to local labels in
8871      these sections.  If the section from that file was discarded, the
8872      labels are not in the output, so the relocs get a value of 0.
8873      If this is a discarded function, mark the pc bounds as invalid,
8874      so that GDB will ignore it.  */
8875   if (has_low_pc_attr && has_high_pc_attr
8876       && part_die->lowpc < part_die->highpc
8877       && (part_die->lowpc != 0
8878           || dwarf2_per_objfile->has_section_at_zero))
8879     part_die->has_pc_info = 1;
8880
8881   return info_ptr;
8882 }
8883
8884 /* Find a cached partial DIE at OFFSET in CU.  */
8885
8886 static struct partial_die_info *
8887 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8888 {
8889   struct partial_die_info *lookup_die = NULL;
8890   struct partial_die_info part_die;
8891
8892   part_die.offset = offset;
8893   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8894
8895   return lookup_die;
8896 }
8897
8898 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8899    except in the case of .debug_types DIEs which do not reference
8900    outside their CU (they do however referencing other types via
8901    DW_FORM_sig8).  */
8902
8903 static struct partial_die_info *
8904 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8905 {
8906   struct dwarf2_per_cu_data *per_cu = NULL;
8907   struct partial_die_info *pd = NULL;
8908
8909   if (cu->per_cu->from_debug_types)
8910     {
8911       pd = find_partial_die_in_comp_unit (offset, cu);
8912       if (pd != NULL)
8913         return pd;
8914       goto not_found;
8915     }
8916
8917   if (offset_in_cu_p (&cu->header, offset))
8918     {
8919       pd = find_partial_die_in_comp_unit (offset, cu);
8920       if (pd != NULL)
8921         return pd;
8922     }
8923
8924   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8925
8926   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8927     load_partial_comp_unit (per_cu, cu->objfile);
8928
8929   per_cu->cu->last_used = 0;
8930   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8931
8932   if (pd == NULL && per_cu->load_all_dies == 0)
8933     {
8934       struct cleanup *back_to;
8935       struct partial_die_info comp_unit_die;
8936       struct abbrev_info *abbrev;
8937       unsigned int bytes_read;
8938       char *info_ptr;
8939
8940       per_cu->load_all_dies = 1;
8941
8942       /* Re-read the DIEs.  */
8943       back_to = make_cleanup (null_cleanup, 0);
8944       if (per_cu->cu->dwarf2_abbrevs == NULL)
8945         {
8946           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8947           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8948         }
8949       info_ptr = (dwarf2_per_objfile->info.buffer
8950                   + per_cu->cu->header.offset
8951                   + per_cu->cu->header.first_die_offset);
8952       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8953       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8954                                    per_cu->cu->objfile->obfd,
8955                                    dwarf2_per_objfile->info.buffer, info_ptr,
8956                                    per_cu->cu);
8957       if (comp_unit_die.has_children)
8958         load_partial_dies (per_cu->cu->objfile->obfd,
8959                            dwarf2_per_objfile->info.buffer, info_ptr,
8960                            0, per_cu->cu);
8961       do_cleanups (back_to);
8962
8963       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8964     }
8965
8966  not_found:
8967
8968   if (pd == NULL)
8969     internal_error (__FILE__, __LINE__,
8970                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8971                     offset, bfd_get_filename (cu->objfile->obfd));
8972   return pd;
8973 }
8974
8975 /* Adjust PART_DIE before generating a symbol for it.  This function
8976    may set the is_external flag or change the DIE's name.  */
8977
8978 static void
8979 fixup_partial_die (struct partial_die_info *part_die,
8980                    struct dwarf2_cu *cu)
8981 {
8982   /* If we found a reference attribute and the DIE has no name, try
8983      to find a name in the referred to DIE.  */
8984
8985   if (part_die->name == NULL && part_die->has_specification)
8986     {
8987       struct partial_die_info *spec_die;
8988
8989       spec_die = find_partial_die (part_die->spec_offset, cu);
8990
8991       fixup_partial_die (spec_die, cu);
8992
8993       if (spec_die->name)
8994         {
8995           part_die->name = spec_die->name;
8996
8997           /* Copy DW_AT_external attribute if it is set.  */
8998           if (spec_die->is_external)
8999             part_die->is_external = spec_die->is_external;
9000         }
9001     }
9002
9003   /* Set default names for some unnamed DIEs.  */
9004   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
9005                                  || part_die->tag == DW_TAG_class_type))
9006     part_die->name = "(anonymous class)";
9007
9008   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9009     part_die->name = "(anonymous namespace)";
9010
9011   if (part_die->tag == DW_TAG_structure_type
9012       || part_die->tag == DW_TAG_class_type
9013       || part_die->tag == DW_TAG_union_type)
9014     guess_structure_name (part_die, cu);
9015 }
9016
9017 /* Read an attribute value described by an attribute form.  */
9018
9019 static gdb_byte *
9020 read_attribute_value (struct attribute *attr, unsigned form,
9021                       bfd *abfd, gdb_byte *info_ptr,
9022                       struct dwarf2_cu *cu)
9023 {
9024   struct comp_unit_head *cu_header = &cu->header;
9025   unsigned int bytes_read;
9026   struct dwarf_block *blk;
9027
9028   attr->form = form;
9029   switch (form)
9030     {
9031     case DW_FORM_ref_addr:
9032       if (cu->header.version == 2)
9033         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9034       else
9035         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9036       info_ptr += bytes_read;
9037       break;
9038     case DW_FORM_addr:
9039       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9040       info_ptr += bytes_read;
9041       break;
9042     case DW_FORM_block2:
9043       blk = dwarf_alloc_block (cu);
9044       blk->size = read_2_bytes (abfd, info_ptr);
9045       info_ptr += 2;
9046       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9047       info_ptr += blk->size;
9048       DW_BLOCK (attr) = blk;
9049       break;
9050     case DW_FORM_block4:
9051       blk = dwarf_alloc_block (cu);
9052       blk->size = read_4_bytes (abfd, info_ptr);
9053       info_ptr += 4;
9054       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9055       info_ptr += blk->size;
9056       DW_BLOCK (attr) = blk;
9057       break;
9058     case DW_FORM_data2:
9059       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9060       info_ptr += 2;
9061       break;
9062     case DW_FORM_data4:
9063       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9064       info_ptr += 4;
9065       break;
9066     case DW_FORM_data8:
9067       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9068       info_ptr += 8;
9069       break;
9070     case DW_FORM_sec_offset:
9071       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9072       info_ptr += bytes_read;
9073       break;
9074     case DW_FORM_string:
9075       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9076       DW_STRING_IS_CANONICAL (attr) = 0;
9077       info_ptr += bytes_read;
9078       break;
9079     case DW_FORM_strp:
9080       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9081                                                &bytes_read);
9082       DW_STRING_IS_CANONICAL (attr) = 0;
9083       info_ptr += bytes_read;
9084       break;
9085     case DW_FORM_exprloc:
9086     case DW_FORM_block:
9087       blk = dwarf_alloc_block (cu);
9088       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9089       info_ptr += bytes_read;
9090       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9091       info_ptr += blk->size;
9092       DW_BLOCK (attr) = blk;
9093       break;
9094     case DW_FORM_block1:
9095       blk = dwarf_alloc_block (cu);
9096       blk->size = read_1_byte (abfd, info_ptr);
9097       info_ptr += 1;
9098       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9099       info_ptr += blk->size;
9100       DW_BLOCK (attr) = blk;
9101       break;
9102     case DW_FORM_data1:
9103       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9104       info_ptr += 1;
9105       break;
9106     case DW_FORM_flag:
9107       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9108       info_ptr += 1;
9109       break;
9110     case DW_FORM_flag_present:
9111       DW_UNSND (attr) = 1;
9112       break;
9113     case DW_FORM_sdata:
9114       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9115       info_ptr += bytes_read;
9116       break;
9117     case DW_FORM_udata:
9118       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9119       info_ptr += bytes_read;
9120       break;
9121     case DW_FORM_ref1:
9122       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9123       info_ptr += 1;
9124       break;
9125     case DW_FORM_ref2:
9126       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9127       info_ptr += 2;
9128       break;
9129     case DW_FORM_ref4:
9130       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9131       info_ptr += 4;
9132       break;
9133     case DW_FORM_ref8:
9134       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9135       info_ptr += 8;
9136       break;
9137     case DW_FORM_sig8:
9138       /* Convert the signature to something we can record in DW_UNSND
9139          for later lookup.
9140          NOTE: This is NULL if the type wasn't found.  */
9141       DW_SIGNATURED_TYPE (attr) =
9142         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9143       info_ptr += 8;
9144       break;
9145     case DW_FORM_ref_udata:
9146       DW_ADDR (attr) = (cu->header.offset
9147                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9148       info_ptr += bytes_read;
9149       break;
9150     case DW_FORM_indirect:
9151       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9152       info_ptr += bytes_read;
9153       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9154       break;
9155     default:
9156       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9157              dwarf_form_name (form),
9158              bfd_get_filename (abfd));
9159     }
9160
9161   /* We have seen instances where the compiler tried to emit a byte
9162      size attribute of -1 which ended up being encoded as an unsigned
9163      0xffffffff.  Although 0xffffffff is technically a valid size value,
9164      an object of this size seems pretty unlikely so we can relatively
9165      safely treat these cases as if the size attribute was invalid and
9166      treat them as zero by default.  */
9167   if (attr->name == DW_AT_byte_size
9168       && form == DW_FORM_data4
9169       && DW_UNSND (attr) >= 0xffffffff)
9170     {
9171       complaint
9172         (&symfile_complaints,
9173          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9174          hex_string (DW_UNSND (attr)));
9175       DW_UNSND (attr) = 0;
9176     }
9177
9178   return info_ptr;
9179 }
9180
9181 /* Read an attribute described by an abbreviated attribute.  */
9182
9183 static gdb_byte *
9184 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9185                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9186 {
9187   attr->name = abbrev->name;
9188   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9189 }
9190
9191 /* read dwarf information from a buffer */
9192
9193 static unsigned int
9194 read_1_byte (bfd *abfd, gdb_byte *buf)
9195 {
9196   return bfd_get_8 (abfd, buf);
9197 }
9198
9199 static int
9200 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9201 {
9202   return bfd_get_signed_8 (abfd, buf);
9203 }
9204
9205 static unsigned int
9206 read_2_bytes (bfd *abfd, gdb_byte *buf)
9207 {
9208   return bfd_get_16 (abfd, buf);
9209 }
9210
9211 static int
9212 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9213 {
9214   return bfd_get_signed_16 (abfd, buf);
9215 }
9216
9217 static unsigned int
9218 read_4_bytes (bfd *abfd, gdb_byte *buf)
9219 {
9220   return bfd_get_32 (abfd, buf);
9221 }
9222
9223 static int
9224 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9225 {
9226   return bfd_get_signed_32 (abfd, buf);
9227 }
9228
9229 static ULONGEST
9230 read_8_bytes (bfd *abfd, gdb_byte *buf)
9231 {
9232   return bfd_get_64 (abfd, buf);
9233 }
9234
9235 static CORE_ADDR
9236 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9237               unsigned int *bytes_read)
9238 {
9239   struct comp_unit_head *cu_header = &cu->header;
9240   CORE_ADDR retval = 0;
9241
9242   if (cu_header->signed_addr_p)
9243     {
9244       switch (cu_header->addr_size)
9245         {
9246         case 2:
9247           retval = bfd_get_signed_16 (abfd, buf);
9248           break;
9249         case 4:
9250           retval = bfd_get_signed_32 (abfd, buf);
9251           break;
9252         case 8:
9253           retval = bfd_get_signed_64 (abfd, buf);
9254           break;
9255         default:
9256           internal_error (__FILE__, __LINE__,
9257                           _("read_address: bad switch, signed [in module %s]"),
9258                           bfd_get_filename (abfd));
9259         }
9260     }
9261   else
9262     {
9263       switch (cu_header->addr_size)
9264         {
9265         case 2:
9266           retval = bfd_get_16 (abfd, buf);
9267           break;
9268         case 4:
9269           retval = bfd_get_32 (abfd, buf);
9270           break;
9271         case 8:
9272           retval = bfd_get_64 (abfd, buf);
9273           break;
9274         default:
9275           internal_error (__FILE__, __LINE__,
9276                           _("read_address: bad switch, unsigned [in module %s]"),
9277                           bfd_get_filename (abfd));
9278         }
9279     }
9280
9281   *bytes_read = cu_header->addr_size;
9282   return retval;
9283 }
9284
9285 /* Read the initial length from a section.  The (draft) DWARF 3
9286    specification allows the initial length to take up either 4 bytes
9287    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9288    bytes describe the length and all offsets will be 8 bytes in length
9289    instead of 4.
9290
9291    An older, non-standard 64-bit format is also handled by this
9292    function.  The older format in question stores the initial length
9293    as an 8-byte quantity without an escape value.  Lengths greater
9294    than 2^32 aren't very common which means that the initial 4 bytes
9295    is almost always zero.  Since a length value of zero doesn't make
9296    sense for the 32-bit format, this initial zero can be considered to
9297    be an escape value which indicates the presence of the older 64-bit
9298    format.  As written, the code can't detect (old format) lengths
9299    greater than 4GB.  If it becomes necessary to handle lengths
9300    somewhat larger than 4GB, we could allow other small values (such
9301    as the non-sensical values of 1, 2, and 3) to also be used as
9302    escape values indicating the presence of the old format.
9303
9304    The value returned via bytes_read should be used to increment the
9305    relevant pointer after calling read_initial_length().
9306
9307    [ Note:  read_initial_length() and read_offset() are based on the
9308      document entitled "DWARF Debugging Information Format", revision
9309      3, draft 8, dated November 19, 2001.  This document was obtained
9310      from:
9311
9312         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9313
9314      This document is only a draft and is subject to change.  (So beware.)
9315
9316      Details regarding the older, non-standard 64-bit format were
9317      determined empirically by examining 64-bit ELF files produced by
9318      the SGI toolchain on an IRIX 6.5 machine.
9319
9320      - Kevin, July 16, 2002
9321    ] */
9322
9323 static LONGEST
9324 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9325 {
9326   LONGEST length = bfd_get_32 (abfd, buf);
9327
9328   if (length == 0xffffffff)
9329     {
9330       length = bfd_get_64 (abfd, buf + 4);
9331       *bytes_read = 12;
9332     }
9333   else if (length == 0)
9334     {
9335       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9336       length = bfd_get_64 (abfd, buf);
9337       *bytes_read = 8;
9338     }
9339   else
9340     {
9341       *bytes_read = 4;
9342     }
9343
9344   return length;
9345 }
9346
9347 /* Cover function for read_initial_length.
9348    Returns the length of the object at BUF, and stores the size of the
9349    initial length in *BYTES_READ and stores the size that offsets will be in
9350    *OFFSET_SIZE.
9351    If the initial length size is not equivalent to that specified in
9352    CU_HEADER then issue a complaint.
9353    This is useful when reading non-comp-unit headers.  */
9354
9355 static LONGEST
9356 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9357                                         const struct comp_unit_head *cu_header,
9358                                         unsigned int *bytes_read,
9359                                         unsigned int *offset_size)
9360 {
9361   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9362
9363   gdb_assert (cu_header->initial_length_size == 4
9364               || cu_header->initial_length_size == 8
9365               || cu_header->initial_length_size == 12);
9366
9367   if (cu_header->initial_length_size != *bytes_read)
9368     complaint (&symfile_complaints,
9369                _("intermixed 32-bit and 64-bit DWARF sections"));
9370
9371   *offset_size = (*bytes_read == 4) ? 4 : 8;
9372   return length;
9373 }
9374
9375 /* Read an offset from the data stream.  The size of the offset is
9376    given by cu_header->offset_size.  */
9377
9378 static LONGEST
9379 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9380              unsigned int *bytes_read)
9381 {
9382   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9383
9384   *bytes_read = cu_header->offset_size;
9385   return offset;
9386 }
9387
9388 /* Read an offset from the data stream.  */
9389
9390 static LONGEST
9391 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9392 {
9393   LONGEST retval = 0;
9394
9395   switch (offset_size)
9396     {
9397     case 4:
9398       retval = bfd_get_32 (abfd, buf);
9399       break;
9400     case 8:
9401       retval = bfd_get_64 (abfd, buf);
9402       break;
9403     default:
9404       internal_error (__FILE__, __LINE__,
9405                       _("read_offset_1: bad switch [in module %s]"),
9406                       bfd_get_filename (abfd));
9407     }
9408
9409   return retval;
9410 }
9411
9412 static gdb_byte *
9413 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9414 {
9415   /* If the size of a host char is 8 bits, we can return a pointer
9416      to the buffer, otherwise we have to copy the data to a buffer
9417      allocated on the temporary obstack.  */
9418   gdb_assert (HOST_CHAR_BIT == 8);
9419   return buf;
9420 }
9421
9422 static char *
9423 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9424 {
9425   /* If the size of a host char is 8 bits, we can return a pointer
9426      to the string, otherwise we have to copy the string to a buffer
9427      allocated on the temporary obstack.  */
9428   gdb_assert (HOST_CHAR_BIT == 8);
9429   if (*buf == '\0')
9430     {
9431       *bytes_read_ptr = 1;
9432       return NULL;
9433     }
9434   *bytes_read_ptr = strlen ((char *) buf) + 1;
9435   return (char *) buf;
9436 }
9437
9438 static char *
9439 read_indirect_string (bfd *abfd, gdb_byte *buf,
9440                       const struct comp_unit_head *cu_header,
9441                       unsigned int *bytes_read_ptr)
9442 {
9443   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9444
9445   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9446   if (dwarf2_per_objfile->str.buffer == NULL)
9447     {
9448       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9449                       bfd_get_filename (abfd));
9450       return NULL;
9451     }
9452   if (str_offset >= dwarf2_per_objfile->str.size)
9453     {
9454       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9455                       bfd_get_filename (abfd));
9456       return NULL;
9457     }
9458   gdb_assert (HOST_CHAR_BIT == 8);
9459   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9460     return NULL;
9461   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9462 }
9463
9464 static unsigned long
9465 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9466 {
9467   unsigned long result;
9468   unsigned int num_read;
9469   int i, shift;
9470   unsigned char byte;
9471
9472   result = 0;
9473   shift = 0;
9474   num_read = 0;
9475   i = 0;
9476   while (1)
9477     {
9478       byte = bfd_get_8 (abfd, buf);
9479       buf++;
9480       num_read++;
9481       result |= ((unsigned long)(byte & 127) << shift);
9482       if ((byte & 128) == 0)
9483         {
9484           break;
9485         }
9486       shift += 7;
9487     }
9488   *bytes_read_ptr = num_read;
9489   return result;
9490 }
9491
9492 static long
9493 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9494 {
9495   long result;
9496   int i, shift, num_read;
9497   unsigned char byte;
9498
9499   result = 0;
9500   shift = 0;
9501   num_read = 0;
9502   i = 0;
9503   while (1)
9504     {
9505       byte = bfd_get_8 (abfd, buf);
9506       buf++;
9507       num_read++;
9508       result |= ((long)(byte & 127) << shift);
9509       shift += 7;
9510       if ((byte & 128) == 0)
9511         {
9512           break;
9513         }
9514     }
9515   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9516     result |= -(((long)1) << shift);
9517   *bytes_read_ptr = num_read;
9518   return result;
9519 }
9520
9521 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9522
9523 static gdb_byte *
9524 skip_leb128 (bfd *abfd, gdb_byte *buf)
9525 {
9526   int byte;
9527
9528   while (1)
9529     {
9530       byte = bfd_get_8 (abfd, buf);
9531       buf++;
9532       if ((byte & 128) == 0)
9533         return buf;
9534     }
9535 }
9536
9537 static void
9538 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9539 {
9540   switch (lang)
9541     {
9542     case DW_LANG_C89:
9543     case DW_LANG_C99:
9544     case DW_LANG_C:
9545       cu->language = language_c;
9546       break;
9547     case DW_LANG_C_plus_plus:
9548       cu->language = language_cplus;
9549       break;
9550     case DW_LANG_D:
9551       cu->language = language_d;
9552       break;
9553     case DW_LANG_Fortran77:
9554     case DW_LANG_Fortran90:
9555     case DW_LANG_Fortran95:
9556       cu->language = language_fortran;
9557       break;
9558     case DW_LANG_Mips_Assembler:
9559       cu->language = language_asm;
9560       break;
9561     case DW_LANG_Java:
9562       cu->language = language_java;
9563       break;
9564     case DW_LANG_Ada83:
9565     case DW_LANG_Ada95:
9566       cu->language = language_ada;
9567       break;
9568     case DW_LANG_Modula2:
9569       cu->language = language_m2;
9570       break;
9571     case DW_LANG_Pascal83:
9572       cu->language = language_pascal;
9573       break;
9574     case DW_LANG_ObjC:
9575       cu->language = language_objc;
9576       break;
9577     case DW_LANG_Cobol74:
9578     case DW_LANG_Cobol85:
9579     default:
9580       cu->language = language_minimal;
9581       break;
9582     }
9583   cu->language_defn = language_def (cu->language);
9584 }
9585
9586 /* Return the named attribute or NULL if not there.  */
9587
9588 static struct attribute *
9589 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9590 {
9591   unsigned int i;
9592   struct attribute *spec = NULL;
9593
9594   for (i = 0; i < die->num_attrs; ++i)
9595     {
9596       if (die->attrs[i].name == name)
9597         return &die->attrs[i];
9598       if (die->attrs[i].name == DW_AT_specification
9599           || die->attrs[i].name == DW_AT_abstract_origin)
9600         spec = &die->attrs[i];
9601     }
9602
9603   if (spec)
9604     {
9605       die = follow_die_ref (die, spec, &cu);
9606       return dwarf2_attr (die, name, cu);
9607     }
9608
9609   return NULL;
9610 }
9611
9612 /* Return the named attribute or NULL if not there,
9613    but do not follow DW_AT_specification, etc.
9614    This is for use in contexts where we're reading .debug_types dies.
9615    Following DW_AT_specification, DW_AT_abstract_origin will take us
9616    back up the chain, and we want to go down.  */
9617
9618 static struct attribute *
9619 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9620                        struct dwarf2_cu *cu)
9621 {
9622   unsigned int i;
9623
9624   for (i = 0; i < die->num_attrs; ++i)
9625     if (die->attrs[i].name == name)
9626       return &die->attrs[i];
9627
9628   return NULL;
9629 }
9630
9631 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9632    and holds a non-zero value.  This function should only be used for
9633    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9634
9635 static int
9636 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9637 {
9638   struct attribute *attr = dwarf2_attr (die, name, cu);
9639
9640   return (attr && DW_UNSND (attr));
9641 }
9642
9643 static int
9644 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9645 {
9646   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9647      which value is non-zero.  However, we have to be careful with
9648      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9649      (via dwarf2_flag_true_p) follows this attribute.  So we may
9650      end up accidently finding a declaration attribute that belongs
9651      to a different DIE referenced by the specification attribute,
9652      even though the given DIE does not have a declaration attribute.  */
9653   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9654           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9655 }
9656
9657 /* Return the die giving the specification for DIE, if there is
9658    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9659    containing the return value on output.  If there is no
9660    specification, but there is an abstract origin, that is
9661    returned.  */
9662
9663 static struct die_info *
9664 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9665 {
9666   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9667                                              *spec_cu);
9668
9669   if (spec_attr == NULL)
9670     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9671
9672   if (spec_attr == NULL)
9673     return NULL;
9674   else
9675     return follow_die_ref (die, spec_attr, spec_cu);
9676 }
9677
9678 /* Free the line_header structure *LH, and any arrays and strings it
9679    refers to.  */
9680 static void
9681 free_line_header (struct line_header *lh)
9682 {
9683   if (lh->standard_opcode_lengths)
9684     xfree (lh->standard_opcode_lengths);
9685
9686   /* Remember that all the lh->file_names[i].name pointers are
9687      pointers into debug_line_buffer, and don't need to be freed.  */
9688   if (lh->file_names)
9689     xfree (lh->file_names);
9690
9691   /* Similarly for the include directory names.  */
9692   if (lh->include_dirs)
9693     xfree (lh->include_dirs);
9694
9695   xfree (lh);
9696 }
9697
9698
9699 /* Add an entry to LH's include directory table.  */
9700 static void
9701 add_include_dir (struct line_header *lh, char *include_dir)
9702 {
9703   /* Grow the array if necessary.  */
9704   if (lh->include_dirs_size == 0)
9705     {
9706       lh->include_dirs_size = 1; /* for testing */
9707       lh->include_dirs = xmalloc (lh->include_dirs_size
9708                                   * sizeof (*lh->include_dirs));
9709     }
9710   else if (lh->num_include_dirs >= lh->include_dirs_size)
9711     {
9712       lh->include_dirs_size *= 2;
9713       lh->include_dirs = xrealloc (lh->include_dirs,
9714                                    (lh->include_dirs_size
9715                                     * sizeof (*lh->include_dirs)));
9716     }
9717
9718   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9719 }
9720
9721
9722 /* Add an entry to LH's file name table.  */
9723 static void
9724 add_file_name (struct line_header *lh,
9725                char *name,
9726                unsigned int dir_index,
9727                unsigned int mod_time,
9728                unsigned int length)
9729 {
9730   struct file_entry *fe;
9731
9732   /* Grow the array if necessary.  */
9733   if (lh->file_names_size == 0)
9734     {
9735       lh->file_names_size = 1; /* for testing */
9736       lh->file_names = xmalloc (lh->file_names_size
9737                                 * sizeof (*lh->file_names));
9738     }
9739   else if (lh->num_file_names >= lh->file_names_size)
9740     {
9741       lh->file_names_size *= 2;
9742       lh->file_names = xrealloc (lh->file_names,
9743                                  (lh->file_names_size
9744                                   * sizeof (*lh->file_names)));
9745     }
9746
9747   fe = &lh->file_names[lh->num_file_names++];
9748   fe->name = name;
9749   fe->dir_index = dir_index;
9750   fe->mod_time = mod_time;
9751   fe->length = length;
9752   fe->included_p = 0;
9753   fe->symtab = NULL;
9754 }
9755
9756
9757 /* Read the statement program header starting at OFFSET in
9758    .debug_line, according to the endianness of ABFD.  Return a pointer
9759    to a struct line_header, allocated using xmalloc.
9760
9761    NOTE: the strings in the include directory and file name tables of
9762    the returned object point into debug_line_buffer, and must not be
9763    freed.  */
9764 static struct line_header *
9765 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9766                           struct dwarf2_cu *cu)
9767 {
9768   struct cleanup *back_to;
9769   struct line_header *lh;
9770   gdb_byte *line_ptr;
9771   unsigned int bytes_read, offset_size;
9772   int i;
9773   char *cur_dir, *cur_file;
9774
9775   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9776   if (dwarf2_per_objfile->line.buffer == NULL)
9777     {
9778       complaint (&symfile_complaints, _("missing .debug_line section"));
9779       return 0;
9780     }
9781
9782   /* Make sure that at least there's room for the total_length field.
9783      That could be 12 bytes long, but we're just going to fudge that.  */
9784   if (offset + 4 >= dwarf2_per_objfile->line.size)
9785     {
9786       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9787       return 0;
9788     }
9789
9790   lh = xmalloc (sizeof (*lh));
9791   memset (lh, 0, sizeof (*lh));
9792   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9793                           (void *) lh);
9794
9795   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9796
9797   /* Read in the header.  */
9798   lh->total_length =
9799     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9800                                             &bytes_read, &offset_size);
9801   line_ptr += bytes_read;
9802   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9803                                      + dwarf2_per_objfile->line.size))
9804     {
9805       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9806       return 0;
9807     }
9808   lh->statement_program_end = line_ptr + lh->total_length;
9809   lh->version = read_2_bytes (abfd, line_ptr);
9810   line_ptr += 2;
9811   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9812   line_ptr += offset_size;
9813   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9814   line_ptr += 1;
9815   if (lh->version >= 4)
9816     {
9817       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9818       line_ptr += 1;
9819     }
9820   else
9821     lh->maximum_ops_per_instruction = 1;
9822
9823   if (lh->maximum_ops_per_instruction == 0)
9824     {
9825       lh->maximum_ops_per_instruction = 1;
9826       complaint (&symfile_complaints,
9827                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9828     }
9829
9830   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9831   line_ptr += 1;
9832   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9833   line_ptr += 1;
9834   lh->line_range = read_1_byte (abfd, line_ptr);
9835   line_ptr += 1;
9836   lh->opcode_base = read_1_byte (abfd, line_ptr);
9837   line_ptr += 1;
9838   lh->standard_opcode_lengths
9839     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9840
9841   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9842   for (i = 1; i < lh->opcode_base; ++i)
9843     {
9844       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9845       line_ptr += 1;
9846     }
9847
9848   /* Read directory table.  */
9849   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9850     {
9851       line_ptr += bytes_read;
9852       add_include_dir (lh, cur_dir);
9853     }
9854   line_ptr += bytes_read;
9855
9856   /* Read file name table.  */
9857   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9858     {
9859       unsigned int dir_index, mod_time, length;
9860
9861       line_ptr += bytes_read;
9862       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9863       line_ptr += bytes_read;
9864       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9865       line_ptr += bytes_read;
9866       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9867       line_ptr += bytes_read;
9868
9869       add_file_name (lh, cur_file, dir_index, mod_time, length);
9870     }
9871   line_ptr += bytes_read;
9872   lh->statement_program_start = line_ptr;
9873
9874   if (line_ptr > (dwarf2_per_objfile->line.buffer
9875                   + dwarf2_per_objfile->line.size))
9876     complaint (&symfile_complaints,
9877                _("line number info header doesn't fit in `.debug_line' section"));
9878
9879   discard_cleanups (back_to);
9880   return lh;
9881 }
9882
9883 /* This function exists to work around a bug in certain compilers
9884    (particularly GCC 2.95), in which the first line number marker of a
9885    function does not show up until after the prologue, right before
9886    the second line number marker.  This function shifts ADDRESS down
9887    to the beginning of the function if necessary, and is called on
9888    addresses passed to record_line.  */
9889
9890 static CORE_ADDR
9891 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9892 {
9893   struct function_range *fn;
9894
9895   /* Find the function_range containing address.  */
9896   if (!cu->first_fn)
9897     return address;
9898
9899   if (!cu->cached_fn)
9900     cu->cached_fn = cu->first_fn;
9901
9902   fn = cu->cached_fn;
9903   while (fn)
9904     if (fn->lowpc <= address && fn->highpc > address)
9905       goto found;
9906     else
9907       fn = fn->next;
9908
9909   fn = cu->first_fn;
9910   while (fn && fn != cu->cached_fn)
9911     if (fn->lowpc <= address && fn->highpc > address)
9912       goto found;
9913     else
9914       fn = fn->next;
9915
9916   return address;
9917
9918  found:
9919   if (fn->seen_line)
9920     return address;
9921   if (address != fn->lowpc)
9922     complaint (&symfile_complaints,
9923                _("misplaced first line number at 0x%lx for '%s'"),
9924                (unsigned long) address, fn->name);
9925   fn->seen_line = 1;
9926   return fn->lowpc;
9927 }
9928
9929 /* Subroutine of dwarf_decode_lines to simplify it.
9930    Return the file name of the psymtab for included file FILE_INDEX
9931    in line header LH of PST.
9932    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9933    If space for the result is malloc'd, it will be freed by a cleanup.
9934    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
9935
9936 static char *
9937 psymtab_include_file_name (const struct line_header *lh, int file_index,
9938                            const struct partial_symtab *pst,
9939                            const char *comp_dir)
9940 {
9941   const struct file_entry fe = lh->file_names [file_index];
9942   char *include_name = fe.name;
9943   char *include_name_to_compare = include_name;
9944   char *dir_name = NULL;
9945   char *pst_filename;
9946   int file_is_pst;
9947
9948   if (fe.dir_index)
9949     dir_name = lh->include_dirs[fe.dir_index - 1];
9950
9951   if (!IS_ABSOLUTE_PATH (include_name)
9952       && (dir_name != NULL || comp_dir != NULL))
9953     {
9954       /* Avoid creating a duplicate psymtab for PST.
9955          We do this by comparing INCLUDE_NAME and PST_FILENAME.
9956          Before we do the comparison, however, we need to account
9957          for DIR_NAME and COMP_DIR.
9958          First prepend dir_name (if non-NULL).  If we still don't
9959          have an absolute path prepend comp_dir (if non-NULL).
9960          However, the directory we record in the include-file's
9961          psymtab does not contain COMP_DIR (to match the
9962          corresponding symtab(s)).
9963
9964          Example:
9965
9966          bash$ cd /tmp
9967          bash$ gcc -g ./hello.c
9968          include_name = "hello.c"
9969          dir_name = "."
9970          DW_AT_comp_dir = comp_dir = "/tmp"
9971          DW_AT_name = "./hello.c"  */
9972
9973       if (dir_name != NULL)
9974         {
9975           include_name = concat (dir_name, SLASH_STRING,
9976                                  include_name, (char *)NULL);
9977           include_name_to_compare = include_name;
9978           make_cleanup (xfree, include_name);
9979         }
9980       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
9981         {
9982           include_name_to_compare = concat (comp_dir, SLASH_STRING,
9983                                             include_name, (char *)NULL);
9984         }
9985     }
9986
9987   pst_filename = pst->filename;
9988   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9989     {
9990       pst_filename = concat (pst->dirname, SLASH_STRING,
9991                              pst_filename, (char *)NULL);
9992     }
9993
9994   file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
9995
9996   if (include_name_to_compare != include_name)
9997     xfree (include_name_to_compare);
9998   if (pst_filename != pst->filename)
9999     xfree (pst_filename);
10000
10001   if (file_is_pst)
10002     return NULL;
10003   return include_name;
10004 }
10005
10006 /* Decode the Line Number Program (LNP) for the given line_header
10007    structure and CU.  The actual information extracted and the type
10008    of structures created from the LNP depends on the value of PST.
10009
10010    1. If PST is NULL, then this procedure uses the data from the program
10011       to create all necessary symbol tables, and their linetables.
10012
10013    2. If PST is not NULL, this procedure reads the program to determine
10014       the list of files included by the unit represented by PST, and
10015       builds all the associated partial symbol tables.
10016
10017    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10018    It is used for relative paths in the line table.
10019    NOTE: When processing partial symtabs (pst != NULL),
10020    comp_dir == pst->dirname.
10021
10022    NOTE: It is important that psymtabs have the same file name (via strcmp)
10023    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10024    symtab we don't use it in the name of the psymtabs we create.
10025    E.g. expand_line_sal requires this when finding psymtabs to expand.
10026    A good testcase for this is mb-inline.exp.  */
10027
10028 static void
10029 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
10030                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10031 {
10032   gdb_byte *line_ptr, *extended_end;
10033   gdb_byte *line_end;
10034   unsigned int bytes_read, extended_len;
10035   unsigned char op_code, extended_op, adj_opcode;
10036   CORE_ADDR baseaddr;
10037   struct objfile *objfile = cu->objfile;
10038   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10039   const int decode_for_pst_p = (pst != NULL);
10040   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10041
10042   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10043
10044   line_ptr = lh->statement_program_start;
10045   line_end = lh->statement_program_end;
10046
10047   /* Read the statement sequences until there's nothing left.  */
10048   while (line_ptr < line_end)
10049     {
10050       /* state machine registers  */
10051       CORE_ADDR address = 0;
10052       unsigned int file = 1;
10053       unsigned int line = 1;
10054       unsigned int column = 0;
10055       int is_stmt = lh->default_is_stmt;
10056       int basic_block = 0;
10057       int end_sequence = 0;
10058       CORE_ADDR addr;
10059       unsigned char op_index = 0;
10060
10061       if (!decode_for_pst_p && lh->num_file_names >= file)
10062         {
10063           /* Start a subfile for the current file of the state machine.  */
10064           /* lh->include_dirs and lh->file_names are 0-based, but the
10065              directory and file name numbers in the statement program
10066              are 1-based.  */
10067           struct file_entry *fe = &lh->file_names[file - 1];
10068           char *dir = NULL;
10069
10070           if (fe->dir_index)
10071             dir = lh->include_dirs[fe->dir_index - 1];
10072
10073           dwarf2_start_subfile (fe->name, dir, comp_dir);
10074         }
10075
10076       /* Decode the table.  */
10077       while (!end_sequence)
10078         {
10079           op_code = read_1_byte (abfd, line_ptr);
10080           line_ptr += 1;
10081           if (line_ptr > line_end)
10082             {
10083               dwarf2_debug_line_missing_end_sequence_complaint ();
10084               break;
10085             }
10086
10087           if (op_code >= lh->opcode_base)
10088             {
10089               /* Special operand.  */
10090               adj_opcode = op_code - lh->opcode_base;
10091               address += (((op_index + (adj_opcode / lh->line_range))
10092                            / lh->maximum_ops_per_instruction)
10093                           * lh->minimum_instruction_length);
10094               op_index = ((op_index + (adj_opcode / lh->line_range))
10095                           % lh->maximum_ops_per_instruction);
10096               line += lh->line_base + (adj_opcode % lh->line_range);
10097               if (lh->num_file_names < file || file == 0)
10098                 dwarf2_debug_line_missing_file_complaint ();
10099               /* For now we ignore lines not starting on an
10100                  instruction boundary.  */
10101               else if (op_index == 0)
10102                 {
10103                   lh->file_names[file - 1].included_p = 1;
10104                   if (!decode_for_pst_p && is_stmt)
10105                     {
10106                       if (last_subfile != current_subfile)
10107                         {
10108                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10109                           if (last_subfile)
10110                             record_line (last_subfile, 0, addr);
10111                           last_subfile = current_subfile;
10112                         }
10113                       /* Append row to matrix using current values.  */
10114                       addr = check_cu_functions (address, cu);
10115                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10116                       record_line (current_subfile, line, addr);
10117                     }
10118                 }
10119               basic_block = 0;
10120             }
10121           else switch (op_code)
10122             {
10123             case DW_LNS_extended_op:
10124               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10125               line_ptr += bytes_read;
10126               extended_end = line_ptr + extended_len;
10127               extended_op = read_1_byte (abfd, line_ptr);
10128               line_ptr += 1;
10129               switch (extended_op)
10130                 {
10131                 case DW_LNE_end_sequence:
10132                   end_sequence = 1;
10133                   break;
10134                 case DW_LNE_set_address:
10135                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10136                   op_index = 0;
10137                   line_ptr += bytes_read;
10138                   address += baseaddr;
10139                   break;
10140                 case DW_LNE_define_file:
10141                   {
10142                     char *cur_file;
10143                     unsigned int dir_index, mod_time, length;
10144
10145                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10146                     line_ptr += bytes_read;
10147                     dir_index =
10148                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10149                     line_ptr += bytes_read;
10150                     mod_time =
10151                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10152                     line_ptr += bytes_read;
10153                     length =
10154                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10155                     line_ptr += bytes_read;
10156                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10157                   }
10158                   break;
10159                 case DW_LNE_set_discriminator:
10160                   /* The discriminator is not interesting to the debugger;
10161                      just ignore it.  */
10162                   line_ptr = extended_end;
10163                   break;
10164                 default:
10165                   complaint (&symfile_complaints,
10166                              _("mangled .debug_line section"));
10167                   return;
10168                 }
10169               /* Make sure that we parsed the extended op correctly.  If e.g.
10170                  we expected a different address size than the producer used,
10171                  we may have read the wrong number of bytes.  */
10172               if (line_ptr != extended_end)
10173                 {
10174                   complaint (&symfile_complaints,
10175                              _("mangled .debug_line section"));
10176                   return;
10177                 }
10178               break;
10179             case DW_LNS_copy:
10180               if (lh->num_file_names < file || file == 0)
10181                 dwarf2_debug_line_missing_file_complaint ();
10182               else
10183                 {
10184                   lh->file_names[file - 1].included_p = 1;
10185                   if (!decode_for_pst_p && is_stmt)
10186                     {
10187                       if (last_subfile != current_subfile)
10188                         {
10189                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10190                           if (last_subfile)
10191                             record_line (last_subfile, 0, addr);
10192                           last_subfile = current_subfile;
10193                         }
10194                       addr = check_cu_functions (address, cu);
10195                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10196                       record_line (current_subfile, line, addr);
10197                     }
10198                 }
10199               basic_block = 0;
10200               break;
10201             case DW_LNS_advance_pc:
10202               {
10203                 CORE_ADDR adjust
10204                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10205
10206                 address += (((op_index + adjust)
10207                              / lh->maximum_ops_per_instruction)
10208                             * lh->minimum_instruction_length);
10209                 op_index = ((op_index + adjust)
10210                             % lh->maximum_ops_per_instruction);
10211                 line_ptr += bytes_read;
10212               }
10213               break;
10214             case DW_LNS_advance_line:
10215               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10216               line_ptr += bytes_read;
10217               break;
10218             case DW_LNS_set_file:
10219               {
10220                 /* The arrays lh->include_dirs and lh->file_names are
10221                    0-based, but the directory and file name numbers in
10222                    the statement program are 1-based.  */
10223                 struct file_entry *fe;
10224                 char *dir = NULL;
10225
10226                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10227                 line_ptr += bytes_read;
10228                 if (lh->num_file_names < file || file == 0)
10229                   dwarf2_debug_line_missing_file_complaint ();
10230                 else
10231                   {
10232                     fe = &lh->file_names[file - 1];
10233                     if (fe->dir_index)
10234                       dir = lh->include_dirs[fe->dir_index - 1];
10235                     if (!decode_for_pst_p)
10236                       {
10237                         last_subfile = current_subfile;
10238                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10239                       }
10240                   }
10241               }
10242               break;
10243             case DW_LNS_set_column:
10244               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10245               line_ptr += bytes_read;
10246               break;
10247             case DW_LNS_negate_stmt:
10248               is_stmt = (!is_stmt);
10249               break;
10250             case DW_LNS_set_basic_block:
10251               basic_block = 1;
10252               break;
10253             /* Add to the address register of the state machine the
10254                address increment value corresponding to special opcode
10255                255.  I.e., this value is scaled by the minimum
10256                instruction length since special opcode 255 would have
10257                scaled the the increment.  */
10258             case DW_LNS_const_add_pc:
10259               {
10260                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10261
10262                 address += (((op_index + adjust)
10263                              / lh->maximum_ops_per_instruction)
10264                             * lh->minimum_instruction_length);
10265                 op_index = ((op_index + adjust)
10266                             % lh->maximum_ops_per_instruction);
10267               }
10268               break;
10269             case DW_LNS_fixed_advance_pc:
10270               address += read_2_bytes (abfd, line_ptr);
10271               op_index = 0;
10272               line_ptr += 2;
10273               break;
10274             default:
10275               {
10276                 /* Unknown standard opcode, ignore it.  */
10277                 int i;
10278
10279                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10280                   {
10281                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10282                     line_ptr += bytes_read;
10283                   }
10284               }
10285             }
10286         }
10287       if (lh->num_file_names < file || file == 0)
10288         dwarf2_debug_line_missing_file_complaint ();
10289       else
10290         {
10291           lh->file_names[file - 1].included_p = 1;
10292           if (!decode_for_pst_p)
10293             {
10294               addr = gdbarch_addr_bits_remove (gdbarch, address);
10295               record_line (current_subfile, 0, addr);
10296             }
10297         }
10298     }
10299
10300   if (decode_for_pst_p)
10301     {
10302       int file_index;
10303
10304       /* Now that we're done scanning the Line Header Program, we can
10305          create the psymtab of each included file.  */
10306       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10307         if (lh->file_names[file_index].included_p == 1)
10308           {
10309             char *include_name =
10310               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10311             if (include_name != NULL)
10312               dwarf2_create_include_psymtab (include_name, pst, objfile);
10313           }
10314     }
10315   else
10316     {
10317       /* Make sure a symtab is created for every file, even files
10318          which contain only variables (i.e. no code with associated
10319          line numbers).  */
10320
10321       int i;
10322       struct file_entry *fe;
10323
10324       for (i = 0; i < lh->num_file_names; i++)
10325         {
10326           char *dir = NULL;
10327
10328           fe = &lh->file_names[i];
10329           if (fe->dir_index)
10330             dir = lh->include_dirs[fe->dir_index - 1];
10331           dwarf2_start_subfile (fe->name, dir, comp_dir);
10332
10333           /* Skip the main file; we don't need it, and it must be
10334              allocated last, so that it will show up before the
10335              non-primary symtabs in the objfile's symtab list.  */
10336           if (current_subfile == first_subfile)
10337             continue;
10338
10339           if (current_subfile->symtab == NULL)
10340             current_subfile->symtab = allocate_symtab (current_subfile->name,
10341                                                        cu->objfile);
10342           fe->symtab = current_subfile->symtab;
10343         }
10344     }
10345 }
10346
10347 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10348    DIRNAME the name of the source directory which contains FILENAME
10349    or NULL if not known.  COMP_DIR is the compilation directory for the
10350    linetable's compilation unit or NULL if not known.
10351    This routine tries to keep line numbers from identical absolute and
10352    relative file names in a common subfile.
10353
10354    Using the `list' example from the GDB testsuite, which resides in
10355    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10356    of /srcdir/list0.c yields the following debugging information for list0.c:
10357
10358    DW_AT_name:          /srcdir/list0.c
10359    DW_AT_comp_dir:              /compdir
10360    files.files[0].name: list0.h
10361    files.files[0].dir:  /srcdir
10362    files.files[1].name: list0.c
10363    files.files[1].dir:  /srcdir
10364
10365    The line number information for list0.c has to end up in a single
10366    subfile, so that `break /srcdir/list0.c:1' works as expected.
10367    start_subfile will ensure that this happens provided that we pass the
10368    concatenation of files.files[1].dir and files.files[1].name as the
10369    subfile's name.  */
10370
10371 static void
10372 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10373 {
10374   char *fullname;
10375
10376   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10377      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10378      second argument to start_subfile.  To be consistent, we do the
10379      same here.  In order not to lose the line information directory,
10380      we concatenate it to the filename when it makes sense.
10381      Note that the Dwarf3 standard says (speaking of filenames in line
10382      information): ``The directory index is ignored for file names
10383      that represent full path names''.  Thus ignoring dirname in the
10384      `else' branch below isn't an issue.  */
10385
10386   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10387     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10388   else
10389     fullname = filename;
10390
10391   start_subfile (fullname, comp_dir);
10392
10393   if (fullname != filename)
10394     xfree (fullname);
10395 }
10396
10397 static void
10398 var_decode_location (struct attribute *attr, struct symbol *sym,
10399                      struct dwarf2_cu *cu)
10400 {
10401   struct objfile *objfile = cu->objfile;
10402   struct comp_unit_head *cu_header = &cu->header;
10403
10404   /* NOTE drow/2003-01-30: There used to be a comment and some special
10405      code here to turn a symbol with DW_AT_external and a
10406      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10407      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10408      with some versions of binutils) where shared libraries could have
10409      relocations against symbols in their debug information - the
10410      minimal symbol would have the right address, but the debug info
10411      would not.  It's no longer necessary, because we will explicitly
10412      apply relocations when we read in the debug information now.  */
10413
10414   /* A DW_AT_location attribute with no contents indicates that a
10415      variable has been optimized away.  */
10416   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10417     {
10418       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10419       return;
10420     }
10421
10422   /* Handle one degenerate form of location expression specially, to
10423      preserve GDB's previous behavior when section offsets are
10424      specified.  If this is just a DW_OP_addr then mark this symbol
10425      as LOC_STATIC.  */
10426
10427   if (attr_form_is_block (attr)
10428       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10429       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10430     {
10431       unsigned int dummy;
10432
10433       SYMBOL_VALUE_ADDRESS (sym) =
10434         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10435       SYMBOL_CLASS (sym) = LOC_STATIC;
10436       fixup_symbol_section (sym, objfile);
10437       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10438                                               SYMBOL_SECTION (sym));
10439       return;
10440     }
10441
10442   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10443      expression evaluator, and use LOC_COMPUTED only when necessary
10444      (i.e. when the value of a register or memory location is
10445      referenced, or a thread-local block, etc.).  Then again, it might
10446      not be worthwhile.  I'm assuming that it isn't unless performance
10447      or memory numbers show me otherwise.  */
10448
10449   dwarf2_symbol_mark_computed (attr, sym, cu);
10450   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10451 }
10452
10453 /* Given a pointer to a DWARF information entry, figure out if we need
10454    to make a symbol table entry for it, and if so, create a new entry
10455    and return a pointer to it.
10456    If TYPE is NULL, determine symbol type from the die, otherwise
10457    used the passed type.
10458    If SPACE is not NULL, use it to hold the new symbol.  If it is
10459    NULL, allocate a new symbol on the objfile's obstack.  */
10460
10461 static struct symbol *
10462 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10463                  struct symbol *space)
10464 {
10465   struct objfile *objfile = cu->objfile;
10466   struct symbol *sym = NULL;
10467   char *name;
10468   struct attribute *attr = NULL;
10469   struct attribute *attr2 = NULL;
10470   CORE_ADDR baseaddr;
10471   struct pending **list_to_add = NULL;
10472
10473   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10474
10475   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10476
10477   name = dwarf2_name (die, cu);
10478   if (name)
10479     {
10480       const char *linkagename;
10481       int suppress_add = 0;
10482
10483       if (space)
10484         sym = space;
10485       else
10486         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10487       OBJSTAT (objfile, n_syms++);
10488
10489       /* Cache this symbol's name and the name's demangled form (if any).  */
10490       SYMBOL_SET_LANGUAGE (sym, cu->language);
10491       linkagename = dwarf2_physname (name, die, cu);
10492       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10493
10494       /* Fortran does not have mangling standard and the mangling does differ
10495          between gfortran, iFort etc.  */
10496       if (cu->language == language_fortran
10497           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10498         symbol_set_demangled_name (&(sym->ginfo),
10499                                    (char *) dwarf2_full_name (name, die, cu),
10500                                    NULL);
10501
10502       /* Default assumptions.
10503          Use the passed type or decode it from the die.  */
10504       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10505       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10506       if (type != NULL)
10507         SYMBOL_TYPE (sym) = type;
10508       else
10509         SYMBOL_TYPE (sym) = die_type (die, cu);
10510       attr = dwarf2_attr (die,
10511                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10512                           cu);
10513       if (attr)
10514         {
10515           SYMBOL_LINE (sym) = DW_UNSND (attr);
10516         }
10517
10518       attr = dwarf2_attr (die,
10519                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10520                           cu);
10521       if (attr)
10522         {
10523           int file_index = DW_UNSND (attr);
10524
10525           if (cu->line_header == NULL
10526               || file_index > cu->line_header->num_file_names)
10527             complaint (&symfile_complaints,
10528                        _("file index out of range"));
10529           else if (file_index > 0)
10530             {
10531               struct file_entry *fe;
10532
10533               fe = &cu->line_header->file_names[file_index - 1];
10534               SYMBOL_SYMTAB (sym) = fe->symtab;
10535             }
10536         }
10537
10538       switch (die->tag)
10539         {
10540         case DW_TAG_label:
10541           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10542           if (attr)
10543             {
10544               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10545             }
10546           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10547           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10548           SYMBOL_CLASS (sym) = LOC_LABEL;
10549           add_symbol_to_list (sym, cu->list_in_scope);
10550           break;
10551         case DW_TAG_subprogram:
10552           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10553              finish_block.  */
10554           SYMBOL_CLASS (sym) = LOC_BLOCK;
10555           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10556           if ((attr2 && (DW_UNSND (attr2) != 0))
10557               || cu->language == language_ada)
10558             {
10559               /* Subprograms marked external are stored as a global symbol.
10560                  Ada subprograms, whether marked external or not, are always
10561                  stored as a global symbol, because we want to be able to
10562                  access them globally.  For instance, we want to be able
10563                  to break on a nested subprogram without having to
10564                  specify the context.  */
10565               list_to_add = &global_symbols;
10566             }
10567           else
10568             {
10569               list_to_add = cu->list_in_scope;
10570             }
10571           break;
10572         case DW_TAG_inlined_subroutine:
10573           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10574              finish_block.  */
10575           SYMBOL_CLASS (sym) = LOC_BLOCK;
10576           SYMBOL_INLINED (sym) = 1;
10577           /* Do not add the symbol to any lists.  It will be found via
10578              BLOCK_FUNCTION from the blockvector.  */
10579           break;
10580         case DW_TAG_template_value_param:
10581           suppress_add = 1;
10582           /* Fall through.  */
10583         case DW_TAG_variable:
10584         case DW_TAG_member:
10585           /* Compilation with minimal debug info may result in variables
10586              with missing type entries. Change the misleading `void' type
10587              to something sensible.  */
10588           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10589             SYMBOL_TYPE (sym)
10590               = objfile_type (objfile)->nodebug_data_symbol;
10591
10592           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10593           /* In the case of DW_TAG_member, we should only be called for
10594              static const members.  */
10595           if (die->tag == DW_TAG_member)
10596             {
10597               /* dwarf2_add_field uses die_is_declaration,
10598                  so we do the same.  */
10599               gdb_assert (die_is_declaration (die, cu));
10600               gdb_assert (attr);
10601             }
10602           if (attr)
10603             {
10604               dwarf2_const_value (attr, sym, cu);
10605               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10606               if (!suppress_add)
10607                 {
10608                   if (attr2 && (DW_UNSND (attr2) != 0))
10609                     list_to_add = &global_symbols;
10610                   else
10611                     list_to_add = cu->list_in_scope;
10612                 }
10613               break;
10614             }
10615           attr = dwarf2_attr (die, DW_AT_location, cu);
10616           if (attr)
10617             {
10618               var_decode_location (attr, sym, cu);
10619               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10620               if (SYMBOL_CLASS (sym) == LOC_STATIC
10621                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10622                   && !dwarf2_per_objfile->has_section_at_zero)
10623                 {
10624                   /* When a static variable is eliminated by the linker,
10625                      the corresponding debug information is not stripped
10626                      out, but the variable address is set to null;
10627                      do not add such variables into symbol table.  */
10628                 }
10629               else if (attr2 && (DW_UNSND (attr2) != 0))
10630                 {
10631                   /* Workaround gfortran PR debug/40040 - it uses
10632                      DW_AT_location for variables in -fPIC libraries which may
10633                      get overriden by other libraries/executable and get
10634                      a different address.  Resolve it by the minimal symbol
10635                      which may come from inferior's executable using copy
10636                      relocation.  Make this workaround only for gfortran as for
10637                      other compilers GDB cannot guess the minimal symbol
10638                      Fortran mangling kind.  */
10639                   if (cu->language == language_fortran && die->parent
10640                       && die->parent->tag == DW_TAG_module
10641                       && cu->producer
10642                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10643                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10644
10645                   /* A variable with DW_AT_external is never static,
10646                      but it may be block-scoped.  */
10647                   list_to_add = (cu->list_in_scope == &file_symbols
10648                                  ? &global_symbols : cu->list_in_scope);
10649                 }
10650               else
10651                 list_to_add = cu->list_in_scope;
10652             }
10653           else
10654             {
10655               /* We do not know the address of this symbol.
10656                  If it is an external symbol and we have type information
10657                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10658                  The address of the variable will then be determined from
10659                  the minimal symbol table whenever the variable is
10660                  referenced.  */
10661               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10662               if (attr2 && (DW_UNSND (attr2) != 0)
10663                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10664                 {
10665                   /* A variable with DW_AT_external is never static, but it
10666                      may be block-scoped.  */
10667                   list_to_add = (cu->list_in_scope == &file_symbols
10668                                  ? &global_symbols : cu->list_in_scope);
10669
10670                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10671                 }
10672               else if (!die_is_declaration (die, cu))
10673                 {
10674                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10675                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10676                   if (!suppress_add)
10677                     list_to_add = cu->list_in_scope;
10678                 }
10679             }
10680           break;
10681         case DW_TAG_formal_parameter:
10682           /* If we are inside a function, mark this as an argument.  If
10683              not, we might be looking at an argument to an inlined function
10684              when we do not have enough information to show inlined frames;
10685              pretend it's a local variable in that case so that the user can
10686              still see it.  */
10687           if (context_stack_depth > 0
10688               && context_stack[context_stack_depth - 1].name != NULL)
10689             SYMBOL_IS_ARGUMENT (sym) = 1;
10690           attr = dwarf2_attr (die, DW_AT_location, cu);
10691           if (attr)
10692             {
10693               var_decode_location (attr, sym, cu);
10694             }
10695           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10696           if (attr)
10697             {
10698               dwarf2_const_value (attr, sym, cu);
10699             }
10700           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10701           if (attr && DW_UNSND (attr))
10702             {
10703               struct type *ref_type;
10704
10705               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10706               SYMBOL_TYPE (sym) = ref_type;
10707             }
10708
10709           list_to_add = cu->list_in_scope;
10710           break;
10711         case DW_TAG_unspecified_parameters:
10712           /* From varargs functions; gdb doesn't seem to have any
10713              interest in this information, so just ignore it for now.
10714              (FIXME?) */
10715           break;
10716         case DW_TAG_template_type_param:
10717           suppress_add = 1;
10718           /* Fall through.  */
10719         case DW_TAG_class_type:
10720         case DW_TAG_interface_type:
10721         case DW_TAG_structure_type:
10722         case DW_TAG_union_type:
10723         case DW_TAG_set_type:
10724         case DW_TAG_enumeration_type:
10725           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10726           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10727
10728           {
10729             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10730                really ever be static objects: otherwise, if you try
10731                to, say, break of a class's method and you're in a file
10732                which doesn't mention that class, it won't work unless
10733                the check for all static symbols in lookup_symbol_aux
10734                saves you.  See the OtherFileClass tests in
10735                gdb.c++/namespace.exp.  */
10736
10737             if (!suppress_add)
10738               {
10739                 list_to_add = (cu->list_in_scope == &file_symbols
10740                                && (cu->language == language_cplus
10741                                    || cu->language == language_java)
10742                                ? &global_symbols : cu->list_in_scope);
10743
10744                 /* The semantics of C++ state that "struct foo {
10745                    ... }" also defines a typedef for "foo".  A Java
10746                    class declaration also defines a typedef for the
10747                    class.  */
10748                 if (cu->language == language_cplus
10749                     || cu->language == language_java
10750                     || cu->language == language_ada)
10751                   {
10752                     /* The symbol's name is already allocated along
10753                        with this objfile, so we don't need to
10754                        duplicate it for the type.  */
10755                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10756                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10757                   }
10758               }
10759           }
10760           break;
10761         case DW_TAG_typedef:
10762           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10763           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10764           list_to_add = cu->list_in_scope;
10765           break;
10766         case DW_TAG_base_type:
10767         case DW_TAG_subrange_type:
10768           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10769           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10770           list_to_add = cu->list_in_scope;
10771           break;
10772         case DW_TAG_enumerator:
10773           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10774           if (attr)
10775             {
10776               dwarf2_const_value (attr, sym, cu);
10777             }
10778           {
10779             /* NOTE: carlton/2003-11-10: See comment above in the
10780                DW_TAG_class_type, etc. block.  */
10781
10782             list_to_add = (cu->list_in_scope == &file_symbols
10783                            && (cu->language == language_cplus
10784                                || cu->language == language_java)
10785                            ? &global_symbols : cu->list_in_scope);
10786           }
10787           break;
10788         case DW_TAG_namespace:
10789           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10790           list_to_add = &global_symbols;
10791           break;
10792         default:
10793           /* Not a tag we recognize.  Hopefully we aren't processing
10794              trash data, but since we must specifically ignore things
10795              we don't recognize, there is nothing else we should do at
10796              this point. */
10797           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10798                      dwarf_tag_name (die->tag));
10799           break;
10800         }
10801
10802       if (suppress_add)
10803         {
10804           sym->hash_next = objfile->template_symbols;
10805           objfile->template_symbols = sym;
10806           list_to_add = NULL;
10807         }
10808
10809       if (list_to_add != NULL)
10810         add_symbol_to_list (sym, list_to_add);
10811
10812       /* For the benefit of old versions of GCC, check for anonymous
10813          namespaces based on the demangled name.  */
10814       if (!processing_has_namespace_info
10815           && cu->language == language_cplus)
10816         cp_scan_for_anonymous_namespaces (sym);
10817     }
10818   return (sym);
10819 }
10820
10821 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
10822
10823 static struct symbol *
10824 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10825 {
10826   return new_symbol_full (die, type, cu, NULL);
10827 }
10828
10829 /* Given an attr with a DW_FORM_dataN value in host byte order,
10830    zero-extend it as appropriate for the symbol's type.  The DWARF
10831    standard (v4) is not entirely clear about the meaning of using
10832    DW_FORM_dataN for a constant with a signed type, where the type is
10833    wider than the data.  The conclusion of a discussion on the DWARF
10834    list was that this is unspecified.  We choose to always zero-extend
10835    because that is the interpretation long in use by GCC.  */
10836
10837 static gdb_byte *
10838 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10839                          const char *name, struct obstack *obstack,
10840                          struct dwarf2_cu *cu, long *value, int bits)
10841 {
10842   struct objfile *objfile = cu->objfile;
10843   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10844                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10845   LONGEST l = DW_UNSND (attr);
10846
10847   if (bits < sizeof (*value) * 8)
10848     {
10849       l &= ((LONGEST) 1 << bits) - 1;
10850       *value = l;
10851     }
10852   else if (bits == sizeof (*value) * 8)
10853     *value = l;
10854   else
10855     {
10856       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10857       store_unsigned_integer (bytes, bits / 8, byte_order, l);
10858       return bytes;
10859     }
10860
10861   return NULL;
10862 }
10863
10864 /* Read a constant value from an attribute.  Either set *VALUE, or if
10865    the value does not fit in *VALUE, set *BYTES - either already
10866    allocated on the objfile obstack, or newly allocated on OBSTACK,
10867    or, set *BATON, if we translated the constant to a location
10868    expression.  */
10869
10870 static void
10871 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10872                          const char *name, struct obstack *obstack,
10873                          struct dwarf2_cu *cu,
10874                          long *value, gdb_byte **bytes,
10875                          struct dwarf2_locexpr_baton **baton)
10876 {
10877   struct objfile *objfile = cu->objfile;
10878   struct comp_unit_head *cu_header = &cu->header;
10879   struct dwarf_block *blk;
10880   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10881                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10882
10883   *value = 0;
10884   *bytes = NULL;
10885   *baton = NULL;
10886
10887   switch (attr->form)
10888     {
10889     case DW_FORM_addr:
10890       {
10891         gdb_byte *data;
10892
10893         if (TYPE_LENGTH (type) != cu_header->addr_size)
10894           dwarf2_const_value_length_mismatch_complaint (name,
10895                                                         cu_header->addr_size,
10896                                                         TYPE_LENGTH (type));
10897         /* Symbols of this form are reasonably rare, so we just
10898            piggyback on the existing location code rather than writing
10899            a new implementation of symbol_computed_ops.  */
10900         *baton = obstack_alloc (&objfile->objfile_obstack,
10901                                 sizeof (struct dwarf2_locexpr_baton));
10902         (*baton)->per_cu = cu->per_cu;
10903         gdb_assert ((*baton)->per_cu);
10904
10905         (*baton)->size = 2 + cu_header->addr_size;
10906         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10907         (*baton)->data = data;
10908
10909         data[0] = DW_OP_addr;
10910         store_unsigned_integer (&data[1], cu_header->addr_size,
10911                                 byte_order, DW_ADDR (attr));
10912         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10913       }
10914       break;
10915     case DW_FORM_string:
10916     case DW_FORM_strp:
10917       /* DW_STRING is already allocated on the objfile obstack, point
10918          directly to it.  */
10919       *bytes = (gdb_byte *) DW_STRING (attr);
10920       break;
10921     case DW_FORM_block1:
10922     case DW_FORM_block2:
10923     case DW_FORM_block4:
10924     case DW_FORM_block:
10925     case DW_FORM_exprloc:
10926       blk = DW_BLOCK (attr);
10927       if (TYPE_LENGTH (type) != blk->size)
10928         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10929                                                       TYPE_LENGTH (type));
10930       *bytes = blk->data;
10931       break;
10932
10933       /* The DW_AT_const_value attributes are supposed to carry the
10934          symbol's value "represented as it would be on the target
10935          architecture."  By the time we get here, it's already been
10936          converted to host endianness, so we just need to sign- or
10937          zero-extend it as appropriate.  */
10938     case DW_FORM_data1:
10939       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10940       break;
10941     case DW_FORM_data2:
10942       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10943       break;
10944     case DW_FORM_data4:
10945       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10946       break;
10947     case DW_FORM_data8:
10948       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10949       break;
10950
10951     case DW_FORM_sdata:
10952       *value = DW_SND (attr);
10953       break;
10954
10955     case DW_FORM_udata:
10956       *value = DW_UNSND (attr);
10957       break;
10958
10959     default:
10960       complaint (&symfile_complaints,
10961                  _("unsupported const value attribute form: '%s'"),
10962                  dwarf_form_name (attr->form));
10963       *value = 0;
10964       break;
10965     }
10966 }
10967
10968
10969 /* Copy constant value from an attribute to a symbol.  */
10970
10971 static void
10972 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10973                     struct dwarf2_cu *cu)
10974 {
10975   struct objfile *objfile = cu->objfile;
10976   struct comp_unit_head *cu_header = &cu->header;
10977   long value;
10978   gdb_byte *bytes;
10979   struct dwarf2_locexpr_baton *baton;
10980
10981   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10982                            SYMBOL_PRINT_NAME (sym),
10983                            &objfile->objfile_obstack, cu,
10984                            &value, &bytes, &baton);
10985
10986   if (baton != NULL)
10987     {
10988       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10989       SYMBOL_LOCATION_BATON (sym) = baton;
10990       SYMBOL_CLASS (sym) = LOC_COMPUTED;
10991     }
10992   else if (bytes != NULL)
10993      {
10994       SYMBOL_VALUE_BYTES (sym) = bytes;
10995       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10996     }
10997   else
10998     {
10999       SYMBOL_VALUE (sym) = value;
11000       SYMBOL_CLASS (sym) = LOC_CONST;
11001     }
11002 }
11003
11004 /* Return the type of the die in question using its DW_AT_type attribute.  */
11005
11006 static struct type *
11007 die_type (struct die_info *die, struct dwarf2_cu *cu)
11008 {
11009   struct attribute *type_attr;
11010
11011   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11012   if (!type_attr)
11013     {
11014       /* A missing DW_AT_type represents a void type.  */
11015       return objfile_type (cu->objfile)->builtin_void;
11016     }
11017
11018   return lookup_die_type (die, type_attr, cu);
11019 }
11020
11021 /* True iff CU's producer generates GNAT Ada auxiliary information
11022    that allows to find parallel types through that information instead
11023    of having to do expensive parallel lookups by type name.  */
11024
11025 static int
11026 need_gnat_info (struct dwarf2_cu *cu)
11027 {
11028   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11029      of GNAT produces this auxiliary information, without any indication
11030      that it is produced.  Part of enhancing the FSF version of GNAT
11031      to produce that information will be to put in place an indicator
11032      that we can use in order to determine whether the descriptive type
11033      info is available or not.  One suggestion that has been made is
11034      to use a new attribute, attached to the CU die.  For now, assume
11035      that the descriptive type info is not available.  */
11036   return 0;
11037 }
11038
11039 /* Return the auxiliary type of the die in question using its
11040    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11041    attribute is not present.  */
11042
11043 static struct type *
11044 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11045 {
11046   struct attribute *type_attr;
11047
11048   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11049   if (!type_attr)
11050     return NULL;
11051
11052   return lookup_die_type (die, type_attr, cu);
11053 }
11054
11055 /* If DIE has a descriptive_type attribute, then set the TYPE's
11056    descriptive type accordingly.  */
11057
11058 static void
11059 set_descriptive_type (struct type *type, struct die_info *die,
11060                       struct dwarf2_cu *cu)
11061 {
11062   struct type *descriptive_type = die_descriptive_type (die, cu);
11063
11064   if (descriptive_type)
11065     {
11066       ALLOCATE_GNAT_AUX_TYPE (type);
11067       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11068     }
11069 }
11070
11071 /* Return the containing type of the die in question using its
11072    DW_AT_containing_type attribute.  */
11073
11074 static struct type *
11075 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11076 {
11077   struct attribute *type_attr;
11078
11079   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11080   if (!type_attr)
11081     error (_("Dwarf Error: Problem turning containing type into gdb type "
11082              "[in module %s]"), cu->objfile->name);
11083
11084   return lookup_die_type (die, type_attr, cu);
11085 }
11086
11087 /* Look up the type of DIE in CU using its type attribute ATTR.
11088    If there is no type substitute an error marker.  */
11089
11090 static struct type *
11091 lookup_die_type (struct die_info *die, struct attribute *attr,
11092                  struct dwarf2_cu *cu)
11093 {
11094   struct type *this_type;
11095
11096   /* First see if we have it cached.  */
11097
11098   if (is_ref_attr (attr))
11099     {
11100       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11101
11102       this_type = get_die_type_at_offset (offset, cu->per_cu);
11103     }
11104   else if (attr->form == DW_FORM_sig8)
11105     {
11106       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11107       struct dwarf2_cu *sig_cu;
11108       unsigned int offset;
11109
11110       /* sig_type will be NULL if the signatured type is missing from
11111          the debug info.  */
11112       if (sig_type == NULL)
11113         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11114                  "at 0x%x [in module %s]"),
11115                die->offset, cu->objfile->name);
11116
11117       gdb_assert (sig_type->per_cu.from_debug_types);
11118       offset = sig_type->offset + sig_type->type_offset;
11119       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11120     }
11121   else
11122     {
11123       dump_die_for_error (die);
11124       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11125              dwarf_attr_name (attr->name), cu->objfile->name);
11126     }
11127
11128   /* If not cached we need to read it in.  */
11129
11130   if (this_type == NULL)
11131     {
11132       struct die_info *type_die;
11133       struct dwarf2_cu *type_cu = cu;
11134
11135       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11136       /* If the type is cached, we should have found it above.  */
11137       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11138       this_type = read_type_die_1 (type_die, type_cu);
11139     }
11140
11141   /* If we still don't have a type use an error marker.  */
11142
11143   if (this_type == NULL)
11144     {
11145       char *message, *saved;
11146
11147       /* read_type_die already issued a complaint.  */
11148       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11149                             cu->objfile->name,
11150                             cu->header.offset,
11151                             die->offset);
11152       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11153                              message, strlen (message));
11154       xfree (message);
11155
11156       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11157     }
11158
11159   return this_type;
11160 }
11161
11162 /* Return the type in DIE, CU.
11163    Returns NULL for invalid types.
11164
11165    This first does a lookup in the appropriate type_hash table,
11166    and only reads the die in if necessary.
11167
11168    NOTE: This can be called when reading in partial or full symbols.  */
11169
11170 static struct type *
11171 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11172 {
11173   struct type *this_type;
11174
11175   this_type = get_die_type (die, cu);
11176   if (this_type)
11177     return this_type;
11178
11179   return read_type_die_1 (die, cu);
11180 }
11181
11182 /* Read the type in DIE, CU.
11183    Returns NULL for invalid types.  */
11184
11185 static struct type *
11186 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11187 {
11188   struct type *this_type = NULL;
11189
11190   switch (die->tag)
11191     {
11192     case DW_TAG_class_type:
11193     case DW_TAG_interface_type:
11194     case DW_TAG_structure_type:
11195     case DW_TAG_union_type:
11196       this_type = read_structure_type (die, cu);
11197       break;
11198     case DW_TAG_enumeration_type:
11199       this_type = read_enumeration_type (die, cu);
11200       break;
11201     case DW_TAG_subprogram:
11202     case DW_TAG_subroutine_type:
11203     case DW_TAG_inlined_subroutine:
11204       this_type = read_subroutine_type (die, cu);
11205       break;
11206     case DW_TAG_array_type:
11207       this_type = read_array_type (die, cu);
11208       break;
11209     case DW_TAG_set_type:
11210       this_type = read_set_type (die, cu);
11211       break;
11212     case DW_TAG_pointer_type:
11213       this_type = read_tag_pointer_type (die, cu);
11214       break;
11215     case DW_TAG_ptr_to_member_type:
11216       this_type = read_tag_ptr_to_member_type (die, cu);
11217       break;
11218     case DW_TAG_reference_type:
11219       this_type = read_tag_reference_type (die, cu);
11220       break;
11221     case DW_TAG_const_type:
11222       this_type = read_tag_const_type (die, cu);
11223       break;
11224     case DW_TAG_volatile_type:
11225       this_type = read_tag_volatile_type (die, cu);
11226       break;
11227     case DW_TAG_string_type:
11228       this_type = read_tag_string_type (die, cu);
11229       break;
11230     case DW_TAG_typedef:
11231       this_type = read_typedef (die, cu);
11232       break;
11233     case DW_TAG_subrange_type:
11234       this_type = read_subrange_type (die, cu);
11235       break;
11236     case DW_TAG_base_type:
11237       this_type = read_base_type (die, cu);
11238       break;
11239     case DW_TAG_unspecified_type:
11240       this_type = read_unspecified_type (die, cu);
11241       break;
11242     case DW_TAG_namespace:
11243       this_type = read_namespace_type (die, cu);
11244       break;
11245     case DW_TAG_module:
11246       this_type = read_module_type (die, cu);
11247       break;
11248     default:
11249       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11250                  dwarf_tag_name (die->tag));
11251       break;
11252     }
11253
11254   return this_type;
11255 }
11256
11257 /* Return the name of the namespace/class that DIE is defined within,
11258    or "" if we can't tell.  The caller should not xfree the result.
11259
11260    For example, if we're within the method foo() in the following
11261    code:
11262
11263    namespace N {
11264      class C {
11265        void foo () {
11266        }
11267      };
11268    }
11269
11270    then determine_prefix on foo's die will return "N::C".  */
11271
11272 static char *
11273 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11274 {
11275   struct die_info *parent, *spec_die;
11276   struct dwarf2_cu *spec_cu;
11277   struct type *parent_type;
11278
11279   if (cu->language != language_cplus && cu->language != language_java
11280       && cu->language != language_fortran)
11281     return "";
11282
11283   /* We have to be careful in the presence of DW_AT_specification.
11284      For example, with GCC 3.4, given the code
11285
11286      namespace N {
11287        void foo() {
11288          // Definition of N::foo.
11289        }
11290      }
11291
11292      then we'll have a tree of DIEs like this:
11293
11294      1: DW_TAG_compile_unit
11295        2: DW_TAG_namespace        // N
11296          3: DW_TAG_subprogram     // declaration of N::foo
11297        4: DW_TAG_subprogram       // definition of N::foo
11298             DW_AT_specification   // refers to die #3
11299
11300      Thus, when processing die #4, we have to pretend that we're in
11301      the context of its DW_AT_specification, namely the contex of die
11302      #3.  */
11303   spec_cu = cu;
11304   spec_die = die_specification (die, &spec_cu);
11305   if (spec_die == NULL)
11306     parent = die->parent;
11307   else
11308     {
11309       parent = spec_die->parent;
11310       cu = spec_cu;
11311     }
11312
11313   if (parent == NULL)
11314     return "";
11315   else if (parent->building_fullname)
11316     {
11317       const char *name;
11318       const char *parent_name;
11319
11320       /* It has been seen on RealView 2.2 built binaries,
11321          DW_TAG_template_type_param types actually _defined_ as
11322          children of the parent class:
11323
11324          enum E {};
11325          template class <class Enum> Class{};
11326          Class<enum E> class_e;
11327
11328          1: DW_TAG_class_type (Class)
11329            2: DW_TAG_enumeration_type (E)
11330              3: DW_TAG_enumerator (enum1:0)
11331              3: DW_TAG_enumerator (enum2:1)
11332              ...
11333            2: DW_TAG_template_type_param
11334               DW_AT_type  DW_FORM_ref_udata (E)
11335
11336          Besides being broken debug info, it can put GDB into an
11337          infinite loop.  Consider:
11338
11339          When we're building the full name for Class<E>, we'll start
11340          at Class, and go look over its template type parameters,
11341          finding E.  We'll then try to build the full name of E, and
11342          reach here.  We're now trying to build the full name of E,
11343          and look over the parent DIE for containing scope.  In the
11344          broken case, if we followed the parent DIE of E, we'd again
11345          find Class, and once again go look at its template type
11346          arguments, etc., etc.  Simply don't consider such parent die
11347          as source-level parent of this die (it can't be, the language
11348          doesn't allow it), and break the loop here.  */
11349       name = dwarf2_name (die, cu);
11350       parent_name = dwarf2_name (parent, cu);
11351       complaint (&symfile_complaints,
11352                  _("template param type '%s' defined within parent '%s'"),
11353                  name ? name : "<unknown>",
11354                  parent_name ? parent_name : "<unknown>");
11355       return "";
11356     }
11357   else
11358     switch (parent->tag)
11359       {
11360       case DW_TAG_namespace:
11361         parent_type = read_type_die (parent, cu);
11362         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11363            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11364            Work around this problem here.  */
11365         if (cu->language == language_cplus
11366             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11367           return "";
11368         /* We give a name to even anonymous namespaces.  */
11369         return TYPE_TAG_NAME (parent_type);
11370       case DW_TAG_class_type:
11371       case DW_TAG_interface_type:
11372       case DW_TAG_structure_type:
11373       case DW_TAG_union_type:
11374       case DW_TAG_module:
11375         parent_type = read_type_die (parent, cu);
11376         if (TYPE_TAG_NAME (parent_type) != NULL)
11377           return TYPE_TAG_NAME (parent_type);
11378         else
11379           /* An anonymous structure is only allowed non-static data
11380              members; no typedefs, no member functions, et cetera.
11381              So it does not need a prefix.  */
11382           return "";
11383       default:
11384         return determine_prefix (parent, cu);
11385       }
11386 }
11387
11388 /* Return a newly-allocated string formed by concatenating PREFIX and
11389    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11390    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
11391    perform an obconcat, otherwise allocate storage for the result.  The CU argument
11392    is used to determine the language and hence, the appropriate separator.  */
11393
11394 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11395
11396 static char *
11397 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11398                  int physname, struct dwarf2_cu *cu)
11399 {
11400   const char *lead = "";
11401   const char *sep;
11402
11403   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11404     sep = "";
11405   else if (cu->language == language_java)
11406     sep = ".";
11407   else if (cu->language == language_fortran && physname)
11408     {
11409       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11410          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11411
11412       lead = "__";
11413       sep = "_MOD_";
11414     }
11415   else
11416     sep = "::";
11417
11418   if (prefix == NULL)
11419     prefix = "";
11420   if (suffix == NULL)
11421     suffix = "";
11422
11423   if (obs == NULL)
11424     {
11425       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11426
11427       strcpy (retval, lead);
11428       strcat (retval, prefix);
11429       strcat (retval, sep);
11430       strcat (retval, suffix);
11431       return retval;
11432     }
11433   else
11434     {
11435       /* We have an obstack.  */
11436       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11437     }
11438 }
11439
11440 /* Return sibling of die, NULL if no sibling.  */
11441
11442 static struct die_info *
11443 sibling_die (struct die_info *die)
11444 {
11445   return die->sibling;
11446 }
11447
11448 /* Get name of a die, return NULL if not found.  */
11449
11450 static char *
11451 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11452                           struct obstack *obstack)
11453 {
11454   if (name && cu->language == language_cplus)
11455     {
11456       char *canon_name = cp_canonicalize_string (name);
11457
11458       if (canon_name != NULL)
11459         {
11460           if (strcmp (canon_name, name) != 0)
11461             name = obsavestring (canon_name, strlen (canon_name),
11462                                  obstack);
11463           xfree (canon_name);
11464         }
11465     }
11466
11467   return name;
11468 }
11469
11470 /* Get name of a die, return NULL if not found.  */
11471
11472 static char *
11473 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11474 {
11475   struct attribute *attr;
11476
11477   attr = dwarf2_attr (die, DW_AT_name, cu);
11478   if (!attr || !DW_STRING (attr))
11479     return NULL;
11480
11481   switch (die->tag)
11482     {
11483     case DW_TAG_compile_unit:
11484       /* Compilation units have a DW_AT_name that is a filename, not
11485          a source language identifier.  */
11486     case DW_TAG_enumeration_type:
11487     case DW_TAG_enumerator:
11488       /* These tags always have simple identifiers already; no need
11489          to canonicalize them.  */
11490       return DW_STRING (attr);
11491
11492     case DW_TAG_subprogram:
11493       /* Java constructors will all be named "<init>", so return
11494          the class name when we see this special case.  */
11495       if (cu->language == language_java
11496           && DW_STRING (attr) != NULL
11497           && strcmp (DW_STRING (attr), "<init>") == 0)
11498         {
11499           struct dwarf2_cu *spec_cu = cu;
11500           struct die_info *spec_die;
11501
11502           /* GCJ will output '<init>' for Java constructor names.
11503              For this special case, return the name of the parent class.  */
11504
11505           /* GCJ may output suprogram DIEs with AT_specification set.
11506              If so, use the name of the specified DIE.  */
11507           spec_die = die_specification (die, &spec_cu);
11508           if (spec_die != NULL)
11509             return dwarf2_name (spec_die, spec_cu);
11510
11511           do
11512             {
11513               die = die->parent;
11514               if (die->tag == DW_TAG_class_type)
11515                 return dwarf2_name (die, cu);
11516             }
11517           while (die->tag != DW_TAG_compile_unit);
11518         }
11519       break;
11520
11521     case DW_TAG_class_type:
11522     case DW_TAG_interface_type:
11523     case DW_TAG_structure_type:
11524     case DW_TAG_union_type:
11525       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11526          structures or unions.  These were of the form "._%d" in GCC 4.1,
11527          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11528          and GCC 4.4.  We work around this problem by ignoring these.  */
11529       if (strncmp (DW_STRING (attr), "._", 2) == 0
11530           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11531         return NULL;
11532       break;
11533
11534     default:
11535       break;
11536     }
11537
11538   if (!DW_STRING_IS_CANONICAL (attr))
11539     {
11540       DW_STRING (attr)
11541         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11542                                     &cu->objfile->objfile_obstack);
11543       DW_STRING_IS_CANONICAL (attr) = 1;
11544     }
11545   return DW_STRING (attr);
11546 }
11547
11548 /* Return the die that this die in an extension of, or NULL if there
11549    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11550    containing the return value on output.  */
11551
11552 static struct die_info *
11553 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11554 {
11555   struct attribute *attr;
11556
11557   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11558   if (attr == NULL)
11559     return NULL;
11560
11561   return follow_die_ref (die, attr, ext_cu);
11562 }
11563
11564 /* Convert a DIE tag into its string name.  */
11565
11566 static char *
11567 dwarf_tag_name (unsigned tag)
11568 {
11569   switch (tag)
11570     {
11571     case DW_TAG_padding:
11572       return "DW_TAG_padding";
11573     case DW_TAG_array_type:
11574       return "DW_TAG_array_type";
11575     case DW_TAG_class_type:
11576       return "DW_TAG_class_type";
11577     case DW_TAG_entry_point:
11578       return "DW_TAG_entry_point";
11579     case DW_TAG_enumeration_type:
11580       return "DW_TAG_enumeration_type";
11581     case DW_TAG_formal_parameter:
11582       return "DW_TAG_formal_parameter";
11583     case DW_TAG_imported_declaration:
11584       return "DW_TAG_imported_declaration";
11585     case DW_TAG_label:
11586       return "DW_TAG_label";
11587     case DW_TAG_lexical_block:
11588       return "DW_TAG_lexical_block";
11589     case DW_TAG_member:
11590       return "DW_TAG_member";
11591     case DW_TAG_pointer_type:
11592       return "DW_TAG_pointer_type";
11593     case DW_TAG_reference_type:
11594       return "DW_TAG_reference_type";
11595     case DW_TAG_compile_unit:
11596       return "DW_TAG_compile_unit";
11597     case DW_TAG_string_type:
11598       return "DW_TAG_string_type";
11599     case DW_TAG_structure_type:
11600       return "DW_TAG_structure_type";
11601     case DW_TAG_subroutine_type:
11602       return "DW_TAG_subroutine_type";
11603     case DW_TAG_typedef:
11604       return "DW_TAG_typedef";
11605     case DW_TAG_union_type:
11606       return "DW_TAG_union_type";
11607     case DW_TAG_unspecified_parameters:
11608       return "DW_TAG_unspecified_parameters";
11609     case DW_TAG_variant:
11610       return "DW_TAG_variant";
11611     case DW_TAG_common_block:
11612       return "DW_TAG_common_block";
11613     case DW_TAG_common_inclusion:
11614       return "DW_TAG_common_inclusion";
11615     case DW_TAG_inheritance:
11616       return "DW_TAG_inheritance";
11617     case DW_TAG_inlined_subroutine:
11618       return "DW_TAG_inlined_subroutine";
11619     case DW_TAG_module:
11620       return "DW_TAG_module";
11621     case DW_TAG_ptr_to_member_type:
11622       return "DW_TAG_ptr_to_member_type";
11623     case DW_TAG_set_type:
11624       return "DW_TAG_set_type";
11625     case DW_TAG_subrange_type:
11626       return "DW_TAG_subrange_type";
11627     case DW_TAG_with_stmt:
11628       return "DW_TAG_with_stmt";
11629     case DW_TAG_access_declaration:
11630       return "DW_TAG_access_declaration";
11631     case DW_TAG_base_type:
11632       return "DW_TAG_base_type";
11633     case DW_TAG_catch_block:
11634       return "DW_TAG_catch_block";
11635     case DW_TAG_const_type:
11636       return "DW_TAG_const_type";
11637     case DW_TAG_constant:
11638       return "DW_TAG_constant";
11639     case DW_TAG_enumerator:
11640       return "DW_TAG_enumerator";
11641     case DW_TAG_file_type:
11642       return "DW_TAG_file_type";
11643     case DW_TAG_friend:
11644       return "DW_TAG_friend";
11645     case DW_TAG_namelist:
11646       return "DW_TAG_namelist";
11647     case DW_TAG_namelist_item:
11648       return "DW_TAG_namelist_item";
11649     case DW_TAG_packed_type:
11650       return "DW_TAG_packed_type";
11651     case DW_TAG_subprogram:
11652       return "DW_TAG_subprogram";
11653     case DW_TAG_template_type_param:
11654       return "DW_TAG_template_type_param";
11655     case DW_TAG_template_value_param:
11656       return "DW_TAG_template_value_param";
11657     case DW_TAG_thrown_type:
11658       return "DW_TAG_thrown_type";
11659     case DW_TAG_try_block:
11660       return "DW_TAG_try_block";
11661     case DW_TAG_variant_part:
11662       return "DW_TAG_variant_part";
11663     case DW_TAG_variable:
11664       return "DW_TAG_variable";
11665     case DW_TAG_volatile_type:
11666       return "DW_TAG_volatile_type";
11667     case DW_TAG_dwarf_procedure:
11668       return "DW_TAG_dwarf_procedure";
11669     case DW_TAG_restrict_type:
11670       return "DW_TAG_restrict_type";
11671     case DW_TAG_interface_type:
11672       return "DW_TAG_interface_type";
11673     case DW_TAG_namespace:
11674       return "DW_TAG_namespace";
11675     case DW_TAG_imported_module:
11676       return "DW_TAG_imported_module";
11677     case DW_TAG_unspecified_type:
11678       return "DW_TAG_unspecified_type";
11679     case DW_TAG_partial_unit:
11680       return "DW_TAG_partial_unit";
11681     case DW_TAG_imported_unit:
11682       return "DW_TAG_imported_unit";
11683     case DW_TAG_condition:
11684       return "DW_TAG_condition";
11685     case DW_TAG_shared_type:
11686       return "DW_TAG_shared_type";
11687     case DW_TAG_type_unit:
11688       return "DW_TAG_type_unit";
11689     case DW_TAG_MIPS_loop:
11690       return "DW_TAG_MIPS_loop";
11691     case DW_TAG_HP_array_descriptor:
11692       return "DW_TAG_HP_array_descriptor";
11693     case DW_TAG_format_label:
11694       return "DW_TAG_format_label";
11695     case DW_TAG_function_template:
11696       return "DW_TAG_function_template";
11697     case DW_TAG_class_template:
11698       return "DW_TAG_class_template";
11699     case DW_TAG_GNU_BINCL:
11700       return "DW_TAG_GNU_BINCL";
11701     case DW_TAG_GNU_EINCL:
11702       return "DW_TAG_GNU_EINCL";
11703     case DW_TAG_upc_shared_type:
11704       return "DW_TAG_upc_shared_type";
11705     case DW_TAG_upc_strict_type:
11706       return "DW_TAG_upc_strict_type";
11707     case DW_TAG_upc_relaxed_type:
11708       return "DW_TAG_upc_relaxed_type";
11709     case DW_TAG_PGI_kanji_type:
11710       return "DW_TAG_PGI_kanji_type";
11711     case DW_TAG_PGI_interface_block:
11712       return "DW_TAG_PGI_interface_block";
11713     default:
11714       return "DW_TAG_<unknown>";
11715     }
11716 }
11717
11718 /* Convert a DWARF attribute code into its string name.  */
11719
11720 static char *
11721 dwarf_attr_name (unsigned attr)
11722 {
11723   switch (attr)
11724     {
11725     case DW_AT_sibling:
11726       return "DW_AT_sibling";
11727     case DW_AT_location:
11728       return "DW_AT_location";
11729     case DW_AT_name:
11730       return "DW_AT_name";
11731     case DW_AT_ordering:
11732       return "DW_AT_ordering";
11733     case DW_AT_subscr_data:
11734       return "DW_AT_subscr_data";
11735     case DW_AT_byte_size:
11736       return "DW_AT_byte_size";
11737     case DW_AT_bit_offset:
11738       return "DW_AT_bit_offset";
11739     case DW_AT_bit_size:
11740       return "DW_AT_bit_size";
11741     case DW_AT_element_list:
11742       return "DW_AT_element_list";
11743     case DW_AT_stmt_list:
11744       return "DW_AT_stmt_list";
11745     case DW_AT_low_pc:
11746       return "DW_AT_low_pc";
11747     case DW_AT_high_pc:
11748       return "DW_AT_high_pc";
11749     case DW_AT_language:
11750       return "DW_AT_language";
11751     case DW_AT_member:
11752       return "DW_AT_member";
11753     case DW_AT_discr:
11754       return "DW_AT_discr";
11755     case DW_AT_discr_value:
11756       return "DW_AT_discr_value";
11757     case DW_AT_visibility:
11758       return "DW_AT_visibility";
11759     case DW_AT_import:
11760       return "DW_AT_import";
11761     case DW_AT_string_length:
11762       return "DW_AT_string_length";
11763     case DW_AT_common_reference:
11764       return "DW_AT_common_reference";
11765     case DW_AT_comp_dir:
11766       return "DW_AT_comp_dir";
11767     case DW_AT_const_value:
11768       return "DW_AT_const_value";
11769     case DW_AT_containing_type:
11770       return "DW_AT_containing_type";
11771     case DW_AT_default_value:
11772       return "DW_AT_default_value";
11773     case DW_AT_inline:
11774       return "DW_AT_inline";
11775     case DW_AT_is_optional:
11776       return "DW_AT_is_optional";
11777     case DW_AT_lower_bound:
11778       return "DW_AT_lower_bound";
11779     case DW_AT_producer:
11780       return "DW_AT_producer";
11781     case DW_AT_prototyped:
11782       return "DW_AT_prototyped";
11783     case DW_AT_return_addr:
11784       return "DW_AT_return_addr";
11785     case DW_AT_start_scope:
11786       return "DW_AT_start_scope";
11787     case DW_AT_bit_stride:
11788       return "DW_AT_bit_stride";
11789     case DW_AT_upper_bound:
11790       return "DW_AT_upper_bound";
11791     case DW_AT_abstract_origin:
11792       return "DW_AT_abstract_origin";
11793     case DW_AT_accessibility:
11794       return "DW_AT_accessibility";
11795     case DW_AT_address_class:
11796       return "DW_AT_address_class";
11797     case DW_AT_artificial:
11798       return "DW_AT_artificial";
11799     case DW_AT_base_types:
11800       return "DW_AT_base_types";
11801     case DW_AT_calling_convention:
11802       return "DW_AT_calling_convention";
11803     case DW_AT_count:
11804       return "DW_AT_count";
11805     case DW_AT_data_member_location:
11806       return "DW_AT_data_member_location";
11807     case DW_AT_decl_column:
11808       return "DW_AT_decl_column";
11809     case DW_AT_decl_file:
11810       return "DW_AT_decl_file";
11811     case DW_AT_decl_line:
11812       return "DW_AT_decl_line";
11813     case DW_AT_declaration:
11814       return "DW_AT_declaration";
11815     case DW_AT_discr_list:
11816       return "DW_AT_discr_list";
11817     case DW_AT_encoding:
11818       return "DW_AT_encoding";
11819     case DW_AT_external:
11820       return "DW_AT_external";
11821     case DW_AT_frame_base:
11822       return "DW_AT_frame_base";
11823     case DW_AT_friend:
11824       return "DW_AT_friend";
11825     case DW_AT_identifier_case:
11826       return "DW_AT_identifier_case";
11827     case DW_AT_macro_info:
11828       return "DW_AT_macro_info";
11829     case DW_AT_namelist_items:
11830       return "DW_AT_namelist_items";
11831     case DW_AT_priority:
11832       return "DW_AT_priority";
11833     case DW_AT_segment:
11834       return "DW_AT_segment";
11835     case DW_AT_specification:
11836       return "DW_AT_specification";
11837     case DW_AT_static_link:
11838       return "DW_AT_static_link";
11839     case DW_AT_type:
11840       return "DW_AT_type";
11841     case DW_AT_use_location:
11842       return "DW_AT_use_location";
11843     case DW_AT_variable_parameter:
11844       return "DW_AT_variable_parameter";
11845     case DW_AT_virtuality:
11846       return "DW_AT_virtuality";
11847     case DW_AT_vtable_elem_location:
11848       return "DW_AT_vtable_elem_location";
11849     /* DWARF 3 values.  */
11850     case DW_AT_allocated:
11851       return "DW_AT_allocated";
11852     case DW_AT_associated:
11853       return "DW_AT_associated";
11854     case DW_AT_data_location:
11855       return "DW_AT_data_location";
11856     case DW_AT_byte_stride:
11857       return "DW_AT_byte_stride";
11858     case DW_AT_entry_pc:
11859       return "DW_AT_entry_pc";
11860     case DW_AT_use_UTF8:
11861       return "DW_AT_use_UTF8";
11862     case DW_AT_extension:
11863       return "DW_AT_extension";
11864     case DW_AT_ranges:
11865       return "DW_AT_ranges";
11866     case DW_AT_trampoline:
11867       return "DW_AT_trampoline";
11868     case DW_AT_call_column:
11869       return "DW_AT_call_column";
11870     case DW_AT_call_file:
11871       return "DW_AT_call_file";
11872     case DW_AT_call_line:
11873       return "DW_AT_call_line";
11874     case DW_AT_description:
11875       return "DW_AT_description";
11876     case DW_AT_binary_scale:
11877       return "DW_AT_binary_scale";
11878     case DW_AT_decimal_scale:
11879       return "DW_AT_decimal_scale";
11880     case DW_AT_small:
11881       return "DW_AT_small";
11882     case DW_AT_decimal_sign:
11883       return "DW_AT_decimal_sign";
11884     case DW_AT_digit_count:
11885       return "DW_AT_digit_count";
11886     case DW_AT_picture_string:
11887       return "DW_AT_picture_string";
11888     case DW_AT_mutable:
11889       return "DW_AT_mutable";
11890     case DW_AT_threads_scaled:
11891       return "DW_AT_threads_scaled";
11892     case DW_AT_explicit:
11893       return "DW_AT_explicit";
11894     case DW_AT_object_pointer:
11895       return "DW_AT_object_pointer";
11896     case DW_AT_endianity:
11897       return "DW_AT_endianity";
11898     case DW_AT_elemental:
11899       return "DW_AT_elemental";
11900     case DW_AT_pure:
11901       return "DW_AT_pure";
11902     case DW_AT_recursive:
11903       return "DW_AT_recursive";
11904     /* DWARF 4 values.  */
11905     case DW_AT_signature:
11906       return "DW_AT_signature";
11907     case DW_AT_linkage_name:
11908       return "DW_AT_linkage_name";
11909     /* SGI/MIPS extensions.  */
11910 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11911     case DW_AT_MIPS_fde:
11912       return "DW_AT_MIPS_fde";
11913 #endif
11914     case DW_AT_MIPS_loop_begin:
11915       return "DW_AT_MIPS_loop_begin";
11916     case DW_AT_MIPS_tail_loop_begin:
11917       return "DW_AT_MIPS_tail_loop_begin";
11918     case DW_AT_MIPS_epilog_begin:
11919       return "DW_AT_MIPS_epilog_begin";
11920     case DW_AT_MIPS_loop_unroll_factor:
11921       return "DW_AT_MIPS_loop_unroll_factor";
11922     case DW_AT_MIPS_software_pipeline_depth:
11923       return "DW_AT_MIPS_software_pipeline_depth";
11924     case DW_AT_MIPS_linkage_name:
11925       return "DW_AT_MIPS_linkage_name";
11926     case DW_AT_MIPS_stride:
11927       return "DW_AT_MIPS_stride";
11928     case DW_AT_MIPS_abstract_name:
11929       return "DW_AT_MIPS_abstract_name";
11930     case DW_AT_MIPS_clone_origin:
11931       return "DW_AT_MIPS_clone_origin";
11932     case DW_AT_MIPS_has_inlines:
11933       return "DW_AT_MIPS_has_inlines";
11934     /* HP extensions.  */
11935 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11936     case DW_AT_HP_block_index:
11937       return "DW_AT_HP_block_index";
11938 #endif
11939     case DW_AT_HP_unmodifiable:
11940       return "DW_AT_HP_unmodifiable";
11941     case DW_AT_HP_actuals_stmt_list:
11942       return "DW_AT_HP_actuals_stmt_list";
11943     case DW_AT_HP_proc_per_section:
11944       return "DW_AT_HP_proc_per_section";
11945     case DW_AT_HP_raw_data_ptr:
11946       return "DW_AT_HP_raw_data_ptr";
11947     case DW_AT_HP_pass_by_reference:
11948       return "DW_AT_HP_pass_by_reference";
11949     case DW_AT_HP_opt_level:
11950       return "DW_AT_HP_opt_level";
11951     case DW_AT_HP_prof_version_id:
11952       return "DW_AT_HP_prof_version_id";
11953     case DW_AT_HP_opt_flags:
11954       return "DW_AT_HP_opt_flags";
11955     case DW_AT_HP_cold_region_low_pc:
11956       return "DW_AT_HP_cold_region_low_pc";
11957     case DW_AT_HP_cold_region_high_pc:
11958       return "DW_AT_HP_cold_region_high_pc";
11959     case DW_AT_HP_all_variables_modifiable:
11960       return "DW_AT_HP_all_variables_modifiable";
11961     case DW_AT_HP_linkage_name:
11962       return "DW_AT_HP_linkage_name";
11963     case DW_AT_HP_prof_flags:
11964       return "DW_AT_HP_prof_flags";
11965     /* GNU extensions.  */
11966     case DW_AT_sf_names:
11967       return "DW_AT_sf_names";
11968     case DW_AT_src_info:
11969       return "DW_AT_src_info";
11970     case DW_AT_mac_info:
11971       return "DW_AT_mac_info";
11972     case DW_AT_src_coords:
11973       return "DW_AT_src_coords";
11974     case DW_AT_body_begin:
11975       return "DW_AT_body_begin";
11976     case DW_AT_body_end:
11977       return "DW_AT_body_end";
11978     case DW_AT_GNU_vector:
11979       return "DW_AT_GNU_vector";
11980     case DW_AT_GNU_odr_signature:
11981       return "DW_AT_GNU_odr_signature";
11982     /* VMS extensions.  */
11983     case DW_AT_VMS_rtnbeg_pd_address:
11984       return "DW_AT_VMS_rtnbeg_pd_address";
11985     /* UPC extension.  */
11986     case DW_AT_upc_threads_scaled:
11987       return "DW_AT_upc_threads_scaled";
11988     /* PGI (STMicroelectronics) extensions.  */
11989     case DW_AT_PGI_lbase:
11990       return "DW_AT_PGI_lbase";
11991     case DW_AT_PGI_soffset:
11992       return "DW_AT_PGI_soffset";
11993     case DW_AT_PGI_lstride:
11994       return "DW_AT_PGI_lstride";
11995     default:
11996       return "DW_AT_<unknown>";
11997     }
11998 }
11999
12000 /* Convert a DWARF value form code into its string name.  */
12001
12002 static char *
12003 dwarf_form_name (unsigned form)
12004 {
12005   switch (form)
12006     {
12007     case DW_FORM_addr:
12008       return "DW_FORM_addr";
12009     case DW_FORM_block2:
12010       return "DW_FORM_block2";
12011     case DW_FORM_block4:
12012       return "DW_FORM_block4";
12013     case DW_FORM_data2:
12014       return "DW_FORM_data2";
12015     case DW_FORM_data4:
12016       return "DW_FORM_data4";
12017     case DW_FORM_data8:
12018       return "DW_FORM_data8";
12019     case DW_FORM_string:
12020       return "DW_FORM_string";
12021     case DW_FORM_block:
12022       return "DW_FORM_block";
12023     case DW_FORM_block1:
12024       return "DW_FORM_block1";
12025     case DW_FORM_data1:
12026       return "DW_FORM_data1";
12027     case DW_FORM_flag:
12028       return "DW_FORM_flag";
12029     case DW_FORM_sdata:
12030       return "DW_FORM_sdata";
12031     case DW_FORM_strp:
12032       return "DW_FORM_strp";
12033     case DW_FORM_udata:
12034       return "DW_FORM_udata";
12035     case DW_FORM_ref_addr:
12036       return "DW_FORM_ref_addr";
12037     case DW_FORM_ref1:
12038       return "DW_FORM_ref1";
12039     case DW_FORM_ref2:
12040       return "DW_FORM_ref2";
12041     case DW_FORM_ref4:
12042       return "DW_FORM_ref4";
12043     case DW_FORM_ref8:
12044       return "DW_FORM_ref8";
12045     case DW_FORM_ref_udata:
12046       return "DW_FORM_ref_udata";
12047     case DW_FORM_indirect:
12048       return "DW_FORM_indirect";
12049     case DW_FORM_sec_offset:
12050       return "DW_FORM_sec_offset";
12051     case DW_FORM_exprloc:
12052       return "DW_FORM_exprloc";
12053     case DW_FORM_flag_present:
12054       return "DW_FORM_flag_present";
12055     case DW_FORM_sig8:
12056       return "DW_FORM_sig8";
12057     default:
12058       return "DW_FORM_<unknown>";
12059     }
12060 }
12061
12062 /* Convert a DWARF stack opcode into its string name.  */
12063
12064 const char *
12065 dwarf_stack_op_name (unsigned op, int def)
12066 {
12067   switch (op)
12068     {
12069     case DW_OP_addr:
12070       return "DW_OP_addr";
12071     case DW_OP_deref:
12072       return "DW_OP_deref";
12073     case DW_OP_const1u:
12074       return "DW_OP_const1u";
12075     case DW_OP_const1s:
12076       return "DW_OP_const1s";
12077     case DW_OP_const2u:
12078       return "DW_OP_const2u";
12079     case DW_OP_const2s:
12080       return "DW_OP_const2s";
12081     case DW_OP_const4u:
12082       return "DW_OP_const4u";
12083     case DW_OP_const4s:
12084       return "DW_OP_const4s";
12085     case DW_OP_const8u:
12086       return "DW_OP_const8u";
12087     case DW_OP_const8s:
12088       return "DW_OP_const8s";
12089     case DW_OP_constu:
12090       return "DW_OP_constu";
12091     case DW_OP_consts:
12092       return "DW_OP_consts";
12093     case DW_OP_dup:
12094       return "DW_OP_dup";
12095     case DW_OP_drop:
12096       return "DW_OP_drop";
12097     case DW_OP_over:
12098       return "DW_OP_over";
12099     case DW_OP_pick:
12100       return "DW_OP_pick";
12101     case DW_OP_swap:
12102       return "DW_OP_swap";
12103     case DW_OP_rot:
12104       return "DW_OP_rot";
12105     case DW_OP_xderef:
12106       return "DW_OP_xderef";
12107     case DW_OP_abs:
12108       return "DW_OP_abs";
12109     case DW_OP_and:
12110       return "DW_OP_and";
12111     case DW_OP_div:
12112       return "DW_OP_div";
12113     case DW_OP_minus:
12114       return "DW_OP_minus";
12115     case DW_OP_mod:
12116       return "DW_OP_mod";
12117     case DW_OP_mul:
12118       return "DW_OP_mul";
12119     case DW_OP_neg:
12120       return "DW_OP_neg";
12121     case DW_OP_not:
12122       return "DW_OP_not";
12123     case DW_OP_or:
12124       return "DW_OP_or";
12125     case DW_OP_plus:
12126       return "DW_OP_plus";
12127     case DW_OP_plus_uconst:
12128       return "DW_OP_plus_uconst";
12129     case DW_OP_shl:
12130       return "DW_OP_shl";
12131     case DW_OP_shr:
12132       return "DW_OP_shr";
12133     case DW_OP_shra:
12134       return "DW_OP_shra";
12135     case DW_OP_xor:
12136       return "DW_OP_xor";
12137     case DW_OP_bra:
12138       return "DW_OP_bra";
12139     case DW_OP_eq:
12140       return "DW_OP_eq";
12141     case DW_OP_ge:
12142       return "DW_OP_ge";
12143     case DW_OP_gt:
12144       return "DW_OP_gt";
12145     case DW_OP_le:
12146       return "DW_OP_le";
12147     case DW_OP_lt:
12148       return "DW_OP_lt";
12149     case DW_OP_ne:
12150       return "DW_OP_ne";
12151     case DW_OP_skip:
12152       return "DW_OP_skip";
12153     case DW_OP_lit0:
12154       return "DW_OP_lit0";
12155     case DW_OP_lit1:
12156       return "DW_OP_lit1";
12157     case DW_OP_lit2:
12158       return "DW_OP_lit2";
12159     case DW_OP_lit3:
12160       return "DW_OP_lit3";
12161     case DW_OP_lit4:
12162       return "DW_OP_lit4";
12163     case DW_OP_lit5:
12164       return "DW_OP_lit5";
12165     case DW_OP_lit6:
12166       return "DW_OP_lit6";
12167     case DW_OP_lit7:
12168       return "DW_OP_lit7";
12169     case DW_OP_lit8:
12170       return "DW_OP_lit8";
12171     case DW_OP_lit9:
12172       return "DW_OP_lit9";
12173     case DW_OP_lit10:
12174       return "DW_OP_lit10";
12175     case DW_OP_lit11:
12176       return "DW_OP_lit11";
12177     case DW_OP_lit12:
12178       return "DW_OP_lit12";
12179     case DW_OP_lit13:
12180       return "DW_OP_lit13";
12181     case DW_OP_lit14:
12182       return "DW_OP_lit14";
12183     case DW_OP_lit15:
12184       return "DW_OP_lit15";
12185     case DW_OP_lit16:
12186       return "DW_OP_lit16";
12187     case DW_OP_lit17:
12188       return "DW_OP_lit17";
12189     case DW_OP_lit18:
12190       return "DW_OP_lit18";
12191     case DW_OP_lit19:
12192       return "DW_OP_lit19";
12193     case DW_OP_lit20:
12194       return "DW_OP_lit20";
12195     case DW_OP_lit21:
12196       return "DW_OP_lit21";
12197     case DW_OP_lit22:
12198       return "DW_OP_lit22";
12199     case DW_OP_lit23:
12200       return "DW_OP_lit23";
12201     case DW_OP_lit24:
12202       return "DW_OP_lit24";
12203     case DW_OP_lit25:
12204       return "DW_OP_lit25";
12205     case DW_OP_lit26:
12206       return "DW_OP_lit26";
12207     case DW_OP_lit27:
12208       return "DW_OP_lit27";
12209     case DW_OP_lit28:
12210       return "DW_OP_lit28";
12211     case DW_OP_lit29:
12212       return "DW_OP_lit29";
12213     case DW_OP_lit30:
12214       return "DW_OP_lit30";
12215     case DW_OP_lit31:
12216       return "DW_OP_lit31";
12217     case DW_OP_reg0:
12218       return "DW_OP_reg0";
12219     case DW_OP_reg1:
12220       return "DW_OP_reg1";
12221     case DW_OP_reg2:
12222       return "DW_OP_reg2";
12223     case DW_OP_reg3:
12224       return "DW_OP_reg3";
12225     case DW_OP_reg4:
12226       return "DW_OP_reg4";
12227     case DW_OP_reg5:
12228       return "DW_OP_reg5";
12229     case DW_OP_reg6:
12230       return "DW_OP_reg6";
12231     case DW_OP_reg7:
12232       return "DW_OP_reg7";
12233     case DW_OP_reg8:
12234       return "DW_OP_reg8";
12235     case DW_OP_reg9:
12236       return "DW_OP_reg9";
12237     case DW_OP_reg10:
12238       return "DW_OP_reg10";
12239     case DW_OP_reg11:
12240       return "DW_OP_reg11";
12241     case DW_OP_reg12:
12242       return "DW_OP_reg12";
12243     case DW_OP_reg13:
12244       return "DW_OP_reg13";
12245     case DW_OP_reg14:
12246       return "DW_OP_reg14";
12247     case DW_OP_reg15:
12248       return "DW_OP_reg15";
12249     case DW_OP_reg16:
12250       return "DW_OP_reg16";
12251     case DW_OP_reg17:
12252       return "DW_OP_reg17";
12253     case DW_OP_reg18:
12254       return "DW_OP_reg18";
12255     case DW_OP_reg19:
12256       return "DW_OP_reg19";
12257     case DW_OP_reg20:
12258       return "DW_OP_reg20";
12259     case DW_OP_reg21:
12260       return "DW_OP_reg21";
12261     case DW_OP_reg22:
12262       return "DW_OP_reg22";
12263     case DW_OP_reg23:
12264       return "DW_OP_reg23";
12265     case DW_OP_reg24:
12266       return "DW_OP_reg24";
12267     case DW_OP_reg25:
12268       return "DW_OP_reg25";
12269     case DW_OP_reg26:
12270       return "DW_OP_reg26";
12271     case DW_OP_reg27:
12272       return "DW_OP_reg27";
12273     case DW_OP_reg28:
12274       return "DW_OP_reg28";
12275     case DW_OP_reg29:
12276       return "DW_OP_reg29";
12277     case DW_OP_reg30:
12278       return "DW_OP_reg30";
12279     case DW_OP_reg31:
12280       return "DW_OP_reg31";
12281     case DW_OP_breg0:
12282       return "DW_OP_breg0";
12283     case DW_OP_breg1:
12284       return "DW_OP_breg1";
12285     case DW_OP_breg2:
12286       return "DW_OP_breg2";
12287     case DW_OP_breg3:
12288       return "DW_OP_breg3";
12289     case DW_OP_breg4:
12290       return "DW_OP_breg4";
12291     case DW_OP_breg5:
12292       return "DW_OP_breg5";
12293     case DW_OP_breg6:
12294       return "DW_OP_breg6";
12295     case DW_OP_breg7:
12296       return "DW_OP_breg7";
12297     case DW_OP_breg8:
12298       return "DW_OP_breg8";
12299     case DW_OP_breg9:
12300       return "DW_OP_breg9";
12301     case DW_OP_breg10:
12302       return "DW_OP_breg10";
12303     case DW_OP_breg11:
12304       return "DW_OP_breg11";
12305     case DW_OP_breg12:
12306       return "DW_OP_breg12";
12307     case DW_OP_breg13:
12308       return "DW_OP_breg13";
12309     case DW_OP_breg14:
12310       return "DW_OP_breg14";
12311     case DW_OP_breg15:
12312       return "DW_OP_breg15";
12313     case DW_OP_breg16:
12314       return "DW_OP_breg16";
12315     case DW_OP_breg17:
12316       return "DW_OP_breg17";
12317     case DW_OP_breg18:
12318       return "DW_OP_breg18";
12319     case DW_OP_breg19:
12320       return "DW_OP_breg19";
12321     case DW_OP_breg20:
12322       return "DW_OP_breg20";
12323     case DW_OP_breg21:
12324       return "DW_OP_breg21";
12325     case DW_OP_breg22:
12326       return "DW_OP_breg22";
12327     case DW_OP_breg23:
12328       return "DW_OP_breg23";
12329     case DW_OP_breg24:
12330       return "DW_OP_breg24";
12331     case DW_OP_breg25:
12332       return "DW_OP_breg25";
12333     case DW_OP_breg26:
12334       return "DW_OP_breg26";
12335     case DW_OP_breg27:
12336       return "DW_OP_breg27";
12337     case DW_OP_breg28:
12338       return "DW_OP_breg28";
12339     case DW_OP_breg29:
12340       return "DW_OP_breg29";
12341     case DW_OP_breg30:
12342       return "DW_OP_breg30";
12343     case DW_OP_breg31:
12344       return "DW_OP_breg31";
12345     case DW_OP_regx:
12346       return "DW_OP_regx";
12347     case DW_OP_fbreg:
12348       return "DW_OP_fbreg";
12349     case DW_OP_bregx:
12350       return "DW_OP_bregx";
12351     case DW_OP_piece:
12352       return "DW_OP_piece";
12353     case DW_OP_deref_size:
12354       return "DW_OP_deref_size";
12355     case DW_OP_xderef_size:
12356       return "DW_OP_xderef_size";
12357     case DW_OP_nop:
12358       return "DW_OP_nop";
12359     /* DWARF 3 extensions.  */
12360     case DW_OP_push_object_address:
12361       return "DW_OP_push_object_address";
12362     case DW_OP_call2:
12363       return "DW_OP_call2";
12364     case DW_OP_call4:
12365       return "DW_OP_call4";
12366     case DW_OP_call_ref:
12367       return "DW_OP_call_ref";
12368     case DW_OP_form_tls_address:
12369       return "DW_OP_form_tls_address";
12370     case DW_OP_call_frame_cfa:
12371       return "DW_OP_call_frame_cfa";
12372     case DW_OP_bit_piece:
12373       return "DW_OP_bit_piece";
12374     /* DWARF 4 extensions.  */
12375     case DW_OP_implicit_value:
12376       return "DW_OP_implicit_value";
12377     case DW_OP_stack_value:
12378       return "DW_OP_stack_value";
12379     /* GNU extensions.  */
12380     case DW_OP_GNU_push_tls_address:
12381       return "DW_OP_GNU_push_tls_address";
12382     case DW_OP_GNU_uninit:
12383       return "DW_OP_GNU_uninit";
12384     default:
12385       return def ? "OP_<unknown>" : NULL;
12386     }
12387 }
12388
12389 static char *
12390 dwarf_bool_name (unsigned mybool)
12391 {
12392   if (mybool)
12393     return "TRUE";
12394   else
12395     return "FALSE";
12396 }
12397
12398 /* Convert a DWARF type code into its string name.  */
12399
12400 static char *
12401 dwarf_type_encoding_name (unsigned enc)
12402 {
12403   switch (enc)
12404     {
12405     case DW_ATE_void:
12406       return "DW_ATE_void";
12407     case DW_ATE_address:
12408       return "DW_ATE_address";
12409     case DW_ATE_boolean:
12410       return "DW_ATE_boolean";
12411     case DW_ATE_complex_float:
12412       return "DW_ATE_complex_float";
12413     case DW_ATE_float:
12414       return "DW_ATE_float";
12415     case DW_ATE_signed:
12416       return "DW_ATE_signed";
12417     case DW_ATE_signed_char:
12418       return "DW_ATE_signed_char";
12419     case DW_ATE_unsigned:
12420       return "DW_ATE_unsigned";
12421     case DW_ATE_unsigned_char:
12422       return "DW_ATE_unsigned_char";
12423     /* DWARF 3.  */
12424     case DW_ATE_imaginary_float:
12425       return "DW_ATE_imaginary_float";
12426     case DW_ATE_packed_decimal:
12427       return "DW_ATE_packed_decimal";
12428     case DW_ATE_numeric_string:
12429       return "DW_ATE_numeric_string";
12430     case DW_ATE_edited:
12431       return "DW_ATE_edited";
12432     case DW_ATE_signed_fixed:
12433       return "DW_ATE_signed_fixed";
12434     case DW_ATE_unsigned_fixed:
12435       return "DW_ATE_unsigned_fixed";
12436     case DW_ATE_decimal_float:
12437       return "DW_ATE_decimal_float";
12438     /* DWARF 4.  */
12439     case DW_ATE_UTF:
12440       return "DW_ATE_UTF";
12441     /* HP extensions.  */
12442     case DW_ATE_HP_float80:
12443       return "DW_ATE_HP_float80";
12444     case DW_ATE_HP_complex_float80:
12445       return "DW_ATE_HP_complex_float80";
12446     case DW_ATE_HP_float128:
12447       return "DW_ATE_HP_float128";
12448     case DW_ATE_HP_complex_float128:
12449       return "DW_ATE_HP_complex_float128";
12450     case DW_ATE_HP_floathpintel:
12451       return "DW_ATE_HP_floathpintel";
12452     case DW_ATE_HP_imaginary_float80:
12453       return "DW_ATE_HP_imaginary_float80";
12454     case DW_ATE_HP_imaginary_float128:
12455       return "DW_ATE_HP_imaginary_float128";
12456     default:
12457       return "DW_ATE_<unknown>";
12458     }
12459 }
12460
12461 /* Convert a DWARF call frame info operation to its string name. */
12462
12463 #if 0
12464 static char *
12465 dwarf_cfi_name (unsigned cfi_opc)
12466 {
12467   switch (cfi_opc)
12468     {
12469     case DW_CFA_advance_loc:
12470       return "DW_CFA_advance_loc";
12471     case DW_CFA_offset:
12472       return "DW_CFA_offset";
12473     case DW_CFA_restore:
12474       return "DW_CFA_restore";
12475     case DW_CFA_nop:
12476       return "DW_CFA_nop";
12477     case DW_CFA_set_loc:
12478       return "DW_CFA_set_loc";
12479     case DW_CFA_advance_loc1:
12480       return "DW_CFA_advance_loc1";
12481     case DW_CFA_advance_loc2:
12482       return "DW_CFA_advance_loc2";
12483     case DW_CFA_advance_loc4:
12484       return "DW_CFA_advance_loc4";
12485     case DW_CFA_offset_extended:
12486       return "DW_CFA_offset_extended";
12487     case DW_CFA_restore_extended:
12488       return "DW_CFA_restore_extended";
12489     case DW_CFA_undefined:
12490       return "DW_CFA_undefined";
12491     case DW_CFA_same_value:
12492       return "DW_CFA_same_value";
12493     case DW_CFA_register:
12494       return "DW_CFA_register";
12495     case DW_CFA_remember_state:
12496       return "DW_CFA_remember_state";
12497     case DW_CFA_restore_state:
12498       return "DW_CFA_restore_state";
12499     case DW_CFA_def_cfa:
12500       return "DW_CFA_def_cfa";
12501     case DW_CFA_def_cfa_register:
12502       return "DW_CFA_def_cfa_register";
12503     case DW_CFA_def_cfa_offset:
12504       return "DW_CFA_def_cfa_offset";
12505     /* DWARF 3.  */
12506     case DW_CFA_def_cfa_expression:
12507       return "DW_CFA_def_cfa_expression";
12508     case DW_CFA_expression:
12509       return "DW_CFA_expression";
12510     case DW_CFA_offset_extended_sf:
12511       return "DW_CFA_offset_extended_sf";
12512     case DW_CFA_def_cfa_sf:
12513       return "DW_CFA_def_cfa_sf";
12514     case DW_CFA_def_cfa_offset_sf:
12515       return "DW_CFA_def_cfa_offset_sf";
12516     case DW_CFA_val_offset:
12517       return "DW_CFA_val_offset";
12518     case DW_CFA_val_offset_sf:
12519       return "DW_CFA_val_offset_sf";
12520     case DW_CFA_val_expression:
12521       return "DW_CFA_val_expression";
12522     /* SGI/MIPS specific.  */
12523     case DW_CFA_MIPS_advance_loc8:
12524       return "DW_CFA_MIPS_advance_loc8";
12525     /* GNU extensions.  */
12526     case DW_CFA_GNU_window_save:
12527       return "DW_CFA_GNU_window_save";
12528     case DW_CFA_GNU_args_size:
12529       return "DW_CFA_GNU_args_size";
12530     case DW_CFA_GNU_negative_offset_extended:
12531       return "DW_CFA_GNU_negative_offset_extended";
12532     default:
12533       return "DW_CFA_<unknown>";
12534     }
12535 }
12536 #endif
12537
12538 static void
12539 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12540 {
12541   unsigned int i;
12542
12543   print_spaces (indent, f);
12544   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12545            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12546
12547   if (die->parent != NULL)
12548     {
12549       print_spaces (indent, f);
12550       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12551                           die->parent->offset);
12552     }
12553
12554   print_spaces (indent, f);
12555   fprintf_unfiltered (f, "  has children: %s\n",
12556            dwarf_bool_name (die->child != NULL));
12557
12558   print_spaces (indent, f);
12559   fprintf_unfiltered (f, "  attributes:\n");
12560
12561   for (i = 0; i < die->num_attrs; ++i)
12562     {
12563       print_spaces (indent, f);
12564       fprintf_unfiltered (f, "    %s (%s) ",
12565                dwarf_attr_name (die->attrs[i].name),
12566                dwarf_form_name (die->attrs[i].form));
12567
12568       switch (die->attrs[i].form)
12569         {
12570         case DW_FORM_ref_addr:
12571         case DW_FORM_addr:
12572           fprintf_unfiltered (f, "address: ");
12573           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12574           break;
12575         case DW_FORM_block2:
12576         case DW_FORM_block4:
12577         case DW_FORM_block:
12578         case DW_FORM_block1:
12579           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12580           break;
12581         case DW_FORM_exprloc:
12582           fprintf_unfiltered (f, "expression: size %u",
12583                               DW_BLOCK (&die->attrs[i])->size);
12584           break;
12585         case DW_FORM_ref1:
12586         case DW_FORM_ref2:
12587         case DW_FORM_ref4:
12588           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12589                               (long) (DW_ADDR (&die->attrs[i])));
12590           break;
12591         case DW_FORM_data1:
12592         case DW_FORM_data2:
12593         case DW_FORM_data4:
12594         case DW_FORM_data8:
12595         case DW_FORM_udata:
12596         case DW_FORM_sdata:
12597           fprintf_unfiltered (f, "constant: %s",
12598                               pulongest (DW_UNSND (&die->attrs[i])));
12599           break;
12600         case DW_FORM_sec_offset:
12601           fprintf_unfiltered (f, "section offset: %s",
12602                               pulongest (DW_UNSND (&die->attrs[i])));
12603           break;
12604         case DW_FORM_sig8:
12605           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12606             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12607                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12608           else
12609             fprintf_unfiltered (f, "signatured type, offset: unknown");
12610           break;
12611         case DW_FORM_string:
12612         case DW_FORM_strp:
12613           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12614                    DW_STRING (&die->attrs[i])
12615                    ? DW_STRING (&die->attrs[i]) : "",
12616                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12617           break;
12618         case DW_FORM_flag:
12619           if (DW_UNSND (&die->attrs[i]))
12620             fprintf_unfiltered (f, "flag: TRUE");
12621           else
12622             fprintf_unfiltered (f, "flag: FALSE");
12623           break;
12624         case DW_FORM_flag_present:
12625           fprintf_unfiltered (f, "flag: TRUE");
12626           break;
12627         case DW_FORM_indirect:
12628           /* the reader will have reduced the indirect form to
12629              the "base form" so this form should not occur */
12630           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12631           break;
12632         default:
12633           fprintf_unfiltered (f, "unsupported attribute form: %d.",
12634                    die->attrs[i].form);
12635           break;
12636         }
12637       fprintf_unfiltered (f, "\n");
12638     }
12639 }
12640
12641 static void
12642 dump_die_for_error (struct die_info *die)
12643 {
12644   dump_die_shallow (gdb_stderr, 0, die);
12645 }
12646
12647 static void
12648 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12649 {
12650   int indent = level * 4;
12651
12652   gdb_assert (die != NULL);
12653
12654   if (level >= max_level)
12655     return;
12656
12657   dump_die_shallow (f, indent, die);
12658
12659   if (die->child != NULL)
12660     {
12661       print_spaces (indent, f);
12662       fprintf_unfiltered (f, "  Children:");
12663       if (level + 1 < max_level)
12664         {
12665           fprintf_unfiltered (f, "\n");
12666           dump_die_1 (f, level + 1, max_level, die->child);
12667         }
12668       else
12669         {
12670           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12671         }
12672     }
12673
12674   if (die->sibling != NULL && level > 0)
12675     {
12676       dump_die_1 (f, level, max_level, die->sibling);
12677     }
12678 }
12679
12680 /* This is called from the pdie macro in gdbinit.in.
12681    It's not static so gcc will keep a copy callable from gdb.  */
12682
12683 void
12684 dump_die (struct die_info *die, int max_level)
12685 {
12686   dump_die_1 (gdb_stdlog, 0, max_level, die);
12687 }
12688
12689 static void
12690 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12691 {
12692   void **slot;
12693
12694   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12695
12696   *slot = die;
12697 }
12698
12699 static int
12700 is_ref_attr (struct attribute *attr)
12701 {
12702   switch (attr->form)
12703     {
12704     case DW_FORM_ref_addr:
12705     case DW_FORM_ref1:
12706     case DW_FORM_ref2:
12707     case DW_FORM_ref4:
12708     case DW_FORM_ref8:
12709     case DW_FORM_ref_udata:
12710       return 1;
12711     default:
12712       return 0;
12713     }
12714 }
12715
12716 static unsigned int
12717 dwarf2_get_ref_die_offset (struct attribute *attr)
12718 {
12719   if (is_ref_attr (attr))
12720     return DW_ADDR (attr);
12721
12722   complaint (&symfile_complaints,
12723              _("unsupported die ref attribute form: '%s'"),
12724              dwarf_form_name (attr->form));
12725   return 0;
12726 }
12727
12728 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
12729  * the value held by the attribute is not constant.  */
12730
12731 static LONGEST
12732 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12733 {
12734   if (attr->form == DW_FORM_sdata)
12735     return DW_SND (attr);
12736   else if (attr->form == DW_FORM_udata
12737            || attr->form == DW_FORM_data1
12738            || attr->form == DW_FORM_data2
12739            || attr->form == DW_FORM_data4
12740            || attr->form == DW_FORM_data8)
12741     return DW_UNSND (attr);
12742   else
12743     {
12744       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12745                  dwarf_form_name (attr->form));
12746       return default_value;
12747     }
12748 }
12749
12750 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
12751    unit and add it to our queue.
12752    The result is non-zero if PER_CU was queued, otherwise the result is zero
12753    meaning either PER_CU is already queued or it is already loaded.  */
12754
12755 static int
12756 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12757                        struct dwarf2_per_cu_data *per_cu)
12758 {
12759   /* We may arrive here during partial symbol reading, if we need full
12760      DIEs to process an unusual case (e.g. template arguments).  Do
12761      not queue PER_CU, just tell our caller to load its DIEs.  */
12762   if (dwarf2_per_objfile->reading_partial_symbols)
12763     {
12764       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12765         return 1;
12766       return 0;
12767     }
12768
12769   /* Mark the dependence relation so that we don't flush PER_CU
12770      too early.  */
12771   dwarf2_add_dependence (this_cu, per_cu);
12772
12773   /* If it's already on the queue, we have nothing to do.  */
12774   if (per_cu->queued)
12775     return 0;
12776
12777   /* If the compilation unit is already loaded, just mark it as
12778      used.  */
12779   if (per_cu->cu != NULL)
12780     {
12781       per_cu->cu->last_used = 0;
12782       return 0;
12783     }
12784
12785   /* Add it to the queue.  */
12786   queue_comp_unit (per_cu, this_cu->objfile);
12787
12788   return 1;
12789 }
12790
12791 /* Follow reference or signature attribute ATTR of SRC_DIE.
12792    On entry *REF_CU is the CU of SRC_DIE.
12793    On exit *REF_CU is the CU of the result.  */
12794
12795 static struct die_info *
12796 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12797                        struct dwarf2_cu **ref_cu)
12798 {
12799   struct die_info *die;
12800
12801   if (is_ref_attr (attr))
12802     die = follow_die_ref (src_die, attr, ref_cu);
12803   else if (attr->form == DW_FORM_sig8)
12804     die = follow_die_sig (src_die, attr, ref_cu);
12805   else
12806     {
12807       dump_die_for_error (src_die);
12808       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12809              (*ref_cu)->objfile->name);
12810     }
12811
12812   return die;
12813 }
12814
12815 /* Follow reference OFFSET.
12816    On entry *REF_CU is the CU of the source die referencing OFFSET.
12817    On exit *REF_CU is the CU of the result.
12818    Returns NULL if OFFSET is invalid.  */
12819
12820 static struct die_info *
12821 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12822 {
12823   struct die_info temp_die;
12824   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12825
12826   gdb_assert (cu->per_cu != NULL);
12827
12828   target_cu = cu;
12829
12830   if (cu->per_cu->from_debug_types)
12831     {
12832       /* .debug_types CUs cannot reference anything outside their CU.
12833          If they need to, they have to reference a signatured type via
12834          DW_FORM_sig8.  */
12835       if (! offset_in_cu_p (&cu->header, offset))
12836         return NULL;
12837     }
12838   else if (! offset_in_cu_p (&cu->header, offset))
12839     {
12840       struct dwarf2_per_cu_data *per_cu;
12841
12842       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12843
12844       /* If necessary, add it to the queue and load its DIEs.  */
12845       if (maybe_queue_comp_unit (cu, per_cu))
12846         load_full_comp_unit (per_cu, cu->objfile);
12847
12848       target_cu = per_cu->cu;
12849     }
12850   else if (cu->dies == NULL)
12851     {
12852       /* We're loading full DIEs during partial symbol reading.  */
12853       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12854       load_full_comp_unit (cu->per_cu, cu->objfile);
12855     }
12856
12857   *ref_cu = target_cu;
12858   temp_die.offset = offset;
12859   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12860 }
12861
12862 /* Follow reference attribute ATTR of SRC_DIE.
12863    On entry *REF_CU is the CU of SRC_DIE.
12864    On exit *REF_CU is the CU of the result.  */
12865
12866 static struct die_info *
12867 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12868                 struct dwarf2_cu **ref_cu)
12869 {
12870   unsigned int offset = dwarf2_get_ref_die_offset (attr);
12871   struct dwarf2_cu *cu = *ref_cu;
12872   struct die_info *die;
12873
12874   die = follow_die_offset (offset, ref_cu);
12875   if (!die)
12876     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12877            "at 0x%x [in module %s]"),
12878            offset, src_die->offset, cu->objfile->name);
12879
12880   return die;
12881 }
12882
12883 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
12884    value is intended for DW_OP_call*.  */
12885
12886 struct dwarf2_locexpr_baton
12887 dwarf2_fetch_die_location_block (unsigned int offset,
12888                                  struct dwarf2_per_cu_data *per_cu)
12889 {
12890   struct dwarf2_cu *cu = per_cu->cu;
12891   struct die_info *die;
12892   struct attribute *attr;
12893   struct dwarf2_locexpr_baton retval;
12894
12895   die = follow_die_offset (offset, &cu);
12896   if (!die)
12897     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12898            offset, per_cu->cu->objfile->name);
12899
12900   attr = dwarf2_attr (die, DW_AT_location, cu);
12901   if (!attr)
12902     {
12903       /* DWARF: "If there is no such attribute, then there is no effect.".  */
12904
12905       retval.data = NULL;
12906       retval.size = 0;
12907     }
12908   else
12909     {
12910       if (!attr_form_is_block (attr))
12911         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12912                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12913                offset, per_cu->cu->objfile->name);
12914
12915       retval.data = DW_BLOCK (attr)->data;
12916       retval.size = DW_BLOCK (attr)->size;
12917     }
12918   retval.per_cu = cu->per_cu;
12919   return retval;
12920 }
12921
12922 /* Follow the signature attribute ATTR in SRC_DIE.
12923    On entry *REF_CU is the CU of SRC_DIE.
12924    On exit *REF_CU is the CU of the result.  */
12925
12926 static struct die_info *
12927 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12928                 struct dwarf2_cu **ref_cu)
12929 {
12930   struct objfile *objfile = (*ref_cu)->objfile;
12931   struct die_info temp_die;
12932   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12933   struct dwarf2_cu *sig_cu;
12934   struct die_info *die;
12935
12936   /* sig_type will be NULL if the signatured type is missing from
12937      the debug info.  */
12938   if (sig_type == NULL)
12939     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12940              "at 0x%x [in module %s]"),
12941            src_die->offset, objfile->name);
12942
12943   /* If necessary, add it to the queue and load its DIEs.  */
12944
12945   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12946     read_signatured_type (objfile, sig_type);
12947
12948   gdb_assert (sig_type->per_cu.cu != NULL);
12949
12950   sig_cu = sig_type->per_cu.cu;
12951   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12952   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12953   if (die)
12954     {
12955       *ref_cu = sig_cu;
12956       return die;
12957     }
12958
12959   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12960          "at 0x%x [in module %s]"),
12961          sig_type->type_offset, src_die->offset, objfile->name);
12962 }
12963
12964 /* Given an offset of a signatured type, return its signatured_type.  */
12965
12966 static struct signatured_type *
12967 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12968 {
12969   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12970   unsigned int length, initial_length_size;
12971   unsigned int sig_offset;
12972   struct signatured_type find_entry, *type_sig;
12973
12974   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12975   sig_offset = (initial_length_size
12976                 + 2 /*version*/
12977                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12978                 + 1 /*address_size*/);
12979   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12980   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12981
12982   /* This is only used to lookup previously recorded types.
12983      If we didn't find it, it's our bug.  */
12984   gdb_assert (type_sig != NULL);
12985   gdb_assert (offset == type_sig->offset);
12986
12987   return type_sig;
12988 }
12989
12990 /* Read in signatured type at OFFSET and build its CU and die(s).  */
12991
12992 static void
12993 read_signatured_type_at_offset (struct objfile *objfile,
12994                                 unsigned int offset)
12995 {
12996   struct signatured_type *type_sig;
12997
12998   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12999
13000   /* We have the section offset, but we need the signature to do the
13001      hash table lookup.  */
13002   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13003
13004   gdb_assert (type_sig->per_cu.cu == NULL);
13005
13006   read_signatured_type (objfile, type_sig);
13007
13008   gdb_assert (type_sig->per_cu.cu != NULL);
13009 }
13010
13011 /* Read in a signatured type and build its CU and DIEs.  */
13012
13013 static void
13014 read_signatured_type (struct objfile *objfile,
13015                       struct signatured_type *type_sig)
13016 {
13017   gdb_byte *types_ptr;
13018   struct die_reader_specs reader_specs;
13019   struct dwarf2_cu *cu;
13020   ULONGEST signature;
13021   struct cleanup *back_to, *free_cu_cleanup;
13022   struct attribute *attr;
13023
13024   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13025   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13026
13027   gdb_assert (type_sig->per_cu.cu == NULL);
13028
13029   cu = xmalloc (sizeof (struct dwarf2_cu));
13030   memset (cu, 0, sizeof (struct dwarf2_cu));
13031   obstack_init (&cu->comp_unit_obstack);
13032   cu->objfile = objfile;
13033   type_sig->per_cu.cu = cu;
13034   cu->per_cu = &type_sig->per_cu;
13035
13036   /* If an error occurs while loading, release our storage.  */
13037   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13038
13039   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13040                                         types_ptr, objfile->obfd);
13041   gdb_assert (signature == type_sig->signature);
13042
13043   cu->die_hash
13044     = htab_create_alloc_ex (cu->header.length / 12,
13045                             die_hash,
13046                             die_eq,
13047                             NULL,
13048                             &cu->comp_unit_obstack,
13049                             hashtab_obstack_allocate,
13050                             dummy_obstack_deallocate);
13051
13052   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13053   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13054
13055   init_cu_die_reader (&reader_specs, cu);
13056
13057   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13058                                     NULL /*parent*/);
13059
13060   /* We try not to read any attributes in this function, because not
13061      all objfiles needed for references have been loaded yet, and symbol
13062      table processing isn't initialized.  But we have to set the CU language,
13063      or we won't be able to build types correctly.  */
13064   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13065   if (attr)
13066     set_cu_language (DW_UNSND (attr), cu);
13067   else
13068     set_cu_language (language_minimal, cu);
13069
13070   do_cleanups (back_to);
13071
13072   /* We've successfully allocated this compilation unit.  Let our caller
13073      clean it up when finished with it.  */
13074   discard_cleanups (free_cu_cleanup);
13075
13076   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13077   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13078 }
13079
13080 /* Decode simple location descriptions.
13081    Given a pointer to a dwarf block that defines a location, compute
13082    the location and return the value.
13083
13084    NOTE drow/2003-11-18: This function is called in two situations
13085    now: for the address of static or global variables (partial symbols
13086    only) and for offsets into structures which are expected to be
13087    (more or less) constant.  The partial symbol case should go away,
13088    and only the constant case should remain.  That will let this
13089    function complain more accurately.  A few special modes are allowed
13090    without complaint for global variables (for instance, global
13091    register values and thread-local values).
13092
13093    A location description containing no operations indicates that the
13094    object is optimized out.  The return value is 0 for that case.
13095    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13096    callers will only want a very basic result and this can become a
13097    complaint.
13098
13099    Note that stack[0] is unused except as a default error return.
13100    Note that stack overflow is not yet handled.  */
13101
13102 static CORE_ADDR
13103 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13104 {
13105   struct objfile *objfile = cu->objfile;
13106   int i;
13107   int size = blk->size;
13108   gdb_byte *data = blk->data;
13109   CORE_ADDR stack[64];
13110   int stacki;
13111   unsigned int bytes_read, unsnd;
13112   gdb_byte op;
13113
13114   i = 0;
13115   stacki = 0;
13116   stack[stacki] = 0;
13117
13118   while (i < size)
13119     {
13120       op = data[i++];
13121       switch (op)
13122         {
13123         case DW_OP_lit0:
13124         case DW_OP_lit1:
13125         case DW_OP_lit2:
13126         case DW_OP_lit3:
13127         case DW_OP_lit4:
13128         case DW_OP_lit5:
13129         case DW_OP_lit6:
13130         case DW_OP_lit7:
13131         case DW_OP_lit8:
13132         case DW_OP_lit9:
13133         case DW_OP_lit10:
13134         case DW_OP_lit11:
13135         case DW_OP_lit12:
13136         case DW_OP_lit13:
13137         case DW_OP_lit14:
13138         case DW_OP_lit15:
13139         case DW_OP_lit16:
13140         case DW_OP_lit17:
13141         case DW_OP_lit18:
13142         case DW_OP_lit19:
13143         case DW_OP_lit20:
13144         case DW_OP_lit21:
13145         case DW_OP_lit22:
13146         case DW_OP_lit23:
13147         case DW_OP_lit24:
13148         case DW_OP_lit25:
13149         case DW_OP_lit26:
13150         case DW_OP_lit27:
13151         case DW_OP_lit28:
13152         case DW_OP_lit29:
13153         case DW_OP_lit30:
13154         case DW_OP_lit31:
13155           stack[++stacki] = op - DW_OP_lit0;
13156           break;
13157
13158         case DW_OP_reg0:
13159         case DW_OP_reg1:
13160         case DW_OP_reg2:
13161         case DW_OP_reg3:
13162         case DW_OP_reg4:
13163         case DW_OP_reg5:
13164         case DW_OP_reg6:
13165         case DW_OP_reg7:
13166         case DW_OP_reg8:
13167         case DW_OP_reg9:
13168         case DW_OP_reg10:
13169         case DW_OP_reg11:
13170         case DW_OP_reg12:
13171         case DW_OP_reg13:
13172         case DW_OP_reg14:
13173         case DW_OP_reg15:
13174         case DW_OP_reg16:
13175         case DW_OP_reg17:
13176         case DW_OP_reg18:
13177         case DW_OP_reg19:
13178         case DW_OP_reg20:
13179         case DW_OP_reg21:
13180         case DW_OP_reg22:
13181         case DW_OP_reg23:
13182         case DW_OP_reg24:
13183         case DW_OP_reg25:
13184         case DW_OP_reg26:
13185         case DW_OP_reg27:
13186         case DW_OP_reg28:
13187         case DW_OP_reg29:
13188         case DW_OP_reg30:
13189         case DW_OP_reg31:
13190           stack[++stacki] = op - DW_OP_reg0;
13191           if (i < size)
13192             dwarf2_complex_location_expr_complaint ();
13193           break;
13194
13195         case DW_OP_regx:
13196           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13197           i += bytes_read;
13198           stack[++stacki] = unsnd;
13199           if (i < size)
13200             dwarf2_complex_location_expr_complaint ();
13201           break;
13202
13203         case DW_OP_addr:
13204           stack[++stacki] = read_address (objfile->obfd, &data[i],
13205                                           cu, &bytes_read);
13206           i += bytes_read;
13207           break;
13208
13209         case DW_OP_const1u:
13210           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13211           i += 1;
13212           break;
13213
13214         case DW_OP_const1s:
13215           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13216           i += 1;
13217           break;
13218
13219         case DW_OP_const2u:
13220           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13221           i += 2;
13222           break;
13223
13224         case DW_OP_const2s:
13225           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13226           i += 2;
13227           break;
13228
13229         case DW_OP_const4u:
13230           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13231           i += 4;
13232           break;
13233
13234         case DW_OP_const4s:
13235           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13236           i += 4;
13237           break;
13238
13239         case DW_OP_constu:
13240           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13241                                                   &bytes_read);
13242           i += bytes_read;
13243           break;
13244
13245         case DW_OP_consts:
13246           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13247           i += bytes_read;
13248           break;
13249
13250         case DW_OP_dup:
13251           stack[stacki + 1] = stack[stacki];
13252           stacki++;
13253           break;
13254
13255         case DW_OP_plus:
13256           stack[stacki - 1] += stack[stacki];
13257           stacki--;
13258           break;
13259
13260         case DW_OP_plus_uconst:
13261           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13262           i += bytes_read;
13263           break;
13264
13265         case DW_OP_minus:
13266           stack[stacki - 1] -= stack[stacki];
13267           stacki--;
13268           break;
13269
13270         case DW_OP_deref:
13271           /* If we're not the last op, then we definitely can't encode
13272              this using GDB's address_class enum.  This is valid for partial
13273              global symbols, although the variable's address will be bogus
13274              in the psymtab.  */
13275           if (i < size)
13276             dwarf2_complex_location_expr_complaint ();
13277           break;
13278
13279         case DW_OP_GNU_push_tls_address:
13280           /* The top of the stack has the offset from the beginning
13281              of the thread control block at which the variable is located.  */
13282           /* Nothing should follow this operator, so the top of stack would
13283              be returned.  */
13284           /* This is valid for partial global symbols, but the variable's
13285              address will be bogus in the psymtab.  */
13286           if (i < size)
13287             dwarf2_complex_location_expr_complaint ();
13288           break;
13289
13290         case DW_OP_GNU_uninit:
13291           break;
13292
13293         default:
13294           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13295                      dwarf_stack_op_name (op, 1));
13296           return (stack[stacki]);
13297         }
13298     }
13299   return (stack[stacki]);
13300 }
13301
13302 /* memory allocation interface */
13303
13304 static struct dwarf_block *
13305 dwarf_alloc_block (struct dwarf2_cu *cu)
13306 {
13307   struct dwarf_block *blk;
13308
13309   blk = (struct dwarf_block *)
13310     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13311   return (blk);
13312 }
13313
13314 static struct abbrev_info *
13315 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13316 {
13317   struct abbrev_info *abbrev;
13318
13319   abbrev = (struct abbrev_info *)
13320     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13321   memset (abbrev, 0, sizeof (struct abbrev_info));
13322   return (abbrev);
13323 }
13324
13325 static struct die_info *
13326 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13327 {
13328   struct die_info *die;
13329   size_t size = sizeof (struct die_info);
13330
13331   if (num_attrs > 1)
13332     size += (num_attrs - 1) * sizeof (struct attribute);
13333
13334   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13335   memset (die, 0, sizeof (struct die_info));
13336   return (die);
13337 }
13338
13339 \f
13340 /* Macro support.  */
13341
13342
13343 /* Return the full name of file number I in *LH's file name table.
13344    Use COMP_DIR as the name of the current directory of the
13345    compilation.  The result is allocated using xmalloc; the caller is
13346    responsible for freeing it.  */
13347 static char *
13348 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13349 {
13350   /* Is the file number a valid index into the line header's file name
13351      table?  Remember that file numbers start with one, not zero.  */
13352   if (1 <= file && file <= lh->num_file_names)
13353     {
13354       struct file_entry *fe = &lh->file_names[file - 1];
13355
13356       if (IS_ABSOLUTE_PATH (fe->name))
13357         return xstrdup (fe->name);
13358       else
13359         {
13360           const char *dir;
13361           int dir_len;
13362           char *full_name;
13363
13364           if (fe->dir_index)
13365             dir = lh->include_dirs[fe->dir_index - 1];
13366           else
13367             dir = comp_dir;
13368
13369           if (dir)
13370             {
13371               dir_len = strlen (dir);
13372               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13373               strcpy (full_name, dir);
13374               full_name[dir_len] = '/';
13375               strcpy (full_name + dir_len + 1, fe->name);
13376               return full_name;
13377             }
13378           else
13379             return xstrdup (fe->name);
13380         }
13381     }
13382   else
13383     {
13384       /* The compiler produced a bogus file number.  We can at least
13385          record the macro definitions made in the file, even if we
13386          won't be able to find the file by name.  */
13387       char fake_name[80];
13388
13389       sprintf (fake_name, "<bad macro file number %d>", file);
13390
13391       complaint (&symfile_complaints,
13392                  _("bad file number in macro information (%d)"),
13393                  file);
13394
13395       return xstrdup (fake_name);
13396     }
13397 }
13398
13399
13400 static struct macro_source_file *
13401 macro_start_file (int file, int line,
13402                   struct macro_source_file *current_file,
13403                   const char *comp_dir,
13404                   struct line_header *lh, struct objfile *objfile)
13405 {
13406   /* The full name of this source file.  */
13407   char *full_name = file_full_name (file, lh, comp_dir);
13408
13409   /* We don't create a macro table for this compilation unit
13410      at all until we actually get a filename.  */
13411   if (! pending_macros)
13412     pending_macros = new_macro_table (&objfile->objfile_obstack,
13413                                       objfile->macro_cache);
13414
13415   if (! current_file)
13416     /* If we have no current file, then this must be the start_file
13417        directive for the compilation unit's main source file.  */
13418     current_file = macro_set_main (pending_macros, full_name);
13419   else
13420     current_file = macro_include (current_file, line, full_name);
13421
13422   xfree (full_name);
13423
13424   return current_file;
13425 }
13426
13427
13428 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13429    followed by a null byte.  */
13430 static char *
13431 copy_string (const char *buf, int len)
13432 {
13433   char *s = xmalloc (len + 1);
13434
13435   memcpy (s, buf, len);
13436   s[len] = '\0';
13437   return s;
13438 }
13439
13440
13441 static const char *
13442 consume_improper_spaces (const char *p, const char *body)
13443 {
13444   if (*p == ' ')
13445     {
13446       complaint (&symfile_complaints,
13447                  _("macro definition contains spaces in formal argument list:\n`%s'"),
13448                  body);
13449
13450       while (*p == ' ')
13451         p++;
13452     }
13453
13454   return p;
13455 }
13456
13457
13458 static void
13459 parse_macro_definition (struct macro_source_file *file, int line,
13460                         const char *body)
13461 {
13462   const char *p;
13463
13464   /* The body string takes one of two forms.  For object-like macro
13465      definitions, it should be:
13466
13467         <macro name> " " <definition>
13468
13469      For function-like macro definitions, it should be:
13470
13471         <macro name> "() " <definition>
13472      or
13473         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13474
13475      Spaces may appear only where explicitly indicated, and in the
13476      <definition>.
13477
13478      The Dwarf 2 spec says that an object-like macro's name is always
13479      followed by a space, but versions of GCC around March 2002 omit
13480      the space when the macro's definition is the empty string.
13481
13482      The Dwarf 2 spec says that there should be no spaces between the
13483      formal arguments in a function-like macro's formal argument list,
13484      but versions of GCC around March 2002 include spaces after the
13485      commas.  */
13486
13487
13488   /* Find the extent of the macro name.  The macro name is terminated
13489      by either a space or null character (for an object-like macro) or
13490      an opening paren (for a function-like macro).  */
13491   for (p = body; *p; p++)
13492     if (*p == ' ' || *p == '(')
13493       break;
13494
13495   if (*p == ' ' || *p == '\0')
13496     {
13497       /* It's an object-like macro.  */
13498       int name_len = p - body;
13499       char *name = copy_string (body, name_len);
13500       const char *replacement;
13501
13502       if (*p == ' ')
13503         replacement = body + name_len + 1;
13504       else
13505         {
13506           dwarf2_macro_malformed_definition_complaint (body);
13507           replacement = body + name_len;
13508         }
13509
13510       macro_define_object (file, line, name, replacement);
13511
13512       xfree (name);
13513     }
13514   else if (*p == '(')
13515     {
13516       /* It's a function-like macro.  */
13517       char *name = copy_string (body, p - body);
13518       int argc = 0;
13519       int argv_size = 1;
13520       char **argv = xmalloc (argv_size * sizeof (*argv));
13521
13522       p++;
13523
13524       p = consume_improper_spaces (p, body);
13525
13526       /* Parse the formal argument list.  */
13527       while (*p && *p != ')')
13528         {
13529           /* Find the extent of the current argument name.  */
13530           const char *arg_start = p;
13531
13532           while (*p && *p != ',' && *p != ')' && *p != ' ')
13533             p++;
13534
13535           if (! *p || p == arg_start)
13536             dwarf2_macro_malformed_definition_complaint (body);
13537           else
13538             {
13539               /* Make sure argv has room for the new argument.  */
13540               if (argc >= argv_size)
13541                 {
13542                   argv_size *= 2;
13543                   argv = xrealloc (argv, argv_size * sizeof (*argv));
13544                 }
13545
13546               argv[argc++] = copy_string (arg_start, p - arg_start);
13547             }
13548
13549           p = consume_improper_spaces (p, body);
13550
13551           /* Consume the comma, if present.  */
13552           if (*p == ',')
13553             {
13554               p++;
13555
13556               p = consume_improper_spaces (p, body);
13557             }
13558         }
13559
13560       if (*p == ')')
13561         {
13562           p++;
13563
13564           if (*p == ' ')
13565             /* Perfectly formed definition, no complaints.  */
13566             macro_define_function (file, line, name,
13567                                    argc, (const char **) argv,
13568                                    p + 1);
13569           else if (*p == '\0')
13570             {
13571               /* Complain, but do define it.  */
13572               dwarf2_macro_malformed_definition_complaint (body);
13573               macro_define_function (file, line, name,
13574                                      argc, (const char **) argv,
13575                                      p);
13576             }
13577           else
13578             /* Just complain.  */
13579             dwarf2_macro_malformed_definition_complaint (body);
13580         }
13581       else
13582         /* Just complain.  */
13583         dwarf2_macro_malformed_definition_complaint (body);
13584
13585       xfree (name);
13586       {
13587         int i;
13588
13589         for (i = 0; i < argc; i++)
13590           xfree (argv[i]);
13591       }
13592       xfree (argv);
13593     }
13594   else
13595     dwarf2_macro_malformed_definition_complaint (body);
13596 }
13597
13598
13599 static void
13600 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13601                      char *comp_dir, bfd *abfd,
13602                      struct dwarf2_cu *cu)
13603 {
13604   gdb_byte *mac_ptr, *mac_end;
13605   struct macro_source_file *current_file = 0;
13606   enum dwarf_macinfo_record_type macinfo_type;
13607   int at_commandline;
13608
13609   dwarf2_read_section (dwarf2_per_objfile->objfile,
13610                        &dwarf2_per_objfile->macinfo);
13611   if (dwarf2_per_objfile->macinfo.buffer == NULL)
13612     {
13613       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13614       return;
13615     }
13616
13617   /* First pass: Find the name of the base filename.
13618      This filename is needed in order to process all macros whose definition
13619      (or undefinition) comes from the command line.  These macros are defined
13620      before the first DW_MACINFO_start_file entry, and yet still need to be
13621      associated to the base file.
13622
13623      To determine the base file name, we scan the macro definitions until we
13624      reach the first DW_MACINFO_start_file entry.  We then initialize
13625      CURRENT_FILE accordingly so that any macro definition found before the
13626      first DW_MACINFO_start_file can still be associated to the base file.  */
13627
13628   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13629   mac_end = dwarf2_per_objfile->macinfo.buffer
13630     + dwarf2_per_objfile->macinfo.size;
13631
13632   do
13633     {
13634       /* Do we at least have room for a macinfo type byte?  */
13635       if (mac_ptr >= mac_end)
13636         {
13637           /* Complaint is printed during the second pass as GDB will probably
13638              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
13639           break;
13640         }
13641
13642       macinfo_type = read_1_byte (abfd, mac_ptr);
13643       mac_ptr++;
13644
13645       switch (macinfo_type)
13646         {
13647           /* A zero macinfo type indicates the end of the macro
13648              information.  */
13649         case 0:
13650           break;
13651
13652         case DW_MACINFO_define:
13653         case DW_MACINFO_undef:
13654           /* Only skip the data by MAC_PTR.  */
13655           {
13656             unsigned int bytes_read;
13657
13658             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13659             mac_ptr += bytes_read;
13660             read_direct_string (abfd, mac_ptr, &bytes_read);
13661             mac_ptr += bytes_read;
13662           }
13663           break;
13664
13665         case DW_MACINFO_start_file:
13666           {
13667             unsigned int bytes_read;
13668             int line, file;
13669
13670             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13671             mac_ptr += bytes_read;
13672             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13673             mac_ptr += bytes_read;
13674
13675             current_file = macro_start_file (file, line, current_file, comp_dir,
13676                                              lh, cu->objfile);
13677           }
13678           break;
13679
13680         case DW_MACINFO_end_file:
13681           /* No data to skip by MAC_PTR.  */
13682           break;
13683
13684         case DW_MACINFO_vendor_ext:
13685           /* Only skip the data by MAC_PTR.  */
13686           {
13687             unsigned int bytes_read;
13688
13689             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13690             mac_ptr += bytes_read;
13691             read_direct_string (abfd, mac_ptr, &bytes_read);
13692             mac_ptr += bytes_read;
13693           }
13694           break;
13695
13696         default:
13697           break;
13698         }
13699     } while (macinfo_type != 0 && current_file == NULL);
13700
13701   /* Second pass: Process all entries.
13702
13703      Use the AT_COMMAND_LINE flag to determine whether we are still processing
13704      command-line macro definitions/undefinitions.  This flag is unset when we
13705      reach the first DW_MACINFO_start_file entry.  */
13706
13707   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13708
13709   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
13710      GDB is still reading the definitions from command line.  First
13711      DW_MACINFO_start_file will need to be ignored as it was already executed
13712      to create CURRENT_FILE for the main source holding also the command line
13713      definitions.  On first met DW_MACINFO_start_file this flag is reset to
13714      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
13715
13716   at_commandline = 1;
13717
13718   do
13719     {
13720       /* Do we at least have room for a macinfo type byte?  */
13721       if (mac_ptr >= mac_end)
13722         {
13723           dwarf2_macros_too_long_complaint ();
13724           break;
13725         }
13726
13727       macinfo_type = read_1_byte (abfd, mac_ptr);
13728       mac_ptr++;
13729
13730       switch (macinfo_type)
13731         {
13732           /* A zero macinfo type indicates the end of the macro
13733              information.  */
13734         case 0:
13735           break;
13736
13737         case DW_MACINFO_define:
13738         case DW_MACINFO_undef:
13739           {
13740             unsigned int bytes_read;
13741             int line;
13742             char *body;
13743
13744             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13745             mac_ptr += bytes_read;
13746             body = read_direct_string (abfd, mac_ptr, &bytes_read);
13747             mac_ptr += bytes_read;
13748
13749             if (! current_file)
13750               {
13751                 /* DWARF violation as no main source is present.  */
13752                 complaint (&symfile_complaints,
13753                            _("debug info with no main source gives macro %s "
13754                              "on line %d: %s"),
13755                            macinfo_type == DW_MACINFO_define ?
13756                              _("definition") :
13757                                macinfo_type == DW_MACINFO_undef ?
13758                                  _("undefinition") :
13759                                  _("something-or-other"), line, body);
13760                 break;
13761               }
13762             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13763               complaint (&symfile_complaints,
13764                          _("debug info gives %s macro %s with %s line %d: %s"),
13765                          at_commandline ? _("command-line") : _("in-file"),
13766                          macinfo_type == DW_MACINFO_define ?
13767                            _("definition") :
13768                              macinfo_type == DW_MACINFO_undef ?
13769                                _("undefinition") :
13770                                _("something-or-other"),
13771                          line == 0 ? _("zero") : _("non-zero"), line, body);
13772
13773             if (macinfo_type == DW_MACINFO_define)
13774               parse_macro_definition (current_file, line, body);
13775             else if (macinfo_type == DW_MACINFO_undef)
13776               macro_undef (current_file, line, body);
13777           }
13778           break;
13779
13780         case DW_MACINFO_start_file:
13781           {
13782             unsigned int bytes_read;
13783             int line, file;
13784
13785             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13786             mac_ptr += bytes_read;
13787             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13788             mac_ptr += bytes_read;
13789
13790             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13791               complaint (&symfile_complaints,
13792                          _("debug info gives source %d included "
13793                            "from %s at %s line %d"),
13794                          file, at_commandline ? _("command-line") : _("file"),
13795                          line == 0 ? _("zero") : _("non-zero"), line);
13796
13797             if (at_commandline)
13798               {
13799                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13800                 at_commandline = 0;
13801               }
13802             else
13803               current_file = macro_start_file (file, line,
13804                                                current_file, comp_dir,
13805                                                lh, cu->objfile);
13806           }
13807           break;
13808
13809         case DW_MACINFO_end_file:
13810           if (! current_file)
13811             complaint (&symfile_complaints,
13812                        _("macro debug info has an unmatched `close_file' directive"));
13813           else
13814             {
13815               current_file = current_file->included_by;
13816               if (! current_file)
13817                 {
13818                   enum dwarf_macinfo_record_type next_type;
13819
13820                   /* GCC circa March 2002 doesn't produce the zero
13821                      type byte marking the end of the compilation
13822                      unit.  Complain if it's not there, but exit no
13823                      matter what.  */
13824
13825                   /* Do we at least have room for a macinfo type byte?  */
13826                   if (mac_ptr >= mac_end)
13827                     {
13828                       dwarf2_macros_too_long_complaint ();
13829                       return;
13830                     }
13831
13832                   /* We don't increment mac_ptr here, so this is just
13833                      a look-ahead.  */
13834                   next_type = read_1_byte (abfd, mac_ptr);
13835                   if (next_type != 0)
13836                     complaint (&symfile_complaints,
13837                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13838
13839                   return;
13840                 }
13841             }
13842           break;
13843
13844         case DW_MACINFO_vendor_ext:
13845           {
13846             unsigned int bytes_read;
13847             int constant;
13848             char *string;
13849
13850             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13851             mac_ptr += bytes_read;
13852             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13853             mac_ptr += bytes_read;
13854
13855             /* We don't recognize any vendor extensions.  */
13856           }
13857           break;
13858         }
13859     } while (macinfo_type != 0);
13860 }
13861
13862 /* Check if the attribute's form is a DW_FORM_block*
13863    if so return true else false. */
13864 static int
13865 attr_form_is_block (struct attribute *attr)
13866 {
13867   return (attr == NULL ? 0 :
13868       attr->form == DW_FORM_block1
13869       || attr->form == DW_FORM_block2
13870       || attr->form == DW_FORM_block4
13871       || attr->form == DW_FORM_block
13872       || attr->form == DW_FORM_exprloc);
13873 }
13874
13875 /* Return non-zero if ATTR's value is a section offset --- classes
13876    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13877    You may use DW_UNSND (attr) to retrieve such offsets.
13878
13879    Section 7.5.4, "Attribute Encodings", explains that no attribute
13880    may have a value that belongs to more than one of these classes; it
13881    would be ambiguous if we did, because we use the same forms for all
13882    of them.  */
13883 static int
13884 attr_form_is_section_offset (struct attribute *attr)
13885 {
13886   return (attr->form == DW_FORM_data4
13887           || attr->form == DW_FORM_data8
13888           || attr->form == DW_FORM_sec_offset);
13889 }
13890
13891
13892 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13893    zero otherwise.  When this function returns true, you can apply
13894    dwarf2_get_attr_constant_value to it.
13895
13896    However, note that for some attributes you must check
13897    attr_form_is_section_offset before using this test.  DW_FORM_data4
13898    and DW_FORM_data8 are members of both the constant class, and of
13899    the classes that contain offsets into other debug sections
13900    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
13901    that, if an attribute's can be either a constant or one of the
13902    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13903    taken as section offsets, not constants.  */
13904 static int
13905 attr_form_is_constant (struct attribute *attr)
13906 {
13907   switch (attr->form)
13908     {
13909     case DW_FORM_sdata:
13910     case DW_FORM_udata:
13911     case DW_FORM_data1:
13912     case DW_FORM_data2:
13913     case DW_FORM_data4:
13914     case DW_FORM_data8:
13915       return 1;
13916     default:
13917       return 0;
13918     }
13919 }
13920
13921 static void
13922 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13923                              struct dwarf2_cu *cu)
13924 {
13925   if (attr_form_is_section_offset (attr)
13926       /* ".debug_loc" may not exist at all, or the offset may be outside
13927          the section.  If so, fall through to the complaint in the
13928          other branch.  */
13929       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13930     {
13931       struct dwarf2_loclist_baton *baton;
13932
13933       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13934                              sizeof (struct dwarf2_loclist_baton));
13935       baton->per_cu = cu->per_cu;
13936       gdb_assert (baton->per_cu);
13937
13938       dwarf2_read_section (dwarf2_per_objfile->objfile,
13939                            &dwarf2_per_objfile->loc);
13940
13941       /* We don't know how long the location list is, but make sure we
13942          don't run off the edge of the section.  */
13943       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13944       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13945       baton->base_address = cu->base_address;
13946       if (cu->base_known == 0)
13947         complaint (&symfile_complaints,
13948                    _("Location list used without specifying the CU base address."));
13949
13950       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13951       SYMBOL_LOCATION_BATON (sym) = baton;
13952     }
13953   else
13954     {
13955       struct dwarf2_locexpr_baton *baton;
13956
13957       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13958                              sizeof (struct dwarf2_locexpr_baton));
13959       baton->per_cu = cu->per_cu;
13960       gdb_assert (baton->per_cu);
13961
13962       if (attr_form_is_block (attr))
13963         {
13964           /* Note that we're just copying the block's data pointer
13965              here, not the actual data.  We're still pointing into the
13966              info_buffer for SYM's objfile; right now we never release
13967              that buffer, but when we do clean up properly this may
13968              need to change.  */
13969           baton->size = DW_BLOCK (attr)->size;
13970           baton->data = DW_BLOCK (attr)->data;
13971         }
13972       else
13973         {
13974           dwarf2_invalid_attrib_class_complaint ("location description",
13975                                                  SYMBOL_NATURAL_NAME (sym));
13976           baton->size = 0;
13977           baton->data = NULL;
13978         }
13979
13980       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13981       SYMBOL_LOCATION_BATON (sym) = baton;
13982     }
13983 }
13984
13985 /* Return the OBJFILE associated with the compilation unit CU.  If CU
13986    came from a separate debuginfo file, then the master objfile is
13987    returned.  */
13988
13989 struct objfile *
13990 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13991 {
13992   struct objfile *objfile = per_cu->objfile;
13993
13994   /* Return the master objfile, so that we can report and look up the
13995      correct file containing this variable.  */
13996   if (objfile->separate_debug_objfile_backlink)
13997     objfile = objfile->separate_debug_objfile_backlink;
13998
13999   return objfile;
14000 }
14001
14002 /* Return the address size given in the compilation unit header for CU.  */
14003
14004 CORE_ADDR
14005 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14006 {
14007   if (per_cu->cu)
14008     return per_cu->cu->header.addr_size;
14009   else
14010     {
14011       /* If the CU is not currently read in, we re-read its header.  */
14012       struct objfile *objfile = per_cu->objfile;
14013       struct dwarf2_per_objfile *per_objfile
14014         = objfile_data (objfile, dwarf2_objfile_data_key);
14015       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14016       struct comp_unit_head cu_header;
14017
14018       memset (&cu_header, 0, sizeof cu_header);
14019       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14020       return cu_header.addr_size;
14021     }
14022 }
14023
14024 /* Return the offset size given in the compilation unit header for CU.  */
14025
14026 int
14027 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14028 {
14029   if (per_cu->cu)
14030     return per_cu->cu->header.offset_size;
14031   else
14032     {
14033       /* If the CU is not currently read in, we re-read its header.  */
14034       struct objfile *objfile = per_cu->objfile;
14035       struct dwarf2_per_objfile *per_objfile
14036         = objfile_data (objfile, dwarf2_objfile_data_key);
14037       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14038       struct comp_unit_head cu_header;
14039
14040       memset (&cu_header, 0, sizeof cu_header);
14041       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14042       return cu_header.offset_size;
14043     }
14044 }
14045
14046 /* Return the text offset of the CU.  The returned offset comes from
14047    this CU's objfile.  If this objfile came from a separate debuginfo
14048    file, then the offset may be different from the corresponding
14049    offset in the parent objfile.  */
14050
14051 CORE_ADDR
14052 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14053 {
14054   struct objfile *objfile = per_cu->objfile;
14055
14056   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14057 }
14058
14059 /* Locate the .debug_info compilation unit from CU's objfile which contains
14060    the DIE at OFFSET.  Raises an error on failure.  */
14061
14062 static struct dwarf2_per_cu_data *
14063 dwarf2_find_containing_comp_unit (unsigned int offset,
14064                                   struct objfile *objfile)
14065 {
14066   struct dwarf2_per_cu_data *this_cu;
14067   int low, high;
14068
14069   low = 0;
14070   high = dwarf2_per_objfile->n_comp_units - 1;
14071   while (high > low)
14072     {
14073       int mid = low + (high - low) / 2;
14074
14075       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14076         high = mid;
14077       else
14078         low = mid + 1;
14079     }
14080   gdb_assert (low == high);
14081   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14082     {
14083       if (low == 0)
14084         error (_("Dwarf Error: could not find partial DIE containing "
14085                "offset 0x%lx [in module %s]"),
14086                (long) offset, bfd_get_filename (objfile->obfd));
14087
14088       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14089       return dwarf2_per_objfile->all_comp_units[low-1];
14090     }
14091   else
14092     {
14093       this_cu = dwarf2_per_objfile->all_comp_units[low];
14094       if (low == dwarf2_per_objfile->n_comp_units - 1
14095           && offset >= this_cu->offset + this_cu->length)
14096         error (_("invalid dwarf2 offset %u"), offset);
14097       gdb_assert (offset < this_cu->offset + this_cu->length);
14098       return this_cu;
14099     }
14100 }
14101
14102 /* Locate the compilation unit from OBJFILE which is located at exactly
14103    OFFSET.  Raises an error on failure.  */
14104
14105 static struct dwarf2_per_cu_data *
14106 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14107 {
14108   struct dwarf2_per_cu_data *this_cu;
14109
14110   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14111   if (this_cu->offset != offset)
14112     error (_("no compilation unit with offset %u."), offset);
14113   return this_cu;
14114 }
14115
14116 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
14117
14118 static struct dwarf2_cu *
14119 alloc_one_comp_unit (struct objfile *objfile)
14120 {
14121   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14122   cu->objfile = objfile;
14123   obstack_init (&cu->comp_unit_obstack);
14124   return cu;
14125 }
14126
14127 /* Release one cached compilation unit, CU.  We unlink it from the tree
14128    of compilation units, but we don't remove it from the read_in_chain;
14129    the caller is responsible for that.
14130    NOTE: DATA is a void * because this function is also used as a
14131    cleanup routine.  */
14132
14133 static void
14134 free_one_comp_unit (void *data)
14135 {
14136   struct dwarf2_cu *cu = data;
14137
14138   if (cu->per_cu != NULL)
14139     cu->per_cu->cu = NULL;
14140   cu->per_cu = NULL;
14141
14142   obstack_free (&cu->comp_unit_obstack, NULL);
14143
14144   xfree (cu);
14145 }
14146
14147 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14148    when we're finished with it.  We can't free the pointer itself, but be
14149    sure to unlink it from the cache.  Also release any associated storage
14150    and perform cache maintenance.
14151
14152    Only used during partial symbol parsing.  */
14153
14154 static void
14155 free_stack_comp_unit (void *data)
14156 {
14157   struct dwarf2_cu *cu = data;
14158
14159   obstack_free (&cu->comp_unit_obstack, NULL);
14160   cu->partial_dies = NULL;
14161
14162   if (cu->per_cu != NULL)
14163     {
14164       /* This compilation unit is on the stack in our caller, so we
14165          should not xfree it.  Just unlink it.  */
14166       cu->per_cu->cu = NULL;
14167       cu->per_cu = NULL;
14168
14169       /* If we had a per-cu pointer, then we may have other compilation
14170          units loaded, so age them now.  */
14171       age_cached_comp_units ();
14172     }
14173 }
14174
14175 /* Free all cached compilation units.  */
14176
14177 static void
14178 free_cached_comp_units (void *data)
14179 {
14180   struct dwarf2_per_cu_data *per_cu, **last_chain;
14181
14182   per_cu = dwarf2_per_objfile->read_in_chain;
14183   last_chain = &dwarf2_per_objfile->read_in_chain;
14184   while (per_cu != NULL)
14185     {
14186       struct dwarf2_per_cu_data *next_cu;
14187
14188       next_cu = per_cu->cu->read_in_chain;
14189
14190       free_one_comp_unit (per_cu->cu);
14191       *last_chain = next_cu;
14192
14193       per_cu = next_cu;
14194     }
14195 }
14196
14197 /* Increase the age counter on each cached compilation unit, and free
14198    any that are too old.  */
14199
14200 static void
14201 age_cached_comp_units (void)
14202 {
14203   struct dwarf2_per_cu_data *per_cu, **last_chain;
14204
14205   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14206   per_cu = dwarf2_per_objfile->read_in_chain;
14207   while (per_cu != NULL)
14208     {
14209       per_cu->cu->last_used ++;
14210       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14211         dwarf2_mark (per_cu->cu);
14212       per_cu = per_cu->cu->read_in_chain;
14213     }
14214
14215   per_cu = dwarf2_per_objfile->read_in_chain;
14216   last_chain = &dwarf2_per_objfile->read_in_chain;
14217   while (per_cu != NULL)
14218     {
14219       struct dwarf2_per_cu_data *next_cu;
14220
14221       next_cu = per_cu->cu->read_in_chain;
14222
14223       if (!per_cu->cu->mark)
14224         {
14225           free_one_comp_unit (per_cu->cu);
14226           *last_chain = next_cu;
14227         }
14228       else
14229         last_chain = &per_cu->cu->read_in_chain;
14230
14231       per_cu = next_cu;
14232     }
14233 }
14234
14235 /* Remove a single compilation unit from the cache.  */
14236
14237 static void
14238 free_one_cached_comp_unit (void *target_cu)
14239 {
14240   struct dwarf2_per_cu_data *per_cu, **last_chain;
14241
14242   per_cu = dwarf2_per_objfile->read_in_chain;
14243   last_chain = &dwarf2_per_objfile->read_in_chain;
14244   while (per_cu != NULL)
14245     {
14246       struct dwarf2_per_cu_data *next_cu;
14247
14248       next_cu = per_cu->cu->read_in_chain;
14249
14250       if (per_cu->cu == target_cu)
14251         {
14252           free_one_comp_unit (per_cu->cu);
14253           *last_chain = next_cu;
14254           break;
14255         }
14256       else
14257         last_chain = &per_cu->cu->read_in_chain;
14258
14259       per_cu = next_cu;
14260     }
14261 }
14262
14263 /* Release all extra memory associated with OBJFILE.  */
14264
14265 void
14266 dwarf2_free_objfile (struct objfile *objfile)
14267 {
14268   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14269
14270   if (dwarf2_per_objfile == NULL)
14271     return;
14272
14273   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14274   free_cached_comp_units (NULL);
14275
14276   if (dwarf2_per_objfile->using_index)
14277     {
14278       int i;
14279
14280       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14281         {
14282           int j;
14283           struct dwarf2_per_cu_data *per_cu =
14284             dwarf2_per_objfile->all_comp_units[i];
14285
14286           if (!per_cu->v.quick->lines)
14287             continue;
14288
14289           for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14290             {
14291               if (per_cu->v.quick->file_names)
14292                 xfree ((void *) per_cu->v.quick->file_names[j]);
14293               if (per_cu->v.quick->full_names)
14294                 xfree ((void *) per_cu->v.quick->full_names[j]);
14295             }
14296
14297           free_line_header (per_cu->v.quick->lines);
14298         }
14299     }
14300
14301   /* Everything else should be on the objfile obstack.  */
14302 }
14303
14304 /* A pair of DIE offset and GDB type pointer.  We store these
14305    in a hash table separate from the DIEs, and preserve them
14306    when the DIEs are flushed out of cache.  */
14307
14308 struct dwarf2_offset_and_type
14309 {
14310   unsigned int offset;
14311   struct type *type;
14312 };
14313
14314 /* Hash function for a dwarf2_offset_and_type.  */
14315
14316 static hashval_t
14317 offset_and_type_hash (const void *item)
14318 {
14319   const struct dwarf2_offset_and_type *ofs = item;
14320
14321   return ofs->offset;
14322 }
14323
14324 /* Equality function for a dwarf2_offset_and_type.  */
14325
14326 static int
14327 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14328 {
14329   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14330   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14331
14332   return ofs_lhs->offset == ofs_rhs->offset;
14333 }
14334
14335 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14336    table if necessary.  For convenience, return TYPE.
14337
14338    The DIEs reading must have careful ordering to:
14339     * Not cause infite loops trying to read in DIEs as a prerequisite for
14340       reading current DIE.
14341     * Not trying to dereference contents of still incompletely read in types
14342       while reading in other DIEs.
14343     * Enable referencing still incompletely read in types just by a pointer to
14344       the type without accessing its fields.
14345
14346    Therefore caller should follow these rules:
14347      * Try to fetch any prerequisite types we may need to build this DIE type
14348        before building the type and calling set_die_type.
14349      * After building type call set_die_type for current DIE as soon as
14350        possible before fetching more types to complete the current type.
14351      * Make the type as complete as possible before fetching more types.  */
14352
14353 static struct type *
14354 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14355 {
14356   struct dwarf2_offset_and_type **slot, ofs;
14357   struct objfile *objfile = cu->objfile;
14358   htab_t *type_hash_ptr;
14359
14360   /* For Ada types, make sure that the gnat-specific data is always
14361      initialized (if not already set).  There are a few types where
14362      we should not be doing so, because the type-specific area is
14363      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14364      where the type-specific area is used to store the floatformat).
14365      But this is not a problem, because the gnat-specific information
14366      is actually not needed for these types.  */
14367   if (need_gnat_info (cu)
14368       && TYPE_CODE (type) != TYPE_CODE_FUNC
14369       && TYPE_CODE (type) != TYPE_CODE_FLT
14370       && !HAVE_GNAT_AUX_INFO (type))
14371     INIT_GNAT_SPECIFIC (type);
14372
14373   if (cu->per_cu->from_debug_types)
14374     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14375   else
14376     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14377
14378   if (*type_hash_ptr == NULL)
14379     {
14380       *type_hash_ptr
14381         = htab_create_alloc_ex (127,
14382                                 offset_and_type_hash,
14383                                 offset_and_type_eq,
14384                                 NULL,
14385                                 &objfile->objfile_obstack,
14386                                 hashtab_obstack_allocate,
14387                                 dummy_obstack_deallocate);
14388     }
14389
14390   ofs.offset = die->offset;
14391   ofs.type = type;
14392   slot = (struct dwarf2_offset_and_type **)
14393     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14394   if (*slot)
14395     complaint (&symfile_complaints,
14396                _("A problem internal to GDB: DIE 0x%x has type already set"),
14397                die->offset);
14398   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14399   **slot = ofs;
14400   return type;
14401 }
14402
14403 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14404    table, or return NULL if the die does not have a saved type.  */
14405
14406 static struct type *
14407 get_die_type_at_offset (unsigned int offset,
14408                         struct dwarf2_per_cu_data *per_cu)
14409 {
14410   struct dwarf2_offset_and_type *slot, ofs;
14411   htab_t type_hash;
14412
14413   if (per_cu->from_debug_types)
14414     type_hash = dwarf2_per_objfile->debug_types_type_hash;
14415   else
14416     type_hash = dwarf2_per_objfile->debug_info_type_hash;
14417   if (type_hash == NULL)
14418     return NULL;
14419
14420   ofs.offset = offset;
14421   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14422   if (slot)
14423     return slot->type;
14424   else
14425     return NULL;
14426 }
14427
14428 /* Look up the type for DIE in the appropriate type_hash table,
14429    or return NULL if DIE does not have a saved type.  */
14430
14431 static struct type *
14432 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14433 {
14434   return get_die_type_at_offset (die->offset, cu->per_cu);
14435 }
14436
14437 /* Add a dependence relationship from CU to REF_PER_CU.  */
14438
14439 static void
14440 dwarf2_add_dependence (struct dwarf2_cu *cu,
14441                        struct dwarf2_per_cu_data *ref_per_cu)
14442 {
14443   void **slot;
14444
14445   if (cu->dependencies == NULL)
14446     cu->dependencies
14447       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14448                               NULL, &cu->comp_unit_obstack,
14449                               hashtab_obstack_allocate,
14450                               dummy_obstack_deallocate);
14451
14452   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14453   if (*slot == NULL)
14454     *slot = ref_per_cu;
14455 }
14456
14457 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14458    Set the mark field in every compilation unit in the
14459    cache that we must keep because we are keeping CU.  */
14460
14461 static int
14462 dwarf2_mark_helper (void **slot, void *data)
14463 {
14464   struct dwarf2_per_cu_data *per_cu;
14465
14466   per_cu = (struct dwarf2_per_cu_data *) *slot;
14467   if (per_cu->cu->mark)
14468     return 1;
14469   per_cu->cu->mark = 1;
14470
14471   if (per_cu->cu->dependencies != NULL)
14472     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14473
14474   return 1;
14475 }
14476
14477 /* Set the mark field in CU and in every other compilation unit in the
14478    cache that we must keep because we are keeping CU.  */
14479
14480 static void
14481 dwarf2_mark (struct dwarf2_cu *cu)
14482 {
14483   if (cu->mark)
14484     return;
14485   cu->mark = 1;
14486   if (cu->dependencies != NULL)
14487     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14488 }
14489
14490 static void
14491 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14492 {
14493   while (per_cu)
14494     {
14495       per_cu->cu->mark = 0;
14496       per_cu = per_cu->cu->read_in_chain;
14497     }
14498 }
14499
14500 /* Trivial hash function for partial_die_info: the hash value of a DIE
14501    is its offset in .debug_info for this objfile.  */
14502
14503 static hashval_t
14504 partial_die_hash (const void *item)
14505 {
14506   const struct partial_die_info *part_die = item;
14507
14508   return part_die->offset;
14509 }
14510
14511 /* Trivial comparison function for partial_die_info structures: two DIEs
14512    are equal if they have the same offset.  */
14513
14514 static int
14515 partial_die_eq (const void *item_lhs, const void *item_rhs)
14516 {
14517   const struct partial_die_info *part_die_lhs = item_lhs;
14518   const struct partial_die_info *part_die_rhs = item_rhs;
14519
14520   return part_die_lhs->offset == part_die_rhs->offset;
14521 }
14522
14523 static struct cmd_list_element *set_dwarf2_cmdlist;
14524 static struct cmd_list_element *show_dwarf2_cmdlist;
14525
14526 static void
14527 set_dwarf2_cmd (char *args, int from_tty)
14528 {
14529   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14530 }
14531
14532 static void
14533 show_dwarf2_cmd (char *args, int from_tty)
14534 {
14535   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14536 }
14537
14538 /* If section described by INFO was mmapped, munmap it now.  */
14539
14540 static void
14541 munmap_section_buffer (struct dwarf2_section_info *info)
14542 {
14543   if (info->was_mmapped)
14544     {
14545 #ifdef HAVE_MMAP
14546       intptr_t begin = (intptr_t) info->buffer;
14547       intptr_t map_begin = begin & ~(pagesize - 1);
14548       size_t map_length = info->size + begin - map_begin;
14549
14550       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14551 #else
14552       /* Without HAVE_MMAP, we should never be here to begin with.  */
14553       gdb_assert_not_reached ("no mmap support");
14554 #endif
14555     }
14556 }
14557
14558 /* munmap debug sections for OBJFILE, if necessary.  */
14559
14560 static void
14561 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14562 {
14563   struct dwarf2_per_objfile *data = d;
14564
14565   /* This is sorted according to the order they're defined in to make it easier
14566      to keep in sync.  */
14567   munmap_section_buffer (&data->info);
14568   munmap_section_buffer (&data->abbrev);
14569   munmap_section_buffer (&data->line);
14570   munmap_section_buffer (&data->loc);
14571   munmap_section_buffer (&data->macinfo);
14572   munmap_section_buffer (&data->str);
14573   munmap_section_buffer (&data->ranges);
14574   munmap_section_buffer (&data->types);
14575   munmap_section_buffer (&data->frame);
14576   munmap_section_buffer (&data->eh_frame);
14577   munmap_section_buffer (&data->gdb_index);
14578 }
14579
14580 \f
14581
14582 /* The contents of the hash table we create when building the string
14583    table.  */
14584 struct strtab_entry
14585 {
14586   offset_type offset;
14587   const char *str;
14588 };
14589
14590 /* Hash function for a strtab_entry.  */
14591 static hashval_t
14592 hash_strtab_entry (const void *e)
14593 {
14594   const struct strtab_entry *entry = e;
14595   return mapped_index_string_hash (entry->str);
14596 }
14597
14598 /* Equality function for a strtab_entry.  */
14599 static int
14600 eq_strtab_entry (const void *a, const void *b)
14601 {
14602   const struct strtab_entry *ea = a;
14603   const struct strtab_entry *eb = b;
14604   return !strcmp (ea->str, eb->str);
14605 }
14606
14607 /* Create a strtab_entry hash table.  */
14608 static htab_t
14609 create_strtab (void)
14610 {
14611   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14612                             xfree, xcalloc, xfree);
14613 }
14614
14615 /* Add a string to the constant pool.  Return the string's offset in
14616    host order.  */
14617 static offset_type
14618 add_string (htab_t table, struct obstack *cpool, const char *str)
14619 {
14620   void **slot;
14621   struct strtab_entry entry;
14622   struct strtab_entry *result;
14623
14624   entry.str = str;
14625   slot = htab_find_slot (table, &entry, INSERT);
14626   if (*slot)
14627     result = *slot;
14628   else
14629     {
14630       result = XNEW (struct strtab_entry);
14631       result->offset = obstack_object_size (cpool);
14632       result->str = str;
14633       obstack_grow_str0 (cpool, str);
14634       *slot = result;
14635     }
14636   return result->offset;
14637 }
14638
14639 /* An entry in the symbol table.  */
14640 struct symtab_index_entry
14641 {
14642   /* The name of the symbol.  */
14643   const char *name;
14644   /* The offset of the name in the constant pool.  */
14645   offset_type index_offset;
14646   /* A sorted vector of the indices of all the CUs that hold an object
14647      of this name.  */
14648   VEC (offset_type) *cu_indices;
14649 };
14650
14651 /* The symbol table.  This is a power-of-2-sized hash table.  */
14652 struct mapped_symtab
14653 {
14654   offset_type n_elements;
14655   offset_type size;
14656   struct symtab_index_entry **data;
14657 };
14658
14659 /* Hash function for a symtab_index_entry.  */
14660 static hashval_t
14661 hash_symtab_entry (const void *e)
14662 {
14663   const struct symtab_index_entry *entry = e;
14664   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14665                          sizeof (offset_type) * VEC_length (offset_type,
14666                                                             entry->cu_indices),
14667                          0);
14668 }
14669
14670 /* Equality function for a symtab_index_entry.  */
14671 static int
14672 eq_symtab_entry (const void *a, const void *b)
14673 {
14674   const struct symtab_index_entry *ea = a;
14675   const struct symtab_index_entry *eb = b;
14676   int len = VEC_length (offset_type, ea->cu_indices);
14677   if (len != VEC_length (offset_type, eb->cu_indices))
14678     return 0;
14679   return !memcmp (VEC_address (offset_type, ea->cu_indices),
14680                   VEC_address (offset_type, eb->cu_indices),
14681                   sizeof (offset_type) * len);
14682 }
14683
14684 /* Destroy a symtab_index_entry.  */
14685 static void
14686 delete_symtab_entry (void *p)
14687 {
14688   struct symtab_index_entry *entry = p;
14689   VEC_free (offset_type, entry->cu_indices);
14690   xfree (entry);
14691 }
14692
14693 /* Create a hash table holding symtab_index_entry objects.  */
14694 static htab_t
14695 create_index_table (void)
14696 {
14697   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14698                             delete_symtab_entry, xcalloc, xfree);
14699 }
14700
14701 /* Create a new mapped symtab object.  */
14702 static struct mapped_symtab *
14703 create_mapped_symtab (void)
14704 {
14705   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14706   symtab->n_elements = 0;
14707   symtab->size = 1024;
14708   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14709   return symtab;
14710 }
14711
14712 /* Destroy a mapped_symtab.  */
14713 static void
14714 cleanup_mapped_symtab (void *p)
14715 {
14716   struct mapped_symtab *symtab = p;
14717   /* The contents of the array are freed when the other hash table is
14718      destroyed.  */
14719   xfree (symtab->data);
14720   xfree (symtab);
14721 }
14722
14723 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
14724    the slot.  */
14725 static struct symtab_index_entry **
14726 find_slot (struct mapped_symtab *symtab, const char *name)
14727 {
14728   offset_type index, step, hash = mapped_index_string_hash (name);
14729
14730   index = hash & (symtab->size - 1);
14731   step = ((hash * 17) & (symtab->size - 1)) | 1;
14732
14733   for (;;)
14734     {
14735       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14736         return &symtab->data[index];
14737       index = (index + step) & (symtab->size - 1);
14738     }
14739 }
14740
14741 /* Expand SYMTAB's hash table.  */
14742 static void
14743 hash_expand (struct mapped_symtab *symtab)
14744 {
14745   offset_type old_size = symtab->size;
14746   offset_type i;
14747   struct symtab_index_entry **old_entries = symtab->data;
14748
14749   symtab->size *= 2;
14750   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14751
14752   for (i = 0; i < old_size; ++i)
14753     {
14754       if (old_entries[i])
14755         {
14756           struct symtab_index_entry **slot = find_slot (symtab,
14757                                                         old_entries[i]->name);
14758           *slot = old_entries[i];
14759         }
14760     }
14761
14762   xfree (old_entries);
14763 }
14764
14765 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
14766    is the index of the CU in which the symbol appears.  */
14767 static void
14768 add_index_entry (struct mapped_symtab *symtab, const char *name,
14769                  offset_type cu_index)
14770 {
14771   struct symtab_index_entry **slot;
14772
14773   ++symtab->n_elements;
14774   if (4 * symtab->n_elements / 3 >= symtab->size)
14775     hash_expand (symtab);
14776
14777   slot = find_slot (symtab, name);
14778   if (!*slot)
14779     {
14780       *slot = XNEW (struct symtab_index_entry);
14781       (*slot)->name = name;
14782       (*slot)->cu_indices = NULL;
14783     }
14784   /* Don't push an index twice.  Due to how we add entries we only
14785      have to check the last one.  */ 
14786   if (VEC_empty (offset_type, (*slot)->cu_indices)
14787       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14788     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14789 }
14790
14791 /* Add a vector of indices to the constant pool.  */
14792 static offset_type
14793 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14794                       struct symtab_index_entry *entry)
14795 {
14796   void **slot;
14797
14798   slot = htab_find_slot (index_table, entry, INSERT);
14799   if (!*slot)
14800     {
14801       offset_type len = VEC_length (offset_type, entry->cu_indices);
14802       offset_type val = MAYBE_SWAP (len);
14803       offset_type iter;
14804       int i;
14805
14806       *slot = entry;
14807       entry->index_offset = obstack_object_size (cpool);
14808
14809       obstack_grow (cpool, &val, sizeof (val));
14810       for (i = 0;
14811            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14812            ++i)
14813         {
14814           val = MAYBE_SWAP (iter);
14815           obstack_grow (cpool, &val, sizeof (val));
14816         }
14817     }
14818   else
14819     {
14820       struct symtab_index_entry *old_entry = *slot;
14821       entry->index_offset = old_entry->index_offset;
14822       entry = old_entry;
14823     }
14824   return entry->index_offset;
14825 }
14826
14827 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14828    constant pool entries going into the obstack CPOOL.  */
14829 static void
14830 write_hash_table (struct mapped_symtab *symtab,
14831                   struct obstack *output, struct obstack *cpool)
14832 {
14833   offset_type i;
14834   htab_t index_table;
14835   htab_t str_table;
14836
14837   index_table = create_index_table ();
14838   str_table = create_strtab ();
14839   /* We add all the index vectors to the constant pool first, to
14840      ensure alignment is ok.  */
14841   for (i = 0; i < symtab->size; ++i)
14842     {
14843       if (symtab->data[i])
14844         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14845     }
14846
14847   /* Now write out the hash table.  */
14848   for (i = 0; i < symtab->size; ++i)
14849     {
14850       offset_type str_off, vec_off;
14851
14852       if (symtab->data[i])
14853         {
14854           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14855           vec_off = symtab->data[i]->index_offset;
14856         }
14857       else
14858         {
14859           /* While 0 is a valid constant pool index, it is not valid
14860              to have 0 for both offsets.  */
14861           str_off = 0;
14862           vec_off = 0;
14863         }
14864
14865       str_off = MAYBE_SWAP (str_off);
14866       vec_off = MAYBE_SWAP (vec_off);
14867
14868       obstack_grow (output, &str_off, sizeof (str_off));
14869       obstack_grow (output, &vec_off, sizeof (vec_off));
14870     }
14871
14872   htab_delete (str_table);
14873   htab_delete (index_table);
14874 }
14875
14876 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
14877    from PST; CU_INDEX is the index of the CU in the vector of all
14878    CUs.  */
14879 static void
14880 add_address_entry (struct objfile *objfile,
14881                    struct obstack *addr_obstack, struct partial_symtab *pst,
14882                    unsigned int cu_index)
14883 {
14884   offset_type offset;
14885   char addr[8];
14886   CORE_ADDR baseaddr;
14887
14888   /* Don't bother recording empty ranges.  */
14889   if (pst->textlow == pst->texthigh)
14890     return;
14891
14892   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14893
14894   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14895   obstack_grow (addr_obstack, addr, 8);
14896   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14897   obstack_grow (addr_obstack, addr, 8);
14898   offset = MAYBE_SWAP (cu_index);
14899   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14900 }
14901
14902 /* Add a list of partial symbols to SYMTAB.  */
14903 static void
14904 write_psymbols (struct mapped_symtab *symtab,
14905                 struct partial_symbol **psymp,
14906                 int count,
14907                 offset_type cu_index)
14908 {
14909   for (; count-- > 0; ++psymp)
14910     {
14911       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14912         error (_("Ada is not currently supported by the index"));
14913       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14914     }
14915 }
14916
14917 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
14918    exception if there is an error.  */
14919 static void
14920 write_obstack (FILE *file, struct obstack *obstack)
14921 {
14922   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14923               file)
14924       != obstack_object_size (obstack))
14925     error (_("couldn't data write to file"));
14926 }
14927
14928 /* Unlink a file if the argument is not NULL.  */
14929 static void
14930 unlink_if_set (void *p)
14931 {
14932   char **filename = p;
14933   if (*filename)
14934     unlink (*filename);
14935 }
14936
14937 /* A helper struct used when iterating over debug_types.  */
14938 struct signatured_type_index_data
14939 {
14940   struct objfile *objfile;
14941   struct mapped_symtab *symtab;
14942   struct obstack *types_list;
14943   int cu_index;
14944 };
14945
14946 /* A helper function that writes a single signatured_type to an
14947    obstack.  */
14948 static int
14949 write_one_signatured_type (void **slot, void *d)
14950 {
14951   struct signatured_type_index_data *info = d;
14952   struct signatured_type *entry = (struct signatured_type *) *slot;
14953   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14954   struct partial_symtab *psymtab = per_cu->v.psymtab;
14955   gdb_byte val[8];
14956
14957   write_psymbols (info->symtab,
14958                   info->objfile->global_psymbols.list + psymtab->globals_offset,
14959                   psymtab->n_global_syms, info->cu_index);
14960   write_psymbols (info->symtab,
14961                   info->objfile->static_psymbols.list + psymtab->statics_offset,
14962                   psymtab->n_static_syms, info->cu_index);
14963
14964   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14965   obstack_grow (info->types_list, val, 8);
14966   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14967   obstack_grow (info->types_list, val, 8);
14968   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14969   obstack_grow (info->types_list, val, 8);
14970
14971   ++info->cu_index;
14972
14973   return 1;
14974 }
14975
14976 /* Create an index file for OBJFILE in the directory DIR.  */
14977 static void
14978 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14979 {
14980   struct cleanup *cleanup;
14981   char *filename, *cleanup_filename;
14982   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14983   struct obstack cu_list, types_cu_list;
14984   int i;
14985   FILE *out_file;
14986   struct mapped_symtab *symtab;
14987   offset_type val, size_of_contents, total_len;
14988   struct stat st;
14989   char buf[8];
14990
14991   if (!objfile->psymtabs)
14992     return;
14993   if (dwarf2_per_objfile->using_index)
14994     error (_("Cannot use an index to create the index"));
14995
14996   if (stat (objfile->name, &st) < 0)
14997     perror_with_name (_("Could not stat"));
14998
14999   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15000                      INDEX_SUFFIX, (char *) NULL);
15001   cleanup = make_cleanup (xfree, filename);
15002
15003   out_file = fopen (filename, "wb");
15004   if (!out_file)
15005     error (_("Can't open `%s' for writing"), filename);
15006
15007   cleanup_filename = filename;
15008   make_cleanup (unlink_if_set, &cleanup_filename);
15009
15010   symtab = create_mapped_symtab ();
15011   make_cleanup (cleanup_mapped_symtab, symtab);
15012
15013   obstack_init (&addr_obstack);
15014   make_cleanup_obstack_free (&addr_obstack);
15015
15016   obstack_init (&cu_list);
15017   make_cleanup_obstack_free (&cu_list);
15018
15019   obstack_init (&types_cu_list);
15020   make_cleanup_obstack_free (&types_cu_list);
15021
15022   /* The list is already sorted, so we don't need to do additional
15023      work here.  Also, the debug_types entries do not appear in
15024      all_comp_units, but only in their own hash table.  */
15025   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15026     {
15027       struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15028       struct partial_symtab *psymtab = per_cu->v.psymtab;
15029       gdb_byte val[8];
15030
15031       write_psymbols (symtab,
15032                       objfile->global_psymbols.list + psymtab->globals_offset,
15033                       psymtab->n_global_syms, i);
15034       write_psymbols (symtab,
15035                       objfile->static_psymbols.list + psymtab->statics_offset,
15036                       psymtab->n_static_syms, i);
15037
15038       add_address_entry (objfile, &addr_obstack, psymtab, i);
15039
15040       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15041       obstack_grow (&cu_list, val, 8);
15042       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15043       obstack_grow (&cu_list, val, 8);
15044     }
15045
15046   /* Write out the .debug_type entries, if any.  */
15047   if (dwarf2_per_objfile->signatured_types)
15048     {
15049       struct signatured_type_index_data sig_data;
15050
15051       sig_data.objfile = objfile;
15052       sig_data.symtab = symtab;
15053       sig_data.types_list = &types_cu_list;
15054       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15055       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15056                               write_one_signatured_type, &sig_data);
15057     }
15058
15059   obstack_init (&constant_pool);
15060   make_cleanup_obstack_free (&constant_pool);
15061   obstack_init (&symtab_obstack);
15062   make_cleanup_obstack_free (&symtab_obstack);
15063   write_hash_table (symtab, &symtab_obstack, &constant_pool);
15064
15065   obstack_init (&contents);
15066   make_cleanup_obstack_free (&contents);
15067   size_of_contents = 6 * sizeof (offset_type);
15068   total_len = size_of_contents;
15069
15070   /* The version number.  */
15071   val = MAYBE_SWAP (2);
15072   obstack_grow (&contents, &val, sizeof (val));
15073
15074   /* The offset of the CU list from the start of the file.  */
15075   val = MAYBE_SWAP (total_len);
15076   obstack_grow (&contents, &val, sizeof (val));
15077   total_len += obstack_object_size (&cu_list);
15078
15079   /* The offset of the types CU list from the start of the file.  */
15080   val = MAYBE_SWAP (total_len);
15081   obstack_grow (&contents, &val, sizeof (val));
15082   total_len += obstack_object_size (&types_cu_list);
15083
15084   /* The offset of the address table from the start of the file.  */
15085   val = MAYBE_SWAP (total_len);
15086   obstack_grow (&contents, &val, sizeof (val));
15087   total_len += obstack_object_size (&addr_obstack);
15088
15089   /* The offset of the symbol table from the start of the file.  */
15090   val = MAYBE_SWAP (total_len);
15091   obstack_grow (&contents, &val, sizeof (val));
15092   total_len += obstack_object_size (&symtab_obstack);
15093
15094   /* The offset of the constant pool from the start of the file.  */
15095   val = MAYBE_SWAP (total_len);
15096   obstack_grow (&contents, &val, sizeof (val));
15097   total_len += obstack_object_size (&constant_pool);
15098
15099   gdb_assert (obstack_object_size (&contents) == size_of_contents);
15100
15101   write_obstack (out_file, &contents);
15102   write_obstack (out_file, &cu_list);
15103   write_obstack (out_file, &types_cu_list);
15104   write_obstack (out_file, &addr_obstack);
15105   write_obstack (out_file, &symtab_obstack);
15106   write_obstack (out_file, &constant_pool);
15107
15108   fclose (out_file);
15109
15110   /* We want to keep the file, so we set cleanup_filename to NULL
15111      here.  See unlink_if_set.  */
15112   cleanup_filename = NULL;
15113
15114   do_cleanups (cleanup);
15115 }
15116
15117 /* The mapped index file format is designed to be directly mmap()able
15118    on any architecture.  In most cases, a datum is represented using a
15119    little-endian 32-bit integer value, called an offset_type.  Big
15120    endian machines must byte-swap the values before using them.
15121    Exceptions to this rule are noted.  The data is laid out such that
15122    alignment is always respected.
15123
15124    A mapped index consists of several sections.
15125
15126    1. The file header.  This is a sequence of values, of offset_type
15127    unless otherwise noted:
15128    [0] The version number.  Currently 1 or 2.  The differences are
15129    noted below.  Version 1 did not account for .debug_types sections;
15130    the presence of a .debug_types section invalidates any version 1
15131    index that may exist.
15132    [1] The offset, from the start of the file, of the CU list.
15133    [1.5] In version 2, the offset, from the start of the file, of the
15134    types CU list.  This offset does not appear in version 1.  Note
15135    that this can be empty, in which case this offset will be equal to
15136    the next offset.
15137    [2] The offset, from the start of the file, of the address section.
15138    [3] The offset, from the start of the file, of the symbol table.
15139    [4] The offset, from the start of the file, of the constant pool.
15140
15141    2. The CU list.  This is a sequence of pairs of 64-bit
15142    little-endian values, sorted by the CU offset.  The first element
15143    in each pair is the offset of a CU in the .debug_info section.  The
15144    second element in each pair is the length of that CU.  References
15145    to a CU elsewhere in the map are done using a CU index, which is
15146    just the 0-based index into this table.  Note that if there are
15147    type CUs, then conceptually CUs and type CUs form a single list for
15148    the purposes of CU indices.
15149
15150    2.5 The types CU list.  This does not appear in a version 1 index.
15151    This is a sequence of triplets of 64-bit little-endian values.  In
15152    a triplet, the first value is the CU offset, the second value is
15153    the type offset in the CU, and the third value is the type
15154    signature.  The types CU list is not sorted.
15155
15156    3. The address section.  The address section consists of a sequence
15157    of address entries.  Each address entry has three elements.
15158    [0] The low address.  This is a 64-bit little-endian value.
15159    [1] The high address.  This is a 64-bit little-endian value.
15160    [2] The CU index.  This is an offset_type value.
15161
15162    4. The symbol table.  This is a hash table.  The size of the hash
15163    table is always a power of 2.  The initial hash and the step are
15164    currently defined by the `find_slot' function.
15165
15166    Each slot in the hash table consists of a pair of offset_type
15167    values.  The first value is the offset of the symbol's name in the
15168    constant pool.  The second value is the offset of the CU vector in
15169    the constant pool.
15170
15171    If both values are 0, then this slot in the hash table is empty.
15172    This is ok because while 0 is a valid constant pool index, it
15173    cannot be a valid index for both a string and a CU vector.
15174
15175    A string in the constant pool is stored as a \0-terminated string,
15176    as you'd expect.
15177
15178    A CU vector in the constant pool is a sequence of offset_type
15179    values.  The first value is the number of CU indices in the vector.
15180    Each subsequent value is the index of a CU in the CU list.  This
15181    element in the hash table is used to indicate which CUs define the
15182    symbol.
15183
15184    5. The constant pool.  This is simply a bunch of bytes.  It is
15185    organized so that alignment is correct: CU vectors are stored
15186    first, followed by strings.  */
15187 static void
15188 save_gdb_index_command (char *arg, int from_tty)
15189 {
15190   struct objfile *objfile;
15191
15192   if (!arg || !*arg)
15193     error (_("usage: save gdb-index DIRECTORY"));
15194
15195   ALL_OBJFILES (objfile)
15196   {
15197     struct stat st;
15198
15199     /* If the objfile does not correspond to an actual file, skip it.  */
15200     if (stat (objfile->name, &st) < 0)
15201       continue;
15202
15203     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15204     if (dwarf2_per_objfile)
15205       {
15206         volatile struct gdb_exception except;
15207
15208         TRY_CATCH (except, RETURN_MASK_ERROR)
15209           {
15210             write_psymtabs_to_index (objfile, arg);
15211           }
15212         if (except.reason < 0)
15213           exception_fprintf (gdb_stderr, except,
15214                              _("Error while writing index for `%s': "),
15215                              objfile->name);
15216       }
15217   }
15218 }
15219
15220 \f
15221
15222 int dwarf2_always_disassemble;
15223
15224 static void
15225 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15226                                 struct cmd_list_element *c, const char *value)
15227 {
15228   fprintf_filtered (file, _("\
15229 Whether to always disassemble DWARF expressions is %s.\n"),
15230                     value);
15231 }
15232
15233 void _initialize_dwarf2_read (void);
15234
15235 void
15236 _initialize_dwarf2_read (void)
15237 {
15238   struct cmd_list_element *c;
15239
15240   dwarf2_objfile_data_key
15241     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15242
15243   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15244 Set DWARF 2 specific variables.\n\
15245 Configure DWARF 2 variables such as the cache size"),
15246                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15247                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15248
15249   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15250 Show DWARF 2 specific variables\n\
15251 Show DWARF 2 variables such as the cache size"),
15252                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15253                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15254
15255   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15256                             &dwarf2_max_cache_age, _("\
15257 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15258 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15259 A higher limit means that cached compilation units will be stored\n\
15260 in memory longer, and more total memory will be used.  Zero disables\n\
15261 caching, which can slow down startup."),
15262                             NULL,
15263                             show_dwarf2_max_cache_age,
15264                             &set_dwarf2_cmdlist,
15265                             &show_dwarf2_cmdlist);
15266
15267   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15268                            &dwarf2_always_disassemble, _("\
15269 Set whether `info address' always disassembles DWARF expressions."), _("\
15270 Show whether `info address' always disassembles DWARF expressions."), _("\
15271 When enabled, DWARF expressions are always printed in an assembly-like\n\
15272 syntax.  When disabled, expressions will be printed in a more\n\
15273 conversational style, when possible."),
15274                            NULL,
15275                            show_dwarf2_always_disassemble,
15276                            &set_dwarf2_cmdlist,
15277                            &show_dwarf2_cmdlist);
15278
15279   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15280 Set debugging of the dwarf2 DIE reader."), _("\
15281 Show debugging of the dwarf2 DIE reader."), _("\
15282 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15283 The value is the maximum depth to print."),
15284                             NULL,
15285                             NULL,
15286                             &setdebuglist, &showdebuglist);
15287
15288   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15289                _("Save a .gdb-index file"),
15290                &save_cmdlist);
15291   set_cmd_completer (c, filename_completer);
15292 }