OSDN Git Service

* gdb_assert.h (gdb_assert_not_reached): New macro.
[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
218 static struct dwarf2_per_objfile *dwarf2_per_objfile;
219
220 /* names of the debugging sections */
221
222 /* Note that if the debugging section has been compressed, it might
223    have a name like .zdebug_info.  */
224
225 #define INFO_SECTION     "debug_info"
226 #define ABBREV_SECTION   "debug_abbrev"
227 #define LINE_SECTION     "debug_line"
228 #define LOC_SECTION      "debug_loc"
229 #define MACINFO_SECTION  "debug_macinfo"
230 #define STR_SECTION      "debug_str"
231 #define RANGES_SECTION   "debug_ranges"
232 #define TYPES_SECTION    "debug_types"
233 #define FRAME_SECTION    "debug_frame"
234 #define EH_FRAME_SECTION "eh_frame"
235 #define GDB_INDEX_SECTION "gdb_index"
236
237 /* local data types */
238
239 /* We hold several abbreviation tables in memory at the same time. */
240 #ifndef ABBREV_HASH_SIZE
241 #define ABBREV_HASH_SIZE 121
242 #endif
243
244 /* The data in a compilation unit header, after target2host
245    translation, looks like this.  */
246 struct comp_unit_head
247 {
248   unsigned int length;
249   short version;
250   unsigned char addr_size;
251   unsigned char signed_addr_p;
252   unsigned int abbrev_offset;
253
254   /* Size of file offsets; either 4 or 8.  */
255   unsigned int offset_size;
256
257   /* Size of the length field; either 4 or 12.  */
258   unsigned int initial_length_size;
259
260   /* Offset to the first byte of this compilation unit header in the
261      .debug_info section, for resolving relative reference dies.  */
262   unsigned int offset;
263
264   /* Offset to first die in this cu from the start of the cu.
265      This will be the first byte following the compilation unit header.  */
266   unsigned int first_die_offset;
267 };
268
269 /* Internal state when decoding a particular compilation unit.  */
270 struct dwarf2_cu
271 {
272   /* The objfile containing this compilation unit.  */
273   struct objfile *objfile;
274
275   /* The header of the compilation unit.  */
276   struct comp_unit_head header;
277
278   /* Base address of this compilation unit.  */
279   CORE_ADDR base_address;
280
281   /* Non-zero if base_address has been set.  */
282   int base_known;
283
284   struct function_range *first_fn, *last_fn, *cached_fn;
285
286   /* The language we are debugging.  */
287   enum language language;
288   const struct language_defn *language_defn;
289
290   const char *producer;
291
292   /* The generic symbol table building routines have separate lists for
293      file scope symbols and all all other scopes (local scopes).  So
294      we need to select the right one to pass to add_symbol_to_list().
295      We do it by keeping a pointer to the correct list in list_in_scope.
296
297      FIXME: The original dwarf code just treated the file scope as the
298      first local scope, and all other local scopes as nested local
299      scopes, and worked fine.  Check to see if we really need to
300      distinguish these in buildsym.c.  */
301   struct pending **list_in_scope;
302
303   /* DWARF abbreviation table associated with this compilation unit.  */
304   struct abbrev_info **dwarf2_abbrevs;
305
306   /* Storage for the abbrev table.  */
307   struct obstack abbrev_obstack;
308
309   /* Hash table holding all the loaded partial DIEs.  */
310   htab_t partial_dies;
311
312   /* Storage for things with the same lifetime as this read-in compilation
313      unit, including partial DIEs.  */
314   struct obstack comp_unit_obstack;
315
316   /* When multiple dwarf2_cu structures are living in memory, this field
317      chains them all together, so that they can be released efficiently.
318      We will probably also want a generation counter so that most-recently-used
319      compilation units are cached...  */
320   struct dwarf2_per_cu_data *read_in_chain;
321
322   /* Backchain to our per_cu entry if the tree has been built.  */
323   struct dwarf2_per_cu_data *per_cu;
324
325   /* Pointer to the die -> type map.  Although it is stored
326      permanently in per_cu, we copy it here to avoid double
327      indirection.  */
328   htab_t type_hash;
329
330   /* How many compilation units ago was this CU last referenced?  */
331   int last_used;
332
333   /* A hash table of die offsets for following references.  */
334   htab_t die_hash;
335
336   /* Full DIEs if read in.  */
337   struct die_info *dies;
338
339   /* A set of pointers to dwarf2_per_cu_data objects for compilation
340      units referenced by this one.  Only set during full symbol processing;
341      partial symbol tables do not have dependencies.  */
342   htab_t dependencies;
343
344   /* Header data from the line table, during full symbol processing.  */
345   struct line_header *line_header;
346
347   /* Mark used when releasing cached dies.  */
348   unsigned int mark : 1;
349
350   /* This flag will be set if this compilation unit might include
351      inter-compilation-unit references.  */
352   unsigned int has_form_ref_addr : 1;
353
354   /* This flag will be set if this compilation unit includes any
355      DW_TAG_namespace DIEs.  If we know that there are explicit
356      DIEs for namespaces, we don't need to try to infer them
357      from mangled names.  */
358   unsigned int has_namespace_info : 1;
359 };
360
361 /* When using the index (and thus not using psymtabs), each CU has an
362    object of this type.  This is used to hold information needed by
363    the various "quick" methods.  */
364 struct dwarf2_per_cu_quick_data
365 {
366   /* The line table.  This can be NULL if there was no line table.  */
367   struct line_header *lines;
368
369   /* The file names from the line table.  */
370   const char **file_names;
371   /* The file names from the line table after being run through
372      gdb_realpath.  */
373   const char **full_names;
374
375   /* The corresponding symbol table.  This is NULL if symbols for this
376      CU have not yet been read.  */
377   struct symtab *symtab;
378
379   /* A temporary mark bit used when iterating over all CUs in
380      expand_symtabs_matching.  */
381   unsigned int mark : 1;
382
383   /* True if we've tried to read the line table.  */
384   unsigned int read_lines : 1;
385 };
386
387 /* Persistent data held for a compilation unit, even when not
388    processing it.  We put a pointer to this structure in the
389    read_symtab_private field of the psymtab.  If we encounter
390    inter-compilation-unit references, we also maintain a sorted
391    list of all compilation units.  */
392
393 struct dwarf2_per_cu_data
394 {
395   /* The start offset and length of this compilation unit.  2**29-1
396      bytes should suffice to store the length of any compilation unit
397      - if it doesn't, GDB will fall over anyway.
398      NOTE: Unlike comp_unit_head.length, this length includes
399      initial_length_size.  */
400   unsigned int offset;
401   unsigned int length : 29;
402
403   /* Flag indicating this compilation unit will be read in before
404      any of the current compilation units are processed.  */
405   unsigned int queued : 1;
406
407   /* This flag will be set if we need to load absolutely all DIEs
408      for this compilation unit, instead of just the ones we think
409      are interesting.  It gets set if we look for a DIE in the
410      hash table and don't find it.  */
411   unsigned int load_all_dies : 1;
412
413   /* Non-zero if this CU is from .debug_types.
414      Otherwise it's from .debug_info.  */
415   unsigned int from_debug_types : 1;
416
417   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
418      of the CU cache it gets reset to NULL again.  */
419   struct dwarf2_cu *cu;
420
421   /* If full symbols for this CU have been read in, then this field
422      holds a map of DIE offsets to types.  It isn't always possible
423      to reconstruct this information later, so we have to preserve
424      it.  */
425   htab_t type_hash;
426
427   /* The corresponding objfile.  */
428   struct objfile *objfile;
429
430   /* When using partial symbol tables, the 'psymtab' field is active.
431      Otherwise the 'quick' field is active.  */
432   union
433   {
434     /* The partial symbol table associated with this compilation unit,
435        or NULL for partial units (which do not have an associated
436        symtab).  */
437     struct partial_symtab *psymtab;
438
439     /* Data needed by the "quick" functions.  */
440     struct dwarf2_per_cu_quick_data *quick;
441   } v;
442 };
443
444 /* Entry in the signatured_types hash table.  */
445
446 struct signatured_type
447 {
448   ULONGEST signature;
449
450   /* Offset in .debug_types of the TU (type_unit) for this type.  */
451   unsigned int offset;
452
453   /* Offset in .debug_types of the type defined by this TU.  */
454   unsigned int type_offset;
455
456   /* The CU(/TU) of this type.  */
457   struct dwarf2_per_cu_data per_cu;
458 };
459
460 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
461    which are used for both .debug_info and .debug_types dies.
462    All parameters here are unchanging for the life of the call.
463    This struct exists to abstract away the constant parameters of
464    die reading.  */
465
466 struct die_reader_specs
467 {
468   /* The bfd of this objfile.  */
469   bfd* abfd;
470
471   /* The CU of the DIE we are parsing.  */
472   struct dwarf2_cu *cu;
473
474   /* Pointer to start of section buffer.
475      This is either the start of .debug_info or .debug_types.  */
476   const gdb_byte *buffer;
477 };
478
479 /* The line number information for a compilation unit (found in the
480    .debug_line section) begins with a "statement program header",
481    which contains the following information.  */
482 struct line_header
483 {
484   unsigned int total_length;
485   unsigned short version;
486   unsigned int header_length;
487   unsigned char minimum_instruction_length;
488   unsigned char maximum_ops_per_instruction;
489   unsigned char default_is_stmt;
490   int line_base;
491   unsigned char line_range;
492   unsigned char opcode_base;
493
494   /* standard_opcode_lengths[i] is the number of operands for the
495      standard opcode whose value is i.  This means that
496      standard_opcode_lengths[0] is unused, and the last meaningful
497      element is standard_opcode_lengths[opcode_base - 1].  */
498   unsigned char *standard_opcode_lengths;
499
500   /* The include_directories table.  NOTE!  These strings are not
501      allocated with xmalloc; instead, they are pointers into
502      debug_line_buffer.  If you try to free them, `free' will get
503      indigestion.  */
504   unsigned int num_include_dirs, include_dirs_size;
505   char **include_dirs;
506
507   /* The file_names table.  NOTE!  These strings are not allocated
508      with xmalloc; instead, they are pointers into debug_line_buffer.
509      Don't try to free them directly.  */
510   unsigned int num_file_names, file_names_size;
511   struct file_entry
512   {
513     char *name;
514     unsigned int dir_index;
515     unsigned int mod_time;
516     unsigned int length;
517     int included_p; /* Non-zero if referenced by the Line Number Program.  */
518     struct symtab *symtab; /* The associated symbol table, if any.  */
519   } *file_names;
520
521   /* The start and end of the statement program following this
522      header.  These point into dwarf2_per_objfile->line_buffer.  */
523   gdb_byte *statement_program_start, *statement_program_end;
524 };
525
526 /* When we construct a partial symbol table entry we only
527    need this much information. */
528 struct partial_die_info
529   {
530     /* Offset of this DIE.  */
531     unsigned int offset;
532
533     /* DWARF-2 tag for this DIE.  */
534     ENUM_BITFIELD(dwarf_tag) tag : 16;
535
536     /* Assorted flags describing the data found in this DIE.  */
537     unsigned int has_children : 1;
538     unsigned int is_external : 1;
539     unsigned int is_declaration : 1;
540     unsigned int has_type : 1;
541     unsigned int has_specification : 1;
542     unsigned int has_pc_info : 1;
543
544     /* Flag set if the SCOPE field of this structure has been
545        computed.  */
546     unsigned int scope_set : 1;
547
548     /* Flag set if the DIE has a byte_size attribute.  */
549     unsigned int has_byte_size : 1;
550
551     /* Flag set if any of the DIE's children are template arguments.  */
552     unsigned int has_template_arguments : 1;
553
554     /* The name of this DIE.  Normally the value of DW_AT_name, but
555        sometimes a default name for unnamed DIEs.  */
556     char *name;
557
558     /* The scope to prepend to our children.  This is generally
559        allocated on the comp_unit_obstack, so will disappear
560        when this compilation unit leaves the cache.  */
561     char *scope;
562
563     /* The location description associated with this DIE, if any.  */
564     struct dwarf_block *locdesc;
565
566     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
567     CORE_ADDR lowpc;
568     CORE_ADDR highpc;
569
570     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
571        DW_AT_sibling, if any.  */
572     gdb_byte *sibling;
573
574     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
575        DW_AT_specification (or DW_AT_abstract_origin or
576        DW_AT_extension).  */
577     unsigned int spec_offset;
578
579     /* Pointers to this DIE's parent, first child, and next sibling,
580        if any.  */
581     struct partial_die_info *die_parent, *die_child, *die_sibling;
582   };
583
584 /* This data structure holds the information of an abbrev. */
585 struct abbrev_info
586   {
587     unsigned int number;        /* number identifying abbrev */
588     enum dwarf_tag tag;         /* dwarf tag */
589     unsigned short has_children;                /* boolean */
590     unsigned short num_attrs;   /* number of attributes */
591     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
592     struct abbrev_info *next;   /* next in chain */
593   };
594
595 struct attr_abbrev
596   {
597     ENUM_BITFIELD(dwarf_attribute) name : 16;
598     ENUM_BITFIELD(dwarf_form) form : 16;
599   };
600
601 /* Attributes have a name and a value */
602 struct attribute
603   {
604     ENUM_BITFIELD(dwarf_attribute) name : 16;
605     ENUM_BITFIELD(dwarf_form) form : 15;
606
607     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
608        field should be in u.str (existing only for DW_STRING) but it is kept
609        here for better struct attribute alignment.  */
610     unsigned int string_is_canonical : 1;
611
612     union
613       {
614         char *str;
615         struct dwarf_block *blk;
616         ULONGEST unsnd;
617         LONGEST snd;
618         CORE_ADDR addr;
619         struct signatured_type *signatured_type;
620       }
621     u;
622   };
623
624 /* This data structure holds a complete die structure. */
625 struct die_info
626   {
627     /* DWARF-2 tag for this DIE.  */
628     ENUM_BITFIELD(dwarf_tag) tag : 16;
629
630     /* Number of attributes */
631     unsigned char num_attrs;
632
633     /* True if we're presently building the full type name for the
634        type derived from this DIE.  */
635     unsigned char building_fullname : 1;
636
637     /* Abbrev number */
638     unsigned int abbrev;
639
640     /* Offset in .debug_info or .debug_types section.  */
641     unsigned int offset;
642
643     /* The dies in a compilation unit form an n-ary tree.  PARENT
644        points to this die's parent; CHILD points to the first child of
645        this node; and all the children of a given node are chained
646        together via their SIBLING fields, terminated by a die whose
647        tag is zero.  */
648     struct die_info *child;     /* Its first child, if any.  */
649     struct die_info *sibling;   /* Its next sibling, if any.  */
650     struct die_info *parent;    /* Its parent, if any.  */
651
652     /* An array of attributes, with NUM_ATTRS elements.  There may be
653        zero, but it's not common and zero-sized arrays are not
654        sufficiently portable C.  */
655     struct attribute attrs[1];
656   };
657
658 struct function_range
659 {
660   const char *name;
661   CORE_ADDR lowpc, highpc;
662   int seen_line;
663   struct function_range *next;
664 };
665
666 /* Get at parts of an attribute structure */
667
668 #define DW_STRING(attr)    ((attr)->u.str)
669 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
670 #define DW_UNSND(attr)     ((attr)->u.unsnd)
671 #define DW_BLOCK(attr)     ((attr)->u.blk)
672 #define DW_SND(attr)       ((attr)->u.snd)
673 #define DW_ADDR(attr)      ((attr)->u.addr)
674 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
675
676 /* Blocks are a bunch of untyped bytes. */
677 struct dwarf_block
678   {
679     unsigned int size;
680     gdb_byte *data;
681   };
682
683 #ifndef ATTR_ALLOC_CHUNK
684 #define ATTR_ALLOC_CHUNK 4
685 #endif
686
687 /* Allocate fields for structs, unions and enums in this size.  */
688 #ifndef DW_FIELD_ALLOC_CHUNK
689 #define DW_FIELD_ALLOC_CHUNK 4
690 #endif
691
692 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
693    but this would require a corresponding change in unpack_field_as_long
694    and friends.  */
695 static int bits_per_byte = 8;
696
697 /* The routines that read and process dies for a C struct or C++ class
698    pass lists of data member fields and lists of member function fields
699    in an instance of a field_info structure, as defined below.  */
700 struct field_info
701   {
702     /* List of data member and baseclasses fields. */
703     struct nextfield
704       {
705         struct nextfield *next;
706         int accessibility;
707         int virtuality;
708         struct field field;
709       }
710      *fields, *baseclasses;
711
712     /* Number of fields (including baseclasses).  */
713     int nfields;
714
715     /* Number of baseclasses.  */
716     int nbaseclasses;
717
718     /* Set if the accesibility of one of the fields is not public.  */
719     int non_public_fields;
720
721     /* Member function fields array, entries are allocated in the order they
722        are encountered in the object file.  */
723     struct nextfnfield
724       {
725         struct nextfnfield *next;
726         struct fn_field fnfield;
727       }
728      *fnfields;
729
730     /* Member function fieldlist array, contains name of possibly overloaded
731        member function, number of overloaded member functions and a pointer
732        to the head of the member function field chain.  */
733     struct fnfieldlist
734       {
735         char *name;
736         int length;
737         struct nextfnfield *head;
738       }
739      *fnfieldlists;
740
741     /* Number of entries in the fnfieldlists array.  */
742     int nfnfields;
743
744     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
745        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
746     struct typedef_field_list
747       {
748         struct typedef_field field;
749         struct typedef_field_list *next;
750       }
751     *typedef_field_list;
752     unsigned typedef_field_list_count;
753   };
754
755 /* One item on the queue of compilation units to read in full symbols
756    for.  */
757 struct dwarf2_queue_item
758 {
759   struct dwarf2_per_cu_data *per_cu;
760   struct dwarf2_queue_item *next;
761 };
762
763 /* The current queue.  */
764 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
765
766 /* Loaded secondary compilation units are kept in memory until they
767    have not been referenced for the processing of this many
768    compilation units.  Set this to zero to disable caching.  Cache
769    sizes of up to at least twenty will improve startup time for
770    typical inter-CU-reference binaries, at an obvious memory cost.  */
771 static int dwarf2_max_cache_age = 5;
772 static void
773 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
774                            struct cmd_list_element *c, const char *value)
775 {
776   fprintf_filtered (file, _("\
777 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
778                     value);
779 }
780
781
782 /* Various complaints about symbol reading that don't abort the process */
783
784 static void
785 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
786 {
787   complaint (&symfile_complaints,
788              _("statement list doesn't fit in .debug_line section"));
789 }
790
791 static void
792 dwarf2_debug_line_missing_file_complaint (void)
793 {
794   complaint (&symfile_complaints,
795              _(".debug_line section has line data without a file"));
796 }
797
798 static void
799 dwarf2_debug_line_missing_end_sequence_complaint (void)
800 {
801   complaint (&symfile_complaints,
802              _(".debug_line section has line program sequence without an end"));
803 }
804
805 static void
806 dwarf2_complex_location_expr_complaint (void)
807 {
808   complaint (&symfile_complaints, _("location expression too complex"));
809 }
810
811 static void
812 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
813                                               int arg3)
814 {
815   complaint (&symfile_complaints,
816              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
817              arg2, arg3);
818 }
819
820 static void
821 dwarf2_macros_too_long_complaint (void)
822 {
823   complaint (&symfile_complaints,
824              _("macro info runs off end of `.debug_macinfo' section"));
825 }
826
827 static void
828 dwarf2_macro_malformed_definition_complaint (const char *arg1)
829 {
830   complaint (&symfile_complaints,
831              _("macro debug info contains a malformed macro definition:\n`%s'"),
832              arg1);
833 }
834
835 static void
836 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
837 {
838   complaint (&symfile_complaints,
839              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
840 }
841
842 /* local function prototypes */
843
844 static void dwarf2_locate_sections (bfd *, asection *, void *);
845
846 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
847                                            struct objfile *);
848
849 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
850                                            struct die_info *,
851                                            struct partial_symtab *);
852
853 static void dwarf2_build_psymtabs_hard (struct objfile *);
854
855 static void scan_partial_symbols (struct partial_die_info *,
856                                   CORE_ADDR *, CORE_ADDR *,
857                                   int, struct dwarf2_cu *);
858
859 static void add_partial_symbol (struct partial_die_info *,
860                                 struct dwarf2_cu *);
861
862 static void add_partial_namespace (struct partial_die_info *pdi,
863                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
864                                    int need_pc, struct dwarf2_cu *cu);
865
866 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
867                                 CORE_ADDR *highpc, int need_pc,
868                                 struct dwarf2_cu *cu);
869
870 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
871                                      struct dwarf2_cu *cu);
872
873 static void add_partial_subprogram (struct partial_die_info *pdi,
874                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
875                                     int need_pc, struct dwarf2_cu *cu);
876
877 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
878                                      gdb_byte *buffer, gdb_byte *info_ptr,
879                                      bfd *abfd, struct dwarf2_cu *cu);
880
881 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
882
883 static void psymtab_to_symtab_1 (struct partial_symtab *);
884
885 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
886
887 static void dwarf2_free_abbrev_table (void *);
888
889 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
890                                             struct dwarf2_cu *);
891
892 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
893                                                  struct dwarf2_cu *);
894
895 static struct partial_die_info *load_partial_dies (bfd *,
896                                                    gdb_byte *, gdb_byte *,
897                                                    int, struct dwarf2_cu *);
898
899 static gdb_byte *read_partial_die (struct partial_die_info *,
900                                    struct abbrev_info *abbrev,
901                                    unsigned int, bfd *,
902                                    gdb_byte *, gdb_byte *,
903                                    struct dwarf2_cu *);
904
905 static struct partial_die_info *find_partial_die (unsigned int,
906                                                   struct dwarf2_cu *);
907
908 static void fixup_partial_die (struct partial_die_info *,
909                                struct dwarf2_cu *);
910
911 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
912                                  bfd *, gdb_byte *, struct dwarf2_cu *);
913
914 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
915                                        bfd *, gdb_byte *, struct dwarf2_cu *);
916
917 static unsigned int read_1_byte (bfd *, gdb_byte *);
918
919 static int read_1_signed_byte (bfd *, gdb_byte *);
920
921 static unsigned int read_2_bytes (bfd *, gdb_byte *);
922
923 static unsigned int read_4_bytes (bfd *, gdb_byte *);
924
925 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
926
927 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
928                                unsigned int *);
929
930 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
931
932 static LONGEST read_checked_initial_length_and_offset
933   (bfd *, gdb_byte *, const struct comp_unit_head *,
934    unsigned int *, unsigned int *);
935
936 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
937                             unsigned int *);
938
939 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
940
941 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
942
943 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
944
945 static char *read_indirect_string (bfd *, gdb_byte *,
946                                    const struct comp_unit_head *,
947                                    unsigned int *);
948
949 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
950
951 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
952
953 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
954
955 static void set_cu_language (unsigned int, struct dwarf2_cu *);
956
957 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
958                                       struct dwarf2_cu *);
959
960 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
961                                                 unsigned int,
962                                                 struct dwarf2_cu *);
963
964 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
965                                struct dwarf2_cu *cu);
966
967 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
968
969 static struct die_info *die_specification (struct die_info *die,
970                                            struct dwarf2_cu **);
971
972 static void free_line_header (struct line_header *lh);
973
974 static void add_file_name (struct line_header *, char *, unsigned int,
975                            unsigned int, unsigned int);
976
977 static struct line_header *(dwarf_decode_line_header
978                             (unsigned int offset,
979                              bfd *abfd, struct dwarf2_cu *cu));
980
981 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
982                                 struct dwarf2_cu *, struct partial_symtab *);
983
984 static void dwarf2_start_subfile (char *, char *, char *);
985
986 static struct symbol *new_symbol (struct die_info *, struct type *,
987                                   struct dwarf2_cu *);
988
989 static struct symbol *new_symbol_full (struct die_info *, struct type *,
990                                        struct dwarf2_cu *, struct symbol *);
991
992 static void dwarf2_const_value (struct attribute *, struct symbol *,
993                                 struct dwarf2_cu *);
994
995 static void dwarf2_const_value_attr (struct attribute *attr,
996                                      struct type *type,
997                                      const char *name,
998                                      struct obstack *obstack,
999                                      struct dwarf2_cu *cu, long *value,
1000                                      gdb_byte **bytes,
1001                                      struct dwarf2_locexpr_baton **baton);
1002
1003 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1004
1005 static int need_gnat_info (struct dwarf2_cu *);
1006
1007 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1008
1009 static void set_descriptive_type (struct type *, struct die_info *,
1010                                   struct dwarf2_cu *);
1011
1012 static struct type *die_containing_type (struct die_info *,
1013                                          struct dwarf2_cu *);
1014
1015 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
1016
1017 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1018
1019 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1020
1021 static char *typename_concat (struct obstack *obs, const char *prefix,
1022                               const char *suffix, int physname,
1023                               struct dwarf2_cu *cu);
1024
1025 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1026
1027 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1028
1029 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1030
1031 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1032
1033 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1034                                struct dwarf2_cu *, struct partial_symtab *);
1035
1036 static int dwarf2_get_pc_bounds (struct die_info *,
1037                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1038                                  struct partial_symtab *);
1039
1040 static void get_scope_pc_bounds (struct die_info *,
1041                                  CORE_ADDR *, CORE_ADDR *,
1042                                  struct dwarf2_cu *);
1043
1044 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1045                                         CORE_ADDR, struct dwarf2_cu *);
1046
1047 static void dwarf2_add_field (struct field_info *, struct die_info *,
1048                               struct dwarf2_cu *);
1049
1050 static void dwarf2_attach_fields_to_type (struct field_info *,
1051                                           struct type *, struct dwarf2_cu *);
1052
1053 static void dwarf2_add_member_fn (struct field_info *,
1054                                   struct die_info *, struct type *,
1055                                   struct dwarf2_cu *);
1056
1057 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1058                                              struct type *, struct dwarf2_cu *);
1059
1060 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1061
1062 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1065
1066 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1067
1068 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1069
1070 static struct type *read_module_type (struct die_info *die,
1071                                       struct dwarf2_cu *cu);
1072
1073 static const char *namespace_name (struct die_info *die,
1074                                    int *is_anonymous, struct dwarf2_cu *);
1075
1076 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1077
1078 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1079
1080 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1081                                                        struct dwarf2_cu *);
1082
1083 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1084
1085 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1086                                                  gdb_byte *info_ptr,
1087                                                  gdb_byte **new_info_ptr,
1088                                                  struct die_info *parent);
1089
1090 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1091                                                gdb_byte *info_ptr,
1092                                                gdb_byte **new_info_ptr,
1093                                                struct die_info *parent);
1094
1095 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1096                                                gdb_byte *info_ptr,
1097                                                gdb_byte **new_info_ptr,
1098                                                struct die_info *parent);
1099
1100 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1101                                 struct die_info **, gdb_byte *,
1102                                 int *);
1103
1104 static void process_die (struct die_info *, struct dwarf2_cu *);
1105
1106 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1107                                        struct obstack *);
1108
1109 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1110
1111 static const char *dwarf2_full_name (char *name,
1112                                      struct die_info *die,
1113                                      struct dwarf2_cu *cu);
1114
1115 static struct die_info *dwarf2_extension (struct die_info *die,
1116                                           struct dwarf2_cu **);
1117
1118 static char *dwarf_tag_name (unsigned int);
1119
1120 static char *dwarf_attr_name (unsigned int);
1121
1122 static char *dwarf_form_name (unsigned int);
1123
1124 static char *dwarf_bool_name (unsigned int);
1125
1126 static char *dwarf_type_encoding_name (unsigned int);
1127
1128 #if 0
1129 static char *dwarf_cfi_name (unsigned int);
1130 #endif
1131
1132 static struct die_info *sibling_die (struct die_info *);
1133
1134 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1135
1136 static void dump_die_for_error (struct die_info *);
1137
1138 static void dump_die_1 (struct ui_file *, int level, int max_level,
1139                         struct die_info *);
1140
1141 /*static*/ void dump_die (struct die_info *, int max_level);
1142
1143 static void store_in_ref_table (struct die_info *,
1144                                 struct dwarf2_cu *);
1145
1146 static int is_ref_attr (struct attribute *);
1147
1148 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1149
1150 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1151
1152 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1153                                                struct attribute *,
1154                                                struct dwarf2_cu **);
1155
1156 static struct die_info *follow_die_ref (struct die_info *,
1157                                         struct attribute *,
1158                                         struct dwarf2_cu **);
1159
1160 static struct die_info *follow_die_sig (struct die_info *,
1161                                         struct attribute *,
1162                                         struct dwarf2_cu **);
1163
1164 static void read_signatured_type_at_offset (struct objfile *objfile,
1165                                             unsigned int offset);
1166
1167 static void read_signatured_type (struct objfile *,
1168                                   struct signatured_type *type_sig);
1169
1170 /* memory allocation interface */
1171
1172 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1173
1174 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1175
1176 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1177
1178 static void initialize_cu_func_list (struct dwarf2_cu *);
1179
1180 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1181                                  struct dwarf2_cu *);
1182
1183 static void dwarf_decode_macros (struct line_header *, unsigned int,
1184                                  char *, bfd *, struct dwarf2_cu *);
1185
1186 static int attr_form_is_block (struct attribute *);
1187
1188 static int attr_form_is_section_offset (struct attribute *);
1189
1190 static int attr_form_is_constant (struct attribute *);
1191
1192 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1193                                          struct symbol *sym,
1194                                          struct dwarf2_cu *cu);
1195
1196 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1197                                struct abbrev_info *abbrev,
1198                                struct dwarf2_cu *cu);
1199
1200 static void free_stack_comp_unit (void *);
1201
1202 static hashval_t partial_die_hash (const void *item);
1203
1204 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1205
1206 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1207   (unsigned int offset, struct objfile *objfile);
1208
1209 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1210   (unsigned int offset, struct objfile *objfile);
1211
1212 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1213
1214 static void free_one_comp_unit (void *);
1215
1216 static void free_cached_comp_units (void *);
1217
1218 static void age_cached_comp_units (void);
1219
1220 static void free_one_cached_comp_unit (void *);
1221
1222 static struct type *set_die_type (struct die_info *, struct type *,
1223                                   struct dwarf2_cu *);
1224
1225 static void create_all_comp_units (struct objfile *);
1226
1227 static int create_debug_types_hash_table (struct objfile *objfile);
1228
1229 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1230                                  struct objfile *);
1231
1232 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1233
1234 static void dwarf2_add_dependence (struct dwarf2_cu *,
1235                                    struct dwarf2_per_cu_data *);
1236
1237 static void dwarf2_mark (struct dwarf2_cu *);
1238
1239 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1240
1241 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1242
1243 static void dwarf2_release_queue (void *dummy);
1244
1245 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1246                              struct objfile *objfile);
1247
1248 static void process_queue (struct objfile *objfile);
1249
1250 static void find_file_and_directory (struct die_info *die,
1251                                      struct dwarf2_cu *cu,
1252                                      char **name, char **comp_dir);
1253
1254 static char *file_full_name (int file, struct line_header *lh,
1255                              const char *comp_dir);
1256
1257 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1258                                               gdb_byte *info_ptr,
1259                                               gdb_byte *buffer,
1260                                               unsigned int buffer_size,
1261                                               bfd *abfd);
1262
1263 static void init_cu_die_reader (struct die_reader_specs *reader,
1264                                 struct dwarf2_cu *cu);
1265
1266 static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1267
1268 #if WORDS_BIGENDIAN
1269
1270 /* Convert VALUE between big- and little-endian.  */
1271 static offset_type
1272 byte_swap (offset_type value)
1273 {
1274   offset_type result;
1275
1276   result = (value & 0xff) << 24;
1277   result |= (value & 0xff00) << 8;
1278   result |= (value & 0xff0000) >> 8;
1279   result |= (value & 0xff000000) >> 24;
1280   return result;
1281 }
1282
1283 #define MAYBE_SWAP(V)  byte_swap (V)
1284
1285 #else
1286 #define MAYBE_SWAP(V) (V)
1287 #endif /* WORDS_BIGENDIAN */
1288
1289 /* The suffix for an index file.  */
1290 #define INDEX_SUFFIX ".gdb-index"
1291
1292 /* Try to locate the sections we need for DWARF 2 debugging
1293    information and return true if we have enough to do something.  */
1294
1295 int
1296 dwarf2_has_info (struct objfile *objfile)
1297 {
1298   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1299   if (!dwarf2_per_objfile)
1300     {
1301       /* Initialize per-objfile state.  */
1302       struct dwarf2_per_objfile *data
1303         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1304
1305       memset (data, 0, sizeof (*data));
1306       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1307       dwarf2_per_objfile = data;
1308
1309       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1310       dwarf2_per_objfile->objfile = objfile;
1311     }
1312   return (dwarf2_per_objfile->info.asection != NULL
1313           && dwarf2_per_objfile->abbrev.asection != NULL);
1314 }
1315
1316 /* When loading sections, we can either look for ".<name>", or for
1317  * ".z<name>", which indicates a compressed section.  */
1318
1319 static int
1320 section_is_p (const char *section_name, const char *name)
1321 {
1322   return (section_name[0] == '.'
1323           && (strcmp (section_name + 1, name) == 0
1324               || (section_name[1] == 'z'
1325                   && strcmp (section_name + 2, name) == 0)));
1326 }
1327
1328 /* This function is mapped across the sections and remembers the
1329    offset and size of each of the debugging sections we are interested
1330    in.  */
1331
1332 static void
1333 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1334 {
1335   if (section_is_p (sectp->name, INFO_SECTION))
1336     {
1337       dwarf2_per_objfile->info.asection = sectp;
1338       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1339     }
1340   else if (section_is_p (sectp->name, ABBREV_SECTION))
1341     {
1342       dwarf2_per_objfile->abbrev.asection = sectp;
1343       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1344     }
1345   else if (section_is_p (sectp->name, LINE_SECTION))
1346     {
1347       dwarf2_per_objfile->line.asection = sectp;
1348       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1349     }
1350   else if (section_is_p (sectp->name, LOC_SECTION))
1351     {
1352       dwarf2_per_objfile->loc.asection = sectp;
1353       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1354     }
1355   else if (section_is_p (sectp->name, MACINFO_SECTION))
1356     {
1357       dwarf2_per_objfile->macinfo.asection = sectp;
1358       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1359     }
1360   else if (section_is_p (sectp->name, STR_SECTION))
1361     {
1362       dwarf2_per_objfile->str.asection = sectp;
1363       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1364     }
1365   else if (section_is_p (sectp->name, FRAME_SECTION))
1366     {
1367       dwarf2_per_objfile->frame.asection = sectp;
1368       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1369     }
1370   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1371     {
1372       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1373
1374       if (aflag & SEC_HAS_CONTENTS)
1375         {
1376           dwarf2_per_objfile->eh_frame.asection = sectp;
1377           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1378         }
1379     }
1380   else if (section_is_p (sectp->name, RANGES_SECTION))
1381     {
1382       dwarf2_per_objfile->ranges.asection = sectp;
1383       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1384     }
1385   else if (section_is_p (sectp->name, TYPES_SECTION))
1386     {
1387       dwarf2_per_objfile->types.asection = sectp;
1388       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1389     }
1390   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1391     {
1392       dwarf2_per_objfile->gdb_index.asection = sectp;
1393       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1394     }
1395
1396   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1397       && bfd_section_vma (abfd, sectp) == 0)
1398     dwarf2_per_objfile->has_section_at_zero = 1;
1399 }
1400
1401 /* Decompress a section that was compressed using zlib.  Store the
1402    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1403
1404 static void
1405 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1406                          gdb_byte **outbuf, bfd_size_type *outsize)
1407 {
1408   bfd *abfd = objfile->obfd;
1409 #ifndef HAVE_ZLIB_H
1410   error (_("Support for zlib-compressed DWARF data (from '%s') "
1411            "is disabled in this copy of GDB"),
1412          bfd_get_filename (abfd));
1413 #else
1414   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1415   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1416   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1417   bfd_size_type uncompressed_size;
1418   gdb_byte *uncompressed_buffer;
1419   z_stream strm;
1420   int rc;
1421   int header_size = 12;
1422
1423   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1424       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1425     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1426            bfd_get_filename (abfd));
1427
1428   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1429      by the uncompressed section size, 8 bytes in big-endian order.  */
1430   if (compressed_size < header_size
1431       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1432     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1433            bfd_get_filename (abfd));
1434   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1435   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1436   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1437   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1438   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1439   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1440   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1441   uncompressed_size += compressed_buffer[11];
1442
1443   /* It is possible the section consists of several compressed
1444      buffers concatenated together, so we uncompress in a loop.  */
1445   strm.zalloc = NULL;
1446   strm.zfree = NULL;
1447   strm.opaque = NULL;
1448   strm.avail_in = compressed_size - header_size;
1449   strm.next_in = (Bytef*) compressed_buffer + header_size;
1450   strm.avail_out = uncompressed_size;
1451   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1452                                        uncompressed_size);
1453   rc = inflateInit (&strm);
1454   while (strm.avail_in > 0)
1455     {
1456       if (rc != Z_OK)
1457         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1458                bfd_get_filename (abfd), rc);
1459       strm.next_out = ((Bytef*) uncompressed_buffer
1460                        + (uncompressed_size - strm.avail_out));
1461       rc = inflate (&strm, Z_FINISH);
1462       if (rc != Z_STREAM_END)
1463         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1464                bfd_get_filename (abfd), rc);
1465       rc = inflateReset (&strm);
1466     }
1467   rc = inflateEnd (&strm);
1468   if (rc != Z_OK
1469       || strm.avail_out != 0)
1470     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1471            bfd_get_filename (abfd), rc);
1472
1473   do_cleanups (cleanup);
1474   *outbuf = uncompressed_buffer;
1475   *outsize = uncompressed_size;
1476 #endif
1477 }
1478
1479 /* Read the contents of the section SECTP from object file specified by
1480    OBJFILE, store info about the section into INFO.
1481    If the section is compressed, uncompress it before returning.  */
1482
1483 static void
1484 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1485 {
1486   bfd *abfd = objfile->obfd;
1487   asection *sectp = info->asection;
1488   gdb_byte *buf, *retbuf;
1489   unsigned char header[4];
1490
1491   if (info->readin)
1492     return;
1493   info->buffer = NULL;
1494   info->was_mmapped = 0;
1495   info->readin = 1;
1496
1497   if (info->asection == NULL || info->size == 0)
1498     return;
1499
1500   /* Check if the file has a 4-byte header indicating compression.  */
1501   if (info->size > sizeof (header)
1502       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1503       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1504     {
1505       /* Upon decompression, update the buffer and its size.  */
1506       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1507         {
1508           zlib_decompress_section (objfile, sectp, &info->buffer,
1509                                    &info->size);
1510           return;
1511         }
1512     }
1513
1514 #ifdef HAVE_MMAP
1515   if (pagesize == 0)
1516     pagesize = getpagesize ();
1517
1518   /* Only try to mmap sections which are large enough: we don't want to
1519      waste space due to fragmentation.  Also, only try mmap for sections
1520      without relocations.  */
1521
1522   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1523     {
1524       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1525       size_t map_length = info->size + sectp->filepos - pg_offset;
1526       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1527                                  MAP_PRIVATE, pg_offset);
1528
1529       if (retbuf != MAP_FAILED)
1530         {
1531           info->was_mmapped = 1;
1532           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1533 #if HAVE_POSIX_MADVISE
1534           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1535 #endif
1536           return;
1537         }
1538     }
1539 #endif
1540
1541   /* If we get here, we are a normal, not-compressed section.  */
1542   info->buffer = buf
1543     = obstack_alloc (&objfile->objfile_obstack, info->size);
1544
1545   /* When debugging .o files, we may need to apply relocations; see
1546      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1547      We never compress sections in .o files, so we only need to
1548      try this when the section is not compressed.  */
1549   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1550   if (retbuf != NULL)
1551     {
1552       info->buffer = retbuf;
1553       return;
1554     }
1555
1556   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1557       || bfd_bread (buf, info->size, abfd) != info->size)
1558     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1559            bfd_get_filename (abfd));
1560 }
1561
1562 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1563    SECTION_NAME. */
1564
1565 void
1566 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1567                          asection **sectp, gdb_byte **bufp,
1568                          bfd_size_type *sizep)
1569 {
1570   struct dwarf2_per_objfile *data
1571     = objfile_data (objfile, dwarf2_objfile_data_key);
1572   struct dwarf2_section_info *info;
1573
1574   /* We may see an objfile without any DWARF, in which case we just
1575      return nothing.  */
1576   if (data == NULL)
1577     {
1578       *sectp = NULL;
1579       *bufp = NULL;
1580       *sizep = 0;
1581       return;
1582     }
1583   if (section_is_p (section_name, EH_FRAME_SECTION))
1584     info = &data->eh_frame;
1585   else if (section_is_p (section_name, FRAME_SECTION))
1586     info = &data->frame;
1587   else
1588     gdb_assert_not_reached ("unexpected section");
1589
1590   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1591     /* We haven't read this section in yet.  Do it now.  */
1592     dwarf2_read_section (objfile, info);
1593
1594   *sectp = info->asection;
1595   *bufp = info->buffer;
1596   *sizep = info->size;
1597 }
1598
1599 \f
1600
1601 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1602    this CU came.  */
1603 static void
1604 dw2_do_instantiate_symtab (struct objfile *objfile,
1605                            struct dwarf2_per_cu_data *per_cu)
1606 {
1607   struct cleanup *back_to;
1608
1609   back_to = make_cleanup (dwarf2_release_queue, NULL);
1610
1611   queue_comp_unit (per_cu, objfile);
1612
1613   if (per_cu->from_debug_types)
1614     read_signatured_type_at_offset (objfile, per_cu->offset);
1615   else
1616     load_full_comp_unit (per_cu, objfile);
1617
1618   process_queue (objfile);
1619
1620   /* Age the cache, releasing compilation units that have not
1621      been used recently.  */
1622   age_cached_comp_units ();
1623
1624   do_cleanups (back_to);
1625 }
1626
1627 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1628    the objfile from which this CU came.  Returns the resulting symbol
1629    table.  */
1630 static struct symtab *
1631 dw2_instantiate_symtab (struct objfile *objfile,
1632                         struct dwarf2_per_cu_data *per_cu)
1633 {
1634   if (!per_cu->v.quick->symtab)
1635     {
1636       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1637       increment_reading_symtab ();
1638       dw2_do_instantiate_symtab (objfile, per_cu);
1639       do_cleanups (back_to);
1640     }
1641   return per_cu->v.quick->symtab;
1642 }
1643
1644 /* Return the CU given its index.  */
1645 static struct dwarf2_per_cu_data *
1646 dw2_get_cu (int index)
1647 {
1648   if (index >= dwarf2_per_objfile->n_comp_units)
1649     {
1650       index -= dwarf2_per_objfile->n_comp_units;
1651       return dwarf2_per_objfile->type_comp_units[index];
1652     }
1653   return dwarf2_per_objfile->all_comp_units[index];
1654 }
1655
1656 /* A helper function that knows how to read a 64-bit value in a way
1657    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1658    otherwise.  */
1659 static int
1660 extract_cu_value (const char *bytes, ULONGEST *result)
1661 {
1662   if (sizeof (ULONGEST) < 8)
1663     {
1664       int i;
1665
1666       /* Ignore the upper 4 bytes if they are all zero.  */
1667       for (i = 0; i < 4; ++i)
1668         if (bytes[i + 4] != 0)
1669           return 0;
1670
1671       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1672     }
1673   else
1674     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1675   return 1;
1676 }
1677
1678 /* Read the CU list from the mapped index, and use it to create all
1679    the CU objects for this objfile.  Return 0 if something went wrong,
1680    1 if everything went ok.  */
1681 static int
1682 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1683                        offset_type cu_list_elements)
1684 {
1685   offset_type i;
1686
1687   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1688   dwarf2_per_objfile->all_comp_units
1689     = obstack_alloc (&objfile->objfile_obstack,
1690                      dwarf2_per_objfile->n_comp_units
1691                      * sizeof (struct dwarf2_per_cu_data *));
1692
1693   for (i = 0; i < cu_list_elements; i += 2)
1694     {
1695       struct dwarf2_per_cu_data *the_cu;
1696       ULONGEST offset, length;
1697
1698       if (!extract_cu_value (cu_list, &offset)
1699           || !extract_cu_value (cu_list + 8, &length))
1700         return 0;
1701       cu_list += 2 * 8;
1702
1703       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1704                                struct dwarf2_per_cu_data);
1705       the_cu->offset = offset;
1706       the_cu->length = length;
1707       the_cu->objfile = objfile;
1708       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1709                                         struct dwarf2_per_cu_quick_data);
1710       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1711     }
1712
1713   return 1;
1714 }
1715
1716 /* Create the signatured type hash table from the index.  */
1717 static int
1718 create_signatured_type_hash_from_index (struct objfile *objfile,
1719                                         const gdb_byte *bytes,
1720                                         offset_type elements)
1721 {
1722   offset_type i;
1723   htab_t type_hash;
1724
1725   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1726   dwarf2_per_objfile->type_comp_units
1727     = obstack_alloc (&objfile->objfile_obstack,
1728                      dwarf2_per_objfile->n_type_comp_units
1729                      * sizeof (struct dwarf2_per_cu_data *));
1730
1731   type_hash = allocate_signatured_type_hash_table (objfile);
1732
1733   for (i = 0; i < elements; i += 3)
1734     {
1735       struct signatured_type *type_sig;
1736       ULONGEST offset, type_offset, signature;
1737       void **slot;
1738
1739       if (!extract_cu_value (bytes, &offset)
1740           || !extract_cu_value (bytes + 8, &type_offset))
1741         return 0;
1742       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1743       bytes += 3 * 8;
1744
1745       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1746                                  struct signatured_type);
1747       type_sig->signature = signature;
1748       type_sig->offset = offset;
1749       type_sig->type_offset = type_offset;
1750       type_sig->per_cu.from_debug_types = 1;
1751       type_sig->per_cu.offset = offset;
1752       type_sig->per_cu.objfile = objfile;
1753       type_sig->per_cu.v.quick
1754         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1755                           struct dwarf2_per_cu_quick_data);
1756
1757       slot = htab_find_slot (type_hash, type_sig, INSERT);
1758       *slot = type_sig;
1759
1760       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1761     }
1762
1763   dwarf2_per_objfile->signatured_types = type_hash;
1764
1765   return 1;
1766 }
1767
1768 /* Read the address map data from the mapped index, and use it to
1769    populate the objfile's psymtabs_addrmap.  */
1770 static void
1771 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1772 {
1773   const gdb_byte *iter, *end;
1774   struct obstack temp_obstack;
1775   struct addrmap *mutable_map;
1776   struct cleanup *cleanup;
1777   CORE_ADDR baseaddr;
1778
1779   obstack_init (&temp_obstack);
1780   cleanup = make_cleanup_obstack_free (&temp_obstack);
1781   mutable_map = addrmap_create_mutable (&temp_obstack);
1782
1783   iter = index->address_table;
1784   end = iter + index->address_table_size;
1785
1786   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1787
1788   while (iter < end)
1789     {
1790       ULONGEST hi, lo, cu_index;
1791       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1792       iter += 8;
1793       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1794       iter += 8;
1795       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1796       iter += 4;
1797       
1798       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1799                          dw2_get_cu (cu_index));
1800     }
1801
1802   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1803                                                     &objfile->objfile_obstack);
1804   do_cleanups (cleanup);
1805 }
1806
1807 /* The hash function for strings in the mapped index.  This is the
1808    same as the hashtab.c hash function, but we keep a separate copy to
1809    maintain control over the implementation.  This is necessary
1810    because the hash function is tied to the format of the mapped index
1811    file.  */
1812 static hashval_t
1813 mapped_index_string_hash (const void *p)
1814 {
1815   const unsigned char *str = (const unsigned char *) p;
1816   hashval_t r = 0;
1817   unsigned char c;
1818
1819   while ((c = *str++) != 0)
1820     r = r * 67 + c - 113;
1821
1822   return r;
1823 }
1824
1825 /* Find a slot in the mapped index INDEX for the object named NAME.
1826    If NAME is found, set *VEC_OUT to point to the CU vector in the
1827    constant pool and return 1.  If NAME cannot be found, return 0.  */
1828 static int
1829 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1830                           offset_type **vec_out)
1831 {
1832   offset_type hash = mapped_index_string_hash (name);
1833   offset_type slot, step;
1834
1835   slot = hash & (index->index_table_slots - 1);
1836   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1837
1838   for (;;)
1839     {
1840       /* Convert a slot number to an offset into the table.  */
1841       offset_type i = 2 * slot;
1842       const char *str;
1843       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1844         return 0;
1845
1846       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1847       if (!strcmp (name, str))
1848         {
1849           *vec_out = (offset_type *) (index->constant_pool
1850                                       + MAYBE_SWAP (index->index_table[i + 1]));
1851           return 1;
1852         }
1853
1854       slot = (slot + step) & (index->index_table_slots - 1);
1855     }
1856 }
1857
1858 /* Read the index file.  If everything went ok, initialize the "quick"
1859    elements of all the CUs and return 1.  Otherwise, return 0.  */
1860 static int
1861 dwarf2_read_index (struct objfile *objfile)
1862 {
1863   char *addr;
1864   struct mapped_index *map;
1865   offset_type *metadata;
1866   const gdb_byte *cu_list;
1867   const gdb_byte *types_list = NULL;
1868   offset_type version, cu_list_elements;
1869   offset_type types_list_elements = 0;
1870   int i;
1871
1872   if (dwarf2_per_objfile->gdb_index.asection == NULL
1873       || dwarf2_per_objfile->gdb_index.size == 0)
1874     return 0;
1875   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1876
1877   addr = dwarf2_per_objfile->gdb_index.buffer;
1878   /* Version check.  */
1879   version = MAYBE_SWAP (*(offset_type *) addr);
1880   if (version == 1)
1881     {
1882       /* Index version 1 neglected to account for .debug_types.  So,
1883          if we see .debug_types, we cannot use this index.  */
1884       if (dwarf2_per_objfile->types.asection != NULL
1885           && dwarf2_per_objfile->types.size != 0)
1886         return 0;
1887     }
1888   else if (version != 2)
1889     return 0;
1890
1891   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1892   map->total_size = dwarf2_per_objfile->gdb_index.size;
1893
1894   metadata = (offset_type *) (addr + sizeof (offset_type));
1895
1896   i = 0;
1897   cu_list = addr + MAYBE_SWAP (metadata[i]);
1898   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1899                       / 8);
1900   ++i;
1901
1902   if (version == 2)
1903     {
1904       types_list = addr + MAYBE_SWAP (metadata[i]);
1905       types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1906                               - MAYBE_SWAP (metadata[i]))
1907                              / 8);
1908       ++i;
1909     }
1910
1911   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1912   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1913                              - MAYBE_SWAP (metadata[i]));
1914   ++i;
1915
1916   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1917   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1918                              - MAYBE_SWAP (metadata[i]))
1919                             / (2 * sizeof (offset_type)));
1920   ++i;
1921
1922   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1923
1924   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1925     return 0;
1926
1927   if (version == 2
1928       && types_list_elements
1929       && !create_signatured_type_hash_from_index (objfile, types_list,
1930                                                   types_list_elements))
1931     return 0;
1932
1933   create_addrmap_from_index (objfile, map);
1934
1935   dwarf2_per_objfile->index_table = map;
1936   dwarf2_per_objfile->using_index = 1;
1937
1938   return 1;
1939 }
1940
1941 /* A helper for the "quick" functions which sets the global
1942    dwarf2_per_objfile according to OBJFILE.  */
1943 static void
1944 dw2_setup (struct objfile *objfile)
1945 {
1946   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1947   gdb_assert (dwarf2_per_objfile);
1948 }
1949
1950 /* A helper for the "quick" functions which attempts to read the line
1951    table for THIS_CU.  */
1952 static void
1953 dw2_require_line_header (struct objfile *objfile,
1954                          struct dwarf2_per_cu_data *this_cu)
1955 {
1956   bfd *abfd = objfile->obfd;
1957   struct line_header *lh = NULL;
1958   struct attribute *attr;
1959   struct cleanup *cleanups;
1960   struct die_info *comp_unit_die;
1961   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1962   int has_children, i;
1963   struct dwarf2_cu cu;
1964   unsigned int bytes_read, buffer_size;
1965   struct die_reader_specs reader_specs;
1966   char *name, *comp_dir;
1967
1968   if (this_cu->v.quick->read_lines)
1969     return;
1970   this_cu->v.quick->read_lines = 1;
1971
1972   memset (&cu, 0, sizeof (cu));
1973   cu.objfile = objfile;
1974   obstack_init (&cu.comp_unit_obstack);
1975
1976   cleanups = make_cleanup (free_stack_comp_unit, &cu);
1977
1978   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1979   buffer_size = dwarf2_per_objfile->info.size;
1980   buffer = dwarf2_per_objfile->info.buffer;
1981   info_ptr = buffer + this_cu->offset;
1982   beg_of_comp_unit = info_ptr;
1983
1984   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1985                                           buffer, buffer_size,
1986                                           abfd);
1987
1988   /* Complete the cu_header.  */
1989   cu.header.offset = beg_of_comp_unit - buffer;
1990   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1991
1992   this_cu->cu = &cu;
1993   cu.per_cu = this_cu;
1994
1995   dwarf2_read_abbrevs (abfd, &cu);
1996   make_cleanup (dwarf2_free_abbrev_table, &cu);
1997
1998   if (this_cu->from_debug_types)
1999     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2000   init_cu_die_reader (&reader_specs, &cu);
2001   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2002                             &has_children);
2003
2004   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2005   if (attr)
2006     {
2007       unsigned int line_offset = DW_UNSND (attr);
2008       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2009     }
2010   if (lh == NULL)
2011     {
2012       do_cleanups (cleanups);
2013       return;
2014     }
2015
2016   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2017
2018   this_cu->v.quick->lines = lh;
2019
2020   this_cu->v.quick->file_names
2021     = obstack_alloc (&objfile->objfile_obstack,
2022                      lh->num_file_names * sizeof (char *));
2023   for (i = 0; i < lh->num_file_names; ++i)
2024     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2025
2026   do_cleanups (cleanups);
2027 }
2028
2029 /* A helper for the "quick" functions which computes and caches the
2030    real path for a given file name from the line table.
2031    dw2_require_line_header must have been called before this is
2032    invoked.  */
2033 static const char *
2034 dw2_require_full_path (struct objfile *objfile,
2035                        struct dwarf2_per_cu_data *cu,
2036                        int index)
2037 {
2038   if (!cu->v.quick->full_names)
2039     cu->v.quick->full_names
2040       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2041                         cu->v.quick->lines->num_file_names,
2042                         sizeof (char *));
2043
2044   if (!cu->v.quick->full_names[index])
2045     cu->v.quick->full_names[index]
2046       = gdb_realpath (cu->v.quick->file_names[index]);
2047
2048   return cu->v.quick->full_names[index];
2049 }
2050
2051 static struct symtab *
2052 dw2_find_last_source_symtab (struct objfile *objfile)
2053 {
2054   int index;
2055   dw2_setup (objfile);
2056   index = dwarf2_per_objfile->n_comp_units - 1;
2057   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2058 }
2059
2060 static void
2061 dw2_forget_cached_source_info (struct objfile *objfile)
2062 {
2063   int i;
2064
2065   dw2_setup (objfile);
2066   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2067                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2068     {
2069       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2070
2071       if (cu->v.quick->full_names)
2072         {
2073           int j;
2074
2075           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2076             xfree ((void *) cu->v.quick->full_names[j]);
2077         }
2078     }
2079 }
2080
2081 static int
2082 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2083                    const char *full_path, const char *real_path,
2084                    struct symtab **result)
2085 {
2086   int i;
2087   int check_basename = lbasename (name) == name;
2088   struct dwarf2_per_cu_data *base_cu = NULL;
2089
2090   dw2_setup (objfile);
2091   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2092                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2093     {
2094       int j;
2095       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2096
2097       if (cu->v.quick->symtab)
2098         continue;
2099
2100       dw2_require_line_header (objfile, cu);
2101       if (!cu->v.quick->lines)
2102         continue;
2103
2104       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2105         {
2106           const char *this_name = cu->v.quick->file_names[j];
2107
2108           if (FILENAME_CMP (name, this_name) == 0)
2109             {
2110               *result = dw2_instantiate_symtab (objfile, cu);
2111               return 1;
2112             }
2113
2114           if (check_basename && ! base_cu
2115               && FILENAME_CMP (lbasename (this_name), name) == 0)
2116             base_cu = cu;
2117
2118           if (full_path != NULL)
2119             {
2120               const char *this_full_name = dw2_require_full_path (objfile,
2121                                                                   cu, j);
2122
2123               if (this_full_name
2124                   && FILENAME_CMP (full_path, this_full_name) == 0)
2125                 {
2126                   *result = dw2_instantiate_symtab (objfile, cu);
2127                   return 1;
2128                 }
2129             }
2130
2131           if (real_path != NULL)
2132             {
2133               const char *this_full_name = dw2_require_full_path (objfile,
2134                                                                   cu, j);
2135
2136               if (this_full_name != NULL)
2137                 {
2138                   char *rp = gdb_realpath (this_full_name);
2139                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2140                     {
2141                       xfree (rp);
2142                       *result = dw2_instantiate_symtab (objfile, cu);
2143                       return 1;
2144                     }
2145                   xfree (rp);
2146                 }
2147             }
2148         }
2149     }
2150
2151   if (base_cu)
2152     {
2153       *result = dw2_instantiate_symtab (objfile, base_cu);
2154       return 1;
2155     }
2156
2157   return 0;
2158 }
2159
2160 static struct symtab *
2161 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2162                    const char *name, domain_enum domain)
2163 {
2164   /* We do all the work in the pre_expand_symtabs_matching hook
2165      instead.  */
2166   return NULL;
2167 }
2168
2169 /* A helper function that expands all symtabs that hold an object
2170    named NAME.  */
2171 static void
2172 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2173 {
2174   dw2_setup (objfile);
2175
2176   if (dwarf2_per_objfile->index_table)
2177     {
2178       offset_type *vec;
2179
2180       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2181                                     name, &vec))
2182         {
2183           offset_type i, len = MAYBE_SWAP (*vec);
2184           for (i = 0; i < len; ++i)
2185             {
2186               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2187               struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2188
2189               dw2_instantiate_symtab (objfile, cu);
2190             }
2191         }
2192     }
2193 }
2194
2195 static void
2196 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2197                                  int kind, const char *name,
2198                                  domain_enum domain)
2199 {
2200   dw2_do_expand_symtabs_matching (objfile, name);
2201 }
2202
2203 static void
2204 dw2_print_stats (struct objfile *objfile)
2205 {
2206   int i, count;
2207
2208   dw2_setup (objfile);
2209   count = 0;
2210   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2211                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2212     {
2213       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2214
2215       if (!cu->v.quick->symtab)
2216         ++count;
2217     }
2218   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2219 }
2220
2221 static void
2222 dw2_dump (struct objfile *objfile)
2223 {
2224   /* Nothing worth printing.  */
2225 }
2226
2227 static void
2228 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2229               struct section_offsets *delta)
2230 {
2231   /* There's nothing to relocate here.  */
2232 }
2233
2234 static void
2235 dw2_expand_symtabs_for_function (struct objfile *objfile,
2236                                  const char *func_name)
2237 {
2238   dw2_do_expand_symtabs_matching (objfile, func_name);
2239 }
2240
2241 static void
2242 dw2_expand_all_symtabs (struct objfile *objfile)
2243 {
2244   int i;
2245
2246   dw2_setup (objfile);
2247
2248   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2249                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2250     {
2251       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2252
2253       dw2_instantiate_symtab (objfile, cu);
2254     }
2255 }
2256
2257 static void
2258 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2259                                   const char *filename)
2260 {
2261   int i;
2262
2263   dw2_setup (objfile);
2264   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2265                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2266     {
2267       int j;
2268       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2269
2270       if (cu->v.quick->symtab)
2271         continue;
2272
2273       dw2_require_line_header (objfile, cu);
2274       if (!cu->v.quick->lines)
2275         continue;
2276
2277       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2278         {
2279           const char *this_name = cu->v.quick->file_names[j];
2280           if (strcmp (this_name, filename) == 0)
2281             {
2282               dw2_instantiate_symtab (objfile, cu);
2283               break;
2284             }
2285         }
2286     }
2287 }
2288
2289 static const char *
2290 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2291 {
2292   struct dwarf2_per_cu_data *cu;
2293   offset_type *vec;
2294
2295   dw2_setup (objfile);
2296
2297   if (!dwarf2_per_objfile->index_table)
2298     return NULL;
2299
2300   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2301                                  name, &vec))
2302     return NULL;
2303
2304   /* Note that this just looks at the very first one named NAME -- but
2305      actually we are looking for a function.  find_main_filename
2306      should be rewritten so that it doesn't require a custom hook.  It
2307      could just use the ordinary symbol tables.  */
2308   /* vec[0] is the length, which must always be >0.  */
2309   cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2310
2311   dw2_require_line_header (objfile, cu);
2312   if (!cu->v.quick->lines)
2313     return NULL;
2314
2315   return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2316 }
2317
2318 static void
2319 dw2_map_ada_symtabs (struct objfile *objfile,
2320                      int (*wild_match) (const char *, int, const char *),
2321                      int (*is_name_suffix) (const char *),
2322                      void (*callback) (struct objfile *,
2323                                        struct symtab *, void *),
2324                      const char *name, int global,
2325                      domain_enum namespace, int wild,
2326                      void *data)
2327 {
2328   /* For now, we don't support Ada, so this function can't be
2329      reached.  */
2330   internal_error (__FILE__, __LINE__,
2331                   _("map_ada_symtabs called via index method"));
2332 }
2333
2334 static void
2335 dw2_expand_symtabs_matching (struct objfile *objfile,
2336                              int (*file_matcher) (const char *, void *),
2337                              int (*name_matcher) (const char *, void *),
2338                              domain_enum kind,
2339                              void *data)
2340 {
2341   int i;
2342   offset_type iter;
2343
2344   dw2_setup (objfile);
2345   if (!dwarf2_per_objfile->index_table)
2346     return;
2347
2348   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2349                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2350     {
2351       int j;
2352       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2353
2354       cu->v.quick->mark = 0;
2355       if (cu->v.quick->symtab)
2356         continue;
2357
2358       dw2_require_line_header (objfile, cu);
2359       if (!cu->v.quick->lines)
2360         continue;
2361
2362       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2363         {
2364           if (file_matcher (cu->v.quick->file_names[j], data))
2365             {
2366               cu->v.quick->mark = 1;
2367               break;
2368             }
2369         }
2370     }
2371
2372   for (iter = 0;
2373        iter < dwarf2_per_objfile->index_table->index_table_slots;
2374        ++iter)
2375     {
2376       offset_type idx = 2 * iter;
2377       const char *name;
2378       offset_type *vec, vec_len, vec_idx;
2379
2380       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2381           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2382         continue;
2383
2384       name = (dwarf2_per_objfile->index_table->constant_pool
2385               + dwarf2_per_objfile->index_table->index_table[idx]);
2386
2387       if (! (*name_matcher) (name, data))
2388         continue;
2389
2390       /* The name was matched, now expand corresponding CUs that were
2391          marked.  */
2392       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2393                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2394       vec_len = MAYBE_SWAP (vec[0]);
2395       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2396         {
2397           struct dwarf2_per_cu_data *cu;
2398
2399           cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2400           if (cu->v.quick->mark)
2401             dw2_instantiate_symtab (objfile, cu);
2402         }
2403     }
2404 }
2405
2406 static struct symtab *
2407 dw2_find_pc_sect_symtab (struct objfile *objfile,
2408                          struct minimal_symbol *msymbol,
2409                          CORE_ADDR pc,
2410                          struct obj_section *section,
2411                          int warn_if_readin)
2412 {
2413   struct dwarf2_per_cu_data *data;
2414
2415   dw2_setup (objfile);
2416
2417   if (!objfile->psymtabs_addrmap)
2418     return NULL;
2419
2420   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2421   if (!data)
2422     return NULL;
2423
2424   if (warn_if_readin && data->v.quick->symtab)
2425     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2426              paddress (get_objfile_arch (objfile), pc));
2427
2428   return dw2_instantiate_symtab (objfile, data);
2429 }
2430
2431 static void
2432 dw2_map_symbol_names (struct objfile *objfile,
2433                       void (*fun) (const char *, void *),
2434                       void *data)
2435 {
2436   offset_type iter;
2437   dw2_setup (objfile);
2438
2439   if (!dwarf2_per_objfile->index_table)
2440     return;
2441
2442   for (iter = 0;
2443        iter < dwarf2_per_objfile->index_table->index_table_slots;
2444        ++iter)
2445     {
2446       offset_type idx = 2 * iter;
2447       const char *name;
2448       offset_type *vec, vec_len, vec_idx;
2449
2450       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2451           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2452         continue;
2453
2454       name = (dwarf2_per_objfile->index_table->constant_pool
2455               + dwarf2_per_objfile->index_table->index_table[idx]);
2456
2457       (*fun) (name, data);
2458     }
2459 }
2460
2461 static void
2462 dw2_map_symbol_filenames (struct objfile *objfile,
2463                           void (*fun) (const char *, const char *, void *),
2464                           void *data)
2465 {
2466   int i;
2467
2468   dw2_setup (objfile);
2469   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2470                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2471     {
2472       int j;
2473       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2474
2475       if (cu->v.quick->symtab)
2476         continue;
2477
2478       dw2_require_line_header (objfile, cu);
2479       if (!cu->v.quick->lines)
2480         continue;
2481
2482       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2483         {
2484           const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2485           (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2486         }
2487     }
2488 }
2489
2490 static int
2491 dw2_has_symbols (struct objfile *objfile)
2492 {
2493   return 1;
2494 }
2495
2496 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2497 {
2498   dw2_has_symbols,
2499   dw2_find_last_source_symtab,
2500   dw2_forget_cached_source_info,
2501   dw2_lookup_symtab,
2502   dw2_lookup_symbol,
2503   dw2_pre_expand_symtabs_matching,
2504   dw2_print_stats,
2505   dw2_dump,
2506   dw2_relocate,
2507   dw2_expand_symtabs_for_function,
2508   dw2_expand_all_symtabs,
2509   dw2_expand_symtabs_with_filename,
2510   dw2_find_symbol_file,
2511   dw2_map_ada_symtabs,
2512   dw2_expand_symtabs_matching,
2513   dw2_find_pc_sect_symtab,
2514   dw2_map_symbol_names,
2515   dw2_map_symbol_filenames
2516 };
2517
2518 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2519    file will use psymtabs, or 1 if using the GNU index.  */
2520
2521 int
2522 dwarf2_initialize_objfile (struct objfile *objfile)
2523 {
2524   /* If we're about to read full symbols, don't bother with the
2525      indices.  In this case we also don't care if some other debug
2526      format is making psymtabs, because they are all about to be
2527      expanded anyway.  */
2528   if ((objfile->flags & OBJF_READNOW))
2529     {
2530       int i;
2531
2532       dwarf2_per_objfile->using_index = 1;
2533       create_all_comp_units (objfile);
2534       create_debug_types_hash_table (objfile);
2535
2536       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2537                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2538         {
2539           struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2540
2541           cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542                                         struct dwarf2_per_cu_quick_data);
2543         }
2544
2545       /* Return 1 so that gdb sees the "quick" functions.  However,
2546          these functions will be no-ops because we will have expanded
2547          all symtabs.  */
2548       return 1;
2549     }
2550
2551   if (dwarf2_read_index (objfile))
2552     return 1;
2553
2554   dwarf2_build_psymtabs (objfile);
2555   return 0;
2556 }
2557
2558 \f
2559
2560 /* Build a partial symbol table.  */
2561
2562 void
2563 dwarf2_build_psymtabs (struct objfile *objfile)
2564 {
2565   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2566     {
2567       init_psymbol_list (objfile, 1024);
2568     }
2569
2570   dwarf2_build_psymtabs_hard (objfile);
2571 }
2572
2573 /* Return TRUE if OFFSET is within CU_HEADER.  */
2574
2575 static inline int
2576 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2577 {
2578   unsigned int bottom = cu_header->offset;
2579   unsigned int top = (cu_header->offset
2580                       + cu_header->length
2581                       + cu_header->initial_length_size);
2582
2583   return (offset >= bottom && offset < top);
2584 }
2585
2586 /* Read in the comp unit header information from the debug_info at info_ptr.
2587    NOTE: This leaves members offset, first_die_offset to be filled in
2588    by the caller.  */
2589
2590 static gdb_byte *
2591 read_comp_unit_head (struct comp_unit_head *cu_header,
2592                      gdb_byte *info_ptr, bfd *abfd)
2593 {
2594   int signed_addr;
2595   unsigned int bytes_read;
2596
2597   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2598   cu_header->initial_length_size = bytes_read;
2599   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2600   info_ptr += bytes_read;
2601   cu_header->version = read_2_bytes (abfd, info_ptr);
2602   info_ptr += 2;
2603   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2604                                           &bytes_read);
2605   info_ptr += bytes_read;
2606   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2607   info_ptr += 1;
2608   signed_addr = bfd_get_sign_extend_vma (abfd);
2609   if (signed_addr < 0)
2610     internal_error (__FILE__, __LINE__,
2611                     _("read_comp_unit_head: dwarf from non elf file"));
2612   cu_header->signed_addr_p = signed_addr;
2613
2614   return info_ptr;
2615 }
2616
2617 static gdb_byte *
2618 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2619                              gdb_byte *buffer, unsigned int buffer_size,
2620                              bfd *abfd)
2621 {
2622   gdb_byte *beg_of_comp_unit = info_ptr;
2623
2624   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2625
2626   if (header->version != 2 && header->version != 3 && header->version != 4)
2627     error (_("Dwarf Error: wrong version in compilation unit header "
2628            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2629            bfd_get_filename (abfd));
2630
2631   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2632     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2633            "(offset 0x%lx + 6) [in module %s]"),
2634            (long) header->abbrev_offset,
2635            (long) (beg_of_comp_unit - buffer),
2636            bfd_get_filename (abfd));
2637
2638   if (beg_of_comp_unit + header->length + header->initial_length_size
2639       > buffer + buffer_size)
2640     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2641            "(offset 0x%lx + 0) [in module %s]"),
2642            (long) header->length,
2643            (long) (beg_of_comp_unit - buffer),
2644            bfd_get_filename (abfd));
2645
2646   return info_ptr;
2647 }
2648
2649 /* Read in the types comp unit header information from .debug_types entry at
2650    types_ptr.  The result is a pointer to one past the end of the header.  */
2651
2652 static gdb_byte *
2653 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2654                           ULONGEST *signature,
2655                           gdb_byte *types_ptr, bfd *abfd)
2656 {
2657   gdb_byte *initial_types_ptr = types_ptr;
2658
2659   dwarf2_read_section (dwarf2_per_objfile->objfile,
2660                        &dwarf2_per_objfile->types);
2661   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2662
2663   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2664
2665   *signature = read_8_bytes (abfd, types_ptr);
2666   types_ptr += 8;
2667   types_ptr += cu_header->offset_size;
2668   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2669
2670   return types_ptr;
2671 }
2672
2673 /* Allocate a new partial symtab for file named NAME and mark this new
2674    partial symtab as being an include of PST.  */
2675
2676 static void
2677 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2678                                struct objfile *objfile)
2679 {
2680   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2681
2682   subpst->section_offsets = pst->section_offsets;
2683   subpst->textlow = 0;
2684   subpst->texthigh = 0;
2685
2686   subpst->dependencies = (struct partial_symtab **)
2687     obstack_alloc (&objfile->objfile_obstack,
2688                    sizeof (struct partial_symtab *));
2689   subpst->dependencies[0] = pst;
2690   subpst->number_of_dependencies = 1;
2691
2692   subpst->globals_offset = 0;
2693   subpst->n_global_syms = 0;
2694   subpst->statics_offset = 0;
2695   subpst->n_static_syms = 0;
2696   subpst->symtab = NULL;
2697   subpst->read_symtab = pst->read_symtab;
2698   subpst->readin = 0;
2699
2700   /* No private part is necessary for include psymtabs.  This property
2701      can be used to differentiate between such include psymtabs and
2702      the regular ones.  */
2703   subpst->read_symtab_private = NULL;
2704 }
2705
2706 /* Read the Line Number Program data and extract the list of files
2707    included by the source file represented by PST.  Build an include
2708    partial symtab for each of these included files.  */
2709
2710 static void
2711 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2712                                struct die_info *die,
2713                                struct partial_symtab *pst)
2714 {
2715   struct objfile *objfile = cu->objfile;
2716   bfd *abfd = objfile->obfd;
2717   struct line_header *lh = NULL;
2718   struct attribute *attr;
2719
2720   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2721   if (attr)
2722     {
2723       unsigned int line_offset = DW_UNSND (attr);
2724
2725       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2726     }
2727   if (lh == NULL)
2728     return;  /* No linetable, so no includes.  */
2729
2730   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2731
2732   free_line_header (lh);
2733 }
2734
2735 static hashval_t
2736 hash_type_signature (const void *item)
2737 {
2738   const struct signatured_type *type_sig = item;
2739
2740   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2741   return type_sig->signature;
2742 }
2743
2744 static int
2745 eq_type_signature (const void *item_lhs, const void *item_rhs)
2746 {
2747   const struct signatured_type *lhs = item_lhs;
2748   const struct signatured_type *rhs = item_rhs;
2749
2750   return lhs->signature == rhs->signature;
2751 }
2752
2753 /* Allocate a hash table for signatured types.  */
2754
2755 static htab_t
2756 allocate_signatured_type_hash_table (struct objfile *objfile)
2757 {
2758   return htab_create_alloc_ex (41,
2759                                hash_type_signature,
2760                                eq_type_signature,
2761                                NULL,
2762                                &objfile->objfile_obstack,
2763                                hashtab_obstack_allocate,
2764                                dummy_obstack_deallocate);
2765 }
2766
2767 /* A helper function to add a signatured type CU to a list.  */
2768
2769 static int
2770 add_signatured_type_cu_to_list (void **slot, void *datum)
2771 {
2772   struct signatured_type *sigt = *slot;
2773   struct dwarf2_per_cu_data ***datap = datum;
2774
2775   **datap = &sigt->per_cu;
2776   ++*datap;
2777
2778   return 1;
2779 }
2780
2781 /* Create the hash table of all entries in the .debug_types section.
2782    The result is zero if there is an error (e.g. missing .debug_types section),
2783    otherwise non-zero.  */
2784
2785 static int
2786 create_debug_types_hash_table (struct objfile *objfile)
2787 {
2788   gdb_byte *info_ptr;
2789   htab_t types_htab;
2790   struct dwarf2_per_cu_data **iter;
2791
2792   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2793   info_ptr = dwarf2_per_objfile->types.buffer;
2794
2795   if (info_ptr == NULL)
2796     {
2797       dwarf2_per_objfile->signatured_types = NULL;
2798       return 0;
2799     }
2800
2801   types_htab = allocate_signatured_type_hash_table (objfile);
2802
2803   if (dwarf2_die_debug)
2804     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2805
2806   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2807     {
2808       unsigned int offset;
2809       unsigned int offset_size;
2810       unsigned int type_offset;
2811       unsigned int length, initial_length_size;
2812       unsigned short version;
2813       ULONGEST signature;
2814       struct signatured_type *type_sig;
2815       void **slot;
2816       gdb_byte *ptr = info_ptr;
2817
2818       offset = ptr - dwarf2_per_objfile->types.buffer;
2819
2820       /* We need to read the type's signature in order to build the hash
2821          table, but we don't need to read anything else just yet.  */
2822
2823       /* Sanity check to ensure entire cu is present.  */
2824       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2825       if (ptr + length + initial_length_size
2826           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2827         {
2828           complaint (&symfile_complaints,
2829                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2830           break;
2831         }
2832
2833       offset_size = initial_length_size == 4 ? 4 : 8;
2834       ptr += initial_length_size;
2835       version = bfd_get_16 (objfile->obfd, ptr);
2836       ptr += 2;
2837       ptr += offset_size; /* abbrev offset */
2838       ptr += 1; /* address size */
2839       signature = bfd_get_64 (objfile->obfd, ptr);
2840       ptr += 8;
2841       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2842
2843       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2844       memset (type_sig, 0, sizeof (*type_sig));
2845       type_sig->signature = signature;
2846       type_sig->offset = offset;
2847       type_sig->type_offset = type_offset;
2848       type_sig->per_cu.objfile = objfile;
2849       type_sig->per_cu.from_debug_types = 1;
2850
2851       slot = htab_find_slot (types_htab, type_sig, INSERT);
2852       gdb_assert (slot != NULL);
2853       *slot = type_sig;
2854
2855       if (dwarf2_die_debug)
2856         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2857                             offset, phex (signature, sizeof (signature)));
2858
2859       info_ptr = info_ptr + initial_length_size + length;
2860     }
2861
2862   dwarf2_per_objfile->signatured_types = types_htab;
2863
2864   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2865   dwarf2_per_objfile->type_comp_units
2866     = obstack_alloc (&objfile->objfile_obstack,
2867                      dwarf2_per_objfile->n_type_comp_units
2868                      * sizeof (struct dwarf2_per_cu_data *));
2869   iter = &dwarf2_per_objfile->type_comp_units[0];
2870   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2871   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2872               == dwarf2_per_objfile->n_type_comp_units);
2873
2874   return 1;
2875 }
2876
2877 /* Lookup a signature based type.
2878    Returns NULL if SIG is not present in the table.  */
2879
2880 static struct signatured_type *
2881 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2882 {
2883   struct signatured_type find_entry, *entry;
2884
2885   if (dwarf2_per_objfile->signatured_types == NULL)
2886     {
2887       complaint (&symfile_complaints,
2888                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2889       return 0;
2890     }
2891
2892   find_entry.signature = sig;
2893   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2894   return entry;
2895 }
2896
2897 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2898
2899 static void
2900 init_cu_die_reader (struct die_reader_specs *reader,
2901                     struct dwarf2_cu *cu)
2902 {
2903   reader->abfd = cu->objfile->obfd;
2904   reader->cu = cu;
2905   if (cu->per_cu->from_debug_types)
2906     {
2907       gdb_assert (dwarf2_per_objfile->types.readin);
2908       reader->buffer = dwarf2_per_objfile->types.buffer;
2909     }
2910   else
2911     {
2912       gdb_assert (dwarf2_per_objfile->info.readin);
2913       reader->buffer = dwarf2_per_objfile->info.buffer;
2914     }
2915 }
2916
2917 /* Find the base address of the compilation unit for range lists and
2918    location lists.  It will normally be specified by DW_AT_low_pc.
2919    In DWARF-3 draft 4, the base address could be overridden by
2920    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2921    compilation units with discontinuous ranges.  */
2922
2923 static void
2924 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2925 {
2926   struct attribute *attr;
2927
2928   cu->base_known = 0;
2929   cu->base_address = 0;
2930
2931   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2932   if (attr)
2933     {
2934       cu->base_address = DW_ADDR (attr);
2935       cu->base_known = 1;
2936     }
2937   else
2938     {
2939       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2940       if (attr)
2941         {
2942           cu->base_address = DW_ADDR (attr);
2943           cu->base_known = 1;
2944         }
2945     }
2946 }
2947
2948 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2949    to combine the common parts.
2950    Process a compilation unit for a psymtab.
2951    BUFFER is a pointer to the beginning of the dwarf section buffer,
2952    either .debug_info or debug_types.
2953    INFO_PTR is a pointer to the start of the CU.
2954    Returns a pointer to the next CU.  */
2955
2956 static gdb_byte *
2957 process_psymtab_comp_unit (struct objfile *objfile,
2958                            struct dwarf2_per_cu_data *this_cu,
2959                            gdb_byte *buffer, gdb_byte *info_ptr,
2960                            unsigned int buffer_size)
2961 {
2962   bfd *abfd = objfile->obfd;
2963   gdb_byte *beg_of_comp_unit = info_ptr;
2964   struct die_info *comp_unit_die;
2965   struct partial_symtab *pst;
2966   CORE_ADDR baseaddr;
2967   struct cleanup *back_to_inner;
2968   struct dwarf2_cu cu;
2969   int has_children, has_pc_info;
2970   struct attribute *attr;
2971   CORE_ADDR best_lowpc = 0, best_highpc = 0;
2972   struct die_reader_specs reader_specs;
2973
2974   memset (&cu, 0, sizeof (cu));
2975   cu.objfile = objfile;
2976   obstack_init (&cu.comp_unit_obstack);
2977
2978   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
2979
2980   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2981                                           buffer, buffer_size,
2982                                           abfd);
2983
2984   /* Complete the cu_header.  */
2985   cu.header.offset = beg_of_comp_unit - buffer;
2986   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2987
2988   cu.list_in_scope = &file_symbols;
2989
2990   /* If this compilation unit was already read in, free the
2991      cached copy in order to read it in again.  This is
2992      necessary because we skipped some symbols when we first
2993      read in the compilation unit (see load_partial_dies).
2994      This problem could be avoided, but the benefit is
2995      unclear.  */
2996   if (this_cu->cu != NULL)
2997     free_one_cached_comp_unit (this_cu->cu);
2998
2999   /* Note that this is a pointer to our stack frame, being
3000      added to a global data structure.  It will be cleaned up
3001      in free_stack_comp_unit when we finish with this
3002      compilation unit.  */
3003   this_cu->cu = &cu;
3004   cu.per_cu = this_cu;
3005
3006   /* Read the abbrevs for this compilation unit into a table.  */
3007   dwarf2_read_abbrevs (abfd, &cu);
3008   make_cleanup (dwarf2_free_abbrev_table, &cu);
3009
3010   /* Read the compilation unit die.  */
3011   if (this_cu->from_debug_types)
3012     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3013   init_cu_die_reader (&reader_specs, &cu);
3014   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3015                             &has_children);
3016
3017   if (this_cu->from_debug_types)
3018     {
3019       /* offset,length haven't been set yet for type units.  */
3020       this_cu->offset = cu.header.offset;
3021       this_cu->length = cu.header.length + cu.header.initial_length_size;
3022     }
3023   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3024     {
3025       info_ptr = (beg_of_comp_unit + cu.header.length
3026                   + cu.header.initial_length_size);
3027       do_cleanups (back_to_inner);
3028       return info_ptr;
3029     }
3030
3031   /* Set the language we're debugging.  */
3032   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3033   if (attr)
3034     set_cu_language (DW_UNSND (attr), &cu);
3035   else
3036     set_cu_language (language_minimal, &cu);
3037
3038   /* Allocate a new partial symbol table structure.  */
3039   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3040   pst = start_psymtab_common (objfile, objfile->section_offsets,
3041                               (attr != NULL) ? DW_STRING (attr) : "",
3042                               /* TEXTLOW and TEXTHIGH are set below.  */
3043                               0,
3044                               objfile->global_psymbols.next,
3045                               objfile->static_psymbols.next);
3046
3047   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3048   if (attr != NULL)
3049     pst->dirname = DW_STRING (attr);
3050
3051   pst->read_symtab_private = this_cu;
3052
3053   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3054
3055   /* Store the function that reads in the rest of the symbol table */
3056   pst->read_symtab = dwarf2_psymtab_to_symtab;
3057
3058   this_cu->v.psymtab = pst;
3059
3060   dwarf2_find_base_address (comp_unit_die, &cu);
3061
3062   /* Possibly set the default values of LOWPC and HIGHPC from
3063      `DW_AT_ranges'.  */
3064   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3065                                       &best_highpc, &cu, pst);
3066   if (has_pc_info == 1 && best_lowpc < best_highpc)
3067     /* Store the contiguous range if it is not empty; it can be empty for
3068        CUs with no code.  */
3069     addrmap_set_empty (objfile->psymtabs_addrmap,
3070                        best_lowpc + baseaddr,
3071                        best_highpc + baseaddr - 1, pst);
3072
3073   /* Check if comp unit has_children.
3074      If so, read the rest of the partial symbols from this comp unit.
3075      If not, there's no more debug_info for this comp unit. */
3076   if (has_children)
3077     {
3078       struct partial_die_info *first_die;
3079       CORE_ADDR lowpc, highpc;
3080
3081       lowpc = ((CORE_ADDR) -1);
3082       highpc = ((CORE_ADDR) 0);
3083
3084       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3085
3086       scan_partial_symbols (first_die, &lowpc, &highpc,
3087                             ! has_pc_info, &cu);
3088
3089       /* If we didn't find a lowpc, set it to highpc to avoid
3090          complaints from `maint check'.  */
3091       if (lowpc == ((CORE_ADDR) -1))
3092         lowpc = highpc;
3093
3094       /* If the compilation unit didn't have an explicit address range,
3095          then use the information extracted from its child dies.  */
3096       if (! has_pc_info)
3097         {
3098           best_lowpc = lowpc;
3099           best_highpc = highpc;
3100         }
3101     }
3102   pst->textlow = best_lowpc + baseaddr;
3103   pst->texthigh = best_highpc + baseaddr;
3104
3105   pst->n_global_syms = objfile->global_psymbols.next -
3106     (objfile->global_psymbols.list + pst->globals_offset);
3107   pst->n_static_syms = objfile->static_psymbols.next -
3108     (objfile->static_psymbols.list + pst->statics_offset);
3109   sort_pst_symbols (pst);
3110
3111   info_ptr = (beg_of_comp_unit + cu.header.length
3112               + cu.header.initial_length_size);
3113
3114   if (this_cu->from_debug_types)
3115     {
3116       /* It's not clear we want to do anything with stmt lists here.
3117          Waiting to see what gcc ultimately does.  */
3118     }
3119   else
3120     {
3121       /* Get the list of files included in the current compilation unit,
3122          and build a psymtab for each of them.  */
3123       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3124     }
3125
3126   do_cleanups (back_to_inner);
3127
3128   return info_ptr;
3129 }
3130
3131 /* Traversal function for htab_traverse_noresize.
3132    Process one .debug_types comp-unit.  */
3133
3134 static int
3135 process_type_comp_unit (void **slot, void *info)
3136 {
3137   struct signatured_type *entry = (struct signatured_type *) *slot;
3138   struct objfile *objfile = (struct objfile *) info;
3139   struct dwarf2_per_cu_data *this_cu;
3140
3141   this_cu = &entry->per_cu;
3142
3143   gdb_assert (dwarf2_per_objfile->types.readin);
3144   process_psymtab_comp_unit (objfile, this_cu,
3145                              dwarf2_per_objfile->types.buffer,
3146                              dwarf2_per_objfile->types.buffer + entry->offset,
3147                              dwarf2_per_objfile->types.size);
3148
3149   return 1;
3150 }
3151
3152 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3153    Build partial symbol tables for the .debug_types comp-units.  */
3154
3155 static void
3156 build_type_psymtabs (struct objfile *objfile)
3157 {
3158   if (! create_debug_types_hash_table (objfile))
3159     return;
3160
3161   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3162                           process_type_comp_unit, objfile);
3163 }
3164
3165 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3166
3167 static void
3168 psymtabs_addrmap_cleanup (void *o)
3169 {
3170   struct objfile *objfile = o;
3171
3172   objfile->psymtabs_addrmap = NULL;
3173 }
3174
3175 /* Build the partial symbol table by doing a quick pass through the
3176    .debug_info and .debug_abbrev sections.  */
3177
3178 static void
3179 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3180 {
3181   gdb_byte *info_ptr;
3182   struct cleanup *back_to, *addrmap_cleanup;
3183   struct obstack temp_obstack;
3184
3185   dwarf2_per_objfile->reading_partial_symbols = 1;
3186
3187   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3188   info_ptr = dwarf2_per_objfile->info.buffer;
3189
3190   /* Any cached compilation units will be linked by the per-objfile
3191      read_in_chain.  Make sure to free them when we're done.  */
3192   back_to = make_cleanup (free_cached_comp_units, NULL);
3193
3194   build_type_psymtabs (objfile);
3195
3196   create_all_comp_units (objfile);
3197
3198   /* Create a temporary address map on a temporary obstack.  We later
3199      copy this to the final obstack.  */
3200   obstack_init (&temp_obstack);
3201   make_cleanup_obstack_free (&temp_obstack);
3202   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3203   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3204
3205   /* Since the objects we're extracting from .debug_info vary in
3206      length, only the individual functions to extract them (like
3207      read_comp_unit_head and load_partial_die) can really know whether
3208      the buffer is large enough to hold another complete object.
3209
3210      At the moment, they don't actually check that.  If .debug_info
3211      holds just one extra byte after the last compilation unit's dies,
3212      then read_comp_unit_head will happily read off the end of the
3213      buffer.  read_partial_die is similarly casual.  Those functions
3214      should be fixed.
3215
3216      For this loop condition, simply checking whether there's any data
3217      left at all should be sufficient.  */
3218
3219   while (info_ptr < (dwarf2_per_objfile->info.buffer
3220                      + dwarf2_per_objfile->info.size))
3221     {
3222       struct dwarf2_per_cu_data *this_cu;
3223
3224       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3225                                        objfile);
3226
3227       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3228                                             dwarf2_per_objfile->info.buffer,
3229                                             info_ptr,
3230                                             dwarf2_per_objfile->info.size);
3231     }
3232
3233   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3234                                                     &objfile->objfile_obstack);
3235   discard_cleanups (addrmap_cleanup);
3236
3237   do_cleanups (back_to);
3238 }
3239
3240 /* Load the partial DIEs for a secondary CU into memory.  */
3241
3242 static void
3243 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3244                         struct objfile *objfile)
3245 {
3246   bfd *abfd = objfile->obfd;
3247   gdb_byte *info_ptr, *beg_of_comp_unit;
3248   struct die_info *comp_unit_die;
3249   struct dwarf2_cu *cu;
3250   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3251   struct attribute *attr;
3252   int has_children;
3253   struct die_reader_specs reader_specs;
3254   int read_cu = 0;
3255
3256   gdb_assert (! this_cu->from_debug_types);
3257
3258   gdb_assert (dwarf2_per_objfile->info.readin);
3259   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3260   beg_of_comp_unit = info_ptr;
3261
3262   if (this_cu->cu == NULL)
3263     {
3264       cu = alloc_one_comp_unit (objfile);
3265
3266       read_cu = 1;
3267
3268       /* If an error occurs while loading, release our storage.  */
3269       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3270
3271       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3272                                               dwarf2_per_objfile->info.buffer,
3273                                               dwarf2_per_objfile->info.size,
3274                                               abfd);
3275
3276       /* Complete the cu_header.  */
3277       cu->header.offset = this_cu->offset;
3278       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3279
3280       /* Link this compilation unit into the compilation unit tree.  */
3281       this_cu->cu = cu;
3282       cu->per_cu = this_cu;
3283       cu->type_hash = this_cu->type_hash;
3284
3285       /* Link this CU into read_in_chain.  */
3286       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3287       dwarf2_per_objfile->read_in_chain = this_cu;
3288     }
3289   else
3290     {
3291       cu = this_cu->cu;
3292       info_ptr += cu->header.first_die_offset;
3293     }
3294
3295   /* Read the abbrevs for this compilation unit into a table.  */
3296   gdb_assert (cu->dwarf2_abbrevs == NULL);
3297   dwarf2_read_abbrevs (abfd, cu);
3298   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3299
3300   /* Read the compilation unit die.  */
3301   init_cu_die_reader (&reader_specs, cu);
3302   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3303                             &has_children);
3304
3305   /* Set the language we're debugging.  */
3306   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3307   if (attr)
3308     set_cu_language (DW_UNSND (attr), cu);
3309   else
3310     set_cu_language (language_minimal, cu);
3311
3312   /* Check if comp unit has_children.
3313      If so, read the rest of the partial symbols from this comp unit.
3314      If not, there's no more debug_info for this comp unit. */
3315   if (has_children)
3316     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3317
3318   do_cleanups (free_abbrevs_cleanup);
3319
3320   if (read_cu)
3321     {
3322       /* We've successfully allocated this compilation unit.  Let our
3323          caller clean it up when finished with it.  */
3324       discard_cleanups (free_cu_cleanup);
3325     }
3326 }
3327
3328 /* Create a list of all compilation units in OBJFILE.  We do this only
3329    if an inter-comp-unit reference is found; presumably if there is one,
3330    there will be many, and one will occur early in the .debug_info section.
3331    So there's no point in building this list incrementally.  */
3332
3333 static void
3334 create_all_comp_units (struct objfile *objfile)
3335 {
3336   int n_allocated;
3337   int n_comp_units;
3338   struct dwarf2_per_cu_data **all_comp_units;
3339   gdb_byte *info_ptr;
3340
3341   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3342   info_ptr = dwarf2_per_objfile->info.buffer;
3343
3344   n_comp_units = 0;
3345   n_allocated = 10;
3346   all_comp_units = xmalloc (n_allocated
3347                             * sizeof (struct dwarf2_per_cu_data *));
3348
3349   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3350     {
3351       unsigned int length, initial_length_size;
3352       struct dwarf2_per_cu_data *this_cu;
3353       unsigned int offset;
3354
3355       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3356
3357       /* Read just enough information to find out where the next
3358          compilation unit is.  */
3359       length = read_initial_length (objfile->obfd, info_ptr,
3360                                     &initial_length_size);
3361
3362       /* Save the compilation unit for later lookup.  */
3363       this_cu = obstack_alloc (&objfile->objfile_obstack,
3364                                sizeof (struct dwarf2_per_cu_data));
3365       memset (this_cu, 0, sizeof (*this_cu));
3366       this_cu->offset = offset;
3367       this_cu->length = length + initial_length_size;
3368       this_cu->objfile = objfile;
3369
3370       if (n_comp_units == n_allocated)
3371         {
3372           n_allocated *= 2;
3373           all_comp_units = xrealloc (all_comp_units,
3374                                      n_allocated
3375                                      * sizeof (struct dwarf2_per_cu_data *));
3376         }
3377       all_comp_units[n_comp_units++] = this_cu;
3378
3379       info_ptr = info_ptr + this_cu->length;
3380     }
3381
3382   dwarf2_per_objfile->all_comp_units
3383     = obstack_alloc (&objfile->objfile_obstack,
3384                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3385   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3386           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3387   xfree (all_comp_units);
3388   dwarf2_per_objfile->n_comp_units = n_comp_units;
3389 }
3390
3391 /* Process all loaded DIEs for compilation unit CU, starting at
3392    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3393    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3394    DW_AT_ranges).  If NEED_PC is set, then this function will set
3395    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3396    and record the covered ranges in the addrmap.  */
3397
3398 static void
3399 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3400                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3401 {
3402   struct partial_die_info *pdi;
3403
3404   /* Now, march along the PDI's, descending into ones which have
3405      interesting children but skipping the children of the other ones,
3406      until we reach the end of the compilation unit.  */
3407
3408   pdi = first_die;
3409
3410   while (pdi != NULL)
3411     {
3412       fixup_partial_die (pdi, cu);
3413
3414       /* Anonymous namespaces or modules have no name but have interesting
3415          children, so we need to look at them.  Ditto for anonymous
3416          enums.  */
3417
3418       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3419           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3420         {
3421           switch (pdi->tag)
3422             {
3423             case DW_TAG_subprogram:
3424               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3425               break;
3426             case DW_TAG_variable:
3427             case DW_TAG_typedef:
3428             case DW_TAG_union_type:
3429               if (!pdi->is_declaration)
3430                 {
3431                   add_partial_symbol (pdi, cu);
3432                 }
3433               break;
3434             case DW_TAG_class_type:
3435             case DW_TAG_interface_type:
3436             case DW_TAG_structure_type:
3437               if (!pdi->is_declaration)
3438                 {
3439                   add_partial_symbol (pdi, cu);
3440                 }
3441               break;
3442             case DW_TAG_enumeration_type:
3443               if (!pdi->is_declaration)
3444                 add_partial_enumeration (pdi, cu);
3445               break;
3446             case DW_TAG_base_type:
3447             case DW_TAG_subrange_type:
3448               /* File scope base type definitions are added to the partial
3449                  symbol table.  */
3450               add_partial_symbol (pdi, cu);
3451               break;
3452             case DW_TAG_namespace:
3453               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3454               break;
3455             case DW_TAG_module:
3456               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3457               break;
3458             default:
3459               break;
3460             }
3461         }
3462
3463       /* If the die has a sibling, skip to the sibling.  */
3464
3465       pdi = pdi->die_sibling;
3466     }
3467 }
3468
3469 /* Functions used to compute the fully scoped name of a partial DIE.
3470
3471    Normally, this is simple.  For C++, the parent DIE's fully scoped
3472    name is concatenated with "::" and the partial DIE's name.  For
3473    Java, the same thing occurs except that "." is used instead of "::".
3474    Enumerators are an exception; they use the scope of their parent
3475    enumeration type, i.e. the name of the enumeration type is not
3476    prepended to the enumerator.
3477
3478    There are two complexities.  One is DW_AT_specification; in this
3479    case "parent" means the parent of the target of the specification,
3480    instead of the direct parent of the DIE.  The other is compilers
3481    which do not emit DW_TAG_namespace; in this case we try to guess
3482    the fully qualified name of structure types from their members'
3483    linkage names.  This must be done using the DIE's children rather
3484    than the children of any DW_AT_specification target.  We only need
3485    to do this for structures at the top level, i.e. if the target of
3486    any DW_AT_specification (if any; otherwise the DIE itself) does not
3487    have a parent.  */
3488
3489 /* Compute the scope prefix associated with PDI's parent, in
3490    compilation unit CU.  The result will be allocated on CU's
3491    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3492    field.  NULL is returned if no prefix is necessary.  */
3493 static char *
3494 partial_die_parent_scope (struct partial_die_info *pdi,
3495                           struct dwarf2_cu *cu)
3496 {
3497   char *grandparent_scope;
3498   struct partial_die_info *parent, *real_pdi;
3499
3500   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3501      then this means the parent of the specification DIE.  */
3502
3503   real_pdi = pdi;
3504   while (real_pdi->has_specification)
3505     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3506
3507   parent = real_pdi->die_parent;
3508   if (parent == NULL)
3509     return NULL;
3510
3511   if (parent->scope_set)
3512     return parent->scope;
3513
3514   fixup_partial_die (parent, cu);
3515
3516   grandparent_scope = partial_die_parent_scope (parent, cu);
3517
3518   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3519      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3520      Work around this problem here.  */
3521   if (cu->language == language_cplus
3522       && parent->tag == DW_TAG_namespace
3523       && strcmp (parent->name, "::") == 0
3524       && grandparent_scope == NULL)
3525     {
3526       parent->scope = NULL;
3527       parent->scope_set = 1;
3528       return NULL;
3529     }
3530
3531   if (parent->tag == DW_TAG_namespace
3532       || parent->tag == DW_TAG_module
3533       || parent->tag == DW_TAG_structure_type
3534       || parent->tag == DW_TAG_class_type
3535       || parent->tag == DW_TAG_interface_type
3536       || parent->tag == DW_TAG_union_type
3537       || parent->tag == DW_TAG_enumeration_type)
3538     {
3539       if (grandparent_scope == NULL)
3540         parent->scope = parent->name;
3541       else
3542         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3543                                          parent->name, 0, cu);
3544     }
3545   else if (parent->tag == DW_TAG_enumerator)
3546     /* Enumerators should not get the name of the enumeration as a prefix.  */
3547     parent->scope = grandparent_scope;
3548   else
3549     {
3550       /* FIXME drow/2004-04-01: What should we be doing with
3551          function-local names?  For partial symbols, we should probably be
3552          ignoring them.  */
3553       complaint (&symfile_complaints,
3554                  _("unhandled containing DIE tag %d for DIE at %d"),
3555                  parent->tag, pdi->offset);
3556       parent->scope = grandparent_scope;
3557     }
3558
3559   parent->scope_set = 1;
3560   return parent->scope;
3561 }
3562
3563 /* Return the fully scoped name associated with PDI, from compilation unit
3564    CU.  The result will be allocated with malloc.  */
3565 static char *
3566 partial_die_full_name (struct partial_die_info *pdi,
3567                        struct dwarf2_cu *cu)
3568 {
3569   char *parent_scope;
3570
3571   /* If this is a template instantiation, we can not work out the
3572      template arguments from partial DIEs.  So, unfortunately, we have
3573      to go through the full DIEs.  At least any work we do building
3574      types here will be reused if full symbols are loaded later.  */
3575   if (pdi->has_template_arguments)
3576     {
3577       fixup_partial_die (pdi, cu);
3578
3579       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3580         {
3581           struct die_info *die;
3582           struct attribute attr;
3583           struct dwarf2_cu *ref_cu = cu;
3584
3585           attr.name = 0;
3586           attr.form = DW_FORM_ref_addr;
3587           attr.u.addr = pdi->offset;
3588           die = follow_die_ref (NULL, &attr, &ref_cu);
3589
3590           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3591         }
3592     }
3593
3594   parent_scope = partial_die_parent_scope (pdi, cu);
3595   if (parent_scope == NULL)
3596     return NULL;
3597   else
3598     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3599 }
3600
3601 static void
3602 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3603 {
3604   struct objfile *objfile = cu->objfile;
3605   CORE_ADDR addr = 0;
3606   char *actual_name = NULL;
3607   const struct partial_symbol *psym = NULL;
3608   CORE_ADDR baseaddr;
3609   int built_actual_name = 0;
3610
3611   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3612
3613   actual_name = partial_die_full_name (pdi, cu);
3614   if (actual_name)
3615     built_actual_name = 1;
3616
3617   if (actual_name == NULL)
3618     actual_name = pdi->name;
3619
3620   switch (pdi->tag)
3621     {
3622     case DW_TAG_subprogram:
3623       if (pdi->is_external || cu->language == language_ada)
3624         {
3625           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3626              of the global scope.  But in Ada, we want to be able to access
3627              nested procedures globally.  So all Ada subprograms are stored
3628              in the global scope.  */
3629           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3630              mst_text, objfile); */
3631           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3632                                       built_actual_name,
3633                                       VAR_DOMAIN, LOC_BLOCK,
3634                                       &objfile->global_psymbols,
3635                                       0, pdi->lowpc + baseaddr,
3636                                       cu->language, objfile);
3637         }
3638       else
3639         {
3640           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3641              mst_file_text, objfile); */
3642           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3643                                       built_actual_name,
3644                                       VAR_DOMAIN, LOC_BLOCK,
3645                                       &objfile->static_psymbols,
3646                                       0, pdi->lowpc + baseaddr,
3647                                       cu->language, objfile);
3648         }
3649       break;
3650     case DW_TAG_variable:
3651       if (pdi->locdesc)
3652         addr = decode_locdesc (pdi->locdesc, cu);
3653
3654       if (pdi->locdesc
3655           && addr == 0
3656           && !dwarf2_per_objfile->has_section_at_zero)
3657         {
3658           /* A global or static variable may also have been stripped
3659              out by the linker if unused, in which case its address
3660              will be nullified; do not add such variables into partial
3661              symbol table then.  */
3662         }
3663       else if (pdi->is_external)
3664         {
3665           /* Global Variable.
3666              Don't enter into the minimal symbol tables as there is
3667              a minimal symbol table entry from the ELF symbols already.
3668              Enter into partial symbol table if it has a location
3669              descriptor or a type.
3670              If the location descriptor is missing, new_symbol will create
3671              a LOC_UNRESOLVED symbol, the address of the variable will then
3672              be determined from the minimal symbol table whenever the variable
3673              is referenced.
3674              The address for the partial symbol table entry is not
3675              used by GDB, but it comes in handy for debugging partial symbol
3676              table building.  */
3677
3678           if (pdi->locdesc || pdi->has_type)
3679             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3680                                         built_actual_name,
3681                                         VAR_DOMAIN, LOC_STATIC,
3682                                         &objfile->global_psymbols,
3683                                         0, addr + baseaddr,
3684                                         cu->language, objfile);
3685         }
3686       else
3687         {
3688           /* Static Variable. Skip symbols without location descriptors.  */
3689           if (pdi->locdesc == NULL)
3690             {
3691               if (built_actual_name)
3692                 xfree (actual_name);
3693               return;
3694             }
3695           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3696              mst_file_data, objfile); */
3697           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3698                                       built_actual_name,
3699                                       VAR_DOMAIN, LOC_STATIC,
3700                                       &objfile->static_psymbols,
3701                                       0, addr + baseaddr,
3702                                       cu->language, objfile);
3703         }
3704       break;
3705     case DW_TAG_typedef:
3706     case DW_TAG_base_type:
3707     case DW_TAG_subrange_type:
3708       add_psymbol_to_list (actual_name, strlen (actual_name),
3709                            built_actual_name,
3710                            VAR_DOMAIN, LOC_TYPEDEF,
3711                            &objfile->static_psymbols,
3712                            0, (CORE_ADDR) 0, cu->language, objfile);
3713       break;
3714     case DW_TAG_namespace:
3715       add_psymbol_to_list (actual_name, strlen (actual_name),
3716                            built_actual_name,
3717                            VAR_DOMAIN, LOC_TYPEDEF,
3718                            &objfile->global_psymbols,
3719                            0, (CORE_ADDR) 0, cu->language, objfile);
3720       break;
3721     case DW_TAG_class_type:
3722     case DW_TAG_interface_type:
3723     case DW_TAG_structure_type:
3724     case DW_TAG_union_type:
3725     case DW_TAG_enumeration_type:
3726       /* Skip external references.  The DWARF standard says in the section
3727          about "Structure, Union, and Class Type Entries": "An incomplete
3728          structure, union or class type is represented by a structure,
3729          union or class entry that does not have a byte size attribute
3730          and that has a DW_AT_declaration attribute."  */
3731       if (!pdi->has_byte_size && pdi->is_declaration)
3732         {
3733           if (built_actual_name)
3734             xfree (actual_name);
3735           return;
3736         }
3737
3738       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3739          static vs. global.  */
3740       add_psymbol_to_list (actual_name, strlen (actual_name),
3741                            built_actual_name,
3742                            STRUCT_DOMAIN, LOC_TYPEDEF,
3743                            (cu->language == language_cplus
3744                             || cu->language == language_java)
3745                            ? &objfile->global_psymbols
3746                            : &objfile->static_psymbols,
3747                            0, (CORE_ADDR) 0, cu->language, objfile);
3748
3749       break;
3750     case DW_TAG_enumerator:
3751       add_psymbol_to_list (actual_name, strlen (actual_name),
3752                            built_actual_name,
3753                            VAR_DOMAIN, LOC_CONST,
3754                            (cu->language == language_cplus
3755                             || cu->language == language_java)
3756                            ? &objfile->global_psymbols
3757                            : &objfile->static_psymbols,
3758                            0, (CORE_ADDR) 0, cu->language, objfile);
3759       break;
3760     default:
3761       break;
3762     }
3763
3764   if (built_actual_name)
3765     xfree (actual_name);
3766 }
3767
3768 /* Read a partial die corresponding to a namespace; also, add a symbol
3769    corresponding to that namespace to the symbol table.  NAMESPACE is
3770    the name of the enclosing namespace.  */
3771
3772 static void
3773 add_partial_namespace (struct partial_die_info *pdi,
3774                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3775                        int need_pc, struct dwarf2_cu *cu)
3776 {
3777   /* Add a symbol for the namespace.  */
3778
3779   add_partial_symbol (pdi, cu);
3780
3781   /* Now scan partial symbols in that namespace.  */
3782
3783   if (pdi->has_children)
3784     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3785 }
3786
3787 /* Read a partial die corresponding to a Fortran module.  */
3788
3789 static void
3790 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3791                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3792 {
3793   /* Now scan partial symbols in that module.  */
3794
3795   if (pdi->has_children)
3796     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3797 }
3798
3799 /* Read a partial die corresponding to a subprogram and create a partial
3800    symbol for that subprogram.  When the CU language allows it, this
3801    routine also defines a partial symbol for each nested subprogram
3802    that this subprogram contains.
3803
3804    DIE my also be a lexical block, in which case we simply search
3805    recursively for suprograms defined inside that lexical block.
3806    Again, this is only performed when the CU language allows this
3807    type of definitions.  */
3808
3809 static void
3810 add_partial_subprogram (struct partial_die_info *pdi,
3811                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3812                         int need_pc, struct dwarf2_cu *cu)
3813 {
3814   if (pdi->tag == DW_TAG_subprogram)
3815     {
3816       if (pdi->has_pc_info)
3817         {
3818           if (pdi->lowpc < *lowpc)
3819             *lowpc = pdi->lowpc;
3820           if (pdi->highpc > *highpc)
3821             *highpc = pdi->highpc;
3822           if (need_pc)
3823             {
3824               CORE_ADDR baseaddr;
3825               struct objfile *objfile = cu->objfile;
3826
3827               baseaddr = ANOFFSET (objfile->section_offsets,
3828                                    SECT_OFF_TEXT (objfile));
3829               addrmap_set_empty (objfile->psymtabs_addrmap,
3830                                  pdi->lowpc + baseaddr,
3831                                  pdi->highpc - 1 + baseaddr,
3832                                  cu->per_cu->v.psymtab);
3833             }
3834           if (!pdi->is_declaration)
3835             /* Ignore subprogram DIEs that do not have a name, they are
3836                illegal.  Do not emit a complaint at this point, we will
3837                do so when we convert this psymtab into a symtab.  */
3838             if (pdi->name)
3839               add_partial_symbol (pdi, cu);
3840         }
3841     }
3842
3843   if (! pdi->has_children)
3844     return;
3845
3846   if (cu->language == language_ada)
3847     {
3848       pdi = pdi->die_child;
3849       while (pdi != NULL)
3850         {
3851           fixup_partial_die (pdi, cu);
3852           if (pdi->tag == DW_TAG_subprogram
3853               || pdi->tag == DW_TAG_lexical_block)
3854             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3855           pdi = pdi->die_sibling;
3856         }
3857     }
3858 }
3859
3860 /* See if we can figure out if the class lives in a namespace.  We do
3861    this by looking for a member function; its demangled name will
3862    contain namespace info, if there is any.  */
3863
3864 static void
3865 guess_structure_name (struct partial_die_info *struct_pdi,
3866                       struct dwarf2_cu *cu)
3867 {
3868   if ((cu->language == language_cplus
3869        || cu->language == language_java)
3870       && cu->has_namespace_info == 0
3871       && struct_pdi->has_children)
3872     {
3873       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3874          what template types look like, because the demangler
3875          frequently doesn't give the same name as the debug info.  We
3876          could fix this by only using the demangled name to get the
3877          prefix (but see comment in read_structure_type).  */
3878
3879       struct partial_die_info *real_pdi;
3880
3881       /* If this DIE (this DIE's specification, if any) has a parent, then
3882          we should not do this.  We'll prepend the parent's fully qualified
3883          name when we create the partial symbol.  */
3884
3885       real_pdi = struct_pdi;
3886       while (real_pdi->has_specification)
3887         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3888
3889       if (real_pdi->die_parent != NULL)
3890         return;
3891     }
3892 }
3893
3894 /* Read a partial die corresponding to an enumeration type.  */
3895
3896 static void
3897 add_partial_enumeration (struct partial_die_info *enum_pdi,
3898                          struct dwarf2_cu *cu)
3899 {
3900   struct partial_die_info *pdi;
3901
3902   if (enum_pdi->name != NULL)
3903     add_partial_symbol (enum_pdi, cu);
3904
3905   pdi = enum_pdi->die_child;
3906   while (pdi)
3907     {
3908       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3909         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3910       else
3911         add_partial_symbol (pdi, cu);
3912       pdi = pdi->die_sibling;
3913     }
3914 }
3915
3916 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3917    Return the corresponding abbrev, or NULL if the number is zero (indicating
3918    an empty DIE).  In either case *BYTES_READ will be set to the length of
3919    the initial number.  */
3920
3921 static struct abbrev_info *
3922 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3923                  struct dwarf2_cu *cu)
3924 {
3925   bfd *abfd = cu->objfile->obfd;
3926   unsigned int abbrev_number;
3927   struct abbrev_info *abbrev;
3928
3929   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3930
3931   if (abbrev_number == 0)
3932     return NULL;
3933
3934   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3935   if (!abbrev)
3936     {
3937       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3938                       bfd_get_filename (abfd));
3939     }
3940
3941   return abbrev;
3942 }
3943
3944 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3945    Returns a pointer to the end of a series of DIEs, terminated by an empty
3946    DIE.  Any children of the skipped DIEs will also be skipped.  */
3947
3948 static gdb_byte *
3949 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3950 {
3951   struct abbrev_info *abbrev;
3952   unsigned int bytes_read;
3953
3954   while (1)
3955     {
3956       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3957       if (abbrev == NULL)
3958         return info_ptr + bytes_read;
3959       else
3960         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3961     }
3962 }
3963
3964 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3965    INFO_PTR should point just after the initial uleb128 of a DIE, and the
3966    abbrev corresponding to that skipped uleb128 should be passed in
3967    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
3968    children.  */
3969
3970 static gdb_byte *
3971 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3972               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3973 {
3974   unsigned int bytes_read;
3975   struct attribute attr;
3976   bfd *abfd = cu->objfile->obfd;
3977   unsigned int form, i;
3978
3979   for (i = 0; i < abbrev->num_attrs; i++)
3980     {
3981       /* The only abbrev we care about is DW_AT_sibling.  */
3982       if (abbrev->attrs[i].name == DW_AT_sibling)
3983         {
3984           read_attribute (&attr, &abbrev->attrs[i],
3985                           abfd, info_ptr, cu);
3986           if (attr.form == DW_FORM_ref_addr)
3987             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
3988           else
3989             return buffer + dwarf2_get_ref_die_offset (&attr);
3990         }
3991
3992       /* If it isn't DW_AT_sibling, skip this attribute.  */
3993       form = abbrev->attrs[i].form;
3994     skip_attribute:
3995       switch (form)
3996         {
3997         case DW_FORM_ref_addr:
3998           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3999              and later it is offset sized.  */
4000           if (cu->header.version == 2)
4001             info_ptr += cu->header.addr_size;
4002           else
4003             info_ptr += cu->header.offset_size;
4004           break;
4005         case DW_FORM_addr:
4006           info_ptr += cu->header.addr_size;
4007           break;
4008         case DW_FORM_data1:
4009         case DW_FORM_ref1:
4010         case DW_FORM_flag:
4011           info_ptr += 1;
4012           break;
4013         case DW_FORM_flag_present:
4014           break;
4015         case DW_FORM_data2:
4016         case DW_FORM_ref2:
4017           info_ptr += 2;
4018           break;
4019         case DW_FORM_data4:
4020         case DW_FORM_ref4:
4021           info_ptr += 4;
4022           break;
4023         case DW_FORM_data8:
4024         case DW_FORM_ref8:
4025         case DW_FORM_sig8:
4026           info_ptr += 8;
4027           break;
4028         case DW_FORM_string:
4029           read_direct_string (abfd, info_ptr, &bytes_read);
4030           info_ptr += bytes_read;
4031           break;
4032         case DW_FORM_sec_offset:
4033         case DW_FORM_strp:
4034           info_ptr += cu->header.offset_size;
4035           break;
4036         case DW_FORM_exprloc:
4037         case DW_FORM_block:
4038           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4039           info_ptr += bytes_read;
4040           break;
4041         case DW_FORM_block1:
4042           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4043           break;
4044         case DW_FORM_block2:
4045           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4046           break;
4047         case DW_FORM_block4:
4048           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4049           break;
4050         case DW_FORM_sdata:
4051         case DW_FORM_udata:
4052         case DW_FORM_ref_udata:
4053           info_ptr = skip_leb128 (abfd, info_ptr);
4054           break;
4055         case DW_FORM_indirect:
4056           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4057           info_ptr += bytes_read;
4058           /* We need to continue parsing from here, so just go back to
4059              the top.  */
4060           goto skip_attribute;
4061
4062         default:
4063           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4064                  dwarf_form_name (form),
4065                  bfd_get_filename (abfd));
4066         }
4067     }
4068
4069   if (abbrev->has_children)
4070     return skip_children (buffer, info_ptr, cu);
4071   else
4072     return info_ptr;
4073 }
4074
4075 /* Locate ORIG_PDI's sibling.
4076    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4077    in BUFFER.  */
4078
4079 static gdb_byte *
4080 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4081                     gdb_byte *buffer, gdb_byte *info_ptr,
4082                     bfd *abfd, struct dwarf2_cu *cu)
4083 {
4084   /* Do we know the sibling already?  */
4085
4086   if (orig_pdi->sibling)
4087     return orig_pdi->sibling;
4088
4089   /* Are there any children to deal with?  */
4090
4091   if (!orig_pdi->has_children)
4092     return info_ptr;
4093
4094   /* Skip the children the long way.  */
4095
4096   return skip_children (buffer, info_ptr, cu);
4097 }
4098
4099 /* Expand this partial symbol table into a full symbol table.  */
4100
4101 static void
4102 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4103 {
4104   if (pst != NULL)
4105     {
4106       if (pst->readin)
4107         {
4108           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4109         }
4110       else
4111         {
4112           if (info_verbose)
4113             {
4114               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4115               gdb_flush (gdb_stdout);
4116             }
4117
4118           /* Restore our global data.  */
4119           dwarf2_per_objfile = objfile_data (pst->objfile,
4120                                              dwarf2_objfile_data_key);
4121
4122           /* If this psymtab is constructed from a debug-only objfile, the
4123              has_section_at_zero flag will not necessarily be correct.  We
4124              can get the correct value for this flag by looking at the data
4125              associated with the (presumably stripped) associated objfile.  */
4126           if (pst->objfile->separate_debug_objfile_backlink)
4127             {
4128               struct dwarf2_per_objfile *dpo_backlink
4129                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4130                                 dwarf2_objfile_data_key);
4131
4132               dwarf2_per_objfile->has_section_at_zero
4133                 = dpo_backlink->has_section_at_zero;
4134             }
4135
4136           dwarf2_per_objfile->reading_partial_symbols = 0;
4137
4138           psymtab_to_symtab_1 (pst);
4139
4140           /* Finish up the debug error message.  */
4141           if (info_verbose)
4142             printf_filtered (_("done.\n"));
4143         }
4144     }
4145 }
4146
4147 /* Add PER_CU to the queue.  */
4148
4149 static void
4150 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4151 {
4152   struct dwarf2_queue_item *item;
4153
4154   per_cu->queued = 1;
4155   item = xmalloc (sizeof (*item));
4156   item->per_cu = per_cu;
4157   item->next = NULL;
4158
4159   if (dwarf2_queue == NULL)
4160     dwarf2_queue = item;
4161   else
4162     dwarf2_queue_tail->next = item;
4163
4164   dwarf2_queue_tail = item;
4165 }
4166
4167 /* Process the queue.  */
4168
4169 static void
4170 process_queue (struct objfile *objfile)
4171 {
4172   struct dwarf2_queue_item *item, *next_item;
4173
4174   /* The queue starts out with one item, but following a DIE reference
4175      may load a new CU, adding it to the end of the queue.  */
4176   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4177     {
4178       if (dwarf2_per_objfile->using_index
4179           ? !item->per_cu->v.quick->symtab
4180           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4181         process_full_comp_unit (item->per_cu);
4182
4183       item->per_cu->queued = 0;
4184       next_item = item->next;
4185       xfree (item);
4186     }
4187
4188   dwarf2_queue_tail = NULL;
4189 }
4190
4191 /* Free all allocated queue entries.  This function only releases anything if
4192    an error was thrown; if the queue was processed then it would have been
4193    freed as we went along.  */
4194
4195 static void
4196 dwarf2_release_queue (void *dummy)
4197 {
4198   struct dwarf2_queue_item *item, *last;
4199
4200   item = dwarf2_queue;
4201   while (item)
4202     {
4203       /* Anything still marked queued is likely to be in an
4204          inconsistent state, so discard it.  */
4205       if (item->per_cu->queued)
4206         {
4207           if (item->per_cu->cu != NULL)
4208             free_one_cached_comp_unit (item->per_cu->cu);
4209           item->per_cu->queued = 0;
4210         }
4211
4212       last = item;
4213       item = item->next;
4214       xfree (last);
4215     }
4216
4217   dwarf2_queue = dwarf2_queue_tail = NULL;
4218 }
4219
4220 /* Read in full symbols for PST, and anything it depends on.  */
4221
4222 static void
4223 psymtab_to_symtab_1 (struct partial_symtab *pst)
4224 {
4225   struct dwarf2_per_cu_data *per_cu;
4226   struct cleanup *back_to;
4227   int i;
4228
4229   for (i = 0; i < pst->number_of_dependencies; i++)
4230     if (!pst->dependencies[i]->readin)
4231       {
4232         /* Inform about additional files that need to be read in.  */
4233         if (info_verbose)
4234           {
4235             /* FIXME: i18n: Need to make this a single string.  */
4236             fputs_filtered (" ", gdb_stdout);
4237             wrap_here ("");
4238             fputs_filtered ("and ", gdb_stdout);
4239             wrap_here ("");
4240             printf_filtered ("%s...", pst->dependencies[i]->filename);
4241             wrap_here ("");     /* Flush output */
4242             gdb_flush (gdb_stdout);
4243           }
4244         psymtab_to_symtab_1 (pst->dependencies[i]);
4245       }
4246
4247   per_cu = pst->read_symtab_private;
4248
4249   if (per_cu == NULL)
4250     {
4251       /* It's an include file, no symbols to read for it.
4252          Everything is in the parent symtab.  */
4253       pst->readin = 1;
4254       return;
4255     }
4256
4257   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4258 }
4259
4260 /* Load the DIEs associated with PER_CU into memory.  */
4261
4262 static void
4263 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4264 {
4265   bfd *abfd = objfile->obfd;
4266   struct dwarf2_cu *cu;
4267   unsigned int offset;
4268   gdb_byte *info_ptr, *beg_of_comp_unit;
4269   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4270   struct attribute *attr;
4271   int read_cu = 0;
4272
4273   gdb_assert (! per_cu->from_debug_types);
4274
4275   /* Set local variables from the partial symbol table info.  */
4276   offset = per_cu->offset;
4277
4278   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4279   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4280   beg_of_comp_unit = info_ptr;
4281
4282   if (per_cu->cu == NULL)
4283     {
4284       cu = alloc_one_comp_unit (objfile);
4285
4286       read_cu = 1;
4287
4288       /* If an error occurs while loading, release our storage.  */
4289       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4290
4291       /* Read in the comp_unit header.  */
4292       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4293
4294       /* Complete the cu_header.  */
4295       cu->header.offset = offset;
4296       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4297
4298       /* Read the abbrevs for this compilation unit.  */
4299       dwarf2_read_abbrevs (abfd, cu);
4300       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4301
4302       /* Link this compilation unit into the compilation unit tree.  */
4303       per_cu->cu = cu;
4304       cu->per_cu = per_cu;
4305       cu->type_hash = per_cu->type_hash;
4306
4307       /* Link this CU into read_in_chain.  */
4308       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4309       dwarf2_per_objfile->read_in_chain = per_cu;
4310     }
4311   else
4312     {
4313       cu = per_cu->cu;
4314       info_ptr += cu->header.first_die_offset;
4315     }
4316
4317   cu->dies = read_comp_unit (info_ptr, cu);
4318
4319   /* We try not to read any attributes in this function, because not
4320      all objfiles needed for references have been loaded yet, and symbol
4321      table processing isn't initialized.  But we have to set the CU language,
4322      or we won't be able to build types correctly.  */
4323   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4324   if (attr)
4325     set_cu_language (DW_UNSND (attr), cu);
4326   else
4327     set_cu_language (language_minimal, cu);
4328
4329   /* Similarly, if we do not read the producer, we can not apply
4330      producer-specific interpretation.  */
4331   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4332   if (attr)
4333     cu->producer = DW_STRING (attr);
4334
4335   if (read_cu)
4336     {
4337       do_cleanups (free_abbrevs_cleanup);
4338
4339       /* We've successfully allocated this compilation unit.  Let our
4340          caller clean it up when finished with it.  */
4341       discard_cleanups (free_cu_cleanup);
4342     }
4343 }
4344
4345 /* Generate full symbol information for PST and CU, whose DIEs have
4346    already been loaded into memory.  */
4347
4348 static void
4349 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4350 {
4351   struct dwarf2_cu *cu = per_cu->cu;
4352   struct objfile *objfile = per_cu->objfile;
4353   CORE_ADDR lowpc, highpc;
4354   struct symtab *symtab;
4355   struct cleanup *back_to;
4356   CORE_ADDR baseaddr;
4357
4358   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4359
4360   buildsym_init ();
4361   back_to = make_cleanup (really_free_pendings, NULL);
4362
4363   cu->list_in_scope = &file_symbols;
4364
4365   dwarf2_find_base_address (cu->dies, cu);
4366
4367   /* Do line number decoding in read_file_scope () */
4368   process_die (cu->dies, cu);
4369
4370   /* Some compilers don't define a DW_AT_high_pc attribute for the
4371      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4372      it, by scanning the DIE's below the compilation unit.  */
4373   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4374
4375   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4376
4377   /* Set symtab language to language from DW_AT_language.
4378      If the compilation is from a C file generated by language preprocessors,
4379      do not set the language if it was already deduced by start_subfile.  */
4380   if (symtab != NULL
4381       && !(cu->language == language_c && symtab->language != language_c))
4382     {
4383       symtab->language = cu->language;
4384     }
4385
4386   if (dwarf2_per_objfile->using_index)
4387     per_cu->v.quick->symtab = symtab;
4388   else
4389     {
4390       struct partial_symtab *pst = per_cu->v.psymtab;
4391       pst->symtab = symtab;
4392       pst->readin = 1;
4393     }
4394
4395   do_cleanups (back_to);
4396 }
4397
4398 /* Process a die and its children.  */
4399
4400 static void
4401 process_die (struct die_info *die, struct dwarf2_cu *cu)
4402 {
4403   switch (die->tag)
4404     {
4405     case DW_TAG_padding:
4406       break;
4407     case DW_TAG_compile_unit:
4408       read_file_scope (die, cu);
4409       break;
4410     case DW_TAG_type_unit:
4411       read_type_unit_scope (die, cu);
4412       break;
4413     case DW_TAG_subprogram:
4414     case DW_TAG_inlined_subroutine:
4415       read_func_scope (die, cu);
4416       break;
4417     case DW_TAG_lexical_block:
4418     case DW_TAG_try_block:
4419     case DW_TAG_catch_block:
4420       read_lexical_block_scope (die, cu);
4421       break;
4422     case DW_TAG_class_type:
4423     case DW_TAG_interface_type:
4424     case DW_TAG_structure_type:
4425     case DW_TAG_union_type:
4426       process_structure_scope (die, cu);
4427       break;
4428     case DW_TAG_enumeration_type:
4429       process_enumeration_scope (die, cu);
4430       break;
4431
4432     /* These dies have a type, but processing them does not create
4433        a symbol or recurse to process the children.  Therefore we can
4434        read them on-demand through read_type_die.  */
4435     case DW_TAG_subroutine_type:
4436     case DW_TAG_set_type:
4437     case DW_TAG_array_type:
4438     case DW_TAG_pointer_type:
4439     case DW_TAG_ptr_to_member_type:
4440     case DW_TAG_reference_type:
4441     case DW_TAG_string_type:
4442       break;
4443
4444     case DW_TAG_base_type:
4445     case DW_TAG_subrange_type:
4446     case DW_TAG_typedef:
4447       /* Add a typedef symbol for the type definition, if it has a
4448          DW_AT_name.  */
4449       new_symbol (die, read_type_die (die, cu), cu);
4450       break;
4451     case DW_TAG_common_block:
4452       read_common_block (die, cu);
4453       break;
4454     case DW_TAG_common_inclusion:
4455       break;
4456     case DW_TAG_namespace:
4457       processing_has_namespace_info = 1;
4458       read_namespace (die, cu);
4459       break;
4460     case DW_TAG_module:
4461       processing_has_namespace_info = 1;
4462       read_module (die, cu);
4463       break;
4464     case DW_TAG_imported_declaration:
4465     case DW_TAG_imported_module:
4466       processing_has_namespace_info = 1;
4467       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4468                                  || cu->language != language_fortran))
4469         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4470                    dwarf_tag_name (die->tag));
4471       read_import_statement (die, cu);
4472       break;
4473     default:
4474       new_symbol (die, NULL, cu);
4475       break;
4476     }
4477 }
4478
4479 /* A helper function for dwarf2_compute_name which determines whether DIE
4480    needs to have the name of the scope prepended to the name listed in the
4481    die.  */
4482
4483 static int
4484 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4485 {
4486   struct attribute *attr;
4487
4488   switch (die->tag)
4489     {
4490     case DW_TAG_namespace:
4491     case DW_TAG_typedef:
4492     case DW_TAG_class_type:
4493     case DW_TAG_interface_type:
4494     case DW_TAG_structure_type:
4495     case DW_TAG_union_type:
4496     case DW_TAG_enumeration_type:
4497     case DW_TAG_enumerator:
4498     case DW_TAG_subprogram:
4499     case DW_TAG_member:
4500       return 1;
4501
4502     case DW_TAG_variable:
4503       /* We only need to prefix "globally" visible variables.  These include
4504          any variable marked with DW_AT_external or any variable that
4505          lives in a namespace.  [Variables in anonymous namespaces
4506          require prefixing, but they are not DW_AT_external.]  */
4507
4508       if (dwarf2_attr (die, DW_AT_specification, cu))
4509         {
4510           struct dwarf2_cu *spec_cu = cu;
4511
4512           return die_needs_namespace (die_specification (die, &spec_cu),
4513                                       spec_cu);
4514         }
4515
4516       attr = dwarf2_attr (die, DW_AT_external, cu);
4517       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4518           && die->parent->tag != DW_TAG_module)
4519         return 0;
4520       /* A variable in a lexical block of some kind does not need a
4521          namespace, even though in C++ such variables may be external
4522          and have a mangled name.  */
4523       if (die->parent->tag ==  DW_TAG_lexical_block
4524           || die->parent->tag ==  DW_TAG_try_block
4525           || die->parent->tag ==  DW_TAG_catch_block
4526           || die->parent->tag == DW_TAG_subprogram)
4527         return 0;
4528       return 1;
4529
4530     default:
4531       return 0;
4532     }
4533 }
4534
4535 /* Retrieve the last character from a mem_file.  */
4536
4537 static void
4538 do_ui_file_peek_last (void *object, const char *buffer, long length)
4539 {
4540   char *last_char_p = (char *) object;
4541
4542   if (length > 0)
4543     *last_char_p = buffer[length - 1];
4544 }
4545
4546 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4547    compute the physname for the object, which include a method's
4548    formal parameters (C++/Java) and return type (Java).
4549
4550    For Ada, return the DIE's linkage name rather than the fully qualified
4551    name.  PHYSNAME is ignored..
4552
4553    The result is allocated on the objfile_obstack and canonicalized.  */
4554
4555 static const char *
4556 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4557                      int physname)
4558 {
4559   if (name == NULL)
4560     name = dwarf2_name (die, cu);
4561
4562   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4563      compute it by typename_concat inside GDB.  */
4564   if (cu->language == language_ada
4565       || (cu->language == language_fortran && physname))
4566     {
4567       /* For Ada unit, we prefer the linkage name over the name, as
4568          the former contains the exported name, which the user expects
4569          to be able to reference.  Ideally, we want the user to be able
4570          to reference this entity using either natural or linkage name,
4571          but we haven't started looking at this enhancement yet.  */
4572       struct attribute *attr;
4573
4574       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4575       if (attr == NULL)
4576         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4577       if (attr && DW_STRING (attr))
4578         return DW_STRING (attr);
4579     }
4580
4581   /* These are the only languages we know how to qualify names in.  */
4582   if (name != NULL
4583       && (cu->language == language_cplus || cu->language == language_java
4584           || cu->language == language_fortran))
4585     {
4586       if (die_needs_namespace (die, cu))
4587         {
4588           long length;
4589           char *prefix;
4590           struct ui_file *buf;
4591
4592           prefix = determine_prefix (die, cu);
4593           buf = mem_fileopen ();
4594           if (*prefix != '\0')
4595             {
4596               char *prefixed_name = typename_concat (NULL, prefix, name,
4597                                                      physname, cu);
4598
4599               fputs_unfiltered (prefixed_name, buf);
4600               xfree (prefixed_name);
4601             }
4602           else
4603             fputs_unfiltered (name ? name : "", buf);
4604
4605           /* Template parameters may be specified in the DIE's DW_AT_name, or
4606              as children with DW_TAG_template_type_param or
4607              DW_TAG_value_type_param.  If the latter, add them to the name
4608              here.  If the name already has template parameters, then
4609              skip this step; some versions of GCC emit both, and
4610              it is more efficient to use the pre-computed name.
4611
4612              Something to keep in mind about this process: it is very
4613              unlikely, or in some cases downright impossible, to produce
4614              something that will match the mangled name of a function.
4615              If the definition of the function has the same debug info,
4616              we should be able to match up with it anyway.  But fallbacks
4617              using the minimal symbol, for instance to find a method
4618              implemented in a stripped copy of libstdc++, will not work.
4619              If we do not have debug info for the definition, we will have to
4620              match them up some other way.
4621
4622              When we do name matching there is a related problem with function
4623              templates; two instantiated function templates are allowed to
4624              differ only by their return types, which we do not add here.  */
4625
4626           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4627             {
4628               struct attribute *attr;
4629               struct die_info *child;
4630               int first = 1;
4631
4632               die->building_fullname = 1;
4633
4634               for (child = die->child; child != NULL; child = child->sibling)
4635                 {
4636                   struct type *type;
4637                   long value;
4638                   gdb_byte *bytes;
4639                   struct dwarf2_locexpr_baton *baton;
4640                   struct value *v;
4641
4642                   if (child->tag != DW_TAG_template_type_param
4643                       && child->tag != DW_TAG_template_value_param)
4644                     continue;
4645
4646                   if (first)
4647                     {
4648                       fputs_unfiltered ("<", buf);
4649                       first = 0;
4650                     }
4651                   else
4652                     fputs_unfiltered (", ", buf);
4653
4654                   attr = dwarf2_attr (child, DW_AT_type, cu);
4655                   if (attr == NULL)
4656                     {
4657                       complaint (&symfile_complaints,
4658                                  _("template parameter missing DW_AT_type"));
4659                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4660                       continue;
4661                     }
4662                   type = die_type (child, cu);
4663
4664                   if (child->tag == DW_TAG_template_type_param)
4665                     {
4666                       c_print_type (type, "", buf, -1, 0);
4667                       continue;
4668                     }
4669
4670                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4671                   if (attr == NULL)
4672                     {
4673                       complaint (&symfile_complaints,
4674                                  _("template parameter missing DW_AT_const_value"));
4675                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4676                       continue;
4677                     }
4678
4679                   dwarf2_const_value_attr (attr, type, name,
4680                                            &cu->comp_unit_obstack, cu,
4681                                            &value, &bytes, &baton);
4682
4683                   if (TYPE_NOSIGN (type))
4684                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4685                        changed, this can use value_print instead.  */
4686                     c_printchar (value, type, buf);
4687                   else
4688                     {
4689                       struct value_print_options opts;
4690
4691                       if (baton != NULL)
4692                         v = dwarf2_evaluate_loc_desc (type, NULL,
4693                                                       baton->data,
4694                                                       baton->size,
4695                                                       baton->per_cu);
4696                       else if (bytes != NULL)
4697                         {
4698                           v = allocate_value (type);
4699                           memcpy (value_contents_writeable (v), bytes,
4700                                   TYPE_LENGTH (type));
4701                         }
4702                       else
4703                         v = value_from_longest (type, value);
4704
4705                       /* Specify decimal so that we do not depend on the radix.  */
4706                       get_formatted_print_options (&opts, 'd');
4707                       opts.raw = 1;
4708                       value_print (v, buf, &opts);
4709                       release_value (v);
4710                       value_free (v);
4711                     }
4712                 }
4713
4714               die->building_fullname = 0;
4715
4716               if (!first)
4717                 {
4718                   /* Close the argument list, with a space if necessary
4719                      (nested templates).  */
4720                   char last_char = '\0';
4721                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4722                   if (last_char == '>')
4723                     fputs_unfiltered (" >", buf);
4724                   else
4725                     fputs_unfiltered (">", buf);
4726                 }
4727             }
4728
4729           /* For Java and C++ methods, append formal parameter type
4730              information, if PHYSNAME.  */
4731
4732           if (physname && die->tag == DW_TAG_subprogram
4733               && (cu->language == language_cplus
4734                   || cu->language == language_java))
4735             {
4736               struct type *type = read_type_die (die, cu);
4737
4738               c_type_print_args (type, buf, 0, cu->language);
4739
4740               if (cu->language == language_java)
4741                 {
4742                   /* For java, we must append the return type to method
4743                      names. */
4744                   if (die->tag == DW_TAG_subprogram)
4745                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4746                                      0, 0);
4747                 }
4748               else if (cu->language == language_cplus)
4749                 {
4750                   if (TYPE_NFIELDS (type) > 0
4751                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4752                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4753                     fputs_unfiltered (" const", buf);
4754                 }
4755             }
4756
4757           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4758                                        &length);
4759           ui_file_delete (buf);
4760
4761           if (cu->language == language_cplus)
4762             {
4763               char *cname
4764                 = dwarf2_canonicalize_name (name, cu,
4765                                             &cu->objfile->objfile_obstack);
4766
4767               if (cname != NULL)
4768                 name = cname;
4769             }
4770         }
4771     }
4772
4773   return name;
4774 }
4775
4776 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4777    If scope qualifiers are appropriate they will be added.  The result
4778    will be allocated on the objfile_obstack, or NULL if the DIE does
4779    not have a name.  NAME may either be from a previous call to
4780    dwarf2_name or NULL.
4781
4782    The output string will be canonicalized (if C++/Java). */
4783
4784 static const char *
4785 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4786 {
4787   return dwarf2_compute_name (name, die, cu, 0);
4788 }
4789
4790 /* Construct a physname for the given DIE in CU.  NAME may either be
4791    from a previous call to dwarf2_name or NULL.  The result will be
4792    allocated on the objfile_objstack or NULL if the DIE does not have a
4793    name.
4794
4795    The output string will be canonicalized (if C++/Java).  */
4796
4797 static const char *
4798 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4799 {
4800   return dwarf2_compute_name (name, die, cu, 1);
4801 }
4802
4803 /* Read the import statement specified by the given die and record it.  */
4804
4805 static void
4806 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4807 {
4808   struct attribute *import_attr;
4809   struct die_info *imported_die;
4810   struct dwarf2_cu *imported_cu;
4811   const char *imported_name;
4812   const char *imported_name_prefix;
4813   const char *canonical_name;
4814   const char *import_alias;
4815   const char *imported_declaration = NULL;
4816   const char *import_prefix;
4817
4818   char *temp;
4819
4820   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4821   if (import_attr == NULL)
4822     {
4823       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4824                  dwarf_tag_name (die->tag));
4825       return;
4826     }
4827
4828   imported_cu = cu;
4829   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4830   imported_name = dwarf2_name (imported_die, imported_cu);
4831   if (imported_name == NULL)
4832     {
4833       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4834
4835         The import in the following code:
4836         namespace A
4837           {
4838             typedef int B;
4839           }
4840
4841         int main ()
4842           {
4843             using A::B;
4844             B b;
4845             return b;
4846           }
4847
4848         ...
4849          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4850             <52>   DW_AT_decl_file   : 1
4851             <53>   DW_AT_decl_line   : 6
4852             <54>   DW_AT_import      : <0x75>
4853          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4854             <59>   DW_AT_name        : B
4855             <5b>   DW_AT_decl_file   : 1
4856             <5c>   DW_AT_decl_line   : 2
4857             <5d>   DW_AT_type        : <0x6e>
4858         ...
4859          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4860             <76>   DW_AT_byte_size   : 4
4861             <77>   DW_AT_encoding    : 5        (signed)
4862
4863         imports the wrong die ( 0x75 instead of 0x58 ).
4864         This case will be ignored until the gcc bug is fixed.  */
4865       return;
4866     }
4867
4868   /* Figure out the local name after import.  */
4869   import_alias = dwarf2_name (die, cu);
4870
4871   /* Figure out where the statement is being imported to.  */
4872   import_prefix = determine_prefix (die, cu);
4873
4874   /* Figure out what the scope of the imported die is and prepend it
4875      to the name of the imported die.  */
4876   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4877
4878   if (imported_die->tag != DW_TAG_namespace
4879       && imported_die->tag != DW_TAG_module)
4880     {
4881       imported_declaration = imported_name;
4882       canonical_name = imported_name_prefix;
4883     }
4884   else if (strlen (imported_name_prefix) > 0)
4885     {
4886       temp = alloca (strlen (imported_name_prefix)
4887                      + 2 + strlen (imported_name) + 1);
4888       strcpy (temp, imported_name_prefix);
4889       strcat (temp, "::");
4890       strcat (temp, imported_name);
4891       canonical_name = temp;
4892     }
4893   else
4894     canonical_name = imported_name;
4895
4896   cp_add_using_directive (import_prefix,
4897                           canonical_name,
4898                           import_alias,
4899                           imported_declaration,
4900                           &cu->objfile->objfile_obstack);
4901 }
4902
4903 static void
4904 initialize_cu_func_list (struct dwarf2_cu *cu)
4905 {
4906   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4907 }
4908
4909 static void
4910 free_cu_line_header (void *arg)
4911 {
4912   struct dwarf2_cu *cu = arg;
4913
4914   free_line_header (cu->line_header);
4915   cu->line_header = NULL;
4916 }
4917
4918 static void
4919 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4920                          char **name, char **comp_dir)
4921 {
4922   struct attribute *attr;
4923
4924   *name = NULL;
4925   *comp_dir = NULL;
4926
4927   /* Find the filename.  Do not use dwarf2_name here, since the filename
4928      is not a source language identifier.  */
4929   attr = dwarf2_attr (die, DW_AT_name, cu);
4930   if (attr)
4931     {
4932       *name = DW_STRING (attr);
4933     }
4934
4935   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4936   if (attr)
4937     *comp_dir = DW_STRING (attr);
4938   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4939     {
4940       *comp_dir = ldirname (*name);
4941       if (*comp_dir != NULL)
4942         make_cleanup (xfree, *comp_dir);
4943     }
4944   if (*comp_dir != NULL)
4945     {
4946       /* Irix 6.2 native cc prepends <machine>.: to the compilation
4947          directory, get rid of it.  */
4948       char *cp = strchr (*comp_dir, ':');
4949
4950       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4951         *comp_dir = cp + 1;
4952     }
4953
4954   if (*name == NULL)
4955     *name = "<unknown>";
4956 }
4957
4958 static void
4959 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
4960 {
4961   struct objfile *objfile = cu->objfile;
4962   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4963   CORE_ADDR lowpc = ((CORE_ADDR) -1);
4964   CORE_ADDR highpc = ((CORE_ADDR) 0);
4965   struct attribute *attr;
4966   char *name = NULL;
4967   char *comp_dir = NULL;
4968   struct die_info *child_die;
4969   bfd *abfd = objfile->obfd;
4970   struct line_header *line_header = 0;
4971   CORE_ADDR baseaddr;
4972
4973   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4974
4975   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
4976
4977   /* If we didn't find a lowpc, set it to highpc to avoid complaints
4978      from finish_block.  */
4979   if (lowpc == ((CORE_ADDR) -1))
4980     lowpc = highpc;
4981   lowpc += baseaddr;
4982   highpc += baseaddr;
4983
4984   find_file_and_directory (die, cu, &name, &comp_dir);
4985
4986   attr = dwarf2_attr (die, DW_AT_language, cu);
4987   if (attr)
4988     {
4989       set_cu_language (DW_UNSND (attr), cu);
4990     }
4991
4992   attr = dwarf2_attr (die, DW_AT_producer, cu);
4993   if (attr)
4994     cu->producer = DW_STRING (attr);
4995
4996   /* We assume that we're processing GCC output. */
4997   processing_gcc_compilation = 2;
4998
4999   processing_has_namespace_info = 0;
5000
5001   start_symtab (name, comp_dir, lowpc);
5002   record_debugformat ("DWARF 2");
5003   record_producer (cu->producer);
5004
5005   initialize_cu_func_list (cu);
5006
5007   /* Decode line number information if present.  We do this before
5008      processing child DIEs, so that the line header table is available
5009      for DW_AT_decl_file.  */
5010   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5011   if (attr)
5012     {
5013       unsigned int line_offset = DW_UNSND (attr);
5014       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5015       if (line_header)
5016         {
5017           cu->line_header = line_header;
5018           make_cleanup (free_cu_line_header, cu);
5019           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5020         }
5021     }
5022
5023   /* Process all dies in compilation unit.  */
5024   if (die->child != NULL)
5025     {
5026       child_die = die->child;
5027       while (child_die && child_die->tag)
5028         {
5029           process_die (child_die, cu);
5030           child_die = sibling_die (child_die);
5031         }
5032     }
5033
5034   /* Decode macro information, if present.  Dwarf 2 macro information
5035      refers to information in the line number info statement program
5036      header, so we can only read it if we've read the header
5037      successfully.  */
5038   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5039   if (attr && line_header)
5040     {
5041       unsigned int macro_offset = DW_UNSND (attr);
5042
5043       dwarf_decode_macros (line_header, macro_offset,
5044                            comp_dir, abfd, cu);
5045     }
5046   do_cleanups (back_to);
5047 }
5048
5049 /* For TUs we want to skip the first top level sibling if it's not the
5050    actual type being defined by this TU.  In this case the first top
5051    level sibling is there to provide context only.  */
5052
5053 static void
5054 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5055 {
5056   struct objfile *objfile = cu->objfile;
5057   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5058   CORE_ADDR lowpc;
5059   struct attribute *attr;
5060   char *name = NULL;
5061   char *comp_dir = NULL;
5062   struct die_info *child_die;
5063   bfd *abfd = objfile->obfd;
5064
5065   /* start_symtab needs a low pc, but we don't really have one.
5066      Do what read_file_scope would do in the absence of such info.  */
5067   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5068
5069   /* Find the filename.  Do not use dwarf2_name here, since the filename
5070      is not a source language identifier.  */
5071   attr = dwarf2_attr (die, DW_AT_name, cu);
5072   if (attr)
5073     name = DW_STRING (attr);
5074
5075   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5076   if (attr)
5077     comp_dir = DW_STRING (attr);
5078   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5079     {
5080       comp_dir = ldirname (name);
5081       if (comp_dir != NULL)
5082         make_cleanup (xfree, comp_dir);
5083     }
5084
5085   if (name == NULL)
5086     name = "<unknown>";
5087
5088   attr = dwarf2_attr (die, DW_AT_language, cu);
5089   if (attr)
5090     set_cu_language (DW_UNSND (attr), cu);
5091
5092   /* This isn't technically needed today.  It is done for symmetry
5093      with read_file_scope.  */
5094   attr = dwarf2_attr (die, DW_AT_producer, cu);
5095   if (attr)
5096     cu->producer = DW_STRING (attr);
5097
5098   /* We assume that we're processing GCC output. */
5099   processing_gcc_compilation = 2;
5100
5101   processing_has_namespace_info = 0;
5102
5103   start_symtab (name, comp_dir, lowpc);
5104   record_debugformat ("DWARF 2");
5105   record_producer (cu->producer);
5106
5107   /* Process the dies in the type unit.  */
5108   if (die->child == NULL)
5109     {
5110       dump_die_for_error (die);
5111       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5112              bfd_get_filename (abfd));
5113     }
5114
5115   child_die = die->child;
5116
5117   while (child_die && child_die->tag)
5118     {
5119       process_die (child_die, cu);
5120
5121       child_die = sibling_die (child_die);
5122     }
5123
5124   do_cleanups (back_to);
5125 }
5126
5127 static void
5128 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5129                      struct dwarf2_cu *cu)
5130 {
5131   struct function_range *thisfn;
5132
5133   thisfn = (struct function_range *)
5134     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5135   thisfn->name = name;
5136   thisfn->lowpc = lowpc;
5137   thisfn->highpc = highpc;
5138   thisfn->seen_line = 0;
5139   thisfn->next = NULL;
5140
5141   if (cu->last_fn == NULL)
5142       cu->first_fn = thisfn;
5143   else
5144       cu->last_fn->next = thisfn;
5145
5146   cu->last_fn = thisfn;
5147 }
5148
5149 /* qsort helper for inherit_abstract_dies.  */
5150
5151 static int
5152 unsigned_int_compar (const void *ap, const void *bp)
5153 {
5154   unsigned int a = *(unsigned int *) ap;
5155   unsigned int b = *(unsigned int *) bp;
5156
5157   return (a > b) - (b > a);
5158 }
5159
5160 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5161    Inherit only the children of the DW_AT_abstract_origin DIE not being already
5162    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
5163
5164 static void
5165 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5166 {
5167   struct die_info *child_die;
5168   unsigned die_children_count;
5169   /* CU offsets which were referenced by children of the current DIE.  */
5170   unsigned *offsets;
5171   unsigned *offsets_end, *offsetp;
5172   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5173   struct die_info *origin_die;
5174   /* Iterator of the ORIGIN_DIE children.  */
5175   struct die_info *origin_child_die;
5176   struct cleanup *cleanups;
5177   struct attribute *attr;
5178
5179   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5180   if (!attr)
5181     return;
5182
5183   origin_die = follow_die_ref (die, attr, &cu);
5184   if (die->tag != origin_die->tag
5185       && !(die->tag == DW_TAG_inlined_subroutine
5186            && origin_die->tag == DW_TAG_subprogram))
5187     complaint (&symfile_complaints,
5188                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5189                die->offset, origin_die->offset);
5190
5191   child_die = die->child;
5192   die_children_count = 0;
5193   while (child_die && child_die->tag)
5194     {
5195       child_die = sibling_die (child_die);
5196       die_children_count++;
5197     }
5198   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5199   cleanups = make_cleanup (xfree, offsets);
5200
5201   offsets_end = offsets;
5202   child_die = die->child;
5203   while (child_die && child_die->tag)
5204     {
5205       /* For each CHILD_DIE, find the corresponding child of
5206          ORIGIN_DIE.  If there is more than one layer of
5207          DW_AT_abstract_origin, follow them all; there shouldn't be,
5208          but GCC versions at least through 4.4 generate this (GCC PR
5209          40573).  */
5210       struct die_info *child_origin_die = child_die;
5211
5212       while (1)
5213         {
5214           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
5215           if (attr == NULL)
5216             break;
5217           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
5218         }
5219
5220       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5221          counterpart may exist.  */
5222       if (child_origin_die != child_die)
5223         {
5224           if (child_die->tag != child_origin_die->tag
5225               && !(child_die->tag == DW_TAG_inlined_subroutine
5226                    && child_origin_die->tag == DW_TAG_subprogram))
5227             complaint (&symfile_complaints,
5228                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5229                          "different tags"), child_die->offset,
5230                        child_origin_die->offset);
5231           if (child_origin_die->parent != origin_die)
5232             complaint (&symfile_complaints,
5233                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5234                          "different parents"), child_die->offset,
5235                        child_origin_die->offset);
5236           else
5237             *offsets_end++ = child_origin_die->offset;
5238         }
5239       child_die = sibling_die (child_die);
5240     }
5241   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5242          unsigned_int_compar);
5243   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5244     if (offsetp[-1] == *offsetp)
5245       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5246                                         "to DIE 0x%x as their abstract origin"),
5247                  die->offset, *offsetp);
5248
5249   offsetp = offsets;
5250   origin_child_die = origin_die->child;
5251   while (origin_child_die && origin_child_die->tag)
5252     {
5253       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5254       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5255         offsetp++;
5256       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5257         {
5258           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5259           process_die (origin_child_die, cu);
5260         }
5261       origin_child_die = sibling_die (origin_child_die);
5262     }
5263
5264   do_cleanups (cleanups);
5265 }
5266
5267 static void
5268 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5269 {
5270   struct objfile *objfile = cu->objfile;
5271   struct context_stack *new;
5272   CORE_ADDR lowpc;
5273   CORE_ADDR highpc;
5274   struct die_info *child_die;
5275   struct attribute *attr, *call_line, *call_file;
5276   char *name;
5277   CORE_ADDR baseaddr;
5278   struct block *block;
5279   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5280   VEC (symbolp) *template_args = NULL;
5281   struct template_symbol *templ_func = NULL;
5282
5283   if (inlined_func)
5284     {
5285       /* If we do not have call site information, we can't show the
5286          caller of this inlined function.  That's too confusing, so
5287          only use the scope for local variables.  */
5288       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5289       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5290       if (call_line == NULL || call_file == NULL)
5291         {
5292           read_lexical_block_scope (die, cu);
5293           return;
5294         }
5295     }
5296
5297   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5298
5299   name = dwarf2_name (die, cu);
5300
5301   /* Ignore functions with missing or empty names.  These are actually
5302      illegal according to the DWARF standard.  */
5303   if (name == NULL)
5304     {
5305       complaint (&symfile_complaints,
5306                  _("missing name for subprogram DIE at %d"), die->offset);
5307       return;
5308     }
5309
5310   /* Ignore functions with missing or invalid low and high pc attributes.  */
5311   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5312     {
5313       attr = dwarf2_attr (die, DW_AT_external, cu);
5314       if (!attr || !DW_UNSND (attr))
5315         complaint (&symfile_complaints,
5316                    _("cannot get low and high bounds for subprogram DIE at %d"),
5317                    die->offset);
5318       return;
5319     }
5320
5321   lowpc += baseaddr;
5322   highpc += baseaddr;
5323
5324   /* Record the function range for dwarf_decode_lines.  */
5325   add_to_cu_func_list (name, lowpc, highpc, cu);
5326
5327   /* If we have any template arguments, then we must allocate a
5328      different sort of symbol.  */
5329   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5330     {
5331       if (child_die->tag == DW_TAG_template_type_param
5332           || child_die->tag == DW_TAG_template_value_param)
5333         {
5334           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5335                                        struct template_symbol);
5336           templ_func->base.is_cplus_template_function = 1;
5337           break;
5338         }
5339     }
5340
5341   new = push_context (0, lowpc);
5342   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5343                                (struct symbol *) templ_func);
5344
5345   /* If there is a location expression for DW_AT_frame_base, record
5346      it.  */
5347   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5348   if (attr)
5349     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5350        expression is being recorded directly in the function's symbol
5351        and not in a separate frame-base object.  I guess this hack is
5352        to avoid adding some sort of frame-base adjunct/annex to the
5353        function's symbol :-(.  The problem with doing this is that it
5354        results in a function symbol with a location expression that
5355        has nothing to do with the location of the function, ouch!  The
5356        relationship should be: a function's symbol has-a frame base; a
5357        frame-base has-a location expression.  */
5358     dwarf2_symbol_mark_computed (attr, new->name, cu);
5359
5360   cu->list_in_scope = &local_symbols;
5361
5362   if (die->child != NULL)
5363     {
5364       child_die = die->child;
5365       while (child_die && child_die->tag)
5366         {
5367           if (child_die->tag == DW_TAG_template_type_param
5368               || child_die->tag == DW_TAG_template_value_param)
5369             {
5370               struct symbol *arg = new_symbol (child_die, NULL, cu);
5371
5372               VEC_safe_push (symbolp, template_args, arg);
5373             }
5374           else
5375             process_die (child_die, cu);
5376           child_die = sibling_die (child_die);
5377         }
5378     }
5379
5380   inherit_abstract_dies (die, cu);
5381
5382   /* If we have a DW_AT_specification, we might need to import using
5383      directives from the context of the specification DIE.  See the
5384      comment in determine_prefix.  */
5385   if (cu->language == language_cplus
5386       && dwarf2_attr (die, DW_AT_specification, cu))
5387     {
5388       struct dwarf2_cu *spec_cu = cu;
5389       struct die_info *spec_die = die_specification (die, &spec_cu);
5390
5391       while (spec_die)
5392         {
5393           child_die = spec_die->child;
5394           while (child_die && child_die->tag)
5395             {
5396               if (child_die->tag == DW_TAG_imported_module)
5397                 process_die (child_die, spec_cu);
5398               child_die = sibling_die (child_die);
5399             }
5400
5401           /* In some cases, GCC generates specification DIEs that
5402              themselves contain DW_AT_specification attributes.  */
5403           spec_die = die_specification (spec_die, &spec_cu);
5404         }
5405     }
5406
5407   new = pop_context ();
5408   /* Make a block for the local symbols within.  */
5409   block = finish_block (new->name, &local_symbols, new->old_blocks,
5410                         lowpc, highpc, objfile);
5411
5412   /* For C++, set the block's scope.  */
5413   if (cu->language == language_cplus || cu->language == language_fortran)
5414     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5415                         determine_prefix (die, cu),
5416                         processing_has_namespace_info);
5417
5418   /* If we have address ranges, record them.  */
5419   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5420
5421   /* Attach template arguments to function.  */
5422   if (! VEC_empty (symbolp, template_args))
5423     {
5424       gdb_assert (templ_func != NULL);
5425
5426       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5427       templ_func->template_arguments
5428         = obstack_alloc (&objfile->objfile_obstack,
5429                          (templ_func->n_template_arguments
5430                           * sizeof (struct symbol *)));
5431       memcpy (templ_func->template_arguments,
5432               VEC_address (symbolp, template_args),
5433               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5434       VEC_free (symbolp, template_args);
5435     }
5436
5437   /* In C++, we can have functions nested inside functions (e.g., when
5438      a function declares a class that has methods).  This means that
5439      when we finish processing a function scope, we may need to go
5440      back to building a containing block's symbol lists.  */
5441   local_symbols = new->locals;
5442   param_symbols = new->params;
5443   using_directives = new->using_directives;
5444
5445   /* If we've finished processing a top-level function, subsequent
5446      symbols go in the file symbol list.  */
5447   if (outermost_context_p ())
5448     cu->list_in_scope = &file_symbols;
5449 }
5450
5451 /* Process all the DIES contained within a lexical block scope.  Start
5452    a new scope, process the dies, and then close the scope.  */
5453
5454 static void
5455 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5456 {
5457   struct objfile *objfile = cu->objfile;
5458   struct context_stack *new;
5459   CORE_ADDR lowpc, highpc;
5460   struct die_info *child_die;
5461   CORE_ADDR baseaddr;
5462
5463   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5464
5465   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5466   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5467      as multiple lexical blocks?  Handling children in a sane way would
5468      be nasty.  Might be easier to properly extend generic blocks to
5469      describe ranges.  */
5470   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5471     return;
5472   lowpc += baseaddr;
5473   highpc += baseaddr;
5474
5475   push_context (0, lowpc);
5476   if (die->child != NULL)
5477     {
5478       child_die = die->child;
5479       while (child_die && child_die->tag)
5480         {
5481           process_die (child_die, cu);
5482           child_die = sibling_die (child_die);
5483         }
5484     }
5485   new = pop_context ();
5486
5487   if (local_symbols != NULL || using_directives != NULL)
5488     {
5489       struct block *block
5490         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5491                         highpc, objfile);
5492
5493       /* Note that recording ranges after traversing children, as we
5494          do here, means that recording a parent's ranges entails
5495          walking across all its children's ranges as they appear in
5496          the address map, which is quadratic behavior.
5497
5498          It would be nicer to record the parent's ranges before
5499          traversing its children, simply overriding whatever you find
5500          there.  But since we don't even decide whether to create a
5501          block until after we've traversed its children, that's hard
5502          to do.  */
5503       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5504     }
5505   local_symbols = new->locals;
5506   using_directives = new->using_directives;
5507 }
5508
5509 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5510    Return 1 if the attributes are present and valid, otherwise, return 0.
5511    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5512
5513 static int
5514 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5515                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5516                     struct partial_symtab *ranges_pst)
5517 {
5518   struct objfile *objfile = cu->objfile;
5519   struct comp_unit_head *cu_header = &cu->header;
5520   bfd *obfd = objfile->obfd;
5521   unsigned int addr_size = cu_header->addr_size;
5522   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5523   /* Base address selection entry.  */
5524   CORE_ADDR base;
5525   int found_base;
5526   unsigned int dummy;
5527   gdb_byte *buffer;
5528   CORE_ADDR marker;
5529   int low_set;
5530   CORE_ADDR low = 0;
5531   CORE_ADDR high = 0;
5532   CORE_ADDR baseaddr;
5533
5534   found_base = cu->base_known;
5535   base = cu->base_address;
5536
5537   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5538   if (offset >= dwarf2_per_objfile->ranges.size)
5539     {
5540       complaint (&symfile_complaints,
5541                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5542                  offset);
5543       return 0;
5544     }
5545   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5546
5547   /* Read in the largest possible address.  */
5548   marker = read_address (obfd, buffer, cu, &dummy);
5549   if ((marker & mask) == mask)
5550     {
5551       /* If we found the largest possible address, then
5552          read the base address.  */
5553       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5554       buffer += 2 * addr_size;
5555       offset += 2 * addr_size;
5556       found_base = 1;
5557     }
5558
5559   low_set = 0;
5560
5561   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5562
5563   while (1)
5564     {
5565       CORE_ADDR range_beginning, range_end;
5566
5567       range_beginning = read_address (obfd, buffer, cu, &dummy);
5568       buffer += addr_size;
5569       range_end = read_address (obfd, buffer, cu, &dummy);
5570       buffer += addr_size;
5571       offset += 2 * addr_size;
5572
5573       /* An end of list marker is a pair of zero addresses.  */
5574       if (range_beginning == 0 && range_end == 0)
5575         /* Found the end of list entry.  */
5576         break;
5577
5578       /* Each base address selection entry is a pair of 2 values.
5579          The first is the largest possible address, the second is
5580          the base address.  Check for a base address here.  */
5581       if ((range_beginning & mask) == mask)
5582         {
5583           /* If we found the largest possible address, then
5584              read the base address.  */
5585           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5586           found_base = 1;
5587           continue;
5588         }
5589
5590       if (!found_base)
5591         {
5592           /* We have no valid base address for the ranges
5593              data.  */
5594           complaint (&symfile_complaints,
5595                      _("Invalid .debug_ranges data (no base address)"));
5596           return 0;
5597         }
5598
5599       range_beginning += base;
5600       range_end += base;
5601
5602       if (ranges_pst != NULL && range_beginning < range_end)
5603         addrmap_set_empty (objfile->psymtabs_addrmap,
5604                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5605                            ranges_pst);
5606
5607       /* FIXME: This is recording everything as a low-high
5608          segment of consecutive addresses.  We should have a
5609          data structure for discontiguous block ranges
5610          instead.  */
5611       if (! low_set)
5612         {
5613           low = range_beginning;
5614           high = range_end;
5615           low_set = 1;
5616         }
5617       else
5618         {
5619           if (range_beginning < low)
5620             low = range_beginning;
5621           if (range_end > high)
5622             high = range_end;
5623         }
5624     }
5625
5626   if (! low_set)
5627     /* If the first entry is an end-of-list marker, the range
5628        describes an empty scope, i.e. no instructions.  */
5629     return 0;
5630
5631   if (low_return)
5632     *low_return = low;
5633   if (high_return)
5634     *high_return = high;
5635   return 1;
5636 }
5637
5638 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5639    are present and valid, otherwise, return 0.  Return -1 if the range is
5640    discontinuous, i.e. derived from DW_AT_ranges information.  */
5641 static int
5642 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5643                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5644                       struct partial_symtab *pst)
5645 {
5646   struct attribute *attr;
5647   CORE_ADDR low = 0;
5648   CORE_ADDR high = 0;
5649   int ret = 0;
5650
5651   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5652   if (attr)
5653     {
5654       high = DW_ADDR (attr);
5655       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5656       if (attr)
5657         low = DW_ADDR (attr);
5658       else
5659         /* Found high w/o low attribute.  */
5660         return 0;
5661
5662       /* Found consecutive range of addresses.  */
5663       ret = 1;
5664     }
5665   else
5666     {
5667       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5668       if (attr != NULL)
5669         {
5670           /* Value of the DW_AT_ranges attribute is the offset in the
5671              .debug_ranges section.  */
5672           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5673             return 0;
5674           /* Found discontinuous range of addresses.  */
5675           ret = -1;
5676         }
5677     }
5678
5679   if (high < low)
5680     return 0;
5681
5682   /* When using the GNU linker, .gnu.linkonce. sections are used to
5683      eliminate duplicate copies of functions and vtables and such.
5684      The linker will arbitrarily choose one and discard the others.
5685      The AT_*_pc values for such functions refer to local labels in
5686      these sections.  If the section from that file was discarded, the
5687      labels are not in the output, so the relocs get a value of 0.
5688      If this is a discarded function, mark the pc bounds as invalid,
5689      so that GDB will ignore it.  */
5690   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5691     return 0;
5692
5693   *lowpc = low;
5694   *highpc = high;
5695   return ret;
5696 }
5697
5698 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5699    its low and high PC addresses.  Do nothing if these addresses could not
5700    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5701    and HIGHPC to the high address if greater than HIGHPC.  */
5702
5703 static void
5704 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5705                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5706                                  struct dwarf2_cu *cu)
5707 {
5708   CORE_ADDR low, high;
5709   struct die_info *child = die->child;
5710
5711   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5712     {
5713       *lowpc = min (*lowpc, low);
5714       *highpc = max (*highpc, high);
5715     }
5716
5717   /* If the language does not allow nested subprograms (either inside
5718      subprograms or lexical blocks), we're done.  */
5719   if (cu->language != language_ada)
5720     return;
5721
5722   /* Check all the children of the given DIE.  If it contains nested
5723      subprograms, then check their pc bounds.  Likewise, we need to
5724      check lexical blocks as well, as they may also contain subprogram
5725      definitions.  */
5726   while (child && child->tag)
5727     {
5728       if (child->tag == DW_TAG_subprogram
5729           || child->tag == DW_TAG_lexical_block)
5730         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5731       child = sibling_die (child);
5732     }
5733 }
5734
5735 /* Get the low and high pc's represented by the scope DIE, and store
5736    them in *LOWPC and *HIGHPC.  If the correct values can't be
5737    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5738
5739 static void
5740 get_scope_pc_bounds (struct die_info *die,
5741                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5742                      struct dwarf2_cu *cu)
5743 {
5744   CORE_ADDR best_low = (CORE_ADDR) -1;
5745   CORE_ADDR best_high = (CORE_ADDR) 0;
5746   CORE_ADDR current_low, current_high;
5747
5748   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5749     {
5750       best_low = current_low;
5751       best_high = current_high;
5752     }
5753   else
5754     {
5755       struct die_info *child = die->child;
5756
5757       while (child && child->tag)
5758         {
5759           switch (child->tag) {
5760           case DW_TAG_subprogram:
5761             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5762             break;
5763           case DW_TAG_namespace:
5764           case DW_TAG_module:
5765             /* FIXME: carlton/2004-01-16: Should we do this for
5766                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5767                that current GCC's always emit the DIEs corresponding
5768                to definitions of methods of classes as children of a
5769                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5770                the DIEs giving the declarations, which could be
5771                anywhere).  But I don't see any reason why the
5772                standards says that they have to be there.  */
5773             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5774
5775             if (current_low != ((CORE_ADDR) -1))
5776               {
5777                 best_low = min (best_low, current_low);
5778                 best_high = max (best_high, current_high);
5779               }
5780             break;
5781           default:
5782             /* Ignore. */
5783             break;
5784           }
5785
5786           child = sibling_die (child);
5787         }
5788     }
5789
5790   *lowpc = best_low;
5791   *highpc = best_high;
5792 }
5793
5794 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5795    in DIE.  */
5796 static void
5797 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5798                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5799 {
5800   struct attribute *attr;
5801
5802   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5803   if (attr)
5804     {
5805       CORE_ADDR high = DW_ADDR (attr);
5806
5807       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5808       if (attr)
5809         {
5810           CORE_ADDR low = DW_ADDR (attr);
5811
5812           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5813         }
5814     }
5815
5816   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5817   if (attr)
5818     {
5819       bfd *obfd = cu->objfile->obfd;
5820
5821       /* The value of the DW_AT_ranges attribute is the offset of the
5822          address range list in the .debug_ranges section.  */
5823       unsigned long offset = DW_UNSND (attr);
5824       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5825
5826       /* For some target architectures, but not others, the
5827          read_address function sign-extends the addresses it returns.
5828          To recognize base address selection entries, we need a
5829          mask.  */
5830       unsigned int addr_size = cu->header.addr_size;
5831       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5832
5833       /* The base address, to which the next pair is relative.  Note
5834          that this 'base' is a DWARF concept: most entries in a range
5835          list are relative, to reduce the number of relocs against the
5836          debugging information.  This is separate from this function's
5837          'baseaddr' argument, which GDB uses to relocate debugging
5838          information from a shared library based on the address at
5839          which the library was loaded.  */
5840       CORE_ADDR base = cu->base_address;
5841       int base_known = cu->base_known;
5842
5843       gdb_assert (dwarf2_per_objfile->ranges.readin);
5844       if (offset >= dwarf2_per_objfile->ranges.size)
5845         {
5846           complaint (&symfile_complaints,
5847                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5848                      offset);
5849           return;
5850         }
5851
5852       for (;;)
5853         {
5854           unsigned int bytes_read;
5855           CORE_ADDR start, end;
5856
5857           start = read_address (obfd, buffer, cu, &bytes_read);
5858           buffer += bytes_read;
5859           end = read_address (obfd, buffer, cu, &bytes_read);
5860           buffer += bytes_read;
5861
5862           /* Did we find the end of the range list?  */
5863           if (start == 0 && end == 0)
5864             break;
5865
5866           /* Did we find a base address selection entry?  */
5867           else if ((start & base_select_mask) == base_select_mask)
5868             {
5869               base = end;
5870               base_known = 1;
5871             }
5872
5873           /* We found an ordinary address range.  */
5874           else
5875             {
5876               if (!base_known)
5877                 {
5878                   complaint (&symfile_complaints,
5879                              _("Invalid .debug_ranges data (no base address)"));
5880                   return;
5881                 }
5882
5883               record_block_range (block,
5884                                   baseaddr + base + start,
5885                                   baseaddr + base + end - 1);
5886             }
5887         }
5888     }
5889 }
5890
5891 /* Add an aggregate field to the field list.  */
5892
5893 static void
5894 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5895                   struct dwarf2_cu *cu)
5896 {
5897   struct objfile *objfile = cu->objfile;
5898   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5899   struct nextfield *new_field;
5900   struct attribute *attr;
5901   struct field *fp;
5902   char *fieldname = "";
5903
5904   /* Allocate a new field list entry and link it in.  */
5905   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5906   make_cleanup (xfree, new_field);
5907   memset (new_field, 0, sizeof (struct nextfield));
5908
5909   if (die->tag == DW_TAG_inheritance)
5910     {
5911       new_field->next = fip->baseclasses;
5912       fip->baseclasses = new_field;
5913     }
5914   else
5915     {
5916       new_field->next = fip->fields;
5917       fip->fields = new_field;
5918     }
5919   fip->nfields++;
5920
5921   /* Handle accessibility and virtuality of field.
5922      The default accessibility for members is public, the default
5923      accessibility for inheritance is private.  */
5924   if (die->tag != DW_TAG_inheritance)
5925     new_field->accessibility = DW_ACCESS_public;
5926   else
5927     new_field->accessibility = DW_ACCESS_private;
5928   new_field->virtuality = DW_VIRTUALITY_none;
5929
5930   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5931   if (attr)
5932     new_field->accessibility = DW_UNSND (attr);
5933   if (new_field->accessibility != DW_ACCESS_public)
5934     fip->non_public_fields = 1;
5935   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5936   if (attr)
5937     new_field->virtuality = DW_UNSND (attr);
5938
5939   fp = &new_field->field;
5940
5941   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
5942     {
5943       /* Data member other than a C++ static data member.  */
5944
5945       /* Get type of field.  */
5946       fp->type = die_type (die, cu);
5947
5948       SET_FIELD_BITPOS (*fp, 0);
5949
5950       /* Get bit size of field (zero if none).  */
5951       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
5952       if (attr)
5953         {
5954           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5955         }
5956       else
5957         {
5958           FIELD_BITSIZE (*fp) = 0;
5959         }
5960
5961       /* Get bit offset of field.  */
5962       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5963       if (attr)
5964         {
5965           int byte_offset = 0;
5966
5967           if (attr_form_is_section_offset (attr))
5968             dwarf2_complex_location_expr_complaint ();
5969           else if (attr_form_is_constant (attr))
5970             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5971           else if (attr_form_is_block (attr))
5972             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5973           else
5974             dwarf2_complex_location_expr_complaint ();
5975
5976           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5977         }
5978       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
5979       if (attr)
5980         {
5981           if (gdbarch_bits_big_endian (gdbarch))
5982             {
5983               /* For big endian bits, the DW_AT_bit_offset gives the
5984                  additional bit offset from the MSB of the containing
5985                  anonymous object to the MSB of the field.  We don't
5986                  have to do anything special since we don't need to
5987                  know the size of the anonymous object.  */
5988               FIELD_BITPOS (*fp) += DW_UNSND (attr);
5989             }
5990           else
5991             {
5992               /* For little endian bits, compute the bit offset to the
5993                  MSB of the anonymous object, subtract off the number of
5994                  bits from the MSB of the field to the MSB of the
5995                  object, and then subtract off the number of bits of
5996                  the field itself.  The result is the bit offset of
5997                  the LSB of the field.  */
5998               int anonymous_size;
5999               int bit_offset = DW_UNSND (attr);
6000
6001               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6002               if (attr)
6003                 {
6004                   /* The size of the anonymous object containing
6005                      the bit field is explicit, so use the
6006                      indicated size (in bytes).  */
6007                   anonymous_size = DW_UNSND (attr);
6008                 }
6009               else
6010                 {
6011                   /* The size of the anonymous object containing
6012                      the bit field must be inferred from the type
6013                      attribute of the data member containing the
6014                      bit field.  */
6015                   anonymous_size = TYPE_LENGTH (fp->type);
6016                 }
6017               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6018                 - bit_offset - FIELD_BITSIZE (*fp);
6019             }
6020         }
6021
6022       /* Get name of field.  */
6023       fieldname = dwarf2_name (die, cu);
6024       if (fieldname == NULL)
6025         fieldname = "";
6026
6027       /* The name is already allocated along with this objfile, so we don't
6028          need to duplicate it for the type.  */
6029       fp->name = fieldname;
6030
6031       /* Change accessibility for artificial fields (e.g. virtual table
6032          pointer or virtual base class pointer) to private.  */
6033       if (dwarf2_attr (die, DW_AT_artificial, cu))
6034         {
6035           FIELD_ARTIFICIAL (*fp) = 1;
6036           new_field->accessibility = DW_ACCESS_private;
6037           fip->non_public_fields = 1;
6038         }
6039     }
6040   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6041     {
6042       /* C++ static member.  */
6043
6044       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6045          is a declaration, but all versions of G++ as of this writing
6046          (so through at least 3.2.1) incorrectly generate
6047          DW_TAG_variable tags.  */
6048
6049       char *physname;
6050
6051       /* Get name of field.  */
6052       fieldname = dwarf2_name (die, cu);
6053       if (fieldname == NULL)
6054         return;
6055
6056       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6057       if (attr
6058           /* Only create a symbol if this is an external value.
6059              new_symbol checks this and puts the value in the global symbol
6060              table, which we want.  If it is not external, new_symbol
6061              will try to put the value in cu->list_in_scope which is wrong.  */
6062           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6063         {
6064           /* A static const member, not much different than an enum as far as
6065              we're concerned, except that we can support more types.  */
6066           new_symbol (die, NULL, cu);
6067         }
6068
6069       /* Get physical name.  */
6070       physname = (char *) dwarf2_physname (fieldname, die, cu);
6071
6072       /* The name is already allocated along with this objfile, so we don't
6073          need to duplicate it for the type.  */
6074       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6075       FIELD_TYPE (*fp) = die_type (die, cu);
6076       FIELD_NAME (*fp) = fieldname;
6077     }
6078   else if (die->tag == DW_TAG_inheritance)
6079     {
6080       /* C++ base class field.  */
6081       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6082       if (attr)
6083         {
6084           int byte_offset = 0;
6085
6086           if (attr_form_is_section_offset (attr))
6087             dwarf2_complex_location_expr_complaint ();
6088           else if (attr_form_is_constant (attr))
6089             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6090           else if (attr_form_is_block (attr))
6091             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6092           else
6093             dwarf2_complex_location_expr_complaint ();
6094
6095           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6096         }
6097       FIELD_BITSIZE (*fp) = 0;
6098       FIELD_TYPE (*fp) = die_type (die, cu);
6099       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6100       fip->nbaseclasses++;
6101     }
6102 }
6103
6104 /* Add a typedef defined in the scope of the FIP's class.  */
6105
6106 static void
6107 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6108                     struct dwarf2_cu *cu)
6109 {
6110   struct objfile *objfile = cu->objfile;
6111   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6112   struct typedef_field_list *new_field;
6113   struct attribute *attr;
6114   struct typedef_field *fp;
6115   char *fieldname = "";
6116
6117   /* Allocate a new field list entry and link it in.  */
6118   new_field = xzalloc (sizeof (*new_field));
6119   make_cleanup (xfree, new_field);
6120
6121   gdb_assert (die->tag == DW_TAG_typedef);
6122
6123   fp = &new_field->field;
6124
6125   /* Get name of field.  */
6126   fp->name = dwarf2_name (die, cu);
6127   if (fp->name == NULL)
6128     return;
6129
6130   fp->type = read_type_die (die, cu);
6131
6132   new_field->next = fip->typedef_field_list;
6133   fip->typedef_field_list = new_field;
6134   fip->typedef_field_list_count++;
6135 }
6136
6137 /* Create the vector of fields, and attach it to the type.  */
6138
6139 static void
6140 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6141                               struct dwarf2_cu *cu)
6142 {
6143   int nfields = fip->nfields;
6144
6145   /* Record the field count, allocate space for the array of fields,
6146      and create blank accessibility bitfields if necessary.  */
6147   TYPE_NFIELDS (type) = nfields;
6148   TYPE_FIELDS (type) = (struct field *)
6149     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6150   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6151
6152   if (fip->non_public_fields && cu->language != language_ada)
6153     {
6154       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6155
6156       TYPE_FIELD_PRIVATE_BITS (type) =
6157         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6158       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6159
6160       TYPE_FIELD_PROTECTED_BITS (type) =
6161         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6162       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6163
6164       TYPE_FIELD_IGNORE_BITS (type) =
6165         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6166       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6167     }
6168
6169   /* If the type has baseclasses, allocate and clear a bit vector for
6170      TYPE_FIELD_VIRTUAL_BITS.  */
6171   if (fip->nbaseclasses && cu->language != language_ada)
6172     {
6173       int num_bytes = B_BYTES (fip->nbaseclasses);
6174       unsigned char *pointer;
6175
6176       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6177       pointer = TYPE_ALLOC (type, num_bytes);
6178       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6179       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6180       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6181     }
6182
6183   /* Copy the saved-up fields into the field vector.  Start from the head
6184      of the list, adding to the tail of the field array, so that they end
6185      up in the same order in the array in which they were added to the list.  */
6186   while (nfields-- > 0)
6187     {
6188       struct nextfield *fieldp;
6189
6190       if (fip->fields)
6191         {
6192           fieldp = fip->fields;
6193           fip->fields = fieldp->next;
6194         }
6195       else
6196         {
6197           fieldp = fip->baseclasses;
6198           fip->baseclasses = fieldp->next;
6199         }
6200
6201       TYPE_FIELD (type, nfields) = fieldp->field;
6202       switch (fieldp->accessibility)
6203         {
6204         case DW_ACCESS_private:
6205           if (cu->language != language_ada)
6206             SET_TYPE_FIELD_PRIVATE (type, nfields);
6207           break;
6208
6209         case DW_ACCESS_protected:
6210           if (cu->language != language_ada)
6211             SET_TYPE_FIELD_PROTECTED (type, nfields);
6212           break;
6213
6214         case DW_ACCESS_public:
6215           break;
6216
6217         default:
6218           /* Unknown accessibility.  Complain and treat it as public.  */
6219           {
6220             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6221                        fieldp->accessibility);
6222           }
6223           break;
6224         }
6225       if (nfields < fip->nbaseclasses)
6226         {
6227           switch (fieldp->virtuality)
6228             {
6229             case DW_VIRTUALITY_virtual:
6230             case DW_VIRTUALITY_pure_virtual:
6231               if (cu->language == language_ada)
6232                 error ("unexpected virtuality in component of Ada type");
6233               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6234               break;
6235             }
6236         }
6237     }
6238 }
6239
6240 /* Add a member function to the proper fieldlist.  */
6241
6242 static void
6243 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6244                       struct type *type, struct dwarf2_cu *cu)
6245 {
6246   struct objfile *objfile = cu->objfile;
6247   struct attribute *attr;
6248   struct fnfieldlist *flp;
6249   int i;
6250   struct fn_field *fnp;
6251   char *fieldname;
6252   char *physname;
6253   struct nextfnfield *new_fnfield;
6254   struct type *this_type;
6255
6256   if (cu->language == language_ada)
6257     error ("unexpected member function in Ada type");
6258
6259   /* Get name of member function.  */
6260   fieldname = dwarf2_name (die, cu);
6261   if (fieldname == NULL)
6262     return;
6263
6264   /* Get the mangled name.  */
6265   physname = (char *) dwarf2_physname (fieldname, die, cu);
6266
6267   /* Look up member function name in fieldlist.  */
6268   for (i = 0; i < fip->nfnfields; i++)
6269     {
6270       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6271         break;
6272     }
6273
6274   /* Create new list element if necessary.  */
6275   if (i < fip->nfnfields)
6276     flp = &fip->fnfieldlists[i];
6277   else
6278     {
6279       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6280         {
6281           fip->fnfieldlists = (struct fnfieldlist *)
6282             xrealloc (fip->fnfieldlists,
6283                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6284                       * sizeof (struct fnfieldlist));
6285           if (fip->nfnfields == 0)
6286             make_cleanup (free_current_contents, &fip->fnfieldlists);
6287         }
6288       flp = &fip->fnfieldlists[fip->nfnfields];
6289       flp->name = fieldname;
6290       flp->length = 0;
6291       flp->head = NULL;
6292       fip->nfnfields++;
6293     }
6294
6295   /* Create a new member function field and chain it to the field list
6296      entry. */
6297   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6298   make_cleanup (xfree, new_fnfield);
6299   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6300   new_fnfield->next = flp->head;
6301   flp->head = new_fnfield;
6302   flp->length++;
6303
6304   /* Fill in the member function field info.  */
6305   fnp = &new_fnfield->fnfield;
6306   /* The name is already allocated along with this objfile, so we don't
6307      need to duplicate it for the type.  */
6308   fnp->physname = physname ? physname : "";
6309   fnp->type = alloc_type (objfile);
6310   this_type = read_type_die (die, cu);
6311   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6312     {
6313       int nparams = TYPE_NFIELDS (this_type);
6314
6315       /* TYPE is the domain of this method, and THIS_TYPE is the type
6316            of the method itself (TYPE_CODE_METHOD).  */
6317       smash_to_method_type (fnp->type, type,
6318                             TYPE_TARGET_TYPE (this_type),
6319                             TYPE_FIELDS (this_type),
6320                             TYPE_NFIELDS (this_type),
6321                             TYPE_VARARGS (this_type));
6322
6323       /* Handle static member functions.
6324          Dwarf2 has no clean way to discern C++ static and non-static
6325          member functions. G++ helps GDB by marking the first
6326          parameter for non-static member functions (which is the
6327          this pointer) as artificial. We obtain this information
6328          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6329       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6330         fnp->voffset = VOFFSET_STATIC;
6331     }
6332   else
6333     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6334                physname);
6335
6336   /* Get fcontext from DW_AT_containing_type if present.  */
6337   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6338     fnp->fcontext = die_containing_type (die, cu);
6339
6340   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6341      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6342
6343   /* Get accessibility.  */
6344   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6345   if (attr)
6346     {
6347       switch (DW_UNSND (attr))
6348         {
6349         case DW_ACCESS_private:
6350           fnp->is_private = 1;
6351           break;
6352         case DW_ACCESS_protected:
6353           fnp->is_protected = 1;
6354           break;
6355         }
6356     }
6357
6358   /* Check for artificial methods.  */
6359   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6360   if (attr && DW_UNSND (attr) != 0)
6361     fnp->is_artificial = 1;
6362
6363   /* Get index in virtual function table if it is a virtual member
6364      function.  For older versions of GCC, this is an offset in the
6365      appropriate virtual table, as specified by DW_AT_containing_type.
6366      For everyone else, it is an expression to be evaluated relative
6367      to the object address.  */
6368
6369   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6370   if (attr)
6371     {
6372       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6373         {
6374           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6375             {
6376               /* Old-style GCC.  */
6377               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6378             }
6379           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6380                    || (DW_BLOCK (attr)->size > 1
6381                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6382                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6383             {
6384               struct dwarf_block blk;
6385               int offset;
6386
6387               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6388                         ? 1 : 2);
6389               blk.size = DW_BLOCK (attr)->size - offset;
6390               blk.data = DW_BLOCK (attr)->data + offset;
6391               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6392               if ((fnp->voffset % cu->header.addr_size) != 0)
6393                 dwarf2_complex_location_expr_complaint ();
6394               else
6395                 fnp->voffset /= cu->header.addr_size;
6396               fnp->voffset += 2;
6397             }
6398           else
6399             dwarf2_complex_location_expr_complaint ();
6400
6401           if (!fnp->fcontext)
6402             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6403         }
6404       else if (attr_form_is_section_offset (attr))
6405         {
6406           dwarf2_complex_location_expr_complaint ();
6407         }
6408       else
6409         {
6410           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6411                                                  fieldname);
6412         }
6413     }
6414   else
6415     {
6416       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6417       if (attr && DW_UNSND (attr))
6418         {
6419           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6420           complaint (&symfile_complaints,
6421                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6422                      fieldname, die->offset);
6423           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6424           TYPE_CPLUS_DYNAMIC (type) = 1;
6425         }
6426     }
6427 }
6428
6429 /* Create the vector of member function fields, and attach it to the type.  */
6430
6431 static void
6432 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6433                                  struct dwarf2_cu *cu)
6434 {
6435   struct fnfieldlist *flp;
6436   int total_length = 0;
6437   int i;
6438
6439   if (cu->language == language_ada)
6440     error ("unexpected member functions in Ada type");
6441
6442   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6443   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6444     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6445
6446   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6447     {
6448       struct nextfnfield *nfp = flp->head;
6449       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6450       int k;
6451
6452       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6453       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6454       fn_flp->fn_fields = (struct fn_field *)
6455         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6456       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6457         fn_flp->fn_fields[k] = nfp->fnfield;
6458
6459       total_length += flp->length;
6460     }
6461
6462   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6463   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6464 }
6465
6466 /* Returns non-zero if NAME is the name of a vtable member in CU's
6467    language, zero otherwise.  */
6468 static int
6469 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6470 {
6471   static const char vptr[] = "_vptr";
6472   static const char vtable[] = "vtable";
6473
6474   /* Look for the C++ and Java forms of the vtable.  */
6475   if ((cu->language == language_java
6476        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6477        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6478        && is_cplus_marker (name[sizeof (vptr) - 1])))
6479     return 1;
6480
6481   return 0;
6482 }
6483
6484 /* GCC outputs unnamed structures that are really pointers to member
6485    functions, with the ABI-specified layout.  If TYPE describes
6486    such a structure, smash it into a member function type.
6487
6488    GCC shouldn't do this; it should just output pointer to member DIEs.
6489    This is GCC PR debug/28767.  */
6490
6491 static void
6492 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6493 {
6494   struct type *pfn_type, *domain_type, *new_type;
6495
6496   /* Check for a structure with no name and two children.  */
6497   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6498     return;
6499
6500   /* Check for __pfn and __delta members.  */
6501   if (TYPE_FIELD_NAME (type, 0) == NULL
6502       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6503       || TYPE_FIELD_NAME (type, 1) == NULL
6504       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6505     return;
6506
6507   /* Find the type of the method.  */
6508   pfn_type = TYPE_FIELD_TYPE (type, 0);
6509   if (pfn_type == NULL
6510       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6511       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6512     return;
6513
6514   /* Look for the "this" argument.  */
6515   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6516   if (TYPE_NFIELDS (pfn_type) == 0
6517       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6518       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6519     return;
6520
6521   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6522   new_type = alloc_type (objfile);
6523   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6524                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6525                         TYPE_VARARGS (pfn_type));
6526   smash_to_methodptr_type (type, new_type);
6527 }
6528
6529 /* Called when we find the DIE that starts a structure or union scope
6530    (definition) to process all dies that define the members of the
6531    structure or union.
6532
6533    NOTE: we need to call struct_type regardless of whether or not the
6534    DIE has an at_name attribute, since it might be an anonymous
6535    structure or union.  This gets the type entered into our set of
6536    user defined types.
6537
6538    However, if the structure is incomplete (an opaque struct/union)
6539    then suppress creating a symbol table entry for it since gdb only
6540    wants to find the one with the complete definition.  Note that if
6541    it is complete, we just call new_symbol, which does it's own
6542    checking about whether the struct/union is anonymous or not (and
6543    suppresses creating a symbol table entry itself).  */
6544
6545 static struct type *
6546 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6547 {
6548   struct objfile *objfile = cu->objfile;
6549   struct type *type;
6550   struct attribute *attr;
6551   char *name;
6552   struct cleanup *back_to;
6553
6554   /* If the definition of this type lives in .debug_types, read that type.
6555      Don't follow DW_AT_specification though, that will take us back up
6556      the chain and we want to go down.  */
6557   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6558   if (attr)
6559     {
6560       struct dwarf2_cu *type_cu = cu;
6561       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6562
6563       /* We could just recurse on read_structure_type, but we need to call
6564          get_die_type to ensure only one type for this DIE is created.
6565          This is important, for example, because for c++ classes we need
6566          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6567       type = read_type_die (type_die, type_cu);
6568       return set_die_type (die, type, cu);
6569     }
6570
6571   back_to = make_cleanup (null_cleanup, 0);
6572
6573   type = alloc_type (objfile);
6574   INIT_CPLUS_SPECIFIC (type);
6575
6576   name = dwarf2_name (die, cu);
6577   if (name != NULL)
6578     {
6579       if (cu->language == language_cplus
6580           || cu->language == language_java)
6581         {
6582           TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6583           if (die->tag == DW_TAG_structure_type
6584               || die->tag == DW_TAG_class_type)
6585             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6586         }
6587       else
6588         {
6589           /* The name is already allocated along with this objfile, so
6590              we don't need to duplicate it for the type.  */
6591           TYPE_TAG_NAME (type) = (char *) name;
6592           if (die->tag == DW_TAG_class_type)
6593             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6594         }
6595     }
6596
6597   if (die->tag == DW_TAG_structure_type)
6598     {
6599       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6600     }
6601   else if (die->tag == DW_TAG_union_type)
6602     {
6603       TYPE_CODE (type) = TYPE_CODE_UNION;
6604     }
6605   else
6606     {
6607       TYPE_CODE (type) = TYPE_CODE_CLASS;
6608     }
6609
6610   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6611     TYPE_DECLARED_CLASS (type) = 1;
6612
6613   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6614   if (attr)
6615     {
6616       TYPE_LENGTH (type) = DW_UNSND (attr);
6617     }
6618   else
6619     {
6620       TYPE_LENGTH (type) = 0;
6621     }
6622
6623   TYPE_STUB_SUPPORTED (type) = 1;
6624   if (die_is_declaration (die, cu))
6625     TYPE_STUB (type) = 1;
6626   else if (attr == NULL && die->child == NULL
6627            && producer_is_realview (cu->producer))
6628     /* RealView does not output the required DW_AT_declaration
6629        on incomplete types.  */
6630     TYPE_STUB (type) = 1;
6631
6632   /* We need to add the type field to the die immediately so we don't
6633      infinitely recurse when dealing with pointers to the structure
6634      type within the structure itself. */
6635   set_die_type (die, type, cu);
6636
6637   /* set_die_type should be already done.  */
6638   set_descriptive_type (type, die, cu);
6639
6640   if (die->child != NULL && ! die_is_declaration (die, cu))
6641     {
6642       struct field_info fi;
6643       struct die_info *child_die;
6644       VEC (symbolp) *template_args = NULL;
6645
6646       memset (&fi, 0, sizeof (struct field_info));
6647
6648       child_die = die->child;
6649
6650       while (child_die && child_die->tag)
6651         {
6652           if (child_die->tag == DW_TAG_member
6653               || child_die->tag == DW_TAG_variable)
6654             {
6655               /* NOTE: carlton/2002-11-05: A C++ static data member
6656                  should be a DW_TAG_member that is a declaration, but
6657                  all versions of G++ as of this writing (so through at
6658                  least 3.2.1) incorrectly generate DW_TAG_variable
6659                  tags for them instead.  */
6660               dwarf2_add_field (&fi, child_die, cu);
6661             }
6662           else if (child_die->tag == DW_TAG_subprogram)
6663             {
6664               /* C++ member function. */
6665               dwarf2_add_member_fn (&fi, child_die, type, cu);
6666             }
6667           else if (child_die->tag == DW_TAG_inheritance)
6668             {
6669               /* C++ base class field.  */
6670               dwarf2_add_field (&fi, child_die, cu);
6671             }
6672           else if (child_die->tag == DW_TAG_typedef)
6673             dwarf2_add_typedef (&fi, child_die, cu);
6674           else if (child_die->tag == DW_TAG_template_type_param
6675                    || child_die->tag == DW_TAG_template_value_param)
6676             {
6677               struct symbol *arg = new_symbol (child_die, NULL, cu);
6678
6679               VEC_safe_push (symbolp, template_args, arg);
6680             }
6681
6682           child_die = sibling_die (child_die);
6683         }
6684
6685       /* Attach template arguments to type.  */
6686       if (! VEC_empty (symbolp, template_args))
6687         {
6688           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6689           TYPE_N_TEMPLATE_ARGUMENTS (type)
6690             = VEC_length (symbolp, template_args);
6691           TYPE_TEMPLATE_ARGUMENTS (type)
6692             = obstack_alloc (&objfile->objfile_obstack,
6693                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
6694                               * sizeof (struct symbol *)));
6695           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6696                   VEC_address (symbolp, template_args),
6697                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
6698                    * sizeof (struct symbol *)));
6699           VEC_free (symbolp, template_args);
6700         }
6701
6702       /* Attach fields and member functions to the type.  */
6703       if (fi.nfields)
6704         dwarf2_attach_fields_to_type (&fi, type, cu);
6705       if (fi.nfnfields)
6706         {
6707           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6708
6709           /* Get the type which refers to the base class (possibly this
6710              class itself) which contains the vtable pointer for the current
6711              class from the DW_AT_containing_type attribute.  This use of
6712              DW_AT_containing_type is a GNU extension.  */
6713
6714           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6715             {
6716               struct type *t = die_containing_type (die, cu);
6717
6718               TYPE_VPTR_BASETYPE (type) = t;
6719               if (type == t)
6720                 {
6721                   int i;
6722
6723                   /* Our own class provides vtbl ptr.  */
6724                   for (i = TYPE_NFIELDS (t) - 1;
6725                        i >= TYPE_N_BASECLASSES (t);
6726                        --i)
6727                     {
6728                       char *fieldname = TYPE_FIELD_NAME (t, i);
6729
6730                       if (is_vtable_name (fieldname, cu))
6731                         {
6732                           TYPE_VPTR_FIELDNO (type) = i;
6733                           break;
6734                         }
6735                     }
6736
6737                   /* Complain if virtual function table field not found.  */
6738                   if (i < TYPE_N_BASECLASSES (t))
6739                     complaint (&symfile_complaints,
6740                                _("virtual function table pointer not found when defining class '%s'"),
6741                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6742                                "");
6743                 }
6744               else
6745                 {
6746                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6747                 }
6748             }
6749           else if (cu->producer
6750                    && strncmp (cu->producer,
6751                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6752             {
6753               /* The IBM XLC compiler does not provide direct indication
6754                  of the containing type, but the vtable pointer is
6755                  always named __vfp.  */
6756
6757               int i;
6758
6759               for (i = TYPE_NFIELDS (type) - 1;
6760                    i >= TYPE_N_BASECLASSES (type);
6761                    --i)
6762                 {
6763                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6764                     {
6765                       TYPE_VPTR_FIELDNO (type) = i;
6766                       TYPE_VPTR_BASETYPE (type) = type;
6767                       break;
6768                     }
6769                 }
6770             }
6771         }
6772
6773       /* Copy fi.typedef_field_list linked list elements content into the
6774          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6775       if (fi.typedef_field_list)
6776         {
6777           int i = fi.typedef_field_list_count;
6778
6779           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6780           TYPE_TYPEDEF_FIELD_ARRAY (type)
6781             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6782           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6783
6784           /* Reverse the list order to keep the debug info elements order.  */
6785           while (--i >= 0)
6786             {
6787               struct typedef_field *dest, *src;
6788
6789               dest = &TYPE_TYPEDEF_FIELD (type, i);
6790               src = &fi.typedef_field_list->field;
6791               fi.typedef_field_list = fi.typedef_field_list->next;
6792               *dest = *src;
6793             }
6794         }
6795     }
6796
6797   quirk_gcc_member_function_pointer (type, cu->objfile);
6798
6799   do_cleanups (back_to);
6800   return type;
6801 }
6802
6803 static void
6804 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6805 {
6806   struct die_info *child_die = die->child;
6807   struct type *this_type;
6808
6809   this_type = get_die_type (die, cu);
6810   if (this_type == NULL)
6811     this_type = read_structure_type (die, cu);
6812
6813   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6814      snapshots) has been known to create a die giving a declaration
6815      for a class that has, as a child, a die giving a definition for a
6816      nested class.  So we have to process our children even if the
6817      current die is a declaration.  Normally, of course, a declaration
6818      won't have any children at all.  */
6819
6820   while (child_die != NULL && child_die->tag)
6821     {
6822       if (child_die->tag == DW_TAG_member
6823           || child_die->tag == DW_TAG_variable
6824           || child_die->tag == DW_TAG_inheritance
6825           || child_die->tag == DW_TAG_template_value_param
6826           || child_die->tag == DW_TAG_template_type_param)
6827         {
6828           /* Do nothing.  */
6829         }
6830       else
6831         process_die (child_die, cu);
6832
6833       child_die = sibling_die (child_die);
6834     }
6835
6836   /* Do not consider external references.  According to the DWARF standard,
6837      these DIEs are identified by the fact that they have no byte_size
6838      attribute, and a declaration attribute.  */
6839   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6840       || !die_is_declaration (die, cu))
6841     new_symbol (die, this_type, cu);
6842 }
6843
6844 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6845    complete the type's fields yet, or create any symbols.  */
6846
6847 static struct type *
6848 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6849 {
6850   struct objfile *objfile = cu->objfile;
6851   struct type *type;
6852   struct attribute *attr;
6853   const char *name;
6854
6855   /* If the definition of this type lives in .debug_types, read that type.
6856      Don't follow DW_AT_specification though, that will take us back up
6857      the chain and we want to go down.  */
6858   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6859   if (attr)
6860     {
6861       struct dwarf2_cu *type_cu = cu;
6862       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6863
6864       type = read_type_die (type_die, type_cu);
6865       return set_die_type (die, type, cu);
6866     }
6867
6868   type = alloc_type (objfile);
6869
6870   TYPE_CODE (type) = TYPE_CODE_ENUM;
6871   name = dwarf2_full_name (NULL, die, cu);
6872   if (name != NULL)
6873     TYPE_TAG_NAME (type) = (char *) name;
6874
6875   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6876   if (attr)
6877     {
6878       TYPE_LENGTH (type) = DW_UNSND (attr);
6879     }
6880   else
6881     {
6882       TYPE_LENGTH (type) = 0;
6883     }
6884
6885   /* The enumeration DIE can be incomplete.  In Ada, any type can be
6886      declared as private in the package spec, and then defined only
6887      inside the package body.  Such types are known as Taft Amendment
6888      Types.  When another package uses such a type, an incomplete DIE
6889      may be generated by the compiler.  */
6890   if (die_is_declaration (die, cu))
6891     TYPE_STUB (type) = 1;
6892
6893   return set_die_type (die, type, cu);
6894 }
6895
6896 /* Given a pointer to a die which begins an enumeration, process all
6897    the dies that define the members of the enumeration, and create the
6898    symbol for the enumeration type.
6899
6900    NOTE: We reverse the order of the element list.  */
6901
6902 static void
6903 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6904 {
6905   struct die_info *child_die;
6906   struct field *fields;
6907   struct symbol *sym;
6908   int num_fields;
6909   int unsigned_enum = 1;
6910   char *name;
6911   struct type *this_type;
6912
6913   num_fields = 0;
6914   fields = NULL;
6915   this_type = get_die_type (die, cu);
6916   if (this_type == NULL)
6917     this_type = read_enumeration_type (die, cu);
6918   if (die->child != NULL)
6919     {
6920       child_die = die->child;
6921       while (child_die && child_die->tag)
6922         {
6923           if (child_die->tag != DW_TAG_enumerator)
6924             {
6925               process_die (child_die, cu);
6926             }
6927           else
6928             {
6929               name = dwarf2_name (child_die, cu);
6930               if (name)
6931                 {
6932                   sym = new_symbol (child_die, this_type, cu);
6933                   if (SYMBOL_VALUE (sym) < 0)
6934                     unsigned_enum = 0;
6935
6936                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6937                     {
6938                       fields = (struct field *)
6939                         xrealloc (fields,
6940                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
6941                                   * sizeof (struct field));
6942                     }
6943
6944                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
6945                   FIELD_TYPE (fields[num_fields]) = NULL;
6946                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
6947                   FIELD_BITSIZE (fields[num_fields]) = 0;
6948
6949                   num_fields++;
6950                 }
6951             }
6952
6953           child_die = sibling_die (child_die);
6954         }
6955
6956       if (num_fields)
6957         {
6958           TYPE_NFIELDS (this_type) = num_fields;
6959           TYPE_FIELDS (this_type) = (struct field *)
6960             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6961           memcpy (TYPE_FIELDS (this_type), fields,
6962                   sizeof (struct field) * num_fields);
6963           xfree (fields);
6964         }
6965       if (unsigned_enum)
6966         TYPE_UNSIGNED (this_type) = 1;
6967     }
6968
6969   new_symbol (die, this_type, cu);
6970 }
6971
6972 /* Extract all information from a DW_TAG_array_type DIE and put it in
6973    the DIE's type field.  For now, this only handles one dimensional
6974    arrays.  */
6975
6976 static struct type *
6977 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
6978 {
6979   struct objfile *objfile = cu->objfile;
6980   struct die_info *child_die;
6981   struct type *type;
6982   struct type *element_type, *range_type, *index_type;
6983   struct type **range_types = NULL;
6984   struct attribute *attr;
6985   int ndim = 0;
6986   struct cleanup *back_to;
6987   char *name;
6988
6989   element_type = die_type (die, cu);
6990
6991   /* The die_type call above may have already set the type for this DIE.  */
6992   type = get_die_type (die, cu);
6993   if (type)
6994     return type;
6995
6996   /* Irix 6.2 native cc creates array types without children for
6997      arrays with unspecified length.  */
6998   if (die->child == NULL)
6999     {
7000       index_type = objfile_type (objfile)->builtin_int;
7001       range_type = create_range_type (NULL, index_type, 0, -1);
7002       type = create_array_type (NULL, element_type, range_type);
7003       return set_die_type (die, type, cu);
7004     }
7005
7006   back_to = make_cleanup (null_cleanup, NULL);
7007   child_die = die->child;
7008   while (child_die && child_die->tag)
7009     {
7010       if (child_die->tag == DW_TAG_subrange_type)
7011         {
7012           struct type *child_type = read_type_die (child_die, cu);
7013
7014           if (child_type != NULL)
7015             {
7016               /* The range type was succesfully read. Save it for
7017                  the array type creation.  */
7018               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7019                 {
7020                   range_types = (struct type **)
7021                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7022                               * sizeof (struct type *));
7023                   if (ndim == 0)
7024                     make_cleanup (free_current_contents, &range_types);
7025                 }
7026               range_types[ndim++] = child_type;
7027             }
7028         }
7029       child_die = sibling_die (child_die);
7030     }
7031
7032   /* Dwarf2 dimensions are output from left to right, create the
7033      necessary array types in backwards order.  */
7034
7035   type = element_type;
7036
7037   if (read_array_order (die, cu) == DW_ORD_col_major)
7038     {
7039       int i = 0;
7040
7041       while (i < ndim)
7042         type = create_array_type (NULL, type, range_types[i++]);
7043     }
7044   else
7045     {
7046       while (ndim-- > 0)
7047         type = create_array_type (NULL, type, range_types[ndim]);
7048     }
7049
7050   /* Understand Dwarf2 support for vector types (like they occur on
7051      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7052      array type.  This is not part of the Dwarf2/3 standard yet, but a
7053      custom vendor extension.  The main difference between a regular
7054      array and the vector variant is that vectors are passed by value
7055      to functions.  */
7056   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7057   if (attr)
7058     make_vector_type (type);
7059
7060   name = dwarf2_name (die, cu);
7061   if (name)
7062     TYPE_NAME (type) = name;
7063
7064   /* Install the type in the die. */
7065   set_die_type (die, type, cu);
7066
7067   /* set_die_type should be already done.  */
7068   set_descriptive_type (type, die, cu);
7069
7070   do_cleanups (back_to);
7071
7072   return type;
7073 }
7074
7075 static enum dwarf_array_dim_ordering
7076 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7077 {
7078   struct attribute *attr;
7079
7080   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7081
7082   if (attr) return DW_SND (attr);
7083
7084   /*
7085     GNU F77 is a special case, as at 08/2004 array type info is the
7086     opposite order to the dwarf2 specification, but data is still
7087     laid out as per normal fortran.
7088
7089     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7090     version checking.
7091   */
7092
7093   if (cu->language == language_fortran
7094       && cu->producer && strstr (cu->producer, "GNU F77"))
7095     {
7096       return DW_ORD_row_major;
7097     }
7098
7099   switch (cu->language_defn->la_array_ordering)
7100     {
7101     case array_column_major:
7102       return DW_ORD_col_major;
7103     case array_row_major:
7104     default:
7105       return DW_ORD_row_major;
7106     };
7107 }
7108
7109 /* Extract all information from a DW_TAG_set_type DIE and put it in
7110    the DIE's type field. */
7111
7112 static struct type *
7113 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7114 {
7115   struct type *domain_type, *set_type;
7116   struct attribute *attr;
7117
7118   domain_type = die_type (die, cu);
7119
7120   /* The die_type call above may have already set the type for this DIE.  */
7121   set_type = get_die_type (die, cu);
7122   if (set_type)
7123     return set_type;
7124
7125   set_type = create_set_type (NULL, domain_type);
7126
7127   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7128   if (attr)
7129     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7130
7131   return set_die_type (die, set_type, cu);
7132 }
7133
7134 /* First cut: install each common block member as a global variable.  */
7135
7136 static void
7137 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7138 {
7139   struct die_info *child_die;
7140   struct attribute *attr;
7141   struct symbol *sym;
7142   CORE_ADDR base = (CORE_ADDR) 0;
7143
7144   attr = dwarf2_attr (die, DW_AT_location, cu);
7145   if (attr)
7146     {
7147       /* Support the .debug_loc offsets */
7148       if (attr_form_is_block (attr))
7149         {
7150           base = decode_locdesc (DW_BLOCK (attr), cu);
7151         }
7152       else if (attr_form_is_section_offset (attr))
7153         {
7154           dwarf2_complex_location_expr_complaint ();
7155         }
7156       else
7157         {
7158           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7159                                                  "common block member");
7160         }
7161     }
7162   if (die->child != NULL)
7163     {
7164       child_die = die->child;
7165       while (child_die && child_die->tag)
7166         {
7167           sym = new_symbol (child_die, NULL, cu);
7168           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7169           if (attr)
7170             {
7171               CORE_ADDR byte_offset = 0;
7172
7173               if (attr_form_is_section_offset (attr))
7174                 dwarf2_complex_location_expr_complaint ();
7175               else if (attr_form_is_constant (attr))
7176                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7177               else if (attr_form_is_block (attr))
7178                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7179               else
7180                 dwarf2_complex_location_expr_complaint ();
7181
7182               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7183               add_symbol_to_list (sym, &global_symbols);
7184             }
7185           child_die = sibling_die (child_die);
7186         }
7187     }
7188 }
7189
7190 /* Create a type for a C++ namespace.  */
7191
7192 static struct type *
7193 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7194 {
7195   struct objfile *objfile = cu->objfile;
7196   const char *previous_prefix, *name;
7197   int is_anonymous;
7198   struct type *type;
7199
7200   /* For extensions, reuse the type of the original namespace.  */
7201   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7202     {
7203       struct die_info *ext_die;
7204       struct dwarf2_cu *ext_cu = cu;
7205
7206       ext_die = dwarf2_extension (die, &ext_cu);
7207       type = read_type_die (ext_die, ext_cu);
7208       return set_die_type (die, type, cu);
7209     }
7210
7211   name = namespace_name (die, &is_anonymous, cu);
7212
7213   /* Now build the name of the current namespace.  */
7214
7215   previous_prefix = determine_prefix (die, cu);
7216   if (previous_prefix[0] != '\0')
7217     name = typename_concat (&objfile->objfile_obstack,
7218                             previous_prefix, name, 0, cu);
7219
7220   /* Create the type.  */
7221   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7222                     objfile);
7223   TYPE_NAME (type) = (char *) name;
7224   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7225
7226   return set_die_type (die, type, cu);
7227 }
7228
7229 /* Read a C++ namespace.  */
7230
7231 static void
7232 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7233 {
7234   struct objfile *objfile = cu->objfile;
7235   const char *name;
7236   int is_anonymous;
7237
7238   /* Add a symbol associated to this if we haven't seen the namespace
7239      before.  Also, add a using directive if it's an anonymous
7240      namespace.  */
7241
7242   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7243     {
7244       struct type *type;
7245
7246       type = read_type_die (die, cu);
7247       new_symbol (die, type, cu);
7248
7249       name = namespace_name (die, &is_anonymous, cu);
7250       if (is_anonymous)
7251         {
7252           const char *previous_prefix = determine_prefix (die, cu);
7253
7254           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7255                                   NULL, &objfile->objfile_obstack);
7256         }
7257     }
7258
7259   if (die->child != NULL)
7260     {
7261       struct die_info *child_die = die->child;
7262
7263       while (child_die && child_die->tag)
7264         {
7265           process_die (child_die, cu);
7266           child_die = sibling_die (child_die);
7267         }
7268     }
7269 }
7270
7271 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7272    imported module.  Still we need that type as local Fortran "use ... only"
7273    declaration imports depend on the created type in determine_prefix.  */
7274
7275 static struct type *
7276 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7277 {
7278   struct objfile *objfile = cu->objfile;
7279   char *module_name;
7280   struct type *type;
7281
7282   module_name = dwarf2_name (die, cu);
7283   if (!module_name)
7284     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7285                die->offset);
7286   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7287
7288   /* determine_prefix uses TYPE_TAG_NAME.  */
7289   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7290
7291   return set_die_type (die, type, cu);
7292 }
7293
7294 /* Read a Fortran module.  */
7295
7296 static void
7297 read_module (struct die_info *die, struct dwarf2_cu *cu)
7298 {
7299   struct die_info *child_die = die->child;
7300
7301   while (child_die && child_die->tag)
7302     {
7303       process_die (child_die, cu);
7304       child_die = sibling_die (child_die);
7305     }
7306 }
7307
7308 /* Return the name of the namespace represented by DIE.  Set
7309    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7310    namespace.  */
7311
7312 static const char *
7313 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7314 {
7315   struct die_info *current_die;
7316   const char *name = NULL;
7317
7318   /* Loop through the extensions until we find a name.  */
7319
7320   for (current_die = die;
7321        current_die != NULL;
7322        current_die = dwarf2_extension (die, &cu))
7323     {
7324       name = dwarf2_name (current_die, cu);
7325       if (name != NULL)
7326         break;
7327     }
7328
7329   /* Is it an anonymous namespace?  */
7330
7331   *is_anonymous = (name == NULL);
7332   if (*is_anonymous)
7333     name = "(anonymous namespace)";
7334
7335   return name;
7336 }
7337
7338 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7339    the user defined type vector.  */
7340
7341 static struct type *
7342 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7343 {
7344   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7345   struct comp_unit_head *cu_header = &cu->header;
7346   struct type *type;
7347   struct attribute *attr_byte_size;
7348   struct attribute *attr_address_class;
7349   int byte_size, addr_class;
7350   struct type *target_type;
7351
7352   target_type = die_type (die, cu);
7353
7354   /* The die_type call above may have already set the type for this DIE.  */
7355   type = get_die_type (die, cu);
7356   if (type)
7357     return type;
7358
7359   type = lookup_pointer_type (target_type);
7360
7361   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7362   if (attr_byte_size)
7363     byte_size = DW_UNSND (attr_byte_size);
7364   else
7365     byte_size = cu_header->addr_size;
7366
7367   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7368   if (attr_address_class)
7369     addr_class = DW_UNSND (attr_address_class);
7370   else
7371     addr_class = DW_ADDR_none;
7372
7373   /* If the pointer size or address class is different than the
7374      default, create a type variant marked as such and set the
7375      length accordingly.  */
7376   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7377     {
7378       if (gdbarch_address_class_type_flags_p (gdbarch))
7379         {
7380           int type_flags;
7381
7382           type_flags = gdbarch_address_class_type_flags
7383                          (gdbarch, byte_size, addr_class);
7384           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7385                       == 0);
7386           type = make_type_with_address_space (type, type_flags);
7387         }
7388       else if (TYPE_LENGTH (type) != byte_size)
7389         {
7390           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7391         }
7392       else
7393         {
7394           /* Should we also complain about unhandled address classes?  */
7395         }
7396     }
7397
7398   TYPE_LENGTH (type) = byte_size;
7399   return set_die_type (die, type, cu);
7400 }
7401
7402 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7403    the user defined type vector.  */
7404
7405 static struct type *
7406 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7407 {
7408   struct type *type;
7409   struct type *to_type;
7410   struct type *domain;
7411
7412   to_type = die_type (die, cu);
7413   domain = die_containing_type (die, cu);
7414
7415   /* The calls above may have already set the type for this DIE.  */
7416   type = get_die_type (die, cu);
7417   if (type)
7418     return type;
7419
7420   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7421     type = lookup_methodptr_type (to_type);
7422   else
7423     type = lookup_memberptr_type (to_type, domain);
7424
7425   return set_die_type (die, type, cu);
7426 }
7427
7428 /* Extract all information from a DW_TAG_reference_type DIE and add to
7429    the user defined type vector.  */
7430
7431 static struct type *
7432 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7433 {
7434   struct comp_unit_head *cu_header = &cu->header;
7435   struct type *type, *target_type;
7436   struct attribute *attr;
7437
7438   target_type = die_type (die, cu);
7439
7440   /* The die_type call above may have already set the type for this DIE.  */
7441   type = get_die_type (die, cu);
7442   if (type)
7443     return type;
7444
7445   type = lookup_reference_type (target_type);
7446   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7447   if (attr)
7448     {
7449       TYPE_LENGTH (type) = DW_UNSND (attr);
7450     }
7451   else
7452     {
7453       TYPE_LENGTH (type) = cu_header->addr_size;
7454     }
7455   return set_die_type (die, type, cu);
7456 }
7457
7458 static struct type *
7459 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7460 {
7461   struct type *base_type, *cv_type;
7462
7463   base_type = die_type (die, cu);
7464
7465   /* The die_type call above may have already set the type for this DIE.  */
7466   cv_type = get_die_type (die, cu);
7467   if (cv_type)
7468     return cv_type;
7469
7470   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7471   return set_die_type (die, cv_type, cu);
7472 }
7473
7474 static struct type *
7475 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7476 {
7477   struct type *base_type, *cv_type;
7478
7479   base_type = die_type (die, cu);
7480
7481   /* The die_type call above may have already set the type for this DIE.  */
7482   cv_type = get_die_type (die, cu);
7483   if (cv_type)
7484     return cv_type;
7485
7486   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7487   return set_die_type (die, cv_type, cu);
7488 }
7489
7490 /* Extract all information from a DW_TAG_string_type DIE and add to
7491    the user defined type vector.  It isn't really a user defined type,
7492    but it behaves like one, with other DIE's using an AT_user_def_type
7493    attribute to reference it.  */
7494
7495 static struct type *
7496 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7497 {
7498   struct objfile *objfile = cu->objfile;
7499   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7500   struct type *type, *range_type, *index_type, *char_type;
7501   struct attribute *attr;
7502   unsigned int length;
7503
7504   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7505   if (attr)
7506     {
7507       length = DW_UNSND (attr);
7508     }
7509   else
7510     {
7511       /* check for the DW_AT_byte_size attribute */
7512       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7513       if (attr)
7514         {
7515           length = DW_UNSND (attr);
7516         }
7517       else
7518         {
7519           length = 1;
7520         }
7521     }
7522
7523   index_type = objfile_type (objfile)->builtin_int;
7524   range_type = create_range_type (NULL, index_type, 1, length);
7525   char_type = language_string_char_type (cu->language_defn, gdbarch);
7526   type = create_string_type (NULL, char_type, range_type);
7527
7528   return set_die_type (die, type, cu);
7529 }
7530
7531 /* Handle DIES due to C code like:
7532
7533    struct foo
7534    {
7535    int (*funcp)(int a, long l);
7536    int b;
7537    };
7538
7539    ('funcp' generates a DW_TAG_subroutine_type DIE)
7540  */
7541
7542 static struct type *
7543 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7544 {
7545   struct type *type;            /* Type that this function returns */
7546   struct type *ftype;           /* Function that returns above type */
7547   struct attribute *attr;
7548
7549   type = die_type (die, cu);
7550
7551   /* The die_type call above may have already set the type for this DIE.  */
7552   ftype = get_die_type (die, cu);
7553   if (ftype)
7554     return ftype;
7555
7556   ftype = lookup_function_type (type);
7557
7558   /* All functions in C++, Pascal and Java have prototypes.  */
7559   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7560   if ((attr && (DW_UNSND (attr) != 0))
7561       || cu->language == language_cplus
7562       || cu->language == language_java
7563       || cu->language == language_pascal)
7564     TYPE_PROTOTYPED (ftype) = 1;
7565   else if (producer_is_realview (cu->producer))
7566     /* RealView does not emit DW_AT_prototyped.  We can not
7567        distinguish prototyped and unprototyped functions; default to
7568        prototyped, since that is more common in modern code (and
7569        RealView warns about unprototyped functions).  */
7570     TYPE_PROTOTYPED (ftype) = 1;
7571
7572   /* Store the calling convention in the type if it's available in
7573      the subroutine die.  Otherwise set the calling convention to
7574      the default value DW_CC_normal.  */
7575   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7576   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7577
7578   /* We need to add the subroutine type to the die immediately so
7579      we don't infinitely recurse when dealing with parameters
7580      declared as the same subroutine type. */
7581   set_die_type (die, ftype, cu);
7582
7583   if (die->child != NULL)
7584     {
7585       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7586       struct die_info *child_die;
7587       int nparams, iparams;
7588
7589       /* Count the number of parameters.
7590          FIXME: GDB currently ignores vararg functions, but knows about
7591          vararg member functions.  */
7592       nparams = 0;
7593       child_die = die->child;
7594       while (child_die && child_die->tag)
7595         {
7596           if (child_die->tag == DW_TAG_formal_parameter)
7597             nparams++;
7598           else if (child_die->tag == DW_TAG_unspecified_parameters)
7599             TYPE_VARARGS (ftype) = 1;
7600           child_die = sibling_die (child_die);
7601         }
7602
7603       /* Allocate storage for parameters and fill them in.  */
7604       TYPE_NFIELDS (ftype) = nparams;
7605       TYPE_FIELDS (ftype) = (struct field *)
7606         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7607
7608       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7609          even if we error out during the parameters reading below.  */
7610       for (iparams = 0; iparams < nparams; iparams++)
7611         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7612
7613       iparams = 0;
7614       child_die = die->child;
7615       while (child_die && child_die->tag)
7616         {
7617           if (child_die->tag == DW_TAG_formal_parameter)
7618             {
7619               struct type *arg_type;
7620
7621               /* DWARF version 2 has no clean way to discern C++
7622                  static and non-static member functions.  G++ helps
7623                  GDB by marking the first parameter for non-static
7624                  member functions (which is the this pointer) as
7625                  artificial.  We pass this information to
7626                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7627
7628                  DWARF version 3 added DW_AT_object_pointer, which GCC
7629                  4.5 does not yet generate.  */
7630               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7631               if (attr)
7632                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7633               else
7634                 {
7635                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7636
7637                   /* GCC/43521: In java, the formal parameter
7638                      "this" is sometimes not marked with DW_AT_artificial.  */
7639                   if (cu->language == language_java)
7640                     {
7641                       const char *name = dwarf2_name (child_die, cu);
7642
7643                       if (name && !strcmp (name, "this"))
7644                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7645                     }
7646                 }
7647               arg_type = die_type (child_die, cu);
7648
7649               /* RealView does not mark THIS as const, which the testsuite
7650                  expects.  GCC marks THIS as const in method definitions,
7651                  but not in the class specifications (GCC PR 43053).  */
7652               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7653                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7654                 {
7655                   int is_this = 0;
7656                   struct dwarf2_cu *arg_cu = cu;
7657                   const char *name = dwarf2_name (child_die, cu);
7658
7659                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7660                   if (attr)
7661                     {
7662                       /* If the compiler emits this, use it.  */
7663                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
7664                         is_this = 1;
7665                     }
7666                   else if (name && strcmp (name, "this") == 0)
7667                     /* Function definitions will have the argument names.  */
7668                     is_this = 1;
7669                   else if (name == NULL && iparams == 0)
7670                     /* Declarations may not have the names, so like
7671                        elsewhere in GDB, assume an artificial first
7672                        argument is "this".  */
7673                     is_this = 1;
7674
7675                   if (is_this)
7676                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7677                                              arg_type, 0);
7678                 }
7679
7680               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7681               iparams++;
7682             }
7683           child_die = sibling_die (child_die);
7684         }
7685     }
7686
7687   return ftype;
7688 }
7689
7690 static struct type *
7691 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7692 {
7693   struct objfile *objfile = cu->objfile;
7694   const char *name = NULL;
7695   struct type *this_type;
7696
7697   name = dwarf2_full_name (NULL, die, cu);
7698   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7699                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7700   TYPE_NAME (this_type) = (char *) name;
7701   set_die_type (die, this_type, cu);
7702   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7703   return this_type;
7704 }
7705
7706 /* Find a representation of a given base type and install
7707    it in the TYPE field of the die.  */
7708
7709 static struct type *
7710 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7711 {
7712   struct objfile *objfile = cu->objfile;
7713   struct type *type;
7714   struct attribute *attr;
7715   int encoding = 0, size = 0;
7716   char *name;
7717   enum type_code code = TYPE_CODE_INT;
7718   int type_flags = 0;
7719   struct type *target_type = NULL;
7720
7721   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7722   if (attr)
7723     {
7724       encoding = DW_UNSND (attr);
7725     }
7726   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7727   if (attr)
7728     {
7729       size = DW_UNSND (attr);
7730     }
7731   name = dwarf2_name (die, cu);
7732   if (!name)
7733     {
7734       complaint (&symfile_complaints,
7735                  _("DW_AT_name missing from DW_TAG_base_type"));
7736     }
7737
7738   switch (encoding)
7739     {
7740       case DW_ATE_address:
7741         /* Turn DW_ATE_address into a void * pointer.  */
7742         code = TYPE_CODE_PTR;
7743         type_flags |= TYPE_FLAG_UNSIGNED;
7744         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7745         break;
7746       case DW_ATE_boolean:
7747         code = TYPE_CODE_BOOL;
7748         type_flags |= TYPE_FLAG_UNSIGNED;
7749         break;
7750       case DW_ATE_complex_float:
7751         code = TYPE_CODE_COMPLEX;
7752         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7753         break;
7754       case DW_ATE_decimal_float:
7755         code = TYPE_CODE_DECFLOAT;
7756         break;
7757       case DW_ATE_float:
7758         code = TYPE_CODE_FLT;
7759         break;
7760       case DW_ATE_signed:
7761         break;
7762       case DW_ATE_unsigned:
7763         type_flags |= TYPE_FLAG_UNSIGNED;
7764         break;
7765       case DW_ATE_signed_char:
7766         if (cu->language == language_ada || cu->language == language_m2
7767             || cu->language == language_pascal)
7768           code = TYPE_CODE_CHAR;
7769         break;
7770       case DW_ATE_unsigned_char:
7771         if (cu->language == language_ada || cu->language == language_m2
7772             || cu->language == language_pascal)
7773           code = TYPE_CODE_CHAR;
7774         type_flags |= TYPE_FLAG_UNSIGNED;
7775         break;
7776       case DW_ATE_UTF:
7777         /* We just treat this as an integer and then recognize the
7778            type by name elsewhere.  */
7779         break;
7780
7781       default:
7782         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7783                    dwarf_type_encoding_name (encoding));
7784         break;
7785     }
7786
7787   type = init_type (code, size, type_flags, NULL, objfile);
7788   TYPE_NAME (type) = name;
7789   TYPE_TARGET_TYPE (type) = target_type;
7790
7791   if (name && strcmp (name, "char") == 0)
7792     TYPE_NOSIGN (type) = 1;
7793
7794   return set_die_type (die, type, cu);
7795 }
7796
7797 /* Read the given DW_AT_subrange DIE.  */
7798
7799 static struct type *
7800 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7801 {
7802   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7803   struct type *base_type;
7804   struct type *range_type;
7805   struct attribute *attr;
7806   LONGEST low = 0;
7807   LONGEST high = -1;
7808   char *name;
7809   LONGEST negative_mask;
7810
7811   base_type = die_type (die, cu);
7812   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7813   check_typedef (base_type);
7814
7815   /* The die_type call above may have already set the type for this DIE.  */
7816   range_type = get_die_type (die, cu);
7817   if (range_type)
7818     return range_type;
7819
7820   if (cu->language == language_fortran)
7821     {
7822       /* FORTRAN implies a lower bound of 1, if not given.  */
7823       low = 1;
7824     }
7825
7826   /* FIXME: For variable sized arrays either of these could be
7827      a variable rather than a constant value.  We'll allow it,
7828      but we don't know how to handle it.  */
7829   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7830   if (attr)
7831     low = dwarf2_get_attr_constant_value (attr, 0);
7832
7833   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7834   if (attr)
7835     {
7836       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7837         {
7838           /* GCC encodes arrays with unspecified or dynamic length
7839              with a DW_FORM_block1 attribute or a reference attribute.
7840              FIXME: GDB does not yet know how to handle dynamic
7841              arrays properly, treat them as arrays with unspecified
7842              length for now.
7843
7844              FIXME: jimb/2003-09-22: GDB does not really know
7845              how to handle arrays of unspecified length
7846              either; we just represent them as zero-length
7847              arrays.  Choose an appropriate upper bound given
7848              the lower bound we've computed above.  */
7849           high = low - 1;
7850         }
7851       else
7852         high = dwarf2_get_attr_constant_value (attr, 1);
7853     }
7854   else
7855     {
7856       attr = dwarf2_attr (die, DW_AT_count, cu);
7857       if (attr)
7858         {
7859           int count = dwarf2_get_attr_constant_value (attr, 1);
7860           high = low + count - 1;
7861         }
7862     }
7863
7864   /* Dwarf-2 specifications explicitly allows to create subrange types
7865      without specifying a base type.
7866      In that case, the base type must be set to the type of
7867      the lower bound, upper bound or count, in that order, if any of these
7868      three attributes references an object that has a type.
7869      If no base type is found, the Dwarf-2 specifications say that
7870      a signed integer type of size equal to the size of an address should
7871      be used.
7872      For the following C code: `extern char gdb_int [];'
7873      GCC produces an empty range DIE.
7874      FIXME: muller/2010-05-28: Possible references to object for low bound,
7875      high bound or count are not yet handled by this code.
7876   */
7877   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7878     {
7879       struct objfile *objfile = cu->objfile;
7880       struct gdbarch *gdbarch = get_objfile_arch (objfile);
7881       int addr_size = gdbarch_addr_bit (gdbarch) /8;
7882       struct type *int_type = objfile_type (objfile)->builtin_int;
7883
7884       /* Test "int", "long int", and "long long int" objfile types,
7885          and select the first one having a size above or equal to the
7886          architecture address size.  */
7887       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7888         base_type = int_type;
7889       else
7890         {
7891           int_type = objfile_type (objfile)->builtin_long;
7892           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7893             base_type = int_type;
7894           else
7895             {
7896               int_type = objfile_type (objfile)->builtin_long_long;
7897               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7898                 base_type = int_type;
7899             }
7900         }
7901     }
7902
7903   negative_mask =
7904     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7905   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7906     low |= negative_mask;
7907   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7908     high |= negative_mask;
7909
7910   range_type = create_range_type (NULL, base_type, low, high);
7911
7912   /* Mark arrays with dynamic length at least as an array of unspecified
7913      length.  GDB could check the boundary but before it gets implemented at
7914      least allow accessing the array elements.  */
7915   if (attr && attr->form == DW_FORM_block1)
7916     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7917
7918   name = dwarf2_name (die, cu);
7919   if (name)
7920     TYPE_NAME (range_type) = name;
7921
7922   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7923   if (attr)
7924     TYPE_LENGTH (range_type) = DW_UNSND (attr);
7925
7926   set_die_type (die, range_type, cu);
7927
7928   /* set_die_type should be already done.  */
7929   set_descriptive_type (range_type, die, cu);
7930
7931   return range_type;
7932 }
7933
7934 static struct type *
7935 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7936 {
7937   struct type *type;
7938
7939   /* For now, we only support the C meaning of an unspecified type: void.  */
7940
7941   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7942   TYPE_NAME (type) = dwarf2_name (die, cu);
7943
7944   return set_die_type (die, type, cu);
7945 }
7946
7947 /* Trivial hash function for die_info: the hash value of a DIE
7948    is its offset in .debug_info for this objfile.  */
7949
7950 static hashval_t
7951 die_hash (const void *item)
7952 {
7953   const struct die_info *die = item;
7954
7955   return die->offset;
7956 }
7957
7958 /* Trivial comparison function for die_info structures: two DIEs
7959    are equal if they have the same offset.  */
7960
7961 static int
7962 die_eq (const void *item_lhs, const void *item_rhs)
7963 {
7964   const struct die_info *die_lhs = item_lhs;
7965   const struct die_info *die_rhs = item_rhs;
7966
7967   return die_lhs->offset == die_rhs->offset;
7968 }
7969
7970 /* Read a whole compilation unit into a linked list of dies.  */
7971
7972 static struct die_info *
7973 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
7974 {
7975   struct die_reader_specs reader_specs;
7976   int read_abbrevs = 0;
7977   struct cleanup *back_to = NULL;
7978   struct die_info *die;
7979
7980   if (cu->dwarf2_abbrevs == NULL)
7981     {
7982       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
7983       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
7984       read_abbrevs = 1;
7985     }
7986
7987   gdb_assert (cu->die_hash == NULL);
7988   cu->die_hash
7989     = htab_create_alloc_ex (cu->header.length / 12,
7990                             die_hash,
7991                             die_eq,
7992                             NULL,
7993                             &cu->comp_unit_obstack,
7994                             hashtab_obstack_allocate,
7995                             dummy_obstack_deallocate);
7996
7997   init_cu_die_reader (&reader_specs, cu);
7998
7999   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8000
8001   if (read_abbrevs)
8002     do_cleanups (back_to);
8003
8004   return die;
8005 }
8006
8007 /* Main entry point for reading a DIE and all children.
8008    Read the DIE and dump it if requested.  */
8009
8010 static struct die_info *
8011 read_die_and_children (const struct die_reader_specs *reader,
8012                        gdb_byte *info_ptr,
8013                        gdb_byte **new_info_ptr,
8014                        struct die_info *parent)
8015 {
8016   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8017                                                      new_info_ptr, parent);
8018
8019   if (dwarf2_die_debug)
8020     {
8021       fprintf_unfiltered (gdb_stdlog,
8022                           "\nRead die from %s of %s:\n",
8023                           reader->buffer == dwarf2_per_objfile->info.buffer
8024                           ? ".debug_info"
8025                           : reader->buffer == dwarf2_per_objfile->types.buffer
8026                           ? ".debug_types"
8027                           : "unknown section",
8028                           reader->abfd->filename);
8029       dump_die (result, dwarf2_die_debug);
8030     }
8031
8032   return result;
8033 }
8034
8035 /* Read a single die and all its descendents.  Set the die's sibling
8036    field to NULL; set other fields in the die correctly, and set all
8037    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8038    location of the info_ptr after reading all of those dies.  PARENT
8039    is the parent of the die in question.  */
8040
8041 static struct die_info *
8042 read_die_and_children_1 (const struct die_reader_specs *reader,
8043                          gdb_byte *info_ptr,
8044                          gdb_byte **new_info_ptr,
8045                          struct die_info *parent)
8046 {
8047   struct die_info *die;
8048   gdb_byte *cur_ptr;
8049   int has_children;
8050
8051   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8052   if (die == NULL)
8053     {
8054       *new_info_ptr = cur_ptr;
8055       return NULL;
8056     }
8057   store_in_ref_table (die, reader->cu);
8058
8059   if (has_children)
8060     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8061   else
8062     {
8063       die->child = NULL;
8064       *new_info_ptr = cur_ptr;
8065     }
8066
8067   die->sibling = NULL;
8068   die->parent = parent;
8069   return die;
8070 }
8071
8072 /* Read a die, all of its descendents, and all of its siblings; set
8073    all of the fields of all of the dies correctly.  Arguments are as
8074    in read_die_and_children.  */
8075
8076 static struct die_info *
8077 read_die_and_siblings (const struct die_reader_specs *reader,
8078                        gdb_byte *info_ptr,
8079                        gdb_byte **new_info_ptr,
8080                        struct die_info *parent)
8081 {
8082   struct die_info *first_die, *last_sibling;
8083   gdb_byte *cur_ptr;
8084
8085   cur_ptr = info_ptr;
8086   first_die = last_sibling = NULL;
8087
8088   while (1)
8089     {
8090       struct die_info *die
8091         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8092
8093       if (die == NULL)
8094         {
8095           *new_info_ptr = cur_ptr;
8096           return first_die;
8097         }
8098
8099       if (!first_die)
8100         first_die = die;
8101       else
8102         last_sibling->sibling = die;
8103
8104       last_sibling = die;
8105     }
8106 }
8107
8108 /* Read the die from the .debug_info section buffer.  Set DIEP to
8109    point to a newly allocated die with its information, except for its
8110    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8111    whether the die has children or not.  */
8112
8113 static gdb_byte *
8114 read_full_die (const struct die_reader_specs *reader,
8115                struct die_info **diep, gdb_byte *info_ptr,
8116                int *has_children)
8117 {
8118   unsigned int abbrev_number, bytes_read, i, offset;
8119   struct abbrev_info *abbrev;
8120   struct die_info *die;
8121   struct dwarf2_cu *cu = reader->cu;
8122   bfd *abfd = reader->abfd;
8123
8124   offset = info_ptr - reader->buffer;
8125   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8126   info_ptr += bytes_read;
8127   if (!abbrev_number)
8128     {
8129       *diep = NULL;
8130       *has_children = 0;
8131       return info_ptr;
8132     }
8133
8134   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8135   if (!abbrev)
8136     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8137            abbrev_number,
8138            bfd_get_filename (abfd));
8139
8140   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8141   die->offset = offset;
8142   die->tag = abbrev->tag;
8143   die->abbrev = abbrev_number;
8144
8145   die->num_attrs = abbrev->num_attrs;
8146
8147   for (i = 0; i < abbrev->num_attrs; ++i)
8148     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8149                                abfd, info_ptr, cu);
8150
8151   *diep = die;
8152   *has_children = abbrev->has_children;
8153   return info_ptr;
8154 }
8155
8156 /* In DWARF version 2, the description of the debugging information is
8157    stored in a separate .debug_abbrev section.  Before we read any
8158    dies from a section we read in all abbreviations and install them
8159    in a hash table.  This function also sets flags in CU describing
8160    the data found in the abbrev table.  */
8161
8162 static void
8163 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8164 {
8165   struct comp_unit_head *cu_header = &cu->header;
8166   gdb_byte *abbrev_ptr;
8167   struct abbrev_info *cur_abbrev;
8168   unsigned int abbrev_number, bytes_read, abbrev_name;
8169   unsigned int abbrev_form, hash_number;
8170   struct attr_abbrev *cur_attrs;
8171   unsigned int allocated_attrs;
8172
8173   /* Initialize dwarf2 abbrevs */
8174   obstack_init (&cu->abbrev_obstack);
8175   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8176                                       (ABBREV_HASH_SIZE
8177                                        * sizeof (struct abbrev_info *)));
8178   memset (cu->dwarf2_abbrevs, 0,
8179           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8180
8181   dwarf2_read_section (dwarf2_per_objfile->objfile,
8182                        &dwarf2_per_objfile->abbrev);
8183   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8184   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8185   abbrev_ptr += bytes_read;
8186
8187   allocated_attrs = ATTR_ALLOC_CHUNK;
8188   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8189
8190   /* loop until we reach an abbrev number of 0 */
8191   while (abbrev_number)
8192     {
8193       cur_abbrev = dwarf_alloc_abbrev (cu);
8194
8195       /* read in abbrev header */
8196       cur_abbrev->number = abbrev_number;
8197       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8198       abbrev_ptr += bytes_read;
8199       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8200       abbrev_ptr += 1;
8201
8202       if (cur_abbrev->tag == DW_TAG_namespace)
8203         cu->has_namespace_info = 1;
8204
8205       /* now read in declarations */
8206       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8207       abbrev_ptr += bytes_read;
8208       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8209       abbrev_ptr += bytes_read;
8210       while (abbrev_name)
8211         {
8212           if (cur_abbrev->num_attrs == allocated_attrs)
8213             {
8214               allocated_attrs += ATTR_ALLOC_CHUNK;
8215               cur_attrs
8216                 = xrealloc (cur_attrs, (allocated_attrs
8217                                         * sizeof (struct attr_abbrev)));
8218             }
8219
8220           /* Record whether this compilation unit might have
8221              inter-compilation-unit references.  If we don't know what form
8222              this attribute will have, then it might potentially be a
8223              DW_FORM_ref_addr, so we conservatively expect inter-CU
8224              references.  */
8225
8226           if (abbrev_form == DW_FORM_ref_addr
8227               || abbrev_form == DW_FORM_indirect)
8228             cu->has_form_ref_addr = 1;
8229
8230           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8231           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8232           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8233           abbrev_ptr += bytes_read;
8234           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8235           abbrev_ptr += bytes_read;
8236         }
8237
8238       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8239                                          (cur_abbrev->num_attrs
8240                                           * sizeof (struct attr_abbrev)));
8241       memcpy (cur_abbrev->attrs, cur_attrs,
8242               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8243
8244       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8245       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8246       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8247
8248       /* Get next abbreviation.
8249          Under Irix6 the abbreviations for a compilation unit are not
8250          always properly terminated with an abbrev number of 0.
8251          Exit loop if we encounter an abbreviation which we have
8252          already read (which means we are about to read the abbreviations
8253          for the next compile unit) or if the end of the abbreviation
8254          table is reached.  */
8255       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8256           >= dwarf2_per_objfile->abbrev.size)
8257         break;
8258       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8259       abbrev_ptr += bytes_read;
8260       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8261         break;
8262     }
8263
8264   xfree (cur_attrs);
8265 }
8266
8267 /* Release the memory used by the abbrev table for a compilation unit.  */
8268
8269 static void
8270 dwarf2_free_abbrev_table (void *ptr_to_cu)
8271 {
8272   struct dwarf2_cu *cu = ptr_to_cu;
8273
8274   obstack_free (&cu->abbrev_obstack, NULL);
8275   cu->dwarf2_abbrevs = NULL;
8276 }
8277
8278 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8279
8280 static struct abbrev_info *
8281 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8282 {
8283   unsigned int hash_number;
8284   struct abbrev_info *abbrev;
8285
8286   hash_number = number % ABBREV_HASH_SIZE;
8287   abbrev = cu->dwarf2_abbrevs[hash_number];
8288
8289   while (abbrev)
8290     {
8291       if (abbrev->number == number)
8292         return abbrev;
8293       else
8294         abbrev = abbrev->next;
8295     }
8296   return NULL;
8297 }
8298
8299 /* Returns nonzero if TAG represents a type that we might generate a partial
8300    symbol for.  */
8301
8302 static int
8303 is_type_tag_for_partial (int tag)
8304 {
8305   switch (tag)
8306     {
8307 #if 0
8308     /* Some types that would be reasonable to generate partial symbols for,
8309        that we don't at present.  */
8310     case DW_TAG_array_type:
8311     case DW_TAG_file_type:
8312     case DW_TAG_ptr_to_member_type:
8313     case DW_TAG_set_type:
8314     case DW_TAG_string_type:
8315     case DW_TAG_subroutine_type:
8316 #endif
8317     case DW_TAG_base_type:
8318     case DW_TAG_class_type:
8319     case DW_TAG_interface_type:
8320     case DW_TAG_enumeration_type:
8321     case DW_TAG_structure_type:
8322     case DW_TAG_subrange_type:
8323     case DW_TAG_typedef:
8324     case DW_TAG_union_type:
8325       return 1;
8326     default:
8327       return 0;
8328     }
8329 }
8330
8331 /* Load all DIEs that are interesting for partial symbols into memory.  */
8332
8333 static struct partial_die_info *
8334 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8335                    int building_psymtab, struct dwarf2_cu *cu)
8336 {
8337   struct partial_die_info *part_die;
8338   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8339   struct abbrev_info *abbrev;
8340   unsigned int bytes_read;
8341   unsigned int load_all = 0;
8342
8343   int nesting_level = 1;
8344
8345   parent_die = NULL;
8346   last_die = NULL;
8347
8348   if (cu->per_cu && cu->per_cu->load_all_dies)
8349     load_all = 1;
8350
8351   cu->partial_dies
8352     = htab_create_alloc_ex (cu->header.length / 12,
8353                             partial_die_hash,
8354                             partial_die_eq,
8355                             NULL,
8356                             &cu->comp_unit_obstack,
8357                             hashtab_obstack_allocate,
8358                             dummy_obstack_deallocate);
8359
8360   part_die = obstack_alloc (&cu->comp_unit_obstack,
8361                             sizeof (struct partial_die_info));
8362
8363   while (1)
8364     {
8365       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8366
8367       /* A NULL abbrev means the end of a series of children.  */
8368       if (abbrev == NULL)
8369         {
8370           if (--nesting_level == 0)
8371             {
8372               /* PART_DIE was probably the last thing allocated on the
8373                  comp_unit_obstack, so we could call obstack_free
8374                  here.  We don't do that because the waste is small,
8375                  and will be cleaned up when we're done with this
8376                  compilation unit.  This way, we're also more robust
8377                  against other users of the comp_unit_obstack.  */
8378               return first_die;
8379             }
8380           info_ptr += bytes_read;
8381           last_die = parent_die;
8382           parent_die = parent_die->die_parent;
8383           continue;
8384         }
8385
8386       /* Check for template arguments.  We never save these; if
8387          they're seen, we just mark the parent, and go on our way.  */
8388       if (parent_die != NULL
8389           && cu->language == language_cplus
8390           && (abbrev->tag == DW_TAG_template_type_param
8391               || abbrev->tag == DW_TAG_template_value_param))
8392         {
8393           parent_die->has_template_arguments = 1;
8394
8395           if (!load_all)
8396             {
8397               /* We don't need a partial DIE for the template argument.  */
8398               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8399                                        cu);
8400               continue;
8401             }
8402         }
8403
8404       /* We only recurse into subprograms looking for template arguments.
8405          Skip their other children.  */
8406       if (!load_all
8407           && cu->language == language_cplus
8408           && parent_die != NULL
8409           && parent_die->tag == DW_TAG_subprogram)
8410         {
8411           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8412           continue;
8413         }
8414
8415       /* Check whether this DIE is interesting enough to save.  Normally
8416          we would not be interested in members here, but there may be
8417          later variables referencing them via DW_AT_specification (for
8418          static members).  */
8419       if (!load_all
8420           && !is_type_tag_for_partial (abbrev->tag)
8421           && abbrev->tag != DW_TAG_enumerator
8422           && abbrev->tag != DW_TAG_subprogram
8423           && abbrev->tag != DW_TAG_lexical_block
8424           && abbrev->tag != DW_TAG_variable
8425           && abbrev->tag != DW_TAG_namespace
8426           && abbrev->tag != DW_TAG_module
8427           && abbrev->tag != DW_TAG_member)
8428         {
8429           /* Otherwise we skip to the next sibling, if any.  */
8430           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8431           continue;
8432         }
8433
8434       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8435                                    buffer, info_ptr, cu);
8436
8437       /* This two-pass algorithm for processing partial symbols has a
8438          high cost in cache pressure.  Thus, handle some simple cases
8439          here which cover the majority of C partial symbols.  DIEs
8440          which neither have specification tags in them, nor could have
8441          specification tags elsewhere pointing at them, can simply be
8442          processed and discarded.
8443
8444          This segment is also optional; scan_partial_symbols and
8445          add_partial_symbol will handle these DIEs if we chain
8446          them in normally.  When compilers which do not emit large
8447          quantities of duplicate debug information are more common,
8448          this code can probably be removed.  */
8449
8450       /* Any complete simple types at the top level (pretty much all
8451          of them, for a language without namespaces), can be processed
8452          directly.  */
8453       if (parent_die == NULL
8454           && part_die->has_specification == 0
8455           && part_die->is_declaration == 0
8456           && (part_die->tag == DW_TAG_typedef
8457               || part_die->tag == DW_TAG_base_type
8458               || part_die->tag == DW_TAG_subrange_type))
8459         {
8460           if (building_psymtab && part_die->name != NULL)
8461             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8462                                  VAR_DOMAIN, LOC_TYPEDEF,
8463                                  &cu->objfile->static_psymbols,
8464                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8465           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8466           continue;
8467         }
8468
8469       /* If we're at the second level, and we're an enumerator, and
8470          our parent has no specification (meaning possibly lives in a
8471          namespace elsewhere), then we can add the partial symbol now
8472          instead of queueing it.  */
8473       if (part_die->tag == DW_TAG_enumerator
8474           && parent_die != NULL
8475           && parent_die->die_parent == NULL
8476           && parent_die->tag == DW_TAG_enumeration_type
8477           && parent_die->has_specification == 0)
8478         {
8479           if (part_die->name == NULL)
8480             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8481           else if (building_psymtab)
8482             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8483                                  VAR_DOMAIN, LOC_CONST,
8484                                  (cu->language == language_cplus
8485                                   || cu->language == language_java)
8486                                  ? &cu->objfile->global_psymbols
8487                                  : &cu->objfile->static_psymbols,
8488                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8489
8490           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8491           continue;
8492         }
8493
8494       /* We'll save this DIE so link it in.  */
8495       part_die->die_parent = parent_die;
8496       part_die->die_sibling = NULL;
8497       part_die->die_child = NULL;
8498
8499       if (last_die && last_die == parent_die)
8500         last_die->die_child = part_die;
8501       else if (last_die)
8502         last_die->die_sibling = part_die;
8503
8504       last_die = part_die;
8505
8506       if (first_die == NULL)
8507         first_die = part_die;
8508
8509       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8510          find interesting need to be in the hash table, because we
8511          also have the parent/sibling/child chains; only those that we
8512          might refer to by offset later during partial symbol reading.
8513
8514          For now this means things that might have be the target of a
8515          DW_AT_specification, DW_AT_abstract_origin, or
8516          DW_AT_extension.  DW_AT_extension will refer only to
8517          namespaces; DW_AT_abstract_origin refers to functions (and
8518          many things under the function DIE, but we do not recurse
8519          into function DIEs during partial symbol reading) and
8520          possibly variables as well; DW_AT_specification refers to
8521          declarations.  Declarations ought to have the DW_AT_declaration
8522          flag.  It happens that GCC forgets to put it in sometimes, but
8523          only for functions, not for types.
8524
8525          Adding more things than necessary to the hash table is harmless
8526          except for the performance cost.  Adding too few will result in
8527          wasted time in find_partial_die, when we reread the compilation
8528          unit with load_all_dies set.  */
8529
8530       if (load_all
8531           || abbrev->tag == DW_TAG_subprogram
8532           || abbrev->tag == DW_TAG_variable
8533           || abbrev->tag == DW_TAG_namespace
8534           || part_die->is_declaration)
8535         {
8536           void **slot;
8537
8538           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8539                                            part_die->offset, INSERT);
8540           *slot = part_die;
8541         }
8542
8543       part_die = obstack_alloc (&cu->comp_unit_obstack,
8544                                 sizeof (struct partial_die_info));
8545
8546       /* For some DIEs we want to follow their children (if any).  For C
8547          we have no reason to follow the children of structures; for other
8548          languages we have to, so that we can get at method physnames
8549          to infer fully qualified class names, for DW_AT_specification,
8550          and for C++ template arguments.  For C++, we also look one level
8551          inside functions to find template arguments (if the name of the
8552          function does not already contain the template arguments).
8553
8554          For Ada, we need to scan the children of subprograms and lexical
8555          blocks as well because Ada allows the definition of nested
8556          entities that could be interesting for the debugger, such as
8557          nested subprograms for instance.  */
8558       if (last_die->has_children
8559           && (load_all
8560               || last_die->tag == DW_TAG_namespace
8561               || last_die->tag == DW_TAG_module
8562               || last_die->tag == DW_TAG_enumeration_type
8563               || (cu->language == language_cplus
8564                   && last_die->tag == DW_TAG_subprogram
8565                   && (last_die->name == NULL
8566                       || strchr (last_die->name, '<') == NULL))
8567               || (cu->language != language_c
8568                   && (last_die->tag == DW_TAG_class_type
8569                       || last_die->tag == DW_TAG_interface_type
8570                       || last_die->tag == DW_TAG_structure_type
8571                       || last_die->tag == DW_TAG_union_type))
8572               || (cu->language == language_ada
8573                   && (last_die->tag == DW_TAG_subprogram
8574                       || last_die->tag == DW_TAG_lexical_block))))
8575         {
8576           nesting_level++;
8577           parent_die = last_die;
8578           continue;
8579         }
8580
8581       /* Otherwise we skip to the next sibling, if any.  */
8582       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8583
8584       /* Back to the top, do it again.  */
8585     }
8586 }
8587
8588 /* Read a minimal amount of information into the minimal die structure.  */
8589
8590 static gdb_byte *
8591 read_partial_die (struct partial_die_info *part_die,
8592                   struct abbrev_info *abbrev,
8593                   unsigned int abbrev_len, bfd *abfd,
8594                   gdb_byte *buffer, gdb_byte *info_ptr,
8595                   struct dwarf2_cu *cu)
8596 {
8597   unsigned int i;
8598   struct attribute attr;
8599   int has_low_pc_attr = 0;
8600   int has_high_pc_attr = 0;
8601
8602   memset (part_die, 0, sizeof (struct partial_die_info));
8603
8604   part_die->offset = info_ptr - buffer;
8605
8606   info_ptr += abbrev_len;
8607
8608   if (abbrev == NULL)
8609     return info_ptr;
8610
8611   part_die->tag = abbrev->tag;
8612   part_die->has_children = abbrev->has_children;
8613
8614   for (i = 0; i < abbrev->num_attrs; ++i)
8615     {
8616       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8617
8618       /* Store the data if it is of an attribute we want to keep in a
8619          partial symbol table.  */
8620       switch (attr.name)
8621         {
8622         case DW_AT_name:
8623           switch (part_die->tag)
8624             {
8625             case DW_TAG_compile_unit:
8626             case DW_TAG_type_unit:
8627               /* Compilation units have a DW_AT_name that is a filename, not
8628                  a source language identifier.  */
8629             case DW_TAG_enumeration_type:
8630             case DW_TAG_enumerator:
8631               /* These tags always have simple identifiers already; no need
8632                  to canonicalize them.  */
8633               part_die->name = DW_STRING (&attr);
8634               break;
8635             default:
8636               part_die->name
8637                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8638                                             &cu->objfile->objfile_obstack);
8639               break;
8640             }
8641           break;
8642         case DW_AT_linkage_name:
8643         case DW_AT_MIPS_linkage_name:
8644           /* Note that both forms of linkage name might appear.  We
8645              assume they will be the same, and we only store the last
8646              one we see.  */
8647           if (cu->language == language_ada)
8648             part_die->name = DW_STRING (&attr);
8649           break;
8650         case DW_AT_low_pc:
8651           has_low_pc_attr = 1;
8652           part_die->lowpc = DW_ADDR (&attr);
8653           break;
8654         case DW_AT_high_pc:
8655           has_high_pc_attr = 1;
8656           part_die->highpc = DW_ADDR (&attr);
8657           break;
8658         case DW_AT_location:
8659           /* Support the .debug_loc offsets */
8660           if (attr_form_is_block (&attr))
8661             {
8662                part_die->locdesc = DW_BLOCK (&attr);
8663             }
8664           else if (attr_form_is_section_offset (&attr))
8665             {
8666               dwarf2_complex_location_expr_complaint ();
8667             }
8668           else
8669             {
8670               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8671                                                      "partial symbol information");
8672             }
8673           break;
8674         case DW_AT_external:
8675           part_die->is_external = DW_UNSND (&attr);
8676           break;
8677         case DW_AT_declaration:
8678           part_die->is_declaration = DW_UNSND (&attr);
8679           break;
8680         case DW_AT_type:
8681           part_die->has_type = 1;
8682           break;
8683         case DW_AT_abstract_origin:
8684         case DW_AT_specification:
8685         case DW_AT_extension:
8686           part_die->has_specification = 1;
8687           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8688           break;
8689         case DW_AT_sibling:
8690           /* Ignore absolute siblings, they might point outside of
8691              the current compile unit.  */
8692           if (attr.form == DW_FORM_ref_addr)
8693             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8694           else
8695             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8696           break;
8697         case DW_AT_byte_size:
8698           part_die->has_byte_size = 1;
8699           break;
8700         case DW_AT_calling_convention:
8701           /* DWARF doesn't provide a way to identify a program's source-level
8702              entry point.  DW_AT_calling_convention attributes are only meant
8703              to describe functions' calling conventions.
8704
8705              However, because it's a necessary piece of information in
8706              Fortran, and because DW_CC_program is the only piece of debugging
8707              information whose definition refers to a 'main program' at all,
8708              several compilers have begun marking Fortran main programs with
8709              DW_CC_program --- even when those functions use the standard
8710              calling conventions.
8711
8712              So until DWARF specifies a way to provide this information and
8713              compilers pick up the new representation, we'll support this
8714              practice.  */
8715           if (DW_UNSND (&attr) == DW_CC_program
8716               && cu->language == language_fortran)
8717             set_main_name (part_die->name);
8718           break;
8719         default:
8720           break;
8721         }
8722     }
8723
8724   /* When using the GNU linker, .gnu.linkonce. sections are used to
8725      eliminate duplicate copies of functions and vtables and such.
8726      The linker will arbitrarily choose one and discard the others.
8727      The AT_*_pc values for such functions refer to local labels in
8728      these sections.  If the section from that file was discarded, the
8729      labels are not in the output, so the relocs get a value of 0.
8730      If this is a discarded function, mark the pc bounds as invalid,
8731      so that GDB will ignore it.  */
8732   if (has_low_pc_attr && has_high_pc_attr
8733       && part_die->lowpc < part_die->highpc
8734       && (part_die->lowpc != 0
8735           || dwarf2_per_objfile->has_section_at_zero))
8736     part_die->has_pc_info = 1;
8737
8738   return info_ptr;
8739 }
8740
8741 /* Find a cached partial DIE at OFFSET in CU.  */
8742
8743 static struct partial_die_info *
8744 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8745 {
8746   struct partial_die_info *lookup_die = NULL;
8747   struct partial_die_info part_die;
8748
8749   part_die.offset = offset;
8750   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8751
8752   return lookup_die;
8753 }
8754
8755 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8756    except in the case of .debug_types DIEs which do not reference
8757    outside their CU (they do however referencing other types via
8758    DW_FORM_sig8).  */
8759
8760 static struct partial_die_info *
8761 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8762 {
8763   struct dwarf2_per_cu_data *per_cu = NULL;
8764   struct partial_die_info *pd = NULL;
8765
8766   if (cu->per_cu->from_debug_types)
8767     {
8768       pd = find_partial_die_in_comp_unit (offset, cu);
8769       if (pd != NULL)
8770         return pd;
8771       goto not_found;
8772     }
8773
8774   if (offset_in_cu_p (&cu->header, offset))
8775     {
8776       pd = find_partial_die_in_comp_unit (offset, cu);
8777       if (pd != NULL)
8778         return pd;
8779     }
8780
8781   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8782
8783   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8784     load_partial_comp_unit (per_cu, cu->objfile);
8785
8786   per_cu->cu->last_used = 0;
8787   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8788
8789   if (pd == NULL && per_cu->load_all_dies == 0)
8790     {
8791       struct cleanup *back_to;
8792       struct partial_die_info comp_unit_die;
8793       struct abbrev_info *abbrev;
8794       unsigned int bytes_read;
8795       char *info_ptr;
8796
8797       per_cu->load_all_dies = 1;
8798
8799       /* Re-read the DIEs.  */
8800       back_to = make_cleanup (null_cleanup, 0);
8801       if (per_cu->cu->dwarf2_abbrevs == NULL)
8802         {
8803           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8804           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8805         }
8806       info_ptr = (dwarf2_per_objfile->info.buffer
8807                   + per_cu->cu->header.offset
8808                   + per_cu->cu->header.first_die_offset);
8809       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8810       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8811                                    per_cu->cu->objfile->obfd,
8812                                    dwarf2_per_objfile->info.buffer, info_ptr,
8813                                    per_cu->cu);
8814       if (comp_unit_die.has_children)
8815         load_partial_dies (per_cu->cu->objfile->obfd,
8816                            dwarf2_per_objfile->info.buffer, info_ptr,
8817                            0, per_cu->cu);
8818       do_cleanups (back_to);
8819
8820       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8821     }
8822
8823  not_found:
8824
8825   if (pd == NULL)
8826     internal_error (__FILE__, __LINE__,
8827                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8828                     offset, bfd_get_filename (cu->objfile->obfd));
8829   return pd;
8830 }
8831
8832 /* Adjust PART_DIE before generating a symbol for it.  This function
8833    may set the is_external flag or change the DIE's name.  */
8834
8835 static void
8836 fixup_partial_die (struct partial_die_info *part_die,
8837                    struct dwarf2_cu *cu)
8838 {
8839   /* If we found a reference attribute and the DIE has no name, try
8840      to find a name in the referred to DIE.  */
8841
8842   if (part_die->name == NULL && part_die->has_specification)
8843     {
8844       struct partial_die_info *spec_die;
8845
8846       spec_die = find_partial_die (part_die->spec_offset, cu);
8847
8848       fixup_partial_die (spec_die, cu);
8849
8850       if (spec_die->name)
8851         {
8852           part_die->name = spec_die->name;
8853
8854           /* Copy DW_AT_external attribute if it is set.  */
8855           if (spec_die->is_external)
8856             part_die->is_external = spec_die->is_external;
8857         }
8858     }
8859
8860   /* Set default names for some unnamed DIEs.  */
8861   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8862                                  || part_die->tag == DW_TAG_class_type))
8863     part_die->name = "(anonymous class)";
8864
8865   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8866     part_die->name = "(anonymous namespace)";
8867
8868   if (part_die->tag == DW_TAG_structure_type
8869       || part_die->tag == DW_TAG_class_type
8870       || part_die->tag == DW_TAG_union_type)
8871     guess_structure_name (part_die, cu);
8872 }
8873
8874 /* Read an attribute value described by an attribute form.  */
8875
8876 static gdb_byte *
8877 read_attribute_value (struct attribute *attr, unsigned form,
8878                       bfd *abfd, gdb_byte *info_ptr,
8879                       struct dwarf2_cu *cu)
8880 {
8881   struct comp_unit_head *cu_header = &cu->header;
8882   unsigned int bytes_read;
8883   struct dwarf_block *blk;
8884
8885   attr->form = form;
8886   switch (form)
8887     {
8888     case DW_FORM_ref_addr:
8889       if (cu->header.version == 2)
8890         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8891       else
8892         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8893       info_ptr += bytes_read;
8894       break;
8895     case DW_FORM_addr:
8896       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8897       info_ptr += bytes_read;
8898       break;
8899     case DW_FORM_block2:
8900       blk = dwarf_alloc_block (cu);
8901       blk->size = read_2_bytes (abfd, info_ptr);
8902       info_ptr += 2;
8903       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8904       info_ptr += blk->size;
8905       DW_BLOCK (attr) = blk;
8906       break;
8907     case DW_FORM_block4:
8908       blk = dwarf_alloc_block (cu);
8909       blk->size = read_4_bytes (abfd, info_ptr);
8910       info_ptr += 4;
8911       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8912       info_ptr += blk->size;
8913       DW_BLOCK (attr) = blk;
8914       break;
8915     case DW_FORM_data2:
8916       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8917       info_ptr += 2;
8918       break;
8919     case DW_FORM_data4:
8920       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8921       info_ptr += 4;
8922       break;
8923     case DW_FORM_data8:
8924       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8925       info_ptr += 8;
8926       break;
8927     case DW_FORM_sec_offset:
8928       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8929       info_ptr += bytes_read;
8930       break;
8931     case DW_FORM_string:
8932       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8933       DW_STRING_IS_CANONICAL (attr) = 0;
8934       info_ptr += bytes_read;
8935       break;
8936     case DW_FORM_strp:
8937       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8938                                                &bytes_read);
8939       DW_STRING_IS_CANONICAL (attr) = 0;
8940       info_ptr += bytes_read;
8941       break;
8942     case DW_FORM_exprloc:
8943     case DW_FORM_block:
8944       blk = dwarf_alloc_block (cu);
8945       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8946       info_ptr += bytes_read;
8947       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8948       info_ptr += blk->size;
8949       DW_BLOCK (attr) = blk;
8950       break;
8951     case DW_FORM_block1:
8952       blk = dwarf_alloc_block (cu);
8953       blk->size = read_1_byte (abfd, info_ptr);
8954       info_ptr += 1;
8955       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8956       info_ptr += blk->size;
8957       DW_BLOCK (attr) = blk;
8958       break;
8959     case DW_FORM_data1:
8960       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8961       info_ptr += 1;
8962       break;
8963     case DW_FORM_flag:
8964       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8965       info_ptr += 1;
8966       break;
8967     case DW_FORM_flag_present:
8968       DW_UNSND (attr) = 1;
8969       break;
8970     case DW_FORM_sdata:
8971       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8972       info_ptr += bytes_read;
8973       break;
8974     case DW_FORM_udata:
8975       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8976       info_ptr += bytes_read;
8977       break;
8978     case DW_FORM_ref1:
8979       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
8980       info_ptr += 1;
8981       break;
8982     case DW_FORM_ref2:
8983       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
8984       info_ptr += 2;
8985       break;
8986     case DW_FORM_ref4:
8987       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
8988       info_ptr += 4;
8989       break;
8990     case DW_FORM_ref8:
8991       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
8992       info_ptr += 8;
8993       break;
8994     case DW_FORM_sig8:
8995       /* Convert the signature to something we can record in DW_UNSND
8996          for later lookup.
8997          NOTE: This is NULL if the type wasn't found.  */
8998       DW_SIGNATURED_TYPE (attr) =
8999         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9000       info_ptr += 8;
9001       break;
9002     case DW_FORM_ref_udata:
9003       DW_ADDR (attr) = (cu->header.offset
9004                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9005       info_ptr += bytes_read;
9006       break;
9007     case DW_FORM_indirect:
9008       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9009       info_ptr += bytes_read;
9010       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9011       break;
9012     default:
9013       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9014              dwarf_form_name (form),
9015              bfd_get_filename (abfd));
9016     }
9017
9018   /* We have seen instances where the compiler tried to emit a byte
9019      size attribute of -1 which ended up being encoded as an unsigned
9020      0xffffffff.  Although 0xffffffff is technically a valid size value,
9021      an object of this size seems pretty unlikely so we can relatively
9022      safely treat these cases as if the size attribute was invalid and
9023      treat them as zero by default.  */
9024   if (attr->name == DW_AT_byte_size
9025       && form == DW_FORM_data4
9026       && DW_UNSND (attr) >= 0xffffffff)
9027     {
9028       complaint
9029         (&symfile_complaints,
9030          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9031          hex_string (DW_UNSND (attr)));
9032       DW_UNSND (attr) = 0;
9033     }
9034
9035   return info_ptr;
9036 }
9037
9038 /* Read an attribute described by an abbreviated attribute.  */
9039
9040 static gdb_byte *
9041 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9042                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9043 {
9044   attr->name = abbrev->name;
9045   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9046 }
9047
9048 /* read dwarf information from a buffer */
9049
9050 static unsigned int
9051 read_1_byte (bfd *abfd, gdb_byte *buf)
9052 {
9053   return bfd_get_8 (abfd, buf);
9054 }
9055
9056 static int
9057 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9058 {
9059   return bfd_get_signed_8 (abfd, buf);
9060 }
9061
9062 static unsigned int
9063 read_2_bytes (bfd *abfd, gdb_byte *buf)
9064 {
9065   return bfd_get_16 (abfd, buf);
9066 }
9067
9068 static int
9069 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9070 {
9071   return bfd_get_signed_16 (abfd, buf);
9072 }
9073
9074 static unsigned int
9075 read_4_bytes (bfd *abfd, gdb_byte *buf)
9076 {
9077   return bfd_get_32 (abfd, buf);
9078 }
9079
9080 static int
9081 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9082 {
9083   return bfd_get_signed_32 (abfd, buf);
9084 }
9085
9086 static ULONGEST
9087 read_8_bytes (bfd *abfd, gdb_byte *buf)
9088 {
9089   return bfd_get_64 (abfd, buf);
9090 }
9091
9092 static CORE_ADDR
9093 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9094               unsigned int *bytes_read)
9095 {
9096   struct comp_unit_head *cu_header = &cu->header;
9097   CORE_ADDR retval = 0;
9098
9099   if (cu_header->signed_addr_p)
9100     {
9101       switch (cu_header->addr_size)
9102         {
9103         case 2:
9104           retval = bfd_get_signed_16 (abfd, buf);
9105           break;
9106         case 4:
9107           retval = bfd_get_signed_32 (abfd, buf);
9108           break;
9109         case 8:
9110           retval = bfd_get_signed_64 (abfd, buf);
9111           break;
9112         default:
9113           internal_error (__FILE__, __LINE__,
9114                           _("read_address: bad switch, signed [in module %s]"),
9115                           bfd_get_filename (abfd));
9116         }
9117     }
9118   else
9119     {
9120       switch (cu_header->addr_size)
9121         {
9122         case 2:
9123           retval = bfd_get_16 (abfd, buf);
9124           break;
9125         case 4:
9126           retval = bfd_get_32 (abfd, buf);
9127           break;
9128         case 8:
9129           retval = bfd_get_64 (abfd, buf);
9130           break;
9131         default:
9132           internal_error (__FILE__, __LINE__,
9133                           _("read_address: bad switch, unsigned [in module %s]"),
9134                           bfd_get_filename (abfd));
9135         }
9136     }
9137
9138   *bytes_read = cu_header->addr_size;
9139   return retval;
9140 }
9141
9142 /* Read the initial length from a section.  The (draft) DWARF 3
9143    specification allows the initial length to take up either 4 bytes
9144    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9145    bytes describe the length and all offsets will be 8 bytes in length
9146    instead of 4.
9147
9148    An older, non-standard 64-bit format is also handled by this
9149    function.  The older format in question stores the initial length
9150    as an 8-byte quantity without an escape value.  Lengths greater
9151    than 2^32 aren't very common which means that the initial 4 bytes
9152    is almost always zero.  Since a length value of zero doesn't make
9153    sense for the 32-bit format, this initial zero can be considered to
9154    be an escape value which indicates the presence of the older 64-bit
9155    format.  As written, the code can't detect (old format) lengths
9156    greater than 4GB.  If it becomes necessary to handle lengths
9157    somewhat larger than 4GB, we could allow other small values (such
9158    as the non-sensical values of 1, 2, and 3) to also be used as
9159    escape values indicating the presence of the old format.
9160
9161    The value returned via bytes_read should be used to increment the
9162    relevant pointer after calling read_initial_length().
9163
9164    [ Note:  read_initial_length() and read_offset() are based on the
9165      document entitled "DWARF Debugging Information Format", revision
9166      3, draft 8, dated November 19, 2001.  This document was obtained
9167      from:
9168
9169         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9170
9171      This document is only a draft and is subject to change.  (So beware.)
9172
9173      Details regarding the older, non-standard 64-bit format were
9174      determined empirically by examining 64-bit ELF files produced by
9175      the SGI toolchain on an IRIX 6.5 machine.
9176
9177      - Kevin, July 16, 2002
9178    ] */
9179
9180 static LONGEST
9181 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9182 {
9183   LONGEST length = bfd_get_32 (abfd, buf);
9184
9185   if (length == 0xffffffff)
9186     {
9187       length = bfd_get_64 (abfd, buf + 4);
9188       *bytes_read = 12;
9189     }
9190   else if (length == 0)
9191     {
9192       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9193       length = bfd_get_64 (abfd, buf);
9194       *bytes_read = 8;
9195     }
9196   else
9197     {
9198       *bytes_read = 4;
9199     }
9200
9201   return length;
9202 }
9203
9204 /* Cover function for read_initial_length.
9205    Returns the length of the object at BUF, and stores the size of the
9206    initial length in *BYTES_READ and stores the size that offsets will be in
9207    *OFFSET_SIZE.
9208    If the initial length size is not equivalent to that specified in
9209    CU_HEADER then issue a complaint.
9210    This is useful when reading non-comp-unit headers.  */
9211
9212 static LONGEST
9213 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9214                                         const struct comp_unit_head *cu_header,
9215                                         unsigned int *bytes_read,
9216                                         unsigned int *offset_size)
9217 {
9218   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9219
9220   gdb_assert (cu_header->initial_length_size == 4
9221               || cu_header->initial_length_size == 8
9222               || cu_header->initial_length_size == 12);
9223
9224   if (cu_header->initial_length_size != *bytes_read)
9225     complaint (&symfile_complaints,
9226                _("intermixed 32-bit and 64-bit DWARF sections"));
9227
9228   *offset_size = (*bytes_read == 4) ? 4 : 8;
9229   return length;
9230 }
9231
9232 /* Read an offset from the data stream.  The size of the offset is
9233    given by cu_header->offset_size.  */
9234
9235 static LONGEST
9236 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9237              unsigned int *bytes_read)
9238 {
9239   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9240
9241   *bytes_read = cu_header->offset_size;
9242   return offset;
9243 }
9244
9245 /* Read an offset from the data stream.  */
9246
9247 static LONGEST
9248 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9249 {
9250   LONGEST retval = 0;
9251
9252   switch (offset_size)
9253     {
9254     case 4:
9255       retval = bfd_get_32 (abfd, buf);
9256       break;
9257     case 8:
9258       retval = bfd_get_64 (abfd, buf);
9259       break;
9260     default:
9261       internal_error (__FILE__, __LINE__,
9262                       _("read_offset_1: bad switch [in module %s]"),
9263                       bfd_get_filename (abfd));
9264     }
9265
9266   return retval;
9267 }
9268
9269 static gdb_byte *
9270 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9271 {
9272   /* If the size of a host char is 8 bits, we can return a pointer
9273      to the buffer, otherwise we have to copy the data to a buffer
9274      allocated on the temporary obstack.  */
9275   gdb_assert (HOST_CHAR_BIT == 8);
9276   return buf;
9277 }
9278
9279 static char *
9280 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9281 {
9282   /* If the size of a host char is 8 bits, we can return a pointer
9283      to the string, otherwise we have to copy the string to a buffer
9284      allocated on the temporary obstack.  */
9285   gdb_assert (HOST_CHAR_BIT == 8);
9286   if (*buf == '\0')
9287     {
9288       *bytes_read_ptr = 1;
9289       return NULL;
9290     }
9291   *bytes_read_ptr = strlen ((char *) buf) + 1;
9292   return (char *) buf;
9293 }
9294
9295 static char *
9296 read_indirect_string (bfd *abfd, gdb_byte *buf,
9297                       const struct comp_unit_head *cu_header,
9298                       unsigned int *bytes_read_ptr)
9299 {
9300   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9301
9302   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9303   if (dwarf2_per_objfile->str.buffer == NULL)
9304     {
9305       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9306                       bfd_get_filename (abfd));
9307       return NULL;
9308     }
9309   if (str_offset >= dwarf2_per_objfile->str.size)
9310     {
9311       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9312                       bfd_get_filename (abfd));
9313       return NULL;
9314     }
9315   gdb_assert (HOST_CHAR_BIT == 8);
9316   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9317     return NULL;
9318   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9319 }
9320
9321 static unsigned long
9322 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9323 {
9324   unsigned long result;
9325   unsigned int num_read;
9326   int i, shift;
9327   unsigned char byte;
9328
9329   result = 0;
9330   shift = 0;
9331   num_read = 0;
9332   i = 0;
9333   while (1)
9334     {
9335       byte = bfd_get_8 (abfd, buf);
9336       buf++;
9337       num_read++;
9338       result |= ((unsigned long)(byte & 127) << shift);
9339       if ((byte & 128) == 0)
9340         {
9341           break;
9342         }
9343       shift += 7;
9344     }
9345   *bytes_read_ptr = num_read;
9346   return result;
9347 }
9348
9349 static long
9350 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9351 {
9352   long result;
9353   int i, shift, num_read;
9354   unsigned char byte;
9355
9356   result = 0;
9357   shift = 0;
9358   num_read = 0;
9359   i = 0;
9360   while (1)
9361     {
9362       byte = bfd_get_8 (abfd, buf);
9363       buf++;
9364       num_read++;
9365       result |= ((long)(byte & 127) << shift);
9366       shift += 7;
9367       if ((byte & 128) == 0)
9368         {
9369           break;
9370         }
9371     }
9372   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9373     result |= -(((long)1) << shift);
9374   *bytes_read_ptr = num_read;
9375   return result;
9376 }
9377
9378 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9379
9380 static gdb_byte *
9381 skip_leb128 (bfd *abfd, gdb_byte *buf)
9382 {
9383   int byte;
9384
9385   while (1)
9386     {
9387       byte = bfd_get_8 (abfd, buf);
9388       buf++;
9389       if ((byte & 128) == 0)
9390         return buf;
9391     }
9392 }
9393
9394 static void
9395 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9396 {
9397   switch (lang)
9398     {
9399     case DW_LANG_C89:
9400     case DW_LANG_C99:
9401     case DW_LANG_C:
9402       cu->language = language_c;
9403       break;
9404     case DW_LANG_C_plus_plus:
9405       cu->language = language_cplus;
9406       break;
9407     case DW_LANG_D:
9408       cu->language = language_d;
9409       break;
9410     case DW_LANG_Fortran77:
9411     case DW_LANG_Fortran90:
9412     case DW_LANG_Fortran95:
9413       cu->language = language_fortran;
9414       break;
9415     case DW_LANG_Mips_Assembler:
9416       cu->language = language_asm;
9417       break;
9418     case DW_LANG_Java:
9419       cu->language = language_java;
9420       break;
9421     case DW_LANG_Ada83:
9422     case DW_LANG_Ada95:
9423       cu->language = language_ada;
9424       break;
9425     case DW_LANG_Modula2:
9426       cu->language = language_m2;
9427       break;
9428     case DW_LANG_Pascal83:
9429       cu->language = language_pascal;
9430       break;
9431     case DW_LANG_ObjC:
9432       cu->language = language_objc;
9433       break;
9434     case DW_LANG_Cobol74:
9435     case DW_LANG_Cobol85:
9436     default:
9437       cu->language = language_minimal;
9438       break;
9439     }
9440   cu->language_defn = language_def (cu->language);
9441 }
9442
9443 /* Return the named attribute or NULL if not there.  */
9444
9445 static struct attribute *
9446 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9447 {
9448   unsigned int i;
9449   struct attribute *spec = NULL;
9450
9451   for (i = 0; i < die->num_attrs; ++i)
9452     {
9453       if (die->attrs[i].name == name)
9454         return &die->attrs[i];
9455       if (die->attrs[i].name == DW_AT_specification
9456           || die->attrs[i].name == DW_AT_abstract_origin)
9457         spec = &die->attrs[i];
9458     }
9459
9460   if (spec)
9461     {
9462       die = follow_die_ref (die, spec, &cu);
9463       return dwarf2_attr (die, name, cu);
9464     }
9465
9466   return NULL;
9467 }
9468
9469 /* Return the named attribute or NULL if not there,
9470    but do not follow DW_AT_specification, etc.
9471    This is for use in contexts where we're reading .debug_types dies.
9472    Following DW_AT_specification, DW_AT_abstract_origin will take us
9473    back up the chain, and we want to go down.  */
9474
9475 static struct attribute *
9476 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9477                        struct dwarf2_cu *cu)
9478 {
9479   unsigned int i;
9480
9481   for (i = 0; i < die->num_attrs; ++i)
9482     if (die->attrs[i].name == name)
9483       return &die->attrs[i];
9484
9485   return NULL;
9486 }
9487
9488 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9489    and holds a non-zero value.  This function should only be used for
9490    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9491
9492 static int
9493 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9494 {
9495   struct attribute *attr = dwarf2_attr (die, name, cu);
9496
9497   return (attr && DW_UNSND (attr));
9498 }
9499
9500 static int
9501 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9502 {
9503   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9504      which value is non-zero.  However, we have to be careful with
9505      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9506      (via dwarf2_flag_true_p) follows this attribute.  So we may
9507      end up accidently finding a declaration attribute that belongs
9508      to a different DIE referenced by the specification attribute,
9509      even though the given DIE does not have a declaration attribute.  */
9510   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9511           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9512 }
9513
9514 /* Return the die giving the specification for DIE, if there is
9515    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9516    containing the return value on output.  If there is no
9517    specification, but there is an abstract origin, that is
9518    returned.  */
9519
9520 static struct die_info *
9521 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9522 {
9523   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9524                                              *spec_cu);
9525
9526   if (spec_attr == NULL)
9527     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9528
9529   if (spec_attr == NULL)
9530     return NULL;
9531   else
9532     return follow_die_ref (die, spec_attr, spec_cu);
9533 }
9534
9535 /* Free the line_header structure *LH, and any arrays and strings it
9536    refers to.  */
9537 static void
9538 free_line_header (struct line_header *lh)
9539 {
9540   if (lh->standard_opcode_lengths)
9541     xfree (lh->standard_opcode_lengths);
9542
9543   /* Remember that all the lh->file_names[i].name pointers are
9544      pointers into debug_line_buffer, and don't need to be freed.  */
9545   if (lh->file_names)
9546     xfree (lh->file_names);
9547
9548   /* Similarly for the include directory names.  */
9549   if (lh->include_dirs)
9550     xfree (lh->include_dirs);
9551
9552   xfree (lh);
9553 }
9554
9555
9556 /* Add an entry to LH's include directory table.  */
9557 static void
9558 add_include_dir (struct line_header *lh, char *include_dir)
9559 {
9560   /* Grow the array if necessary.  */
9561   if (lh->include_dirs_size == 0)
9562     {
9563       lh->include_dirs_size = 1; /* for testing */
9564       lh->include_dirs = xmalloc (lh->include_dirs_size
9565                                   * sizeof (*lh->include_dirs));
9566     }
9567   else if (lh->num_include_dirs >= lh->include_dirs_size)
9568     {
9569       lh->include_dirs_size *= 2;
9570       lh->include_dirs = xrealloc (lh->include_dirs,
9571                                    (lh->include_dirs_size
9572                                     * sizeof (*lh->include_dirs)));
9573     }
9574
9575   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9576 }
9577
9578
9579 /* Add an entry to LH's file name table.  */
9580 static void
9581 add_file_name (struct line_header *lh,
9582                char *name,
9583                unsigned int dir_index,
9584                unsigned int mod_time,
9585                unsigned int length)
9586 {
9587   struct file_entry *fe;
9588
9589   /* Grow the array if necessary.  */
9590   if (lh->file_names_size == 0)
9591     {
9592       lh->file_names_size = 1; /* for testing */
9593       lh->file_names = xmalloc (lh->file_names_size
9594                                 * sizeof (*lh->file_names));
9595     }
9596   else if (lh->num_file_names >= lh->file_names_size)
9597     {
9598       lh->file_names_size *= 2;
9599       lh->file_names = xrealloc (lh->file_names,
9600                                  (lh->file_names_size
9601                                   * sizeof (*lh->file_names)));
9602     }
9603
9604   fe = &lh->file_names[lh->num_file_names++];
9605   fe->name = name;
9606   fe->dir_index = dir_index;
9607   fe->mod_time = mod_time;
9608   fe->length = length;
9609   fe->included_p = 0;
9610   fe->symtab = NULL;
9611 }
9612
9613
9614 /* Read the statement program header starting at OFFSET in
9615    .debug_line, according to the endianness of ABFD.  Return a pointer
9616    to a struct line_header, allocated using xmalloc.
9617
9618    NOTE: the strings in the include directory and file name tables of
9619    the returned object point into debug_line_buffer, and must not be
9620    freed.  */
9621 static struct line_header *
9622 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9623                           struct dwarf2_cu *cu)
9624 {
9625   struct cleanup *back_to;
9626   struct line_header *lh;
9627   gdb_byte *line_ptr;
9628   unsigned int bytes_read, offset_size;
9629   int i;
9630   char *cur_dir, *cur_file;
9631
9632   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9633   if (dwarf2_per_objfile->line.buffer == NULL)
9634     {
9635       complaint (&symfile_complaints, _("missing .debug_line section"));
9636       return 0;
9637     }
9638
9639   /* Make sure that at least there's room for the total_length field.
9640      That could be 12 bytes long, but we're just going to fudge that.  */
9641   if (offset + 4 >= dwarf2_per_objfile->line.size)
9642     {
9643       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9644       return 0;
9645     }
9646
9647   lh = xmalloc (sizeof (*lh));
9648   memset (lh, 0, sizeof (*lh));
9649   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9650                           (void *) lh);
9651
9652   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9653
9654   /* Read in the header.  */
9655   lh->total_length =
9656     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9657                                             &bytes_read, &offset_size);
9658   line_ptr += bytes_read;
9659   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9660                                      + dwarf2_per_objfile->line.size))
9661     {
9662       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9663       return 0;
9664     }
9665   lh->statement_program_end = line_ptr + lh->total_length;
9666   lh->version = read_2_bytes (abfd, line_ptr);
9667   line_ptr += 2;
9668   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9669   line_ptr += offset_size;
9670   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9671   line_ptr += 1;
9672   if (lh->version >= 4)
9673     {
9674       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9675       line_ptr += 1;
9676     }
9677   else
9678     lh->maximum_ops_per_instruction = 1;
9679
9680   if (lh->maximum_ops_per_instruction == 0)
9681     {
9682       lh->maximum_ops_per_instruction = 1;
9683       complaint (&symfile_complaints,
9684                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9685     }
9686
9687   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9688   line_ptr += 1;
9689   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9690   line_ptr += 1;
9691   lh->line_range = read_1_byte (abfd, line_ptr);
9692   line_ptr += 1;
9693   lh->opcode_base = read_1_byte (abfd, line_ptr);
9694   line_ptr += 1;
9695   lh->standard_opcode_lengths
9696     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9697
9698   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9699   for (i = 1; i < lh->opcode_base; ++i)
9700     {
9701       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9702       line_ptr += 1;
9703     }
9704
9705   /* Read directory table.  */
9706   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9707     {
9708       line_ptr += bytes_read;
9709       add_include_dir (lh, cur_dir);
9710     }
9711   line_ptr += bytes_read;
9712
9713   /* Read file name table.  */
9714   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9715     {
9716       unsigned int dir_index, mod_time, length;
9717
9718       line_ptr += bytes_read;
9719       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9720       line_ptr += bytes_read;
9721       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9722       line_ptr += bytes_read;
9723       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9724       line_ptr += bytes_read;
9725
9726       add_file_name (lh, cur_file, dir_index, mod_time, length);
9727     }
9728   line_ptr += bytes_read;
9729   lh->statement_program_start = line_ptr;
9730
9731   if (line_ptr > (dwarf2_per_objfile->line.buffer
9732                   + dwarf2_per_objfile->line.size))
9733     complaint (&symfile_complaints,
9734                _("line number info header doesn't fit in `.debug_line' section"));
9735
9736   discard_cleanups (back_to);
9737   return lh;
9738 }
9739
9740 /* This function exists to work around a bug in certain compilers
9741    (particularly GCC 2.95), in which the first line number marker of a
9742    function does not show up until after the prologue, right before
9743    the second line number marker.  This function shifts ADDRESS down
9744    to the beginning of the function if necessary, and is called on
9745    addresses passed to record_line.  */
9746
9747 static CORE_ADDR
9748 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9749 {
9750   struct function_range *fn;
9751
9752   /* Find the function_range containing address.  */
9753   if (!cu->first_fn)
9754     return address;
9755
9756   if (!cu->cached_fn)
9757     cu->cached_fn = cu->first_fn;
9758
9759   fn = cu->cached_fn;
9760   while (fn)
9761     if (fn->lowpc <= address && fn->highpc > address)
9762       goto found;
9763     else
9764       fn = fn->next;
9765
9766   fn = cu->first_fn;
9767   while (fn && fn != cu->cached_fn)
9768     if (fn->lowpc <= address && fn->highpc > address)
9769       goto found;
9770     else
9771       fn = fn->next;
9772
9773   return address;
9774
9775  found:
9776   if (fn->seen_line)
9777     return address;
9778   if (address != fn->lowpc)
9779     complaint (&symfile_complaints,
9780                _("misplaced first line number at 0x%lx for '%s'"),
9781                (unsigned long) address, fn->name);
9782   fn->seen_line = 1;
9783   return fn->lowpc;
9784 }
9785
9786 /* Decode the Line Number Program (LNP) for the given line_header
9787    structure and CU.  The actual information extracted and the type
9788    of structures created from the LNP depends on the value of PST.
9789
9790    1. If PST is NULL, then this procedure uses the data from the program
9791       to create all necessary symbol tables, and their linetables.
9792       The compilation directory of the file is passed in COMP_DIR,
9793       and must not be NULL.
9794
9795    2. If PST is not NULL, this procedure reads the program to determine
9796       the list of files included by the unit represented by PST, and
9797       builds all the associated partial symbol tables.  In this case,
9798       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9799       is not used to compute the full name of the symtab, and therefore
9800       omitting it when building the partial symtab does not introduce
9801       the potential for inconsistency - a partial symtab and its associated
9802       symbtab having a different fullname -).  */
9803
9804 static void
9805 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9806                     struct dwarf2_cu *cu, struct partial_symtab *pst)
9807 {
9808   gdb_byte *line_ptr, *extended_end;
9809   gdb_byte *line_end;
9810   unsigned int bytes_read, extended_len;
9811   unsigned char op_code, extended_op, adj_opcode;
9812   CORE_ADDR baseaddr;
9813   struct objfile *objfile = cu->objfile;
9814   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9815   const int decode_for_pst_p = (pst != NULL);
9816   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9817
9818   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9819
9820   line_ptr = lh->statement_program_start;
9821   line_end = lh->statement_program_end;
9822
9823   /* Read the statement sequences until there's nothing left.  */
9824   while (line_ptr < line_end)
9825     {
9826       /* state machine registers  */
9827       CORE_ADDR address = 0;
9828       unsigned int file = 1;
9829       unsigned int line = 1;
9830       unsigned int column = 0;
9831       int is_stmt = lh->default_is_stmt;
9832       int basic_block = 0;
9833       int end_sequence = 0;
9834       CORE_ADDR addr;
9835       unsigned char op_index = 0;
9836
9837       if (!decode_for_pst_p && lh->num_file_names >= file)
9838         {
9839           /* Start a subfile for the current file of the state machine.  */
9840           /* lh->include_dirs and lh->file_names are 0-based, but the
9841              directory and file name numbers in the statement program
9842              are 1-based.  */
9843           struct file_entry *fe = &lh->file_names[file - 1];
9844           char *dir = NULL;
9845
9846           if (fe->dir_index)
9847             dir = lh->include_dirs[fe->dir_index - 1];
9848
9849           dwarf2_start_subfile (fe->name, dir, comp_dir);
9850         }
9851
9852       /* Decode the table.  */
9853       while (!end_sequence)
9854         {
9855           op_code = read_1_byte (abfd, line_ptr);
9856           line_ptr += 1;
9857           if (line_ptr > line_end)
9858             {
9859               dwarf2_debug_line_missing_end_sequence_complaint ();
9860               break;
9861             }
9862
9863           if (op_code >= lh->opcode_base)
9864             {
9865               /* Special operand.  */
9866               adj_opcode = op_code - lh->opcode_base;
9867               address += (((op_index + (adj_opcode / lh->line_range))
9868                            / lh->maximum_ops_per_instruction)
9869                           * lh->minimum_instruction_length);
9870               op_index = ((op_index + (adj_opcode / lh->line_range))
9871                           % lh->maximum_ops_per_instruction);
9872               line += lh->line_base + (adj_opcode % lh->line_range);
9873               if (lh->num_file_names < file || file == 0)
9874                 dwarf2_debug_line_missing_file_complaint ();
9875               /* For now we ignore lines not starting on an
9876                  instruction boundary.  */
9877               else if (op_index == 0)
9878                 {
9879                   lh->file_names[file - 1].included_p = 1;
9880                   if (!decode_for_pst_p && is_stmt)
9881                     {
9882                       if (last_subfile != current_subfile)
9883                         {
9884                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9885                           if (last_subfile)
9886                             record_line (last_subfile, 0, addr);
9887                           last_subfile = current_subfile;
9888                         }
9889                       /* Append row to matrix using current values.  */
9890                       addr = check_cu_functions (address, cu);
9891                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9892                       record_line (current_subfile, line, addr);
9893                     }
9894                 }
9895               basic_block = 0;
9896             }
9897           else switch (op_code)
9898             {
9899             case DW_LNS_extended_op:
9900               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9901               line_ptr += bytes_read;
9902               extended_end = line_ptr + extended_len;
9903               extended_op = read_1_byte (abfd, line_ptr);
9904               line_ptr += 1;
9905               switch (extended_op)
9906                 {
9907                 case DW_LNE_end_sequence:
9908                   end_sequence = 1;
9909                   break;
9910                 case DW_LNE_set_address:
9911                   address = read_address (abfd, line_ptr, cu, &bytes_read);
9912                   op_index = 0;
9913                   line_ptr += bytes_read;
9914                   address += baseaddr;
9915                   break;
9916                 case DW_LNE_define_file:
9917                   {
9918                     char *cur_file;
9919                     unsigned int dir_index, mod_time, length;
9920
9921                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
9922                     line_ptr += bytes_read;
9923                     dir_index =
9924                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9925                     line_ptr += bytes_read;
9926                     mod_time =
9927                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9928                     line_ptr += bytes_read;
9929                     length =
9930                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9931                     line_ptr += bytes_read;
9932                     add_file_name (lh, cur_file, dir_index, mod_time, length);
9933                   }
9934                   break;
9935                 case DW_LNE_set_discriminator:
9936                   /* The discriminator is not interesting to the debugger;
9937                      just ignore it.  */
9938                   line_ptr = extended_end;
9939                   break;
9940                 default:
9941                   complaint (&symfile_complaints,
9942                              _("mangled .debug_line section"));
9943                   return;
9944                 }
9945               /* Make sure that we parsed the extended op correctly.  If e.g.
9946                  we expected a different address size than the producer used,
9947                  we may have read the wrong number of bytes.  */
9948               if (line_ptr != extended_end)
9949                 {
9950                   complaint (&symfile_complaints,
9951                              _("mangled .debug_line section"));
9952                   return;
9953                 }
9954               break;
9955             case DW_LNS_copy:
9956               if (lh->num_file_names < file || file == 0)
9957                 dwarf2_debug_line_missing_file_complaint ();
9958               else
9959                 {
9960                   lh->file_names[file - 1].included_p = 1;
9961                   if (!decode_for_pst_p && is_stmt)
9962                     {
9963                       if (last_subfile != current_subfile)
9964                         {
9965                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9966                           if (last_subfile)
9967                             record_line (last_subfile, 0, addr);
9968                           last_subfile = current_subfile;
9969                         }
9970                       addr = check_cu_functions (address, cu);
9971                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9972                       record_line (current_subfile, line, addr);
9973                     }
9974                 }
9975               basic_block = 0;
9976               break;
9977             case DW_LNS_advance_pc:
9978               {
9979                 CORE_ADDR adjust
9980                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9981
9982                 address += (((op_index + adjust)
9983                              / lh->maximum_ops_per_instruction)
9984                             * lh->minimum_instruction_length);
9985                 op_index = ((op_index + adjust)
9986                             % lh->maximum_ops_per_instruction);
9987                 line_ptr += bytes_read;
9988               }
9989               break;
9990             case DW_LNS_advance_line:
9991               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9992               line_ptr += bytes_read;
9993               break;
9994             case DW_LNS_set_file:
9995               {
9996                 /* The arrays lh->include_dirs and lh->file_names are
9997                    0-based, but the directory and file name numbers in
9998                    the statement program are 1-based.  */
9999                 struct file_entry *fe;
10000                 char *dir = NULL;
10001
10002                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10003                 line_ptr += bytes_read;
10004                 if (lh->num_file_names < file || file == 0)
10005                   dwarf2_debug_line_missing_file_complaint ();
10006                 else
10007                   {
10008                     fe = &lh->file_names[file - 1];
10009                     if (fe->dir_index)
10010                       dir = lh->include_dirs[fe->dir_index - 1];
10011                     if (!decode_for_pst_p)
10012                       {
10013                         last_subfile = current_subfile;
10014                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10015                       }
10016                   }
10017               }
10018               break;
10019             case DW_LNS_set_column:
10020               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10021               line_ptr += bytes_read;
10022               break;
10023             case DW_LNS_negate_stmt:
10024               is_stmt = (!is_stmt);
10025               break;
10026             case DW_LNS_set_basic_block:
10027               basic_block = 1;
10028               break;
10029             /* Add to the address register of the state machine the
10030                address increment value corresponding to special opcode
10031                255.  I.e., this value is scaled by the minimum
10032                instruction length since special opcode 255 would have
10033                scaled the the increment.  */
10034             case DW_LNS_const_add_pc:
10035               {
10036                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10037
10038                 address += (((op_index + adjust)
10039                              / lh->maximum_ops_per_instruction)
10040                             * lh->minimum_instruction_length);
10041                 op_index = ((op_index + adjust)
10042                             % lh->maximum_ops_per_instruction);
10043               }
10044               break;
10045             case DW_LNS_fixed_advance_pc:
10046               address += read_2_bytes (abfd, line_ptr);
10047               op_index = 0;
10048               line_ptr += 2;
10049               break;
10050             default:
10051               {
10052                 /* Unknown standard opcode, ignore it.  */
10053                 int i;
10054
10055                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10056                   {
10057                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10058                     line_ptr += bytes_read;
10059                   }
10060               }
10061             }
10062         }
10063       if (lh->num_file_names < file || file == 0)
10064         dwarf2_debug_line_missing_file_complaint ();
10065       else
10066         {
10067           lh->file_names[file - 1].included_p = 1;
10068           if (!decode_for_pst_p)
10069             {
10070               addr = gdbarch_addr_bits_remove (gdbarch, address);
10071               record_line (current_subfile, 0, addr);
10072             }
10073         }
10074     }
10075
10076   if (decode_for_pst_p)
10077     {
10078       int file_index;
10079
10080       /* Now that we're done scanning the Line Header Program, we can
10081          create the psymtab of each included file.  */
10082       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10083         if (lh->file_names[file_index].included_p == 1)
10084           {
10085             const struct file_entry fe = lh->file_names [file_index];
10086             char *include_name = fe.name;
10087             char *dir_name = NULL;
10088             char *pst_filename = pst->filename;
10089
10090             if (fe.dir_index)
10091               dir_name = lh->include_dirs[fe.dir_index - 1];
10092
10093             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10094               {
10095                 include_name = concat (dir_name, SLASH_STRING,
10096                                        include_name, (char *)NULL);
10097                 make_cleanup (xfree, include_name);
10098               }
10099
10100             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10101               {
10102                 pst_filename = concat (pst->dirname, SLASH_STRING,
10103                                        pst_filename, (char *)NULL);
10104                 make_cleanup (xfree, pst_filename);
10105               }
10106
10107             if (strcmp (include_name, pst_filename) != 0)
10108               dwarf2_create_include_psymtab (include_name, pst, objfile);
10109           }
10110     }
10111   else
10112     {
10113       /* Make sure a symtab is created for every file, even files
10114          which contain only variables (i.e. no code with associated
10115          line numbers).  */
10116
10117       int i;
10118       struct file_entry *fe;
10119
10120       for (i = 0; i < lh->num_file_names; i++)
10121         {
10122           char *dir = NULL;
10123
10124           fe = &lh->file_names[i];
10125           if (fe->dir_index)
10126             dir = lh->include_dirs[fe->dir_index - 1];
10127           dwarf2_start_subfile (fe->name, dir, comp_dir);
10128
10129           /* Skip the main file; we don't need it, and it must be
10130              allocated last, so that it will show up before the
10131              non-primary symtabs in the objfile's symtab list.  */
10132           if (current_subfile == first_subfile)
10133             continue;
10134
10135           if (current_subfile->symtab == NULL)
10136             current_subfile->symtab = allocate_symtab (current_subfile->name,
10137                                                        cu->objfile);
10138           fe->symtab = current_subfile->symtab;
10139         }
10140     }
10141 }
10142
10143 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10144    DIRNAME the name of the source directory which contains FILENAME
10145    or NULL if not known.  COMP_DIR is the compilation directory for the
10146    linetable's compilation unit or NULL if not known.
10147    This routine tries to keep line numbers from identical absolute and
10148    relative file names in a common subfile.
10149
10150    Using the `list' example from the GDB testsuite, which resides in
10151    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10152    of /srcdir/list0.c yields the following debugging information for list0.c:
10153
10154    DW_AT_name:          /srcdir/list0.c
10155    DW_AT_comp_dir:              /compdir
10156    files.files[0].name: list0.h
10157    files.files[0].dir:  /srcdir
10158    files.files[1].name: list0.c
10159    files.files[1].dir:  /srcdir
10160
10161    The line number information for list0.c has to end up in a single
10162    subfile, so that `break /srcdir/list0.c:1' works as expected.
10163    start_subfile will ensure that this happens provided that we pass the
10164    concatenation of files.files[1].dir and files.files[1].name as the
10165    subfile's name.  */
10166
10167 static void
10168 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10169 {
10170   char *fullname;
10171
10172   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10173      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10174      second argument to start_subfile.  To be consistent, we do the
10175      same here.  In order not to lose the line information directory,
10176      we concatenate it to the filename when it makes sense.
10177      Note that the Dwarf3 standard says (speaking of filenames in line
10178      information): ``The directory index is ignored for file names
10179      that represent full path names''.  Thus ignoring dirname in the
10180      `else' branch below isn't an issue.  */
10181
10182   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10183     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10184   else
10185     fullname = filename;
10186
10187   start_subfile (fullname, comp_dir);
10188
10189   if (fullname != filename)
10190     xfree (fullname);
10191 }
10192
10193 static void
10194 var_decode_location (struct attribute *attr, struct symbol *sym,
10195                      struct dwarf2_cu *cu)
10196 {
10197   struct objfile *objfile = cu->objfile;
10198   struct comp_unit_head *cu_header = &cu->header;
10199
10200   /* NOTE drow/2003-01-30: There used to be a comment and some special
10201      code here to turn a symbol with DW_AT_external and a
10202      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10203      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10204      with some versions of binutils) where shared libraries could have
10205      relocations against symbols in their debug information - the
10206      minimal symbol would have the right address, but the debug info
10207      would not.  It's no longer necessary, because we will explicitly
10208      apply relocations when we read in the debug information now.  */
10209
10210   /* A DW_AT_location attribute with no contents indicates that a
10211      variable has been optimized away.  */
10212   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10213     {
10214       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10215       return;
10216     }
10217
10218   /* Handle one degenerate form of location expression specially, to
10219      preserve GDB's previous behavior when section offsets are
10220      specified.  If this is just a DW_OP_addr then mark this symbol
10221      as LOC_STATIC.  */
10222
10223   if (attr_form_is_block (attr)
10224       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10225       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10226     {
10227       unsigned int dummy;
10228
10229       SYMBOL_VALUE_ADDRESS (sym) =
10230         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10231       SYMBOL_CLASS (sym) = LOC_STATIC;
10232       fixup_symbol_section (sym, objfile);
10233       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10234                                               SYMBOL_SECTION (sym));
10235       return;
10236     }
10237
10238   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10239      expression evaluator, and use LOC_COMPUTED only when necessary
10240      (i.e. when the value of a register or memory location is
10241      referenced, or a thread-local block, etc.).  Then again, it might
10242      not be worthwhile.  I'm assuming that it isn't unless performance
10243      or memory numbers show me otherwise.  */
10244
10245   dwarf2_symbol_mark_computed (attr, sym, cu);
10246   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10247 }
10248
10249 /* Given a pointer to a DWARF information entry, figure out if we need
10250    to make a symbol table entry for it, and if so, create a new entry
10251    and return a pointer to it.
10252    If TYPE is NULL, determine symbol type from the die, otherwise
10253    used the passed type.
10254    If SPACE is not NULL, use it to hold the new symbol.  If it is
10255    NULL, allocate a new symbol on the objfile's obstack.  */
10256
10257 static struct symbol *
10258 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10259                  struct symbol *space)
10260 {
10261   struct objfile *objfile = cu->objfile;
10262   struct symbol *sym = NULL;
10263   char *name;
10264   struct attribute *attr = NULL;
10265   struct attribute *attr2 = NULL;
10266   CORE_ADDR baseaddr;
10267   struct pending **list_to_add = NULL;
10268
10269   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10270
10271   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10272
10273   name = dwarf2_name (die, cu);
10274   if (name)
10275     {
10276       const char *linkagename;
10277       int suppress_add = 0;
10278
10279       if (space)
10280         sym = space;
10281       else
10282         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10283       OBJSTAT (objfile, n_syms++);
10284
10285       /* Cache this symbol's name and the name's demangled form (if any).  */
10286       SYMBOL_SET_LANGUAGE (sym, cu->language);
10287       linkagename = dwarf2_physname (name, die, cu);
10288       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10289
10290       /* Fortran does not have mangling standard and the mangling does differ
10291          between gfortran, iFort etc.  */
10292       if (cu->language == language_fortran
10293           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10294         symbol_set_demangled_name (&(sym->ginfo),
10295                                    (char *) dwarf2_full_name (name, die, cu),
10296                                    NULL);
10297
10298       /* Default assumptions.
10299          Use the passed type or decode it from the die.  */
10300       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10301       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10302       if (type != NULL)
10303         SYMBOL_TYPE (sym) = type;
10304       else
10305         SYMBOL_TYPE (sym) = die_type (die, cu);
10306       attr = dwarf2_attr (die,
10307                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10308                           cu);
10309       if (attr)
10310         {
10311           SYMBOL_LINE (sym) = DW_UNSND (attr);
10312         }
10313
10314       attr = dwarf2_attr (die,
10315                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10316                           cu);
10317       if (attr)
10318         {
10319           int file_index = DW_UNSND (attr);
10320
10321           if (cu->line_header == NULL
10322               || file_index > cu->line_header->num_file_names)
10323             complaint (&symfile_complaints,
10324                        _("file index out of range"));
10325           else if (file_index > 0)
10326             {
10327               struct file_entry *fe;
10328
10329               fe = &cu->line_header->file_names[file_index - 1];
10330               SYMBOL_SYMTAB (sym) = fe->symtab;
10331             }
10332         }
10333
10334       switch (die->tag)
10335         {
10336         case DW_TAG_label:
10337           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10338           if (attr)
10339             {
10340               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10341             }
10342           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10343           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10344           SYMBOL_CLASS (sym) = LOC_LABEL;
10345           add_symbol_to_list (sym, cu->list_in_scope);
10346           break;
10347         case DW_TAG_subprogram:
10348           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10349              finish_block.  */
10350           SYMBOL_CLASS (sym) = LOC_BLOCK;
10351           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10352           if ((attr2 && (DW_UNSND (attr2) != 0))
10353               || cu->language == language_ada)
10354             {
10355               /* Subprograms marked external are stored as a global symbol.
10356                  Ada subprograms, whether marked external or not, are always
10357                  stored as a global symbol, because we want to be able to
10358                  access them globally.  For instance, we want to be able
10359                  to break on a nested subprogram without having to
10360                  specify the context.  */
10361               list_to_add = &global_symbols;
10362             }
10363           else
10364             {
10365               list_to_add = cu->list_in_scope;
10366             }
10367           break;
10368         case DW_TAG_inlined_subroutine:
10369           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10370              finish_block.  */
10371           SYMBOL_CLASS (sym) = LOC_BLOCK;
10372           SYMBOL_INLINED (sym) = 1;
10373           /* Do not add the symbol to any lists.  It will be found via
10374              BLOCK_FUNCTION from the blockvector.  */
10375           break;
10376         case DW_TAG_template_value_param:
10377           suppress_add = 1;
10378           /* Fall through.  */
10379         case DW_TAG_variable:
10380         case DW_TAG_member:
10381           /* Compilation with minimal debug info may result in variables
10382              with missing type entries. Change the misleading `void' type
10383              to something sensible.  */
10384           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10385             SYMBOL_TYPE (sym)
10386               = objfile_type (objfile)->nodebug_data_symbol;
10387
10388           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10389           /* In the case of DW_TAG_member, we should only be called for
10390              static const members.  */
10391           if (die->tag == DW_TAG_member)
10392             {
10393               /* dwarf2_add_field uses die_is_declaration,
10394                  so we do the same.  */
10395               gdb_assert (die_is_declaration (die, cu));
10396               gdb_assert (attr);
10397             }
10398           if (attr)
10399             {
10400               dwarf2_const_value (attr, sym, cu);
10401               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10402               if (!suppress_add)
10403                 {
10404                   if (attr2 && (DW_UNSND (attr2) != 0))
10405                     list_to_add = &global_symbols;
10406                   else
10407                     list_to_add = cu->list_in_scope;
10408                 }
10409               break;
10410             }
10411           attr = dwarf2_attr (die, DW_AT_location, cu);
10412           if (attr)
10413             {
10414               var_decode_location (attr, sym, cu);
10415               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10416               if (SYMBOL_CLASS (sym) == LOC_STATIC
10417                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10418                   && !dwarf2_per_objfile->has_section_at_zero)
10419                 {
10420                   /* When a static variable is eliminated by the linker,
10421                      the corresponding debug information is not stripped
10422                      out, but the variable address is set to null;
10423                      do not add such variables into symbol table.  */
10424                 }
10425               else if (attr2 && (DW_UNSND (attr2) != 0))
10426                 {
10427                   /* Workaround gfortran PR debug/40040 - it uses
10428                      DW_AT_location for variables in -fPIC libraries which may
10429                      get overriden by other libraries/executable and get
10430                      a different address.  Resolve it by the minimal symbol
10431                      which may come from inferior's executable using copy
10432                      relocation.  Make this workaround only for gfortran as for
10433                      other compilers GDB cannot guess the minimal symbol
10434                      Fortran mangling kind.  */
10435                   if (cu->language == language_fortran && die->parent
10436                       && die->parent->tag == DW_TAG_module
10437                       && cu->producer
10438                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10439                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10440
10441                   /* A variable with DW_AT_external is never static,
10442                      but it may be block-scoped.  */
10443                   list_to_add = (cu->list_in_scope == &file_symbols
10444                                  ? &global_symbols : cu->list_in_scope);
10445                 }
10446               else
10447                 list_to_add = cu->list_in_scope;
10448             }
10449           else
10450             {
10451               /* We do not know the address of this symbol.
10452                  If it is an external symbol and we have type information
10453                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10454                  The address of the variable will then be determined from
10455                  the minimal symbol table whenever the variable is
10456                  referenced.  */
10457               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10458               if (attr2 && (DW_UNSND (attr2) != 0)
10459                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10460                 {
10461                   /* A variable with DW_AT_external is never static, but it
10462                      may be block-scoped.  */
10463                   list_to_add = (cu->list_in_scope == &file_symbols
10464                                  ? &global_symbols : cu->list_in_scope);
10465
10466                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10467                 }
10468               else if (!die_is_declaration (die, cu))
10469                 {
10470                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10471                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10472                   if (!suppress_add)
10473                     list_to_add = cu->list_in_scope;
10474                 }
10475             }
10476           break;
10477         case DW_TAG_formal_parameter:
10478           /* If we are inside a function, mark this as an argument.  If
10479              not, we might be looking at an argument to an inlined function
10480              when we do not have enough information to show inlined frames;
10481              pretend it's a local variable in that case so that the user can
10482              still see it.  */
10483           if (context_stack_depth > 0
10484               && context_stack[context_stack_depth - 1].name != NULL)
10485             SYMBOL_IS_ARGUMENT (sym) = 1;
10486           attr = dwarf2_attr (die, DW_AT_location, cu);
10487           if (attr)
10488             {
10489               var_decode_location (attr, sym, cu);
10490             }
10491           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10492           if (attr)
10493             {
10494               dwarf2_const_value (attr, sym, cu);
10495             }
10496           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10497           if (attr && DW_UNSND (attr))
10498             {
10499               struct type *ref_type;
10500
10501               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10502               SYMBOL_TYPE (sym) = ref_type;
10503             }
10504
10505           list_to_add = cu->list_in_scope;
10506           break;
10507         case DW_TAG_unspecified_parameters:
10508           /* From varargs functions; gdb doesn't seem to have any
10509              interest in this information, so just ignore it for now.
10510              (FIXME?) */
10511           break;
10512         case DW_TAG_template_type_param:
10513           suppress_add = 1;
10514           /* Fall through.  */
10515         case DW_TAG_class_type:
10516         case DW_TAG_interface_type:
10517         case DW_TAG_structure_type:
10518         case DW_TAG_union_type:
10519         case DW_TAG_set_type:
10520         case DW_TAG_enumeration_type:
10521           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10522           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10523
10524           {
10525             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10526                really ever be static objects: otherwise, if you try
10527                to, say, break of a class's method and you're in a file
10528                which doesn't mention that class, it won't work unless
10529                the check for all static symbols in lookup_symbol_aux
10530                saves you.  See the OtherFileClass tests in
10531                gdb.c++/namespace.exp.  */
10532
10533             if (!suppress_add)
10534               {
10535                 list_to_add = (cu->list_in_scope == &file_symbols
10536                                && (cu->language == language_cplus
10537                                    || cu->language == language_java)
10538                                ? &global_symbols : cu->list_in_scope);
10539               }
10540
10541             /* The semantics of C++ state that "struct foo { ... }" also
10542                defines a typedef for "foo".  A Java class declaration also
10543                defines a typedef for the class.  */
10544             if (cu->language == language_cplus
10545                 || cu->language == language_java
10546                 || cu->language == language_ada)
10547               {
10548                 /* The symbol's name is already allocated along with
10549                    this objfile, so we don't need to duplicate it for
10550                    the type.  */
10551                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10552                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10553               }
10554           }
10555           break;
10556         case DW_TAG_typedef:
10557           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10558           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10559           list_to_add = cu->list_in_scope;
10560           break;
10561         case DW_TAG_base_type:
10562         case DW_TAG_subrange_type:
10563           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10564           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10565           list_to_add = cu->list_in_scope;
10566           break;
10567         case DW_TAG_enumerator:
10568           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10569           if (attr)
10570             {
10571               dwarf2_const_value (attr, sym, cu);
10572             }
10573           {
10574             /* NOTE: carlton/2003-11-10: See comment above in the
10575                DW_TAG_class_type, etc. block.  */
10576
10577             list_to_add = (cu->list_in_scope == &file_symbols
10578                            && (cu->language == language_cplus
10579                                || cu->language == language_java)
10580                            ? &global_symbols : cu->list_in_scope);
10581           }
10582           break;
10583         case DW_TAG_namespace:
10584           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10585           list_to_add = &global_symbols;
10586           break;
10587         default:
10588           /* Not a tag we recognize.  Hopefully we aren't processing
10589              trash data, but since we must specifically ignore things
10590              we don't recognize, there is nothing else we should do at
10591              this point. */
10592           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10593                      dwarf_tag_name (die->tag));
10594           break;
10595         }
10596
10597       if (suppress_add)
10598         {
10599           sym->hash_next = objfile->template_symbols;
10600           objfile->template_symbols = sym;
10601           list_to_add = NULL;
10602         }
10603
10604       if (list_to_add != NULL)
10605         add_symbol_to_list (sym, list_to_add);
10606
10607       /* For the benefit of old versions of GCC, check for anonymous
10608          namespaces based on the demangled name.  */
10609       if (!processing_has_namespace_info
10610           && cu->language == language_cplus)
10611         cp_scan_for_anonymous_namespaces (sym);
10612     }
10613   return (sym);
10614 }
10615
10616 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
10617
10618 static struct symbol *
10619 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10620 {
10621   return new_symbol_full (die, type, cu, NULL);
10622 }
10623
10624 /* Given an attr with a DW_FORM_dataN value in host byte order,
10625    zero-extend it as appropriate for the symbol's type.  The DWARF
10626    standard (v4) is not entirely clear about the meaning of using
10627    DW_FORM_dataN for a constant with a signed type, where the type is
10628    wider than the data.  The conclusion of a discussion on the DWARF
10629    list was that this is unspecified.  We choose to always zero-extend
10630    because that is the interpretation long in use by GCC.  */
10631
10632 static gdb_byte *
10633 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10634                          const char *name, struct obstack *obstack,
10635                          struct dwarf2_cu *cu, long *value, int bits)
10636 {
10637   struct objfile *objfile = cu->objfile;
10638   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10639                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10640   LONGEST l = DW_UNSND (attr);
10641
10642   if (bits < sizeof (*value) * 8)
10643     {
10644       l &= ((LONGEST) 1 << bits) - 1;
10645       *value = l;
10646     }
10647   else if (bits == sizeof (*value) * 8)
10648     *value = l;
10649   else
10650     {
10651       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10652       store_unsigned_integer (bytes, bits / 8, byte_order, l);
10653       return bytes;
10654     }
10655
10656   return NULL;
10657 }
10658
10659 /* Read a constant value from an attribute.  Either set *VALUE, or if
10660    the value does not fit in *VALUE, set *BYTES - either already
10661    allocated on the objfile obstack, or newly allocated on OBSTACK,
10662    or, set *BATON, if we translated the constant to a location
10663    expression.  */
10664
10665 static void
10666 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10667                          const char *name, struct obstack *obstack,
10668                          struct dwarf2_cu *cu,
10669                          long *value, gdb_byte **bytes,
10670                          struct dwarf2_locexpr_baton **baton)
10671 {
10672   struct objfile *objfile = cu->objfile;
10673   struct comp_unit_head *cu_header = &cu->header;
10674   struct dwarf_block *blk;
10675   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10676                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10677
10678   *value = 0;
10679   *bytes = NULL;
10680   *baton = NULL;
10681
10682   switch (attr->form)
10683     {
10684     case DW_FORM_addr:
10685       {
10686         gdb_byte *data;
10687
10688         if (TYPE_LENGTH (type) != cu_header->addr_size)
10689           dwarf2_const_value_length_mismatch_complaint (name,
10690                                                         cu_header->addr_size,
10691                                                         TYPE_LENGTH (type));
10692         /* Symbols of this form are reasonably rare, so we just
10693            piggyback on the existing location code rather than writing
10694            a new implementation of symbol_computed_ops.  */
10695         *baton = obstack_alloc (&objfile->objfile_obstack,
10696                                 sizeof (struct dwarf2_locexpr_baton));
10697         (*baton)->per_cu = cu->per_cu;
10698         gdb_assert ((*baton)->per_cu);
10699
10700         (*baton)->size = 2 + cu_header->addr_size;
10701         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10702         (*baton)->data = data;
10703
10704         data[0] = DW_OP_addr;
10705         store_unsigned_integer (&data[1], cu_header->addr_size,
10706                                 byte_order, DW_ADDR (attr));
10707         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10708       }
10709       break;
10710     case DW_FORM_string:
10711     case DW_FORM_strp:
10712       /* DW_STRING is already allocated on the objfile obstack, point
10713          directly to it.  */
10714       *bytes = (gdb_byte *) DW_STRING (attr);
10715       break;
10716     case DW_FORM_block1:
10717     case DW_FORM_block2:
10718     case DW_FORM_block4:
10719     case DW_FORM_block:
10720     case DW_FORM_exprloc:
10721       blk = DW_BLOCK (attr);
10722       if (TYPE_LENGTH (type) != blk->size)
10723         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10724                                                       TYPE_LENGTH (type));
10725       *bytes = blk->data;
10726       break;
10727
10728       /* The DW_AT_const_value attributes are supposed to carry the
10729          symbol's value "represented as it would be on the target
10730          architecture."  By the time we get here, it's already been
10731          converted to host endianness, so we just need to sign- or
10732          zero-extend it as appropriate.  */
10733     case DW_FORM_data1:
10734       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10735       break;
10736     case DW_FORM_data2:
10737       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10738       break;
10739     case DW_FORM_data4:
10740       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10741       break;
10742     case DW_FORM_data8:
10743       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10744       break;
10745
10746     case DW_FORM_sdata:
10747       *value = DW_SND (attr);
10748       break;
10749
10750     case DW_FORM_udata:
10751       *value = DW_UNSND (attr);
10752       break;
10753
10754     default:
10755       complaint (&symfile_complaints,
10756                  _("unsupported const value attribute form: '%s'"),
10757                  dwarf_form_name (attr->form));
10758       *value = 0;
10759       break;
10760     }
10761 }
10762
10763
10764 /* Copy constant value from an attribute to a symbol.  */
10765
10766 static void
10767 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10768                     struct dwarf2_cu *cu)
10769 {
10770   struct objfile *objfile = cu->objfile;
10771   struct comp_unit_head *cu_header = &cu->header;
10772   long value;
10773   gdb_byte *bytes;
10774   struct dwarf2_locexpr_baton *baton;
10775
10776   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10777                            SYMBOL_PRINT_NAME (sym),
10778                            &objfile->objfile_obstack, cu,
10779                            &value, &bytes, &baton);
10780
10781   if (baton != NULL)
10782     {
10783       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10784       SYMBOL_LOCATION_BATON (sym) = baton;
10785       SYMBOL_CLASS (sym) = LOC_COMPUTED;
10786     }
10787   else if (bytes != NULL)
10788      {
10789       SYMBOL_VALUE_BYTES (sym) = bytes;
10790       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10791     }
10792   else
10793     {
10794       SYMBOL_VALUE (sym) = value;
10795       SYMBOL_CLASS (sym) = LOC_CONST;
10796     }
10797 }
10798
10799
10800 /* Return the type of the die in question using its DW_AT_type attribute.  */
10801
10802 static struct type *
10803 die_type (struct die_info *die, struct dwarf2_cu *cu)
10804 {
10805   struct attribute *type_attr;
10806   struct die_info *type_die;
10807
10808   type_attr = dwarf2_attr (die, DW_AT_type, cu);
10809   if (!type_attr)
10810     {
10811       /* A missing DW_AT_type represents a void type.  */
10812       return objfile_type (cu->objfile)->builtin_void;
10813     }
10814
10815   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10816
10817   return tag_type_to_type (type_die, cu);
10818 }
10819
10820 /* True iff CU's producer generates GNAT Ada auxiliary information
10821    that allows to find parallel types through that information instead
10822    of having to do expensive parallel lookups by type name.  */
10823
10824 static int
10825 need_gnat_info (struct dwarf2_cu *cu)
10826 {
10827   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10828      of GNAT produces this auxiliary information, without any indication
10829      that it is produced.  Part of enhancing the FSF version of GNAT
10830      to produce that information will be to put in place an indicator
10831      that we can use in order to determine whether the descriptive type
10832      info is available or not.  One suggestion that has been made is
10833      to use a new attribute, attached to the CU die.  For now, assume
10834      that the descriptive type info is not available.  */
10835   return 0;
10836 }
10837
10838
10839 /* Return the auxiliary type of the die in question using its
10840    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
10841    attribute is not present.  */
10842
10843 static struct type *
10844 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10845 {
10846   struct attribute *type_attr;
10847   struct die_info *type_die;
10848
10849   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10850   if (!type_attr)
10851     return NULL;
10852
10853   type_die = follow_die_ref (die, type_attr, &cu);
10854   return tag_type_to_type (type_die, cu);
10855 }
10856
10857 /* If DIE has a descriptive_type attribute, then set the TYPE's
10858    descriptive type accordingly.  */
10859
10860 static void
10861 set_descriptive_type (struct type *type, struct die_info *die,
10862                       struct dwarf2_cu *cu)
10863 {
10864   struct type *descriptive_type = die_descriptive_type (die, cu);
10865
10866   if (descriptive_type)
10867     {
10868       ALLOCATE_GNAT_AUX_TYPE (type);
10869       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10870     }
10871 }
10872
10873 /* Return the containing type of the die in question using its
10874    DW_AT_containing_type attribute.  */
10875
10876 static struct type *
10877 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10878 {
10879   struct attribute *type_attr;
10880   struct die_info *type_die;
10881
10882   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10883   if (!type_attr)
10884     error (_("Dwarf Error: Problem turning containing type into gdb type "
10885              "[in module %s]"), cu->objfile->name);
10886
10887   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10888   return tag_type_to_type (type_die, cu);
10889 }
10890
10891 static struct type *
10892 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
10893 {
10894   struct type *this_type;
10895
10896   this_type = read_type_die (die, cu);
10897   if (!this_type)
10898     {
10899       char *message, *saved;
10900
10901       /* read_type_die already issued a complaint.  */
10902       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10903                             cu->objfile->name,
10904                             cu->header.offset,
10905                             die->offset);
10906       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10907                              message, strlen (message));
10908       xfree (message);
10909
10910       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
10911     }
10912   return this_type;
10913 }
10914
10915 static struct type *
10916 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
10917 {
10918   struct type *this_type;
10919
10920   this_type = get_die_type (die, cu);
10921   if (this_type)
10922     return this_type;
10923
10924   switch (die->tag)
10925     {
10926     case DW_TAG_class_type:
10927     case DW_TAG_interface_type:
10928     case DW_TAG_structure_type:
10929     case DW_TAG_union_type:
10930       this_type = read_structure_type (die, cu);
10931       break;
10932     case DW_TAG_enumeration_type:
10933       this_type = read_enumeration_type (die, cu);
10934       break;
10935     case DW_TAG_subprogram:
10936     case DW_TAG_subroutine_type:
10937     case DW_TAG_inlined_subroutine:
10938       this_type = read_subroutine_type (die, cu);
10939       break;
10940     case DW_TAG_array_type:
10941       this_type = read_array_type (die, cu);
10942       break;
10943     case DW_TAG_set_type:
10944       this_type = read_set_type (die, cu);
10945       break;
10946     case DW_TAG_pointer_type:
10947       this_type = read_tag_pointer_type (die, cu);
10948       break;
10949     case DW_TAG_ptr_to_member_type:
10950       this_type = read_tag_ptr_to_member_type (die, cu);
10951       break;
10952     case DW_TAG_reference_type:
10953       this_type = read_tag_reference_type (die, cu);
10954       break;
10955     case DW_TAG_const_type:
10956       this_type = read_tag_const_type (die, cu);
10957       break;
10958     case DW_TAG_volatile_type:
10959       this_type = read_tag_volatile_type (die, cu);
10960       break;
10961     case DW_TAG_string_type:
10962       this_type = read_tag_string_type (die, cu);
10963       break;
10964     case DW_TAG_typedef:
10965       this_type = read_typedef (die, cu);
10966       break;
10967     case DW_TAG_subrange_type:
10968       this_type = read_subrange_type (die, cu);
10969       break;
10970     case DW_TAG_base_type:
10971       this_type = read_base_type (die, cu);
10972       break;
10973     case DW_TAG_unspecified_type:
10974       this_type = read_unspecified_type (die, cu);
10975       break;
10976     case DW_TAG_namespace:
10977       this_type = read_namespace_type (die, cu);
10978       break;
10979     case DW_TAG_module:
10980       this_type = read_module_type (die, cu);
10981       break;
10982     default:
10983       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
10984                  dwarf_tag_name (die->tag));
10985       break;
10986     }
10987
10988   return this_type;
10989 }
10990
10991 /* Return the name of the namespace/class that DIE is defined within,
10992    or "" if we can't tell.  The caller should not xfree the result.
10993
10994    For example, if we're within the method foo() in the following
10995    code:
10996
10997    namespace N {
10998      class C {
10999        void foo () {
11000        }
11001      };
11002    }
11003
11004    then determine_prefix on foo's die will return "N::C".  */
11005
11006 static char *
11007 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11008 {
11009   struct die_info *parent, *spec_die;
11010   struct dwarf2_cu *spec_cu;
11011   struct type *parent_type;
11012
11013   if (cu->language != language_cplus && cu->language != language_java
11014       && cu->language != language_fortran)
11015     return "";
11016
11017   /* We have to be careful in the presence of DW_AT_specification.
11018      For example, with GCC 3.4, given the code
11019
11020      namespace N {
11021        void foo() {
11022          // Definition of N::foo.
11023        }
11024      }
11025
11026      then we'll have a tree of DIEs like this:
11027
11028      1: DW_TAG_compile_unit
11029        2: DW_TAG_namespace        // N
11030          3: DW_TAG_subprogram     // declaration of N::foo
11031        4: DW_TAG_subprogram       // definition of N::foo
11032             DW_AT_specification   // refers to die #3
11033
11034      Thus, when processing die #4, we have to pretend that we're in
11035      the context of its DW_AT_specification, namely the contex of die
11036      #3.  */
11037   spec_cu = cu;
11038   spec_die = die_specification (die, &spec_cu);
11039   if (spec_die == NULL)
11040     parent = die->parent;
11041   else
11042     {
11043       parent = spec_die->parent;
11044       cu = spec_cu;
11045     }
11046
11047   if (parent == NULL)
11048     return "";
11049   else if (parent->building_fullname)
11050     {
11051       const char *name;
11052       const char *parent_name;
11053
11054       /* It has been seen on RealView 2.2 built binaries,
11055          DW_TAG_template_type_param types actually _defined_ as
11056          children of the parent class:
11057
11058          enum E {};
11059          template class <class Enum> Class{};
11060          Class<enum E> class_e;
11061
11062          1: DW_TAG_class_type (Class)
11063            2: DW_TAG_enumeration_type (E)
11064              3: DW_TAG_enumerator (enum1:0)
11065              3: DW_TAG_enumerator (enum2:1)
11066              ...
11067            2: DW_TAG_template_type_param
11068               DW_AT_type  DW_FORM_ref_udata (E)
11069
11070          Besides being broken debug info, it can put GDB into an
11071          infinite loop.  Consider:
11072
11073          When we're building the full name for Class<E>, we'll start
11074          at Class, and go look over its template type parameters,
11075          finding E.  We'll then try to build the full name of E, and
11076          reach here.  We're now trying to build the full name of E,
11077          and look over the parent DIE for containing scope.  In the
11078          broken case, if we followed the parent DIE of E, we'd again
11079          find Class, and once again go look at its template type
11080          arguments, etc., etc.  Simply don't consider such parent die
11081          as source-level parent of this die (it can't be, the language
11082          doesn't allow it), and break the loop here.  */
11083       name = dwarf2_name (die, cu);
11084       parent_name = dwarf2_name (parent, cu);
11085       complaint (&symfile_complaints,
11086                  _("template param type '%s' defined within parent '%s'"),
11087                  name ? name : "<unknown>",
11088                  parent_name ? parent_name : "<unknown>");
11089       return "";
11090     }
11091   else
11092     switch (parent->tag)
11093       {
11094       case DW_TAG_namespace:
11095         parent_type = read_type_die (parent, cu);
11096         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11097            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11098            Work around this problem here.  */
11099         if (cu->language == language_cplus
11100             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11101           return "";
11102         /* We give a name to even anonymous namespaces.  */
11103         return TYPE_TAG_NAME (parent_type);
11104       case DW_TAG_class_type:
11105       case DW_TAG_interface_type:
11106       case DW_TAG_structure_type:
11107       case DW_TAG_union_type:
11108       case DW_TAG_module:
11109         parent_type = read_type_die (parent, cu);
11110         if (TYPE_TAG_NAME (parent_type) != NULL)
11111           return TYPE_TAG_NAME (parent_type);
11112         else
11113           /* An anonymous structure is only allowed non-static data
11114              members; no typedefs, no member functions, et cetera.
11115              So it does not need a prefix.  */
11116           return "";
11117       default:
11118         return determine_prefix (parent, cu);
11119       }
11120 }
11121
11122 /* Return a newly-allocated string formed by concatenating PREFIX and
11123    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11124    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
11125    perform an obconcat, otherwise allocate storage for the result.  The CU argument
11126    is used to determine the language and hence, the appropriate separator.  */
11127
11128 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11129
11130 static char *
11131 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11132                  int physname, struct dwarf2_cu *cu)
11133 {
11134   const char *lead = "";
11135   const char *sep;
11136
11137   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11138     sep = "";
11139   else if (cu->language == language_java)
11140     sep = ".";
11141   else if (cu->language == language_fortran && physname)
11142     {
11143       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11144          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11145
11146       lead = "__";
11147       sep = "_MOD_";
11148     }
11149   else
11150     sep = "::";
11151
11152   if (prefix == NULL)
11153     prefix = "";
11154   if (suffix == NULL)
11155     suffix = "";
11156
11157   if (obs == NULL)
11158     {
11159       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11160
11161       strcpy (retval, lead);
11162       strcat (retval, prefix);
11163       strcat (retval, sep);
11164       strcat (retval, suffix);
11165       return retval;
11166     }
11167   else
11168     {
11169       /* We have an obstack.  */
11170       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11171     }
11172 }
11173
11174 /* Return sibling of die, NULL if no sibling.  */
11175
11176 static struct die_info *
11177 sibling_die (struct die_info *die)
11178 {
11179   return die->sibling;
11180 }
11181
11182 /* Get name of a die, return NULL if not found.  */
11183
11184 static char *
11185 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11186                           struct obstack *obstack)
11187 {
11188   if (name && cu->language == language_cplus)
11189     {
11190       char *canon_name = cp_canonicalize_string (name);
11191
11192       if (canon_name != NULL)
11193         {
11194           if (strcmp (canon_name, name) != 0)
11195             name = obsavestring (canon_name, strlen (canon_name),
11196                                  obstack);
11197           xfree (canon_name);
11198         }
11199     }
11200
11201   return name;
11202 }
11203
11204 /* Get name of a die, return NULL if not found.  */
11205
11206 static char *
11207 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11208 {
11209   struct attribute *attr;
11210
11211   attr = dwarf2_attr (die, DW_AT_name, cu);
11212   if (!attr || !DW_STRING (attr))
11213     return NULL;
11214
11215   switch (die->tag)
11216     {
11217     case DW_TAG_compile_unit:
11218       /* Compilation units have a DW_AT_name that is a filename, not
11219          a source language identifier.  */
11220     case DW_TAG_enumeration_type:
11221     case DW_TAG_enumerator:
11222       /* These tags always have simple identifiers already; no need
11223          to canonicalize them.  */
11224       return DW_STRING (attr);
11225
11226     case DW_TAG_subprogram:
11227       /* Java constructors will all be named "<init>", so return
11228          the class name when we see this special case.  */
11229       if (cu->language == language_java
11230           && DW_STRING (attr) != NULL
11231           && strcmp (DW_STRING (attr), "<init>") == 0)
11232         {
11233           struct dwarf2_cu *spec_cu = cu;
11234           struct die_info *spec_die;
11235
11236           /* GCJ will output '<init>' for Java constructor names.
11237              For this special case, return the name of the parent class.  */
11238
11239           /* GCJ may output suprogram DIEs with AT_specification set.
11240              If so, use the name of the specified DIE.  */
11241           spec_die = die_specification (die, &spec_cu);
11242           if (spec_die != NULL)
11243             return dwarf2_name (spec_die, spec_cu);
11244
11245           do
11246             {
11247               die = die->parent;
11248               if (die->tag == DW_TAG_class_type)
11249                 return dwarf2_name (die, cu);
11250             }
11251           while (die->tag != DW_TAG_compile_unit);
11252         }
11253       break;
11254
11255     case DW_TAG_class_type:
11256     case DW_TAG_interface_type:
11257     case DW_TAG_structure_type:
11258     case DW_TAG_union_type:
11259       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11260          structures or unions.  These were of the form "._%d" in GCC 4.1,
11261          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11262          and GCC 4.4.  We work around this problem by ignoring these.  */
11263       if (strncmp (DW_STRING (attr), "._", 2) == 0
11264           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11265         return NULL;
11266       break;
11267
11268     default:
11269       break;
11270     }
11271
11272   if (!DW_STRING_IS_CANONICAL (attr))
11273     {
11274       DW_STRING (attr)
11275         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11276                                     &cu->objfile->objfile_obstack);
11277       DW_STRING_IS_CANONICAL (attr) = 1;
11278     }
11279   return DW_STRING (attr);
11280 }
11281
11282 /* Return the die that this die in an extension of, or NULL if there
11283    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11284    containing the return value on output.  */
11285
11286 static struct die_info *
11287 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11288 {
11289   struct attribute *attr;
11290
11291   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11292   if (attr == NULL)
11293     return NULL;
11294
11295   return follow_die_ref (die, attr, ext_cu);
11296 }
11297
11298 /* Convert a DIE tag into its string name.  */
11299
11300 static char *
11301 dwarf_tag_name (unsigned tag)
11302 {
11303   switch (tag)
11304     {
11305     case DW_TAG_padding:
11306       return "DW_TAG_padding";
11307     case DW_TAG_array_type:
11308       return "DW_TAG_array_type";
11309     case DW_TAG_class_type:
11310       return "DW_TAG_class_type";
11311     case DW_TAG_entry_point:
11312       return "DW_TAG_entry_point";
11313     case DW_TAG_enumeration_type:
11314       return "DW_TAG_enumeration_type";
11315     case DW_TAG_formal_parameter:
11316       return "DW_TAG_formal_parameter";
11317     case DW_TAG_imported_declaration:
11318       return "DW_TAG_imported_declaration";
11319     case DW_TAG_label:
11320       return "DW_TAG_label";
11321     case DW_TAG_lexical_block:
11322       return "DW_TAG_lexical_block";
11323     case DW_TAG_member:
11324       return "DW_TAG_member";
11325     case DW_TAG_pointer_type:
11326       return "DW_TAG_pointer_type";
11327     case DW_TAG_reference_type:
11328       return "DW_TAG_reference_type";
11329     case DW_TAG_compile_unit:
11330       return "DW_TAG_compile_unit";
11331     case DW_TAG_string_type:
11332       return "DW_TAG_string_type";
11333     case DW_TAG_structure_type:
11334       return "DW_TAG_structure_type";
11335     case DW_TAG_subroutine_type:
11336       return "DW_TAG_subroutine_type";
11337     case DW_TAG_typedef:
11338       return "DW_TAG_typedef";
11339     case DW_TAG_union_type:
11340       return "DW_TAG_union_type";
11341     case DW_TAG_unspecified_parameters:
11342       return "DW_TAG_unspecified_parameters";
11343     case DW_TAG_variant:
11344       return "DW_TAG_variant";
11345     case DW_TAG_common_block:
11346       return "DW_TAG_common_block";
11347     case DW_TAG_common_inclusion:
11348       return "DW_TAG_common_inclusion";
11349     case DW_TAG_inheritance:
11350       return "DW_TAG_inheritance";
11351     case DW_TAG_inlined_subroutine:
11352       return "DW_TAG_inlined_subroutine";
11353     case DW_TAG_module:
11354       return "DW_TAG_module";
11355     case DW_TAG_ptr_to_member_type:
11356       return "DW_TAG_ptr_to_member_type";
11357     case DW_TAG_set_type:
11358       return "DW_TAG_set_type";
11359     case DW_TAG_subrange_type:
11360       return "DW_TAG_subrange_type";
11361     case DW_TAG_with_stmt:
11362       return "DW_TAG_with_stmt";
11363     case DW_TAG_access_declaration:
11364       return "DW_TAG_access_declaration";
11365     case DW_TAG_base_type:
11366       return "DW_TAG_base_type";
11367     case DW_TAG_catch_block:
11368       return "DW_TAG_catch_block";
11369     case DW_TAG_const_type:
11370       return "DW_TAG_const_type";
11371     case DW_TAG_constant:
11372       return "DW_TAG_constant";
11373     case DW_TAG_enumerator:
11374       return "DW_TAG_enumerator";
11375     case DW_TAG_file_type:
11376       return "DW_TAG_file_type";
11377     case DW_TAG_friend:
11378       return "DW_TAG_friend";
11379     case DW_TAG_namelist:
11380       return "DW_TAG_namelist";
11381     case DW_TAG_namelist_item:
11382       return "DW_TAG_namelist_item";
11383     case DW_TAG_packed_type:
11384       return "DW_TAG_packed_type";
11385     case DW_TAG_subprogram:
11386       return "DW_TAG_subprogram";
11387     case DW_TAG_template_type_param:
11388       return "DW_TAG_template_type_param";
11389     case DW_TAG_template_value_param:
11390       return "DW_TAG_template_value_param";
11391     case DW_TAG_thrown_type:
11392       return "DW_TAG_thrown_type";
11393     case DW_TAG_try_block:
11394       return "DW_TAG_try_block";
11395     case DW_TAG_variant_part:
11396       return "DW_TAG_variant_part";
11397     case DW_TAG_variable:
11398       return "DW_TAG_variable";
11399     case DW_TAG_volatile_type:
11400       return "DW_TAG_volatile_type";
11401     case DW_TAG_dwarf_procedure:
11402       return "DW_TAG_dwarf_procedure";
11403     case DW_TAG_restrict_type:
11404       return "DW_TAG_restrict_type";
11405     case DW_TAG_interface_type:
11406       return "DW_TAG_interface_type";
11407     case DW_TAG_namespace:
11408       return "DW_TAG_namespace";
11409     case DW_TAG_imported_module:
11410       return "DW_TAG_imported_module";
11411     case DW_TAG_unspecified_type:
11412       return "DW_TAG_unspecified_type";
11413     case DW_TAG_partial_unit:
11414       return "DW_TAG_partial_unit";
11415     case DW_TAG_imported_unit:
11416       return "DW_TAG_imported_unit";
11417     case DW_TAG_condition:
11418       return "DW_TAG_condition";
11419     case DW_TAG_shared_type:
11420       return "DW_TAG_shared_type";
11421     case DW_TAG_type_unit:
11422       return "DW_TAG_type_unit";
11423     case DW_TAG_MIPS_loop:
11424       return "DW_TAG_MIPS_loop";
11425     case DW_TAG_HP_array_descriptor:
11426       return "DW_TAG_HP_array_descriptor";
11427     case DW_TAG_format_label:
11428       return "DW_TAG_format_label";
11429     case DW_TAG_function_template:
11430       return "DW_TAG_function_template";
11431     case DW_TAG_class_template:
11432       return "DW_TAG_class_template";
11433     case DW_TAG_GNU_BINCL:
11434       return "DW_TAG_GNU_BINCL";
11435     case DW_TAG_GNU_EINCL:
11436       return "DW_TAG_GNU_EINCL";
11437     case DW_TAG_upc_shared_type:
11438       return "DW_TAG_upc_shared_type";
11439     case DW_TAG_upc_strict_type:
11440       return "DW_TAG_upc_strict_type";
11441     case DW_TAG_upc_relaxed_type:
11442       return "DW_TAG_upc_relaxed_type";
11443     case DW_TAG_PGI_kanji_type:
11444       return "DW_TAG_PGI_kanji_type";
11445     case DW_TAG_PGI_interface_block:
11446       return "DW_TAG_PGI_interface_block";
11447     default:
11448       return "DW_TAG_<unknown>";
11449     }
11450 }
11451
11452 /* Convert a DWARF attribute code into its string name.  */
11453
11454 static char *
11455 dwarf_attr_name (unsigned attr)
11456 {
11457   switch (attr)
11458     {
11459     case DW_AT_sibling:
11460       return "DW_AT_sibling";
11461     case DW_AT_location:
11462       return "DW_AT_location";
11463     case DW_AT_name:
11464       return "DW_AT_name";
11465     case DW_AT_ordering:
11466       return "DW_AT_ordering";
11467     case DW_AT_subscr_data:
11468       return "DW_AT_subscr_data";
11469     case DW_AT_byte_size:
11470       return "DW_AT_byte_size";
11471     case DW_AT_bit_offset:
11472       return "DW_AT_bit_offset";
11473     case DW_AT_bit_size:
11474       return "DW_AT_bit_size";
11475     case DW_AT_element_list:
11476       return "DW_AT_element_list";
11477     case DW_AT_stmt_list:
11478       return "DW_AT_stmt_list";
11479     case DW_AT_low_pc:
11480       return "DW_AT_low_pc";
11481     case DW_AT_high_pc:
11482       return "DW_AT_high_pc";
11483     case DW_AT_language:
11484       return "DW_AT_language";
11485     case DW_AT_member:
11486       return "DW_AT_member";
11487     case DW_AT_discr:
11488       return "DW_AT_discr";
11489     case DW_AT_discr_value:
11490       return "DW_AT_discr_value";
11491     case DW_AT_visibility:
11492       return "DW_AT_visibility";
11493     case DW_AT_import:
11494       return "DW_AT_import";
11495     case DW_AT_string_length:
11496       return "DW_AT_string_length";
11497     case DW_AT_common_reference:
11498       return "DW_AT_common_reference";
11499     case DW_AT_comp_dir:
11500       return "DW_AT_comp_dir";
11501     case DW_AT_const_value:
11502       return "DW_AT_const_value";
11503     case DW_AT_containing_type:
11504       return "DW_AT_containing_type";
11505     case DW_AT_default_value:
11506       return "DW_AT_default_value";
11507     case DW_AT_inline:
11508       return "DW_AT_inline";
11509     case DW_AT_is_optional:
11510       return "DW_AT_is_optional";
11511     case DW_AT_lower_bound:
11512       return "DW_AT_lower_bound";
11513     case DW_AT_producer:
11514       return "DW_AT_producer";
11515     case DW_AT_prototyped:
11516       return "DW_AT_prototyped";
11517     case DW_AT_return_addr:
11518       return "DW_AT_return_addr";
11519     case DW_AT_start_scope:
11520       return "DW_AT_start_scope";
11521     case DW_AT_bit_stride:
11522       return "DW_AT_bit_stride";
11523     case DW_AT_upper_bound:
11524       return "DW_AT_upper_bound";
11525     case DW_AT_abstract_origin:
11526       return "DW_AT_abstract_origin";
11527     case DW_AT_accessibility:
11528       return "DW_AT_accessibility";
11529     case DW_AT_address_class:
11530       return "DW_AT_address_class";
11531     case DW_AT_artificial:
11532       return "DW_AT_artificial";
11533     case DW_AT_base_types:
11534       return "DW_AT_base_types";
11535     case DW_AT_calling_convention:
11536       return "DW_AT_calling_convention";
11537     case DW_AT_count:
11538       return "DW_AT_count";
11539     case DW_AT_data_member_location:
11540       return "DW_AT_data_member_location";
11541     case DW_AT_decl_column:
11542       return "DW_AT_decl_column";
11543     case DW_AT_decl_file:
11544       return "DW_AT_decl_file";
11545     case DW_AT_decl_line:
11546       return "DW_AT_decl_line";
11547     case DW_AT_declaration:
11548       return "DW_AT_declaration";
11549     case DW_AT_discr_list:
11550       return "DW_AT_discr_list";
11551     case DW_AT_encoding:
11552       return "DW_AT_encoding";
11553     case DW_AT_external:
11554       return "DW_AT_external";
11555     case DW_AT_frame_base:
11556       return "DW_AT_frame_base";
11557     case DW_AT_friend:
11558       return "DW_AT_friend";
11559     case DW_AT_identifier_case:
11560       return "DW_AT_identifier_case";
11561     case DW_AT_macro_info:
11562       return "DW_AT_macro_info";
11563     case DW_AT_namelist_items:
11564       return "DW_AT_namelist_items";
11565     case DW_AT_priority:
11566       return "DW_AT_priority";
11567     case DW_AT_segment:
11568       return "DW_AT_segment";
11569     case DW_AT_specification:
11570       return "DW_AT_specification";
11571     case DW_AT_static_link:
11572       return "DW_AT_static_link";
11573     case DW_AT_type:
11574       return "DW_AT_type";
11575     case DW_AT_use_location:
11576       return "DW_AT_use_location";
11577     case DW_AT_variable_parameter:
11578       return "DW_AT_variable_parameter";
11579     case DW_AT_virtuality:
11580       return "DW_AT_virtuality";
11581     case DW_AT_vtable_elem_location:
11582       return "DW_AT_vtable_elem_location";
11583     /* DWARF 3 values.  */
11584     case DW_AT_allocated:
11585       return "DW_AT_allocated";
11586     case DW_AT_associated:
11587       return "DW_AT_associated";
11588     case DW_AT_data_location:
11589       return "DW_AT_data_location";
11590     case DW_AT_byte_stride:
11591       return "DW_AT_byte_stride";
11592     case DW_AT_entry_pc:
11593       return "DW_AT_entry_pc";
11594     case DW_AT_use_UTF8:
11595       return "DW_AT_use_UTF8";
11596     case DW_AT_extension:
11597       return "DW_AT_extension";
11598     case DW_AT_ranges:
11599       return "DW_AT_ranges";
11600     case DW_AT_trampoline:
11601       return "DW_AT_trampoline";
11602     case DW_AT_call_column:
11603       return "DW_AT_call_column";
11604     case DW_AT_call_file:
11605       return "DW_AT_call_file";
11606     case DW_AT_call_line:
11607       return "DW_AT_call_line";
11608     case DW_AT_description:
11609       return "DW_AT_description";
11610     case DW_AT_binary_scale:
11611       return "DW_AT_binary_scale";
11612     case DW_AT_decimal_scale:
11613       return "DW_AT_decimal_scale";
11614     case DW_AT_small:
11615       return "DW_AT_small";
11616     case DW_AT_decimal_sign:
11617       return "DW_AT_decimal_sign";
11618     case DW_AT_digit_count:
11619       return "DW_AT_digit_count";
11620     case DW_AT_picture_string:
11621       return "DW_AT_picture_string";
11622     case DW_AT_mutable:
11623       return "DW_AT_mutable";
11624     case DW_AT_threads_scaled:
11625       return "DW_AT_threads_scaled";
11626     case DW_AT_explicit:
11627       return "DW_AT_explicit";
11628     case DW_AT_object_pointer:
11629       return "DW_AT_object_pointer";
11630     case DW_AT_endianity:
11631       return "DW_AT_endianity";
11632     case DW_AT_elemental:
11633       return "DW_AT_elemental";
11634     case DW_AT_pure:
11635       return "DW_AT_pure";
11636     case DW_AT_recursive:
11637       return "DW_AT_recursive";
11638     /* DWARF 4 values.  */
11639     case DW_AT_signature:
11640       return "DW_AT_signature";
11641     case DW_AT_linkage_name:
11642       return "DW_AT_linkage_name";
11643     /* SGI/MIPS extensions.  */
11644 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11645     case DW_AT_MIPS_fde:
11646       return "DW_AT_MIPS_fde";
11647 #endif
11648     case DW_AT_MIPS_loop_begin:
11649       return "DW_AT_MIPS_loop_begin";
11650     case DW_AT_MIPS_tail_loop_begin:
11651       return "DW_AT_MIPS_tail_loop_begin";
11652     case DW_AT_MIPS_epilog_begin:
11653       return "DW_AT_MIPS_epilog_begin";
11654     case DW_AT_MIPS_loop_unroll_factor:
11655       return "DW_AT_MIPS_loop_unroll_factor";
11656     case DW_AT_MIPS_software_pipeline_depth:
11657       return "DW_AT_MIPS_software_pipeline_depth";
11658     case DW_AT_MIPS_linkage_name:
11659       return "DW_AT_MIPS_linkage_name";
11660     case DW_AT_MIPS_stride:
11661       return "DW_AT_MIPS_stride";
11662     case DW_AT_MIPS_abstract_name:
11663       return "DW_AT_MIPS_abstract_name";
11664     case DW_AT_MIPS_clone_origin:
11665       return "DW_AT_MIPS_clone_origin";
11666     case DW_AT_MIPS_has_inlines:
11667       return "DW_AT_MIPS_has_inlines";
11668     /* HP extensions.  */
11669 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11670     case DW_AT_HP_block_index:
11671       return "DW_AT_HP_block_index";
11672 #endif
11673     case DW_AT_HP_unmodifiable:
11674       return "DW_AT_HP_unmodifiable";
11675     case DW_AT_HP_actuals_stmt_list:
11676       return "DW_AT_HP_actuals_stmt_list";
11677     case DW_AT_HP_proc_per_section:
11678       return "DW_AT_HP_proc_per_section";
11679     case DW_AT_HP_raw_data_ptr:
11680       return "DW_AT_HP_raw_data_ptr";
11681     case DW_AT_HP_pass_by_reference:
11682       return "DW_AT_HP_pass_by_reference";
11683     case DW_AT_HP_opt_level:
11684       return "DW_AT_HP_opt_level";
11685     case DW_AT_HP_prof_version_id:
11686       return "DW_AT_HP_prof_version_id";
11687     case DW_AT_HP_opt_flags:
11688       return "DW_AT_HP_opt_flags";
11689     case DW_AT_HP_cold_region_low_pc:
11690       return "DW_AT_HP_cold_region_low_pc";
11691     case DW_AT_HP_cold_region_high_pc:
11692       return "DW_AT_HP_cold_region_high_pc";
11693     case DW_AT_HP_all_variables_modifiable:
11694       return "DW_AT_HP_all_variables_modifiable";
11695     case DW_AT_HP_linkage_name:
11696       return "DW_AT_HP_linkage_name";
11697     case DW_AT_HP_prof_flags:
11698       return "DW_AT_HP_prof_flags";
11699     /* GNU extensions.  */
11700     case DW_AT_sf_names:
11701       return "DW_AT_sf_names";
11702     case DW_AT_src_info:
11703       return "DW_AT_src_info";
11704     case DW_AT_mac_info:
11705       return "DW_AT_mac_info";
11706     case DW_AT_src_coords:
11707       return "DW_AT_src_coords";
11708     case DW_AT_body_begin:
11709       return "DW_AT_body_begin";
11710     case DW_AT_body_end:
11711       return "DW_AT_body_end";
11712     case DW_AT_GNU_vector:
11713       return "DW_AT_GNU_vector";
11714     case DW_AT_GNU_odr_signature:
11715       return "DW_AT_GNU_odr_signature";
11716     /* VMS extensions.  */
11717     case DW_AT_VMS_rtnbeg_pd_address:
11718       return "DW_AT_VMS_rtnbeg_pd_address";
11719     /* UPC extension.  */
11720     case DW_AT_upc_threads_scaled:
11721       return "DW_AT_upc_threads_scaled";
11722     /* PGI (STMicroelectronics) extensions.  */
11723     case DW_AT_PGI_lbase:
11724       return "DW_AT_PGI_lbase";
11725     case DW_AT_PGI_soffset:
11726       return "DW_AT_PGI_soffset";
11727     case DW_AT_PGI_lstride:
11728       return "DW_AT_PGI_lstride";
11729     default:
11730       return "DW_AT_<unknown>";
11731     }
11732 }
11733
11734 /* Convert a DWARF value form code into its string name.  */
11735
11736 static char *
11737 dwarf_form_name (unsigned form)
11738 {
11739   switch (form)
11740     {
11741     case DW_FORM_addr:
11742       return "DW_FORM_addr";
11743     case DW_FORM_block2:
11744       return "DW_FORM_block2";
11745     case DW_FORM_block4:
11746       return "DW_FORM_block4";
11747     case DW_FORM_data2:
11748       return "DW_FORM_data2";
11749     case DW_FORM_data4:
11750       return "DW_FORM_data4";
11751     case DW_FORM_data8:
11752       return "DW_FORM_data8";
11753     case DW_FORM_string:
11754       return "DW_FORM_string";
11755     case DW_FORM_block:
11756       return "DW_FORM_block";
11757     case DW_FORM_block1:
11758       return "DW_FORM_block1";
11759     case DW_FORM_data1:
11760       return "DW_FORM_data1";
11761     case DW_FORM_flag:
11762       return "DW_FORM_flag";
11763     case DW_FORM_sdata:
11764       return "DW_FORM_sdata";
11765     case DW_FORM_strp:
11766       return "DW_FORM_strp";
11767     case DW_FORM_udata:
11768       return "DW_FORM_udata";
11769     case DW_FORM_ref_addr:
11770       return "DW_FORM_ref_addr";
11771     case DW_FORM_ref1:
11772       return "DW_FORM_ref1";
11773     case DW_FORM_ref2:
11774       return "DW_FORM_ref2";
11775     case DW_FORM_ref4:
11776       return "DW_FORM_ref4";
11777     case DW_FORM_ref8:
11778       return "DW_FORM_ref8";
11779     case DW_FORM_ref_udata:
11780       return "DW_FORM_ref_udata";
11781     case DW_FORM_indirect:
11782       return "DW_FORM_indirect";
11783     case DW_FORM_sec_offset:
11784       return "DW_FORM_sec_offset";
11785     case DW_FORM_exprloc:
11786       return "DW_FORM_exprloc";
11787     case DW_FORM_flag_present:
11788       return "DW_FORM_flag_present";
11789     case DW_FORM_sig8:
11790       return "DW_FORM_sig8";
11791     default:
11792       return "DW_FORM_<unknown>";
11793     }
11794 }
11795
11796 /* Convert a DWARF stack opcode into its string name.  */
11797
11798 const char *
11799 dwarf_stack_op_name (unsigned op, int def)
11800 {
11801   switch (op)
11802     {
11803     case DW_OP_addr:
11804       return "DW_OP_addr";
11805     case DW_OP_deref:
11806       return "DW_OP_deref";
11807     case DW_OP_const1u:
11808       return "DW_OP_const1u";
11809     case DW_OP_const1s:
11810       return "DW_OP_const1s";
11811     case DW_OP_const2u:
11812       return "DW_OP_const2u";
11813     case DW_OP_const2s:
11814       return "DW_OP_const2s";
11815     case DW_OP_const4u:
11816       return "DW_OP_const4u";
11817     case DW_OP_const4s:
11818       return "DW_OP_const4s";
11819     case DW_OP_const8u:
11820       return "DW_OP_const8u";
11821     case DW_OP_const8s:
11822       return "DW_OP_const8s";
11823     case DW_OP_constu:
11824       return "DW_OP_constu";
11825     case DW_OP_consts:
11826       return "DW_OP_consts";
11827     case DW_OP_dup:
11828       return "DW_OP_dup";
11829     case DW_OP_drop:
11830       return "DW_OP_drop";
11831     case DW_OP_over:
11832       return "DW_OP_over";
11833     case DW_OP_pick:
11834       return "DW_OP_pick";
11835     case DW_OP_swap:
11836       return "DW_OP_swap";
11837     case DW_OP_rot:
11838       return "DW_OP_rot";
11839     case DW_OP_xderef:
11840       return "DW_OP_xderef";
11841     case DW_OP_abs:
11842       return "DW_OP_abs";
11843     case DW_OP_and:
11844       return "DW_OP_and";
11845     case DW_OP_div:
11846       return "DW_OP_div";
11847     case DW_OP_minus:
11848       return "DW_OP_minus";
11849     case DW_OP_mod:
11850       return "DW_OP_mod";
11851     case DW_OP_mul:
11852       return "DW_OP_mul";
11853     case DW_OP_neg:
11854       return "DW_OP_neg";
11855     case DW_OP_not:
11856       return "DW_OP_not";
11857     case DW_OP_or:
11858       return "DW_OP_or";
11859     case DW_OP_plus:
11860       return "DW_OP_plus";
11861     case DW_OP_plus_uconst:
11862       return "DW_OP_plus_uconst";
11863     case DW_OP_shl:
11864       return "DW_OP_shl";
11865     case DW_OP_shr:
11866       return "DW_OP_shr";
11867     case DW_OP_shra:
11868       return "DW_OP_shra";
11869     case DW_OP_xor:
11870       return "DW_OP_xor";
11871     case DW_OP_bra:
11872       return "DW_OP_bra";
11873     case DW_OP_eq:
11874       return "DW_OP_eq";
11875     case DW_OP_ge:
11876       return "DW_OP_ge";
11877     case DW_OP_gt:
11878       return "DW_OP_gt";
11879     case DW_OP_le:
11880       return "DW_OP_le";
11881     case DW_OP_lt:
11882       return "DW_OP_lt";
11883     case DW_OP_ne:
11884       return "DW_OP_ne";
11885     case DW_OP_skip:
11886       return "DW_OP_skip";
11887     case DW_OP_lit0:
11888       return "DW_OP_lit0";
11889     case DW_OP_lit1:
11890       return "DW_OP_lit1";
11891     case DW_OP_lit2:
11892       return "DW_OP_lit2";
11893     case DW_OP_lit3:
11894       return "DW_OP_lit3";
11895     case DW_OP_lit4:
11896       return "DW_OP_lit4";
11897     case DW_OP_lit5:
11898       return "DW_OP_lit5";
11899     case DW_OP_lit6:
11900       return "DW_OP_lit6";
11901     case DW_OP_lit7:
11902       return "DW_OP_lit7";
11903     case DW_OP_lit8:
11904       return "DW_OP_lit8";
11905     case DW_OP_lit9:
11906       return "DW_OP_lit9";
11907     case DW_OP_lit10:
11908       return "DW_OP_lit10";
11909     case DW_OP_lit11:
11910       return "DW_OP_lit11";
11911     case DW_OP_lit12:
11912       return "DW_OP_lit12";
11913     case DW_OP_lit13:
11914       return "DW_OP_lit13";
11915     case DW_OP_lit14:
11916       return "DW_OP_lit14";
11917     case DW_OP_lit15:
11918       return "DW_OP_lit15";
11919     case DW_OP_lit16:
11920       return "DW_OP_lit16";
11921     case DW_OP_lit17:
11922       return "DW_OP_lit17";
11923     case DW_OP_lit18:
11924       return "DW_OP_lit18";
11925     case DW_OP_lit19:
11926       return "DW_OP_lit19";
11927     case DW_OP_lit20:
11928       return "DW_OP_lit20";
11929     case DW_OP_lit21:
11930       return "DW_OP_lit21";
11931     case DW_OP_lit22:
11932       return "DW_OP_lit22";
11933     case DW_OP_lit23:
11934       return "DW_OP_lit23";
11935     case DW_OP_lit24:
11936       return "DW_OP_lit24";
11937     case DW_OP_lit25:
11938       return "DW_OP_lit25";
11939     case DW_OP_lit26:
11940       return "DW_OP_lit26";
11941     case DW_OP_lit27:
11942       return "DW_OP_lit27";
11943     case DW_OP_lit28:
11944       return "DW_OP_lit28";
11945     case DW_OP_lit29:
11946       return "DW_OP_lit29";
11947     case DW_OP_lit30:
11948       return "DW_OP_lit30";
11949     case DW_OP_lit31:
11950       return "DW_OP_lit31";
11951     case DW_OP_reg0:
11952       return "DW_OP_reg0";
11953     case DW_OP_reg1:
11954       return "DW_OP_reg1";
11955     case DW_OP_reg2:
11956       return "DW_OP_reg2";
11957     case DW_OP_reg3:
11958       return "DW_OP_reg3";
11959     case DW_OP_reg4:
11960       return "DW_OP_reg4";
11961     case DW_OP_reg5:
11962       return "DW_OP_reg5";
11963     case DW_OP_reg6:
11964       return "DW_OP_reg6";
11965     case DW_OP_reg7:
11966       return "DW_OP_reg7";
11967     case DW_OP_reg8:
11968       return "DW_OP_reg8";
11969     case DW_OP_reg9:
11970       return "DW_OP_reg9";
11971     case DW_OP_reg10:
11972       return "DW_OP_reg10";
11973     case DW_OP_reg11:
11974       return "DW_OP_reg11";
11975     case DW_OP_reg12:
11976       return "DW_OP_reg12";
11977     case DW_OP_reg13:
11978       return "DW_OP_reg13";
11979     case DW_OP_reg14:
11980       return "DW_OP_reg14";
11981     case DW_OP_reg15:
11982       return "DW_OP_reg15";
11983     case DW_OP_reg16:
11984       return "DW_OP_reg16";
11985     case DW_OP_reg17:
11986       return "DW_OP_reg17";
11987     case DW_OP_reg18:
11988       return "DW_OP_reg18";
11989     case DW_OP_reg19:
11990       return "DW_OP_reg19";
11991     case DW_OP_reg20:
11992       return "DW_OP_reg20";
11993     case DW_OP_reg21:
11994       return "DW_OP_reg21";
11995     case DW_OP_reg22:
11996       return "DW_OP_reg22";
11997     case DW_OP_reg23:
11998       return "DW_OP_reg23";
11999     case DW_OP_reg24:
12000       return "DW_OP_reg24";
12001     case DW_OP_reg25:
12002       return "DW_OP_reg25";
12003     case DW_OP_reg26:
12004       return "DW_OP_reg26";
12005     case DW_OP_reg27:
12006       return "DW_OP_reg27";
12007     case DW_OP_reg28:
12008       return "DW_OP_reg28";
12009     case DW_OP_reg29:
12010       return "DW_OP_reg29";
12011     case DW_OP_reg30:
12012       return "DW_OP_reg30";
12013     case DW_OP_reg31:
12014       return "DW_OP_reg31";
12015     case DW_OP_breg0:
12016       return "DW_OP_breg0";
12017     case DW_OP_breg1:
12018       return "DW_OP_breg1";
12019     case DW_OP_breg2:
12020       return "DW_OP_breg2";
12021     case DW_OP_breg3:
12022       return "DW_OP_breg3";
12023     case DW_OP_breg4:
12024       return "DW_OP_breg4";
12025     case DW_OP_breg5:
12026       return "DW_OP_breg5";
12027     case DW_OP_breg6:
12028       return "DW_OP_breg6";
12029     case DW_OP_breg7:
12030       return "DW_OP_breg7";
12031     case DW_OP_breg8:
12032       return "DW_OP_breg8";
12033     case DW_OP_breg9:
12034       return "DW_OP_breg9";
12035     case DW_OP_breg10:
12036       return "DW_OP_breg10";
12037     case DW_OP_breg11:
12038       return "DW_OP_breg11";
12039     case DW_OP_breg12:
12040       return "DW_OP_breg12";
12041     case DW_OP_breg13:
12042       return "DW_OP_breg13";
12043     case DW_OP_breg14:
12044       return "DW_OP_breg14";
12045     case DW_OP_breg15:
12046       return "DW_OP_breg15";
12047     case DW_OP_breg16:
12048       return "DW_OP_breg16";
12049     case DW_OP_breg17:
12050       return "DW_OP_breg17";
12051     case DW_OP_breg18:
12052       return "DW_OP_breg18";
12053     case DW_OP_breg19:
12054       return "DW_OP_breg19";
12055     case DW_OP_breg20:
12056       return "DW_OP_breg20";
12057     case DW_OP_breg21:
12058       return "DW_OP_breg21";
12059     case DW_OP_breg22:
12060       return "DW_OP_breg22";
12061     case DW_OP_breg23:
12062       return "DW_OP_breg23";
12063     case DW_OP_breg24:
12064       return "DW_OP_breg24";
12065     case DW_OP_breg25:
12066       return "DW_OP_breg25";
12067     case DW_OP_breg26:
12068       return "DW_OP_breg26";
12069     case DW_OP_breg27:
12070       return "DW_OP_breg27";
12071     case DW_OP_breg28:
12072       return "DW_OP_breg28";
12073     case DW_OP_breg29:
12074       return "DW_OP_breg29";
12075     case DW_OP_breg30:
12076       return "DW_OP_breg30";
12077     case DW_OP_breg31:
12078       return "DW_OP_breg31";
12079     case DW_OP_regx:
12080       return "DW_OP_regx";
12081     case DW_OP_fbreg:
12082       return "DW_OP_fbreg";
12083     case DW_OP_bregx:
12084       return "DW_OP_bregx";
12085     case DW_OP_piece:
12086       return "DW_OP_piece";
12087     case DW_OP_deref_size:
12088       return "DW_OP_deref_size";
12089     case DW_OP_xderef_size:
12090       return "DW_OP_xderef_size";
12091     case DW_OP_nop:
12092       return "DW_OP_nop";
12093     /* DWARF 3 extensions.  */
12094     case DW_OP_push_object_address:
12095       return "DW_OP_push_object_address";
12096     case DW_OP_call2:
12097       return "DW_OP_call2";
12098     case DW_OP_call4:
12099       return "DW_OP_call4";
12100     case DW_OP_call_ref:
12101       return "DW_OP_call_ref";
12102     case DW_OP_form_tls_address:
12103       return "DW_OP_form_tls_address";
12104     case DW_OP_call_frame_cfa:
12105       return "DW_OP_call_frame_cfa";
12106     case DW_OP_bit_piece:
12107       return "DW_OP_bit_piece";
12108     /* DWARF 4 extensions.  */
12109     case DW_OP_implicit_value:
12110       return "DW_OP_implicit_value";
12111     case DW_OP_stack_value:
12112       return "DW_OP_stack_value";
12113     /* GNU extensions.  */
12114     case DW_OP_GNU_push_tls_address:
12115       return "DW_OP_GNU_push_tls_address";
12116     case DW_OP_GNU_uninit:
12117       return "DW_OP_GNU_uninit";
12118     default:
12119       return def ? "OP_<unknown>" : NULL;
12120     }
12121 }
12122
12123 static char *
12124 dwarf_bool_name (unsigned mybool)
12125 {
12126   if (mybool)
12127     return "TRUE";
12128   else
12129     return "FALSE";
12130 }
12131
12132 /* Convert a DWARF type code into its string name.  */
12133
12134 static char *
12135 dwarf_type_encoding_name (unsigned enc)
12136 {
12137   switch (enc)
12138     {
12139     case DW_ATE_void:
12140       return "DW_ATE_void";
12141     case DW_ATE_address:
12142       return "DW_ATE_address";
12143     case DW_ATE_boolean:
12144       return "DW_ATE_boolean";
12145     case DW_ATE_complex_float:
12146       return "DW_ATE_complex_float";
12147     case DW_ATE_float:
12148       return "DW_ATE_float";
12149     case DW_ATE_signed:
12150       return "DW_ATE_signed";
12151     case DW_ATE_signed_char:
12152       return "DW_ATE_signed_char";
12153     case DW_ATE_unsigned:
12154       return "DW_ATE_unsigned";
12155     case DW_ATE_unsigned_char:
12156       return "DW_ATE_unsigned_char";
12157     /* DWARF 3.  */
12158     case DW_ATE_imaginary_float:
12159       return "DW_ATE_imaginary_float";
12160     case DW_ATE_packed_decimal:
12161       return "DW_ATE_packed_decimal";
12162     case DW_ATE_numeric_string:
12163       return "DW_ATE_numeric_string";
12164     case DW_ATE_edited:
12165       return "DW_ATE_edited";
12166     case DW_ATE_signed_fixed:
12167       return "DW_ATE_signed_fixed";
12168     case DW_ATE_unsigned_fixed:
12169       return "DW_ATE_unsigned_fixed";
12170     case DW_ATE_decimal_float:
12171       return "DW_ATE_decimal_float";
12172     /* DWARF 4.  */
12173     case DW_ATE_UTF:
12174       return "DW_ATE_UTF";
12175     /* HP extensions.  */
12176     case DW_ATE_HP_float80:
12177       return "DW_ATE_HP_float80";
12178     case DW_ATE_HP_complex_float80:
12179       return "DW_ATE_HP_complex_float80";
12180     case DW_ATE_HP_float128:
12181       return "DW_ATE_HP_float128";
12182     case DW_ATE_HP_complex_float128:
12183       return "DW_ATE_HP_complex_float128";
12184     case DW_ATE_HP_floathpintel:
12185       return "DW_ATE_HP_floathpintel";
12186     case DW_ATE_HP_imaginary_float80:
12187       return "DW_ATE_HP_imaginary_float80";
12188     case DW_ATE_HP_imaginary_float128:
12189       return "DW_ATE_HP_imaginary_float128";
12190     default:
12191       return "DW_ATE_<unknown>";
12192     }
12193 }
12194
12195 /* Convert a DWARF call frame info operation to its string name. */
12196
12197 #if 0
12198 static char *
12199 dwarf_cfi_name (unsigned cfi_opc)
12200 {
12201   switch (cfi_opc)
12202     {
12203     case DW_CFA_advance_loc:
12204       return "DW_CFA_advance_loc";
12205     case DW_CFA_offset:
12206       return "DW_CFA_offset";
12207     case DW_CFA_restore:
12208       return "DW_CFA_restore";
12209     case DW_CFA_nop:
12210       return "DW_CFA_nop";
12211     case DW_CFA_set_loc:
12212       return "DW_CFA_set_loc";
12213     case DW_CFA_advance_loc1:
12214       return "DW_CFA_advance_loc1";
12215     case DW_CFA_advance_loc2:
12216       return "DW_CFA_advance_loc2";
12217     case DW_CFA_advance_loc4:
12218       return "DW_CFA_advance_loc4";
12219     case DW_CFA_offset_extended:
12220       return "DW_CFA_offset_extended";
12221     case DW_CFA_restore_extended:
12222       return "DW_CFA_restore_extended";
12223     case DW_CFA_undefined:
12224       return "DW_CFA_undefined";
12225     case DW_CFA_same_value:
12226       return "DW_CFA_same_value";
12227     case DW_CFA_register:
12228       return "DW_CFA_register";
12229     case DW_CFA_remember_state:
12230       return "DW_CFA_remember_state";
12231     case DW_CFA_restore_state:
12232       return "DW_CFA_restore_state";
12233     case DW_CFA_def_cfa:
12234       return "DW_CFA_def_cfa";
12235     case DW_CFA_def_cfa_register:
12236       return "DW_CFA_def_cfa_register";
12237     case DW_CFA_def_cfa_offset:
12238       return "DW_CFA_def_cfa_offset";
12239     /* DWARF 3.  */
12240     case DW_CFA_def_cfa_expression:
12241       return "DW_CFA_def_cfa_expression";
12242     case DW_CFA_expression:
12243       return "DW_CFA_expression";
12244     case DW_CFA_offset_extended_sf:
12245       return "DW_CFA_offset_extended_sf";
12246     case DW_CFA_def_cfa_sf:
12247       return "DW_CFA_def_cfa_sf";
12248     case DW_CFA_def_cfa_offset_sf:
12249       return "DW_CFA_def_cfa_offset_sf";
12250     case DW_CFA_val_offset:
12251       return "DW_CFA_val_offset";
12252     case DW_CFA_val_offset_sf:
12253       return "DW_CFA_val_offset_sf";
12254     case DW_CFA_val_expression:
12255       return "DW_CFA_val_expression";
12256     /* SGI/MIPS specific.  */
12257     case DW_CFA_MIPS_advance_loc8:
12258       return "DW_CFA_MIPS_advance_loc8";
12259     /* GNU extensions.  */
12260     case DW_CFA_GNU_window_save:
12261       return "DW_CFA_GNU_window_save";
12262     case DW_CFA_GNU_args_size:
12263       return "DW_CFA_GNU_args_size";
12264     case DW_CFA_GNU_negative_offset_extended:
12265       return "DW_CFA_GNU_negative_offset_extended";
12266     default:
12267       return "DW_CFA_<unknown>";
12268     }
12269 }
12270 #endif
12271
12272 static void
12273 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12274 {
12275   unsigned int i;
12276
12277   print_spaces (indent, f);
12278   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12279            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12280
12281   if (die->parent != NULL)
12282     {
12283       print_spaces (indent, f);
12284       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12285                           die->parent->offset);
12286     }
12287
12288   print_spaces (indent, f);
12289   fprintf_unfiltered (f, "  has children: %s\n",
12290            dwarf_bool_name (die->child != NULL));
12291
12292   print_spaces (indent, f);
12293   fprintf_unfiltered (f, "  attributes:\n");
12294
12295   for (i = 0; i < die->num_attrs; ++i)
12296     {
12297       print_spaces (indent, f);
12298       fprintf_unfiltered (f, "    %s (%s) ",
12299                dwarf_attr_name (die->attrs[i].name),
12300                dwarf_form_name (die->attrs[i].form));
12301
12302       switch (die->attrs[i].form)
12303         {
12304         case DW_FORM_ref_addr:
12305         case DW_FORM_addr:
12306           fprintf_unfiltered (f, "address: ");
12307           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12308           break;
12309         case DW_FORM_block2:
12310         case DW_FORM_block4:
12311         case DW_FORM_block:
12312         case DW_FORM_block1:
12313           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12314           break;
12315         case DW_FORM_exprloc:
12316           fprintf_unfiltered (f, "expression: size %u",
12317                               DW_BLOCK (&die->attrs[i])->size);
12318           break;
12319         case DW_FORM_ref1:
12320         case DW_FORM_ref2:
12321         case DW_FORM_ref4:
12322           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12323                               (long) (DW_ADDR (&die->attrs[i])));
12324           break;
12325         case DW_FORM_data1:
12326         case DW_FORM_data2:
12327         case DW_FORM_data4:
12328         case DW_FORM_data8:
12329         case DW_FORM_udata:
12330         case DW_FORM_sdata:
12331           fprintf_unfiltered (f, "constant: %s",
12332                               pulongest (DW_UNSND (&die->attrs[i])));
12333           break;
12334         case DW_FORM_sec_offset:
12335           fprintf_unfiltered (f, "section offset: %s",
12336                               pulongest (DW_UNSND (&die->attrs[i])));
12337           break;
12338         case DW_FORM_sig8:
12339           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12340             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12341                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12342           else
12343             fprintf_unfiltered (f, "signatured type, offset: unknown");
12344           break;
12345         case DW_FORM_string:
12346         case DW_FORM_strp:
12347           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12348                    DW_STRING (&die->attrs[i])
12349                    ? DW_STRING (&die->attrs[i]) : "",
12350                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12351           break;
12352         case DW_FORM_flag:
12353           if (DW_UNSND (&die->attrs[i]))
12354             fprintf_unfiltered (f, "flag: TRUE");
12355           else
12356             fprintf_unfiltered (f, "flag: FALSE");
12357           break;
12358         case DW_FORM_flag_present:
12359           fprintf_unfiltered (f, "flag: TRUE");
12360           break;
12361         case DW_FORM_indirect:
12362           /* the reader will have reduced the indirect form to
12363              the "base form" so this form should not occur */
12364           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12365           break;
12366         default:
12367           fprintf_unfiltered (f, "unsupported attribute form: %d.",
12368                    die->attrs[i].form);
12369           break;
12370         }
12371       fprintf_unfiltered (f, "\n");
12372     }
12373 }
12374
12375 static void
12376 dump_die_for_error (struct die_info *die)
12377 {
12378   dump_die_shallow (gdb_stderr, 0, die);
12379 }
12380
12381 static void
12382 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12383 {
12384   int indent = level * 4;
12385
12386   gdb_assert (die != NULL);
12387
12388   if (level >= max_level)
12389     return;
12390
12391   dump_die_shallow (f, indent, die);
12392
12393   if (die->child != NULL)
12394     {
12395       print_spaces (indent, f);
12396       fprintf_unfiltered (f, "  Children:");
12397       if (level + 1 < max_level)
12398         {
12399           fprintf_unfiltered (f, "\n");
12400           dump_die_1 (f, level + 1, max_level, die->child);
12401         }
12402       else
12403         {
12404           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12405         }
12406     }
12407
12408   if (die->sibling != NULL && level > 0)
12409     {
12410       dump_die_1 (f, level, max_level, die->sibling);
12411     }
12412 }
12413
12414 /* This is called from the pdie macro in gdbinit.in.
12415    It's not static so gcc will keep a copy callable from gdb.  */
12416
12417 void
12418 dump_die (struct die_info *die, int max_level)
12419 {
12420   dump_die_1 (gdb_stdlog, 0, max_level, die);
12421 }
12422
12423 static void
12424 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12425 {
12426   void **slot;
12427
12428   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12429
12430   *slot = die;
12431 }
12432
12433 static int
12434 is_ref_attr (struct attribute *attr)
12435 {
12436   switch (attr->form)
12437     {
12438     case DW_FORM_ref_addr:
12439     case DW_FORM_ref1:
12440     case DW_FORM_ref2:
12441     case DW_FORM_ref4:
12442     case DW_FORM_ref8:
12443     case DW_FORM_ref_udata:
12444       return 1;
12445     default:
12446       return 0;
12447     }
12448 }
12449
12450 static unsigned int
12451 dwarf2_get_ref_die_offset (struct attribute *attr)
12452 {
12453   if (is_ref_attr (attr))
12454     return DW_ADDR (attr);
12455
12456   complaint (&symfile_complaints,
12457              _("unsupported die ref attribute form: '%s'"),
12458              dwarf_form_name (attr->form));
12459   return 0;
12460 }
12461
12462 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
12463  * the value held by the attribute is not constant.  */
12464
12465 static LONGEST
12466 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12467 {
12468   if (attr->form == DW_FORM_sdata)
12469     return DW_SND (attr);
12470   else if (attr->form == DW_FORM_udata
12471            || attr->form == DW_FORM_data1
12472            || attr->form == DW_FORM_data2
12473            || attr->form == DW_FORM_data4
12474            || attr->form == DW_FORM_data8)
12475     return DW_UNSND (attr);
12476   else
12477     {
12478       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12479                  dwarf_form_name (attr->form));
12480       return default_value;
12481     }
12482 }
12483
12484 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
12485    unit and add it to our queue.
12486    The result is non-zero if PER_CU was queued, otherwise the result is zero
12487    meaning either PER_CU is already queued or it is already loaded.  */
12488
12489 static int
12490 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12491                        struct dwarf2_per_cu_data *per_cu)
12492 {
12493   /* We may arrive here during partial symbol reading, if we need full
12494      DIEs to process an unusual case (e.g. template arguments).  Do
12495      not queue PER_CU, just tell our caller to load its DIEs.  */
12496   if (dwarf2_per_objfile->reading_partial_symbols)
12497     {
12498       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12499         return 1;
12500       return 0;
12501     }
12502
12503   /* Mark the dependence relation so that we don't flush PER_CU
12504      too early.  */
12505   dwarf2_add_dependence (this_cu, per_cu);
12506
12507   /* If it's already on the queue, we have nothing to do.  */
12508   if (per_cu->queued)
12509     return 0;
12510
12511   /* If the compilation unit is already loaded, just mark it as
12512      used.  */
12513   if (per_cu->cu != NULL)
12514     {
12515       per_cu->cu->last_used = 0;
12516       return 0;
12517     }
12518
12519   /* Add it to the queue.  */
12520   queue_comp_unit (per_cu, this_cu->objfile);
12521
12522   return 1;
12523 }
12524
12525 /* Follow reference or signature attribute ATTR of SRC_DIE.
12526    On entry *REF_CU is the CU of SRC_DIE.
12527    On exit *REF_CU is the CU of the result.  */
12528
12529 static struct die_info *
12530 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12531                        struct dwarf2_cu **ref_cu)
12532 {
12533   struct die_info *die;
12534
12535   if (is_ref_attr (attr))
12536     die = follow_die_ref (src_die, attr, ref_cu);
12537   else if (attr->form == DW_FORM_sig8)
12538     die = follow_die_sig (src_die, attr, ref_cu);
12539   else
12540     {
12541       dump_die_for_error (src_die);
12542       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12543              (*ref_cu)->objfile->name);
12544     }
12545
12546   return die;
12547 }
12548
12549 /* Follow reference OFFSET.
12550    On entry *REF_CU is the CU of source DIE referencing OFFSET.
12551    On exit *REF_CU is the CU of the result.  */
12552
12553 static struct die_info *
12554 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12555 {
12556   struct die_info temp_die;
12557   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12558
12559   gdb_assert (cu->per_cu != NULL);
12560
12561   target_cu = cu;
12562
12563   if (cu->per_cu->from_debug_types)
12564     {
12565       /* .debug_types CUs cannot reference anything outside their CU.
12566          If they need to, they have to reference a signatured type via
12567          DW_FORM_sig8.  */
12568       if (! offset_in_cu_p (&cu->header, offset))
12569         return NULL;
12570     }
12571   else if (! offset_in_cu_p (&cu->header, offset))
12572     {
12573       struct dwarf2_per_cu_data *per_cu;
12574
12575       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12576
12577       /* If necessary, add it to the queue and load its DIEs.  */
12578       if (maybe_queue_comp_unit (cu, per_cu))
12579         load_full_comp_unit (per_cu, cu->objfile);
12580
12581       target_cu = per_cu->cu;
12582     }
12583   else if (cu->dies == NULL)
12584     {
12585       /* We're loading full DIEs during partial symbol reading.  */
12586       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12587       load_full_comp_unit (cu->per_cu, cu->objfile);
12588     }
12589
12590   *ref_cu = target_cu;
12591   temp_die.offset = offset;
12592   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12593 }
12594
12595 /* Follow reference attribute ATTR of SRC_DIE.
12596    On entry *REF_CU is the CU of SRC_DIE.
12597    On exit *REF_CU is the CU of the result.  */
12598
12599 static struct die_info *
12600 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12601                 struct dwarf2_cu **ref_cu)
12602 {
12603   unsigned int offset = dwarf2_get_ref_die_offset (attr);
12604   struct dwarf2_cu *cu = *ref_cu;
12605   struct die_info *die;
12606
12607   die = follow_die_offset (offset, ref_cu);
12608   if (!die)
12609     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12610            "at 0x%x [in module %s]"),
12611            offset, src_die->offset, cu->objfile->name);
12612
12613   return die;
12614 }
12615
12616 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
12617    value is intended for DW_OP_call*.  */
12618
12619 struct dwarf2_locexpr_baton
12620 dwarf2_fetch_die_location_block (unsigned int offset,
12621                                  struct dwarf2_per_cu_data *per_cu)
12622 {
12623   struct dwarf2_cu *cu = per_cu->cu;
12624   struct die_info *die;
12625   struct attribute *attr;
12626   struct dwarf2_locexpr_baton retval;
12627
12628   die = follow_die_offset (offset, &cu);
12629   if (!die)
12630     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12631            offset, per_cu->cu->objfile->name);
12632
12633   attr = dwarf2_attr (die, DW_AT_location, cu);
12634   if (!attr)
12635     {
12636       /* DWARF: "If there is no such attribute, then there is no effect.".  */
12637
12638       retval.data = NULL;
12639       retval.size = 0;
12640     }
12641   else
12642     {
12643       if (!attr_form_is_block (attr))
12644         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12645                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12646                offset, per_cu->cu->objfile->name);
12647
12648       retval.data = DW_BLOCK (attr)->data;
12649       retval.size = DW_BLOCK (attr)->size;
12650     }
12651   retval.per_cu = cu->per_cu;
12652   return retval;
12653 }
12654
12655 /* Follow the signature attribute ATTR in SRC_DIE.
12656    On entry *REF_CU is the CU of SRC_DIE.
12657    On exit *REF_CU is the CU of the result.  */
12658
12659 static struct die_info *
12660 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12661                 struct dwarf2_cu **ref_cu)
12662 {
12663   struct objfile *objfile = (*ref_cu)->objfile;
12664   struct die_info temp_die;
12665   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12666   struct dwarf2_cu *sig_cu;
12667   struct die_info *die;
12668
12669   /* sig_type will be NULL if the signatured type is missing from
12670      the debug info.  */
12671   if (sig_type == NULL)
12672     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12673              "at 0x%x [in module %s]"),
12674            src_die->offset, objfile->name);
12675
12676   /* If necessary, add it to the queue and load its DIEs.  */
12677
12678   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12679     read_signatured_type (objfile, sig_type);
12680
12681   gdb_assert (sig_type->per_cu.cu != NULL);
12682
12683   sig_cu = sig_type->per_cu.cu;
12684   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12685   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12686   if (die)
12687     {
12688       *ref_cu = sig_cu;
12689       return die;
12690     }
12691
12692   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12693          "at 0x%x [in module %s]"),
12694          sig_type->type_offset, src_die->offset, objfile->name);
12695 }
12696
12697 /* Given an offset of a signatured type, return its signatured_type.  */
12698
12699 static struct signatured_type *
12700 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12701 {
12702   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12703   unsigned int length, initial_length_size;
12704   unsigned int sig_offset;
12705   struct signatured_type find_entry, *type_sig;
12706
12707   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12708   sig_offset = (initial_length_size
12709                 + 2 /*version*/
12710                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12711                 + 1 /*address_size*/);
12712   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12713   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12714
12715   /* This is only used to lookup previously recorded types.
12716      If we didn't find it, it's our bug.  */
12717   gdb_assert (type_sig != NULL);
12718   gdb_assert (offset == type_sig->offset);
12719
12720   return type_sig;
12721 }
12722
12723 /* Read in signatured type at OFFSET and build its CU and die(s).  */
12724
12725 static void
12726 read_signatured_type_at_offset (struct objfile *objfile,
12727                                 unsigned int offset)
12728 {
12729   struct signatured_type *type_sig;
12730
12731   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12732
12733   /* We have the section offset, but we need the signature to do the
12734      hash table lookup.  */
12735   type_sig = lookup_signatured_type_at_offset (objfile, offset);
12736
12737   gdb_assert (type_sig->per_cu.cu == NULL);
12738
12739   read_signatured_type (objfile, type_sig);
12740
12741   gdb_assert (type_sig->per_cu.cu != NULL);
12742 }
12743
12744 /* Read in a signatured type and build its CU and DIEs.  */
12745
12746 static void
12747 read_signatured_type (struct objfile *objfile,
12748                       struct signatured_type *type_sig)
12749 {
12750   gdb_byte *types_ptr;
12751   struct die_reader_specs reader_specs;
12752   struct dwarf2_cu *cu;
12753   ULONGEST signature;
12754   struct cleanup *back_to, *free_cu_cleanup;
12755   struct attribute *attr;
12756
12757   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12758   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12759
12760   gdb_assert (type_sig->per_cu.cu == NULL);
12761
12762   cu = xmalloc (sizeof (struct dwarf2_cu));
12763   memset (cu, 0, sizeof (struct dwarf2_cu));
12764   obstack_init (&cu->comp_unit_obstack);
12765   cu->objfile = objfile;
12766   type_sig->per_cu.cu = cu;
12767   cu->per_cu = &type_sig->per_cu;
12768
12769   /* If an error occurs while loading, release our storage.  */
12770   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12771
12772   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12773                                         types_ptr, objfile->obfd);
12774   gdb_assert (signature == type_sig->signature);
12775
12776   cu->die_hash
12777     = htab_create_alloc_ex (cu->header.length / 12,
12778                             die_hash,
12779                             die_eq,
12780                             NULL,
12781                             &cu->comp_unit_obstack,
12782                             hashtab_obstack_allocate,
12783                             dummy_obstack_deallocate);
12784
12785   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12786   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12787
12788   init_cu_die_reader (&reader_specs, cu);
12789
12790   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12791                                     NULL /*parent*/);
12792
12793   /* We try not to read any attributes in this function, because not
12794      all objfiles needed for references have been loaded yet, and symbol
12795      table processing isn't initialized.  But we have to set the CU language,
12796      or we won't be able to build types correctly.  */
12797   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12798   if (attr)
12799     set_cu_language (DW_UNSND (attr), cu);
12800   else
12801     set_cu_language (language_minimal, cu);
12802
12803   do_cleanups (back_to);
12804
12805   /* We've successfully allocated this compilation unit.  Let our caller
12806      clean it up when finished with it.  */
12807   discard_cleanups (free_cu_cleanup);
12808
12809   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12810   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12811 }
12812
12813 /* Decode simple location descriptions.
12814    Given a pointer to a dwarf block that defines a location, compute
12815    the location and return the value.
12816
12817    NOTE drow/2003-11-18: This function is called in two situations
12818    now: for the address of static or global variables (partial symbols
12819    only) and for offsets into structures which are expected to be
12820    (more or less) constant.  The partial symbol case should go away,
12821    and only the constant case should remain.  That will let this
12822    function complain more accurately.  A few special modes are allowed
12823    without complaint for global variables (for instance, global
12824    register values and thread-local values).
12825
12826    A location description containing no operations indicates that the
12827    object is optimized out.  The return value is 0 for that case.
12828    FIXME drow/2003-11-16: No callers check for this case any more; soon all
12829    callers will only want a very basic result and this can become a
12830    complaint.
12831
12832    Note that stack[0] is unused except as a default error return.
12833    Note that stack overflow is not yet handled.  */
12834
12835 static CORE_ADDR
12836 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12837 {
12838   struct objfile *objfile = cu->objfile;
12839   int i;
12840   int size = blk->size;
12841   gdb_byte *data = blk->data;
12842   CORE_ADDR stack[64];
12843   int stacki;
12844   unsigned int bytes_read, unsnd;
12845   gdb_byte op;
12846
12847   i = 0;
12848   stacki = 0;
12849   stack[stacki] = 0;
12850
12851   while (i < size)
12852     {
12853       op = data[i++];
12854       switch (op)
12855         {
12856         case DW_OP_lit0:
12857         case DW_OP_lit1:
12858         case DW_OP_lit2:
12859         case DW_OP_lit3:
12860         case DW_OP_lit4:
12861         case DW_OP_lit5:
12862         case DW_OP_lit6:
12863         case DW_OP_lit7:
12864         case DW_OP_lit8:
12865         case DW_OP_lit9:
12866         case DW_OP_lit10:
12867         case DW_OP_lit11:
12868         case DW_OP_lit12:
12869         case DW_OP_lit13:
12870         case DW_OP_lit14:
12871         case DW_OP_lit15:
12872         case DW_OP_lit16:
12873         case DW_OP_lit17:
12874         case DW_OP_lit18:
12875         case DW_OP_lit19:
12876         case DW_OP_lit20:
12877         case DW_OP_lit21:
12878         case DW_OP_lit22:
12879         case DW_OP_lit23:
12880         case DW_OP_lit24:
12881         case DW_OP_lit25:
12882         case DW_OP_lit26:
12883         case DW_OP_lit27:
12884         case DW_OP_lit28:
12885         case DW_OP_lit29:
12886         case DW_OP_lit30:
12887         case DW_OP_lit31:
12888           stack[++stacki] = op - DW_OP_lit0;
12889           break;
12890
12891         case DW_OP_reg0:
12892         case DW_OP_reg1:
12893         case DW_OP_reg2:
12894         case DW_OP_reg3:
12895         case DW_OP_reg4:
12896         case DW_OP_reg5:
12897         case DW_OP_reg6:
12898         case DW_OP_reg7:
12899         case DW_OP_reg8:
12900         case DW_OP_reg9:
12901         case DW_OP_reg10:
12902         case DW_OP_reg11:
12903         case DW_OP_reg12:
12904         case DW_OP_reg13:
12905         case DW_OP_reg14:
12906         case DW_OP_reg15:
12907         case DW_OP_reg16:
12908         case DW_OP_reg17:
12909         case DW_OP_reg18:
12910         case DW_OP_reg19:
12911         case DW_OP_reg20:
12912         case DW_OP_reg21:
12913         case DW_OP_reg22:
12914         case DW_OP_reg23:
12915         case DW_OP_reg24:
12916         case DW_OP_reg25:
12917         case DW_OP_reg26:
12918         case DW_OP_reg27:
12919         case DW_OP_reg28:
12920         case DW_OP_reg29:
12921         case DW_OP_reg30:
12922         case DW_OP_reg31:
12923           stack[++stacki] = op - DW_OP_reg0;
12924           if (i < size)
12925             dwarf2_complex_location_expr_complaint ();
12926           break;
12927
12928         case DW_OP_regx:
12929           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12930           i += bytes_read;
12931           stack[++stacki] = unsnd;
12932           if (i < size)
12933             dwarf2_complex_location_expr_complaint ();
12934           break;
12935
12936         case DW_OP_addr:
12937           stack[++stacki] = read_address (objfile->obfd, &data[i],
12938                                           cu, &bytes_read);
12939           i += bytes_read;
12940           break;
12941
12942         case DW_OP_const1u:
12943           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12944           i += 1;
12945           break;
12946
12947         case DW_OP_const1s:
12948           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12949           i += 1;
12950           break;
12951
12952         case DW_OP_const2u:
12953           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12954           i += 2;
12955           break;
12956
12957         case DW_OP_const2s:
12958           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12959           i += 2;
12960           break;
12961
12962         case DW_OP_const4u:
12963           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12964           i += 4;
12965           break;
12966
12967         case DW_OP_const4s:
12968           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12969           i += 4;
12970           break;
12971
12972         case DW_OP_constu:
12973           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
12974                                                   &bytes_read);
12975           i += bytes_read;
12976           break;
12977
12978         case DW_OP_consts:
12979           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12980           i += bytes_read;
12981           break;
12982
12983         case DW_OP_dup:
12984           stack[stacki + 1] = stack[stacki];
12985           stacki++;
12986           break;
12987
12988         case DW_OP_plus:
12989           stack[stacki - 1] += stack[stacki];
12990           stacki--;
12991           break;
12992
12993         case DW_OP_plus_uconst:
12994           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12995           i += bytes_read;
12996           break;
12997
12998         case DW_OP_minus:
12999           stack[stacki - 1] -= stack[stacki];
13000           stacki--;
13001           break;
13002
13003         case DW_OP_deref:
13004           /* If we're not the last op, then we definitely can't encode
13005              this using GDB's address_class enum.  This is valid for partial
13006              global symbols, although the variable's address will be bogus
13007              in the psymtab.  */
13008           if (i < size)
13009             dwarf2_complex_location_expr_complaint ();
13010           break;
13011
13012         case DW_OP_GNU_push_tls_address:
13013           /* The top of the stack has the offset from the beginning
13014              of the thread control block at which the variable is located.  */
13015           /* Nothing should follow this operator, so the top of stack would
13016              be returned.  */
13017           /* This is valid for partial global symbols, but the variable's
13018              address will be bogus in the psymtab.  */
13019           if (i < size)
13020             dwarf2_complex_location_expr_complaint ();
13021           break;
13022
13023         case DW_OP_GNU_uninit:
13024           break;
13025
13026         default:
13027           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13028                      dwarf_stack_op_name (op, 1));
13029           return (stack[stacki]);
13030         }
13031     }
13032   return (stack[stacki]);
13033 }
13034
13035 /* memory allocation interface */
13036
13037 static struct dwarf_block *
13038 dwarf_alloc_block (struct dwarf2_cu *cu)
13039 {
13040   struct dwarf_block *blk;
13041
13042   blk = (struct dwarf_block *)
13043     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13044   return (blk);
13045 }
13046
13047 static struct abbrev_info *
13048 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13049 {
13050   struct abbrev_info *abbrev;
13051
13052   abbrev = (struct abbrev_info *)
13053     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13054   memset (abbrev, 0, sizeof (struct abbrev_info));
13055   return (abbrev);
13056 }
13057
13058 static struct die_info *
13059 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13060 {
13061   struct die_info *die;
13062   size_t size = sizeof (struct die_info);
13063
13064   if (num_attrs > 1)
13065     size += (num_attrs - 1) * sizeof (struct attribute);
13066
13067   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13068   memset (die, 0, sizeof (struct die_info));
13069   return (die);
13070 }
13071
13072 \f
13073 /* Macro support.  */
13074
13075
13076 /* Return the full name of file number I in *LH's file name table.
13077    Use COMP_DIR as the name of the current directory of the
13078    compilation.  The result is allocated using xmalloc; the caller is
13079    responsible for freeing it.  */
13080 static char *
13081 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13082 {
13083   /* Is the file number a valid index into the line header's file name
13084      table?  Remember that file numbers start with one, not zero.  */
13085   if (1 <= file && file <= lh->num_file_names)
13086     {
13087       struct file_entry *fe = &lh->file_names[file - 1];
13088
13089       if (IS_ABSOLUTE_PATH (fe->name))
13090         return xstrdup (fe->name);
13091       else
13092         {
13093           const char *dir;
13094           int dir_len;
13095           char *full_name;
13096
13097           if (fe->dir_index)
13098             dir = lh->include_dirs[fe->dir_index - 1];
13099           else
13100             dir = comp_dir;
13101
13102           if (dir)
13103             {
13104               dir_len = strlen (dir);
13105               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13106               strcpy (full_name, dir);
13107               full_name[dir_len] = '/';
13108               strcpy (full_name + dir_len + 1, fe->name);
13109               return full_name;
13110             }
13111           else
13112             return xstrdup (fe->name);
13113         }
13114     }
13115   else
13116     {
13117       /* The compiler produced a bogus file number.  We can at least
13118          record the macro definitions made in the file, even if we
13119          won't be able to find the file by name.  */
13120       char fake_name[80];
13121
13122       sprintf (fake_name, "<bad macro file number %d>", file);
13123
13124       complaint (&symfile_complaints,
13125                  _("bad file number in macro information (%d)"),
13126                  file);
13127
13128       return xstrdup (fake_name);
13129     }
13130 }
13131
13132
13133 static struct macro_source_file *
13134 macro_start_file (int file, int line,
13135                   struct macro_source_file *current_file,
13136                   const char *comp_dir,
13137                   struct line_header *lh, struct objfile *objfile)
13138 {
13139   /* The full name of this source file.  */
13140   char *full_name = file_full_name (file, lh, comp_dir);
13141
13142   /* We don't create a macro table for this compilation unit
13143      at all until we actually get a filename.  */
13144   if (! pending_macros)
13145     pending_macros = new_macro_table (&objfile->objfile_obstack,
13146                                       objfile->macro_cache);
13147
13148   if (! current_file)
13149     /* If we have no current file, then this must be the start_file
13150        directive for the compilation unit's main source file.  */
13151     current_file = macro_set_main (pending_macros, full_name);
13152   else
13153     current_file = macro_include (current_file, line, full_name);
13154
13155   xfree (full_name);
13156
13157   return current_file;
13158 }
13159
13160
13161 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13162    followed by a null byte.  */
13163 static char *
13164 copy_string (const char *buf, int len)
13165 {
13166   char *s = xmalloc (len + 1);
13167
13168   memcpy (s, buf, len);
13169   s[len] = '\0';
13170   return s;
13171 }
13172
13173
13174 static const char *
13175 consume_improper_spaces (const char *p, const char *body)
13176 {
13177   if (*p == ' ')
13178     {
13179       complaint (&symfile_complaints,
13180                  _("macro definition contains spaces in formal argument list:\n`%s'"),
13181                  body);
13182
13183       while (*p == ' ')
13184         p++;
13185     }
13186
13187   return p;
13188 }
13189
13190
13191 static void
13192 parse_macro_definition (struct macro_source_file *file, int line,
13193                         const char *body)
13194 {
13195   const char *p;
13196
13197   /* The body string takes one of two forms.  For object-like macro
13198      definitions, it should be:
13199
13200         <macro name> " " <definition>
13201
13202      For function-like macro definitions, it should be:
13203
13204         <macro name> "() " <definition>
13205      or
13206         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13207
13208      Spaces may appear only where explicitly indicated, and in the
13209      <definition>.
13210
13211      The Dwarf 2 spec says that an object-like macro's name is always
13212      followed by a space, but versions of GCC around March 2002 omit
13213      the space when the macro's definition is the empty string.
13214
13215      The Dwarf 2 spec says that there should be no spaces between the
13216      formal arguments in a function-like macro's formal argument list,
13217      but versions of GCC around March 2002 include spaces after the
13218      commas.  */
13219
13220
13221   /* Find the extent of the macro name.  The macro name is terminated
13222      by either a space or null character (for an object-like macro) or
13223      an opening paren (for a function-like macro).  */
13224   for (p = body; *p; p++)
13225     if (*p == ' ' || *p == '(')
13226       break;
13227
13228   if (*p == ' ' || *p == '\0')
13229     {
13230       /* It's an object-like macro.  */
13231       int name_len = p - body;
13232       char *name = copy_string (body, name_len);
13233       const char *replacement;
13234
13235       if (*p == ' ')
13236         replacement = body + name_len + 1;
13237       else
13238         {
13239           dwarf2_macro_malformed_definition_complaint (body);
13240           replacement = body + name_len;
13241         }
13242
13243       macro_define_object (file, line, name, replacement);
13244
13245       xfree (name);
13246     }
13247   else if (*p == '(')
13248     {
13249       /* It's a function-like macro.  */
13250       char *name = copy_string (body, p - body);
13251       int argc = 0;
13252       int argv_size = 1;
13253       char **argv = xmalloc (argv_size * sizeof (*argv));
13254
13255       p++;
13256
13257       p = consume_improper_spaces (p, body);
13258
13259       /* Parse the formal argument list.  */
13260       while (*p && *p != ')')
13261         {
13262           /* Find the extent of the current argument name.  */
13263           const char *arg_start = p;
13264
13265           while (*p && *p != ',' && *p != ')' && *p != ' ')
13266             p++;
13267
13268           if (! *p || p == arg_start)
13269             dwarf2_macro_malformed_definition_complaint (body);
13270           else
13271             {
13272               /* Make sure argv has room for the new argument.  */
13273               if (argc >= argv_size)
13274                 {
13275                   argv_size *= 2;
13276                   argv = xrealloc (argv, argv_size * sizeof (*argv));
13277                 }
13278
13279               argv[argc++] = copy_string (arg_start, p - arg_start);
13280             }
13281
13282           p = consume_improper_spaces (p, body);
13283
13284           /* Consume the comma, if present.  */
13285           if (*p == ',')
13286             {
13287               p++;
13288
13289               p = consume_improper_spaces (p, body);
13290             }
13291         }
13292
13293       if (*p == ')')
13294         {
13295           p++;
13296
13297           if (*p == ' ')
13298             /* Perfectly formed definition, no complaints.  */
13299             macro_define_function (file, line, name,
13300                                    argc, (const char **) argv,
13301                                    p + 1);
13302           else if (*p == '\0')
13303             {
13304               /* Complain, but do define it.  */
13305               dwarf2_macro_malformed_definition_complaint (body);
13306               macro_define_function (file, line, name,
13307                                      argc, (const char **) argv,
13308                                      p);
13309             }
13310           else
13311             /* Just complain.  */
13312             dwarf2_macro_malformed_definition_complaint (body);
13313         }
13314       else
13315         /* Just complain.  */
13316         dwarf2_macro_malformed_definition_complaint (body);
13317
13318       xfree (name);
13319       {
13320         int i;
13321
13322         for (i = 0; i < argc; i++)
13323           xfree (argv[i]);
13324       }
13325       xfree (argv);
13326     }
13327   else
13328     dwarf2_macro_malformed_definition_complaint (body);
13329 }
13330
13331
13332 static void
13333 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13334                      char *comp_dir, bfd *abfd,
13335                      struct dwarf2_cu *cu)
13336 {
13337   gdb_byte *mac_ptr, *mac_end;
13338   struct macro_source_file *current_file = 0;
13339   enum dwarf_macinfo_record_type macinfo_type;
13340   int at_commandline;
13341
13342   dwarf2_read_section (dwarf2_per_objfile->objfile,
13343                        &dwarf2_per_objfile->macinfo);
13344   if (dwarf2_per_objfile->macinfo.buffer == NULL)
13345     {
13346       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13347       return;
13348     }
13349
13350   /* First pass: Find the name of the base filename.
13351      This filename is needed in order to process all macros whose definition
13352      (or undefinition) comes from the command line.  These macros are defined
13353      before the first DW_MACINFO_start_file entry, and yet still need to be
13354      associated to the base file.
13355
13356      To determine the base file name, we scan the macro definitions until we
13357      reach the first DW_MACINFO_start_file entry.  We then initialize
13358      CURRENT_FILE accordingly so that any macro definition found before the
13359      first DW_MACINFO_start_file can still be associated to the base file.  */
13360
13361   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13362   mac_end = dwarf2_per_objfile->macinfo.buffer
13363     + dwarf2_per_objfile->macinfo.size;
13364
13365   do
13366     {
13367       /* Do we at least have room for a macinfo type byte?  */
13368       if (mac_ptr >= mac_end)
13369         {
13370           /* Complaint is printed during the second pass as GDB will probably
13371              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
13372           break;
13373         }
13374
13375       macinfo_type = read_1_byte (abfd, mac_ptr);
13376       mac_ptr++;
13377
13378       switch (macinfo_type)
13379         {
13380           /* A zero macinfo type indicates the end of the macro
13381              information.  */
13382         case 0:
13383           break;
13384
13385         case DW_MACINFO_define:
13386         case DW_MACINFO_undef:
13387           /* Only skip the data by MAC_PTR.  */
13388           {
13389             unsigned int bytes_read;
13390
13391             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13392             mac_ptr += bytes_read;
13393             read_direct_string (abfd, mac_ptr, &bytes_read);
13394             mac_ptr += bytes_read;
13395           }
13396           break;
13397
13398         case DW_MACINFO_start_file:
13399           {
13400             unsigned int bytes_read;
13401             int line, file;
13402
13403             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13404             mac_ptr += bytes_read;
13405             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13406             mac_ptr += bytes_read;
13407
13408             current_file = macro_start_file (file, line, current_file, comp_dir,
13409                                              lh, cu->objfile);
13410           }
13411           break;
13412
13413         case DW_MACINFO_end_file:
13414           /* No data to skip by MAC_PTR.  */
13415           break;
13416
13417         case DW_MACINFO_vendor_ext:
13418           /* Only skip the data by MAC_PTR.  */
13419           {
13420             unsigned int bytes_read;
13421
13422             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13423             mac_ptr += bytes_read;
13424             read_direct_string (abfd, mac_ptr, &bytes_read);
13425             mac_ptr += bytes_read;
13426           }
13427           break;
13428
13429         default:
13430           break;
13431         }
13432     } while (macinfo_type != 0 && current_file == NULL);
13433
13434   /* Second pass: Process all entries.
13435
13436      Use the AT_COMMAND_LINE flag to determine whether we are still processing
13437      command-line macro definitions/undefinitions.  This flag is unset when we
13438      reach the first DW_MACINFO_start_file entry.  */
13439
13440   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13441
13442   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
13443      GDB is still reading the definitions from command line.  First
13444      DW_MACINFO_start_file will need to be ignored as it was already executed
13445      to create CURRENT_FILE for the main source holding also the command line
13446      definitions.  On first met DW_MACINFO_start_file this flag is reset to
13447      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
13448
13449   at_commandline = 1;
13450
13451   do
13452     {
13453       /* Do we at least have room for a macinfo type byte?  */
13454       if (mac_ptr >= mac_end)
13455         {
13456           dwarf2_macros_too_long_complaint ();
13457           break;
13458         }
13459
13460       macinfo_type = read_1_byte (abfd, mac_ptr);
13461       mac_ptr++;
13462
13463       switch (macinfo_type)
13464         {
13465           /* A zero macinfo type indicates the end of the macro
13466              information.  */
13467         case 0:
13468           break;
13469
13470         case DW_MACINFO_define:
13471         case DW_MACINFO_undef:
13472           {
13473             unsigned int bytes_read;
13474             int line;
13475             char *body;
13476
13477             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13478             mac_ptr += bytes_read;
13479             body = read_direct_string (abfd, mac_ptr, &bytes_read);
13480             mac_ptr += bytes_read;
13481
13482             if (! current_file)
13483               {
13484                 /* DWARF violation as no main source is present.  */
13485                 complaint (&symfile_complaints,
13486                            _("debug info with no main source gives macro %s "
13487                              "on line %d: %s"),
13488                            macinfo_type == DW_MACINFO_define ?
13489                              _("definition") :
13490                                macinfo_type == DW_MACINFO_undef ?
13491                                  _("undefinition") :
13492                                  _("something-or-other"), line, body);
13493                 break;
13494               }
13495             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13496               complaint (&symfile_complaints,
13497                          _("debug info gives %s macro %s with %s line %d: %s"),
13498                          at_commandline ? _("command-line") : _("in-file"),
13499                          macinfo_type == DW_MACINFO_define ?
13500                            _("definition") :
13501                              macinfo_type == DW_MACINFO_undef ?
13502                                _("undefinition") :
13503                                _("something-or-other"),
13504                          line == 0 ? _("zero") : _("non-zero"), line, body);
13505
13506             if (macinfo_type == DW_MACINFO_define)
13507               parse_macro_definition (current_file, line, body);
13508             else if (macinfo_type == DW_MACINFO_undef)
13509               macro_undef (current_file, line, body);
13510           }
13511           break;
13512
13513         case DW_MACINFO_start_file:
13514           {
13515             unsigned int bytes_read;
13516             int line, file;
13517
13518             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13519             mac_ptr += bytes_read;
13520             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13521             mac_ptr += bytes_read;
13522
13523             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13524               complaint (&symfile_complaints,
13525                          _("debug info gives source %d included "
13526                            "from %s at %s line %d"),
13527                          file, at_commandline ? _("command-line") : _("file"),
13528                          line == 0 ? _("zero") : _("non-zero"), line);
13529
13530             if (at_commandline)
13531               {
13532                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13533                 at_commandline = 0;
13534               }
13535             else
13536               current_file = macro_start_file (file, line,
13537                                                current_file, comp_dir,
13538                                                lh, cu->objfile);
13539           }
13540           break;
13541
13542         case DW_MACINFO_end_file:
13543           if (! current_file)
13544             complaint (&symfile_complaints,
13545                        _("macro debug info has an unmatched `close_file' directive"));
13546           else
13547             {
13548               current_file = current_file->included_by;
13549               if (! current_file)
13550                 {
13551                   enum dwarf_macinfo_record_type next_type;
13552
13553                   /* GCC circa March 2002 doesn't produce the zero
13554                      type byte marking the end of the compilation
13555                      unit.  Complain if it's not there, but exit no
13556                      matter what.  */
13557
13558                   /* Do we at least have room for a macinfo type byte?  */
13559                   if (mac_ptr >= mac_end)
13560                     {
13561                       dwarf2_macros_too_long_complaint ();
13562                       return;
13563                     }
13564
13565                   /* We don't increment mac_ptr here, so this is just
13566                      a look-ahead.  */
13567                   next_type = read_1_byte (abfd, mac_ptr);
13568                   if (next_type != 0)
13569                     complaint (&symfile_complaints,
13570                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13571
13572                   return;
13573                 }
13574             }
13575           break;
13576
13577         case DW_MACINFO_vendor_ext:
13578           {
13579             unsigned int bytes_read;
13580             int constant;
13581             char *string;
13582
13583             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13584             mac_ptr += bytes_read;
13585             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13586             mac_ptr += bytes_read;
13587
13588             /* We don't recognize any vendor extensions.  */
13589           }
13590           break;
13591         }
13592     } while (macinfo_type != 0);
13593 }
13594
13595 /* Check if the attribute's form is a DW_FORM_block*
13596    if so return true else false. */
13597 static int
13598 attr_form_is_block (struct attribute *attr)
13599 {
13600   return (attr == NULL ? 0 :
13601       attr->form == DW_FORM_block1
13602       || attr->form == DW_FORM_block2
13603       || attr->form == DW_FORM_block4
13604       || attr->form == DW_FORM_block
13605       || attr->form == DW_FORM_exprloc);
13606 }
13607
13608 /* Return non-zero if ATTR's value is a section offset --- classes
13609    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13610    You may use DW_UNSND (attr) to retrieve such offsets.
13611
13612    Section 7.5.4, "Attribute Encodings", explains that no attribute
13613    may have a value that belongs to more than one of these classes; it
13614    would be ambiguous if we did, because we use the same forms for all
13615    of them.  */
13616 static int
13617 attr_form_is_section_offset (struct attribute *attr)
13618 {
13619   return (attr->form == DW_FORM_data4
13620           || attr->form == DW_FORM_data8
13621           || attr->form == DW_FORM_sec_offset);
13622 }
13623
13624
13625 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13626    zero otherwise.  When this function returns true, you can apply
13627    dwarf2_get_attr_constant_value to it.
13628
13629    However, note that for some attributes you must check
13630    attr_form_is_section_offset before using this test.  DW_FORM_data4
13631    and DW_FORM_data8 are members of both the constant class, and of
13632    the classes that contain offsets into other debug sections
13633    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
13634    that, if an attribute's can be either a constant or one of the
13635    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13636    taken as section offsets, not constants.  */
13637 static int
13638 attr_form_is_constant (struct attribute *attr)
13639 {
13640   switch (attr->form)
13641     {
13642     case DW_FORM_sdata:
13643     case DW_FORM_udata:
13644     case DW_FORM_data1:
13645     case DW_FORM_data2:
13646     case DW_FORM_data4:
13647     case DW_FORM_data8:
13648       return 1;
13649     default:
13650       return 0;
13651     }
13652 }
13653
13654 static void
13655 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13656                              struct dwarf2_cu *cu)
13657 {
13658   if (attr_form_is_section_offset (attr)
13659       /* ".debug_loc" may not exist at all, or the offset may be outside
13660          the section.  If so, fall through to the complaint in the
13661          other branch.  */
13662       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13663     {
13664       struct dwarf2_loclist_baton *baton;
13665
13666       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13667                              sizeof (struct dwarf2_loclist_baton));
13668       baton->per_cu = cu->per_cu;
13669       gdb_assert (baton->per_cu);
13670
13671       dwarf2_read_section (dwarf2_per_objfile->objfile,
13672                            &dwarf2_per_objfile->loc);
13673
13674       /* We don't know how long the location list is, but make sure we
13675          don't run off the edge of the section.  */
13676       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13677       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13678       baton->base_address = cu->base_address;
13679       if (cu->base_known == 0)
13680         complaint (&symfile_complaints,
13681                    _("Location list used without specifying the CU base address."));
13682
13683       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13684       SYMBOL_LOCATION_BATON (sym) = baton;
13685     }
13686   else
13687     {
13688       struct dwarf2_locexpr_baton *baton;
13689
13690       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13691                              sizeof (struct dwarf2_locexpr_baton));
13692       baton->per_cu = cu->per_cu;
13693       gdb_assert (baton->per_cu);
13694
13695       if (attr_form_is_block (attr))
13696         {
13697           /* Note that we're just copying the block's data pointer
13698              here, not the actual data.  We're still pointing into the
13699              info_buffer for SYM's objfile; right now we never release
13700              that buffer, but when we do clean up properly this may
13701              need to change.  */
13702           baton->size = DW_BLOCK (attr)->size;
13703           baton->data = DW_BLOCK (attr)->data;
13704         }
13705       else
13706         {
13707           dwarf2_invalid_attrib_class_complaint ("location description",
13708                                                  SYMBOL_NATURAL_NAME (sym));
13709           baton->size = 0;
13710           baton->data = NULL;
13711         }
13712
13713       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13714       SYMBOL_LOCATION_BATON (sym) = baton;
13715     }
13716 }
13717
13718 /* Return the OBJFILE associated with the compilation unit CU.  If CU
13719    came from a separate debuginfo file, then the master objfile is
13720    returned.  */
13721
13722 struct objfile *
13723 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13724 {
13725   struct objfile *objfile = per_cu->objfile;
13726
13727   /* Return the master objfile, so that we can report and look up the
13728      correct file containing this variable.  */
13729   if (objfile->separate_debug_objfile_backlink)
13730     objfile = objfile->separate_debug_objfile_backlink;
13731
13732   return objfile;
13733 }
13734
13735 /* Return the address size given in the compilation unit header for CU.  */
13736
13737 CORE_ADDR
13738 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13739 {
13740   if (per_cu->cu)
13741     return per_cu->cu->header.addr_size;
13742   else
13743     {
13744       /* If the CU is not currently read in, we re-read its header.  */
13745       struct objfile *objfile = per_cu->objfile;
13746       struct dwarf2_per_objfile *per_objfile
13747         = objfile_data (objfile, dwarf2_objfile_data_key);
13748       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13749       struct comp_unit_head cu_header;
13750
13751       memset (&cu_header, 0, sizeof cu_header);
13752       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13753       return cu_header.addr_size;
13754     }
13755 }
13756
13757 /* Return the offset size given in the compilation unit header for CU.  */
13758
13759 int
13760 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13761 {
13762   if (per_cu->cu)
13763     return per_cu->cu->header.offset_size;
13764   else
13765     {
13766       /* If the CU is not currently read in, we re-read its header.  */
13767       struct objfile *objfile = per_cu->objfile;
13768       struct dwarf2_per_objfile *per_objfile
13769         = objfile_data (objfile, dwarf2_objfile_data_key);
13770       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13771       struct comp_unit_head cu_header;
13772
13773       memset (&cu_header, 0, sizeof cu_header);
13774       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13775       return cu_header.offset_size;
13776     }
13777 }
13778
13779 /* Return the text offset of the CU.  The returned offset comes from
13780    this CU's objfile.  If this objfile came from a separate debuginfo
13781    file, then the offset may be different from the corresponding
13782    offset in the parent objfile.  */
13783
13784 CORE_ADDR
13785 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13786 {
13787   struct objfile *objfile = per_cu->objfile;
13788
13789   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13790 }
13791
13792 /* Locate the .debug_info compilation unit from CU's objfile which contains
13793    the DIE at OFFSET.  Raises an error on failure.  */
13794
13795 static struct dwarf2_per_cu_data *
13796 dwarf2_find_containing_comp_unit (unsigned int offset,
13797                                   struct objfile *objfile)
13798 {
13799   struct dwarf2_per_cu_data *this_cu;
13800   int low, high;
13801
13802   low = 0;
13803   high = dwarf2_per_objfile->n_comp_units - 1;
13804   while (high > low)
13805     {
13806       int mid = low + (high - low) / 2;
13807
13808       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13809         high = mid;
13810       else
13811         low = mid + 1;
13812     }
13813   gdb_assert (low == high);
13814   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13815     {
13816       if (low == 0)
13817         error (_("Dwarf Error: could not find partial DIE containing "
13818                "offset 0x%lx [in module %s]"),
13819                (long) offset, bfd_get_filename (objfile->obfd));
13820
13821       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13822       return dwarf2_per_objfile->all_comp_units[low-1];
13823     }
13824   else
13825     {
13826       this_cu = dwarf2_per_objfile->all_comp_units[low];
13827       if (low == dwarf2_per_objfile->n_comp_units - 1
13828           && offset >= this_cu->offset + this_cu->length)
13829         error (_("invalid dwarf2 offset %u"), offset);
13830       gdb_assert (offset < this_cu->offset + this_cu->length);
13831       return this_cu;
13832     }
13833 }
13834
13835 /* Locate the compilation unit from OBJFILE which is located at exactly
13836    OFFSET.  Raises an error on failure.  */
13837
13838 static struct dwarf2_per_cu_data *
13839 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13840 {
13841   struct dwarf2_per_cu_data *this_cu;
13842
13843   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13844   if (this_cu->offset != offset)
13845     error (_("no compilation unit with offset %u."), offset);
13846   return this_cu;
13847 }
13848
13849 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
13850
13851 static struct dwarf2_cu *
13852 alloc_one_comp_unit (struct objfile *objfile)
13853 {
13854   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13855   cu->objfile = objfile;
13856   obstack_init (&cu->comp_unit_obstack);
13857   return cu;
13858 }
13859
13860 /* Release one cached compilation unit, CU.  We unlink it from the tree
13861    of compilation units, but we don't remove it from the read_in_chain;
13862    the caller is responsible for that.
13863    NOTE: DATA is a void * because this function is also used as a
13864    cleanup routine.  */
13865
13866 static void
13867 free_one_comp_unit (void *data)
13868 {
13869   struct dwarf2_cu *cu = data;
13870
13871   if (cu->per_cu != NULL)
13872     cu->per_cu->cu = NULL;
13873   cu->per_cu = NULL;
13874
13875   obstack_free (&cu->comp_unit_obstack, NULL);
13876
13877   xfree (cu);
13878 }
13879
13880 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13881    when we're finished with it.  We can't free the pointer itself, but be
13882    sure to unlink it from the cache.  Also release any associated storage
13883    and perform cache maintenance.
13884
13885    Only used during partial symbol parsing.  */
13886
13887 static void
13888 free_stack_comp_unit (void *data)
13889 {
13890   struct dwarf2_cu *cu = data;
13891
13892   obstack_free (&cu->comp_unit_obstack, NULL);
13893   cu->partial_dies = NULL;
13894
13895   if (cu->per_cu != NULL)
13896     {
13897       /* This compilation unit is on the stack in our caller, so we
13898          should not xfree it.  Just unlink it.  */
13899       cu->per_cu->cu = NULL;
13900       cu->per_cu = NULL;
13901
13902       /* If we had a per-cu pointer, then we may have other compilation
13903          units loaded, so age them now.  */
13904       age_cached_comp_units ();
13905     }
13906 }
13907
13908 /* Free all cached compilation units.  */
13909
13910 static void
13911 free_cached_comp_units (void *data)
13912 {
13913   struct dwarf2_per_cu_data *per_cu, **last_chain;
13914
13915   per_cu = dwarf2_per_objfile->read_in_chain;
13916   last_chain = &dwarf2_per_objfile->read_in_chain;
13917   while (per_cu != NULL)
13918     {
13919       struct dwarf2_per_cu_data *next_cu;
13920
13921       next_cu = per_cu->cu->read_in_chain;
13922
13923       free_one_comp_unit (per_cu->cu);
13924       *last_chain = next_cu;
13925
13926       per_cu = next_cu;
13927     }
13928 }
13929
13930 /* Increase the age counter on each cached compilation unit, and free
13931    any that are too old.  */
13932
13933 static void
13934 age_cached_comp_units (void)
13935 {
13936   struct dwarf2_per_cu_data *per_cu, **last_chain;
13937
13938   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13939   per_cu = dwarf2_per_objfile->read_in_chain;
13940   while (per_cu != NULL)
13941     {
13942       per_cu->cu->last_used ++;
13943       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13944         dwarf2_mark (per_cu->cu);
13945       per_cu = per_cu->cu->read_in_chain;
13946     }
13947
13948   per_cu = dwarf2_per_objfile->read_in_chain;
13949   last_chain = &dwarf2_per_objfile->read_in_chain;
13950   while (per_cu != NULL)
13951     {
13952       struct dwarf2_per_cu_data *next_cu;
13953
13954       next_cu = per_cu->cu->read_in_chain;
13955
13956       if (!per_cu->cu->mark)
13957         {
13958           free_one_comp_unit (per_cu->cu);
13959           *last_chain = next_cu;
13960         }
13961       else
13962         last_chain = &per_cu->cu->read_in_chain;
13963
13964       per_cu = next_cu;
13965     }
13966 }
13967
13968 /* Remove a single compilation unit from the cache.  */
13969
13970 static void
13971 free_one_cached_comp_unit (void *target_cu)
13972 {
13973   struct dwarf2_per_cu_data *per_cu, **last_chain;
13974
13975   per_cu = dwarf2_per_objfile->read_in_chain;
13976   last_chain = &dwarf2_per_objfile->read_in_chain;
13977   while (per_cu != NULL)
13978     {
13979       struct dwarf2_per_cu_data *next_cu;
13980
13981       next_cu = per_cu->cu->read_in_chain;
13982
13983       if (per_cu->cu == target_cu)
13984         {
13985           free_one_comp_unit (per_cu->cu);
13986           *last_chain = next_cu;
13987           break;
13988         }
13989       else
13990         last_chain = &per_cu->cu->read_in_chain;
13991
13992       per_cu = next_cu;
13993     }
13994 }
13995
13996 /* Release all extra memory associated with OBJFILE.  */
13997
13998 void
13999 dwarf2_free_objfile (struct objfile *objfile)
14000 {
14001   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14002
14003   if (dwarf2_per_objfile == NULL)
14004     return;
14005
14006   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14007   free_cached_comp_units (NULL);
14008
14009   if (dwarf2_per_objfile->using_index)
14010     {
14011       int i;
14012
14013       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14014         {
14015           int j;
14016           struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14017
14018           if (!cu->v.quick->lines)
14019             continue;
14020
14021           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14022             {
14023               if (cu->v.quick->file_names)
14024                 xfree ((void *) cu->v.quick->file_names[j]);
14025               if (cu->v.quick->full_names)
14026                 xfree ((void *) cu->v.quick->full_names[j]);
14027             }
14028
14029           free_line_header (cu->v.quick->lines);
14030         }
14031     }
14032
14033   /* Everything else should be on the objfile obstack.  */
14034 }
14035
14036 /* A pair of DIE offset and GDB type pointer.  We store these
14037    in a hash table separate from the DIEs, and preserve them
14038    when the DIEs are flushed out of cache.  */
14039
14040 struct dwarf2_offset_and_type
14041 {
14042   unsigned int offset;
14043   struct type *type;
14044 };
14045
14046 /* Hash function for a dwarf2_offset_and_type.  */
14047
14048 static hashval_t
14049 offset_and_type_hash (const void *item)
14050 {
14051   const struct dwarf2_offset_and_type *ofs = item;
14052
14053   return ofs->offset;
14054 }
14055
14056 /* Equality function for a dwarf2_offset_and_type.  */
14057
14058 static int
14059 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14060 {
14061   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14062   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14063
14064   return ofs_lhs->offset == ofs_rhs->offset;
14065 }
14066
14067 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14068    table if necessary.  For convenience, return TYPE.
14069
14070    The DIEs reading must have careful ordering to:
14071     * Not cause infite loops trying to read in DIEs as a prerequisite for
14072       reading current DIE.
14073     * Not trying to dereference contents of still incompletely read in types
14074       while reading in other DIEs.
14075     * Enable referencing still incompletely read in types just by a pointer to
14076       the type without accessing its fields.
14077
14078    Therefore caller should follow these rules:
14079      * Try to fetch any prerequisite types we may need to build this DIE type
14080        before building the type and calling set_die_type.
14081      * After building typer call set_die_type for current DIE as soon as
14082        possible before fetching more types to complete the current type.
14083      * Make the type as complete as possible before fetching more types.  */
14084
14085 static struct type *
14086 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14087 {
14088   struct dwarf2_offset_and_type **slot, ofs;
14089
14090   /* For Ada types, make sure that the gnat-specific data is always
14091      initialized (if not already set).  There are a few types where
14092      we should not be doing so, because the type-specific area is
14093      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14094      where the type-specific area is used to store the floatformat).
14095      But this is not a problem, because the gnat-specific information
14096      is actually not needed for these types.  */
14097   if (need_gnat_info (cu)
14098       && TYPE_CODE (type) != TYPE_CODE_FUNC
14099       && TYPE_CODE (type) != TYPE_CODE_FLT
14100       && !HAVE_GNAT_AUX_INFO (type))
14101     INIT_GNAT_SPECIFIC (type);
14102
14103   if (cu->type_hash == NULL)
14104     {
14105       gdb_assert (cu->per_cu != NULL);
14106       cu->per_cu->type_hash
14107         = htab_create_alloc_ex (cu->header.length / 24,
14108                                 offset_and_type_hash,
14109                                 offset_and_type_eq,
14110                                 NULL,
14111                                 &cu->objfile->objfile_obstack,
14112                                 hashtab_obstack_allocate,
14113                                 dummy_obstack_deallocate);
14114       cu->type_hash = cu->per_cu->type_hash;
14115     }
14116
14117   ofs.offset = die->offset;
14118   ofs.type = type;
14119   slot = (struct dwarf2_offset_and_type **)
14120     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
14121   if (*slot)
14122     complaint (&symfile_complaints,
14123                _("A problem internal to GDB: DIE 0x%x has type already set"),
14124                die->offset);
14125   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
14126   **slot = ofs;
14127   return type;
14128 }
14129
14130 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
14131    not have a saved type.  */
14132
14133 static struct type *
14134 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14135 {
14136   struct dwarf2_offset_and_type *slot, ofs;
14137   htab_t type_hash = cu->type_hash;
14138
14139   if (type_hash == NULL)
14140     return NULL;
14141
14142   ofs.offset = die->offset;
14143   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14144   if (slot)
14145     return slot->type;
14146   else
14147     return NULL;
14148 }
14149
14150 /* Add a dependence relationship from CU to REF_PER_CU.  */
14151
14152 static void
14153 dwarf2_add_dependence (struct dwarf2_cu *cu,
14154                        struct dwarf2_per_cu_data *ref_per_cu)
14155 {
14156   void **slot;
14157
14158   if (cu->dependencies == NULL)
14159     cu->dependencies
14160       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14161                               NULL, &cu->comp_unit_obstack,
14162                               hashtab_obstack_allocate,
14163                               dummy_obstack_deallocate);
14164
14165   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14166   if (*slot == NULL)
14167     *slot = ref_per_cu;
14168 }
14169
14170 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14171    Set the mark field in every compilation unit in the
14172    cache that we must keep because we are keeping CU.  */
14173
14174 static int
14175 dwarf2_mark_helper (void **slot, void *data)
14176 {
14177   struct dwarf2_per_cu_data *per_cu;
14178
14179   per_cu = (struct dwarf2_per_cu_data *) *slot;
14180   if (per_cu->cu->mark)
14181     return 1;
14182   per_cu->cu->mark = 1;
14183
14184   if (per_cu->cu->dependencies != NULL)
14185     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14186
14187   return 1;
14188 }
14189
14190 /* Set the mark field in CU and in every other compilation unit in the
14191    cache that we must keep because we are keeping CU.  */
14192
14193 static void
14194 dwarf2_mark (struct dwarf2_cu *cu)
14195 {
14196   if (cu->mark)
14197     return;
14198   cu->mark = 1;
14199   if (cu->dependencies != NULL)
14200     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14201 }
14202
14203 static void
14204 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14205 {
14206   while (per_cu)
14207     {
14208       per_cu->cu->mark = 0;
14209       per_cu = per_cu->cu->read_in_chain;
14210     }
14211 }
14212
14213 /* Trivial hash function for partial_die_info: the hash value of a DIE
14214    is its offset in .debug_info for this objfile.  */
14215
14216 static hashval_t
14217 partial_die_hash (const void *item)
14218 {
14219   const struct partial_die_info *part_die = item;
14220
14221   return part_die->offset;
14222 }
14223
14224 /* Trivial comparison function for partial_die_info structures: two DIEs
14225    are equal if they have the same offset.  */
14226
14227 static int
14228 partial_die_eq (const void *item_lhs, const void *item_rhs)
14229 {
14230   const struct partial_die_info *part_die_lhs = item_lhs;
14231   const struct partial_die_info *part_die_rhs = item_rhs;
14232
14233   return part_die_lhs->offset == part_die_rhs->offset;
14234 }
14235
14236 static struct cmd_list_element *set_dwarf2_cmdlist;
14237 static struct cmd_list_element *show_dwarf2_cmdlist;
14238
14239 static void
14240 set_dwarf2_cmd (char *args, int from_tty)
14241 {
14242   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14243 }
14244
14245 static void
14246 show_dwarf2_cmd (char *args, int from_tty)
14247 {
14248   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14249 }
14250
14251 /* If section described by INFO was mmapped, munmap it now.  */
14252
14253 static void
14254 munmap_section_buffer (struct dwarf2_section_info *info)
14255 {
14256   if (info->was_mmapped)
14257     {
14258 #ifdef HAVE_MMAP
14259       intptr_t begin = (intptr_t) info->buffer;
14260       intptr_t map_begin = begin & ~(pagesize - 1);
14261       size_t map_length = info->size + begin - map_begin;
14262
14263       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14264 #else
14265       /* Without HAVE_MMAP, we should never be here to begin with.  */
14266       gdb_assert_not_reached ("no mmap support");
14267 #endif
14268     }
14269 }
14270
14271 /* munmap debug sections for OBJFILE, if necessary.  */
14272
14273 static void
14274 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14275 {
14276   struct dwarf2_per_objfile *data = d;
14277
14278   munmap_section_buffer (&data->info);
14279   munmap_section_buffer (&data->abbrev);
14280   munmap_section_buffer (&data->line);
14281   munmap_section_buffer (&data->str);
14282   munmap_section_buffer (&data->macinfo);
14283   munmap_section_buffer (&data->ranges);
14284   munmap_section_buffer (&data->loc);
14285   munmap_section_buffer (&data->frame);
14286   munmap_section_buffer (&data->eh_frame);
14287   munmap_section_buffer (&data->gdb_index);
14288 }
14289
14290 \f
14291
14292 /* The contents of the hash table we create when building the string
14293    table.  */
14294 struct strtab_entry
14295 {
14296   offset_type offset;
14297   const char *str;
14298 };
14299
14300 /* Hash function for a strtab_entry.  */
14301 static hashval_t
14302 hash_strtab_entry (const void *e)
14303 {
14304   const struct strtab_entry *entry = e;
14305   return mapped_index_string_hash (entry->str);
14306 }
14307
14308 /* Equality function for a strtab_entry.  */
14309 static int
14310 eq_strtab_entry (const void *a, const void *b)
14311 {
14312   const struct strtab_entry *ea = a;
14313   const struct strtab_entry *eb = b;
14314   return !strcmp (ea->str, eb->str);
14315 }
14316
14317 /* Create a strtab_entry hash table.  */
14318 static htab_t
14319 create_strtab (void)
14320 {
14321   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14322                             xfree, xcalloc, xfree);
14323 }
14324
14325 /* Add a string to the constant pool.  Return the string's offset in
14326    host order.  */
14327 static offset_type
14328 add_string (htab_t table, struct obstack *cpool, const char *str)
14329 {
14330   void **slot;
14331   struct strtab_entry entry;
14332   struct strtab_entry *result;
14333
14334   entry.str = str;
14335   slot = htab_find_slot (table, &entry, INSERT);
14336   if (*slot)
14337     result = *slot;
14338   else
14339     {
14340       result = XNEW (struct strtab_entry);
14341       result->offset = obstack_object_size (cpool);
14342       result->str = str;
14343       obstack_grow_str0 (cpool, str);
14344       *slot = result;
14345     }
14346   return result->offset;
14347 }
14348
14349 /* An entry in the symbol table.  */
14350 struct symtab_index_entry
14351 {
14352   /* The name of the symbol.  */
14353   const char *name;
14354   /* The offset of the name in the constant pool.  */
14355   offset_type index_offset;
14356   /* A sorted vector of the indices of all the CUs that hold an object
14357      of this name.  */
14358   VEC (offset_type) *cu_indices;
14359 };
14360
14361 /* The symbol table.  This is a power-of-2-sized hash table.  */
14362 struct mapped_symtab
14363 {
14364   offset_type n_elements;
14365   offset_type size;
14366   struct symtab_index_entry **data;
14367 };
14368
14369 /* Hash function for a symtab_index_entry.  */
14370 static hashval_t
14371 hash_symtab_entry (const void *e)
14372 {
14373   const struct symtab_index_entry *entry = e;
14374   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14375                          sizeof (offset_type) * VEC_length (offset_type,
14376                                                             entry->cu_indices),
14377                          0);
14378 }
14379
14380 /* Equality function for a symtab_index_entry.  */
14381 static int
14382 eq_symtab_entry (const void *a, const void *b)
14383 {
14384   const struct symtab_index_entry *ea = a;
14385   const struct symtab_index_entry *eb = b;
14386   int len = VEC_length (offset_type, ea->cu_indices);
14387   if (len != VEC_length (offset_type, eb->cu_indices))
14388     return 0;
14389   return !memcmp (VEC_address (offset_type, ea->cu_indices),
14390                   VEC_address (offset_type, eb->cu_indices),
14391                   sizeof (offset_type) * len);
14392 }
14393
14394 /* Destroy a symtab_index_entry.  */
14395 static void
14396 delete_symtab_entry (void *p)
14397 {
14398   struct symtab_index_entry *entry = p;
14399   VEC_free (offset_type, entry->cu_indices);
14400   xfree (entry);
14401 }
14402
14403 /* Create a hash table holding symtab_index_entry objects.  */
14404 static htab_t
14405 create_index_table (void)
14406 {
14407   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14408                             delete_symtab_entry, xcalloc, xfree);
14409 }
14410
14411 /* Create a new mapped symtab object.  */
14412 static struct mapped_symtab *
14413 create_mapped_symtab (void)
14414 {
14415   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14416   symtab->n_elements = 0;
14417   symtab->size = 1024;
14418   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14419   return symtab;
14420 }
14421
14422 /* Destroy a mapped_symtab.  */
14423 static void
14424 cleanup_mapped_symtab (void *p)
14425 {
14426   struct mapped_symtab *symtab = p;
14427   /* The contents of the array are freed when the other hash table is
14428      destroyed.  */
14429   xfree (symtab->data);
14430   xfree (symtab);
14431 }
14432
14433 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
14434    the slot.  */
14435 static struct symtab_index_entry **
14436 find_slot (struct mapped_symtab *symtab, const char *name)
14437 {
14438   offset_type index, step, hash = mapped_index_string_hash (name);
14439
14440   index = hash & (symtab->size - 1);
14441   step = ((hash * 17) & (symtab->size - 1)) | 1;
14442
14443   for (;;)
14444     {
14445       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14446         return &symtab->data[index];
14447       index = (index + step) & (symtab->size - 1);
14448     }
14449 }
14450
14451 /* Expand SYMTAB's hash table.  */
14452 static void
14453 hash_expand (struct mapped_symtab *symtab)
14454 {
14455   offset_type old_size = symtab->size;
14456   offset_type i;
14457   struct symtab_index_entry **old_entries = symtab->data;
14458
14459   symtab->size *= 2;
14460   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14461
14462   for (i = 0; i < old_size; ++i)
14463     {
14464       if (old_entries[i])
14465         {
14466           struct symtab_index_entry **slot = find_slot (symtab,
14467                                                         old_entries[i]->name);
14468           *slot = old_entries[i];
14469         }
14470     }
14471
14472   xfree (old_entries);
14473 }
14474
14475 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
14476    is the index of the CU in which the symbol appears.  */
14477 static void
14478 add_index_entry (struct mapped_symtab *symtab, const char *name,
14479                  offset_type cu_index)
14480 {
14481   struct symtab_index_entry **slot;
14482
14483   ++symtab->n_elements;
14484   if (4 * symtab->n_elements / 3 >= symtab->size)
14485     hash_expand (symtab);
14486
14487   slot = find_slot (symtab, name);
14488   if (!*slot)
14489     {
14490       *slot = XNEW (struct symtab_index_entry);
14491       (*slot)->name = name;
14492       (*slot)->cu_indices = NULL;
14493     }
14494   /* Don't push an index twice.  Due to how we add entries we only
14495      have to check the last one.  */ 
14496   if (VEC_empty (offset_type, (*slot)->cu_indices)
14497       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14498     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14499 }
14500
14501 /* Add a vector of indices to the constant pool.  */
14502 static offset_type
14503 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14504                       struct symtab_index_entry *entry)
14505 {
14506   void **slot;
14507
14508   slot = htab_find_slot (index_table, entry, INSERT);
14509   if (!*slot)
14510     {
14511       offset_type len = VEC_length (offset_type, entry->cu_indices);
14512       offset_type val = MAYBE_SWAP (len);
14513       offset_type iter;
14514       int i;
14515
14516       *slot = entry;
14517       entry->index_offset = obstack_object_size (cpool);
14518
14519       obstack_grow (cpool, &val, sizeof (val));
14520       for (i = 0;
14521            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14522            ++i)
14523         {
14524           val = MAYBE_SWAP (iter);
14525           obstack_grow (cpool, &val, sizeof (val));
14526         }
14527     }
14528   else
14529     {
14530       struct symtab_index_entry *old_entry = *slot;
14531       entry->index_offset = old_entry->index_offset;
14532       entry = old_entry;
14533     }
14534   return entry->index_offset;
14535 }
14536
14537 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14538    constant pool entries going into the obstack CPOOL.  */
14539 static void
14540 write_hash_table (struct mapped_symtab *symtab,
14541                   struct obstack *output, struct obstack *cpool)
14542 {
14543   offset_type i;
14544   htab_t index_table;
14545   htab_t str_table;
14546
14547   index_table = create_index_table ();
14548   str_table = create_strtab ();
14549   /* We add all the index vectors to the constant pool first, to
14550      ensure alignment is ok.  */
14551   for (i = 0; i < symtab->size; ++i)
14552     {
14553       if (symtab->data[i])
14554         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14555     }
14556
14557   /* Now write out the hash table.  */
14558   for (i = 0; i < symtab->size; ++i)
14559     {
14560       offset_type str_off, vec_off;
14561
14562       if (symtab->data[i])
14563         {
14564           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14565           vec_off = symtab->data[i]->index_offset;
14566         }
14567       else
14568         {
14569           /* While 0 is a valid constant pool index, it is not valid
14570              to have 0 for both offsets.  */
14571           str_off = 0;
14572           vec_off = 0;
14573         }
14574
14575       str_off = MAYBE_SWAP (str_off);
14576       vec_off = MAYBE_SWAP (vec_off);
14577
14578       obstack_grow (output, &str_off, sizeof (str_off));
14579       obstack_grow (output, &vec_off, sizeof (vec_off));
14580     }
14581
14582   htab_delete (str_table);
14583   htab_delete (index_table);
14584 }
14585
14586 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
14587    from PST; CU_INDEX is the index of the CU in the vector of all
14588    CUs.  */
14589 static void
14590 add_address_entry (struct objfile *objfile,
14591                    struct obstack *addr_obstack, struct partial_symtab *pst,
14592                    unsigned int cu_index)
14593 {
14594   offset_type offset;
14595   char addr[8];
14596   CORE_ADDR baseaddr;
14597
14598   /* Don't bother recording empty ranges.  */
14599   if (pst->textlow == pst->texthigh)
14600     return;
14601
14602   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14603
14604   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14605   obstack_grow (addr_obstack, addr, 8);
14606   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14607   obstack_grow (addr_obstack, addr, 8);
14608   offset = MAYBE_SWAP (cu_index);
14609   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14610 }
14611
14612 /* Add a list of partial symbols to SYMTAB.  */
14613 static void
14614 write_psymbols (struct mapped_symtab *symtab,
14615                 struct partial_symbol **psymp,
14616                 int count,
14617                 offset_type cu_index)
14618 {
14619   for (; count-- > 0; ++psymp)
14620     {
14621       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14622         error (_("Ada is not currently supported by the index"));
14623       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14624     }
14625 }
14626
14627 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
14628    exception if there is an error.  */
14629 static void
14630 write_obstack (FILE *file, struct obstack *obstack)
14631 {
14632   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14633               file)
14634       != obstack_object_size (obstack))
14635     error (_("couldn't data write to file"));
14636 }
14637
14638 /* Unlink a file if the argument is not NULL.  */
14639 static void
14640 unlink_if_set (void *p)
14641 {
14642   char **filename = p;
14643   if (*filename)
14644     unlink (*filename);
14645 }
14646
14647 /* A helper struct used when iterating over debug_types.  */
14648 struct signatured_type_index_data
14649 {
14650   struct objfile *objfile;
14651   struct mapped_symtab *symtab;
14652   struct obstack *types_list;
14653   int cu_index;
14654 };
14655
14656 /* A helper function that writes a single signatured_type to an
14657    obstack.  */
14658 static int
14659 write_one_signatured_type (void **slot, void *d)
14660 {
14661   struct signatured_type_index_data *info = d;
14662   struct signatured_type *entry = (struct signatured_type *) *slot;
14663   struct dwarf2_per_cu_data *cu = &entry->per_cu;
14664   struct partial_symtab *psymtab = cu->v.psymtab;
14665   gdb_byte val[8];
14666
14667   write_psymbols (info->symtab,
14668                   info->objfile->global_psymbols.list + psymtab->globals_offset,
14669                   psymtab->n_global_syms, info->cu_index);
14670   write_psymbols (info->symtab,
14671                   info->objfile->static_psymbols.list + psymtab->statics_offset,
14672                   psymtab->n_static_syms, info->cu_index);
14673
14674   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14675   obstack_grow (info->types_list, val, 8);
14676   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14677   obstack_grow (info->types_list, val, 8);
14678   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14679   obstack_grow (info->types_list, val, 8);
14680
14681   ++info->cu_index;
14682
14683   return 1;
14684 }
14685
14686 /* Create an index file for OBJFILE in the directory DIR.  */
14687 static void
14688 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14689 {
14690   struct cleanup *cleanup;
14691   char *filename, *cleanup_filename;
14692   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14693   struct obstack cu_list, types_cu_list;
14694   int i;
14695   FILE *out_file;
14696   struct mapped_symtab *symtab;
14697   offset_type val, size_of_contents, total_len;
14698   struct stat st;
14699   char buf[8];
14700
14701   if (!objfile->psymtabs)
14702     return;
14703   if (dwarf2_per_objfile->using_index)
14704     error (_("Cannot use an index to create the index"));
14705
14706   if (stat (objfile->name, &st) < 0)
14707     perror_with_name (_("Could not stat"));
14708
14709   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14710                      INDEX_SUFFIX, (char *) NULL);
14711   cleanup = make_cleanup (xfree, filename);
14712
14713   out_file = fopen (filename, "wb");
14714   if (!out_file)
14715     error (_("Can't open `%s' for writing"), filename);
14716
14717   cleanup_filename = filename;
14718   make_cleanup (unlink_if_set, &cleanup_filename);
14719
14720   symtab = create_mapped_symtab ();
14721   make_cleanup (cleanup_mapped_symtab, symtab);
14722
14723   obstack_init (&addr_obstack);
14724   make_cleanup_obstack_free (&addr_obstack);
14725
14726   obstack_init (&cu_list);
14727   make_cleanup_obstack_free (&cu_list);
14728
14729   obstack_init (&types_cu_list);
14730   make_cleanup_obstack_free (&types_cu_list);
14731
14732   /* The list is already sorted, so we don't need to do additional
14733      work here.  Also, the debug_types entries do not appear in
14734      all_comp_units, but only in their own hash table.  */
14735   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14736     {
14737       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14738       struct partial_symtab *psymtab = cu->v.psymtab;
14739       gdb_byte val[8];
14740
14741       write_psymbols (symtab,
14742                       objfile->global_psymbols.list + psymtab->globals_offset,
14743                       psymtab->n_global_syms, i);
14744       write_psymbols (symtab,
14745                       objfile->static_psymbols.list + psymtab->statics_offset,
14746                       psymtab->n_static_syms, i);
14747
14748       add_address_entry (objfile, &addr_obstack, psymtab, i);
14749
14750       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14751       obstack_grow (&cu_list, val, 8);
14752       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14753       obstack_grow (&cu_list, val, 8);
14754     }
14755
14756   /* Write out the .debug_type entries, if any.  */
14757   if (dwarf2_per_objfile->signatured_types)
14758     {
14759       struct signatured_type_index_data sig_data;
14760
14761       sig_data.objfile = objfile;
14762       sig_data.symtab = symtab;
14763       sig_data.types_list = &types_cu_list;
14764       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14765       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14766                               write_one_signatured_type, &sig_data);
14767     }
14768
14769   obstack_init (&constant_pool);
14770   make_cleanup_obstack_free (&constant_pool);
14771   obstack_init (&symtab_obstack);
14772   make_cleanup_obstack_free (&symtab_obstack);
14773   write_hash_table (symtab, &symtab_obstack, &constant_pool);
14774
14775   obstack_init (&contents);
14776   make_cleanup_obstack_free (&contents);
14777   size_of_contents = 6 * sizeof (offset_type);
14778   total_len = size_of_contents;
14779
14780   /* The version number.  */
14781   val = MAYBE_SWAP (2);
14782   obstack_grow (&contents, &val, sizeof (val));
14783
14784   /* The offset of the CU list from the start of the file.  */
14785   val = MAYBE_SWAP (total_len);
14786   obstack_grow (&contents, &val, sizeof (val));
14787   total_len += obstack_object_size (&cu_list);
14788
14789   /* The offset of the types CU list from the start of the file.  */
14790   val = MAYBE_SWAP (total_len);
14791   obstack_grow (&contents, &val, sizeof (val));
14792   total_len += obstack_object_size (&types_cu_list);
14793
14794   /* The offset of the address table from the start of the file.  */
14795   val = MAYBE_SWAP (total_len);
14796   obstack_grow (&contents, &val, sizeof (val));
14797   total_len += obstack_object_size (&addr_obstack);
14798
14799   /* The offset of the symbol table from the start of the file.  */
14800   val = MAYBE_SWAP (total_len);
14801   obstack_grow (&contents, &val, sizeof (val));
14802   total_len += obstack_object_size (&symtab_obstack);
14803
14804   /* The offset of the constant pool from the start of the file.  */
14805   val = MAYBE_SWAP (total_len);
14806   obstack_grow (&contents, &val, sizeof (val));
14807   total_len += obstack_object_size (&constant_pool);
14808
14809   gdb_assert (obstack_object_size (&contents) == size_of_contents);
14810
14811   write_obstack (out_file, &contents);
14812   write_obstack (out_file, &cu_list);
14813   write_obstack (out_file, &types_cu_list);
14814   write_obstack (out_file, &addr_obstack);
14815   write_obstack (out_file, &symtab_obstack);
14816   write_obstack (out_file, &constant_pool);
14817
14818   fclose (out_file);
14819
14820   /* We want to keep the file, so we set cleanup_filename to NULL
14821      here.  See unlink_if_set.  */
14822   cleanup_filename = NULL;
14823
14824   do_cleanups (cleanup);
14825 }
14826
14827 /* The mapped index file format is designed to be directly mmap()able
14828    on any architecture.  In most cases, a datum is represented using a
14829    little-endian 32-bit integer value, called an offset_type.  Big
14830    endian machines must byte-swap the values before using them.
14831    Exceptions to this rule are noted.  The data is laid out such that
14832    alignment is always respected.
14833
14834    A mapped index consists of several sections.
14835
14836    1. The file header.  This is a sequence of values, of offset_type
14837    unless otherwise noted:
14838    [0] The version number.  Currently 1 or 2.  The differences are
14839    noted below.  Version 1 did not account for .debug_types sections;
14840    the presence of a .debug_types section invalidates any version 1
14841    index that may exist.
14842    [1] The offset, from the start of the file, of the CU list.
14843    [1.5] In version 2, the offset, from the start of the file, of the
14844    types CU list.  This offset does not appear in version 1.  Note
14845    that this can be empty, in which case this offset will be equal to
14846    the next offset.
14847    [2] The offset, from the start of the file, of the address section.
14848    [3] The offset, from the start of the file, of the symbol table.
14849    [4] The offset, from the start of the file, of the constant pool.
14850
14851    2. The CU list.  This is a sequence of pairs of 64-bit
14852    little-endian values, sorted by the CU offset.  The first element
14853    in each pair is the offset of a CU in the .debug_info section.  The
14854    second element in each pair is the length of that CU.  References
14855    to a CU elsewhere in the map are done using a CU index, which is
14856    just the 0-based index into this table.  Note that if there are
14857    type CUs, then conceptually CUs and type CUs form a single list for
14858    the purposes of CU indices.
14859
14860    2.5 The types CU list.  This does not appear in a version 1 index.
14861    This is a sequence of triplets of 64-bit little-endian values.  In
14862    a triplet, the first value is the CU offset, the second value is
14863    the type offset in the CU, and the third value is the type
14864    signature.  The types CU list is not sorted.
14865
14866    3. The address section.  The address section consists of a sequence
14867    of address entries.  Each address entry has three elements.
14868    [0] The low address.  This is a 64-bit little-endian value.
14869    [1] The high address.  This is a 64-bit little-endian value.
14870    [2] The CU index.  This is an offset_type value.
14871
14872    4. The symbol table.  This is a hash table.  The size of the hash
14873    table is always a power of 2.  The initial hash and the step are
14874    currently defined by the `find_slot' function.
14875
14876    Each slot in the hash table consists of a pair of offset_type
14877    values.  The first value is the offset of the symbol's name in the
14878    constant pool.  The second value is the offset of the CU vector in
14879    the constant pool.
14880
14881    If both values are 0, then this slot in the hash table is empty.
14882    This is ok because while 0 is a valid constant pool index, it
14883    cannot be a valid index for both a string and a CU vector.
14884
14885    A string in the constant pool is stored as a \0-terminated string,
14886    as you'd expect.
14887
14888    A CU vector in the constant pool is a sequence of offset_type
14889    values.  The first value is the number of CU indices in the vector.
14890    Each subsequent value is the index of a CU in the CU list.  This
14891    element in the hash table is used to indicate which CUs define the
14892    symbol.
14893
14894    5. The constant pool.  This is simply a bunch of bytes.  It is
14895    organized so that alignment is correct: CU vectors are stored
14896    first, followed by strings.  */
14897 static void
14898 save_gdb_index_command (char *arg, int from_tty)
14899 {
14900   struct objfile *objfile;
14901
14902   if (!arg || !*arg)
14903     error (_("usage: save gdb-index DIRECTORY"));
14904
14905   ALL_OBJFILES (objfile)
14906   {
14907     struct stat st;
14908
14909     /* If the objfile does not correspond to an actual file, skip it.  */
14910     if (stat (objfile->name, &st) < 0)
14911       continue;
14912
14913     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14914     if (dwarf2_per_objfile)
14915       {
14916         volatile struct gdb_exception except;
14917
14918         TRY_CATCH (except, RETURN_MASK_ERROR)
14919           {
14920             write_psymtabs_to_index (objfile, arg);
14921           }
14922         if (except.reason < 0)
14923           exception_fprintf (gdb_stderr, except,
14924                              _("Error while writing index for `%s': "),
14925                              objfile->name);
14926       }
14927   }
14928 }
14929
14930 \f
14931
14932 int dwarf2_always_disassemble;
14933
14934 static void
14935 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14936                                 struct cmd_list_element *c, const char *value)
14937 {
14938   fprintf_filtered (file, _("\
14939 Whether to always disassemble DWARF expressions is %s.\n"),
14940                     value);
14941 }
14942
14943 void _initialize_dwarf2_read (void);
14944
14945 void
14946 _initialize_dwarf2_read (void)
14947 {
14948   struct cmd_list_element *c;
14949
14950   dwarf2_objfile_data_key
14951     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
14952
14953   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14954 Set DWARF 2 specific variables.\n\
14955 Configure DWARF 2 variables such as the cache size"),
14956                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14957                   0/*allow-unknown*/, &maintenance_set_cmdlist);
14958
14959   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14960 Show DWARF 2 specific variables\n\
14961 Show DWARF 2 variables such as the cache size"),
14962                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14963                   0/*allow-unknown*/, &maintenance_show_cmdlist);
14964
14965   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
14966                             &dwarf2_max_cache_age, _("\
14967 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14968 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14969 A higher limit means that cached compilation units will be stored\n\
14970 in memory longer, and more total memory will be used.  Zero disables\n\
14971 caching, which can slow down startup."),
14972                             NULL,
14973                             show_dwarf2_max_cache_age,
14974                             &set_dwarf2_cmdlist,
14975                             &show_dwarf2_cmdlist);
14976
14977   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14978                            &dwarf2_always_disassemble, _("\
14979 Set whether `info address' always disassembles DWARF expressions."), _("\
14980 Show whether `info address' always disassembles DWARF expressions."), _("\
14981 When enabled, DWARF expressions are always printed in an assembly-like\n\
14982 syntax.  When disabled, expressions will be printed in a more\n\
14983 conversational style, when possible."),
14984                            NULL,
14985                            show_dwarf2_always_disassemble,
14986                            &set_dwarf2_cmdlist,
14987                            &show_dwarf2_cmdlist);
14988
14989   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14990 Set debugging of the dwarf2 DIE reader."), _("\
14991 Show debugging of the dwarf2 DIE reader."), _("\
14992 When enabled (non-zero), DIEs are dumped after they are read in.\n\
14993 The value is the maximum depth to print."),
14994                             NULL,
14995                             NULL,
14996                             &setdebuglist, &showdebuglist);
14997
14998   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
14999                _("Save a .gdb-index file"),
15000                &save_cmdlist);
15001   set_cmd_completer (c, filename_completer);
15002 }