* value.c (value_fn_field): Constify.
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81    Because of alignment constraints, this structure has padding and cannot
82    be mapped directly onto the beginning of the .debug_info section.  */
83 typedef struct comp_unit_header
84   {
85     unsigned int length;        /* length of the .debug_info
86                                    contribution */
87     unsigned short version;     /* version number -- 2 for DWARF
88                                    version 2 */
89     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90     unsigned char addr_size;    /* byte size of an address -- 4 */
91   }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97    Because of alignment constraints, this structure has padding and cannot
98    be mapped directly onto the beginning of the .debug_info section.  */
99 typedef struct statement_prologue
100   {
101     unsigned int total_length;  /* byte length of the statement
102                                    information */
103     unsigned short version;     /* version number -- 2 for DWARF
104                                    version 2 */
105     unsigned int prologue_length;       /* # bytes between prologue &
106                                            stmt program */
107     unsigned char minimum_instruction_length;   /* byte size of
108                                                    smallest instr */
109     unsigned char default_is_stmt;      /* initial value of is_stmt
110                                            register */
111     char line_base;
112     unsigned char line_range;
113     unsigned char opcode_base;  /* number assigned to first special
114                                    opcode */
115     unsigned char *standard_opcode_lengths;
116   }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in.  */
120 static int dwarf2_die_debug = 0;
121
122 static int pagesize;
123
124 /* When set, the file that we're processing is known to have debugging
125    info for C++ namespaces.  GCC 3.3.x did not produce this information,
126    but later versions do.  */
127
128 static int processing_has_namespace_info;
129
130 static const struct objfile_data *dwarf2_objfile_data_key;
131
132 struct dwarf2_section_info
133 {
134   asection *asection;
135   gdb_byte *buffer;
136   bfd_size_type size;
137   int was_mmapped;
138   /* True if we have tried to read this section.  */
139   int readin;
140 };
141
142 /* All offsets in the index are of this type.  It must be
143    architecture-independent.  */
144 typedef uint32_t offset_type;
145
146 DEF_VEC_I (offset_type);
147
148 /* A description of the mapped index.  The file format is described in
149    a comment by the code that writes the index.  */
150 struct mapped_index
151 {
152   /* Index data format version.  */
153   int version;
154
155   /* The total length of the buffer.  */
156   off_t total_size;
157
158   /* A pointer to the address table data.  */
159   const gdb_byte *address_table;
160
161   /* Size of the address table data in bytes.  */
162   offset_type address_table_size;
163
164   /* The symbol table, implemented as a hash table.  */
165   const offset_type *symbol_table;
166
167   /* Size in slots, each slot is 2 offset_types.  */
168   offset_type symbol_table_slots;
169
170   /* A pointer to the constant pool.  */
171   const char *constant_pool;
172 };
173
174 struct dwarf2_per_objfile
175 {
176   struct dwarf2_section_info info;
177   struct dwarf2_section_info abbrev;
178   struct dwarf2_section_info line;
179   struct dwarf2_section_info loc;
180   struct dwarf2_section_info macinfo;
181   struct dwarf2_section_info str;
182   struct dwarf2_section_info ranges;
183   struct dwarf2_section_info types;
184   struct dwarf2_section_info frame;
185   struct dwarf2_section_info eh_frame;
186   struct dwarf2_section_info gdb_index;
187
188   /* Back link.  */
189   struct objfile *objfile;
190
191   /* A list of all the compilation units.  This is used to locate
192      the target compilation unit of a particular reference.  */
193   struct dwarf2_per_cu_data **all_comp_units;
194
195   /* The number of compilation units in ALL_COMP_UNITS.  */
196   int n_comp_units;
197
198   /* The number of .debug_types-related CUs.  */
199   int n_type_comp_units;
200
201   /* The .debug_types-related CUs.  */
202   struct dwarf2_per_cu_data **type_comp_units;
203
204   /* A chain of compilation units that are currently read in, so that
205      they can be freed later.  */
206   struct dwarf2_per_cu_data *read_in_chain;
207
208   /* A table mapping .debug_types signatures to its signatured_type entry.
209      This is NULL if the .debug_types section hasn't been read in yet.  */
210   htab_t signatured_types;
211
212   /* A flag indicating wether this objfile has a section loaded at a
213      VMA of 0.  */
214   int has_section_at_zero;
215
216   /* True if we are using the mapped index,
217      or we are faking it for OBJF_READNOW's sake.  */
218   unsigned char using_index;
219
220   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
221   struct mapped_index *index_table;
222
223   /* When using index_table, this keeps track of all quick_file_names entries.
224      TUs can share line table entries with CUs or other TUs, and there can be
225      a lot more TUs than unique line tables, so we maintain a separate table
226      of all line table entries to support the sharing.  */
227   htab_t quick_file_names_table;
228
229   /* Set during partial symbol reading, to prevent queueing of full
230      symbols.  */
231   int reading_partial_symbols;
232
233   /* Table mapping type .debug_info DIE offsets to types.
234      This is NULL if not allocated yet.
235      It (currently) makes sense to allocate debug_types_type_hash lazily.
236      To keep things simple we allocate both lazily.  */
237   htab_t debug_info_type_hash;
238
239   /* Table mapping type .debug_types DIE offsets to types.
240      This is NULL if not allocated yet.  */
241   htab_t debug_types_type_hash;
242 };
243
244 static struct dwarf2_per_objfile *dwarf2_per_objfile;
245
246 /* names of the debugging sections */
247
248 /* Note that if the debugging section has been compressed, it might
249    have a name like .zdebug_info.  */
250
251 #define INFO_SECTION     "debug_info"
252 #define ABBREV_SECTION   "debug_abbrev"
253 #define LINE_SECTION     "debug_line"
254 #define LOC_SECTION      "debug_loc"
255 #define MACINFO_SECTION  "debug_macinfo"
256 #define STR_SECTION      "debug_str"
257 #define RANGES_SECTION   "debug_ranges"
258 #define TYPES_SECTION    "debug_types"
259 #define FRAME_SECTION    "debug_frame"
260 #define EH_FRAME_SECTION "eh_frame"
261 #define GDB_INDEX_SECTION "gdb_index"
262
263 /* local data types */
264
265 /* We hold several abbreviation tables in memory at the same time.  */
266 #ifndef ABBREV_HASH_SIZE
267 #define ABBREV_HASH_SIZE 121
268 #endif
269
270 /* The data in a compilation unit header, after target2host
271    translation, looks like this.  */
272 struct comp_unit_head
273 {
274   unsigned int length;
275   short version;
276   unsigned char addr_size;
277   unsigned char signed_addr_p;
278   unsigned int abbrev_offset;
279
280   /* Size of file offsets; either 4 or 8.  */
281   unsigned int offset_size;
282
283   /* Size of the length field; either 4 or 12.  */
284   unsigned int initial_length_size;
285
286   /* Offset to the first byte of this compilation unit header in the
287      .debug_info section, for resolving relative reference dies.  */
288   unsigned int offset;
289
290   /* Offset to first die in this cu from the start of the cu.
291      This will be the first byte following the compilation unit header.  */
292   unsigned int first_die_offset;
293 };
294
295 /* Type used for delaying computation of method physnames.
296    See comments for compute_delayed_physnames.  */
297 struct delayed_method_info
298 {
299   /* The type to which the method is attached, i.e., its parent class.  */
300   struct type *type;
301
302   /* The index of the method in the type's function fieldlists.  */
303   int fnfield_index;
304
305   /* The index of the method in the fieldlist.  */
306   int index;
307
308   /* The name of the DIE.  */
309   const char *name;
310
311   /*  The DIE associated with this method.  */
312   struct die_info *die;
313 };
314
315 typedef struct delayed_method_info delayed_method_info;
316 DEF_VEC_O (delayed_method_info);
317
318 /* Internal state when decoding a particular compilation unit.  */
319 struct dwarf2_cu
320 {
321   /* The objfile containing this compilation unit.  */
322   struct objfile *objfile;
323
324   /* The header of the compilation unit.  */
325   struct comp_unit_head header;
326
327   /* Base address of this compilation unit.  */
328   CORE_ADDR base_address;
329
330   /* Non-zero if base_address has been set.  */
331   int base_known;
332
333   struct function_range *first_fn, *last_fn, *cached_fn;
334
335   /* The language we are debugging.  */
336   enum language language;
337   const struct language_defn *language_defn;
338
339   const char *producer;
340
341   /* The generic symbol table building routines have separate lists for
342      file scope symbols and all all other scopes (local scopes).  So
343      we need to select the right one to pass to add_symbol_to_list().
344      We do it by keeping a pointer to the correct list in list_in_scope.
345
346      FIXME: The original dwarf code just treated the file scope as the
347      first local scope, and all other local scopes as nested local
348      scopes, and worked fine.  Check to see if we really need to
349      distinguish these in buildsym.c.  */
350   struct pending **list_in_scope;
351
352   /* DWARF abbreviation table associated with this compilation unit.  */
353   struct abbrev_info **dwarf2_abbrevs;
354
355   /* Storage for the abbrev table.  */
356   struct obstack abbrev_obstack;
357
358   /* Hash table holding all the loaded partial DIEs.  */
359   htab_t partial_dies;
360
361   /* Storage for things with the same lifetime as this read-in compilation
362      unit, including partial DIEs.  */
363   struct obstack comp_unit_obstack;
364
365   /* When multiple dwarf2_cu structures are living in memory, this field
366      chains them all together, so that they can be released efficiently.
367      We will probably also want a generation counter so that most-recently-used
368      compilation units are cached...  */
369   struct dwarf2_per_cu_data *read_in_chain;
370
371   /* Backchain to our per_cu entry if the tree has been built.  */
372   struct dwarf2_per_cu_data *per_cu;
373
374   /* How many compilation units ago was this CU last referenced?  */
375   int last_used;
376
377   /* A hash table of die offsets for following references.  */
378   htab_t die_hash;
379
380   /* Full DIEs if read in.  */
381   struct die_info *dies;
382
383   /* A set of pointers to dwarf2_per_cu_data objects for compilation
384      units referenced by this one.  Only set during full symbol processing;
385      partial symbol tables do not have dependencies.  */
386   htab_t dependencies;
387
388   /* Header data from the line table, during full symbol processing.  */
389   struct line_header *line_header;
390
391   /* A list of methods which need to have physnames computed
392      after all type information has been read.  */
393   VEC (delayed_method_info) *method_list;
394
395   /* Mark used when releasing cached dies.  */
396   unsigned int mark : 1;
397
398   /* This flag will be set if this compilation unit might include
399      inter-compilation-unit references.  */
400   unsigned int has_form_ref_addr : 1;
401
402   /* This flag will be set if this compilation unit includes any
403      DW_TAG_namespace DIEs.  If we know that there are explicit
404      DIEs for namespaces, we don't need to try to infer them
405      from mangled names.  */
406   unsigned int has_namespace_info : 1;
407
408   /* This CU references .debug_loc.  See the symtab->locations_valid field.
409      This test is imperfect as there may exist optimized debug code not using
410      any location list and still facing inlining issues if handled as
411      unoptimized code.  For a future better test see GCC PR other/32998.  */
412
413   unsigned int has_loclist : 1;
414 };
415
416 /* Persistent data held for a compilation unit, even when not
417    processing it.  We put a pointer to this structure in the
418    read_symtab_private field of the psymtab.  If we encounter
419    inter-compilation-unit references, we also maintain a sorted
420    list of all compilation units.  */
421
422 struct dwarf2_per_cu_data
423 {
424   /* The start offset and length of this compilation unit.  2**29-1
425      bytes should suffice to store the length of any compilation unit
426      - if it doesn't, GDB will fall over anyway.
427      NOTE: Unlike comp_unit_head.length, this length includes
428      initial_length_size.  */
429   unsigned int offset;
430   unsigned int length : 29;
431
432   /* Flag indicating this compilation unit will be read in before
433      any of the current compilation units are processed.  */
434   unsigned int queued : 1;
435
436   /* This flag will be set if we need to load absolutely all DIEs
437      for this compilation unit, instead of just the ones we think
438      are interesting.  It gets set if we look for a DIE in the
439      hash table and don't find it.  */
440   unsigned int load_all_dies : 1;
441
442   /* Non-zero if this CU is from .debug_types.
443      Otherwise it's from .debug_info.  */
444   unsigned int from_debug_types : 1;
445
446   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
447      of the CU cache it gets reset to NULL again.  */
448   struct dwarf2_cu *cu;
449
450   /* The corresponding objfile.  */
451   struct objfile *objfile;
452
453   /* When using partial symbol tables, the 'psymtab' field is active.
454      Otherwise the 'quick' field is active.  */
455   union
456   {
457     /* The partial symbol table associated with this compilation unit,
458        or NULL for partial units (which do not have an associated
459        symtab).  */
460     struct partial_symtab *psymtab;
461
462     /* Data needed by the "quick" functions.  */
463     struct dwarf2_per_cu_quick_data *quick;
464   } v;
465 };
466
467 /* Entry in the signatured_types hash table.  */
468
469 struct signatured_type
470 {
471   ULONGEST signature;
472
473   /* Offset in .debug_types of the TU (type_unit) for this type.  */
474   unsigned int offset;
475
476   /* Offset in .debug_types of the type defined by this TU.  */
477   unsigned int type_offset;
478
479   /* The CU(/TU) of this type.  */
480   struct dwarf2_per_cu_data per_cu;
481 };
482
483 /* Struct used to pass misc. parameters to read_die_and_children, et
484    al.  which are used for both .debug_info and .debug_types dies.
485    All parameters here are unchanging for the life of the call.  This
486    struct exists to abstract away the constant parameters of die
487    reading.  */
488
489 struct die_reader_specs
490 {
491   /* The bfd of this objfile.  */
492   bfd* abfd;
493
494   /* The CU of the DIE we are parsing.  */
495   struct dwarf2_cu *cu;
496
497   /* Pointer to start of section buffer.
498      This is either the start of .debug_info or .debug_types.  */
499   const gdb_byte *buffer;
500 };
501
502 /* The line number information for a compilation unit (found in the
503    .debug_line section) begins with a "statement program header",
504    which contains the following information.  */
505 struct line_header
506 {
507   unsigned int total_length;
508   unsigned short version;
509   unsigned int header_length;
510   unsigned char minimum_instruction_length;
511   unsigned char maximum_ops_per_instruction;
512   unsigned char default_is_stmt;
513   int line_base;
514   unsigned char line_range;
515   unsigned char opcode_base;
516
517   /* standard_opcode_lengths[i] is the number of operands for the
518      standard opcode whose value is i.  This means that
519      standard_opcode_lengths[0] is unused, and the last meaningful
520      element is standard_opcode_lengths[opcode_base - 1].  */
521   unsigned char *standard_opcode_lengths;
522
523   /* The include_directories table.  NOTE!  These strings are not
524      allocated with xmalloc; instead, they are pointers into
525      debug_line_buffer.  If you try to free them, `free' will get
526      indigestion.  */
527   unsigned int num_include_dirs, include_dirs_size;
528   char **include_dirs;
529
530   /* The file_names table.  NOTE!  These strings are not allocated
531      with xmalloc; instead, they are pointers into debug_line_buffer.
532      Don't try to free them directly.  */
533   unsigned int num_file_names, file_names_size;
534   struct file_entry
535   {
536     char *name;
537     unsigned int dir_index;
538     unsigned int mod_time;
539     unsigned int length;
540     int included_p; /* Non-zero if referenced by the Line Number Program.  */
541     struct symtab *symtab; /* The associated symbol table, if any.  */
542   } *file_names;
543
544   /* The start and end of the statement program following this
545      header.  These point into dwarf2_per_objfile->line_buffer.  */
546   gdb_byte *statement_program_start, *statement_program_end;
547 };
548
549 /* When we construct a partial symbol table entry we only
550    need this much information.  */
551 struct partial_die_info
552   {
553     /* Offset of this DIE.  */
554     unsigned int offset;
555
556     /* DWARF-2 tag for this DIE.  */
557     ENUM_BITFIELD(dwarf_tag) tag : 16;
558
559     /* Assorted flags describing the data found in this DIE.  */
560     unsigned int has_children : 1;
561     unsigned int is_external : 1;
562     unsigned int is_declaration : 1;
563     unsigned int has_type : 1;
564     unsigned int has_specification : 1;
565     unsigned int has_pc_info : 1;
566
567     /* Flag set if the SCOPE field of this structure has been
568        computed.  */
569     unsigned int scope_set : 1;
570
571     /* Flag set if the DIE has a byte_size attribute.  */
572     unsigned int has_byte_size : 1;
573
574     /* Flag set if any of the DIE's children are template arguments.  */
575     unsigned int has_template_arguments : 1;
576
577     /* Flag set if fixup_partial_die has been called on this die.  */
578     unsigned int fixup_called : 1;
579
580     /* The name of this DIE.  Normally the value of DW_AT_name, but
581        sometimes a default name for unnamed DIEs.  */
582     char *name;
583
584     /* The linkage name, if present.  */
585     const char *linkage_name;
586
587     /* The scope to prepend to our children.  This is generally
588        allocated on the comp_unit_obstack, so will disappear
589        when this compilation unit leaves the cache.  */
590     char *scope;
591
592     /* The location description associated with this DIE, if any.  */
593     struct dwarf_block *locdesc;
594
595     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
596     CORE_ADDR lowpc;
597     CORE_ADDR highpc;
598
599     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
600        DW_AT_sibling, if any.  */
601     /* NOTE: This member isn't strictly necessary, read_partial_die could
602        return DW_AT_sibling values to its caller load_partial_dies.  */
603     gdb_byte *sibling;
604
605     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
606        DW_AT_specification (or DW_AT_abstract_origin or
607        DW_AT_extension).  */
608     unsigned int spec_offset;
609
610     /* Pointers to this DIE's parent, first child, and next sibling,
611        if any.  */
612     struct partial_die_info *die_parent, *die_child, *die_sibling;
613   };
614
615 /* This data structure holds the information of an abbrev.  */
616 struct abbrev_info
617   {
618     unsigned int number;        /* number identifying abbrev */
619     enum dwarf_tag tag;         /* dwarf tag */
620     unsigned short has_children;                /* boolean */
621     unsigned short num_attrs;   /* number of attributes */
622     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
623     struct abbrev_info *next;   /* next in chain */
624   };
625
626 struct attr_abbrev
627   {
628     ENUM_BITFIELD(dwarf_attribute) name : 16;
629     ENUM_BITFIELD(dwarf_form) form : 16;
630   };
631
632 /* Attributes have a name and a value.  */
633 struct attribute
634   {
635     ENUM_BITFIELD(dwarf_attribute) name : 16;
636     ENUM_BITFIELD(dwarf_form) form : 15;
637
638     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
639        field should be in u.str (existing only for DW_STRING) but it is kept
640        here for better struct attribute alignment.  */
641     unsigned int string_is_canonical : 1;
642
643     union
644       {
645         char *str;
646         struct dwarf_block *blk;
647         ULONGEST unsnd;
648         LONGEST snd;
649         CORE_ADDR addr;
650         struct signatured_type *signatured_type;
651       }
652     u;
653   };
654
655 /* This data structure holds a complete die structure.  */
656 struct die_info
657   {
658     /* DWARF-2 tag for this DIE.  */
659     ENUM_BITFIELD(dwarf_tag) tag : 16;
660
661     /* Number of attributes */
662     unsigned char num_attrs;
663
664     /* True if we're presently building the full type name for the
665        type derived from this DIE.  */
666     unsigned char building_fullname : 1;
667
668     /* Abbrev number */
669     unsigned int abbrev;
670
671     /* Offset in .debug_info or .debug_types section.  */
672     unsigned int offset;
673
674     /* The dies in a compilation unit form an n-ary tree.  PARENT
675        points to this die's parent; CHILD points to the first child of
676        this node; and all the children of a given node are chained
677        together via their SIBLING fields.  */
678     struct die_info *child;     /* Its first child, if any.  */
679     struct die_info *sibling;   /* Its next sibling, if any.  */
680     struct die_info *parent;    /* Its parent, if any.  */
681
682     /* An array of attributes, with NUM_ATTRS elements.  There may be
683        zero, but it's not common and zero-sized arrays are not
684        sufficiently portable C.  */
685     struct attribute attrs[1];
686   };
687
688 struct function_range
689 {
690   const char *name;
691   CORE_ADDR lowpc, highpc;
692   int seen_line;
693   struct function_range *next;
694 };
695
696 /* Get at parts of an attribute structure.  */
697
698 #define DW_STRING(attr)    ((attr)->u.str)
699 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
700 #define DW_UNSND(attr)     ((attr)->u.unsnd)
701 #define DW_BLOCK(attr)     ((attr)->u.blk)
702 #define DW_SND(attr)       ((attr)->u.snd)
703 #define DW_ADDR(attr)      ((attr)->u.addr)
704 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
705
706 /* Blocks are a bunch of untyped bytes.  */
707 struct dwarf_block
708   {
709     unsigned int size;
710     gdb_byte *data;
711   };
712
713 #ifndef ATTR_ALLOC_CHUNK
714 #define ATTR_ALLOC_CHUNK 4
715 #endif
716
717 /* Allocate fields for structs, unions and enums in this size.  */
718 #ifndef DW_FIELD_ALLOC_CHUNK
719 #define DW_FIELD_ALLOC_CHUNK 4
720 #endif
721
722 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
723    but this would require a corresponding change in unpack_field_as_long
724    and friends.  */
725 static int bits_per_byte = 8;
726
727 /* The routines that read and process dies for a C struct or C++ class
728    pass lists of data member fields and lists of member function fields
729    in an instance of a field_info structure, as defined below.  */
730 struct field_info
731   {
732     /* List of data member and baseclasses fields.  */
733     struct nextfield
734       {
735         struct nextfield *next;
736         int accessibility;
737         int virtuality;
738         struct field field;
739       }
740      *fields, *baseclasses;
741
742     /* Number of fields (including baseclasses).  */
743     int nfields;
744
745     /* Number of baseclasses.  */
746     int nbaseclasses;
747
748     /* Set if the accesibility of one of the fields is not public.  */
749     int non_public_fields;
750
751     /* Member function fields array, entries are allocated in the order they
752        are encountered in the object file.  */
753     struct nextfnfield
754       {
755         struct nextfnfield *next;
756         struct fn_field fnfield;
757       }
758      *fnfields;
759
760     /* Member function fieldlist array, contains name of possibly overloaded
761        member function, number of overloaded member functions and a pointer
762        to the head of the member function field chain.  */
763     struct fnfieldlist
764       {
765         char *name;
766         int length;
767         struct nextfnfield *head;
768       }
769      *fnfieldlists;
770
771     /* Number of entries in the fnfieldlists array.  */
772     int nfnfields;
773
774     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
775        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
776     struct typedef_field_list
777       {
778         struct typedef_field field;
779         struct typedef_field_list *next;
780       }
781     *typedef_field_list;
782     unsigned typedef_field_list_count;
783   };
784
785 /* One item on the queue of compilation units to read in full symbols
786    for.  */
787 struct dwarf2_queue_item
788 {
789   struct dwarf2_per_cu_data *per_cu;
790   struct dwarf2_queue_item *next;
791 };
792
793 /* The current queue.  */
794 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
795
796 /* Loaded secondary compilation units are kept in memory until they
797    have not been referenced for the processing of this many
798    compilation units.  Set this to zero to disable caching.  Cache
799    sizes of up to at least twenty will improve startup time for
800    typical inter-CU-reference binaries, at an obvious memory cost.  */
801 static int dwarf2_max_cache_age = 5;
802 static void
803 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
804                            struct cmd_list_element *c, const char *value)
805 {
806   fprintf_filtered (file, _("The upper bound on the age of cached "
807                             "dwarf2 compilation units is %s.\n"),
808                     value);
809 }
810
811
812 /* Various complaints about symbol reading that don't abort the process.  */
813
814 static void
815 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
816 {
817   complaint (&symfile_complaints,
818              _("statement list doesn't fit in .debug_line section"));
819 }
820
821 static void
822 dwarf2_debug_line_missing_file_complaint (void)
823 {
824   complaint (&symfile_complaints,
825              _(".debug_line section has line data without a file"));
826 }
827
828 static void
829 dwarf2_debug_line_missing_end_sequence_complaint (void)
830 {
831   complaint (&symfile_complaints,
832              _(".debug_line section has line "
833                "program sequence without an end"));
834 }
835
836 static void
837 dwarf2_complex_location_expr_complaint (void)
838 {
839   complaint (&symfile_complaints, _("location expression too complex"));
840 }
841
842 static void
843 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
844                                               int arg3)
845 {
846   complaint (&symfile_complaints,
847              _("const value length mismatch for '%s', got %d, expected %d"),
848              arg1, arg2, arg3);
849 }
850
851 static void
852 dwarf2_macros_too_long_complaint (void)
853 {
854   complaint (&symfile_complaints,
855              _("macro info runs off end of `.debug_macinfo' section"));
856 }
857
858 static void
859 dwarf2_macro_malformed_definition_complaint (const char *arg1)
860 {
861   complaint (&symfile_complaints,
862              _("macro debug info contains a "
863                "malformed macro definition:\n`%s'"),
864              arg1);
865 }
866
867 static void
868 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
869 {
870   complaint (&symfile_complaints,
871              _("invalid attribute class or form for '%s' in '%s'"),
872              arg1, arg2);
873 }
874
875 /* local function prototypes */
876
877 static void dwarf2_locate_sections (bfd *, asection *, void *);
878
879 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880                                            struct objfile *);
881
882 static void dwarf2_build_psymtabs_hard (struct objfile *);
883
884 static void scan_partial_symbols (struct partial_die_info *,
885                                   CORE_ADDR *, CORE_ADDR *,
886                                   int, struct dwarf2_cu *);
887
888 static void add_partial_symbol (struct partial_die_info *,
889                                 struct dwarf2_cu *);
890
891 static void add_partial_namespace (struct partial_die_info *pdi,
892                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
893                                    int need_pc, struct dwarf2_cu *cu);
894
895 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896                                 CORE_ADDR *highpc, int need_pc,
897                                 struct dwarf2_cu *cu);
898
899 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900                                      struct dwarf2_cu *cu);
901
902 static void add_partial_subprogram (struct partial_die_info *pdi,
903                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
904                                     int need_pc, struct dwarf2_cu *cu);
905
906 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
907                                      gdb_byte *buffer, gdb_byte *info_ptr,
908                                      bfd *abfd, struct dwarf2_cu *cu);
909
910 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
911
912 static void psymtab_to_symtab_1 (struct partial_symtab *);
913
914 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
915
916 static void dwarf2_free_abbrev_table (void *);
917
918 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
919                                             struct dwarf2_cu *);
920
921 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
922                                                  struct dwarf2_cu *);
923
924 static struct partial_die_info *load_partial_dies (bfd *,
925                                                    gdb_byte *, gdb_byte *,
926                                                    int, struct dwarf2_cu *);
927
928 static gdb_byte *read_partial_die (struct partial_die_info *,
929                                    struct abbrev_info *abbrev,
930                                    unsigned int, bfd *,
931                                    gdb_byte *, gdb_byte *,
932                                    struct dwarf2_cu *);
933
934 static struct partial_die_info *find_partial_die (unsigned int,
935                                                   struct dwarf2_cu *);
936
937 static void fixup_partial_die (struct partial_die_info *,
938                                struct dwarf2_cu *);
939
940 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941                                  bfd *, gdb_byte *, struct dwarf2_cu *);
942
943 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944                                        bfd *, gdb_byte *, struct dwarf2_cu *);
945
946 static unsigned int read_1_byte (bfd *, gdb_byte *);
947
948 static int read_1_signed_byte (bfd *, gdb_byte *);
949
950 static unsigned int read_2_bytes (bfd *, gdb_byte *);
951
952 static unsigned int read_4_bytes (bfd *, gdb_byte *);
953
954 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
955
956 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
957                                unsigned int *);
958
959 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961 static LONGEST read_checked_initial_length_and_offset
962   (bfd *, gdb_byte *, const struct comp_unit_head *,
963    unsigned int *, unsigned int *);
964
965 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
966                             unsigned int *);
967
968 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
969
970 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
971
972 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
973
974 static char *read_indirect_string (bfd *, gdb_byte *,
975                                    const struct comp_unit_head *,
976                                    unsigned int *);
977
978 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
979
980 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
981
982 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
983
984 static void set_cu_language (unsigned int, struct dwarf2_cu *);
985
986 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987                                       struct dwarf2_cu *);
988
989 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990                                                 unsigned int,
991                                                 struct dwarf2_cu *);
992
993 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994                                struct dwarf2_cu *cu);
995
996 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
997
998 static struct die_info *die_specification (struct die_info *die,
999                                            struct dwarf2_cu **);
1000
1001 static void free_line_header (struct line_header *lh);
1002
1003 static void add_file_name (struct line_header *, char *, unsigned int,
1004                            unsigned int, unsigned int);
1005
1006 static struct line_header *(dwarf_decode_line_header
1007                             (unsigned int offset,
1008                              bfd *abfd, struct dwarf2_cu *cu));
1009
1010 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1011                                 struct dwarf2_cu *, struct partial_symtab *);
1012
1013 static void dwarf2_start_subfile (char *, const char *, const char *);
1014
1015 static struct symbol *new_symbol (struct die_info *, struct type *,
1016                                   struct dwarf2_cu *);
1017
1018 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019                                        struct dwarf2_cu *, struct symbol *);
1020
1021 static void dwarf2_const_value (struct attribute *, struct symbol *,
1022                                 struct dwarf2_cu *);
1023
1024 static void dwarf2_const_value_attr (struct attribute *attr,
1025                                      struct type *type,
1026                                      const char *name,
1027                                      struct obstack *obstack,
1028                                      struct dwarf2_cu *cu, long *value,
1029                                      gdb_byte **bytes,
1030                                      struct dwarf2_locexpr_baton **baton);
1031
1032 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1033
1034 static int need_gnat_info (struct dwarf2_cu *);
1035
1036 static struct type *die_descriptive_type (struct die_info *,
1037                                           struct dwarf2_cu *);
1038
1039 static void set_descriptive_type (struct type *, struct die_info *,
1040                                   struct dwarf2_cu *);
1041
1042 static struct type *die_containing_type (struct die_info *,
1043                                          struct dwarf2_cu *);
1044
1045 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1046                                      struct dwarf2_cu *);
1047
1048 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1049
1050 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1051
1052 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1053
1054 static char *typename_concat (struct obstack *obs, const char *prefix,
1055                               const char *suffix, int physname,
1056                               struct dwarf2_cu *cu);
1057
1058 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1059
1060 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1061
1062 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1065
1066 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1067                                struct dwarf2_cu *, struct partial_symtab *);
1068
1069 static int dwarf2_get_pc_bounds (struct die_info *,
1070                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1071                                  struct partial_symtab *);
1072
1073 static void get_scope_pc_bounds (struct die_info *,
1074                                  CORE_ADDR *, CORE_ADDR *,
1075                                  struct dwarf2_cu *);
1076
1077 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1078                                         CORE_ADDR, struct dwarf2_cu *);
1079
1080 static void dwarf2_add_field (struct field_info *, struct die_info *,
1081                               struct dwarf2_cu *);
1082
1083 static void dwarf2_attach_fields_to_type (struct field_info *,
1084                                           struct type *, struct dwarf2_cu *);
1085
1086 static void dwarf2_add_member_fn (struct field_info *,
1087                                   struct die_info *, struct type *,
1088                                   struct dwarf2_cu *);
1089
1090 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1091                                              struct type *,
1092                                              struct dwarf2_cu *);
1093
1094 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1095
1096 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1097
1098 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1099
1100 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1101
1102 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1103
1104 static struct type *read_module_type (struct die_info *die,
1105                                       struct dwarf2_cu *cu);
1106
1107 static const char *namespace_name (struct die_info *die,
1108                                    int *is_anonymous, struct dwarf2_cu *);
1109
1110 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1111
1112 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1113
1114 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1115                                                        struct dwarf2_cu *);
1116
1117 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1118
1119 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1120                                                  gdb_byte *info_ptr,
1121                                                  gdb_byte **new_info_ptr,
1122                                                  struct die_info *parent);
1123
1124 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1125                                                gdb_byte *info_ptr,
1126                                                gdb_byte **new_info_ptr,
1127                                                struct die_info *parent);
1128
1129 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1130                                                gdb_byte *info_ptr,
1131                                                gdb_byte **new_info_ptr,
1132                                                struct die_info *parent);
1133
1134 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1135                                 struct die_info **, gdb_byte *,
1136                                 int *);
1137
1138 static void process_die (struct die_info *, struct dwarf2_cu *);
1139
1140 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1141                                        struct obstack *);
1142
1143 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1144
1145 static const char *dwarf2_full_name (char *name,
1146                                      struct die_info *die,
1147                                      struct dwarf2_cu *cu);
1148
1149 static struct die_info *dwarf2_extension (struct die_info *die,
1150                                           struct dwarf2_cu **);
1151
1152 static char *dwarf_tag_name (unsigned int);
1153
1154 static char *dwarf_attr_name (unsigned int);
1155
1156 static char *dwarf_form_name (unsigned int);
1157
1158 static char *dwarf_bool_name (unsigned int);
1159
1160 static char *dwarf_type_encoding_name (unsigned int);
1161
1162 #if 0
1163 static char *dwarf_cfi_name (unsigned int);
1164 #endif
1165
1166 static struct die_info *sibling_die (struct die_info *);
1167
1168 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1169
1170 static void dump_die_for_error (struct die_info *);
1171
1172 static void dump_die_1 (struct ui_file *, int level, int max_level,
1173                         struct die_info *);
1174
1175 /*static*/ void dump_die (struct die_info *, int max_level);
1176
1177 static void store_in_ref_table (struct die_info *,
1178                                 struct dwarf2_cu *);
1179
1180 static int is_ref_attr (struct attribute *);
1181
1182 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1183
1184 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1185
1186 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1187                                                struct attribute *,
1188                                                struct dwarf2_cu **);
1189
1190 static struct die_info *follow_die_ref (struct die_info *,
1191                                         struct attribute *,
1192                                         struct dwarf2_cu **);
1193
1194 static struct die_info *follow_die_sig (struct die_info *,
1195                                         struct attribute *,
1196                                         struct dwarf2_cu **);
1197
1198 static void read_signatured_type_at_offset (struct objfile *objfile,
1199                                             unsigned int offset);
1200
1201 static void read_signatured_type (struct objfile *,
1202                                   struct signatured_type *type_sig);
1203
1204 /* memory allocation interface */
1205
1206 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1207
1208 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1209
1210 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1211
1212 static void initialize_cu_func_list (struct dwarf2_cu *);
1213
1214 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1215                                  struct dwarf2_cu *);
1216
1217 static void dwarf_decode_macros (struct line_header *, unsigned int,
1218                                  char *, bfd *, struct dwarf2_cu *);
1219
1220 static int attr_form_is_block (struct attribute *);
1221
1222 static int attr_form_is_section_offset (struct attribute *);
1223
1224 static int attr_form_is_constant (struct attribute *);
1225
1226 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1227                                    struct dwarf2_loclist_baton *baton,
1228                                    struct attribute *attr);
1229
1230 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1231                                          struct symbol *sym,
1232                                          struct dwarf2_cu *cu);
1233
1234 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1235                                struct abbrev_info *abbrev,
1236                                struct dwarf2_cu *cu);
1237
1238 static void free_stack_comp_unit (void *);
1239
1240 static hashval_t partial_die_hash (const void *item);
1241
1242 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1243
1244 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1245   (unsigned int offset, struct objfile *objfile);
1246
1247 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1248   (unsigned int offset, struct objfile *objfile);
1249
1250 static void init_one_comp_unit (struct dwarf2_cu *cu,
1251                                 struct objfile *objfile);
1252
1253 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1254                                    struct die_info *comp_unit_die);
1255
1256 static void free_one_comp_unit (void *);
1257
1258 static void free_cached_comp_units (void *);
1259
1260 static void age_cached_comp_units (void);
1261
1262 static void free_one_cached_comp_unit (void *);
1263
1264 static struct type *set_die_type (struct die_info *, struct type *,
1265                                   struct dwarf2_cu *);
1266
1267 static void create_all_comp_units (struct objfile *);
1268
1269 static int create_debug_types_hash_table (struct objfile *objfile);
1270
1271 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1272                                  struct objfile *);
1273
1274 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1275
1276 static void dwarf2_add_dependence (struct dwarf2_cu *,
1277                                    struct dwarf2_per_cu_data *);
1278
1279 static void dwarf2_mark (struct dwarf2_cu *);
1280
1281 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1282
1283 static struct type *get_die_type_at_offset (unsigned int,
1284                                             struct dwarf2_per_cu_data *per_cu);
1285
1286 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1287
1288 static void dwarf2_release_queue (void *dummy);
1289
1290 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1291                              struct objfile *objfile);
1292
1293 static void process_queue (struct objfile *objfile);
1294
1295 static void find_file_and_directory (struct die_info *die,
1296                                      struct dwarf2_cu *cu,
1297                                      char **name, char **comp_dir);
1298
1299 static char *file_full_name (int file, struct line_header *lh,
1300                              const char *comp_dir);
1301
1302 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1303                                               gdb_byte *info_ptr,
1304                                               gdb_byte *buffer,
1305                                               unsigned int buffer_size,
1306                                               bfd *abfd);
1307
1308 static void init_cu_die_reader (struct die_reader_specs *reader,
1309                                 struct dwarf2_cu *cu);
1310
1311 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1312
1313 #if WORDS_BIGENDIAN
1314
1315 /* Convert VALUE between big- and little-endian.  */
1316 static offset_type
1317 byte_swap (offset_type value)
1318 {
1319   offset_type result;
1320
1321   result = (value & 0xff) << 24;
1322   result |= (value & 0xff00) << 8;
1323   result |= (value & 0xff0000) >> 8;
1324   result |= (value & 0xff000000) >> 24;
1325   return result;
1326 }
1327
1328 #define MAYBE_SWAP(V)  byte_swap (V)
1329
1330 #else
1331 #define MAYBE_SWAP(V) (V)
1332 #endif /* WORDS_BIGENDIAN */
1333
1334 /* The suffix for an index file.  */
1335 #define INDEX_SUFFIX ".gdb-index"
1336
1337 static const char *dwarf2_physname (char *name, struct die_info *die,
1338                                     struct dwarf2_cu *cu);
1339
1340 /* Try to locate the sections we need for DWARF 2 debugging
1341    information and return true if we have enough to do something.  */
1342
1343 int
1344 dwarf2_has_info (struct objfile *objfile)
1345 {
1346   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1347   if (!dwarf2_per_objfile)
1348     {
1349       /* Initialize per-objfile state.  */
1350       struct dwarf2_per_objfile *data
1351         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1352
1353       memset (data, 0, sizeof (*data));
1354       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1355       dwarf2_per_objfile = data;
1356
1357       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1358       dwarf2_per_objfile->objfile = objfile;
1359     }
1360   return (dwarf2_per_objfile->info.asection != NULL
1361           && dwarf2_per_objfile->abbrev.asection != NULL);
1362 }
1363
1364 /* When loading sections, we can either look for ".<name>", or for
1365  * ".z<name>", which indicates a compressed section.  */
1366
1367 static int
1368 section_is_p (const char *section_name, const char *name)
1369 {
1370   return (section_name[0] == '.'
1371           && (strcmp (section_name + 1, name) == 0
1372               || (section_name[1] == 'z'
1373                   && strcmp (section_name + 2, name) == 0)));
1374 }
1375
1376 /* This function is mapped across the sections and remembers the
1377    offset and size of each of the debugging sections we are interested
1378    in.  */
1379
1380 static void
1381 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1382 {
1383   if (section_is_p (sectp->name, INFO_SECTION))
1384     {
1385       dwarf2_per_objfile->info.asection = sectp;
1386       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1387     }
1388   else if (section_is_p (sectp->name, ABBREV_SECTION))
1389     {
1390       dwarf2_per_objfile->abbrev.asection = sectp;
1391       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1392     }
1393   else if (section_is_p (sectp->name, LINE_SECTION))
1394     {
1395       dwarf2_per_objfile->line.asection = sectp;
1396       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1397     }
1398   else if (section_is_p (sectp->name, LOC_SECTION))
1399     {
1400       dwarf2_per_objfile->loc.asection = sectp;
1401       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1402     }
1403   else if (section_is_p (sectp->name, MACINFO_SECTION))
1404     {
1405       dwarf2_per_objfile->macinfo.asection = sectp;
1406       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1407     }
1408   else if (section_is_p (sectp->name, STR_SECTION))
1409     {
1410       dwarf2_per_objfile->str.asection = sectp;
1411       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1412     }
1413   else if (section_is_p (sectp->name, FRAME_SECTION))
1414     {
1415       dwarf2_per_objfile->frame.asection = sectp;
1416       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1417     }
1418   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1419     {
1420       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1421
1422       if (aflag & SEC_HAS_CONTENTS)
1423         {
1424           dwarf2_per_objfile->eh_frame.asection = sectp;
1425           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1426         }
1427     }
1428   else if (section_is_p (sectp->name, RANGES_SECTION))
1429     {
1430       dwarf2_per_objfile->ranges.asection = sectp;
1431       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1432     }
1433   else if (section_is_p (sectp->name, TYPES_SECTION))
1434     {
1435       dwarf2_per_objfile->types.asection = sectp;
1436       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1437     }
1438   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1439     {
1440       dwarf2_per_objfile->gdb_index.asection = sectp;
1441       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1442     }
1443
1444   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1445       && bfd_section_vma (abfd, sectp) == 0)
1446     dwarf2_per_objfile->has_section_at_zero = 1;
1447 }
1448
1449 /* Decompress a section that was compressed using zlib.  Store the
1450    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1451
1452 static void
1453 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1454                          gdb_byte **outbuf, bfd_size_type *outsize)
1455 {
1456   bfd *abfd = objfile->obfd;
1457 #ifndef HAVE_ZLIB_H
1458   error (_("Support for zlib-compressed DWARF data (from '%s') "
1459            "is disabled in this copy of GDB"),
1460          bfd_get_filename (abfd));
1461 #else
1462   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1463   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1464   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1465   bfd_size_type uncompressed_size;
1466   gdb_byte *uncompressed_buffer;
1467   z_stream strm;
1468   int rc;
1469   int header_size = 12;
1470
1471   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1472       || bfd_bread (compressed_buffer,
1473                     compressed_size, abfd) != compressed_size)
1474     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1475            bfd_get_filename (abfd));
1476
1477   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1478      by the uncompressed section size, 8 bytes in big-endian order.  */
1479   if (compressed_size < header_size
1480       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1481     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1482            bfd_get_filename (abfd));
1483   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1484   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1485   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1486   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1487   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1488   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1489   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1490   uncompressed_size += compressed_buffer[11];
1491
1492   /* It is possible the section consists of several compressed
1493      buffers concatenated together, so we uncompress in a loop.  */
1494   strm.zalloc = NULL;
1495   strm.zfree = NULL;
1496   strm.opaque = NULL;
1497   strm.avail_in = compressed_size - header_size;
1498   strm.next_in = (Bytef*) compressed_buffer + header_size;
1499   strm.avail_out = uncompressed_size;
1500   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1501                                        uncompressed_size);
1502   rc = inflateInit (&strm);
1503   while (strm.avail_in > 0)
1504     {
1505       if (rc != Z_OK)
1506         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1507                bfd_get_filename (abfd), rc);
1508       strm.next_out = ((Bytef*) uncompressed_buffer
1509                        + (uncompressed_size - strm.avail_out));
1510       rc = inflate (&strm, Z_FINISH);
1511       if (rc != Z_STREAM_END)
1512         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1513                bfd_get_filename (abfd), rc);
1514       rc = inflateReset (&strm);
1515     }
1516   rc = inflateEnd (&strm);
1517   if (rc != Z_OK
1518       || strm.avail_out != 0)
1519     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1520            bfd_get_filename (abfd), rc);
1521
1522   do_cleanups (cleanup);
1523   *outbuf = uncompressed_buffer;
1524   *outsize = uncompressed_size;
1525 #endif
1526 }
1527
1528 /* A helper function that decides whether a section is empty.  */
1529
1530 static int
1531 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1532 {
1533   return info->asection == NULL || info->size == 0;
1534 }
1535
1536 /* Read the contents of the section SECTP from object file specified by
1537    OBJFILE, store info about the section into INFO.
1538    If the section is compressed, uncompress it before returning.  */
1539
1540 static void
1541 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1542 {
1543   bfd *abfd = objfile->obfd;
1544   asection *sectp = info->asection;
1545   gdb_byte *buf, *retbuf;
1546   unsigned char header[4];
1547
1548   if (info->readin)
1549     return;
1550   info->buffer = NULL;
1551   info->was_mmapped = 0;
1552   info->readin = 1;
1553
1554   if (dwarf2_section_empty_p (info))
1555     return;
1556
1557   /* Check if the file has a 4-byte header indicating compression.  */
1558   if (info->size > sizeof (header)
1559       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1560       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1561     {
1562       /* Upon decompression, update the buffer and its size.  */
1563       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1564         {
1565           zlib_decompress_section (objfile, sectp, &info->buffer,
1566                                    &info->size);
1567           return;
1568         }
1569     }
1570
1571 #ifdef HAVE_MMAP
1572   if (pagesize == 0)
1573     pagesize = getpagesize ();
1574
1575   /* Only try to mmap sections which are large enough: we don't want to
1576      waste space due to fragmentation.  Also, only try mmap for sections
1577      without relocations.  */
1578
1579   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1580     {
1581       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1582       size_t map_length = info->size + sectp->filepos - pg_offset;
1583       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1584                                  MAP_PRIVATE, pg_offset);
1585
1586       if (retbuf != MAP_FAILED)
1587         {
1588           info->was_mmapped = 1;
1589           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1590 #if HAVE_POSIX_MADVISE
1591           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1592 #endif
1593           return;
1594         }
1595     }
1596 #endif
1597
1598   /* If we get here, we are a normal, not-compressed section.  */
1599   info->buffer = buf
1600     = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602   /* When debugging .o files, we may need to apply relocations; see
1603      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604      We never compress sections in .o files, so we only need to
1605      try this when the section is not compressed.  */
1606   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1607   if (retbuf != NULL)
1608     {
1609       info->buffer = retbuf;
1610       return;
1611     }
1612
1613   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614       || bfd_bread (buf, info->size, abfd) != info->size)
1615     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616            bfd_get_filename (abfd));
1617 }
1618
1619 /* A helper function that returns the size of a section in a safe way.
1620    If you are positive that the section has been read before using the
1621    size, then it is safe to refer to the dwarf2_section_info object's
1622    "size" field directly.  In other cases, you must call this
1623    function, because for compressed sections the size field is not set
1624    correctly until the section has been read.  */
1625
1626 static bfd_size_type
1627 dwarf2_section_size (struct objfile *objfile,
1628                      struct dwarf2_section_info *info)
1629 {
1630   if (!info->readin)
1631     dwarf2_read_section (objfile, info);
1632   return info->size;
1633 }
1634
1635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1636    SECTION_NAME.  */
1637
1638 void
1639 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1640                          asection **sectp, gdb_byte **bufp,
1641                          bfd_size_type *sizep)
1642 {
1643   struct dwarf2_per_objfile *data
1644     = objfile_data (objfile, dwarf2_objfile_data_key);
1645   struct dwarf2_section_info *info;
1646
1647   /* We may see an objfile without any DWARF, in which case we just
1648      return nothing.  */
1649   if (data == NULL)
1650     {
1651       *sectp = NULL;
1652       *bufp = NULL;
1653       *sizep = 0;
1654       return;
1655     }
1656   if (section_is_p (section_name, EH_FRAME_SECTION))
1657     info = &data->eh_frame;
1658   else if (section_is_p (section_name, FRAME_SECTION))
1659     info = &data->frame;
1660   else
1661     gdb_assert_not_reached ("unexpected section");
1662
1663   dwarf2_read_section (objfile, info);
1664
1665   *sectp = info->asection;
1666   *bufp = info->buffer;
1667   *sizep = info->size;
1668 }
1669
1670 \f
1671 /* DWARF quick_symbols_functions support.  */
1672
1673 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1674    unique line tables, so we maintain a separate table of all .debug_line
1675    derived entries to support the sharing.
1676    All the quick functions need is the list of file names.  We discard the
1677    line_header when we're done and don't need to record it here.  */
1678 struct quick_file_names
1679 {
1680   /* The offset in .debug_line of the line table.  We hash on this.  */
1681   unsigned int offset;
1682
1683   /* The number of entries in file_names, real_names.  */
1684   unsigned int num_file_names;
1685
1686   /* The file names from the line table, after being run through
1687      file_full_name.  */
1688   const char **file_names;
1689
1690   /* The file names from the line table after being run through
1691      gdb_realpath.  These are computed lazily.  */
1692   const char **real_names;
1693 };
1694
1695 /* When using the index (and thus not using psymtabs), each CU has an
1696    object of this type.  This is used to hold information needed by
1697    the various "quick" methods.  */
1698 struct dwarf2_per_cu_quick_data
1699 {
1700   /* The file table.  This can be NULL if there was no file table
1701      or it's currently not read in.
1702      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1703   struct quick_file_names *file_names;
1704
1705   /* The corresponding symbol table.  This is NULL if symbols for this
1706      CU have not yet been read.  */
1707   struct symtab *symtab;
1708
1709   /* A temporary mark bit used when iterating over all CUs in
1710      expand_symtabs_matching.  */
1711   unsigned int mark : 1;
1712
1713   /* True if we've tried to read the file table and found there isn't one.
1714      There will be no point in trying to read it again next time.  */
1715   unsigned int no_file_data : 1;
1716 };
1717
1718 /* Hash function for a quick_file_names.  */
1719
1720 static hashval_t
1721 hash_file_name_entry (const void *e)
1722 {
1723   const struct quick_file_names *file_data = e;
1724
1725   return file_data->offset;
1726 }
1727
1728 /* Equality function for a quick_file_names.  */
1729
1730 static int
1731 eq_file_name_entry (const void *a, const void *b)
1732 {
1733   const struct quick_file_names *ea = a;
1734   const struct quick_file_names *eb = b;
1735
1736   return ea->offset == eb->offset;
1737 }
1738
1739 /* Delete function for a quick_file_names.  */
1740
1741 static void
1742 delete_file_name_entry (void *e)
1743 {
1744   struct quick_file_names *file_data = e;
1745   int i;
1746
1747   for (i = 0; i < file_data->num_file_names; ++i)
1748     {
1749       xfree ((void*) file_data->file_names[i]);
1750       if (file_data->real_names)
1751         xfree ((void*) file_data->real_names[i]);
1752     }
1753
1754   /* The space for the struct itself lives on objfile_obstack,
1755      so we don't free it here.  */
1756 }
1757
1758 /* Create a quick_file_names hash table.  */
1759
1760 static htab_t
1761 create_quick_file_names_table (unsigned int nr_initial_entries)
1762 {
1763   return htab_create_alloc (nr_initial_entries,
1764                             hash_file_name_entry, eq_file_name_entry,
1765                             delete_file_name_entry, xcalloc, xfree);
1766 }
1767
1768 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1769    this CU came.  */
1770
1771 static void
1772 dw2_do_instantiate_symtab (struct objfile *objfile,
1773                            struct dwarf2_per_cu_data *per_cu)
1774 {
1775   struct cleanup *back_to;
1776
1777   back_to = make_cleanup (dwarf2_release_queue, NULL);
1778
1779   queue_comp_unit (per_cu, objfile);
1780
1781   if (per_cu->from_debug_types)
1782     read_signatured_type_at_offset (objfile, per_cu->offset);
1783   else
1784     load_full_comp_unit (per_cu, objfile);
1785
1786   process_queue (objfile);
1787
1788   /* Age the cache, releasing compilation units that have not
1789      been used recently.  */
1790   age_cached_comp_units ();
1791
1792   do_cleanups (back_to);
1793 }
1794
1795 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1796    the objfile from which this CU came.  Returns the resulting symbol
1797    table.  */
1798
1799 static struct symtab *
1800 dw2_instantiate_symtab (struct objfile *objfile,
1801                         struct dwarf2_per_cu_data *per_cu)
1802 {
1803   if (!per_cu->v.quick->symtab)
1804     {
1805       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1806       increment_reading_symtab ();
1807       dw2_do_instantiate_symtab (objfile, per_cu);
1808       do_cleanups (back_to);
1809     }
1810   return per_cu->v.quick->symtab;
1811 }
1812
1813 /* Return the CU given its index.  */
1814
1815 static struct dwarf2_per_cu_data *
1816 dw2_get_cu (int index)
1817 {
1818   if (index >= dwarf2_per_objfile->n_comp_units)
1819     {
1820       index -= dwarf2_per_objfile->n_comp_units;
1821       return dwarf2_per_objfile->type_comp_units[index];
1822     }
1823   return dwarf2_per_objfile->all_comp_units[index];
1824 }
1825
1826 /* A helper function that knows how to read a 64-bit value in a way
1827    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1828    otherwise.  */
1829
1830 static int
1831 extract_cu_value (const char *bytes, ULONGEST *result)
1832 {
1833   if (sizeof (ULONGEST) < 8)
1834     {
1835       int i;
1836
1837       /* Ignore the upper 4 bytes if they are all zero.  */
1838       for (i = 0; i < 4; ++i)
1839         if (bytes[i + 4] != 0)
1840           return 0;
1841
1842       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1843     }
1844   else
1845     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1846   return 1;
1847 }
1848
1849 /* Read the CU list from the mapped index, and use it to create all
1850    the CU objects for this objfile.  Return 0 if something went wrong,
1851    1 if everything went ok.  */
1852
1853 static int
1854 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1855                        offset_type cu_list_elements)
1856 {
1857   offset_type i;
1858
1859   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1860   dwarf2_per_objfile->all_comp_units
1861     = obstack_alloc (&objfile->objfile_obstack,
1862                      dwarf2_per_objfile->n_comp_units
1863                      * sizeof (struct dwarf2_per_cu_data *));
1864
1865   for (i = 0; i < cu_list_elements; i += 2)
1866     {
1867       struct dwarf2_per_cu_data *the_cu;
1868       ULONGEST offset, length;
1869
1870       if (!extract_cu_value (cu_list, &offset)
1871           || !extract_cu_value (cu_list + 8, &length))
1872         return 0;
1873       cu_list += 2 * 8;
1874
1875       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1876                                struct dwarf2_per_cu_data);
1877       the_cu->offset = offset;
1878       the_cu->length = length;
1879       the_cu->objfile = objfile;
1880       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1881                                         struct dwarf2_per_cu_quick_data);
1882       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1883     }
1884
1885   return 1;
1886 }
1887
1888 /* Create the signatured type hash table from the index.  */
1889
1890 static int
1891 create_signatured_type_table_from_index (struct objfile *objfile,
1892                                          const gdb_byte *bytes,
1893                                          offset_type elements)
1894 {
1895   offset_type i;
1896   htab_t sig_types_hash;
1897
1898   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1899   dwarf2_per_objfile->type_comp_units
1900     = obstack_alloc (&objfile->objfile_obstack,
1901                      dwarf2_per_objfile->n_type_comp_units
1902                      * sizeof (struct dwarf2_per_cu_data *));
1903
1904   sig_types_hash = allocate_signatured_type_table (objfile);
1905
1906   for (i = 0; i < elements; i += 3)
1907     {
1908       struct signatured_type *type_sig;
1909       ULONGEST offset, type_offset, signature;
1910       void **slot;
1911
1912       if (!extract_cu_value (bytes, &offset)
1913           || !extract_cu_value (bytes + 8, &type_offset))
1914         return 0;
1915       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1916       bytes += 3 * 8;
1917
1918       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1919                                  struct signatured_type);
1920       type_sig->signature = signature;
1921       type_sig->offset = offset;
1922       type_sig->type_offset = type_offset;
1923       type_sig->per_cu.from_debug_types = 1;
1924       type_sig->per_cu.offset = offset;
1925       type_sig->per_cu.objfile = objfile;
1926       type_sig->per_cu.v.quick
1927         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1928                           struct dwarf2_per_cu_quick_data);
1929
1930       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1931       *slot = type_sig;
1932
1933       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1934     }
1935
1936   dwarf2_per_objfile->signatured_types = sig_types_hash;
1937
1938   return 1;
1939 }
1940
1941 /* Read the address map data from the mapped index, and use it to
1942    populate the objfile's psymtabs_addrmap.  */
1943
1944 static void
1945 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1946 {
1947   const gdb_byte *iter, *end;
1948   struct obstack temp_obstack;
1949   struct addrmap *mutable_map;
1950   struct cleanup *cleanup;
1951   CORE_ADDR baseaddr;
1952
1953   obstack_init (&temp_obstack);
1954   cleanup = make_cleanup_obstack_free (&temp_obstack);
1955   mutable_map = addrmap_create_mutable (&temp_obstack);
1956
1957   iter = index->address_table;
1958   end = iter + index->address_table_size;
1959
1960   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1961
1962   while (iter < end)
1963     {
1964       ULONGEST hi, lo, cu_index;
1965       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1966       iter += 8;
1967       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1968       iter += 8;
1969       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1970       iter += 4;
1971       
1972       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1973                          dw2_get_cu (cu_index));
1974     }
1975
1976   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1977                                                     &objfile->objfile_obstack);
1978   do_cleanups (cleanup);
1979 }
1980
1981 /* The hash function for strings in the mapped index.  This is the same as
1982    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1983    implementation.  This is necessary because the hash function is tied to the
1984    format of the mapped index file.  The hash values do not have to match with
1985    SYMBOL_HASH_NEXT.
1986    
1987    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
1988
1989 static hashval_t
1990 mapped_index_string_hash (int index_version, const void *p)
1991 {
1992   const unsigned char *str = (const unsigned char *) p;
1993   hashval_t r = 0;
1994   unsigned char c;
1995
1996   while ((c = *str++) != 0)
1997     {
1998       if (index_version >= 5)
1999         c = tolower (c);
2000       r = r * 67 + c - 113;
2001     }
2002
2003   return r;
2004 }
2005
2006 /* Find a slot in the mapped index INDEX for the object named NAME.
2007    If NAME is found, set *VEC_OUT to point to the CU vector in the
2008    constant pool and return 1.  If NAME cannot be found, return 0.  */
2009
2010 static int
2011 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2012                           offset_type **vec_out)
2013 {
2014   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2015   offset_type hash;
2016   offset_type slot, step;
2017   int (*cmp) (const char *, const char *);
2018
2019   if (current_language->la_language == language_cplus
2020       || current_language->la_language == language_java
2021       || current_language->la_language == language_fortran)
2022     {
2023       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2024          not contain any.  */
2025       const char *paren = strchr (name, '(');
2026
2027       if (paren)
2028         {
2029           char *dup;
2030
2031           dup = xmalloc (paren - name + 1);
2032           memcpy (dup, name, paren - name);
2033           dup[paren - name] = 0;
2034
2035           make_cleanup (xfree, dup);
2036           name = dup;
2037         }
2038     }
2039
2040   /* Index version 4 did not support case insensitive searches.  But the
2041      indexes for case insensitive languages are built in lowercase, therefore
2042      simulate our NAME being searched is also lowercased.  */
2043   hash = mapped_index_string_hash ((index->version == 4
2044                                     && case_sensitivity == case_sensitive_off
2045                                     ? 5 : index->version),
2046                                    name);
2047
2048   slot = hash & (index->symbol_table_slots - 1);
2049   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2050   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2051
2052   for (;;)
2053     {
2054       /* Convert a slot number to an offset into the table.  */
2055       offset_type i = 2 * slot;
2056       const char *str;
2057       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2058         {
2059           do_cleanups (back_to);
2060           return 0;
2061         }
2062
2063       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2064       if (!cmp (name, str))
2065         {
2066           *vec_out = (offset_type *) (index->constant_pool
2067                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2068           do_cleanups (back_to);
2069           return 1;
2070         }
2071
2072       slot = (slot + step) & (index->symbol_table_slots - 1);
2073     }
2074 }
2075
2076 /* Read the index file.  If everything went ok, initialize the "quick"
2077    elements of all the CUs and return 1.  Otherwise, return 0.  */
2078
2079 static int
2080 dwarf2_read_index (struct objfile *objfile)
2081 {
2082   char *addr;
2083   struct mapped_index *map;
2084   offset_type *metadata;
2085   const gdb_byte *cu_list;
2086   const gdb_byte *types_list = NULL;
2087   offset_type version, cu_list_elements;
2088   offset_type types_list_elements = 0;
2089   int i;
2090
2091   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2092     return 0;
2093
2094   /* Older elfutils strip versions could keep the section in the main
2095      executable while splitting it for the separate debug info file.  */
2096   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2097        & SEC_HAS_CONTENTS) == 0)
2098     return 0;
2099
2100   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2101
2102   addr = dwarf2_per_objfile->gdb_index.buffer;
2103   /* Version check.  */
2104   version = MAYBE_SWAP (*(offset_type *) addr);
2105   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2106      causes the index to behave very poorly for certain requests.  Version 3
2107      contained incomplete addrmap.  So, it seems better to just ignore such
2108      indices.  Index version 4 uses a different hash function than index
2109      version 5 and later.  */
2110   if (version < 4)
2111     return 0;
2112   /* Indexes with higher version than the one supported by GDB may be no
2113      longer backward compatible.  */
2114   if (version > 5)
2115     return 0;
2116
2117   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2118   map->version = version;
2119   map->total_size = dwarf2_per_objfile->gdb_index.size;
2120
2121   metadata = (offset_type *) (addr + sizeof (offset_type));
2122
2123   i = 0;
2124   cu_list = addr + MAYBE_SWAP (metadata[i]);
2125   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2126                       / 8);
2127   ++i;
2128
2129   types_list = addr + MAYBE_SWAP (metadata[i]);
2130   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2131                           - MAYBE_SWAP (metadata[i]))
2132                          / 8);
2133   ++i;
2134
2135   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2136   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2137                              - MAYBE_SWAP (metadata[i]));
2138   ++i;
2139
2140   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2141   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2142                               - MAYBE_SWAP (metadata[i]))
2143                              / (2 * sizeof (offset_type)));
2144   ++i;
2145
2146   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2147
2148   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2149     return 0;
2150
2151   if (types_list_elements
2152       && !create_signatured_type_table_from_index (objfile, types_list,
2153                                                    types_list_elements))
2154     return 0;
2155
2156   create_addrmap_from_index (objfile, map);
2157
2158   dwarf2_per_objfile->index_table = map;
2159   dwarf2_per_objfile->using_index = 1;
2160   dwarf2_per_objfile->quick_file_names_table =
2161     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2162
2163   return 1;
2164 }
2165
2166 /* A helper for the "quick" functions which sets the global
2167    dwarf2_per_objfile according to OBJFILE.  */
2168
2169 static void
2170 dw2_setup (struct objfile *objfile)
2171 {
2172   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2173   gdb_assert (dwarf2_per_objfile);
2174 }
2175
2176 /* A helper for the "quick" functions which attempts to read the line
2177    table for THIS_CU.  */
2178
2179 static struct quick_file_names *
2180 dw2_get_file_names (struct objfile *objfile,
2181                     struct dwarf2_per_cu_data *this_cu)
2182 {
2183   bfd *abfd = objfile->obfd;
2184   struct line_header *lh;
2185   struct attribute *attr;
2186   struct cleanup *cleanups;
2187   struct die_info *comp_unit_die;
2188   struct dwarf2_section_info* sec;
2189   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2190   int has_children, i;
2191   struct dwarf2_cu cu;
2192   unsigned int bytes_read, buffer_size;
2193   struct die_reader_specs reader_specs;
2194   char *name, *comp_dir;
2195   void **slot;
2196   struct quick_file_names *qfn;
2197   unsigned int line_offset;
2198
2199   if (this_cu->v.quick->file_names != NULL)
2200     return this_cu->v.quick->file_names;
2201   /* If we know there is no line data, no point in looking again.  */
2202   if (this_cu->v.quick->no_file_data)
2203     return NULL;
2204
2205   init_one_comp_unit (&cu, objfile);
2206   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2207
2208   if (this_cu->from_debug_types)
2209     sec = &dwarf2_per_objfile->types;
2210   else
2211     sec = &dwarf2_per_objfile->info;
2212   dwarf2_read_section (objfile, sec);
2213   buffer_size = sec->size;
2214   buffer = sec->buffer;
2215   info_ptr = buffer + this_cu->offset;
2216   beg_of_comp_unit = info_ptr;
2217
2218   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2219                                           buffer, buffer_size,
2220                                           abfd);
2221
2222   /* Complete the cu_header.  */
2223   cu.header.offset = beg_of_comp_unit - buffer;
2224   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2225
2226   this_cu->cu = &cu;
2227   cu.per_cu = this_cu;
2228
2229   dwarf2_read_abbrevs (abfd, &cu);
2230   make_cleanup (dwarf2_free_abbrev_table, &cu);
2231
2232   if (this_cu->from_debug_types)
2233     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2234   init_cu_die_reader (&reader_specs, &cu);
2235   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2236                  &has_children);
2237
2238   lh = NULL;
2239   slot = NULL;
2240   line_offset = 0;
2241   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2242   if (attr)
2243     {
2244       struct quick_file_names find_entry;
2245
2246       line_offset = DW_UNSND (attr);
2247
2248       /* We may have already read in this line header (TU line header sharing).
2249          If we have we're done.  */
2250       find_entry.offset = line_offset;
2251       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2252                              &find_entry, INSERT);
2253       if (*slot != NULL)
2254         {
2255           do_cleanups (cleanups);
2256           this_cu->v.quick->file_names = *slot;
2257           return *slot;
2258         }
2259
2260       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2261     }
2262   if (lh == NULL)
2263     {
2264       do_cleanups (cleanups);
2265       this_cu->v.quick->no_file_data = 1;
2266       return NULL;
2267     }
2268
2269   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2270   qfn->offset = line_offset;
2271   gdb_assert (slot != NULL);
2272   *slot = qfn;
2273
2274   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2275
2276   qfn->num_file_names = lh->num_file_names;
2277   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2278                                    lh->num_file_names * sizeof (char *));
2279   for (i = 0; i < lh->num_file_names; ++i)
2280     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2281   qfn->real_names = NULL;
2282
2283   free_line_header (lh);
2284   do_cleanups (cleanups);
2285
2286   this_cu->v.quick->file_names = qfn;
2287   return qfn;
2288 }
2289
2290 /* A helper for the "quick" functions which computes and caches the
2291    real path for a given file name from the line table.  */
2292
2293 static const char *
2294 dw2_get_real_path (struct objfile *objfile,
2295                    struct quick_file_names *qfn, int index)
2296 {
2297   if (qfn->real_names == NULL)
2298     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2299                                       qfn->num_file_names, sizeof (char *));
2300
2301   if (qfn->real_names[index] == NULL)
2302     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2303
2304   return qfn->real_names[index];
2305 }
2306
2307 static struct symtab *
2308 dw2_find_last_source_symtab (struct objfile *objfile)
2309 {
2310   int index;
2311
2312   dw2_setup (objfile);
2313   index = dwarf2_per_objfile->n_comp_units - 1;
2314   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2315 }
2316
2317 /* Traversal function for dw2_forget_cached_source_info.  */
2318
2319 static int
2320 dw2_free_cached_file_names (void **slot, void *info)
2321 {
2322   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2323
2324   if (file_data->real_names)
2325     {
2326       int i;
2327
2328       for (i = 0; i < file_data->num_file_names; ++i)
2329         {
2330           xfree ((void*) file_data->real_names[i]);
2331           file_data->real_names[i] = NULL;
2332         }
2333     }
2334
2335   return 1;
2336 }
2337
2338 static void
2339 dw2_forget_cached_source_info (struct objfile *objfile)
2340 {
2341   dw2_setup (objfile);
2342
2343   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2344                           dw2_free_cached_file_names, NULL);
2345 }
2346
2347 static int
2348 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2349                    const char *full_path, const char *real_path,
2350                    struct symtab **result)
2351 {
2352   int i;
2353   int check_basename = lbasename (name) == name;
2354   struct dwarf2_per_cu_data *base_cu = NULL;
2355
2356   dw2_setup (objfile);
2357
2358   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2359                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2360     {
2361       int j;
2362       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2363       struct quick_file_names *file_data;
2364
2365       if (per_cu->v.quick->symtab)
2366         continue;
2367
2368       file_data = dw2_get_file_names (objfile, per_cu);
2369       if (file_data == NULL)
2370         continue;
2371
2372       for (j = 0; j < file_data->num_file_names; ++j)
2373         {
2374           const char *this_name = file_data->file_names[j];
2375
2376           if (FILENAME_CMP (name, this_name) == 0)
2377             {
2378               *result = dw2_instantiate_symtab (objfile, per_cu);
2379               return 1;
2380             }
2381
2382           if (check_basename && ! base_cu
2383               && FILENAME_CMP (lbasename (this_name), name) == 0)
2384             base_cu = per_cu;
2385
2386           if (full_path != NULL)
2387             {
2388               const char *this_real_name = dw2_get_real_path (objfile,
2389                                                               file_data, j);
2390
2391               if (this_real_name != NULL
2392                   && FILENAME_CMP (full_path, this_real_name) == 0)
2393                 {
2394                   *result = dw2_instantiate_symtab (objfile, per_cu);
2395                   return 1;
2396                 }
2397             }
2398
2399           if (real_path != NULL)
2400             {
2401               const char *this_real_name = dw2_get_real_path (objfile,
2402                                                               file_data, j);
2403
2404               if (this_real_name != NULL
2405                   && FILENAME_CMP (real_path, this_real_name) == 0)
2406                 {
2407                   *result = dw2_instantiate_symtab (objfile, per_cu);
2408                   return 1;
2409                 }
2410             }
2411         }
2412     }
2413
2414   if (base_cu)
2415     {
2416       *result = dw2_instantiate_symtab (objfile, base_cu);
2417       return 1;
2418     }
2419
2420   return 0;
2421 }
2422
2423 static struct symtab *
2424 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2425                    const char *name, domain_enum domain)
2426 {
2427   /* We do all the work in the pre_expand_symtabs_matching hook
2428      instead.  */
2429   return NULL;
2430 }
2431
2432 /* A helper function that expands all symtabs that hold an object
2433    named NAME.  */
2434
2435 static void
2436 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2437 {
2438   dw2_setup (objfile);
2439
2440   /* index_table is NULL if OBJF_READNOW.  */
2441   if (dwarf2_per_objfile->index_table)
2442     {
2443       offset_type *vec;
2444
2445       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2446                                     name, &vec))
2447         {
2448           offset_type i, len = MAYBE_SWAP (*vec);
2449           for (i = 0; i < len; ++i)
2450             {
2451               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2452               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2453
2454               dw2_instantiate_symtab (objfile, per_cu);
2455             }
2456         }
2457     }
2458 }
2459
2460 static void
2461 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2462                                  enum block_enum block_kind, const char *name,
2463                                  domain_enum domain)
2464 {
2465   dw2_do_expand_symtabs_matching (objfile, name);
2466 }
2467
2468 static void
2469 dw2_print_stats (struct objfile *objfile)
2470 {
2471   int i, count;
2472
2473   dw2_setup (objfile);
2474   count = 0;
2475   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2476                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2477     {
2478       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2479
2480       if (!per_cu->v.quick->symtab)
2481         ++count;
2482     }
2483   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2484 }
2485
2486 static void
2487 dw2_dump (struct objfile *objfile)
2488 {
2489   /* Nothing worth printing.  */
2490 }
2491
2492 static void
2493 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2494               struct section_offsets *delta)
2495 {
2496   /* There's nothing to relocate here.  */
2497 }
2498
2499 static void
2500 dw2_expand_symtabs_for_function (struct objfile *objfile,
2501                                  const char *func_name)
2502 {
2503   dw2_do_expand_symtabs_matching (objfile, func_name);
2504 }
2505
2506 static void
2507 dw2_expand_all_symtabs (struct objfile *objfile)
2508 {
2509   int i;
2510
2511   dw2_setup (objfile);
2512
2513   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2514                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2515     {
2516       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2517
2518       dw2_instantiate_symtab (objfile, per_cu);
2519     }
2520 }
2521
2522 static void
2523 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2524                                   const char *filename)
2525 {
2526   int i;
2527
2528   dw2_setup (objfile);
2529
2530   /* We don't need to consider type units here.
2531      This is only called for examining code, e.g. expand_line_sal.
2532      There can be an order of magnitude (or more) more type units
2533      than comp units, and we avoid them if we can.  */
2534
2535   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2536     {
2537       int j;
2538       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2539       struct quick_file_names *file_data;
2540
2541       if (per_cu->v.quick->symtab)
2542         continue;
2543
2544       file_data = dw2_get_file_names (objfile, per_cu);
2545       if (file_data == NULL)
2546         continue;
2547
2548       for (j = 0; j < file_data->num_file_names; ++j)
2549         {
2550           const char *this_name = file_data->file_names[j];
2551           if (FILENAME_CMP (this_name, filename) == 0)
2552             {
2553               dw2_instantiate_symtab (objfile, per_cu);
2554               break;
2555             }
2556         }
2557     }
2558 }
2559
2560 static const char *
2561 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2562 {
2563   struct dwarf2_per_cu_data *per_cu;
2564   offset_type *vec;
2565   struct quick_file_names *file_data;
2566
2567   dw2_setup (objfile);
2568
2569   /* index_table is NULL if OBJF_READNOW.  */
2570   if (!dwarf2_per_objfile->index_table)
2571     return NULL;
2572
2573   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2574                                  name, &vec))
2575     return NULL;
2576
2577   /* Note that this just looks at the very first one named NAME -- but
2578      actually we are looking for a function.  find_main_filename
2579      should be rewritten so that it doesn't require a custom hook.  It
2580      could just use the ordinary symbol tables.  */
2581   /* vec[0] is the length, which must always be >0.  */
2582   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2583
2584   file_data = dw2_get_file_names (objfile, per_cu);
2585   if (file_data == NULL)
2586     return NULL;
2587
2588   return file_data->file_names[file_data->num_file_names - 1];
2589 }
2590
2591 static void
2592 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2593                           struct objfile *objfile, int global,
2594                           int (*callback) (struct block *,
2595                                            struct symbol *, void *),
2596                           void *data, symbol_compare_ftype *match,
2597                           symbol_compare_ftype *ordered_compare)
2598 {
2599   /* Currently unimplemented; used for Ada.  The function can be called if the
2600      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2601      does not look for non-Ada symbols this function should just return.  */
2602 }
2603
2604 static void
2605 dw2_expand_symtabs_matching (struct objfile *objfile,
2606                              int (*file_matcher) (const char *, void *),
2607                              int (*name_matcher) (const char *, void *),
2608                              enum search_domain kind,
2609                              void *data)
2610 {
2611   int i;
2612   offset_type iter;
2613   struct mapped_index *index;
2614
2615   dw2_setup (objfile);
2616
2617   /* index_table is NULL if OBJF_READNOW.  */
2618   if (!dwarf2_per_objfile->index_table)
2619     return;
2620   index = dwarf2_per_objfile->index_table;
2621
2622   if (file_matcher != NULL)
2623     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2624                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2625       {
2626         int j;
2627         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2628         struct quick_file_names *file_data;
2629
2630         per_cu->v.quick->mark = 0;
2631         if (per_cu->v.quick->symtab)
2632           continue;
2633
2634         file_data = dw2_get_file_names (objfile, per_cu);
2635         if (file_data == NULL)
2636           continue;
2637
2638         for (j = 0; j < file_data->num_file_names; ++j)
2639           {
2640             if (file_matcher (file_data->file_names[j], data))
2641               {
2642                 per_cu->v.quick->mark = 1;
2643                 break;
2644               }
2645           }
2646       }
2647
2648   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2649     {
2650       offset_type idx = 2 * iter;
2651       const char *name;
2652       offset_type *vec, vec_len, vec_idx;
2653
2654       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2655         continue;
2656
2657       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2658
2659       if (! (*name_matcher) (name, data))
2660         continue;
2661
2662       /* The name was matched, now expand corresponding CUs that were
2663          marked.  */
2664       vec = (offset_type *) (index->constant_pool
2665                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2666       vec_len = MAYBE_SWAP (vec[0]);
2667       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2668         {
2669           struct dwarf2_per_cu_data *per_cu;
2670
2671           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2672           if (file_matcher == NULL || per_cu->v.quick->mark)
2673             dw2_instantiate_symtab (objfile, per_cu);
2674         }
2675     }
2676 }
2677
2678 static struct symtab *
2679 dw2_find_pc_sect_symtab (struct objfile *objfile,
2680                          struct minimal_symbol *msymbol,
2681                          CORE_ADDR pc,
2682                          struct obj_section *section,
2683                          int warn_if_readin)
2684 {
2685   struct dwarf2_per_cu_data *data;
2686
2687   dw2_setup (objfile);
2688
2689   if (!objfile->psymtabs_addrmap)
2690     return NULL;
2691
2692   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2693   if (!data)
2694     return NULL;
2695
2696   if (warn_if_readin && data->v.quick->symtab)
2697     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2698              paddress (get_objfile_arch (objfile), pc));
2699
2700   return dw2_instantiate_symtab (objfile, data);
2701 }
2702
2703 static void
2704 dw2_map_symbol_filenames (struct objfile *objfile,
2705                           void (*fun) (const char *, const char *, void *),
2706                           void *data)
2707 {
2708   int i;
2709
2710   dw2_setup (objfile);
2711
2712   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2713                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2714     {
2715       int j;
2716       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2717       struct quick_file_names *file_data;
2718
2719       if (per_cu->v.quick->symtab)
2720         continue;
2721
2722       file_data = dw2_get_file_names (objfile, per_cu);
2723       if (file_data == NULL)
2724         continue;
2725
2726       for (j = 0; j < file_data->num_file_names; ++j)
2727         {
2728           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2729                                                           j);
2730           (*fun) (file_data->file_names[j], this_real_name, data);
2731         }
2732     }
2733 }
2734
2735 static int
2736 dw2_has_symbols (struct objfile *objfile)
2737 {
2738   return 1;
2739 }
2740
2741 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2742 {
2743   dw2_has_symbols,
2744   dw2_find_last_source_symtab,
2745   dw2_forget_cached_source_info,
2746   dw2_lookup_symtab,
2747   dw2_lookup_symbol,
2748   dw2_pre_expand_symtabs_matching,
2749   dw2_print_stats,
2750   dw2_dump,
2751   dw2_relocate,
2752   dw2_expand_symtabs_for_function,
2753   dw2_expand_all_symtabs,
2754   dw2_expand_symtabs_with_filename,
2755   dw2_find_symbol_file,
2756   dw2_map_matching_symbols,
2757   dw2_expand_symtabs_matching,
2758   dw2_find_pc_sect_symtab,
2759   dw2_map_symbol_filenames
2760 };
2761
2762 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2763    file will use psymtabs, or 1 if using the GNU index.  */
2764
2765 int
2766 dwarf2_initialize_objfile (struct objfile *objfile)
2767 {
2768   /* If we're about to read full symbols, don't bother with the
2769      indices.  In this case we also don't care if some other debug
2770      format is making psymtabs, because they are all about to be
2771      expanded anyway.  */
2772   if ((objfile->flags & OBJF_READNOW))
2773     {
2774       int i;
2775
2776       dwarf2_per_objfile->using_index = 1;
2777       create_all_comp_units (objfile);
2778       create_debug_types_hash_table (objfile);
2779       dwarf2_per_objfile->quick_file_names_table =
2780         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2781
2782       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2783                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2784         {
2785           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2786
2787           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2788                                             struct dwarf2_per_cu_quick_data);
2789         }
2790
2791       /* Return 1 so that gdb sees the "quick" functions.  However,
2792          these functions will be no-ops because we will have expanded
2793          all symtabs.  */
2794       return 1;
2795     }
2796
2797   if (dwarf2_read_index (objfile))
2798     return 1;
2799
2800   return 0;
2801 }
2802
2803 \f
2804
2805 /* Build a partial symbol table.  */
2806
2807 void
2808 dwarf2_build_psymtabs (struct objfile *objfile)
2809 {
2810   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2811     {
2812       init_psymbol_list (objfile, 1024);
2813     }
2814
2815   dwarf2_build_psymtabs_hard (objfile);
2816 }
2817
2818 /* Return TRUE if OFFSET is within CU_HEADER.  */
2819
2820 static inline int
2821 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2822 {
2823   unsigned int bottom = cu_header->offset;
2824   unsigned int top = (cu_header->offset
2825                       + cu_header->length
2826                       + cu_header->initial_length_size);
2827
2828   return (offset >= bottom && offset < top);
2829 }
2830
2831 /* Read in the comp unit header information from the debug_info at info_ptr.
2832    NOTE: This leaves members offset, first_die_offset to be filled in
2833    by the caller.  */
2834
2835 static gdb_byte *
2836 read_comp_unit_head (struct comp_unit_head *cu_header,
2837                      gdb_byte *info_ptr, bfd *abfd)
2838 {
2839   int signed_addr;
2840   unsigned int bytes_read;
2841
2842   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2843   cu_header->initial_length_size = bytes_read;
2844   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2845   info_ptr += bytes_read;
2846   cu_header->version = read_2_bytes (abfd, info_ptr);
2847   info_ptr += 2;
2848   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2849                                           &bytes_read);
2850   info_ptr += bytes_read;
2851   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2852   info_ptr += 1;
2853   signed_addr = bfd_get_sign_extend_vma (abfd);
2854   if (signed_addr < 0)
2855     internal_error (__FILE__, __LINE__,
2856                     _("read_comp_unit_head: dwarf from non elf file"));
2857   cu_header->signed_addr_p = signed_addr;
2858
2859   return info_ptr;
2860 }
2861
2862 static gdb_byte *
2863 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2864                              gdb_byte *buffer, unsigned int buffer_size,
2865                              bfd *abfd)
2866 {
2867   gdb_byte *beg_of_comp_unit = info_ptr;
2868
2869   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2870
2871   if (header->version != 2 && header->version != 3 && header->version != 4)
2872     error (_("Dwarf Error: wrong version in compilation unit header "
2873            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2874            bfd_get_filename (abfd));
2875
2876   if (header->abbrev_offset
2877       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2878                               &dwarf2_per_objfile->abbrev))
2879     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2880            "(offset 0x%lx + 6) [in module %s]"),
2881            (long) header->abbrev_offset,
2882            (long) (beg_of_comp_unit - buffer),
2883            bfd_get_filename (abfd));
2884
2885   if (beg_of_comp_unit + header->length + header->initial_length_size
2886       > buffer + buffer_size)
2887     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2888            "(offset 0x%lx + 0) [in module %s]"),
2889            (long) header->length,
2890            (long) (beg_of_comp_unit - buffer),
2891            bfd_get_filename (abfd));
2892
2893   return info_ptr;
2894 }
2895
2896 /* Read in the types comp unit header information from .debug_types entry at
2897    types_ptr.  The result is a pointer to one past the end of the header.  */
2898
2899 static gdb_byte *
2900 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2901                           ULONGEST *signature,
2902                           gdb_byte *types_ptr, bfd *abfd)
2903 {
2904   gdb_byte *initial_types_ptr = types_ptr;
2905
2906   dwarf2_read_section (dwarf2_per_objfile->objfile,
2907                        &dwarf2_per_objfile->types);
2908   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2909
2910   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2911
2912   *signature = read_8_bytes (abfd, types_ptr);
2913   types_ptr += 8;
2914   types_ptr += cu_header->offset_size;
2915   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2916
2917   return types_ptr;
2918 }
2919
2920 /* Allocate a new partial symtab for file named NAME and mark this new
2921    partial symtab as being an include of PST.  */
2922
2923 static void
2924 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2925                                struct objfile *objfile)
2926 {
2927   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2928
2929   subpst->section_offsets = pst->section_offsets;
2930   subpst->textlow = 0;
2931   subpst->texthigh = 0;
2932
2933   subpst->dependencies = (struct partial_symtab **)
2934     obstack_alloc (&objfile->objfile_obstack,
2935                    sizeof (struct partial_symtab *));
2936   subpst->dependencies[0] = pst;
2937   subpst->number_of_dependencies = 1;
2938
2939   subpst->globals_offset = 0;
2940   subpst->n_global_syms = 0;
2941   subpst->statics_offset = 0;
2942   subpst->n_static_syms = 0;
2943   subpst->symtab = NULL;
2944   subpst->read_symtab = pst->read_symtab;
2945   subpst->readin = 0;
2946
2947   /* No private part is necessary for include psymtabs.  This property
2948      can be used to differentiate between such include psymtabs and
2949      the regular ones.  */
2950   subpst->read_symtab_private = NULL;
2951 }
2952
2953 /* Read the Line Number Program data and extract the list of files
2954    included by the source file represented by PST.  Build an include
2955    partial symtab for each of these included files.  */
2956
2957 static void
2958 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2959                                struct die_info *die,
2960                                struct partial_symtab *pst)
2961 {
2962   struct objfile *objfile = cu->objfile;
2963   bfd *abfd = objfile->obfd;
2964   struct line_header *lh = NULL;
2965   struct attribute *attr;
2966
2967   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2968   if (attr)
2969     {
2970       unsigned int line_offset = DW_UNSND (attr);
2971
2972       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2973     }
2974   if (lh == NULL)
2975     return;  /* No linetable, so no includes.  */
2976
2977   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2978   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2979
2980   free_line_header (lh);
2981 }
2982
2983 static hashval_t
2984 hash_type_signature (const void *item)
2985 {
2986   const struct signatured_type *type_sig = item;
2987
2988   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2989   return type_sig->signature;
2990 }
2991
2992 static int
2993 eq_type_signature (const void *item_lhs, const void *item_rhs)
2994 {
2995   const struct signatured_type *lhs = item_lhs;
2996   const struct signatured_type *rhs = item_rhs;
2997
2998   return lhs->signature == rhs->signature;
2999 }
3000
3001 /* Allocate a hash table for signatured types.  */
3002
3003 static htab_t
3004 allocate_signatured_type_table (struct objfile *objfile)
3005 {
3006   return htab_create_alloc_ex (41,
3007                                hash_type_signature,
3008                                eq_type_signature,
3009                                NULL,
3010                                &objfile->objfile_obstack,
3011                                hashtab_obstack_allocate,
3012                                dummy_obstack_deallocate);
3013 }
3014
3015 /* A helper function to add a signatured type CU to a list.  */
3016
3017 static int
3018 add_signatured_type_cu_to_list (void **slot, void *datum)
3019 {
3020   struct signatured_type *sigt = *slot;
3021   struct dwarf2_per_cu_data ***datap = datum;
3022
3023   **datap = &sigt->per_cu;
3024   ++*datap;
3025
3026   return 1;
3027 }
3028
3029 /* Create the hash table of all entries in the .debug_types section.
3030    The result is zero if there is an error (e.g. missing .debug_types section),
3031    otherwise non-zero.  */
3032
3033 static int
3034 create_debug_types_hash_table (struct objfile *objfile)
3035 {
3036   gdb_byte *info_ptr;
3037   htab_t types_htab;
3038   struct dwarf2_per_cu_data **iter;
3039
3040   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3041   info_ptr = dwarf2_per_objfile->types.buffer;
3042
3043   if (info_ptr == NULL)
3044     {
3045       dwarf2_per_objfile->signatured_types = NULL;
3046       return 0;
3047     }
3048
3049   types_htab = allocate_signatured_type_table (objfile);
3050
3051   if (dwarf2_die_debug)
3052     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3053
3054   while (info_ptr < dwarf2_per_objfile->types.buffer
3055          + dwarf2_per_objfile->types.size)
3056     {
3057       unsigned int offset;
3058       unsigned int offset_size;
3059       unsigned int type_offset;
3060       unsigned int length, initial_length_size;
3061       unsigned short version;
3062       ULONGEST signature;
3063       struct signatured_type *type_sig;
3064       void **slot;
3065       gdb_byte *ptr = info_ptr;
3066
3067       offset = ptr - dwarf2_per_objfile->types.buffer;
3068
3069       /* We need to read the type's signature in order to build the hash
3070          table, but we don't need to read anything else just yet.  */
3071
3072       /* Sanity check to ensure entire cu is present.  */
3073       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3074       if (ptr + length + initial_length_size
3075           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3076         {
3077           complaint (&symfile_complaints,
3078                      _("debug type entry runs off end "
3079                        "of `.debug_types' section, ignored"));
3080           break;
3081         }
3082
3083       offset_size = initial_length_size == 4 ? 4 : 8;
3084       ptr += initial_length_size;
3085       version = bfd_get_16 (objfile->obfd, ptr);
3086       ptr += 2;
3087       ptr += offset_size; /* abbrev offset */
3088       ptr += 1; /* address size */
3089       signature = bfd_get_64 (objfile->obfd, ptr);
3090       ptr += 8;
3091       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3092
3093       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3094       memset (type_sig, 0, sizeof (*type_sig));
3095       type_sig->signature = signature;
3096       type_sig->offset = offset;
3097       type_sig->type_offset = type_offset;
3098       type_sig->per_cu.objfile = objfile;
3099       type_sig->per_cu.from_debug_types = 1;
3100
3101       slot = htab_find_slot (types_htab, type_sig, INSERT);
3102       gdb_assert (slot != NULL);
3103       *slot = type_sig;
3104
3105       if (dwarf2_die_debug)
3106         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3107                             offset, phex (signature, sizeof (signature)));
3108
3109       info_ptr = info_ptr + initial_length_size + length;
3110     }
3111
3112   dwarf2_per_objfile->signatured_types = types_htab;
3113
3114   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3115   dwarf2_per_objfile->type_comp_units
3116     = obstack_alloc (&objfile->objfile_obstack,
3117                      dwarf2_per_objfile->n_type_comp_units
3118                      * sizeof (struct dwarf2_per_cu_data *));
3119   iter = &dwarf2_per_objfile->type_comp_units[0];
3120   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3121   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3122               == dwarf2_per_objfile->n_type_comp_units);
3123
3124   return 1;
3125 }
3126
3127 /* Lookup a signature based type.
3128    Returns NULL if SIG is not present in the table.  */
3129
3130 static struct signatured_type *
3131 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3132 {
3133   struct signatured_type find_entry, *entry;
3134
3135   if (dwarf2_per_objfile->signatured_types == NULL)
3136     {
3137       complaint (&symfile_complaints,
3138                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3139       return 0;
3140     }
3141
3142   find_entry.signature = sig;
3143   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3144   return entry;
3145 }
3146
3147 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3148
3149 static void
3150 init_cu_die_reader (struct die_reader_specs *reader,
3151                     struct dwarf2_cu *cu)
3152 {
3153   reader->abfd = cu->objfile->obfd;
3154   reader->cu = cu;
3155   if (cu->per_cu->from_debug_types)
3156     {
3157       gdb_assert (dwarf2_per_objfile->types.readin);
3158       reader->buffer = dwarf2_per_objfile->types.buffer;
3159     }
3160   else
3161     {
3162       gdb_assert (dwarf2_per_objfile->info.readin);
3163       reader->buffer = dwarf2_per_objfile->info.buffer;
3164     }
3165 }
3166
3167 /* Find the base address of the compilation unit for range lists and
3168    location lists.  It will normally be specified by DW_AT_low_pc.
3169    In DWARF-3 draft 4, the base address could be overridden by
3170    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3171    compilation units with discontinuous ranges.  */
3172
3173 static void
3174 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3175 {
3176   struct attribute *attr;
3177
3178   cu->base_known = 0;
3179   cu->base_address = 0;
3180
3181   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3182   if (attr)
3183     {
3184       cu->base_address = DW_ADDR (attr);
3185       cu->base_known = 1;
3186     }
3187   else
3188     {
3189       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3190       if (attr)
3191         {
3192           cu->base_address = DW_ADDR (attr);
3193           cu->base_known = 1;
3194         }
3195     }
3196 }
3197
3198 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3199    to combine the common parts.
3200    Process a compilation unit for a psymtab.
3201    BUFFER is a pointer to the beginning of the dwarf section buffer,
3202    either .debug_info or debug_types.
3203    INFO_PTR is a pointer to the start of the CU.
3204    Returns a pointer to the next CU.  */
3205
3206 static gdb_byte *
3207 process_psymtab_comp_unit (struct objfile *objfile,
3208                            struct dwarf2_per_cu_data *this_cu,
3209                            gdb_byte *buffer, gdb_byte *info_ptr,
3210                            unsigned int buffer_size)
3211 {
3212   bfd *abfd = objfile->obfd;
3213   gdb_byte *beg_of_comp_unit = info_ptr;
3214   struct die_info *comp_unit_die;
3215   struct partial_symtab *pst;
3216   CORE_ADDR baseaddr;
3217   struct cleanup *back_to_inner;
3218   struct dwarf2_cu cu;
3219   int has_children, has_pc_info;
3220   struct attribute *attr;
3221   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3222   struct die_reader_specs reader_specs;
3223   const char *filename;
3224
3225   init_one_comp_unit (&cu, objfile);
3226   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3227
3228   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3229                                           buffer, buffer_size,
3230                                           abfd);
3231
3232   /* Complete the cu_header.  */
3233   cu.header.offset = beg_of_comp_unit - buffer;
3234   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3235
3236   cu.list_in_scope = &file_symbols;
3237
3238   /* If this compilation unit was already read in, free the
3239      cached copy in order to read it in again.  This is
3240      necessary because we skipped some symbols when we first
3241      read in the compilation unit (see load_partial_dies).
3242      This problem could be avoided, but the benefit is
3243      unclear.  */
3244   if (this_cu->cu != NULL)
3245     free_one_cached_comp_unit (this_cu->cu);
3246
3247   /* Note that this is a pointer to our stack frame, being
3248      added to a global data structure.  It will be cleaned up
3249      in free_stack_comp_unit when we finish with this
3250      compilation unit.  */
3251   this_cu->cu = &cu;
3252   cu.per_cu = this_cu;
3253
3254   /* Read the abbrevs for this compilation unit into a table.  */
3255   dwarf2_read_abbrevs (abfd, &cu);
3256   make_cleanup (dwarf2_free_abbrev_table, &cu);
3257
3258   /* Read the compilation unit die.  */
3259   if (this_cu->from_debug_types)
3260     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3261   init_cu_die_reader (&reader_specs, &cu);
3262   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3263                             &has_children);
3264
3265   if (this_cu->from_debug_types)
3266     {
3267       /* offset,length haven't been set yet for type units.  */
3268       this_cu->offset = cu.header.offset;
3269       this_cu->length = cu.header.length + cu.header.initial_length_size;
3270     }
3271   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3272     {
3273       info_ptr = (beg_of_comp_unit + cu.header.length
3274                   + cu.header.initial_length_size);
3275       do_cleanups (back_to_inner);
3276       return info_ptr;
3277     }
3278
3279   prepare_one_comp_unit (&cu, comp_unit_die);
3280
3281   /* Allocate a new partial symbol table structure.  */
3282   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3283   if (attr == NULL || !DW_STRING (attr))
3284     filename = "";
3285   else
3286     filename = DW_STRING (attr);
3287   pst = start_psymtab_common (objfile, objfile->section_offsets,
3288                               filename,
3289                               /* TEXTLOW and TEXTHIGH are set below.  */
3290                               0,
3291                               objfile->global_psymbols.next,
3292                               objfile->static_psymbols.next);
3293
3294   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3295   if (attr != NULL)
3296     pst->dirname = DW_STRING (attr);
3297
3298   pst->read_symtab_private = this_cu;
3299
3300   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3301
3302   /* Store the function that reads in the rest of the symbol table.  */
3303   pst->read_symtab = dwarf2_psymtab_to_symtab;
3304
3305   this_cu->v.psymtab = pst;
3306
3307   dwarf2_find_base_address (comp_unit_die, &cu);
3308
3309   /* Possibly set the default values of LOWPC and HIGHPC from
3310      `DW_AT_ranges'.  */
3311   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3312                                       &best_highpc, &cu, pst);
3313   if (has_pc_info == 1 && best_lowpc < best_highpc)
3314     /* Store the contiguous range if it is not empty; it can be empty for
3315        CUs with no code.  */
3316     addrmap_set_empty (objfile->psymtabs_addrmap,
3317                        best_lowpc + baseaddr,
3318                        best_highpc + baseaddr - 1, pst);
3319
3320   /* Check if comp unit has_children.
3321      If so, read the rest of the partial symbols from this comp unit.
3322      If not, there's no more debug_info for this comp unit.  */
3323   if (has_children)
3324     {
3325       struct partial_die_info *first_die;
3326       CORE_ADDR lowpc, highpc;
3327
3328       lowpc = ((CORE_ADDR) -1);
3329       highpc = ((CORE_ADDR) 0);
3330
3331       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3332
3333       scan_partial_symbols (first_die, &lowpc, &highpc,
3334                             ! has_pc_info, &cu);
3335
3336       /* If we didn't find a lowpc, set it to highpc to avoid
3337          complaints from `maint check'.  */
3338       if (lowpc == ((CORE_ADDR) -1))
3339         lowpc = highpc;
3340
3341       /* If the compilation unit didn't have an explicit address range,
3342          then use the information extracted from its child dies.  */
3343       if (! has_pc_info)
3344         {
3345           best_lowpc = lowpc;
3346           best_highpc = highpc;
3347         }
3348     }
3349   pst->textlow = best_lowpc + baseaddr;
3350   pst->texthigh = best_highpc + baseaddr;
3351
3352   pst->n_global_syms = objfile->global_psymbols.next -
3353     (objfile->global_psymbols.list + pst->globals_offset);
3354   pst->n_static_syms = objfile->static_psymbols.next -
3355     (objfile->static_psymbols.list + pst->statics_offset);
3356   sort_pst_symbols (pst);
3357
3358   info_ptr = (beg_of_comp_unit + cu.header.length
3359               + cu.header.initial_length_size);
3360
3361   if (this_cu->from_debug_types)
3362     {
3363       /* It's not clear we want to do anything with stmt lists here.
3364          Waiting to see what gcc ultimately does.  */
3365     }
3366   else
3367     {
3368       /* Get the list of files included in the current compilation unit,
3369          and build a psymtab for each of them.  */
3370       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3371     }
3372
3373   do_cleanups (back_to_inner);
3374
3375   return info_ptr;
3376 }
3377
3378 /* Traversal function for htab_traverse_noresize.
3379    Process one .debug_types comp-unit.  */
3380
3381 static int
3382 process_type_comp_unit (void **slot, void *info)
3383 {
3384   struct signatured_type *entry = (struct signatured_type *) *slot;
3385   struct objfile *objfile = (struct objfile *) info;
3386   struct dwarf2_per_cu_data *this_cu;
3387
3388   this_cu = &entry->per_cu;
3389
3390   gdb_assert (dwarf2_per_objfile->types.readin);
3391   process_psymtab_comp_unit (objfile, this_cu,
3392                              dwarf2_per_objfile->types.buffer,
3393                              dwarf2_per_objfile->types.buffer + entry->offset,
3394                              dwarf2_per_objfile->types.size);
3395
3396   return 1;
3397 }
3398
3399 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3400    Build partial symbol tables for the .debug_types comp-units.  */
3401
3402 static void
3403 build_type_psymtabs (struct objfile *objfile)
3404 {
3405   if (! create_debug_types_hash_table (objfile))
3406     return;
3407
3408   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3409                           process_type_comp_unit, objfile);
3410 }
3411
3412 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3413
3414 static void
3415 psymtabs_addrmap_cleanup (void *o)
3416 {
3417   struct objfile *objfile = o;
3418
3419   objfile->psymtabs_addrmap = NULL;
3420 }
3421
3422 /* Build the partial symbol table by doing a quick pass through the
3423    .debug_info and .debug_abbrev sections.  */
3424
3425 static void
3426 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3427 {
3428   gdb_byte *info_ptr;
3429   struct cleanup *back_to, *addrmap_cleanup;
3430   struct obstack temp_obstack;
3431
3432   dwarf2_per_objfile->reading_partial_symbols = 1;
3433
3434   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3435   info_ptr = dwarf2_per_objfile->info.buffer;
3436
3437   /* Any cached compilation units will be linked by the per-objfile
3438      read_in_chain.  Make sure to free them when we're done.  */
3439   back_to = make_cleanup (free_cached_comp_units, NULL);
3440
3441   build_type_psymtabs (objfile);
3442
3443   create_all_comp_units (objfile);
3444
3445   /* Create a temporary address map on a temporary obstack.  We later
3446      copy this to the final obstack.  */
3447   obstack_init (&temp_obstack);
3448   make_cleanup_obstack_free (&temp_obstack);
3449   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3450   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3451
3452   /* Since the objects we're extracting from .debug_info vary in
3453      length, only the individual functions to extract them (like
3454      read_comp_unit_head and load_partial_die) can really know whether
3455      the buffer is large enough to hold another complete object.
3456
3457      At the moment, they don't actually check that.  If .debug_info
3458      holds just one extra byte after the last compilation unit's dies,
3459      then read_comp_unit_head will happily read off the end of the
3460      buffer.  read_partial_die is similarly casual.  Those functions
3461      should be fixed.
3462
3463      For this loop condition, simply checking whether there's any data
3464      left at all should be sufficient.  */
3465
3466   while (info_ptr < (dwarf2_per_objfile->info.buffer
3467                      + dwarf2_per_objfile->info.size))
3468     {
3469       struct dwarf2_per_cu_data *this_cu;
3470
3471       this_cu = dwarf2_find_comp_unit (info_ptr
3472                                        - dwarf2_per_objfile->info.buffer,
3473                                        objfile);
3474
3475       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3476                                             dwarf2_per_objfile->info.buffer,
3477                                             info_ptr,
3478                                             dwarf2_per_objfile->info.size);
3479     }
3480
3481   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3482                                                     &objfile->objfile_obstack);
3483   discard_cleanups (addrmap_cleanup);
3484
3485   do_cleanups (back_to);
3486 }
3487
3488 /* Load the partial DIEs for a secondary CU into memory.  */
3489
3490 static void
3491 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3492                         struct objfile *objfile)
3493 {
3494   bfd *abfd = objfile->obfd;
3495   gdb_byte *info_ptr, *beg_of_comp_unit;
3496   struct die_info *comp_unit_die;
3497   struct dwarf2_cu *cu;
3498   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3499   int has_children;
3500   struct die_reader_specs reader_specs;
3501   int read_cu = 0;
3502
3503   gdb_assert (! this_cu->from_debug_types);
3504
3505   gdb_assert (dwarf2_per_objfile->info.readin);
3506   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3507   beg_of_comp_unit = info_ptr;
3508
3509   if (this_cu->cu == NULL)
3510     {
3511       cu = xmalloc (sizeof (*cu));
3512       init_one_comp_unit (cu, objfile);
3513
3514       read_cu = 1;
3515
3516       /* If an error occurs while loading, release our storage.  */
3517       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3518
3519       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3520                                               dwarf2_per_objfile->info.buffer,
3521                                               dwarf2_per_objfile->info.size,
3522                                               abfd);
3523
3524       /* Complete the cu_header.  */
3525       cu->header.offset = this_cu->offset;
3526       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3527
3528       /* Link this compilation unit into the compilation unit tree.  */
3529       this_cu->cu = cu;
3530       cu->per_cu = this_cu;
3531
3532       /* Link this CU into read_in_chain.  */
3533       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3534       dwarf2_per_objfile->read_in_chain = this_cu;
3535     }
3536   else
3537     {
3538       cu = this_cu->cu;
3539       info_ptr += cu->header.first_die_offset;
3540     }
3541
3542   /* Read the abbrevs for this compilation unit into a table.  */
3543   gdb_assert (cu->dwarf2_abbrevs == NULL);
3544   dwarf2_read_abbrevs (abfd, cu);
3545   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3546
3547   /* Read the compilation unit die.  */
3548   init_cu_die_reader (&reader_specs, cu);
3549   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3550                             &has_children);
3551
3552   prepare_one_comp_unit (cu, comp_unit_die);
3553
3554   /* Check if comp unit has_children.
3555      If so, read the rest of the partial symbols from this comp unit.
3556      If not, there's no more debug_info for this comp unit.  */
3557   if (has_children)
3558     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3559
3560   do_cleanups (free_abbrevs_cleanup);
3561
3562   if (read_cu)
3563     {
3564       /* We've successfully allocated this compilation unit.  Let our
3565          caller clean it up when finished with it.  */
3566       discard_cleanups (free_cu_cleanup);
3567     }
3568 }
3569
3570 /* Create a list of all compilation units in OBJFILE.  We do this only
3571    if an inter-comp-unit reference is found; presumably if there is one,
3572    there will be many, and one will occur early in the .debug_info section.
3573    So there's no point in building this list incrementally.  */
3574
3575 static void
3576 create_all_comp_units (struct objfile *objfile)
3577 {
3578   int n_allocated;
3579   int n_comp_units;
3580   struct dwarf2_per_cu_data **all_comp_units;
3581   gdb_byte *info_ptr;
3582
3583   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3584   info_ptr = dwarf2_per_objfile->info.buffer;
3585
3586   n_comp_units = 0;
3587   n_allocated = 10;
3588   all_comp_units = xmalloc (n_allocated
3589                             * sizeof (struct dwarf2_per_cu_data *));
3590
3591   while (info_ptr < dwarf2_per_objfile->info.buffer
3592          + dwarf2_per_objfile->info.size)
3593     {
3594       unsigned int length, initial_length_size;
3595       struct dwarf2_per_cu_data *this_cu;
3596       unsigned int offset;
3597
3598       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3599
3600       /* Read just enough information to find out where the next
3601          compilation unit is.  */
3602       length = read_initial_length (objfile->obfd, info_ptr,
3603                                     &initial_length_size);
3604
3605       /* Save the compilation unit for later lookup.  */
3606       this_cu = obstack_alloc (&objfile->objfile_obstack,
3607                                sizeof (struct dwarf2_per_cu_data));
3608       memset (this_cu, 0, sizeof (*this_cu));
3609       this_cu->offset = offset;
3610       this_cu->length = length + initial_length_size;
3611       this_cu->objfile = objfile;
3612
3613       if (n_comp_units == n_allocated)
3614         {
3615           n_allocated *= 2;
3616           all_comp_units = xrealloc (all_comp_units,
3617                                      n_allocated
3618                                      * sizeof (struct dwarf2_per_cu_data *));
3619         }
3620       all_comp_units[n_comp_units++] = this_cu;
3621
3622       info_ptr = info_ptr + this_cu->length;
3623     }
3624
3625   dwarf2_per_objfile->all_comp_units
3626     = obstack_alloc (&objfile->objfile_obstack,
3627                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3628   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3629           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3630   xfree (all_comp_units);
3631   dwarf2_per_objfile->n_comp_units = n_comp_units;
3632 }
3633
3634 /* Process all loaded DIEs for compilation unit CU, starting at
3635    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3636    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3637    DW_AT_ranges).  If NEED_PC is set, then this function will set
3638    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3639    and record the covered ranges in the addrmap.  */
3640
3641 static void
3642 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3643                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3644 {
3645   struct partial_die_info *pdi;
3646
3647   /* Now, march along the PDI's, descending into ones which have
3648      interesting children but skipping the children of the other ones,
3649      until we reach the end of the compilation unit.  */
3650
3651   pdi = first_die;
3652
3653   while (pdi != NULL)
3654     {
3655       fixup_partial_die (pdi, cu);
3656
3657       /* Anonymous namespaces or modules have no name but have interesting
3658          children, so we need to look at them.  Ditto for anonymous
3659          enums.  */
3660
3661       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3662           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3663         {
3664           switch (pdi->tag)
3665             {
3666             case DW_TAG_subprogram:
3667               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3668               break;
3669             case DW_TAG_constant:
3670             case DW_TAG_variable:
3671             case DW_TAG_typedef:
3672             case DW_TAG_union_type:
3673               if (!pdi->is_declaration)
3674                 {
3675                   add_partial_symbol (pdi, cu);
3676                 }
3677               break;
3678             case DW_TAG_class_type:
3679             case DW_TAG_interface_type:
3680             case DW_TAG_structure_type:
3681               if (!pdi->is_declaration)
3682                 {
3683                   add_partial_symbol (pdi, cu);
3684                 }
3685               break;
3686             case DW_TAG_enumeration_type:
3687               if (!pdi->is_declaration)
3688                 add_partial_enumeration (pdi, cu);
3689               break;
3690             case DW_TAG_base_type:
3691             case DW_TAG_subrange_type:
3692               /* File scope base type definitions are added to the partial
3693                  symbol table.  */
3694               add_partial_symbol (pdi, cu);
3695               break;
3696             case DW_TAG_namespace:
3697               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3698               break;
3699             case DW_TAG_module:
3700               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3701               break;
3702             default:
3703               break;
3704             }
3705         }
3706
3707       /* If the die has a sibling, skip to the sibling.  */
3708
3709       pdi = pdi->die_sibling;
3710     }
3711 }
3712
3713 /* Functions used to compute the fully scoped name of a partial DIE.
3714
3715    Normally, this is simple.  For C++, the parent DIE's fully scoped
3716    name is concatenated with "::" and the partial DIE's name.  For
3717    Java, the same thing occurs except that "." is used instead of "::".
3718    Enumerators are an exception; they use the scope of their parent
3719    enumeration type, i.e. the name of the enumeration type is not
3720    prepended to the enumerator.
3721
3722    There are two complexities.  One is DW_AT_specification; in this
3723    case "parent" means the parent of the target of the specification,
3724    instead of the direct parent of the DIE.  The other is compilers
3725    which do not emit DW_TAG_namespace; in this case we try to guess
3726    the fully qualified name of structure types from their members'
3727    linkage names.  This must be done using the DIE's children rather
3728    than the children of any DW_AT_specification target.  We only need
3729    to do this for structures at the top level, i.e. if the target of
3730    any DW_AT_specification (if any; otherwise the DIE itself) does not
3731    have a parent.  */
3732
3733 /* Compute the scope prefix associated with PDI's parent, in
3734    compilation unit CU.  The result will be allocated on CU's
3735    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3736    field.  NULL is returned if no prefix is necessary.  */
3737 static char *
3738 partial_die_parent_scope (struct partial_die_info *pdi,
3739                           struct dwarf2_cu *cu)
3740 {
3741   char *grandparent_scope;
3742   struct partial_die_info *parent, *real_pdi;
3743
3744   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3745      then this means the parent of the specification DIE.  */
3746
3747   real_pdi = pdi;
3748   while (real_pdi->has_specification)
3749     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3750
3751   parent = real_pdi->die_parent;
3752   if (parent == NULL)
3753     return NULL;
3754
3755   if (parent->scope_set)
3756     return parent->scope;
3757
3758   fixup_partial_die (parent, cu);
3759
3760   grandparent_scope = partial_die_parent_scope (parent, cu);
3761
3762   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3763      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3764      Work around this problem here.  */
3765   if (cu->language == language_cplus
3766       && parent->tag == DW_TAG_namespace
3767       && strcmp (parent->name, "::") == 0
3768       && grandparent_scope == NULL)
3769     {
3770       parent->scope = NULL;
3771       parent->scope_set = 1;
3772       return NULL;
3773     }
3774
3775   if (parent->tag == DW_TAG_namespace
3776       || parent->tag == DW_TAG_module
3777       || parent->tag == DW_TAG_structure_type
3778       || parent->tag == DW_TAG_class_type
3779       || parent->tag == DW_TAG_interface_type
3780       || parent->tag == DW_TAG_union_type
3781       || parent->tag == DW_TAG_enumeration_type)
3782     {
3783       if (grandparent_scope == NULL)
3784         parent->scope = parent->name;
3785       else
3786         parent->scope = typename_concat (&cu->comp_unit_obstack,
3787                                          grandparent_scope,
3788                                          parent->name, 0, cu);
3789     }
3790   else if (parent->tag == DW_TAG_enumerator)
3791     /* Enumerators should not get the name of the enumeration as a prefix.  */
3792     parent->scope = grandparent_scope;
3793   else
3794     {
3795       /* FIXME drow/2004-04-01: What should we be doing with
3796          function-local names?  For partial symbols, we should probably be
3797          ignoring them.  */
3798       complaint (&symfile_complaints,
3799                  _("unhandled containing DIE tag %d for DIE at %d"),
3800                  parent->tag, pdi->offset);
3801       parent->scope = grandparent_scope;
3802     }
3803
3804   parent->scope_set = 1;
3805   return parent->scope;
3806 }
3807
3808 /* Return the fully scoped name associated with PDI, from compilation unit
3809    CU.  The result will be allocated with malloc.  */
3810 static char *
3811 partial_die_full_name (struct partial_die_info *pdi,
3812                        struct dwarf2_cu *cu)
3813 {
3814   char *parent_scope;
3815
3816   /* If this is a template instantiation, we can not work out the
3817      template arguments from partial DIEs.  So, unfortunately, we have
3818      to go through the full DIEs.  At least any work we do building
3819      types here will be reused if full symbols are loaded later.  */
3820   if (pdi->has_template_arguments)
3821     {
3822       fixup_partial_die (pdi, cu);
3823
3824       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3825         {
3826           struct die_info *die;
3827           struct attribute attr;
3828           struct dwarf2_cu *ref_cu = cu;
3829
3830           attr.name = 0;
3831           attr.form = DW_FORM_ref_addr;
3832           attr.u.addr = pdi->offset;
3833           die = follow_die_ref (NULL, &attr, &ref_cu);
3834
3835           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3836         }
3837     }
3838
3839   parent_scope = partial_die_parent_scope (pdi, cu);
3840   if (parent_scope == NULL)
3841     return NULL;
3842   else
3843     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3844 }
3845
3846 static void
3847 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3848 {
3849   struct objfile *objfile = cu->objfile;
3850   CORE_ADDR addr = 0;
3851   char *actual_name = NULL;
3852   const struct partial_symbol *psym = NULL;
3853   CORE_ADDR baseaddr;
3854   int built_actual_name = 0;
3855
3856   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3857
3858   actual_name = partial_die_full_name (pdi, cu);
3859   if (actual_name)
3860     built_actual_name = 1;
3861
3862   if (actual_name == NULL)
3863     actual_name = pdi->name;
3864
3865   switch (pdi->tag)
3866     {
3867     case DW_TAG_subprogram:
3868       if (pdi->is_external || cu->language == language_ada)
3869         {
3870           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3871              of the global scope.  But in Ada, we want to be able to access
3872              nested procedures globally.  So all Ada subprograms are stored
3873              in the global scope.  */
3874           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3875              mst_text, objfile); */
3876           add_psymbol_to_list (actual_name, strlen (actual_name),
3877                                built_actual_name,
3878                                VAR_DOMAIN, LOC_BLOCK,
3879                                &objfile->global_psymbols,
3880                                0, pdi->lowpc + baseaddr,
3881                                cu->language, objfile);
3882         }
3883       else
3884         {
3885           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3886              mst_file_text, objfile); */
3887           add_psymbol_to_list (actual_name, strlen (actual_name),
3888                                built_actual_name,
3889                                VAR_DOMAIN, LOC_BLOCK,
3890                                &objfile->static_psymbols,
3891                                0, pdi->lowpc + baseaddr,
3892                                cu->language, objfile);
3893         }
3894       break;
3895     case DW_TAG_constant:
3896       {
3897         struct psymbol_allocation_list *list;
3898
3899         if (pdi->is_external)
3900           list = &objfile->global_psymbols;
3901         else
3902           list = &objfile->static_psymbols;
3903         add_psymbol_to_list (actual_name, strlen (actual_name),
3904                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3905                              list, 0, 0, cu->language, objfile);
3906       }
3907       break;
3908     case DW_TAG_variable:
3909       if (pdi->locdesc)
3910         addr = decode_locdesc (pdi->locdesc, cu);
3911
3912       if (pdi->locdesc
3913           && addr == 0
3914           && !dwarf2_per_objfile->has_section_at_zero)
3915         {
3916           /* A global or static variable may also have been stripped
3917              out by the linker if unused, in which case its address
3918              will be nullified; do not add such variables into partial
3919              symbol table then.  */
3920         }
3921       else if (pdi->is_external)
3922         {
3923           /* Global Variable.
3924              Don't enter into the minimal symbol tables as there is
3925              a minimal symbol table entry from the ELF symbols already.
3926              Enter into partial symbol table if it has a location
3927              descriptor or a type.
3928              If the location descriptor is missing, new_symbol will create
3929              a LOC_UNRESOLVED symbol, the address of the variable will then
3930              be determined from the minimal symbol table whenever the variable
3931              is referenced.
3932              The address for the partial symbol table entry is not
3933              used by GDB, but it comes in handy for debugging partial symbol
3934              table building.  */
3935
3936           if (pdi->locdesc || pdi->has_type)
3937             add_psymbol_to_list (actual_name, strlen (actual_name),
3938                                  built_actual_name,
3939                                  VAR_DOMAIN, LOC_STATIC,
3940                                  &objfile->global_psymbols,
3941                                  0, addr + baseaddr,
3942                                  cu->language, objfile);
3943         }
3944       else
3945         {
3946           /* Static Variable.  Skip symbols without location descriptors.  */
3947           if (pdi->locdesc == NULL)
3948             {
3949               if (built_actual_name)
3950                 xfree (actual_name);
3951               return;
3952             }
3953           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3954              mst_file_data, objfile); */
3955           add_psymbol_to_list (actual_name, strlen (actual_name),
3956                                built_actual_name,
3957                                VAR_DOMAIN, LOC_STATIC,
3958                                &objfile->static_psymbols,
3959                                0, addr + baseaddr,
3960                                cu->language, objfile);
3961         }
3962       break;
3963     case DW_TAG_typedef:
3964     case DW_TAG_base_type:
3965     case DW_TAG_subrange_type:
3966       add_psymbol_to_list (actual_name, strlen (actual_name),
3967                            built_actual_name,
3968                            VAR_DOMAIN, LOC_TYPEDEF,
3969                            &objfile->static_psymbols,
3970                            0, (CORE_ADDR) 0, cu->language, objfile);
3971       break;
3972     case DW_TAG_namespace:
3973       add_psymbol_to_list (actual_name, strlen (actual_name),
3974                            built_actual_name,
3975                            VAR_DOMAIN, LOC_TYPEDEF,
3976                            &objfile->global_psymbols,
3977                            0, (CORE_ADDR) 0, cu->language, objfile);
3978       break;
3979     case DW_TAG_class_type:
3980     case DW_TAG_interface_type:
3981     case DW_TAG_structure_type:
3982     case DW_TAG_union_type:
3983     case DW_TAG_enumeration_type:
3984       /* Skip external references.  The DWARF standard says in the section
3985          about "Structure, Union, and Class Type Entries": "An incomplete
3986          structure, union or class type is represented by a structure,
3987          union or class entry that does not have a byte size attribute
3988          and that has a DW_AT_declaration attribute."  */
3989       if (!pdi->has_byte_size && pdi->is_declaration)
3990         {
3991           if (built_actual_name)
3992             xfree (actual_name);
3993           return;
3994         }
3995
3996       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3997          static vs. global.  */
3998       add_psymbol_to_list (actual_name, strlen (actual_name),
3999                            built_actual_name,
4000                            STRUCT_DOMAIN, LOC_TYPEDEF,
4001                            (cu->language == language_cplus
4002                             || cu->language == language_java)
4003                            ? &objfile->global_psymbols
4004                            : &objfile->static_psymbols,
4005                            0, (CORE_ADDR) 0, cu->language, objfile);
4006
4007       break;
4008     case DW_TAG_enumerator:
4009       add_psymbol_to_list (actual_name, strlen (actual_name),
4010                            built_actual_name,
4011                            VAR_DOMAIN, LOC_CONST,
4012                            (cu->language == language_cplus
4013                             || cu->language == language_java)
4014                            ? &objfile->global_psymbols
4015                            : &objfile->static_psymbols,
4016                            0, (CORE_ADDR) 0, cu->language, objfile);
4017       break;
4018     default:
4019       break;
4020     }
4021
4022   if (built_actual_name)
4023     xfree (actual_name);
4024 }
4025
4026 /* Read a partial die corresponding to a namespace; also, add a symbol
4027    corresponding to that namespace to the symbol table.  NAMESPACE is
4028    the name of the enclosing namespace.  */
4029
4030 static void
4031 add_partial_namespace (struct partial_die_info *pdi,
4032                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4033                        int need_pc, struct dwarf2_cu *cu)
4034 {
4035   /* Add a symbol for the namespace.  */
4036
4037   add_partial_symbol (pdi, cu);
4038
4039   /* Now scan partial symbols in that namespace.  */
4040
4041   if (pdi->has_children)
4042     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4043 }
4044
4045 /* Read a partial die corresponding to a Fortran module.  */
4046
4047 static void
4048 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4049                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4050 {
4051   /* Now scan partial symbols in that module.  */
4052
4053   if (pdi->has_children)
4054     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4055 }
4056
4057 /* Read a partial die corresponding to a subprogram and create a partial
4058    symbol for that subprogram.  When the CU language allows it, this
4059    routine also defines a partial symbol for each nested subprogram
4060    that this subprogram contains.
4061
4062    DIE my also be a lexical block, in which case we simply search
4063    recursively for suprograms defined inside that lexical block.
4064    Again, this is only performed when the CU language allows this
4065    type of definitions.  */
4066
4067 static void
4068 add_partial_subprogram (struct partial_die_info *pdi,
4069                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4070                         int need_pc, struct dwarf2_cu *cu)
4071 {
4072   if (pdi->tag == DW_TAG_subprogram)
4073     {
4074       if (pdi->has_pc_info)
4075         {
4076           if (pdi->lowpc < *lowpc)
4077             *lowpc = pdi->lowpc;
4078           if (pdi->highpc > *highpc)
4079             *highpc = pdi->highpc;
4080           if (need_pc)
4081             {
4082               CORE_ADDR baseaddr;
4083               struct objfile *objfile = cu->objfile;
4084
4085               baseaddr = ANOFFSET (objfile->section_offsets,
4086                                    SECT_OFF_TEXT (objfile));
4087               addrmap_set_empty (objfile->psymtabs_addrmap,
4088                                  pdi->lowpc + baseaddr,
4089                                  pdi->highpc - 1 + baseaddr,
4090                                  cu->per_cu->v.psymtab);
4091             }
4092           if (!pdi->is_declaration)
4093             /* Ignore subprogram DIEs that do not have a name, they are
4094                illegal.  Do not emit a complaint at this point, we will
4095                do so when we convert this psymtab into a symtab.  */
4096             if (pdi->name)
4097               add_partial_symbol (pdi, cu);
4098         }
4099     }
4100
4101   if (! pdi->has_children)
4102     return;
4103
4104   if (cu->language == language_ada)
4105     {
4106       pdi = pdi->die_child;
4107       while (pdi != NULL)
4108         {
4109           fixup_partial_die (pdi, cu);
4110           if (pdi->tag == DW_TAG_subprogram
4111               || pdi->tag == DW_TAG_lexical_block)
4112             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4113           pdi = pdi->die_sibling;
4114         }
4115     }
4116 }
4117
4118 /* Read a partial die corresponding to an enumeration type.  */
4119
4120 static void
4121 add_partial_enumeration (struct partial_die_info *enum_pdi,
4122                          struct dwarf2_cu *cu)
4123 {
4124   struct partial_die_info *pdi;
4125
4126   if (enum_pdi->name != NULL)
4127     add_partial_symbol (enum_pdi, cu);
4128
4129   pdi = enum_pdi->die_child;
4130   while (pdi)
4131     {
4132       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4133         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4134       else
4135         add_partial_symbol (pdi, cu);
4136       pdi = pdi->die_sibling;
4137     }
4138 }
4139
4140 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4141    Return the corresponding abbrev, or NULL if the number is zero (indicating
4142    an empty DIE).  In either case *BYTES_READ will be set to the length of
4143    the initial number.  */
4144
4145 static struct abbrev_info *
4146 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4147                  struct dwarf2_cu *cu)
4148 {
4149   bfd *abfd = cu->objfile->obfd;
4150   unsigned int abbrev_number;
4151   struct abbrev_info *abbrev;
4152
4153   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4154
4155   if (abbrev_number == 0)
4156     return NULL;
4157
4158   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4159   if (!abbrev)
4160     {
4161       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4162              abbrev_number, bfd_get_filename (abfd));
4163     }
4164
4165   return abbrev;
4166 }
4167
4168 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4169    Returns a pointer to the end of a series of DIEs, terminated by an empty
4170    DIE.  Any children of the skipped DIEs will also be skipped.  */
4171
4172 static gdb_byte *
4173 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4174 {
4175   struct abbrev_info *abbrev;
4176   unsigned int bytes_read;
4177
4178   while (1)
4179     {
4180       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4181       if (abbrev == NULL)
4182         return info_ptr + bytes_read;
4183       else
4184         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4185     }
4186 }
4187
4188 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4189    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4190    abbrev corresponding to that skipped uleb128 should be passed in
4191    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4192    children.  */
4193
4194 static gdb_byte *
4195 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4196               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4197 {
4198   unsigned int bytes_read;
4199   struct attribute attr;
4200   bfd *abfd = cu->objfile->obfd;
4201   unsigned int form, i;
4202
4203   for (i = 0; i < abbrev->num_attrs; i++)
4204     {
4205       /* The only abbrev we care about is DW_AT_sibling.  */
4206       if (abbrev->attrs[i].name == DW_AT_sibling)
4207         {
4208           read_attribute (&attr, &abbrev->attrs[i],
4209                           abfd, info_ptr, cu);
4210           if (attr.form == DW_FORM_ref_addr)
4211             complaint (&symfile_complaints,
4212                        _("ignoring absolute DW_AT_sibling"));
4213           else
4214             return buffer + dwarf2_get_ref_die_offset (&attr);
4215         }
4216
4217       /* If it isn't DW_AT_sibling, skip this attribute.  */
4218       form = abbrev->attrs[i].form;
4219     skip_attribute:
4220       switch (form)
4221         {
4222         case DW_FORM_ref_addr:
4223           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4224              and later it is offset sized.  */
4225           if (cu->header.version == 2)
4226             info_ptr += cu->header.addr_size;
4227           else
4228             info_ptr += cu->header.offset_size;
4229           break;
4230         case DW_FORM_addr:
4231           info_ptr += cu->header.addr_size;
4232           break;
4233         case DW_FORM_data1:
4234         case DW_FORM_ref1:
4235         case DW_FORM_flag:
4236           info_ptr += 1;
4237           break;
4238         case DW_FORM_flag_present:
4239           break;
4240         case DW_FORM_data2:
4241         case DW_FORM_ref2:
4242           info_ptr += 2;
4243           break;
4244         case DW_FORM_data4:
4245         case DW_FORM_ref4:
4246           info_ptr += 4;
4247           break;
4248         case DW_FORM_data8:
4249         case DW_FORM_ref8:
4250         case DW_FORM_ref_sig8:
4251           info_ptr += 8;
4252           break;
4253         case DW_FORM_string:
4254           read_direct_string (abfd, info_ptr, &bytes_read);
4255           info_ptr += bytes_read;
4256           break;
4257         case DW_FORM_sec_offset:
4258         case DW_FORM_strp:
4259           info_ptr += cu->header.offset_size;
4260           break;
4261         case DW_FORM_exprloc:
4262         case DW_FORM_block:
4263           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4264           info_ptr += bytes_read;
4265           break;
4266         case DW_FORM_block1:
4267           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4268           break;
4269         case DW_FORM_block2:
4270           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4271           break;
4272         case DW_FORM_block4:
4273           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4274           break;
4275         case DW_FORM_sdata:
4276         case DW_FORM_udata:
4277         case DW_FORM_ref_udata:
4278           info_ptr = skip_leb128 (abfd, info_ptr);
4279           break;
4280         case DW_FORM_indirect:
4281           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4282           info_ptr += bytes_read;
4283           /* We need to continue parsing from here, so just go back to
4284              the top.  */
4285           goto skip_attribute;
4286
4287         default:
4288           error (_("Dwarf Error: Cannot handle %s "
4289                    "in DWARF reader [in module %s]"),
4290                  dwarf_form_name (form),
4291                  bfd_get_filename (abfd));
4292         }
4293     }
4294
4295   if (abbrev->has_children)
4296     return skip_children (buffer, info_ptr, cu);
4297   else
4298     return info_ptr;
4299 }
4300
4301 /* Locate ORIG_PDI's sibling.
4302    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4303    in BUFFER.  */
4304
4305 static gdb_byte *
4306 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4307                     gdb_byte *buffer, gdb_byte *info_ptr,
4308                     bfd *abfd, struct dwarf2_cu *cu)
4309 {
4310   /* Do we know the sibling already?  */
4311
4312   if (orig_pdi->sibling)
4313     return orig_pdi->sibling;
4314
4315   /* Are there any children to deal with?  */
4316
4317   if (!orig_pdi->has_children)
4318     return info_ptr;
4319
4320   /* Skip the children the long way.  */
4321
4322   return skip_children (buffer, info_ptr, cu);
4323 }
4324
4325 /* Expand this partial symbol table into a full symbol table.  */
4326
4327 static void
4328 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4329 {
4330   if (pst != NULL)
4331     {
4332       if (pst->readin)
4333         {
4334           warning (_("bug: psymtab for %s is already read in."),
4335                    pst->filename);
4336         }
4337       else
4338         {
4339           if (info_verbose)
4340             {
4341               printf_filtered (_("Reading in symbols for %s..."),
4342                                pst->filename);
4343               gdb_flush (gdb_stdout);
4344             }
4345
4346           /* Restore our global data.  */
4347           dwarf2_per_objfile = objfile_data (pst->objfile,
4348                                              dwarf2_objfile_data_key);
4349
4350           /* If this psymtab is constructed from a debug-only objfile, the
4351              has_section_at_zero flag will not necessarily be correct.  We
4352              can get the correct value for this flag by looking at the data
4353              associated with the (presumably stripped) associated objfile.  */
4354           if (pst->objfile->separate_debug_objfile_backlink)
4355             {
4356               struct dwarf2_per_objfile *dpo_backlink
4357                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4358                                 dwarf2_objfile_data_key);
4359
4360               dwarf2_per_objfile->has_section_at_zero
4361                 = dpo_backlink->has_section_at_zero;
4362             }
4363
4364           dwarf2_per_objfile->reading_partial_symbols = 0;
4365
4366           psymtab_to_symtab_1 (pst);
4367
4368           /* Finish up the debug error message.  */
4369           if (info_verbose)
4370             printf_filtered (_("done.\n"));
4371         }
4372     }
4373 }
4374
4375 /* Add PER_CU to the queue.  */
4376
4377 static void
4378 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4379 {
4380   struct dwarf2_queue_item *item;
4381
4382   per_cu->queued = 1;
4383   item = xmalloc (sizeof (*item));
4384   item->per_cu = per_cu;
4385   item->next = NULL;
4386
4387   if (dwarf2_queue == NULL)
4388     dwarf2_queue = item;
4389   else
4390     dwarf2_queue_tail->next = item;
4391
4392   dwarf2_queue_tail = item;
4393 }
4394
4395 /* Process the queue.  */
4396
4397 static void
4398 process_queue (struct objfile *objfile)
4399 {
4400   struct dwarf2_queue_item *item, *next_item;
4401
4402   /* The queue starts out with one item, but following a DIE reference
4403      may load a new CU, adding it to the end of the queue.  */
4404   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4405     {
4406       if (dwarf2_per_objfile->using_index
4407           ? !item->per_cu->v.quick->symtab
4408           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4409         process_full_comp_unit (item->per_cu);
4410
4411       item->per_cu->queued = 0;
4412       next_item = item->next;
4413       xfree (item);
4414     }
4415
4416   dwarf2_queue_tail = NULL;
4417 }
4418
4419 /* Free all allocated queue entries.  This function only releases anything if
4420    an error was thrown; if the queue was processed then it would have been
4421    freed as we went along.  */
4422
4423 static void
4424 dwarf2_release_queue (void *dummy)
4425 {
4426   struct dwarf2_queue_item *item, *last;
4427
4428   item = dwarf2_queue;
4429   while (item)
4430     {
4431       /* Anything still marked queued is likely to be in an
4432          inconsistent state, so discard it.  */
4433       if (item->per_cu->queued)
4434         {
4435           if (item->per_cu->cu != NULL)
4436             free_one_cached_comp_unit (item->per_cu->cu);
4437           item->per_cu->queued = 0;
4438         }
4439
4440       last = item;
4441       item = item->next;
4442       xfree (last);
4443     }
4444
4445   dwarf2_queue = dwarf2_queue_tail = NULL;
4446 }
4447
4448 /* Read in full symbols for PST, and anything it depends on.  */
4449
4450 static void
4451 psymtab_to_symtab_1 (struct partial_symtab *pst)
4452 {
4453   struct dwarf2_per_cu_data *per_cu;
4454   struct cleanup *back_to;
4455   int i;
4456
4457   for (i = 0; i < pst->number_of_dependencies; i++)
4458     if (!pst->dependencies[i]->readin)
4459       {
4460         /* Inform about additional files that need to be read in.  */
4461         if (info_verbose)
4462           {
4463             /* FIXME: i18n: Need to make this a single string.  */
4464             fputs_filtered (" ", gdb_stdout);
4465             wrap_here ("");
4466             fputs_filtered ("and ", gdb_stdout);
4467             wrap_here ("");
4468             printf_filtered ("%s...", pst->dependencies[i]->filename);
4469             wrap_here ("");     /* Flush output.  */
4470             gdb_flush (gdb_stdout);
4471           }
4472         psymtab_to_symtab_1 (pst->dependencies[i]);
4473       }
4474
4475   per_cu = pst->read_symtab_private;
4476
4477   if (per_cu == NULL)
4478     {
4479       /* It's an include file, no symbols to read for it.
4480          Everything is in the parent symtab.  */
4481       pst->readin = 1;
4482       return;
4483     }
4484
4485   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4486 }
4487
4488 /* Load the DIEs associated with PER_CU into memory.  */
4489
4490 static void
4491 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4492                      struct objfile *objfile)
4493 {
4494   bfd *abfd = objfile->obfd;
4495   struct dwarf2_cu *cu;
4496   unsigned int offset;
4497   gdb_byte *info_ptr, *beg_of_comp_unit;
4498   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4499   struct attribute *attr;
4500   int read_cu = 0;
4501
4502   gdb_assert (! per_cu->from_debug_types);
4503
4504   /* Set local variables from the partial symbol table info.  */
4505   offset = per_cu->offset;
4506
4507   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4508   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4509   beg_of_comp_unit = info_ptr;
4510
4511   if (per_cu->cu == NULL)
4512     {
4513       cu = xmalloc (sizeof (*cu));
4514       init_one_comp_unit (cu, objfile);
4515
4516       read_cu = 1;
4517
4518       /* If an error occurs while loading, release our storage.  */
4519       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4520
4521       /* Read in the comp_unit header.  */
4522       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4523
4524       /* Complete the cu_header.  */
4525       cu->header.offset = offset;
4526       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4527
4528       /* Read the abbrevs for this compilation unit.  */
4529       dwarf2_read_abbrevs (abfd, cu);
4530       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4531
4532       /* Link this compilation unit into the compilation unit tree.  */
4533       per_cu->cu = cu;
4534       cu->per_cu = per_cu;
4535
4536       /* Link this CU into read_in_chain.  */
4537       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4538       dwarf2_per_objfile->read_in_chain = per_cu;
4539     }
4540   else
4541     {
4542       cu = per_cu->cu;
4543       info_ptr += cu->header.first_die_offset;
4544     }
4545
4546   cu->dies = read_comp_unit (info_ptr, cu);
4547
4548   /* We try not to read any attributes in this function, because not
4549      all objfiles needed for references have been loaded yet, and symbol
4550      table processing isn't initialized.  But we have to set the CU language,
4551      or we won't be able to build types correctly.  */
4552   prepare_one_comp_unit (cu, cu->dies);
4553
4554   /* Similarly, if we do not read the producer, we can not apply
4555      producer-specific interpretation.  */
4556   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4557   if (attr)
4558     cu->producer = DW_STRING (attr);
4559
4560   if (read_cu)
4561     {
4562       do_cleanups (free_abbrevs_cleanup);
4563
4564       /* We've successfully allocated this compilation unit.  Let our
4565          caller clean it up when finished with it.  */
4566       discard_cleanups (free_cu_cleanup);
4567     }
4568 }
4569
4570 /* Add a DIE to the delayed physname list.  */
4571
4572 static void
4573 add_to_method_list (struct type *type, int fnfield_index, int index,
4574                     const char *name, struct die_info *die,
4575                     struct dwarf2_cu *cu)
4576 {
4577   struct delayed_method_info mi;
4578   mi.type = type;
4579   mi.fnfield_index = fnfield_index;
4580   mi.index = index;
4581   mi.name = name;
4582   mi.die = die;
4583   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4584 }
4585
4586 /* A cleanup for freeing the delayed method list.  */
4587
4588 static void
4589 free_delayed_list (void *ptr)
4590 {
4591   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4592   if (cu->method_list != NULL)
4593     {
4594       VEC_free (delayed_method_info, cu->method_list);
4595       cu->method_list = NULL;
4596     }
4597 }
4598
4599 /* Compute the physnames of any methods on the CU's method list.
4600
4601    The computation of method physnames is delayed in order to avoid the
4602    (bad) condition that one of the method's formal parameters is of an as yet
4603    incomplete type.  */
4604
4605 static void
4606 compute_delayed_physnames (struct dwarf2_cu *cu)
4607 {
4608   int i;
4609   struct delayed_method_info *mi;
4610   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4611     {
4612       const char *physname;
4613       struct fn_fieldlist *fn_flp
4614         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4615       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4616       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4617     }
4618 }
4619
4620 /* Check for GCC >= 4.0.  */
4621
4622 static int
4623 producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4624 {
4625   const char *cs;
4626   int major, minor;
4627
4628   if (cu->producer == NULL)
4629     {
4630       /* For unknown compilers expect their behavior is not compliant.  For GCC
4631          this case can also happen for -gdwarf-4 type units supported since
4632          gcc-4.5.  */
4633
4634       return 0;
4635     }
4636
4637   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
4638
4639   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4640     {
4641       /* For non-GCC compilers expect their behavior is not compliant.  */
4642
4643       return 0;
4644     }
4645   cs = &cu->producer[strlen ("GNU ")];
4646   while (*cs && !isdigit (*cs))
4647     cs++;
4648   if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4649     {
4650       /* Not recognized as GCC.  */
4651
4652       return 0;
4653     }
4654
4655   return major >= 4;
4656 }
4657
4658 /* Generate full symbol information for PST and CU, whose DIEs have
4659    already been loaded into memory.  */
4660
4661 static void
4662 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4663 {
4664   struct dwarf2_cu *cu = per_cu->cu;
4665   struct objfile *objfile = per_cu->objfile;
4666   CORE_ADDR lowpc, highpc;
4667   struct symtab *symtab;
4668   struct cleanup *back_to, *delayed_list_cleanup;
4669   CORE_ADDR baseaddr;
4670
4671   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4672
4673   buildsym_init ();
4674   back_to = make_cleanup (really_free_pendings, NULL);
4675   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4676
4677   cu->list_in_scope = &file_symbols;
4678
4679   dwarf2_find_base_address (cu->dies, cu);
4680
4681   /* Do line number decoding in read_file_scope () */
4682   process_die (cu->dies, cu);
4683
4684   /* Now that we have processed all the DIEs in the CU, all the types 
4685      should be complete, and it should now be safe to compute all of the
4686      physnames.  */
4687   compute_delayed_physnames (cu);
4688   do_cleanups (delayed_list_cleanup);
4689
4690   /* Some compilers don't define a DW_AT_high_pc attribute for the
4691      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4692      it, by scanning the DIE's below the compilation unit.  */
4693   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4694
4695   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4696
4697   if (symtab != NULL)
4698     {
4699       /* Set symtab language to language from DW_AT_language.  If the
4700          compilation is from a C file generated by language preprocessors, do
4701          not set the language if it was already deduced by start_subfile.  */
4702       if (!(cu->language == language_c && symtab->language != language_c))
4703         symtab->language = cu->language;
4704
4705       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4706          produce DW_AT_location with location lists but it can be possibly
4707          invalid without -fvar-tracking.
4708
4709          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4710          needed, it would be wrong due to missing DW_AT_producer there.
4711
4712          Still one can confuse GDB by using non-standard GCC compilation
4713          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4714          */ 
4715       if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4716         symtab->locations_valid = 1;
4717     }
4718
4719   if (dwarf2_per_objfile->using_index)
4720     per_cu->v.quick->symtab = symtab;
4721   else
4722     {
4723       struct partial_symtab *pst = per_cu->v.psymtab;
4724       pst->symtab = symtab;
4725       pst->readin = 1;
4726     }
4727
4728   do_cleanups (back_to);
4729 }
4730
4731 /* Process a die and its children.  */
4732
4733 static void
4734 process_die (struct die_info *die, struct dwarf2_cu *cu)
4735 {
4736   switch (die->tag)
4737     {
4738     case DW_TAG_padding:
4739       break;
4740     case DW_TAG_compile_unit:
4741       read_file_scope (die, cu);
4742       break;
4743     case DW_TAG_type_unit:
4744       read_type_unit_scope (die, cu);
4745       break;
4746     case DW_TAG_subprogram:
4747     case DW_TAG_inlined_subroutine:
4748       read_func_scope (die, cu);
4749       break;
4750     case DW_TAG_lexical_block:
4751     case DW_TAG_try_block:
4752     case DW_TAG_catch_block:
4753       read_lexical_block_scope (die, cu);
4754       break;
4755     case DW_TAG_class_type:
4756     case DW_TAG_interface_type:
4757     case DW_TAG_structure_type:
4758     case DW_TAG_union_type:
4759       process_structure_scope (die, cu);
4760       break;
4761     case DW_TAG_enumeration_type:
4762       process_enumeration_scope (die, cu);
4763       break;
4764
4765     /* These dies have a type, but processing them does not create
4766        a symbol or recurse to process the children.  Therefore we can
4767        read them on-demand through read_type_die.  */
4768     case DW_TAG_subroutine_type:
4769     case DW_TAG_set_type:
4770     case DW_TAG_array_type:
4771     case DW_TAG_pointer_type:
4772     case DW_TAG_ptr_to_member_type:
4773     case DW_TAG_reference_type:
4774     case DW_TAG_string_type:
4775       break;
4776
4777     case DW_TAG_base_type:
4778     case DW_TAG_subrange_type:
4779     case DW_TAG_typedef:
4780       /* Add a typedef symbol for the type definition, if it has a
4781          DW_AT_name.  */
4782       new_symbol (die, read_type_die (die, cu), cu);
4783       break;
4784     case DW_TAG_common_block:
4785       read_common_block (die, cu);
4786       break;
4787     case DW_TAG_common_inclusion:
4788       break;
4789     case DW_TAG_namespace:
4790       processing_has_namespace_info = 1;
4791       read_namespace (die, cu);
4792       break;
4793     case DW_TAG_module:
4794       processing_has_namespace_info = 1;
4795       read_module (die, cu);
4796       break;
4797     case DW_TAG_imported_declaration:
4798     case DW_TAG_imported_module:
4799       processing_has_namespace_info = 1;
4800       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4801                                  || cu->language != language_fortran))
4802         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4803                    dwarf_tag_name (die->tag));
4804       read_import_statement (die, cu);
4805       break;
4806     default:
4807       new_symbol (die, NULL, cu);
4808       break;
4809     }
4810 }
4811
4812 /* A helper function for dwarf2_compute_name which determines whether DIE
4813    needs to have the name of the scope prepended to the name listed in the
4814    die.  */
4815
4816 static int
4817 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4818 {
4819   struct attribute *attr;
4820
4821   switch (die->tag)
4822     {
4823     case DW_TAG_namespace:
4824     case DW_TAG_typedef:
4825     case DW_TAG_class_type:
4826     case DW_TAG_interface_type:
4827     case DW_TAG_structure_type:
4828     case DW_TAG_union_type:
4829     case DW_TAG_enumeration_type:
4830     case DW_TAG_enumerator:
4831     case DW_TAG_subprogram:
4832     case DW_TAG_member:
4833       return 1;
4834
4835     case DW_TAG_variable:
4836     case DW_TAG_constant:
4837       /* We only need to prefix "globally" visible variables.  These include
4838          any variable marked with DW_AT_external or any variable that
4839          lives in a namespace.  [Variables in anonymous namespaces
4840          require prefixing, but they are not DW_AT_external.]  */
4841
4842       if (dwarf2_attr (die, DW_AT_specification, cu))
4843         {
4844           struct dwarf2_cu *spec_cu = cu;
4845
4846           return die_needs_namespace (die_specification (die, &spec_cu),
4847                                       spec_cu);
4848         }
4849
4850       attr = dwarf2_attr (die, DW_AT_external, cu);
4851       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4852           && die->parent->tag != DW_TAG_module)
4853         return 0;
4854       /* A variable in a lexical block of some kind does not need a
4855          namespace, even though in C++ such variables may be external
4856          and have a mangled name.  */
4857       if (die->parent->tag ==  DW_TAG_lexical_block
4858           || die->parent->tag ==  DW_TAG_try_block
4859           || die->parent->tag ==  DW_TAG_catch_block
4860           || die->parent->tag == DW_TAG_subprogram)
4861         return 0;
4862       return 1;
4863
4864     default:
4865       return 0;
4866     }
4867 }
4868
4869 /* Retrieve the last character from a mem_file.  */
4870
4871 static void
4872 do_ui_file_peek_last (void *object, const char *buffer, long length)
4873 {
4874   char *last_char_p = (char *) object;
4875
4876   if (length > 0)
4877     *last_char_p = buffer[length - 1];
4878 }
4879
4880 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4881    compute the physname for the object, which include a method's
4882    formal parameters (C++/Java) and return type (Java).
4883
4884    For Ada, return the DIE's linkage name rather than the fully qualified
4885    name.  PHYSNAME is ignored..
4886
4887    The result is allocated on the objfile_obstack and canonicalized.  */
4888
4889 static const char *
4890 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4891                      int physname)
4892 {
4893   if (name == NULL)
4894     name = dwarf2_name (die, cu);
4895
4896   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4897      compute it by typename_concat inside GDB.  */
4898   if (cu->language == language_ada
4899       || (cu->language == language_fortran && physname))
4900     {
4901       /* For Ada unit, we prefer the linkage name over the name, as
4902          the former contains the exported name, which the user expects
4903          to be able to reference.  Ideally, we want the user to be able
4904          to reference this entity using either natural or linkage name,
4905          but we haven't started looking at this enhancement yet.  */
4906       struct attribute *attr;
4907
4908       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4909       if (attr == NULL)
4910         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4911       if (attr && DW_STRING (attr))
4912         return DW_STRING (attr);
4913     }
4914
4915   /* These are the only languages we know how to qualify names in.  */
4916   if (name != NULL
4917       && (cu->language == language_cplus || cu->language == language_java
4918           || cu->language == language_fortran))
4919     {
4920       if (die_needs_namespace (die, cu))
4921         {
4922           long length;
4923           char *prefix;
4924           struct ui_file *buf;
4925
4926           prefix = determine_prefix (die, cu);
4927           buf = mem_fileopen ();
4928           if (*prefix != '\0')
4929             {
4930               char *prefixed_name = typename_concat (NULL, prefix, name,
4931                                                      physname, cu);
4932
4933               fputs_unfiltered (prefixed_name, buf);
4934               xfree (prefixed_name);
4935             }
4936           else
4937             fputs_unfiltered (name, buf);
4938
4939           /* Template parameters may be specified in the DIE's DW_AT_name, or
4940              as children with DW_TAG_template_type_param or
4941              DW_TAG_value_type_param.  If the latter, add them to the name
4942              here.  If the name already has template parameters, then
4943              skip this step; some versions of GCC emit both, and
4944              it is more efficient to use the pre-computed name.
4945
4946              Something to keep in mind about this process: it is very
4947              unlikely, or in some cases downright impossible, to produce
4948              something that will match the mangled name of a function.
4949              If the definition of the function has the same debug info,
4950              we should be able to match up with it anyway.  But fallbacks
4951              using the minimal symbol, for instance to find a method
4952              implemented in a stripped copy of libstdc++, will not work.
4953              If we do not have debug info for the definition, we will have to
4954              match them up some other way.
4955
4956              When we do name matching there is a related problem with function
4957              templates; two instantiated function templates are allowed to
4958              differ only by their return types, which we do not add here.  */
4959
4960           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4961             {
4962               struct attribute *attr;
4963               struct die_info *child;
4964               int first = 1;
4965
4966               die->building_fullname = 1;
4967
4968               for (child = die->child; child != NULL; child = child->sibling)
4969                 {
4970                   struct type *type;
4971                   long value;
4972                   gdb_byte *bytes;
4973                   struct dwarf2_locexpr_baton *baton;
4974                   struct value *v;
4975
4976                   if (child->tag != DW_TAG_template_type_param
4977                       && child->tag != DW_TAG_template_value_param)
4978                     continue;
4979
4980                   if (first)
4981                     {
4982                       fputs_unfiltered ("<", buf);
4983                       first = 0;
4984                     }
4985                   else
4986                     fputs_unfiltered (", ", buf);
4987
4988                   attr = dwarf2_attr (child, DW_AT_type, cu);
4989                   if (attr == NULL)
4990                     {
4991                       complaint (&symfile_complaints,
4992                                  _("template parameter missing DW_AT_type"));
4993                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4994                       continue;
4995                     }
4996                   type = die_type (child, cu);
4997
4998                   if (child->tag == DW_TAG_template_type_param)
4999                     {
5000                       c_print_type (type, "", buf, -1, 0);
5001                       continue;
5002                     }
5003
5004                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5005                   if (attr == NULL)
5006                     {
5007                       complaint (&symfile_complaints,
5008                                  _("template parameter missing "
5009                                    "DW_AT_const_value"));
5010                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5011                       continue;
5012                     }
5013
5014                   dwarf2_const_value_attr (attr, type, name,
5015                                            &cu->comp_unit_obstack, cu,
5016                                            &value, &bytes, &baton);
5017
5018                   if (TYPE_NOSIGN (type))
5019                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5020                        changed, this can use value_print instead.  */
5021                     c_printchar (value, type, buf);
5022                   else
5023                     {
5024                       struct value_print_options opts;
5025
5026                       if (baton != NULL)
5027                         v = dwarf2_evaluate_loc_desc (type, NULL,
5028                                                       baton->data,
5029                                                       baton->size,
5030                                                       baton->per_cu);
5031                       else if (bytes != NULL)
5032                         {
5033                           v = allocate_value (type);
5034                           memcpy (value_contents_writeable (v), bytes,
5035                                   TYPE_LENGTH (type));
5036                         }
5037                       else
5038                         v = value_from_longest (type, value);
5039
5040                       /* Specify decimal so that we do not depend on
5041                          the radix.  */
5042                       get_formatted_print_options (&opts, 'd');
5043                       opts.raw = 1;
5044                       value_print (v, buf, &opts);
5045                       release_value (v);
5046                       value_free (v);
5047                     }
5048                 }
5049
5050               die->building_fullname = 0;
5051
5052               if (!first)
5053                 {
5054                   /* Close the argument list, with a space if necessary
5055                      (nested templates).  */
5056                   char last_char = '\0';
5057                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5058                   if (last_char == '>')
5059                     fputs_unfiltered (" >", buf);
5060                   else
5061                     fputs_unfiltered (">", buf);
5062                 }
5063             }
5064
5065           /* For Java and C++ methods, append formal parameter type
5066              information, if PHYSNAME.  */
5067
5068           if (physname && die->tag == DW_TAG_subprogram
5069               && (cu->language == language_cplus
5070                   || cu->language == language_java))
5071             {
5072               struct type *type = read_type_die (die, cu);
5073
5074               c_type_print_args (type, buf, 1, cu->language);
5075
5076               if (cu->language == language_java)
5077                 {
5078                   /* For java, we must append the return type to method
5079                      names.  */
5080                   if (die->tag == DW_TAG_subprogram)
5081                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5082                                      0, 0);
5083                 }
5084               else if (cu->language == language_cplus)
5085                 {
5086                   /* Assume that an artificial first parameter is
5087                      "this", but do not crash if it is not.  RealView
5088                      marks unnamed (and thus unused) parameters as
5089                      artificial; there is no way to differentiate
5090                      the two cases.  */
5091                   if (TYPE_NFIELDS (type) > 0
5092                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5093                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5094                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5095                                                                         0))))
5096                     fputs_unfiltered (" const", buf);
5097                 }
5098             }
5099
5100           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5101                                        &length);
5102           ui_file_delete (buf);
5103
5104           if (cu->language == language_cplus)
5105             {
5106               char *cname
5107                 = dwarf2_canonicalize_name (name, cu,
5108                                             &cu->objfile->objfile_obstack);
5109
5110               if (cname != NULL)
5111                 name = cname;
5112             }
5113         }
5114     }
5115
5116   return name;
5117 }
5118
5119 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5120    If scope qualifiers are appropriate they will be added.  The result
5121    will be allocated on the objfile_obstack, or NULL if the DIE does
5122    not have a name.  NAME may either be from a previous call to
5123    dwarf2_name or NULL.
5124
5125    The output string will be canonicalized (if C++/Java).  */
5126
5127 static const char *
5128 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5129 {
5130   return dwarf2_compute_name (name, die, cu, 0);
5131 }
5132
5133 /* Construct a physname for the given DIE in CU.  NAME may either be
5134    from a previous call to dwarf2_name or NULL.  The result will be
5135    allocated on the objfile_objstack or NULL if the DIE does not have a
5136    name.
5137
5138    The output string will be canonicalized (if C++/Java).  */
5139
5140 static const char *
5141 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5142 {
5143   return dwarf2_compute_name (name, die, cu, 1);
5144 }
5145
5146 /* Read the import statement specified by the given die and record it.  */
5147
5148 static void
5149 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5150 {
5151   struct attribute *import_attr;
5152   struct die_info *imported_die;
5153   struct dwarf2_cu *imported_cu;
5154   const char *imported_name;
5155   const char *imported_name_prefix;
5156   const char *canonical_name;
5157   const char *import_alias;
5158   const char *imported_declaration = NULL;
5159   const char *import_prefix;
5160
5161   char *temp;
5162
5163   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5164   if (import_attr == NULL)
5165     {
5166       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5167                  dwarf_tag_name (die->tag));
5168       return;
5169     }
5170
5171   imported_cu = cu;
5172   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5173   imported_name = dwarf2_name (imported_die, imported_cu);
5174   if (imported_name == NULL)
5175     {
5176       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5177
5178         The import in the following code:
5179         namespace A
5180           {
5181             typedef int B;
5182           }
5183
5184         int main ()
5185           {
5186             using A::B;
5187             B b;
5188             return b;
5189           }
5190
5191         ...
5192          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5193             <52>   DW_AT_decl_file   : 1
5194             <53>   DW_AT_decl_line   : 6
5195             <54>   DW_AT_import      : <0x75>
5196          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5197             <59>   DW_AT_name        : B
5198             <5b>   DW_AT_decl_file   : 1
5199             <5c>   DW_AT_decl_line   : 2
5200             <5d>   DW_AT_type        : <0x6e>
5201         ...
5202          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5203             <76>   DW_AT_byte_size   : 4
5204             <77>   DW_AT_encoding    : 5        (signed)
5205
5206         imports the wrong die ( 0x75 instead of 0x58 ).
5207         This case will be ignored until the gcc bug is fixed.  */
5208       return;
5209     }
5210
5211   /* Figure out the local name after import.  */
5212   import_alias = dwarf2_name (die, cu);
5213
5214   /* Figure out where the statement is being imported to.  */
5215   import_prefix = determine_prefix (die, cu);
5216
5217   /* Figure out what the scope of the imported die is and prepend it
5218      to the name of the imported die.  */
5219   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5220
5221   if (imported_die->tag != DW_TAG_namespace
5222       && imported_die->tag != DW_TAG_module)
5223     {
5224       imported_declaration = imported_name;
5225       canonical_name = imported_name_prefix;
5226     }
5227   else if (strlen (imported_name_prefix) > 0)
5228     {
5229       temp = alloca (strlen (imported_name_prefix)
5230                      + 2 + strlen (imported_name) + 1);
5231       strcpy (temp, imported_name_prefix);
5232       strcat (temp, "::");
5233       strcat (temp, imported_name);
5234       canonical_name = temp;
5235     }
5236   else
5237     canonical_name = imported_name;
5238
5239   cp_add_using_directive (import_prefix,
5240                           canonical_name,
5241                           import_alias,
5242                           imported_declaration,
5243                           &cu->objfile->objfile_obstack);
5244 }
5245
5246 static void
5247 initialize_cu_func_list (struct dwarf2_cu *cu)
5248 {
5249   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5250 }
5251
5252 /* Cleanup function for read_file_scope.  */
5253
5254 static void
5255 free_cu_line_header (void *arg)
5256 {
5257   struct dwarf2_cu *cu = arg;
5258
5259   free_line_header (cu->line_header);
5260   cu->line_header = NULL;
5261 }
5262
5263 static void
5264 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5265                          char **name, char **comp_dir)
5266 {
5267   struct attribute *attr;
5268
5269   *name = NULL;
5270   *comp_dir = NULL;
5271
5272   /* Find the filename.  Do not use dwarf2_name here, since the filename
5273      is not a source language identifier.  */
5274   attr = dwarf2_attr (die, DW_AT_name, cu);
5275   if (attr)
5276     {
5277       *name = DW_STRING (attr);
5278     }
5279
5280   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5281   if (attr)
5282     *comp_dir = DW_STRING (attr);
5283   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5284     {
5285       *comp_dir = ldirname (*name);
5286       if (*comp_dir != NULL)
5287         make_cleanup (xfree, *comp_dir);
5288     }
5289   if (*comp_dir != NULL)
5290     {
5291       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5292          directory, get rid of it.  */
5293       char *cp = strchr (*comp_dir, ':');
5294
5295       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5296         *comp_dir = cp + 1;
5297     }
5298
5299   if (*name == NULL)
5300     *name = "<unknown>";
5301 }
5302
5303 /* Process DW_TAG_compile_unit.  */
5304
5305 static void
5306 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5307 {
5308   struct objfile *objfile = cu->objfile;
5309   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5310   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5311   CORE_ADDR highpc = ((CORE_ADDR) 0);
5312   struct attribute *attr;
5313   char *name = NULL;
5314   char *comp_dir = NULL;
5315   struct die_info *child_die;
5316   bfd *abfd = objfile->obfd;
5317   struct line_header *line_header = 0;
5318   CORE_ADDR baseaddr;
5319
5320   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5321
5322   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5323
5324   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5325      from finish_block.  */
5326   if (lowpc == ((CORE_ADDR) -1))
5327     lowpc = highpc;
5328   lowpc += baseaddr;
5329   highpc += baseaddr;
5330
5331   find_file_and_directory (die, cu, &name, &comp_dir);
5332
5333   attr = dwarf2_attr (die, DW_AT_language, cu);
5334   if (attr)
5335     {
5336       set_cu_language (DW_UNSND (attr), cu);
5337     }
5338
5339   attr = dwarf2_attr (die, DW_AT_producer, cu);
5340   if (attr)
5341     cu->producer = DW_STRING (attr);
5342
5343   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5344      standardised yet.  As a workaround for the language detection we fall
5345      back to the DW_AT_producer string.  */
5346   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5347     cu->language = language_opencl;
5348
5349   /* We assume that we're processing GCC output.  */
5350   processing_gcc_compilation = 2;
5351
5352   processing_has_namespace_info = 0;
5353
5354   start_symtab (name, comp_dir, lowpc);
5355   record_debugformat ("DWARF 2");
5356   record_producer (cu->producer);
5357
5358   initialize_cu_func_list (cu);
5359
5360   /* Decode line number information if present.  We do this before
5361      processing child DIEs, so that the line header table is available
5362      for DW_AT_decl_file.  */
5363   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5364   if (attr)
5365     {
5366       unsigned int line_offset = DW_UNSND (attr);
5367       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5368       if (line_header)
5369         {
5370           cu->line_header = line_header;
5371           make_cleanup (free_cu_line_header, cu);
5372           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5373         }
5374     }
5375
5376   /* Process all dies in compilation unit.  */
5377   if (die->child != NULL)
5378     {
5379       child_die = die->child;
5380       while (child_die && child_die->tag)
5381         {
5382           process_die (child_die, cu);
5383           child_die = sibling_die (child_die);
5384         }
5385     }
5386
5387   /* Decode macro information, if present.  Dwarf 2 macro information
5388      refers to information in the line number info statement program
5389      header, so we can only read it if we've read the header
5390      successfully.  */
5391   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5392   if (attr && line_header)
5393     {
5394       unsigned int macro_offset = DW_UNSND (attr);
5395
5396       dwarf_decode_macros (line_header, macro_offset,
5397                            comp_dir, abfd, cu);
5398     }
5399   do_cleanups (back_to);
5400 }
5401
5402 /* Process DW_TAG_type_unit.
5403    For TUs we want to skip the first top level sibling if it's not the
5404    actual type being defined by this TU.  In this case the first top
5405    level sibling is there to provide context only.  */
5406
5407 static void
5408 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5409 {
5410   struct objfile *objfile = cu->objfile;
5411   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5412   CORE_ADDR lowpc;
5413   struct attribute *attr;
5414   char *name = NULL;
5415   char *comp_dir = NULL;
5416   struct die_info *child_die;
5417   bfd *abfd = objfile->obfd;
5418
5419   /* start_symtab needs a low pc, but we don't really have one.
5420      Do what read_file_scope would do in the absence of such info.  */
5421   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5422
5423   /* Find the filename.  Do not use dwarf2_name here, since the filename
5424      is not a source language identifier.  */
5425   attr = dwarf2_attr (die, DW_AT_name, cu);
5426   if (attr)
5427     name = DW_STRING (attr);
5428
5429   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5430   if (attr)
5431     comp_dir = DW_STRING (attr);
5432   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5433     {
5434       comp_dir = ldirname (name);
5435       if (comp_dir != NULL)
5436         make_cleanup (xfree, comp_dir);
5437     }
5438
5439   if (name == NULL)
5440     name = "<unknown>";
5441
5442   attr = dwarf2_attr (die, DW_AT_language, cu);
5443   if (attr)
5444     set_cu_language (DW_UNSND (attr), cu);
5445
5446   /* This isn't technically needed today.  It is done for symmetry
5447      with read_file_scope.  */
5448   attr = dwarf2_attr (die, DW_AT_producer, cu);
5449   if (attr)
5450     cu->producer = DW_STRING (attr);
5451
5452   /* We assume that we're processing GCC output.  */
5453   processing_gcc_compilation = 2;
5454
5455   processing_has_namespace_info = 0;
5456
5457   start_symtab (name, comp_dir, lowpc);
5458   record_debugformat ("DWARF 2");
5459   record_producer (cu->producer);
5460
5461   /* Process the dies in the type unit.  */
5462   if (die->child == NULL)
5463     {
5464       dump_die_for_error (die);
5465       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5466              bfd_get_filename (abfd));
5467     }
5468
5469   child_die = die->child;
5470
5471   while (child_die && child_die->tag)
5472     {
5473       process_die (child_die, cu);
5474
5475       child_die = sibling_die (child_die);
5476     }
5477
5478   do_cleanups (back_to);
5479 }
5480
5481 static void
5482 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5483                      struct dwarf2_cu *cu)
5484 {
5485   struct function_range *thisfn;
5486
5487   thisfn = (struct function_range *)
5488     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5489   thisfn->name = name;
5490   thisfn->lowpc = lowpc;
5491   thisfn->highpc = highpc;
5492   thisfn->seen_line = 0;
5493   thisfn->next = NULL;
5494
5495   if (cu->last_fn == NULL)
5496       cu->first_fn = thisfn;
5497   else
5498       cu->last_fn->next = thisfn;
5499
5500   cu->last_fn = thisfn;
5501 }
5502
5503 /* qsort helper for inherit_abstract_dies.  */
5504
5505 static int
5506 unsigned_int_compar (const void *ap, const void *bp)
5507 {
5508   unsigned int a = *(unsigned int *) ap;
5509   unsigned int b = *(unsigned int *) bp;
5510
5511   return (a > b) - (b > a);
5512 }
5513
5514 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5515    Inherit only the children of the DW_AT_abstract_origin DIE not being
5516    already referenced by DW_AT_abstract_origin from the children of the
5517    current DIE.  */
5518
5519 static void
5520 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5521 {
5522   struct die_info *child_die;
5523   unsigned die_children_count;
5524   /* CU offsets which were referenced by children of the current DIE.  */
5525   unsigned *offsets;
5526   unsigned *offsets_end, *offsetp;
5527   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5528   struct die_info *origin_die;
5529   /* Iterator of the ORIGIN_DIE children.  */
5530   struct die_info *origin_child_die;
5531   struct cleanup *cleanups;
5532   struct attribute *attr;
5533   struct dwarf2_cu *origin_cu;
5534   struct pending **origin_previous_list_in_scope;
5535
5536   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5537   if (!attr)
5538     return;
5539
5540   /* Note that following die references may follow to a die in a
5541      different cu.  */
5542
5543   origin_cu = cu;
5544   origin_die = follow_die_ref (die, attr, &origin_cu);
5545
5546   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5547      symbols in.  */
5548   origin_previous_list_in_scope = origin_cu->list_in_scope;
5549   origin_cu->list_in_scope = cu->list_in_scope;
5550
5551   if (die->tag != origin_die->tag
5552       && !(die->tag == DW_TAG_inlined_subroutine
5553            && origin_die->tag == DW_TAG_subprogram))
5554     complaint (&symfile_complaints,
5555                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5556                die->offset, origin_die->offset);
5557
5558   child_die = die->child;
5559   die_children_count = 0;
5560   while (child_die && child_die->tag)
5561     {
5562       child_die = sibling_die (child_die);
5563       die_children_count++;
5564     }
5565   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5566   cleanups = make_cleanup (xfree, offsets);
5567
5568   offsets_end = offsets;
5569   child_die = die->child;
5570   while (child_die && child_die->tag)
5571     {
5572       /* For each CHILD_DIE, find the corresponding child of
5573          ORIGIN_DIE.  If there is more than one layer of
5574          DW_AT_abstract_origin, follow them all; there shouldn't be,
5575          but GCC versions at least through 4.4 generate this (GCC PR
5576          40573).  */
5577       struct die_info *child_origin_die = child_die;
5578       struct dwarf2_cu *child_origin_cu = cu;
5579
5580       while (1)
5581         {
5582           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5583                               child_origin_cu);
5584           if (attr == NULL)
5585             break;
5586           child_origin_die = follow_die_ref (child_origin_die, attr,
5587                                              &child_origin_cu);
5588         }
5589
5590       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5591          counterpart may exist.  */
5592       if (child_origin_die != child_die)
5593         {
5594           if (child_die->tag != child_origin_die->tag
5595               && !(child_die->tag == DW_TAG_inlined_subroutine
5596                    && child_origin_die->tag == DW_TAG_subprogram))
5597             complaint (&symfile_complaints,
5598                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5599                          "different tags"), child_die->offset,
5600                        child_origin_die->offset);
5601           if (child_origin_die->parent != origin_die)
5602             complaint (&symfile_complaints,
5603                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5604                          "different parents"), child_die->offset,
5605                        child_origin_die->offset);
5606           else
5607             *offsets_end++ = child_origin_die->offset;
5608         }
5609       child_die = sibling_die (child_die);
5610     }
5611   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5612          unsigned_int_compar);
5613   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5614     if (offsetp[-1] == *offsetp)
5615       complaint (&symfile_complaints,
5616                  _("Multiple children of DIE 0x%x refer "
5617                    "to DIE 0x%x as their abstract origin"),
5618                  die->offset, *offsetp);
5619
5620   offsetp = offsets;
5621   origin_child_die = origin_die->child;
5622   while (origin_child_die && origin_child_die->tag)
5623     {
5624       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5625       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5626         offsetp++;
5627       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5628         {
5629           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5630           process_die (origin_child_die, origin_cu);
5631         }
5632       origin_child_die = sibling_die (origin_child_die);
5633     }
5634   origin_cu->list_in_scope = origin_previous_list_in_scope;
5635
5636   do_cleanups (cleanups);
5637 }
5638
5639 static void
5640 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5641 {
5642   struct objfile *objfile = cu->objfile;
5643   struct context_stack *new;
5644   CORE_ADDR lowpc;
5645   CORE_ADDR highpc;
5646   struct die_info *child_die;
5647   struct attribute *attr, *call_line, *call_file;
5648   char *name;
5649   CORE_ADDR baseaddr;
5650   struct block *block;
5651   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5652   VEC (symbolp) *template_args = NULL;
5653   struct template_symbol *templ_func = NULL;
5654
5655   if (inlined_func)
5656     {
5657       /* If we do not have call site information, we can't show the
5658          caller of this inlined function.  That's too confusing, so
5659          only use the scope for local variables.  */
5660       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5661       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5662       if (call_line == NULL || call_file == NULL)
5663         {
5664           read_lexical_block_scope (die, cu);
5665           return;
5666         }
5667     }
5668
5669   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5670
5671   name = dwarf2_name (die, cu);
5672
5673   /* Ignore functions with missing or empty names.  These are actually
5674      illegal according to the DWARF standard.  */
5675   if (name == NULL)
5676     {
5677       complaint (&symfile_complaints,
5678                  _("missing name for subprogram DIE at %d"), die->offset);
5679       return;
5680     }
5681
5682   /* Ignore functions with missing or invalid low and high pc attributes.  */
5683   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5684     {
5685       attr = dwarf2_attr (die, DW_AT_external, cu);
5686       if (!attr || !DW_UNSND (attr))
5687         complaint (&symfile_complaints,
5688                    _("cannot get low and high bounds "
5689                      "for subprogram DIE at %d"),
5690                    die->offset);
5691       return;
5692     }
5693
5694   lowpc += baseaddr;
5695   highpc += baseaddr;
5696
5697   /* Record the function range for dwarf_decode_lines.  */
5698   add_to_cu_func_list (name, lowpc, highpc, cu);
5699
5700   /* If we have any template arguments, then we must allocate a
5701      different sort of symbol.  */
5702   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5703     {
5704       if (child_die->tag == DW_TAG_template_type_param
5705           || child_die->tag == DW_TAG_template_value_param)
5706         {
5707           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5708                                        struct template_symbol);
5709           templ_func->base.is_cplus_template_function = 1;
5710           break;
5711         }
5712     }
5713
5714   new = push_context (0, lowpc);
5715   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5716                                (struct symbol *) templ_func);
5717
5718   /* If there is a location expression for DW_AT_frame_base, record
5719      it.  */
5720   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5721   if (attr)
5722     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5723        expression is being recorded directly in the function's symbol
5724        and not in a separate frame-base object.  I guess this hack is
5725        to avoid adding some sort of frame-base adjunct/annex to the
5726        function's symbol :-(.  The problem with doing this is that it
5727        results in a function symbol with a location expression that
5728        has nothing to do with the location of the function, ouch!  The
5729        relationship should be: a function's symbol has-a frame base; a
5730        frame-base has-a location expression.  */
5731     dwarf2_symbol_mark_computed (attr, new->name, cu);
5732
5733   cu->list_in_scope = &local_symbols;
5734
5735   if (die->child != NULL)
5736     {
5737       child_die = die->child;
5738       while (child_die && child_die->tag)
5739         {
5740           if (child_die->tag == DW_TAG_template_type_param
5741               || child_die->tag == DW_TAG_template_value_param)
5742             {
5743               struct symbol *arg = new_symbol (child_die, NULL, cu);
5744
5745               if (arg != NULL)
5746                 VEC_safe_push (symbolp, template_args, arg);
5747             }
5748           else
5749             process_die (child_die, cu);
5750           child_die = sibling_die (child_die);
5751         }
5752     }
5753
5754   inherit_abstract_dies (die, cu);
5755
5756   /* If we have a DW_AT_specification, we might need to import using
5757      directives from the context of the specification DIE.  See the
5758      comment in determine_prefix.  */
5759   if (cu->language == language_cplus
5760       && dwarf2_attr (die, DW_AT_specification, cu))
5761     {
5762       struct dwarf2_cu *spec_cu = cu;
5763       struct die_info *spec_die = die_specification (die, &spec_cu);
5764
5765       while (spec_die)
5766         {
5767           child_die = spec_die->child;
5768           while (child_die && child_die->tag)
5769             {
5770               if (child_die->tag == DW_TAG_imported_module)
5771                 process_die (child_die, spec_cu);
5772               child_die = sibling_die (child_die);
5773             }
5774
5775           /* In some cases, GCC generates specification DIEs that
5776              themselves contain DW_AT_specification attributes.  */
5777           spec_die = die_specification (spec_die, &spec_cu);
5778         }
5779     }
5780
5781   new = pop_context ();
5782   /* Make a block for the local symbols within.  */
5783   block = finish_block (new->name, &local_symbols, new->old_blocks,
5784                         lowpc, highpc, objfile);
5785
5786   /* For C++, set the block's scope.  */
5787   if (cu->language == language_cplus || cu->language == language_fortran)
5788     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5789                         determine_prefix (die, cu),
5790                         processing_has_namespace_info);
5791
5792   /* If we have address ranges, record them.  */
5793   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5794
5795   /* Attach template arguments to function.  */
5796   if (! VEC_empty (symbolp, template_args))
5797     {
5798       gdb_assert (templ_func != NULL);
5799
5800       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5801       templ_func->template_arguments
5802         = obstack_alloc (&objfile->objfile_obstack,
5803                          (templ_func->n_template_arguments
5804                           * sizeof (struct symbol *)));
5805       memcpy (templ_func->template_arguments,
5806               VEC_address (symbolp, template_args),
5807               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5808       VEC_free (symbolp, template_args);
5809     }
5810
5811   /* In C++, we can have functions nested inside functions (e.g., when
5812      a function declares a class that has methods).  This means that
5813      when we finish processing a function scope, we may need to go
5814      back to building a containing block's symbol lists.  */
5815   local_symbols = new->locals;
5816   param_symbols = new->params;
5817   using_directives = new->using_directives;
5818
5819   /* If we've finished processing a top-level function, subsequent
5820      symbols go in the file symbol list.  */
5821   if (outermost_context_p ())
5822     cu->list_in_scope = &file_symbols;
5823 }
5824
5825 /* Process all the DIES contained within a lexical block scope.  Start
5826    a new scope, process the dies, and then close the scope.  */
5827
5828 static void
5829 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5830 {
5831   struct objfile *objfile = cu->objfile;
5832   struct context_stack *new;
5833   CORE_ADDR lowpc, highpc;
5834   struct die_info *child_die;
5835   CORE_ADDR baseaddr;
5836
5837   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5838
5839   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5840   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5841      as multiple lexical blocks?  Handling children in a sane way would
5842      be nasty.  Might be easier to properly extend generic blocks to
5843      describe ranges.  */
5844   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5845     return;
5846   lowpc += baseaddr;
5847   highpc += baseaddr;
5848
5849   push_context (0, lowpc);
5850   if (die->child != NULL)
5851     {
5852       child_die = die->child;
5853       while (child_die && child_die->tag)
5854         {
5855           process_die (child_die, cu);
5856           child_die = sibling_die (child_die);
5857         }
5858     }
5859   new = pop_context ();
5860
5861   if (local_symbols != NULL || using_directives != NULL)
5862     {
5863       struct block *block
5864         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5865                         highpc, objfile);
5866
5867       /* Note that recording ranges after traversing children, as we
5868          do here, means that recording a parent's ranges entails
5869          walking across all its children's ranges as they appear in
5870          the address map, which is quadratic behavior.
5871
5872          It would be nicer to record the parent's ranges before
5873          traversing its children, simply overriding whatever you find
5874          there.  But since we don't even decide whether to create a
5875          block until after we've traversed its children, that's hard
5876          to do.  */
5877       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5878     }
5879   local_symbols = new->locals;
5880   using_directives = new->using_directives;
5881 }
5882
5883 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5884    Return 1 if the attributes are present and valid, otherwise, return 0.
5885    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5886
5887 static int
5888 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5889                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5890                     struct partial_symtab *ranges_pst)
5891 {
5892   struct objfile *objfile = cu->objfile;
5893   struct comp_unit_head *cu_header = &cu->header;
5894   bfd *obfd = objfile->obfd;
5895   unsigned int addr_size = cu_header->addr_size;
5896   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5897   /* Base address selection entry.  */
5898   CORE_ADDR base;
5899   int found_base;
5900   unsigned int dummy;
5901   gdb_byte *buffer;
5902   CORE_ADDR marker;
5903   int low_set;
5904   CORE_ADDR low = 0;
5905   CORE_ADDR high = 0;
5906   CORE_ADDR baseaddr;
5907
5908   found_base = cu->base_known;
5909   base = cu->base_address;
5910
5911   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5912   if (offset >= dwarf2_per_objfile->ranges.size)
5913     {
5914       complaint (&symfile_complaints,
5915                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5916                  offset);
5917       return 0;
5918     }
5919   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5920
5921   /* Read in the largest possible address.  */
5922   marker = read_address (obfd, buffer, cu, &dummy);
5923   if ((marker & mask) == mask)
5924     {
5925       /* If we found the largest possible address, then
5926          read the base address.  */
5927       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5928       buffer += 2 * addr_size;
5929       offset += 2 * addr_size;
5930       found_base = 1;
5931     }
5932
5933   low_set = 0;
5934
5935   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5936
5937   while (1)
5938     {
5939       CORE_ADDR range_beginning, range_end;
5940
5941       range_beginning = read_address (obfd, buffer, cu, &dummy);
5942       buffer += addr_size;
5943       range_end = read_address (obfd, buffer, cu, &dummy);
5944       buffer += addr_size;
5945       offset += 2 * addr_size;
5946
5947       /* An end of list marker is a pair of zero addresses.  */
5948       if (range_beginning == 0 && range_end == 0)
5949         /* Found the end of list entry.  */
5950         break;
5951
5952       /* Each base address selection entry is a pair of 2 values.
5953          The first is the largest possible address, the second is
5954          the base address.  Check for a base address here.  */
5955       if ((range_beginning & mask) == mask)
5956         {
5957           /* If we found the largest possible address, then
5958              read the base address.  */
5959           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5960           found_base = 1;
5961           continue;
5962         }
5963
5964       if (!found_base)
5965         {
5966           /* We have no valid base address for the ranges
5967              data.  */
5968           complaint (&symfile_complaints,
5969                      _("Invalid .debug_ranges data (no base address)"));
5970           return 0;
5971         }
5972
5973       if (range_beginning > range_end)
5974         {
5975           /* Inverted range entries are invalid.  */
5976           complaint (&symfile_complaints,
5977                      _("Invalid .debug_ranges data (inverted range)"));
5978           return 0;
5979         }
5980
5981       /* Empty range entries have no effect.  */
5982       if (range_beginning == range_end)
5983         continue;
5984
5985       range_beginning += base;
5986       range_end += base;
5987
5988       if (ranges_pst != NULL)
5989         addrmap_set_empty (objfile->psymtabs_addrmap,
5990                            range_beginning + baseaddr,
5991                            range_end - 1 + baseaddr,
5992                            ranges_pst);
5993
5994       /* FIXME: This is recording everything as a low-high
5995          segment of consecutive addresses.  We should have a
5996          data structure for discontiguous block ranges
5997          instead.  */
5998       if (! low_set)
5999         {
6000           low = range_beginning;
6001           high = range_end;
6002           low_set = 1;
6003         }
6004       else
6005         {
6006           if (range_beginning < low)
6007             low = range_beginning;
6008           if (range_end > high)
6009             high = range_end;
6010         }
6011     }
6012
6013   if (! low_set)
6014     /* If the first entry is an end-of-list marker, the range
6015        describes an empty scope, i.e. no instructions.  */
6016     return 0;
6017
6018   if (low_return)
6019     *low_return = low;
6020   if (high_return)
6021     *high_return = high;
6022   return 1;
6023 }
6024
6025 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6026    are present and valid, otherwise, return 0.  Return -1 if the range is
6027    discontinuous, i.e. derived from DW_AT_ranges information.  */
6028 static int
6029 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6030                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6031                       struct partial_symtab *pst)
6032 {
6033   struct attribute *attr;
6034   CORE_ADDR low = 0;
6035   CORE_ADDR high = 0;
6036   int ret = 0;
6037
6038   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6039   if (attr)
6040     {
6041       high = DW_ADDR (attr);
6042       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6043       if (attr)
6044         low = DW_ADDR (attr);
6045       else
6046         /* Found high w/o low attribute.  */
6047         return 0;
6048
6049       /* Found consecutive range of addresses.  */
6050       ret = 1;
6051     }
6052   else
6053     {
6054       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6055       if (attr != NULL)
6056         {
6057           /* Value of the DW_AT_ranges attribute is the offset in the
6058              .debug_ranges section.  */
6059           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6060             return 0;
6061           /* Found discontinuous range of addresses.  */
6062           ret = -1;
6063         }
6064     }
6065
6066   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6067   if (high <= low)
6068     return 0;
6069
6070   /* When using the GNU linker, .gnu.linkonce. sections are used to
6071      eliminate duplicate copies of functions and vtables and such.
6072      The linker will arbitrarily choose one and discard the others.
6073      The AT_*_pc values for such functions refer to local labels in
6074      these sections.  If the section from that file was discarded, the
6075      labels are not in the output, so the relocs get a value of 0.
6076      If this is a discarded function, mark the pc bounds as invalid,
6077      so that GDB will ignore it.  */
6078   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6079     return 0;
6080
6081   *lowpc = low;
6082   *highpc = high;
6083   return ret;
6084 }
6085
6086 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6087    its low and high PC addresses.  Do nothing if these addresses could not
6088    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6089    and HIGHPC to the high address if greater than HIGHPC.  */
6090
6091 static void
6092 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6093                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6094                                  struct dwarf2_cu *cu)
6095 {
6096   CORE_ADDR low, high;
6097   struct die_info *child = die->child;
6098
6099   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6100     {
6101       *lowpc = min (*lowpc, low);
6102       *highpc = max (*highpc, high);
6103     }
6104
6105   /* If the language does not allow nested subprograms (either inside
6106      subprograms or lexical blocks), we're done.  */
6107   if (cu->language != language_ada)
6108     return;
6109
6110   /* Check all the children of the given DIE.  If it contains nested
6111      subprograms, then check their pc bounds.  Likewise, we need to
6112      check lexical blocks as well, as they may also contain subprogram
6113      definitions.  */
6114   while (child && child->tag)
6115     {
6116       if (child->tag == DW_TAG_subprogram
6117           || child->tag == DW_TAG_lexical_block)
6118         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6119       child = sibling_die (child);
6120     }
6121 }
6122
6123 /* Get the low and high pc's represented by the scope DIE, and store
6124    them in *LOWPC and *HIGHPC.  If the correct values can't be
6125    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6126
6127 static void
6128 get_scope_pc_bounds (struct die_info *die,
6129                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6130                      struct dwarf2_cu *cu)
6131 {
6132   CORE_ADDR best_low = (CORE_ADDR) -1;
6133   CORE_ADDR best_high = (CORE_ADDR) 0;
6134   CORE_ADDR current_low, current_high;
6135
6136   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6137     {
6138       best_low = current_low;
6139       best_high = current_high;
6140     }
6141   else
6142     {
6143       struct die_info *child = die->child;
6144
6145       while (child && child->tag)
6146         {
6147           switch (child->tag) {
6148           case DW_TAG_subprogram:
6149             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6150             break;
6151           case DW_TAG_namespace:
6152           case DW_TAG_module:
6153             /* FIXME: carlton/2004-01-16: Should we do this for
6154                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6155                that current GCC's always emit the DIEs corresponding
6156                to definitions of methods of classes as children of a
6157                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6158                the DIEs giving the declarations, which could be
6159                anywhere).  But I don't see any reason why the
6160                standards says that they have to be there.  */
6161             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6162
6163             if (current_low != ((CORE_ADDR) -1))
6164               {
6165                 best_low = min (best_low, current_low);
6166                 best_high = max (best_high, current_high);
6167               }
6168             break;
6169           default:
6170             /* Ignore.  */
6171             break;
6172           }
6173
6174           child = sibling_die (child);
6175         }
6176     }
6177
6178   *lowpc = best_low;
6179   *highpc = best_high;
6180 }
6181
6182 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6183    in DIE.  */
6184 static void
6185 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6186                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6187 {
6188   struct attribute *attr;
6189
6190   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6191   if (attr)
6192     {
6193       CORE_ADDR high = DW_ADDR (attr);
6194
6195       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6196       if (attr)
6197         {
6198           CORE_ADDR low = DW_ADDR (attr);
6199
6200           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6201         }
6202     }
6203
6204   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6205   if (attr)
6206     {
6207       bfd *obfd = cu->objfile->obfd;
6208
6209       /* The value of the DW_AT_ranges attribute is the offset of the
6210          address range list in the .debug_ranges section.  */
6211       unsigned long offset = DW_UNSND (attr);
6212       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6213
6214       /* For some target architectures, but not others, the
6215          read_address function sign-extends the addresses it returns.
6216          To recognize base address selection entries, we need a
6217          mask.  */
6218       unsigned int addr_size = cu->header.addr_size;
6219       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6220
6221       /* The base address, to which the next pair is relative.  Note
6222          that this 'base' is a DWARF concept: most entries in a range
6223          list are relative, to reduce the number of relocs against the
6224          debugging information.  This is separate from this function's
6225          'baseaddr' argument, which GDB uses to relocate debugging
6226          information from a shared library based on the address at
6227          which the library was loaded.  */
6228       CORE_ADDR base = cu->base_address;
6229       int base_known = cu->base_known;
6230
6231       gdb_assert (dwarf2_per_objfile->ranges.readin);
6232       if (offset >= dwarf2_per_objfile->ranges.size)
6233         {
6234           complaint (&symfile_complaints,
6235                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6236                      offset);
6237           return;
6238         }
6239
6240       for (;;)
6241         {
6242           unsigned int bytes_read;
6243           CORE_ADDR start, end;
6244
6245           start = read_address (obfd, buffer, cu, &bytes_read);
6246           buffer += bytes_read;
6247           end = read_address (obfd, buffer, cu, &bytes_read);
6248           buffer += bytes_read;
6249
6250           /* Did we find the end of the range list?  */
6251           if (start == 0 && end == 0)
6252             break;
6253
6254           /* Did we find a base address selection entry?  */
6255           else if ((start & base_select_mask) == base_select_mask)
6256             {
6257               base = end;
6258               base_known = 1;
6259             }
6260
6261           /* We found an ordinary address range.  */
6262           else
6263             {
6264               if (!base_known)
6265                 {
6266                   complaint (&symfile_complaints,
6267                              _("Invalid .debug_ranges data "
6268                                "(no base address)"));
6269                   return;
6270                 }
6271
6272               if (start > end)
6273                 {
6274                   /* Inverted range entries are invalid.  */
6275                   complaint (&symfile_complaints,
6276                              _("Invalid .debug_ranges data "
6277                                "(inverted range)"));
6278                   return;
6279                 }
6280
6281               /* Empty range entries have no effect.  */
6282               if (start == end)
6283                 continue;
6284
6285               record_block_range (block,
6286                                   baseaddr + base + start,
6287                                   baseaddr + base + end - 1);
6288             }
6289         }
6290     }
6291 }
6292
6293 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6294    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6295    during 4.6.0 experimental.  */
6296
6297 static int
6298 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6299 {
6300   const char *cs;
6301   int major, minor, release;
6302
6303   if (cu->producer == NULL)
6304     {
6305       /* For unknown compilers expect their behavior is DWARF version
6306          compliant.
6307
6308          GCC started to support .debug_types sections by -gdwarf-4 since
6309          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6310          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6311          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6312          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6313
6314       return 0;
6315     }
6316
6317   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6318
6319   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6320     {
6321       /* For non-GCC compilers expect their behavior is DWARF version
6322          compliant.  */
6323
6324       return 0;
6325     }
6326   cs = &cu->producer[strlen ("GNU ")];
6327   while (*cs && !isdigit (*cs))
6328     cs++;
6329   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6330     {
6331       /* Not recognized as GCC.  */
6332
6333       return 0;
6334     }
6335
6336   return major < 4 || (major == 4 && minor < 6);
6337 }
6338
6339 /* Return the default accessibility type if it is not overriden by
6340    DW_AT_accessibility.  */
6341
6342 static enum dwarf_access_attribute
6343 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6344 {
6345   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6346     {
6347       /* The default DWARF 2 accessibility for members is public, the default
6348          accessibility for inheritance is private.  */
6349
6350       if (die->tag != DW_TAG_inheritance)
6351         return DW_ACCESS_public;
6352       else
6353         return DW_ACCESS_private;
6354     }
6355   else
6356     {
6357       /* DWARF 3+ defines the default accessibility a different way.  The same
6358          rules apply now for DW_TAG_inheritance as for the members and it only
6359          depends on the container kind.  */
6360
6361       if (die->parent->tag == DW_TAG_class_type)
6362         return DW_ACCESS_private;
6363       else
6364         return DW_ACCESS_public;
6365     }
6366 }
6367
6368 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6369    offset.  If the attribute was not found return 0, otherwise return
6370    1.  If it was found but could not properly be handled, set *OFFSET
6371    to 0.  */
6372
6373 static int
6374 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6375                              LONGEST *offset)
6376 {
6377   struct attribute *attr;
6378
6379   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6380   if (attr != NULL)
6381     {
6382       *offset = 0;
6383
6384       /* Note that we do not check for a section offset first here.
6385          This is because DW_AT_data_member_location is new in DWARF 4,
6386          so if we see it, we can assume that a constant form is really
6387          a constant and not a section offset.  */
6388       if (attr_form_is_constant (attr))
6389         *offset = dwarf2_get_attr_constant_value (attr, 0);
6390       else if (attr_form_is_section_offset (attr))
6391         dwarf2_complex_location_expr_complaint ();
6392       else if (attr_form_is_block (attr))
6393         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6394       else
6395         dwarf2_complex_location_expr_complaint ();
6396
6397       return 1;
6398     }
6399
6400   return 0;
6401 }
6402
6403 /* Add an aggregate field to the field list.  */
6404
6405 static void
6406 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6407                   struct dwarf2_cu *cu)
6408 {
6409   struct objfile *objfile = cu->objfile;
6410   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6411   struct nextfield *new_field;
6412   struct attribute *attr;
6413   struct field *fp;
6414   char *fieldname = "";
6415
6416   /* Allocate a new field list entry and link it in.  */
6417   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6418   make_cleanup (xfree, new_field);
6419   memset (new_field, 0, sizeof (struct nextfield));
6420
6421   if (die->tag == DW_TAG_inheritance)
6422     {
6423       new_field->next = fip->baseclasses;
6424       fip->baseclasses = new_field;
6425     }
6426   else
6427     {
6428       new_field->next = fip->fields;
6429       fip->fields = new_field;
6430     }
6431   fip->nfields++;
6432
6433   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6434   if (attr)
6435     new_field->accessibility = DW_UNSND (attr);
6436   else
6437     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6438   if (new_field->accessibility != DW_ACCESS_public)
6439     fip->non_public_fields = 1;
6440
6441   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6442   if (attr)
6443     new_field->virtuality = DW_UNSND (attr);
6444   else
6445     new_field->virtuality = DW_VIRTUALITY_none;
6446
6447   fp = &new_field->field;
6448
6449   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6450     {
6451       LONGEST offset;
6452
6453       /* Data member other than a C++ static data member.  */
6454
6455       /* Get type of field.  */
6456       fp->type = die_type (die, cu);
6457
6458       SET_FIELD_BITPOS (*fp, 0);
6459
6460       /* Get bit size of field (zero if none).  */
6461       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6462       if (attr)
6463         {
6464           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6465         }
6466       else
6467         {
6468           FIELD_BITSIZE (*fp) = 0;
6469         }
6470
6471       /* Get bit offset of field.  */
6472       if (handle_data_member_location (die, cu, &offset))
6473         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6474       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6475       if (attr)
6476         {
6477           if (gdbarch_bits_big_endian (gdbarch))
6478             {
6479               /* For big endian bits, the DW_AT_bit_offset gives the
6480                  additional bit offset from the MSB of the containing
6481                  anonymous object to the MSB of the field.  We don't
6482                  have to do anything special since we don't need to
6483                  know the size of the anonymous object.  */
6484               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6485             }
6486           else
6487             {
6488               /* For little endian bits, compute the bit offset to the
6489                  MSB of the anonymous object, subtract off the number of
6490                  bits from the MSB of the field to the MSB of the
6491                  object, and then subtract off the number of bits of
6492                  the field itself.  The result is the bit offset of
6493                  the LSB of the field.  */
6494               int anonymous_size;
6495               int bit_offset = DW_UNSND (attr);
6496
6497               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6498               if (attr)
6499                 {
6500                   /* The size of the anonymous object containing
6501                      the bit field is explicit, so use the
6502                      indicated size (in bytes).  */
6503                   anonymous_size = DW_UNSND (attr);
6504                 }
6505               else
6506                 {
6507                   /* The size of the anonymous object containing
6508                      the bit field must be inferred from the type
6509                      attribute of the data member containing the
6510                      bit field.  */
6511                   anonymous_size = TYPE_LENGTH (fp->type);
6512                 }
6513               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6514                 - bit_offset - FIELD_BITSIZE (*fp);
6515             }
6516         }
6517
6518       /* Get name of field.  */
6519       fieldname = dwarf2_name (die, cu);
6520       if (fieldname == NULL)
6521         fieldname = "";
6522
6523       /* The name is already allocated along with this objfile, so we don't
6524          need to duplicate it for the type.  */
6525       fp->name = fieldname;
6526
6527       /* Change accessibility for artificial fields (e.g. virtual table
6528          pointer or virtual base class pointer) to private.  */
6529       if (dwarf2_attr (die, DW_AT_artificial, cu))
6530         {
6531           FIELD_ARTIFICIAL (*fp) = 1;
6532           new_field->accessibility = DW_ACCESS_private;
6533           fip->non_public_fields = 1;
6534         }
6535     }
6536   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6537     {
6538       /* C++ static member.  */
6539
6540       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6541          is a declaration, but all versions of G++ as of this writing
6542          (so through at least 3.2.1) incorrectly generate
6543          DW_TAG_variable tags.  */
6544
6545       char *physname;
6546
6547       /* Get name of field.  */
6548       fieldname = dwarf2_name (die, cu);
6549       if (fieldname == NULL)
6550         return;
6551
6552       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6553       if (attr
6554           /* Only create a symbol if this is an external value.
6555              new_symbol checks this and puts the value in the global symbol
6556              table, which we want.  If it is not external, new_symbol
6557              will try to put the value in cu->list_in_scope which is wrong.  */
6558           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6559         {
6560           /* A static const member, not much different than an enum as far as
6561              we're concerned, except that we can support more types.  */
6562           new_symbol (die, NULL, cu);
6563         }
6564
6565       /* Get physical name.  */
6566       physname = (char *) dwarf2_physname (fieldname, die, cu);
6567
6568       /* The name is already allocated along with this objfile, so we don't
6569          need to duplicate it for the type.  */
6570       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6571       FIELD_TYPE (*fp) = die_type (die, cu);
6572       FIELD_NAME (*fp) = fieldname;
6573     }
6574   else if (die->tag == DW_TAG_inheritance)
6575     {
6576       LONGEST offset;
6577
6578       /* C++ base class field.  */
6579       if (handle_data_member_location (die, cu, &offset))
6580         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6581       FIELD_BITSIZE (*fp) = 0;
6582       FIELD_TYPE (*fp) = die_type (die, cu);
6583       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6584       fip->nbaseclasses++;
6585     }
6586 }
6587
6588 /* Add a typedef defined in the scope of the FIP's class.  */
6589
6590 static void
6591 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6592                     struct dwarf2_cu *cu)
6593 {
6594   struct objfile *objfile = cu->objfile;
6595   struct typedef_field_list *new_field;
6596   struct attribute *attr;
6597   struct typedef_field *fp;
6598   char *fieldname = "";
6599
6600   /* Allocate a new field list entry and link it in.  */
6601   new_field = xzalloc (sizeof (*new_field));
6602   make_cleanup (xfree, new_field);
6603
6604   gdb_assert (die->tag == DW_TAG_typedef);
6605
6606   fp = &new_field->field;
6607
6608   /* Get name of field.  */
6609   fp->name = dwarf2_name (die, cu);
6610   if (fp->name == NULL)
6611     return;
6612
6613   fp->type = read_type_die (die, cu);
6614
6615   new_field->next = fip->typedef_field_list;
6616   fip->typedef_field_list = new_field;
6617   fip->typedef_field_list_count++;
6618 }
6619
6620 /* Create the vector of fields, and attach it to the type.  */
6621
6622 static void
6623 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6624                               struct dwarf2_cu *cu)
6625 {
6626   int nfields = fip->nfields;
6627
6628   /* Record the field count, allocate space for the array of fields,
6629      and create blank accessibility bitfields if necessary.  */
6630   TYPE_NFIELDS (type) = nfields;
6631   TYPE_FIELDS (type) = (struct field *)
6632     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6633   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6634
6635   if (fip->non_public_fields && cu->language != language_ada)
6636     {
6637       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6638
6639       TYPE_FIELD_PRIVATE_BITS (type) =
6640         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6641       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6642
6643       TYPE_FIELD_PROTECTED_BITS (type) =
6644         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6645       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6646
6647       TYPE_FIELD_IGNORE_BITS (type) =
6648         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6649       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6650     }
6651
6652   /* If the type has baseclasses, allocate and clear a bit vector for
6653      TYPE_FIELD_VIRTUAL_BITS.  */
6654   if (fip->nbaseclasses && cu->language != language_ada)
6655     {
6656       int num_bytes = B_BYTES (fip->nbaseclasses);
6657       unsigned char *pointer;
6658
6659       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6660       pointer = TYPE_ALLOC (type, num_bytes);
6661       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6662       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6663       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6664     }
6665
6666   /* Copy the saved-up fields into the field vector.  Start from the head of
6667      the list, adding to the tail of the field array, so that they end up in
6668      the same order in the array in which they were added to the list.  */
6669   while (nfields-- > 0)
6670     {
6671       struct nextfield *fieldp;
6672
6673       if (fip->fields)
6674         {
6675           fieldp = fip->fields;
6676           fip->fields = fieldp->next;
6677         }
6678       else
6679         {
6680           fieldp = fip->baseclasses;
6681           fip->baseclasses = fieldp->next;
6682         }
6683
6684       TYPE_FIELD (type, nfields) = fieldp->field;
6685       switch (fieldp->accessibility)
6686         {
6687         case DW_ACCESS_private:
6688           if (cu->language != language_ada)
6689             SET_TYPE_FIELD_PRIVATE (type, nfields);
6690           break;
6691
6692         case DW_ACCESS_protected:
6693           if (cu->language != language_ada)
6694             SET_TYPE_FIELD_PROTECTED (type, nfields);
6695           break;
6696
6697         case DW_ACCESS_public:
6698           break;
6699
6700         default:
6701           /* Unknown accessibility.  Complain and treat it as public.  */
6702           {
6703             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6704                        fieldp->accessibility);
6705           }
6706           break;
6707         }
6708       if (nfields < fip->nbaseclasses)
6709         {
6710           switch (fieldp->virtuality)
6711             {
6712             case DW_VIRTUALITY_virtual:
6713             case DW_VIRTUALITY_pure_virtual:
6714               if (cu->language == language_ada)
6715                 error (_("unexpected virtuality in component of Ada type"));
6716               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6717               break;
6718             }
6719         }
6720     }
6721 }
6722
6723 /* Add a member function to the proper fieldlist.  */
6724
6725 static void
6726 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6727                       struct type *type, struct dwarf2_cu *cu)
6728 {
6729   struct objfile *objfile = cu->objfile;
6730   struct attribute *attr;
6731   struct fnfieldlist *flp;
6732   int i;
6733   struct fn_field *fnp;
6734   char *fieldname;
6735   struct nextfnfield *new_fnfield;
6736   struct type *this_type;
6737   enum dwarf_access_attribute accessibility;
6738
6739   if (cu->language == language_ada)
6740     error (_("unexpected member function in Ada type"));
6741
6742   /* Get name of member function.  */
6743   fieldname = dwarf2_name (die, cu);
6744   if (fieldname == NULL)
6745     return;
6746
6747   /* Look up member function name in fieldlist.  */
6748   for (i = 0; i < fip->nfnfields; i++)
6749     {
6750       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6751         break;
6752     }
6753
6754   /* Create new list element if necessary.  */
6755   if (i < fip->nfnfields)
6756     flp = &fip->fnfieldlists[i];
6757   else
6758     {
6759       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6760         {
6761           fip->fnfieldlists = (struct fnfieldlist *)
6762             xrealloc (fip->fnfieldlists,
6763                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6764                       * sizeof (struct fnfieldlist));
6765           if (fip->nfnfields == 0)
6766             make_cleanup (free_current_contents, &fip->fnfieldlists);
6767         }
6768       flp = &fip->fnfieldlists[fip->nfnfields];
6769       flp->name = fieldname;
6770       flp->length = 0;
6771       flp->head = NULL;
6772       i = fip->nfnfields++;
6773     }
6774
6775   /* Create a new member function field and chain it to the field list
6776      entry.  */
6777   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6778   make_cleanup (xfree, new_fnfield);
6779   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6780   new_fnfield->next = flp->head;
6781   flp->head = new_fnfield;
6782   flp->length++;
6783
6784   /* Fill in the member function field info.  */
6785   fnp = &new_fnfield->fnfield;
6786
6787   /* Delay processing of the physname until later.  */
6788   if (cu->language == language_cplus || cu->language == language_java)
6789     {
6790       add_to_method_list (type, i, flp->length - 1, fieldname,
6791                           die, cu);
6792     }
6793   else
6794     {
6795       const char *physname = dwarf2_physname (fieldname, die, cu);
6796       fnp->physname = physname ? physname : "";
6797     }
6798
6799   fnp->type = alloc_type (objfile);
6800   this_type = read_type_die (die, cu);
6801   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6802     {
6803       int nparams = TYPE_NFIELDS (this_type);
6804
6805       /* TYPE is the domain of this method, and THIS_TYPE is the type
6806            of the method itself (TYPE_CODE_METHOD).  */
6807       smash_to_method_type (fnp->type, type,
6808                             TYPE_TARGET_TYPE (this_type),
6809                             TYPE_FIELDS (this_type),
6810                             TYPE_NFIELDS (this_type),
6811                             TYPE_VARARGS (this_type));
6812
6813       /* Handle static member functions.
6814          Dwarf2 has no clean way to discern C++ static and non-static
6815          member functions.  G++ helps GDB by marking the first
6816          parameter for non-static member functions (which is the this
6817          pointer) as artificial.  We obtain this information from
6818          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6819       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6820         fnp->voffset = VOFFSET_STATIC;
6821     }
6822   else
6823     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6824                dwarf2_full_name (fieldname, die, cu));
6825
6826   /* Get fcontext from DW_AT_containing_type if present.  */
6827   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6828     fnp->fcontext = die_containing_type (die, cu);
6829
6830   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6831      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6832
6833   /* Get accessibility.  */
6834   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6835   if (attr)
6836     accessibility = DW_UNSND (attr);
6837   else
6838     accessibility = dwarf2_default_access_attribute (die, cu);
6839   switch (accessibility)
6840     {
6841     case DW_ACCESS_private:
6842       fnp->is_private = 1;
6843       break;
6844     case DW_ACCESS_protected:
6845       fnp->is_protected = 1;
6846       break;
6847     }
6848
6849   /* Check for artificial methods.  */
6850   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6851   if (attr && DW_UNSND (attr) != 0)
6852     fnp->is_artificial = 1;
6853
6854   /* Get index in virtual function table if it is a virtual member
6855      function.  For older versions of GCC, this is an offset in the
6856      appropriate virtual table, as specified by DW_AT_containing_type.
6857      For everyone else, it is an expression to be evaluated relative
6858      to the object address.  */
6859
6860   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6861   if (attr)
6862     {
6863       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6864         {
6865           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6866             {
6867               /* Old-style GCC.  */
6868               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6869             }
6870           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6871                    || (DW_BLOCK (attr)->size > 1
6872                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6873                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6874             {
6875               struct dwarf_block blk;
6876               int offset;
6877
6878               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6879                         ? 1 : 2);
6880               blk.size = DW_BLOCK (attr)->size - offset;
6881               blk.data = DW_BLOCK (attr)->data + offset;
6882               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6883               if ((fnp->voffset % cu->header.addr_size) != 0)
6884                 dwarf2_complex_location_expr_complaint ();
6885               else
6886                 fnp->voffset /= cu->header.addr_size;
6887               fnp->voffset += 2;
6888             }
6889           else
6890             dwarf2_complex_location_expr_complaint ();
6891
6892           if (!fnp->fcontext)
6893             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6894         }
6895       else if (attr_form_is_section_offset (attr))
6896         {
6897           dwarf2_complex_location_expr_complaint ();
6898         }
6899       else
6900         {
6901           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6902                                                  fieldname);
6903         }
6904     }
6905   else
6906     {
6907       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6908       if (attr && DW_UNSND (attr))
6909         {
6910           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6911           complaint (&symfile_complaints,
6912                      _("Member function \"%s\" (offset %d) is virtual "
6913                        "but the vtable offset is not specified"),
6914                      fieldname, die->offset);
6915           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6916           TYPE_CPLUS_DYNAMIC (type) = 1;
6917         }
6918     }
6919 }
6920
6921 /* Create the vector of member function fields, and attach it to the type.  */
6922
6923 static void
6924 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6925                                  struct dwarf2_cu *cu)
6926 {
6927   struct fnfieldlist *flp;
6928   int total_length = 0;
6929   int i;
6930
6931   if (cu->language == language_ada)
6932     error (_("unexpected member functions in Ada type"));
6933
6934   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6935   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6936     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6937
6938   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6939     {
6940       struct nextfnfield *nfp = flp->head;
6941       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6942       int k;
6943
6944       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6945       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6946       fn_flp->fn_fields = (struct fn_field *)
6947         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6948       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6949         fn_flp->fn_fields[k] = nfp->fnfield;
6950
6951       total_length += flp->length;
6952     }
6953
6954   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6955   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6956 }
6957
6958 /* Returns non-zero if NAME is the name of a vtable member in CU's
6959    language, zero otherwise.  */
6960 static int
6961 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6962 {
6963   static const char vptr[] = "_vptr";
6964   static const char vtable[] = "vtable";
6965
6966   /* Look for the C++ and Java forms of the vtable.  */
6967   if ((cu->language == language_java
6968        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6969        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6970        && is_cplus_marker (name[sizeof (vptr) - 1])))
6971     return 1;
6972
6973   return 0;
6974 }
6975
6976 /* GCC outputs unnamed structures that are really pointers to member
6977    functions, with the ABI-specified layout.  If TYPE describes
6978    such a structure, smash it into a member function type.
6979
6980    GCC shouldn't do this; it should just output pointer to member DIEs.
6981    This is GCC PR debug/28767.  */
6982
6983 static void
6984 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6985 {
6986   struct type *pfn_type, *domain_type, *new_type;
6987
6988   /* Check for a structure with no name and two children.  */
6989   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6990     return;
6991
6992   /* Check for __pfn and __delta members.  */
6993   if (TYPE_FIELD_NAME (type, 0) == NULL
6994       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6995       || TYPE_FIELD_NAME (type, 1) == NULL
6996       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6997     return;
6998
6999   /* Find the type of the method.  */
7000   pfn_type = TYPE_FIELD_TYPE (type, 0);
7001   if (pfn_type == NULL
7002       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7003       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7004     return;
7005
7006   /* Look for the "this" argument.  */
7007   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7008   if (TYPE_NFIELDS (pfn_type) == 0
7009       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7010       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7011     return;
7012
7013   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7014   new_type = alloc_type (objfile);
7015   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7016                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7017                         TYPE_VARARGS (pfn_type));
7018   smash_to_methodptr_type (type, new_type);
7019 }
7020
7021 /* Called when we find the DIE that starts a structure or union scope
7022    (definition) to create a type for the structure or union.  Fill in
7023    the type's name and general properties; the members will not be
7024    processed until process_structure_type.
7025
7026    NOTE: we need to call these functions regardless of whether or not the
7027    DIE has a DW_AT_name attribute, since it might be an anonymous
7028    structure or union.  This gets the type entered into our set of
7029    user defined types.
7030
7031    However, if the structure is incomplete (an opaque struct/union)
7032    then suppress creating a symbol table entry for it since gdb only
7033    wants to find the one with the complete definition.  Note that if
7034    it is complete, we just call new_symbol, which does it's own
7035    checking about whether the struct/union is anonymous or not (and
7036    suppresses creating a symbol table entry itself).  */
7037
7038 static struct type *
7039 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7040 {
7041   struct objfile *objfile = cu->objfile;
7042   struct type *type;
7043   struct attribute *attr;
7044   char *name;
7045
7046   /* If the definition of this type lives in .debug_types, read that type.
7047      Don't follow DW_AT_specification though, that will take us back up
7048      the chain and we want to go down.  */
7049   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7050   if (attr)
7051     {
7052       struct dwarf2_cu *type_cu = cu;
7053       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7054
7055       /* We could just recurse on read_structure_type, but we need to call
7056          get_die_type to ensure only one type for this DIE is created.
7057          This is important, for example, because for c++ classes we need
7058          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7059       type = read_type_die (type_die, type_cu);
7060
7061       /* TYPE_CU may not be the same as CU.
7062          Ensure TYPE is recorded in CU's type_hash table.  */
7063       return set_die_type (die, type, cu);
7064     }
7065
7066   type = alloc_type (objfile);
7067   INIT_CPLUS_SPECIFIC (type);
7068
7069   name = dwarf2_name (die, cu);
7070   if (name != NULL)
7071     {
7072       if (cu->language == language_cplus
7073           || cu->language == language_java)
7074         {
7075           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7076
7077           /* dwarf2_full_name might have already finished building the DIE's
7078              type.  If so, there is no need to continue.  */
7079           if (get_die_type (die, cu) != NULL)
7080             return get_die_type (die, cu);
7081
7082           TYPE_TAG_NAME (type) = full_name;
7083           if (die->tag == DW_TAG_structure_type
7084               || die->tag == DW_TAG_class_type)
7085             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7086         }
7087       else
7088         {
7089           /* The name is already allocated along with this objfile, so
7090              we don't need to duplicate it for the type.  */
7091           TYPE_TAG_NAME (type) = (char *) name;
7092           if (die->tag == DW_TAG_class_type)
7093             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7094         }
7095     }
7096
7097   if (die->tag == DW_TAG_structure_type)
7098     {
7099       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7100     }
7101   else if (die->tag == DW_TAG_union_type)
7102     {
7103       TYPE_CODE (type) = TYPE_CODE_UNION;
7104     }
7105   else
7106     {
7107       TYPE_CODE (type) = TYPE_CODE_CLASS;
7108     }
7109
7110   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7111     TYPE_DECLARED_CLASS (type) = 1;
7112
7113   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7114   if (attr)
7115     {
7116       TYPE_LENGTH (type) = DW_UNSND (attr);
7117     }
7118   else
7119     {
7120       TYPE_LENGTH (type) = 0;
7121     }
7122
7123   TYPE_STUB_SUPPORTED (type) = 1;
7124   if (die_is_declaration (die, cu))
7125     TYPE_STUB (type) = 1;
7126   else if (attr == NULL && die->child == NULL
7127            && producer_is_realview (cu->producer))
7128     /* RealView does not output the required DW_AT_declaration
7129        on incomplete types.  */
7130     TYPE_STUB (type) = 1;
7131
7132   /* We need to add the type field to the die immediately so we don't
7133      infinitely recurse when dealing with pointers to the structure
7134      type within the structure itself.  */
7135   set_die_type (die, type, cu);
7136
7137   /* set_die_type should be already done.  */
7138   set_descriptive_type (type, die, cu);
7139
7140   return type;
7141 }
7142
7143 /* Finish creating a structure or union type, including filling in
7144    its members and creating a symbol for it.  */
7145
7146 static void
7147 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7148 {
7149   struct objfile *objfile = cu->objfile;
7150   struct die_info *child_die = die->child;
7151   struct type *type;
7152
7153   type = get_die_type (die, cu);
7154   if (type == NULL)
7155     type = read_structure_type (die, cu);
7156
7157   if (die->child != NULL && ! die_is_declaration (die, cu))
7158     {
7159       struct field_info fi;
7160       struct die_info *child_die;
7161       VEC (symbolp) *template_args = NULL;
7162       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7163
7164       memset (&fi, 0, sizeof (struct field_info));
7165
7166       child_die = die->child;
7167
7168       while (child_die && child_die->tag)
7169         {
7170           if (child_die->tag == DW_TAG_member
7171               || child_die->tag == DW_TAG_variable)
7172             {
7173               /* NOTE: carlton/2002-11-05: A C++ static data member
7174                  should be a DW_TAG_member that is a declaration, but
7175                  all versions of G++ as of this writing (so through at
7176                  least 3.2.1) incorrectly generate DW_TAG_variable
7177                  tags for them instead.  */
7178               dwarf2_add_field (&fi, child_die, cu);
7179             }
7180           else if (child_die->tag == DW_TAG_subprogram)
7181             {
7182               /* C++ member function.  */
7183               dwarf2_add_member_fn (&fi, child_die, type, cu);
7184             }
7185           else if (child_die->tag == DW_TAG_inheritance)
7186             {
7187               /* C++ base class field.  */
7188               dwarf2_add_field (&fi, child_die, cu);
7189             }
7190           else if (child_die->tag == DW_TAG_typedef)
7191             dwarf2_add_typedef (&fi, child_die, cu);
7192           else if (child_die->tag == DW_TAG_template_type_param
7193                    || child_die->tag == DW_TAG_template_value_param)
7194             {
7195               struct symbol *arg = new_symbol (child_die, NULL, cu);
7196
7197               if (arg != NULL)
7198                 VEC_safe_push (symbolp, template_args, arg);
7199             }
7200
7201           child_die = sibling_die (child_die);
7202         }
7203
7204       /* Attach template arguments to type.  */
7205       if (! VEC_empty (symbolp, template_args))
7206         {
7207           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7208           TYPE_N_TEMPLATE_ARGUMENTS (type)
7209             = VEC_length (symbolp, template_args);
7210           TYPE_TEMPLATE_ARGUMENTS (type)
7211             = obstack_alloc (&objfile->objfile_obstack,
7212                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7213                               * sizeof (struct symbol *)));
7214           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7215                   VEC_address (symbolp, template_args),
7216                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7217                    * sizeof (struct symbol *)));
7218           VEC_free (symbolp, template_args);
7219         }
7220
7221       /* Attach fields and member functions to the type.  */
7222       if (fi.nfields)
7223         dwarf2_attach_fields_to_type (&fi, type, cu);
7224       if (fi.nfnfields)
7225         {
7226           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7227
7228           /* Get the type which refers to the base class (possibly this
7229              class itself) which contains the vtable pointer for the current
7230              class from the DW_AT_containing_type attribute.  This use of
7231              DW_AT_containing_type is a GNU extension.  */
7232
7233           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7234             {
7235               struct type *t = die_containing_type (die, cu);
7236
7237               TYPE_VPTR_BASETYPE (type) = t;
7238               if (type == t)
7239                 {
7240                   int i;
7241
7242                   /* Our own class provides vtbl ptr.  */
7243                   for (i = TYPE_NFIELDS (t) - 1;
7244                        i >= TYPE_N_BASECLASSES (t);
7245                        --i)
7246                     {
7247                       char *fieldname = TYPE_FIELD_NAME (t, i);
7248
7249                       if (is_vtable_name (fieldname, cu))
7250                         {
7251                           TYPE_VPTR_FIELDNO (type) = i;
7252                           break;
7253                         }
7254                     }
7255
7256                   /* Complain if virtual function table field not found.  */
7257                   if (i < TYPE_N_BASECLASSES (t))
7258                     complaint (&symfile_complaints,
7259                                _("virtual function table pointer "
7260                                  "not found when defining class '%s'"),
7261                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7262                                "");
7263                 }
7264               else
7265                 {
7266                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7267                 }
7268             }
7269           else if (cu->producer
7270                    && strncmp (cu->producer,
7271                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7272             {
7273               /* The IBM XLC compiler does not provide direct indication
7274                  of the containing type, but the vtable pointer is
7275                  always named __vfp.  */
7276
7277               int i;
7278
7279               for (i = TYPE_NFIELDS (type) - 1;
7280                    i >= TYPE_N_BASECLASSES (type);
7281                    --i)
7282                 {
7283                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7284                     {
7285                       TYPE_VPTR_FIELDNO (type) = i;
7286                       TYPE_VPTR_BASETYPE (type) = type;
7287                       break;
7288                     }
7289                 }
7290             }
7291         }
7292
7293       /* Copy fi.typedef_field_list linked list elements content into the
7294          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7295       if (fi.typedef_field_list)
7296         {
7297           int i = fi.typedef_field_list_count;
7298
7299           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7300           TYPE_TYPEDEF_FIELD_ARRAY (type)
7301             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7302           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7303
7304           /* Reverse the list order to keep the debug info elements order.  */
7305           while (--i >= 0)
7306             {
7307               struct typedef_field *dest, *src;
7308
7309               dest = &TYPE_TYPEDEF_FIELD (type, i);
7310               src = &fi.typedef_field_list->field;
7311               fi.typedef_field_list = fi.typedef_field_list->next;
7312               *dest = *src;
7313             }
7314         }
7315
7316       do_cleanups (back_to);
7317     }
7318
7319   quirk_gcc_member_function_pointer (type, cu->objfile);
7320
7321   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7322      snapshots) has been known to create a die giving a declaration
7323      for a class that has, as a child, a die giving a definition for a
7324      nested class.  So we have to process our children even if the
7325      current die is a declaration.  Normally, of course, a declaration
7326      won't have any children at all.  */
7327
7328   while (child_die != NULL && child_die->tag)
7329     {
7330       if (child_die->tag == DW_TAG_member
7331           || child_die->tag == DW_TAG_variable
7332           || child_die->tag == DW_TAG_inheritance
7333           || child_die->tag == DW_TAG_template_value_param
7334           || child_die->tag == DW_TAG_template_type_param)
7335         {
7336           /* Do nothing.  */
7337         }
7338       else
7339         process_die (child_die, cu);
7340
7341       child_die = sibling_die (child_die);
7342     }
7343
7344   /* Do not consider external references.  According to the DWARF standard,
7345      these DIEs are identified by the fact that they have no byte_size
7346      attribute, and a declaration attribute.  */
7347   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7348       || !die_is_declaration (die, cu))
7349     new_symbol (die, type, cu);
7350 }
7351
7352 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7353    complete the type's fields yet, or create any symbols.  */
7354
7355 static struct type *
7356 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7357 {
7358   struct objfile *objfile = cu->objfile;
7359   struct type *type;
7360   struct attribute *attr;
7361   const char *name;
7362
7363   /* If the definition of this type lives in .debug_types, read that type.
7364      Don't follow DW_AT_specification though, that will take us back up
7365      the chain and we want to go down.  */
7366   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7367   if (attr)
7368     {
7369       struct dwarf2_cu *type_cu = cu;
7370       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7371
7372       type = read_type_die (type_die, type_cu);
7373
7374       /* TYPE_CU may not be the same as CU.
7375          Ensure TYPE is recorded in CU's type_hash table.  */
7376       return set_die_type (die, type, cu);
7377     }
7378
7379   type = alloc_type (objfile);
7380
7381   TYPE_CODE (type) = TYPE_CODE_ENUM;
7382   name = dwarf2_full_name (NULL, die, cu);
7383   if (name != NULL)
7384     TYPE_TAG_NAME (type) = (char *) name;
7385
7386   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7387   if (attr)
7388     {
7389       TYPE_LENGTH (type) = DW_UNSND (attr);
7390     }
7391   else
7392     {
7393       TYPE_LENGTH (type) = 0;
7394     }
7395
7396   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7397      declared as private in the package spec, and then defined only
7398      inside the package body.  Such types are known as Taft Amendment
7399      Types.  When another package uses such a type, an incomplete DIE
7400      may be generated by the compiler.  */
7401   if (die_is_declaration (die, cu))
7402     TYPE_STUB (type) = 1;
7403
7404   return set_die_type (die, type, cu);
7405 }
7406
7407 /* Given a pointer to a die which begins an enumeration, process all
7408    the dies that define the members of the enumeration, and create the
7409    symbol for the enumeration type.
7410
7411    NOTE: We reverse the order of the element list.  */
7412
7413 static void
7414 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7415 {
7416   struct type *this_type;
7417
7418   this_type = get_die_type (die, cu);
7419   if (this_type == NULL)
7420     this_type = read_enumeration_type (die, cu);
7421
7422   if (die->child != NULL)
7423     {
7424       struct die_info *child_die;
7425       struct symbol *sym;
7426       struct field *fields = NULL;
7427       int num_fields = 0;
7428       int unsigned_enum = 1;
7429       char *name;
7430
7431       child_die = die->child;
7432       while (child_die && child_die->tag)
7433         {
7434           if (child_die->tag != DW_TAG_enumerator)
7435             {
7436               process_die (child_die, cu);
7437             }
7438           else
7439             {
7440               name = dwarf2_name (child_die, cu);
7441               if (name)
7442                 {
7443                   sym = new_symbol (child_die, this_type, cu);
7444                   if (SYMBOL_VALUE (sym) < 0)
7445                     unsigned_enum = 0;
7446
7447                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7448                     {
7449                       fields = (struct field *)
7450                         xrealloc (fields,
7451                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7452                                   * sizeof (struct field));
7453                     }
7454
7455                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7456                   FIELD_TYPE (fields[num_fields]) = NULL;
7457                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7458                   FIELD_BITSIZE (fields[num_fields]) = 0;
7459
7460                   num_fields++;
7461                 }
7462             }
7463
7464           child_die = sibling_die (child_die);
7465         }
7466
7467       if (num_fields)
7468         {
7469           TYPE_NFIELDS (this_type) = num_fields;
7470           TYPE_FIELDS (this_type) = (struct field *)
7471             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7472           memcpy (TYPE_FIELDS (this_type), fields,
7473                   sizeof (struct field) * num_fields);
7474           xfree (fields);
7475         }
7476       if (unsigned_enum)
7477         TYPE_UNSIGNED (this_type) = 1;
7478     }
7479
7480   new_symbol (die, this_type, cu);
7481 }
7482
7483 /* Extract all information from a DW_TAG_array_type DIE and put it in
7484    the DIE's type field.  For now, this only handles one dimensional
7485    arrays.  */
7486
7487 static struct type *
7488 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7489 {
7490   struct objfile *objfile = cu->objfile;
7491   struct die_info *child_die;
7492   struct type *type;
7493   struct type *element_type, *range_type, *index_type;
7494   struct type **range_types = NULL;
7495   struct attribute *attr;
7496   int ndim = 0;
7497   struct cleanup *back_to;
7498   char *name;
7499
7500   element_type = die_type (die, cu);
7501
7502   /* The die_type call above may have already set the type for this DIE.  */
7503   type = get_die_type (die, cu);
7504   if (type)
7505     return type;
7506
7507   /* Irix 6.2 native cc creates array types without children for
7508      arrays with unspecified length.  */
7509   if (die->child == NULL)
7510     {
7511       index_type = objfile_type (objfile)->builtin_int;
7512       range_type = create_range_type (NULL, index_type, 0, -1);
7513       type = create_array_type (NULL, element_type, range_type);
7514       return set_die_type (die, type, cu);
7515     }
7516
7517   back_to = make_cleanup (null_cleanup, NULL);
7518   child_die = die->child;
7519   while (child_die && child_die->tag)
7520     {
7521       if (child_die->tag == DW_TAG_subrange_type)
7522         {
7523           struct type *child_type = read_type_die (child_die, cu);
7524
7525           if (child_type != NULL)
7526             {
7527               /* The range type was succesfully read.  Save it for the
7528                  array type creation.  */
7529               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7530                 {
7531                   range_types = (struct type **)
7532                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7533                               * sizeof (struct type *));
7534                   if (ndim == 0)
7535                     make_cleanup (free_current_contents, &range_types);
7536                 }
7537               range_types[ndim++] = child_type;
7538             }
7539         }
7540       child_die = sibling_die (child_die);
7541     }
7542
7543   /* Dwarf2 dimensions are output from left to right, create the
7544      necessary array types in backwards order.  */
7545
7546   type = element_type;
7547
7548   if (read_array_order (die, cu) == DW_ORD_col_major)
7549     {
7550       int i = 0;
7551
7552       while (i < ndim)
7553         type = create_array_type (NULL, type, range_types[i++]);
7554     }
7555   else
7556     {
7557       while (ndim-- > 0)
7558         type = create_array_type (NULL, type, range_types[ndim]);
7559     }
7560
7561   /* Understand Dwarf2 support for vector types (like they occur on
7562      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7563      array type.  This is not part of the Dwarf2/3 standard yet, but a
7564      custom vendor extension.  The main difference between a regular
7565      array and the vector variant is that vectors are passed by value
7566      to functions.  */
7567   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7568   if (attr)
7569     make_vector_type (type);
7570
7571   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7572      implementation may choose to implement triple vectors using this
7573      attribute.  */
7574   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7575   if (attr)
7576     {
7577       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7578         TYPE_LENGTH (type) = DW_UNSND (attr);
7579       else
7580         complaint (&symfile_complaints,
7581                    _("DW_AT_byte_size for array type smaller "
7582                      "than the total size of elements"));
7583     }
7584
7585   name = dwarf2_name (die, cu);
7586   if (name)
7587     TYPE_NAME (type) = name;
7588
7589   /* Install the type in the die.  */
7590   set_die_type (die, type, cu);
7591
7592   /* set_die_type should be already done.  */
7593   set_descriptive_type (type, die, cu);
7594
7595   do_cleanups (back_to);
7596
7597   return type;
7598 }
7599
7600 static enum dwarf_array_dim_ordering
7601 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7602 {
7603   struct attribute *attr;
7604
7605   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7606
7607   if (attr) return DW_SND (attr);
7608
7609   /* GNU F77 is a special case, as at 08/2004 array type info is the
7610      opposite order to the dwarf2 specification, but data is still
7611      laid out as per normal fortran.
7612
7613      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7614      version checking.  */
7615
7616   if (cu->language == language_fortran
7617       && cu->producer && strstr (cu->producer, "GNU F77"))
7618     {
7619       return DW_ORD_row_major;
7620     }
7621
7622   switch (cu->language_defn->la_array_ordering)
7623     {
7624     case array_column_major:
7625       return DW_ORD_col_major;
7626     case array_row_major:
7627     default:
7628       return DW_ORD_row_major;
7629     };
7630 }
7631
7632 /* Extract all information from a DW_TAG_set_type DIE and put it in
7633    the DIE's type field.  */
7634
7635 static struct type *
7636 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7637 {
7638   struct type *domain_type, *set_type;
7639   struct attribute *attr;
7640
7641   domain_type = die_type (die, cu);
7642
7643   /* The die_type call above may have already set the type for this DIE.  */
7644   set_type = get_die_type (die, cu);
7645   if (set_type)
7646     return set_type;
7647
7648   set_type = create_set_type (NULL, domain_type);
7649
7650   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7651   if (attr)
7652     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7653
7654   return set_die_type (die, set_type, cu);
7655 }
7656
7657 /* First cut: install each common block member as a global variable.  */
7658
7659 static void
7660 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7661 {
7662   struct die_info *child_die;
7663   struct attribute *attr;
7664   struct symbol *sym;
7665   CORE_ADDR base = (CORE_ADDR) 0;
7666
7667   attr = dwarf2_attr (die, DW_AT_location, cu);
7668   if (attr)
7669     {
7670       /* Support the .debug_loc offsets.  */
7671       if (attr_form_is_block (attr))
7672         {
7673           base = decode_locdesc (DW_BLOCK (attr), cu);
7674         }
7675       else if (attr_form_is_section_offset (attr))
7676         {
7677           dwarf2_complex_location_expr_complaint ();
7678         }
7679       else
7680         {
7681           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7682                                                  "common block member");
7683         }
7684     }
7685   if (die->child != NULL)
7686     {
7687       child_die = die->child;
7688       while (child_die && child_die->tag)
7689         {
7690           LONGEST offset;
7691
7692           sym = new_symbol (child_die, NULL, cu);
7693           if (sym != NULL
7694               && handle_data_member_location (child_die, cu, &offset))
7695             {
7696               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7697               add_symbol_to_list (sym, &global_symbols);
7698             }
7699           child_die = sibling_die (child_die);
7700         }
7701     }
7702 }
7703
7704 /* Create a type for a C++ namespace.  */
7705
7706 static struct type *
7707 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7708 {
7709   struct objfile *objfile = cu->objfile;
7710   const char *previous_prefix, *name;
7711   int is_anonymous;
7712   struct type *type;
7713
7714   /* For extensions, reuse the type of the original namespace.  */
7715   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7716     {
7717       struct die_info *ext_die;
7718       struct dwarf2_cu *ext_cu = cu;
7719
7720       ext_die = dwarf2_extension (die, &ext_cu);
7721       type = read_type_die (ext_die, ext_cu);
7722
7723       /* EXT_CU may not be the same as CU.
7724          Ensure TYPE is recorded in CU's type_hash table.  */
7725       return set_die_type (die, type, cu);
7726     }
7727
7728   name = namespace_name (die, &is_anonymous, cu);
7729
7730   /* Now build the name of the current namespace.  */
7731
7732   previous_prefix = determine_prefix (die, cu);
7733   if (previous_prefix[0] != '\0')
7734     name = typename_concat (&objfile->objfile_obstack,
7735                             previous_prefix, name, 0, cu);
7736
7737   /* Create the type.  */
7738   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7739                     objfile);
7740   TYPE_NAME (type) = (char *) name;
7741   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7742
7743   return set_die_type (die, type, cu);
7744 }
7745
7746 /* Read a C++ namespace.  */
7747
7748 static void
7749 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7750 {
7751   struct objfile *objfile = cu->objfile;
7752   int is_anonymous;
7753
7754   /* Add a symbol associated to this if we haven't seen the namespace
7755      before.  Also, add a using directive if it's an anonymous
7756      namespace.  */
7757
7758   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7759     {
7760       struct type *type;
7761
7762       type = read_type_die (die, cu);
7763       new_symbol (die, type, cu);
7764
7765       namespace_name (die, &is_anonymous, cu);
7766       if (is_anonymous)
7767         {
7768           const char *previous_prefix = determine_prefix (die, cu);
7769
7770           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7771                                   NULL, &objfile->objfile_obstack);
7772         }
7773     }
7774
7775   if (die->child != NULL)
7776     {
7777       struct die_info *child_die = die->child;
7778
7779       while (child_die && child_die->tag)
7780         {
7781           process_die (child_die, cu);
7782           child_die = sibling_die (child_die);
7783         }
7784     }
7785 }
7786
7787 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7788    imported module.  Still we need that type as local Fortran "use ... only"
7789    declaration imports depend on the created type in determine_prefix.  */
7790
7791 static struct type *
7792 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7793 {
7794   struct objfile *objfile = cu->objfile;
7795   char *module_name;
7796   struct type *type;
7797
7798   module_name = dwarf2_name (die, cu);
7799   if (!module_name)
7800     complaint (&symfile_complaints,
7801                _("DW_TAG_module has no name, offset 0x%x"),
7802                die->offset);
7803   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7804
7805   /* determine_prefix uses TYPE_TAG_NAME.  */
7806   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7807
7808   return set_die_type (die, type, cu);
7809 }
7810
7811 /* Read a Fortran module.  */
7812
7813 static void
7814 read_module (struct die_info *die, struct dwarf2_cu *cu)
7815 {
7816   struct die_info *child_die = die->child;
7817
7818   while (child_die && child_die->tag)
7819     {
7820       process_die (child_die, cu);
7821       child_die = sibling_die (child_die);
7822     }
7823 }
7824
7825 /* Return the name of the namespace represented by DIE.  Set
7826    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7827    namespace.  */
7828
7829 static const char *
7830 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7831 {
7832   struct die_info *current_die;
7833   const char *name = NULL;
7834
7835   /* Loop through the extensions until we find a name.  */
7836
7837   for (current_die = die;
7838        current_die != NULL;
7839        current_die = dwarf2_extension (die, &cu))
7840     {
7841       name = dwarf2_name (current_die, cu);
7842       if (name != NULL)
7843         break;
7844     }
7845
7846   /* Is it an anonymous namespace?  */
7847
7848   *is_anonymous = (name == NULL);
7849   if (*is_anonymous)
7850     name = "(anonymous namespace)";
7851
7852   return name;
7853 }
7854
7855 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7856    the user defined type vector.  */
7857
7858 static struct type *
7859 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7860 {
7861   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7862   struct comp_unit_head *cu_header = &cu->header;
7863   struct type *type;
7864   struct attribute *attr_byte_size;
7865   struct attribute *attr_address_class;
7866   int byte_size, addr_class;
7867   struct type *target_type;
7868
7869   target_type = die_type (die, cu);
7870
7871   /* The die_type call above may have already set the type for this DIE.  */
7872   type = get_die_type (die, cu);
7873   if (type)
7874     return type;
7875
7876   type = lookup_pointer_type (target_type);
7877
7878   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7879   if (attr_byte_size)
7880     byte_size = DW_UNSND (attr_byte_size);
7881   else
7882     byte_size = cu_header->addr_size;
7883
7884   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7885   if (attr_address_class)
7886     addr_class = DW_UNSND (attr_address_class);
7887   else
7888     addr_class = DW_ADDR_none;
7889
7890   /* If the pointer size or address class is different than the
7891      default, create a type variant marked as such and set the
7892      length accordingly.  */
7893   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7894     {
7895       if (gdbarch_address_class_type_flags_p (gdbarch))
7896         {
7897           int type_flags;
7898
7899           type_flags = gdbarch_address_class_type_flags
7900                          (gdbarch, byte_size, addr_class);
7901           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7902                       == 0);
7903           type = make_type_with_address_space (type, type_flags);
7904         }
7905       else if (TYPE_LENGTH (type) != byte_size)
7906         {
7907           complaint (&symfile_complaints,
7908                      _("invalid pointer size %d"), byte_size);
7909         }
7910       else
7911         {
7912           /* Should we also complain about unhandled address classes?  */
7913         }
7914     }
7915
7916   TYPE_LENGTH (type) = byte_size;
7917   return set_die_type (die, type, cu);
7918 }
7919
7920 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7921    the user defined type vector.  */
7922
7923 static struct type *
7924 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7925 {
7926   struct type *type;
7927   struct type *to_type;
7928   struct type *domain;
7929
7930   to_type = die_type (die, cu);
7931   domain = die_containing_type (die, cu);
7932
7933   /* The calls above may have already set the type for this DIE.  */
7934   type = get_die_type (die, cu);
7935   if (type)
7936     return type;
7937
7938   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7939     type = lookup_methodptr_type (to_type);
7940   else
7941     type = lookup_memberptr_type (to_type, domain);
7942
7943   return set_die_type (die, type, cu);
7944 }
7945
7946 /* Extract all information from a DW_TAG_reference_type DIE and add to
7947    the user defined type vector.  */
7948
7949 static struct type *
7950 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7951 {
7952   struct comp_unit_head *cu_header = &cu->header;
7953   struct type *type, *target_type;
7954   struct attribute *attr;
7955
7956   target_type = die_type (die, cu);
7957
7958   /* The die_type call above may have already set the type for this DIE.  */
7959   type = get_die_type (die, cu);
7960   if (type)
7961     return type;
7962
7963   type = lookup_reference_type (target_type);
7964   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7965   if (attr)
7966     {
7967       TYPE_LENGTH (type) = DW_UNSND (attr);
7968     }
7969   else
7970     {
7971       TYPE_LENGTH (type) = cu_header->addr_size;
7972     }
7973   return set_die_type (die, type, cu);
7974 }
7975
7976 static struct type *
7977 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7978 {
7979   struct type *base_type, *cv_type;
7980
7981   base_type = die_type (die, cu);
7982
7983   /* The die_type call above may have already set the type for this DIE.  */
7984   cv_type = get_die_type (die, cu);
7985   if (cv_type)
7986     return cv_type;
7987
7988   /* In case the const qualifier is applied to an array type, the element type
7989      is so qualified, not the array type (section 6.7.3 of C99).  */
7990   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7991     {
7992       struct type *el_type, *inner_array;
7993
7994       base_type = copy_type (base_type);
7995       inner_array = base_type;
7996
7997       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7998         {
7999           TYPE_TARGET_TYPE (inner_array) =
8000             copy_type (TYPE_TARGET_TYPE (inner_array));
8001           inner_array = TYPE_TARGET_TYPE (inner_array);
8002         }
8003
8004       el_type = TYPE_TARGET_TYPE (inner_array);
8005       TYPE_TARGET_TYPE (inner_array) =
8006         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8007
8008       return set_die_type (die, base_type, cu);
8009     }
8010
8011   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8012   return set_die_type (die, cv_type, cu);
8013 }
8014
8015 static struct type *
8016 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8017 {
8018   struct type *base_type, *cv_type;
8019
8020   base_type = die_type (die, cu);
8021
8022   /* The die_type call above may have already set the type for this DIE.  */
8023   cv_type = get_die_type (die, cu);
8024   if (cv_type)
8025     return cv_type;
8026
8027   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8028   return set_die_type (die, cv_type, cu);
8029 }
8030
8031 /* Extract all information from a DW_TAG_string_type DIE and add to
8032    the user defined type vector.  It isn't really a user defined type,
8033    but it behaves like one, with other DIE's using an AT_user_def_type
8034    attribute to reference it.  */
8035
8036 static struct type *
8037 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8038 {
8039   struct objfile *objfile = cu->objfile;
8040   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8041   struct type *type, *range_type, *index_type, *char_type;
8042   struct attribute *attr;
8043   unsigned int length;
8044
8045   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8046   if (attr)
8047     {
8048       length = DW_UNSND (attr);
8049     }
8050   else
8051     {
8052       /* Check for the DW_AT_byte_size attribute.  */
8053       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8054       if (attr)
8055         {
8056           length = DW_UNSND (attr);
8057         }
8058       else
8059         {
8060           length = 1;
8061         }
8062     }
8063
8064   index_type = objfile_type (objfile)->builtin_int;
8065   range_type = create_range_type (NULL, index_type, 1, length);
8066   char_type = language_string_char_type (cu->language_defn, gdbarch);
8067   type = create_string_type (NULL, char_type, range_type);
8068
8069   return set_die_type (die, type, cu);
8070 }
8071
8072 /* Handle DIES due to C code like:
8073
8074    struct foo
8075    {
8076    int (*funcp)(int a, long l);
8077    int b;
8078    };
8079
8080    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8081
8082 static struct type *
8083 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8084 {
8085   struct type *type;            /* Type that this function returns.  */
8086   struct type *ftype;           /* Function that returns above type.  */
8087   struct attribute *attr;
8088
8089   type = die_type (die, cu);
8090
8091   /* The die_type call above may have already set the type for this DIE.  */
8092   ftype = get_die_type (die, cu);
8093   if (ftype)
8094     return ftype;
8095
8096   ftype = lookup_function_type (type);
8097
8098   /* All functions in C++, Pascal and Java have prototypes.  */
8099   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8100   if ((attr && (DW_UNSND (attr) != 0))
8101       || cu->language == language_cplus
8102       || cu->language == language_java
8103       || cu->language == language_pascal)
8104     TYPE_PROTOTYPED (ftype) = 1;
8105   else if (producer_is_realview (cu->producer))
8106     /* RealView does not emit DW_AT_prototyped.  We can not
8107        distinguish prototyped and unprototyped functions; default to
8108        prototyped, since that is more common in modern code (and
8109        RealView warns about unprototyped functions).  */
8110     TYPE_PROTOTYPED (ftype) = 1;
8111
8112   /* Store the calling convention in the type if it's available in
8113      the subroutine die.  Otherwise set the calling convention to
8114      the default value DW_CC_normal.  */
8115   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8116   if (attr)
8117     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8118   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8119     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8120   else
8121     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8122
8123   /* We need to add the subroutine type to the die immediately so
8124      we don't infinitely recurse when dealing with parameters
8125      declared as the same subroutine type.  */
8126   set_die_type (die, ftype, cu);
8127
8128   if (die->child != NULL)
8129     {
8130       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8131       struct die_info *child_die;
8132       int nparams, iparams;
8133
8134       /* Count the number of parameters.
8135          FIXME: GDB currently ignores vararg functions, but knows about
8136          vararg member functions.  */
8137       nparams = 0;
8138       child_die = die->child;
8139       while (child_die && child_die->tag)
8140         {
8141           if (child_die->tag == DW_TAG_formal_parameter)
8142             nparams++;
8143           else if (child_die->tag == DW_TAG_unspecified_parameters)
8144             TYPE_VARARGS (ftype) = 1;
8145           child_die = sibling_die (child_die);
8146         }
8147
8148       /* Allocate storage for parameters and fill them in.  */
8149       TYPE_NFIELDS (ftype) = nparams;
8150       TYPE_FIELDS (ftype) = (struct field *)
8151         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8152
8153       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8154          even if we error out during the parameters reading below.  */
8155       for (iparams = 0; iparams < nparams; iparams++)
8156         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8157
8158       iparams = 0;
8159       child_die = die->child;
8160       while (child_die && child_die->tag)
8161         {
8162           if (child_die->tag == DW_TAG_formal_parameter)
8163             {
8164               struct type *arg_type;
8165
8166               /* DWARF version 2 has no clean way to discern C++
8167                  static and non-static member functions.  G++ helps
8168                  GDB by marking the first parameter for non-static
8169                  member functions (which is the this pointer) as
8170                  artificial.  We pass this information to
8171                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8172
8173                  DWARF version 3 added DW_AT_object_pointer, which GCC
8174                  4.5 does not yet generate.  */
8175               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8176               if (attr)
8177                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8178               else
8179                 {
8180                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8181
8182                   /* GCC/43521: In java, the formal parameter
8183                      "this" is sometimes not marked with DW_AT_artificial.  */
8184                   if (cu->language == language_java)
8185                     {
8186                       const char *name = dwarf2_name (child_die, cu);
8187
8188                       if (name && !strcmp (name, "this"))
8189                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8190                     }
8191                 }
8192               arg_type = die_type (child_die, cu);
8193
8194               /* RealView does not mark THIS as const, which the testsuite
8195                  expects.  GCC marks THIS as const in method definitions,
8196                  but not in the class specifications (GCC PR 43053).  */
8197               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8198                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8199                 {
8200                   int is_this = 0;
8201                   struct dwarf2_cu *arg_cu = cu;
8202                   const char *name = dwarf2_name (child_die, cu);
8203
8204                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8205                   if (attr)
8206                     {
8207                       /* If the compiler emits this, use it.  */
8208                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8209                         is_this = 1;
8210                     }
8211                   else if (name && strcmp (name, "this") == 0)
8212                     /* Function definitions will have the argument names.  */
8213                     is_this = 1;
8214                   else if (name == NULL && iparams == 0)
8215                     /* Declarations may not have the names, so like
8216                        elsewhere in GDB, assume an artificial first
8217                        argument is "this".  */
8218                     is_this = 1;
8219
8220                   if (is_this)
8221                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8222                                              arg_type, 0);
8223                 }
8224
8225               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8226               iparams++;
8227             }
8228           child_die = sibling_die (child_die);
8229         }
8230     }
8231
8232   return ftype;
8233 }
8234
8235 static struct type *
8236 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8237 {
8238   struct objfile *objfile = cu->objfile;
8239   const char *name = NULL;
8240   struct type *this_type;
8241
8242   name = dwarf2_full_name (NULL, die, cu);
8243   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8244                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8245   TYPE_NAME (this_type) = (char *) name;
8246   set_die_type (die, this_type, cu);
8247   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8248   return this_type;
8249 }
8250
8251 /* Find a representation of a given base type and install
8252    it in the TYPE field of the die.  */
8253
8254 static struct type *
8255 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8256 {
8257   struct objfile *objfile = cu->objfile;
8258   struct type *type;
8259   struct attribute *attr;
8260   int encoding = 0, size = 0;
8261   char *name;
8262   enum type_code code = TYPE_CODE_INT;
8263   int type_flags = 0;
8264   struct type *target_type = NULL;
8265
8266   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8267   if (attr)
8268     {
8269       encoding = DW_UNSND (attr);
8270     }
8271   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8272   if (attr)
8273     {
8274       size = DW_UNSND (attr);
8275     }
8276   name = dwarf2_name (die, cu);
8277   if (!name)
8278     {
8279       complaint (&symfile_complaints,
8280                  _("DW_AT_name missing from DW_TAG_base_type"));
8281     }
8282
8283   switch (encoding)
8284     {
8285       case DW_ATE_address:
8286         /* Turn DW_ATE_address into a void * pointer.  */
8287         code = TYPE_CODE_PTR;
8288         type_flags |= TYPE_FLAG_UNSIGNED;
8289         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8290         break;
8291       case DW_ATE_boolean:
8292         code = TYPE_CODE_BOOL;
8293         type_flags |= TYPE_FLAG_UNSIGNED;
8294         break;
8295       case DW_ATE_complex_float:
8296         code = TYPE_CODE_COMPLEX;
8297         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8298         break;
8299       case DW_ATE_decimal_float:
8300         code = TYPE_CODE_DECFLOAT;
8301         break;
8302       case DW_ATE_float:
8303         code = TYPE_CODE_FLT;
8304         break;
8305       case DW_ATE_signed:
8306         break;
8307       case DW_ATE_unsigned:
8308         type_flags |= TYPE_FLAG_UNSIGNED;
8309         break;
8310       case DW_ATE_signed_char:
8311         if (cu->language == language_ada || cu->language == language_m2
8312             || cu->language == language_pascal)
8313           code = TYPE_CODE_CHAR;
8314         break;
8315       case DW_ATE_unsigned_char:
8316         if (cu->language == language_ada || cu->language == language_m2
8317             || cu->language == language_pascal)
8318           code = TYPE_CODE_CHAR;
8319         type_flags |= TYPE_FLAG_UNSIGNED;
8320         break;
8321       case DW_ATE_UTF:
8322         /* We just treat this as an integer and then recognize the
8323            type by name elsewhere.  */
8324         break;
8325
8326       default:
8327         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8328                    dwarf_type_encoding_name (encoding));
8329         break;
8330     }
8331
8332   type = init_type (code, size, type_flags, NULL, objfile);
8333   TYPE_NAME (type) = name;
8334   TYPE_TARGET_TYPE (type) = target_type;
8335
8336   if (name && strcmp (name, "char") == 0)
8337     TYPE_NOSIGN (type) = 1;
8338
8339   return set_die_type (die, type, cu);
8340 }
8341
8342 /* Read the given DW_AT_subrange DIE.  */
8343
8344 static struct type *
8345 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8346 {
8347   struct type *base_type;
8348   struct type *range_type;
8349   struct attribute *attr;
8350   LONGEST low = 0;
8351   LONGEST high = -1;
8352   char *name;
8353   LONGEST negative_mask;
8354
8355   base_type = die_type (die, cu);
8356   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8357   check_typedef (base_type);
8358
8359   /* The die_type call above may have already set the type for this DIE.  */
8360   range_type = get_die_type (die, cu);
8361   if (range_type)
8362     return range_type;
8363
8364   if (cu->language == language_fortran)
8365     {
8366       /* FORTRAN implies a lower bound of 1, if not given.  */
8367       low = 1;
8368     }
8369
8370   /* FIXME: For variable sized arrays either of these could be
8371      a variable rather than a constant value.  We'll allow it,
8372      but we don't know how to handle it.  */
8373   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8374   if (attr)
8375     low = dwarf2_get_attr_constant_value (attr, 0);
8376
8377   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8378   if (attr)
8379     {
8380       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8381         {
8382           /* GCC encodes arrays with unspecified or dynamic length
8383              with a DW_FORM_block1 attribute or a reference attribute.
8384              FIXME: GDB does not yet know how to handle dynamic
8385              arrays properly, treat them as arrays with unspecified
8386              length for now.
8387
8388              FIXME: jimb/2003-09-22: GDB does not really know
8389              how to handle arrays of unspecified length
8390              either; we just represent them as zero-length
8391              arrays.  Choose an appropriate upper bound given
8392              the lower bound we've computed above.  */
8393           high = low - 1;
8394         }
8395       else
8396         high = dwarf2_get_attr_constant_value (attr, 1);
8397     }
8398   else
8399     {
8400       attr = dwarf2_attr (die, DW_AT_count, cu);
8401       if (attr)
8402         {
8403           int count = dwarf2_get_attr_constant_value (attr, 1);
8404           high = low + count - 1;
8405         }
8406       else
8407         {
8408           /* Unspecified array length.  */
8409           high = low - 1;
8410         }
8411     }
8412
8413   /* Dwarf-2 specifications explicitly allows to create subrange types
8414      without specifying a base type.
8415      In that case, the base type must be set to the type of
8416      the lower bound, upper bound or count, in that order, if any of these
8417      three attributes references an object that has a type.
8418      If no base type is found, the Dwarf-2 specifications say that
8419      a signed integer type of size equal to the size of an address should
8420      be used.
8421      For the following C code: `extern char gdb_int [];'
8422      GCC produces an empty range DIE.
8423      FIXME: muller/2010-05-28: Possible references to object for low bound,
8424      high bound or count are not yet handled by this code.  */
8425   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8426     {
8427       struct objfile *objfile = cu->objfile;
8428       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8429       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8430       struct type *int_type = objfile_type (objfile)->builtin_int;
8431
8432       /* Test "int", "long int", and "long long int" objfile types,
8433          and select the first one having a size above or equal to the
8434          architecture address size.  */
8435       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8436         base_type = int_type;
8437       else
8438         {
8439           int_type = objfile_type (objfile)->builtin_long;
8440           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8441             base_type = int_type;
8442           else
8443             {
8444               int_type = objfile_type (objfile)->builtin_long_long;
8445               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8446                 base_type = int_type;
8447             }
8448         }
8449     }
8450
8451   negative_mask =
8452     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8453   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8454     low |= negative_mask;
8455   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8456     high |= negative_mask;
8457
8458   range_type = create_range_type (NULL, base_type, low, high);
8459
8460   /* Mark arrays with dynamic length at least as an array of unspecified
8461      length.  GDB could check the boundary but before it gets implemented at
8462      least allow accessing the array elements.  */
8463   if (attr && attr->form == DW_FORM_block1)
8464     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8465
8466   /* Ada expects an empty array on no boundary attributes.  */
8467   if (attr == NULL && cu->language != language_ada)
8468     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8469
8470   name = dwarf2_name (die, cu);
8471   if (name)
8472     TYPE_NAME (range_type) = name;
8473
8474   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8475   if (attr)
8476     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8477
8478   set_die_type (die, range_type, cu);
8479
8480   /* set_die_type should be already done.  */
8481   set_descriptive_type (range_type, die, cu);
8482
8483   return range_type;
8484 }
8485
8486 static struct type *
8487 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8488 {
8489   struct type *type;
8490
8491   /* For now, we only support the C meaning of an unspecified type: void.  */
8492
8493   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8494   TYPE_NAME (type) = dwarf2_name (die, cu);
8495
8496   return set_die_type (die, type, cu);
8497 }
8498
8499 /* Trivial hash function for die_info: the hash value of a DIE
8500    is its offset in .debug_info for this objfile.  */
8501
8502 static hashval_t
8503 die_hash (const void *item)
8504 {
8505   const struct die_info *die = item;
8506
8507   return die->offset;
8508 }
8509
8510 /* Trivial comparison function for die_info structures: two DIEs
8511    are equal if they have the same offset.  */
8512
8513 static int
8514 die_eq (const void *item_lhs, const void *item_rhs)
8515 {
8516   const struct die_info *die_lhs = item_lhs;
8517   const struct die_info *die_rhs = item_rhs;
8518
8519   return die_lhs->offset == die_rhs->offset;
8520 }
8521
8522 /* Read a whole compilation unit into a linked list of dies.  */
8523
8524 static struct die_info *
8525 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8526 {
8527   struct die_reader_specs reader_specs;
8528   int read_abbrevs = 0;
8529   struct cleanup *back_to = NULL;
8530   struct die_info *die;
8531
8532   if (cu->dwarf2_abbrevs == NULL)
8533     {
8534       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8535       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8536       read_abbrevs = 1;
8537     }
8538
8539   gdb_assert (cu->die_hash == NULL);
8540   cu->die_hash
8541     = htab_create_alloc_ex (cu->header.length / 12,
8542                             die_hash,
8543                             die_eq,
8544                             NULL,
8545                             &cu->comp_unit_obstack,
8546                             hashtab_obstack_allocate,
8547                             dummy_obstack_deallocate);
8548
8549   init_cu_die_reader (&reader_specs, cu);
8550
8551   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8552
8553   if (read_abbrevs)
8554     do_cleanups (back_to);
8555
8556   return die;
8557 }
8558
8559 /* Main entry point for reading a DIE and all children.
8560    Read the DIE and dump it if requested.  */
8561
8562 static struct die_info *
8563 read_die_and_children (const struct die_reader_specs *reader,
8564                        gdb_byte *info_ptr,
8565                        gdb_byte **new_info_ptr,
8566                        struct die_info *parent)
8567 {
8568   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8569                                                      new_info_ptr, parent);
8570
8571   if (dwarf2_die_debug)
8572     {
8573       fprintf_unfiltered (gdb_stdlog,
8574                           "\nRead die from %s of %s:\n",
8575                           reader->buffer == dwarf2_per_objfile->info.buffer
8576                           ? ".debug_info"
8577                           : reader->buffer == dwarf2_per_objfile->types.buffer
8578                           ? ".debug_types"
8579                           : "unknown section",
8580                           reader->abfd->filename);
8581       dump_die (result, dwarf2_die_debug);
8582     }
8583
8584   return result;
8585 }
8586
8587 /* Read a single die and all its descendents.  Set the die's sibling
8588    field to NULL; set other fields in the die correctly, and set all
8589    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8590    location of the info_ptr after reading all of those dies.  PARENT
8591    is the parent of the die in question.  */
8592
8593 static struct die_info *
8594 read_die_and_children_1 (const struct die_reader_specs *reader,
8595                          gdb_byte *info_ptr,
8596                          gdb_byte **new_info_ptr,
8597                          struct die_info *parent)
8598 {
8599   struct die_info *die;
8600   gdb_byte *cur_ptr;
8601   int has_children;
8602
8603   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8604   if (die == NULL)
8605     {
8606       *new_info_ptr = cur_ptr;
8607       return NULL;
8608     }
8609   store_in_ref_table (die, reader->cu);
8610
8611   if (has_children)
8612     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8613   else
8614     {
8615       die->child = NULL;
8616       *new_info_ptr = cur_ptr;
8617     }
8618
8619   die->sibling = NULL;
8620   die->parent = parent;
8621   return die;
8622 }
8623
8624 /* Read a die, all of its descendents, and all of its siblings; set
8625    all of the fields of all of the dies correctly.  Arguments are as
8626    in read_die_and_children.  */
8627
8628 static struct die_info *
8629 read_die_and_siblings (const struct die_reader_specs *reader,
8630                        gdb_byte *info_ptr,
8631                        gdb_byte **new_info_ptr,
8632                        struct die_info *parent)
8633 {
8634   struct die_info *first_die, *last_sibling;
8635   gdb_byte *cur_ptr;
8636
8637   cur_ptr = info_ptr;
8638   first_die = last_sibling = NULL;
8639
8640   while (1)
8641     {
8642       struct die_info *die
8643         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8644
8645       if (die == NULL)
8646         {
8647           *new_info_ptr = cur_ptr;
8648           return first_die;
8649         }
8650
8651       if (!first_die)
8652         first_die = die;
8653       else
8654         last_sibling->sibling = die;
8655
8656       last_sibling = die;
8657     }
8658 }
8659
8660 /* Read the die from the .debug_info section buffer.  Set DIEP to
8661    point to a newly allocated die with its information, except for its
8662    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8663    whether the die has children or not.  */
8664
8665 static gdb_byte *
8666 read_full_die (const struct die_reader_specs *reader,
8667                struct die_info **diep, gdb_byte *info_ptr,
8668                int *has_children)
8669 {
8670   unsigned int abbrev_number, bytes_read, i, offset;
8671   struct abbrev_info *abbrev;
8672   struct die_info *die;
8673   struct dwarf2_cu *cu = reader->cu;
8674   bfd *abfd = reader->abfd;
8675
8676   offset = info_ptr - reader->buffer;
8677   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8678   info_ptr += bytes_read;
8679   if (!abbrev_number)
8680     {
8681       *diep = NULL;
8682       *has_children = 0;
8683       return info_ptr;
8684     }
8685
8686   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8687   if (!abbrev)
8688     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8689            abbrev_number,
8690            bfd_get_filename (abfd));
8691
8692   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8693   die->offset = offset;
8694   die->tag = abbrev->tag;
8695   die->abbrev = abbrev_number;
8696
8697   die->num_attrs = abbrev->num_attrs;
8698
8699   for (i = 0; i < abbrev->num_attrs; ++i)
8700     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8701                                abfd, info_ptr, cu);
8702
8703   *diep = die;
8704   *has_children = abbrev->has_children;
8705   return info_ptr;
8706 }
8707
8708 /* In DWARF version 2, the description of the debugging information is
8709    stored in a separate .debug_abbrev section.  Before we read any
8710    dies from a section we read in all abbreviations and install them
8711    in a hash table.  This function also sets flags in CU describing
8712    the data found in the abbrev table.  */
8713
8714 static void
8715 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8716 {
8717   struct comp_unit_head *cu_header = &cu->header;
8718   gdb_byte *abbrev_ptr;
8719   struct abbrev_info *cur_abbrev;
8720   unsigned int abbrev_number, bytes_read, abbrev_name;
8721   unsigned int abbrev_form, hash_number;
8722   struct attr_abbrev *cur_attrs;
8723   unsigned int allocated_attrs;
8724
8725   /* Initialize dwarf2 abbrevs.  */
8726   obstack_init (&cu->abbrev_obstack);
8727   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8728                                       (ABBREV_HASH_SIZE
8729                                        * sizeof (struct abbrev_info *)));
8730   memset (cu->dwarf2_abbrevs, 0,
8731           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8732
8733   dwarf2_read_section (dwarf2_per_objfile->objfile,
8734                        &dwarf2_per_objfile->abbrev);
8735   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8736   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8737   abbrev_ptr += bytes_read;
8738
8739   allocated_attrs = ATTR_ALLOC_CHUNK;
8740   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8741
8742   /* Loop until we reach an abbrev number of 0.  */
8743   while (abbrev_number)
8744     {
8745       cur_abbrev = dwarf_alloc_abbrev (cu);
8746
8747       /* read in abbrev header */
8748       cur_abbrev->number = abbrev_number;
8749       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8750       abbrev_ptr += bytes_read;
8751       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8752       abbrev_ptr += 1;
8753
8754       if (cur_abbrev->tag == DW_TAG_namespace)
8755         cu->has_namespace_info = 1;
8756
8757       /* now read in declarations */
8758       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8759       abbrev_ptr += bytes_read;
8760       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8761       abbrev_ptr += bytes_read;
8762       while (abbrev_name)
8763         {
8764           if (cur_abbrev->num_attrs == allocated_attrs)
8765             {
8766               allocated_attrs += ATTR_ALLOC_CHUNK;
8767               cur_attrs
8768                 = xrealloc (cur_attrs, (allocated_attrs
8769                                         * sizeof (struct attr_abbrev)));
8770             }
8771
8772           /* Record whether this compilation unit might have
8773              inter-compilation-unit references.  If we don't know what form
8774              this attribute will have, then it might potentially be a
8775              DW_FORM_ref_addr, so we conservatively expect inter-CU
8776              references.  */
8777
8778           if (abbrev_form == DW_FORM_ref_addr
8779               || abbrev_form == DW_FORM_indirect)
8780             cu->has_form_ref_addr = 1;
8781
8782           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8783           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8784           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8785           abbrev_ptr += bytes_read;
8786           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8787           abbrev_ptr += bytes_read;
8788         }
8789
8790       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8791                                          (cur_abbrev->num_attrs
8792                                           * sizeof (struct attr_abbrev)));
8793       memcpy (cur_abbrev->attrs, cur_attrs,
8794               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8795
8796       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8797       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8798       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8799
8800       /* Get next abbreviation.
8801          Under Irix6 the abbreviations for a compilation unit are not
8802          always properly terminated with an abbrev number of 0.
8803          Exit loop if we encounter an abbreviation which we have
8804          already read (which means we are about to read the abbreviations
8805          for the next compile unit) or if the end of the abbreviation
8806          table is reached.  */
8807       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8808           >= dwarf2_per_objfile->abbrev.size)
8809         break;
8810       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8811       abbrev_ptr += bytes_read;
8812       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8813         break;
8814     }
8815
8816   xfree (cur_attrs);
8817 }
8818
8819 /* Release the memory used by the abbrev table for a compilation unit.  */
8820
8821 static void
8822 dwarf2_free_abbrev_table (void *ptr_to_cu)
8823 {
8824   struct dwarf2_cu *cu = ptr_to_cu;
8825
8826   obstack_free (&cu->abbrev_obstack, NULL);
8827   cu->dwarf2_abbrevs = NULL;
8828 }
8829
8830 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8831
8832 static struct abbrev_info *
8833 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8834 {
8835   unsigned int hash_number;
8836   struct abbrev_info *abbrev;
8837
8838   hash_number = number % ABBREV_HASH_SIZE;
8839   abbrev = cu->dwarf2_abbrevs[hash_number];
8840
8841   while (abbrev)
8842     {
8843       if (abbrev->number == number)
8844         return abbrev;
8845       else
8846         abbrev = abbrev->next;
8847     }
8848   return NULL;
8849 }
8850
8851 /* Returns nonzero if TAG represents a type that we might generate a partial
8852    symbol for.  */
8853
8854 static int
8855 is_type_tag_for_partial (int tag)
8856 {
8857   switch (tag)
8858     {
8859 #if 0
8860     /* Some types that would be reasonable to generate partial symbols for,
8861        that we don't at present.  */
8862     case DW_TAG_array_type:
8863     case DW_TAG_file_type:
8864     case DW_TAG_ptr_to_member_type:
8865     case DW_TAG_set_type:
8866     case DW_TAG_string_type:
8867     case DW_TAG_subroutine_type:
8868 #endif
8869     case DW_TAG_base_type:
8870     case DW_TAG_class_type:
8871     case DW_TAG_interface_type:
8872     case DW_TAG_enumeration_type:
8873     case DW_TAG_structure_type:
8874     case DW_TAG_subrange_type:
8875     case DW_TAG_typedef:
8876     case DW_TAG_union_type:
8877       return 1;
8878     default:
8879       return 0;
8880     }
8881 }
8882
8883 /* Load all DIEs that are interesting for partial symbols into memory.  */
8884
8885 static struct partial_die_info *
8886 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8887                    int building_psymtab, struct dwarf2_cu *cu)
8888 {
8889   struct partial_die_info *part_die;
8890   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8891   struct abbrev_info *abbrev;
8892   unsigned int bytes_read;
8893   unsigned int load_all = 0;
8894
8895   int nesting_level = 1;
8896
8897   parent_die = NULL;
8898   last_die = NULL;
8899
8900   if (cu->per_cu && cu->per_cu->load_all_dies)
8901     load_all = 1;
8902
8903   cu->partial_dies
8904     = htab_create_alloc_ex (cu->header.length / 12,
8905                             partial_die_hash,
8906                             partial_die_eq,
8907                             NULL,
8908                             &cu->comp_unit_obstack,
8909                             hashtab_obstack_allocate,
8910                             dummy_obstack_deallocate);
8911
8912   part_die = obstack_alloc (&cu->comp_unit_obstack,
8913                             sizeof (struct partial_die_info));
8914
8915   while (1)
8916     {
8917       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8918
8919       /* A NULL abbrev means the end of a series of children.  */
8920       if (abbrev == NULL)
8921         {
8922           if (--nesting_level == 0)
8923             {
8924               /* PART_DIE was probably the last thing allocated on the
8925                  comp_unit_obstack, so we could call obstack_free
8926                  here.  We don't do that because the waste is small,
8927                  and will be cleaned up when we're done with this
8928                  compilation unit.  This way, we're also more robust
8929                  against other users of the comp_unit_obstack.  */
8930               return first_die;
8931             }
8932           info_ptr += bytes_read;
8933           last_die = parent_die;
8934           parent_die = parent_die->die_parent;
8935           continue;
8936         }
8937
8938       /* Check for template arguments.  We never save these; if
8939          they're seen, we just mark the parent, and go on our way.  */
8940       if (parent_die != NULL
8941           && cu->language == language_cplus
8942           && (abbrev->tag == DW_TAG_template_type_param
8943               || abbrev->tag == DW_TAG_template_value_param))
8944         {
8945           parent_die->has_template_arguments = 1;
8946
8947           if (!load_all)
8948             {
8949               /* We don't need a partial DIE for the template argument.  */
8950               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8951                                        cu);
8952               continue;
8953             }
8954         }
8955
8956       /* We only recurse into subprograms looking for template arguments.
8957          Skip their other children.  */
8958       if (!load_all
8959           && cu->language == language_cplus
8960           && parent_die != NULL
8961           && parent_die->tag == DW_TAG_subprogram)
8962         {
8963           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8964           continue;
8965         }
8966
8967       /* Check whether this DIE is interesting enough to save.  Normally
8968          we would not be interested in members here, but there may be
8969          later variables referencing them via DW_AT_specification (for
8970          static members).  */
8971       if (!load_all
8972           && !is_type_tag_for_partial (abbrev->tag)
8973           && abbrev->tag != DW_TAG_constant
8974           && abbrev->tag != DW_TAG_enumerator
8975           && abbrev->tag != DW_TAG_subprogram
8976           && abbrev->tag != DW_TAG_lexical_block
8977           && abbrev->tag != DW_TAG_variable
8978           && abbrev->tag != DW_TAG_namespace
8979           && abbrev->tag != DW_TAG_module
8980           && abbrev->tag != DW_TAG_member)
8981         {
8982           /* Otherwise we skip to the next sibling, if any.  */
8983           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8984           continue;
8985         }
8986
8987       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8988                                    buffer, info_ptr, cu);
8989
8990       /* This two-pass algorithm for processing partial symbols has a
8991          high cost in cache pressure.  Thus, handle some simple cases
8992          here which cover the majority of C partial symbols.  DIEs
8993          which neither have specification tags in them, nor could have
8994          specification tags elsewhere pointing at them, can simply be
8995          processed and discarded.
8996
8997          This segment is also optional; scan_partial_symbols and
8998          add_partial_symbol will handle these DIEs if we chain
8999          them in normally.  When compilers which do not emit large
9000          quantities of duplicate debug information are more common,
9001          this code can probably be removed.  */
9002
9003       /* Any complete simple types at the top level (pretty much all
9004          of them, for a language without namespaces), can be processed
9005          directly.  */
9006       if (parent_die == NULL
9007           && part_die->has_specification == 0
9008           && part_die->is_declaration == 0
9009           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9010               || part_die->tag == DW_TAG_base_type
9011               || part_die->tag == DW_TAG_subrange_type))
9012         {
9013           if (building_psymtab && part_die->name != NULL)
9014             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9015                                  VAR_DOMAIN, LOC_TYPEDEF,
9016                                  &cu->objfile->static_psymbols,
9017                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9018           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9019           continue;
9020         }
9021
9022       /* The exception for DW_TAG_typedef with has_children above is
9023          a workaround of GCC PR debug/47510.  In the case of this complaint
9024          type_name_no_tag_or_error will error on such types later.
9025
9026          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9027          it could not find the child DIEs referenced later, this is checked
9028          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9029
9030       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9031         complaint (&symfile_complaints,
9032                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9033                      "- DIE at 0x%x [in module %s]"),
9034                    part_die->offset, cu->objfile->name);
9035
9036       /* If we're at the second level, and we're an enumerator, and
9037          our parent has no specification (meaning possibly lives in a
9038          namespace elsewhere), then we can add the partial symbol now
9039          instead of queueing it.  */
9040       if (part_die->tag == DW_TAG_enumerator
9041           && parent_die != NULL
9042           && parent_die->die_parent == NULL
9043           && parent_die->tag == DW_TAG_enumeration_type
9044           && parent_die->has_specification == 0)
9045         {
9046           if (part_die->name == NULL)
9047             complaint (&symfile_complaints,
9048                        _("malformed enumerator DIE ignored"));
9049           else if (building_psymtab)
9050             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9051                                  VAR_DOMAIN, LOC_CONST,
9052                                  (cu->language == language_cplus
9053                                   || cu->language == language_java)
9054                                  ? &cu->objfile->global_psymbols
9055                                  : &cu->objfile->static_psymbols,
9056                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9057
9058           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9059           continue;
9060         }
9061
9062       /* We'll save this DIE so link it in.  */
9063       part_die->die_parent = parent_die;
9064       part_die->die_sibling = NULL;
9065       part_die->die_child = NULL;
9066
9067       if (last_die && last_die == parent_die)
9068         last_die->die_child = part_die;
9069       else if (last_die)
9070         last_die->die_sibling = part_die;
9071
9072       last_die = part_die;
9073
9074       if (first_die == NULL)
9075         first_die = part_die;
9076
9077       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9078          find interesting need to be in the hash table, because we
9079          also have the parent/sibling/child chains; only those that we
9080          might refer to by offset later during partial symbol reading.
9081
9082          For now this means things that might have be the target of a
9083          DW_AT_specification, DW_AT_abstract_origin, or
9084          DW_AT_extension.  DW_AT_extension will refer only to
9085          namespaces; DW_AT_abstract_origin refers to functions (and
9086          many things under the function DIE, but we do not recurse
9087          into function DIEs during partial symbol reading) and
9088          possibly variables as well; DW_AT_specification refers to
9089          declarations.  Declarations ought to have the DW_AT_declaration
9090          flag.  It happens that GCC forgets to put it in sometimes, but
9091          only for functions, not for types.
9092
9093          Adding more things than necessary to the hash table is harmless
9094          except for the performance cost.  Adding too few will result in
9095          wasted time in find_partial_die, when we reread the compilation
9096          unit with load_all_dies set.  */
9097
9098       if (load_all
9099           || abbrev->tag == DW_TAG_constant
9100           || abbrev->tag == DW_TAG_subprogram
9101           || abbrev->tag == DW_TAG_variable
9102           || abbrev->tag == DW_TAG_namespace
9103           || part_die->is_declaration)
9104         {
9105           void **slot;
9106
9107           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9108                                            part_die->offset, INSERT);
9109           *slot = part_die;
9110         }
9111
9112       part_die = obstack_alloc (&cu->comp_unit_obstack,
9113                                 sizeof (struct partial_die_info));
9114
9115       /* For some DIEs we want to follow their children (if any).  For C
9116          we have no reason to follow the children of structures; for other
9117          languages we have to, so that we can get at method physnames
9118          to infer fully qualified class names, for DW_AT_specification,
9119          and for C++ template arguments.  For C++, we also look one level
9120          inside functions to find template arguments (if the name of the
9121          function does not already contain the template arguments).
9122
9123          For Ada, we need to scan the children of subprograms and lexical
9124          blocks as well because Ada allows the definition of nested
9125          entities that could be interesting for the debugger, such as
9126          nested subprograms for instance.  */
9127       if (last_die->has_children
9128           && (load_all
9129               || last_die->tag == DW_TAG_namespace
9130               || last_die->tag == DW_TAG_module
9131               || last_die->tag == DW_TAG_enumeration_type
9132               || (cu->language == language_cplus
9133                   && last_die->tag == DW_TAG_subprogram
9134                   && (last_die->name == NULL
9135                       || strchr (last_die->name, '<') == NULL))
9136               || (cu->language != language_c
9137                   && (last_die->tag == DW_TAG_class_type
9138                       || last_die->tag == DW_TAG_interface_type
9139                       || last_die->tag == DW_TAG_structure_type
9140                       || last_die->tag == DW_TAG_union_type))
9141               || (cu->language == language_ada
9142                   && (last_die->tag == DW_TAG_subprogram
9143                       || last_die->tag == DW_TAG_lexical_block))))
9144         {
9145           nesting_level++;
9146           parent_die = last_die;
9147           continue;
9148         }
9149
9150       /* Otherwise we skip to the next sibling, if any.  */
9151       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9152
9153       /* Back to the top, do it again.  */
9154     }
9155 }
9156
9157 /* Read a minimal amount of information into the minimal die structure.  */
9158
9159 static gdb_byte *
9160 read_partial_die (struct partial_die_info *part_die,
9161                   struct abbrev_info *abbrev,
9162                   unsigned int abbrev_len, bfd *abfd,
9163                   gdb_byte *buffer, gdb_byte *info_ptr,
9164                   struct dwarf2_cu *cu)
9165 {
9166   unsigned int i;
9167   struct attribute attr;
9168   int has_low_pc_attr = 0;
9169   int has_high_pc_attr = 0;
9170
9171   memset (part_die, 0, sizeof (struct partial_die_info));
9172
9173   part_die->offset = info_ptr - buffer;
9174
9175   info_ptr += abbrev_len;
9176
9177   if (abbrev == NULL)
9178     return info_ptr;
9179
9180   part_die->tag = abbrev->tag;
9181   part_die->has_children = abbrev->has_children;
9182
9183   for (i = 0; i < abbrev->num_attrs; ++i)
9184     {
9185       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9186
9187       /* Store the data if it is of an attribute we want to keep in a
9188          partial symbol table.  */
9189       switch (attr.name)
9190         {
9191         case DW_AT_name:
9192           switch (part_die->tag)
9193             {
9194             case DW_TAG_compile_unit:
9195             case DW_TAG_type_unit:
9196               /* Compilation units have a DW_AT_name that is a filename, not
9197                  a source language identifier.  */
9198             case DW_TAG_enumeration_type:
9199             case DW_TAG_enumerator:
9200               /* These tags always have simple identifiers already; no need
9201                  to canonicalize them.  */
9202               part_die->name = DW_STRING (&attr);
9203               break;
9204             default:
9205               part_die->name
9206                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9207                                             &cu->objfile->objfile_obstack);
9208               break;
9209             }
9210           break;
9211         case DW_AT_linkage_name:
9212         case DW_AT_MIPS_linkage_name:
9213           /* Note that both forms of linkage name might appear.  We
9214              assume they will be the same, and we only store the last
9215              one we see.  */
9216           if (cu->language == language_ada)
9217             part_die->name = DW_STRING (&attr);
9218           part_die->linkage_name = DW_STRING (&attr);
9219           break;
9220         case DW_AT_low_pc:
9221           has_low_pc_attr = 1;
9222           part_die->lowpc = DW_ADDR (&attr);
9223           break;
9224         case DW_AT_high_pc:
9225           has_high_pc_attr = 1;
9226           part_die->highpc = DW_ADDR (&attr);
9227           break;
9228         case DW_AT_location:
9229           /* Support the .debug_loc offsets.  */
9230           if (attr_form_is_block (&attr))
9231             {
9232                part_die->locdesc = DW_BLOCK (&attr);
9233             }
9234           else if (attr_form_is_section_offset (&attr))
9235             {
9236               dwarf2_complex_location_expr_complaint ();
9237             }
9238           else
9239             {
9240               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9241                                                      "partial symbol information");
9242             }
9243           break;
9244         case DW_AT_external:
9245           part_die->is_external = DW_UNSND (&attr);
9246           break;
9247         case DW_AT_declaration:
9248           part_die->is_declaration = DW_UNSND (&attr);
9249           break;
9250         case DW_AT_type:
9251           part_die->has_type = 1;
9252           break;
9253         case DW_AT_abstract_origin:
9254         case DW_AT_specification:
9255         case DW_AT_extension:
9256           part_die->has_specification = 1;
9257           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9258           break;
9259         case DW_AT_sibling:
9260           /* Ignore absolute siblings, they might point outside of
9261              the current compile unit.  */
9262           if (attr.form == DW_FORM_ref_addr)
9263             complaint (&symfile_complaints,
9264                        _("ignoring absolute DW_AT_sibling"));
9265           else
9266             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9267           break;
9268         case DW_AT_byte_size:
9269           part_die->has_byte_size = 1;
9270           break;
9271         case DW_AT_calling_convention:
9272           /* DWARF doesn't provide a way to identify a program's source-level
9273              entry point.  DW_AT_calling_convention attributes are only meant
9274              to describe functions' calling conventions.
9275
9276              However, because it's a necessary piece of information in
9277              Fortran, and because DW_CC_program is the only piece of debugging
9278              information whose definition refers to a 'main program' at all,
9279              several compilers have begun marking Fortran main programs with
9280              DW_CC_program --- even when those functions use the standard
9281              calling conventions.
9282
9283              So until DWARF specifies a way to provide this information and
9284              compilers pick up the new representation, we'll support this
9285              practice.  */
9286           if (DW_UNSND (&attr) == DW_CC_program
9287               && cu->language == language_fortran)
9288             {
9289               set_main_name (part_die->name);
9290
9291               /* As this DIE has a static linkage the name would be difficult
9292                  to look up later.  */
9293               language_of_main = language_fortran;
9294             }
9295           break;
9296         default:
9297           break;
9298         }
9299     }
9300
9301   if (has_low_pc_attr && has_high_pc_attr)
9302     {
9303       /* When using the GNU linker, .gnu.linkonce. sections are used to
9304          eliminate duplicate copies of functions and vtables and such.
9305          The linker will arbitrarily choose one and discard the others.
9306          The AT_*_pc values for such functions refer to local labels in
9307          these sections.  If the section from that file was discarded, the
9308          labels are not in the output, so the relocs get a value of 0.
9309          If this is a discarded function, mark the pc bounds as invalid,
9310          so that GDB will ignore it.  */
9311       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9312         {
9313           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9314
9315           complaint (&symfile_complaints,
9316                      _("DW_AT_low_pc %s is zero "
9317                        "for DIE at 0x%x [in module %s]"),
9318                      paddress (gdbarch, part_die->lowpc),
9319                      part_die->offset, cu->objfile->name);
9320         }
9321       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9322       else if (part_die->lowpc >= part_die->highpc)
9323         {
9324           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9325
9326           complaint (&symfile_complaints,
9327                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9328                        "for DIE at 0x%x [in module %s]"),
9329                      paddress (gdbarch, part_die->lowpc),
9330                      paddress (gdbarch, part_die->highpc),
9331                      part_die->offset, cu->objfile->name);
9332         }
9333       else
9334         part_die->has_pc_info = 1;
9335     }
9336
9337   return info_ptr;
9338 }
9339
9340 /* Find a cached partial DIE at OFFSET in CU.  */
9341
9342 static struct partial_die_info *
9343 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9344 {
9345   struct partial_die_info *lookup_die = NULL;
9346   struct partial_die_info part_die;
9347
9348   part_die.offset = offset;
9349   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9350
9351   return lookup_die;
9352 }
9353
9354 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9355    except in the case of .debug_types DIEs which do not reference
9356    outside their CU (they do however referencing other types via
9357    DW_FORM_ref_sig8).  */
9358
9359 static struct partial_die_info *
9360 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9361 {
9362   struct dwarf2_per_cu_data *per_cu = NULL;
9363   struct partial_die_info *pd = NULL;
9364
9365   if (cu->per_cu->from_debug_types)
9366     {
9367       pd = find_partial_die_in_comp_unit (offset, cu);
9368       if (pd != NULL)
9369         return pd;
9370       goto not_found;
9371     }
9372
9373   if (offset_in_cu_p (&cu->header, offset))
9374     {
9375       pd = find_partial_die_in_comp_unit (offset, cu);
9376       if (pd != NULL)
9377         return pd;
9378     }
9379
9380   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9381
9382   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9383     load_partial_comp_unit (per_cu, cu->objfile);
9384
9385   per_cu->cu->last_used = 0;
9386   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9387
9388   if (pd == NULL && per_cu->load_all_dies == 0)
9389     {
9390       struct cleanup *back_to;
9391       struct partial_die_info comp_unit_die;
9392       struct abbrev_info *abbrev;
9393       unsigned int bytes_read;
9394       char *info_ptr;
9395
9396       per_cu->load_all_dies = 1;
9397
9398       /* Re-read the DIEs.  */
9399       back_to = make_cleanup (null_cleanup, 0);
9400       if (per_cu->cu->dwarf2_abbrevs == NULL)
9401         {
9402           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9403           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9404         }
9405       info_ptr = (dwarf2_per_objfile->info.buffer
9406                   + per_cu->cu->header.offset
9407                   + per_cu->cu->header.first_die_offset);
9408       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9409       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9410                                    per_cu->cu->objfile->obfd,
9411                                    dwarf2_per_objfile->info.buffer, info_ptr,
9412                                    per_cu->cu);
9413       if (comp_unit_die.has_children)
9414         load_partial_dies (per_cu->cu->objfile->obfd,
9415                            dwarf2_per_objfile->info.buffer, info_ptr,
9416                            0, per_cu->cu);
9417       do_cleanups (back_to);
9418
9419       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9420     }
9421
9422  not_found:
9423
9424   if (pd == NULL)
9425     internal_error (__FILE__, __LINE__,
9426                     _("could not find partial DIE 0x%x "
9427                       "in cache [from module %s]\n"),
9428                     offset, bfd_get_filename (cu->objfile->obfd));
9429   return pd;
9430 }
9431
9432 /* See if we can figure out if the class lives in a namespace.  We do
9433    this by looking for a member function; its demangled name will
9434    contain namespace info, if there is any.  */
9435
9436 static void
9437 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9438                                   struct dwarf2_cu *cu)
9439 {
9440   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9441      what template types look like, because the demangler
9442      frequently doesn't give the same name as the debug info.  We
9443      could fix this by only using the demangled name to get the
9444      prefix (but see comment in read_structure_type).  */
9445
9446   struct partial_die_info *real_pdi;
9447   struct partial_die_info *child_pdi;
9448
9449   /* If this DIE (this DIE's specification, if any) has a parent, then
9450      we should not do this.  We'll prepend the parent's fully qualified
9451      name when we create the partial symbol.  */
9452
9453   real_pdi = struct_pdi;
9454   while (real_pdi->has_specification)
9455     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9456
9457   if (real_pdi->die_parent != NULL)
9458     return;
9459
9460   for (child_pdi = struct_pdi->die_child;
9461        child_pdi != NULL;
9462        child_pdi = child_pdi->die_sibling)
9463     {
9464       if (child_pdi->tag == DW_TAG_subprogram
9465           && child_pdi->linkage_name != NULL)
9466         {
9467           char *actual_class_name
9468             = language_class_name_from_physname (cu->language_defn,
9469                                                  child_pdi->linkage_name);
9470           if (actual_class_name != NULL)
9471             {
9472               struct_pdi->name
9473                 = obsavestring (actual_class_name,
9474                                 strlen (actual_class_name),
9475                                 &cu->objfile->objfile_obstack);
9476               xfree (actual_class_name);
9477             }
9478           break;
9479         }
9480     }
9481 }
9482
9483 /* Adjust PART_DIE before generating a symbol for it.  This function
9484    may set the is_external flag or change the DIE's name.  */
9485
9486 static void
9487 fixup_partial_die (struct partial_die_info *part_die,
9488                    struct dwarf2_cu *cu)
9489 {
9490   /* Once we've fixed up a die, there's no point in doing so again.
9491      This also avoids a memory leak if we were to call
9492      guess_partial_die_structure_name multiple times.  */
9493   if (part_die->fixup_called)
9494     return;
9495
9496   /* If we found a reference attribute and the DIE has no name, try
9497      to find a name in the referred to DIE.  */
9498
9499   if (part_die->name == NULL && part_die->has_specification)
9500     {
9501       struct partial_die_info *spec_die;
9502
9503       spec_die = find_partial_die (part_die->spec_offset, cu);
9504
9505       fixup_partial_die (spec_die, cu);
9506
9507       if (spec_die->name)
9508         {
9509           part_die->name = spec_die->name;
9510
9511           /* Copy DW_AT_external attribute if it is set.  */
9512           if (spec_die->is_external)
9513             part_die->is_external = spec_die->is_external;
9514         }
9515     }
9516
9517   /* Set default names for some unnamed DIEs.  */
9518
9519   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9520     part_die->name = "(anonymous namespace)";
9521
9522   /* If there is no parent die to provide a namespace, and there are
9523      children, see if we can determine the namespace from their linkage
9524      name.
9525      NOTE: We need to do this even if cu->has_namespace_info != 0.
9526      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9527   if (cu->language == language_cplus
9528       && dwarf2_per_objfile->types.asection != NULL
9529       && part_die->die_parent == NULL
9530       && part_die->has_children
9531       && (part_die->tag == DW_TAG_class_type
9532           || part_die->tag == DW_TAG_structure_type
9533           || part_die->tag == DW_TAG_union_type))
9534     guess_partial_die_structure_name (part_die, cu);
9535
9536   /* GCC might emit a nameless struct or union that has a linkage
9537      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
9538   if (part_die->name == NULL
9539       && (part_die->tag == DW_TAG_structure_type
9540           || part_die->tag == DW_TAG_union_type
9541           || part_die->tag == DW_TAG_class_type)
9542       && part_die->linkage_name != NULL)
9543     {
9544       char *demangled;
9545
9546       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9547       if (demangled)
9548         {
9549           part_die->name = obsavestring (demangled, strlen (demangled),
9550                                          &cu->objfile->objfile_obstack);
9551           xfree (demangled);
9552         }
9553     }
9554
9555   part_die->fixup_called = 1;
9556 }
9557
9558 /* Read an attribute value described by an attribute form.  */
9559
9560 static gdb_byte *
9561 read_attribute_value (struct attribute *attr, unsigned form,
9562                       bfd *abfd, gdb_byte *info_ptr,
9563                       struct dwarf2_cu *cu)
9564 {
9565   struct comp_unit_head *cu_header = &cu->header;
9566   unsigned int bytes_read;
9567   struct dwarf_block *blk;
9568
9569   attr->form = form;
9570   switch (form)
9571     {
9572     case DW_FORM_ref_addr:
9573       if (cu->header.version == 2)
9574         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9575       else
9576         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9577                                       &cu->header, &bytes_read);
9578       info_ptr += bytes_read;
9579       break;
9580     case DW_FORM_addr:
9581       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9582       info_ptr += bytes_read;
9583       break;
9584     case DW_FORM_block2:
9585       blk = dwarf_alloc_block (cu);
9586       blk->size = read_2_bytes (abfd, info_ptr);
9587       info_ptr += 2;
9588       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9589       info_ptr += blk->size;
9590       DW_BLOCK (attr) = blk;
9591       break;
9592     case DW_FORM_block4:
9593       blk = dwarf_alloc_block (cu);
9594       blk->size = read_4_bytes (abfd, info_ptr);
9595       info_ptr += 4;
9596       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9597       info_ptr += blk->size;
9598       DW_BLOCK (attr) = blk;
9599       break;
9600     case DW_FORM_data2:
9601       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9602       info_ptr += 2;
9603       break;
9604     case DW_FORM_data4:
9605       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9606       info_ptr += 4;
9607       break;
9608     case DW_FORM_data8:
9609       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9610       info_ptr += 8;
9611       break;
9612     case DW_FORM_sec_offset:
9613       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9614       info_ptr += bytes_read;
9615       break;
9616     case DW_FORM_string:
9617       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9618       DW_STRING_IS_CANONICAL (attr) = 0;
9619       info_ptr += bytes_read;
9620       break;
9621     case DW_FORM_strp:
9622       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9623                                                &bytes_read);
9624       DW_STRING_IS_CANONICAL (attr) = 0;
9625       info_ptr += bytes_read;
9626       break;
9627     case DW_FORM_exprloc:
9628     case DW_FORM_block:
9629       blk = dwarf_alloc_block (cu);
9630       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9631       info_ptr += bytes_read;
9632       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9633       info_ptr += blk->size;
9634       DW_BLOCK (attr) = blk;
9635       break;
9636     case DW_FORM_block1:
9637       blk = dwarf_alloc_block (cu);
9638       blk->size = read_1_byte (abfd, info_ptr);
9639       info_ptr += 1;
9640       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9641       info_ptr += blk->size;
9642       DW_BLOCK (attr) = blk;
9643       break;
9644     case DW_FORM_data1:
9645       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9646       info_ptr += 1;
9647       break;
9648     case DW_FORM_flag:
9649       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9650       info_ptr += 1;
9651       break;
9652     case DW_FORM_flag_present:
9653       DW_UNSND (attr) = 1;
9654       break;
9655     case DW_FORM_sdata:
9656       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9657       info_ptr += bytes_read;
9658       break;
9659     case DW_FORM_udata:
9660       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9661       info_ptr += bytes_read;
9662       break;
9663     case DW_FORM_ref1:
9664       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9665       info_ptr += 1;
9666       break;
9667     case DW_FORM_ref2:
9668       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9669       info_ptr += 2;
9670       break;
9671     case DW_FORM_ref4:
9672       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9673       info_ptr += 4;
9674       break;
9675     case DW_FORM_ref8:
9676       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9677       info_ptr += 8;
9678       break;
9679     case DW_FORM_ref_sig8:
9680       /* Convert the signature to something we can record in DW_UNSND
9681          for later lookup.
9682          NOTE: This is NULL if the type wasn't found.  */
9683       DW_SIGNATURED_TYPE (attr) =
9684         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9685       info_ptr += 8;
9686       break;
9687     case DW_FORM_ref_udata:
9688       DW_ADDR (attr) = (cu->header.offset
9689                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9690       info_ptr += bytes_read;
9691       break;
9692     case DW_FORM_indirect:
9693       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9694       info_ptr += bytes_read;
9695       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9696       break;
9697     default:
9698       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9699              dwarf_form_name (form),
9700              bfd_get_filename (abfd));
9701     }
9702
9703   /* We have seen instances where the compiler tried to emit a byte
9704      size attribute of -1 which ended up being encoded as an unsigned
9705      0xffffffff.  Although 0xffffffff is technically a valid size value,
9706      an object of this size seems pretty unlikely so we can relatively
9707      safely treat these cases as if the size attribute was invalid and
9708      treat them as zero by default.  */
9709   if (attr->name == DW_AT_byte_size
9710       && form == DW_FORM_data4
9711       && DW_UNSND (attr) >= 0xffffffff)
9712     {
9713       complaint
9714         (&symfile_complaints,
9715          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9716          hex_string (DW_UNSND (attr)));
9717       DW_UNSND (attr) = 0;
9718     }
9719
9720   return info_ptr;
9721 }
9722
9723 /* Read an attribute described by an abbreviated attribute.  */
9724
9725 static gdb_byte *
9726 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9727                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9728 {
9729   attr->name = abbrev->name;
9730   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9731 }
9732
9733 /* Read dwarf information from a buffer.  */
9734
9735 static unsigned int
9736 read_1_byte (bfd *abfd, gdb_byte *buf)
9737 {
9738   return bfd_get_8 (abfd, buf);
9739 }
9740
9741 static int
9742 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9743 {
9744   return bfd_get_signed_8 (abfd, buf);
9745 }
9746
9747 static unsigned int
9748 read_2_bytes (bfd *abfd, gdb_byte *buf)
9749 {
9750   return bfd_get_16 (abfd, buf);
9751 }
9752
9753 static int
9754 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9755 {
9756   return bfd_get_signed_16 (abfd, buf);
9757 }
9758
9759 static unsigned int
9760 read_4_bytes (bfd *abfd, gdb_byte *buf)
9761 {
9762   return bfd_get_32 (abfd, buf);
9763 }
9764
9765 static int
9766 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9767 {
9768   return bfd_get_signed_32 (abfd, buf);
9769 }
9770
9771 static ULONGEST
9772 read_8_bytes (bfd *abfd, gdb_byte *buf)
9773 {
9774   return bfd_get_64 (abfd, buf);
9775 }
9776
9777 static CORE_ADDR
9778 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9779               unsigned int *bytes_read)
9780 {
9781   struct comp_unit_head *cu_header = &cu->header;
9782   CORE_ADDR retval = 0;
9783
9784   if (cu_header->signed_addr_p)
9785     {
9786       switch (cu_header->addr_size)
9787         {
9788         case 2:
9789           retval = bfd_get_signed_16 (abfd, buf);
9790           break;
9791         case 4:
9792           retval = bfd_get_signed_32 (abfd, buf);
9793           break;
9794         case 8:
9795           retval = bfd_get_signed_64 (abfd, buf);
9796           break;
9797         default:
9798           internal_error (__FILE__, __LINE__,
9799                           _("read_address: bad switch, signed [in module %s]"),
9800                           bfd_get_filename (abfd));
9801         }
9802     }
9803   else
9804     {
9805       switch (cu_header->addr_size)
9806         {
9807         case 2:
9808           retval = bfd_get_16 (abfd, buf);
9809           break;
9810         case 4:
9811           retval = bfd_get_32 (abfd, buf);
9812           break;
9813         case 8:
9814           retval = bfd_get_64 (abfd, buf);
9815           break;
9816         default:
9817           internal_error (__FILE__, __LINE__,
9818                           _("read_address: bad switch, "
9819                             "unsigned [in module %s]"),
9820                           bfd_get_filename (abfd));
9821         }
9822     }
9823
9824   *bytes_read = cu_header->addr_size;
9825   return retval;
9826 }
9827
9828 /* Read the initial length from a section.  The (draft) DWARF 3
9829    specification allows the initial length to take up either 4 bytes
9830    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9831    bytes describe the length and all offsets will be 8 bytes in length
9832    instead of 4.
9833
9834    An older, non-standard 64-bit format is also handled by this
9835    function.  The older format in question stores the initial length
9836    as an 8-byte quantity without an escape value.  Lengths greater
9837    than 2^32 aren't very common which means that the initial 4 bytes
9838    is almost always zero.  Since a length value of zero doesn't make
9839    sense for the 32-bit format, this initial zero can be considered to
9840    be an escape value which indicates the presence of the older 64-bit
9841    format.  As written, the code can't detect (old format) lengths
9842    greater than 4GB.  If it becomes necessary to handle lengths
9843    somewhat larger than 4GB, we could allow other small values (such
9844    as the non-sensical values of 1, 2, and 3) to also be used as
9845    escape values indicating the presence of the old format.
9846
9847    The value returned via bytes_read should be used to increment the
9848    relevant pointer after calling read_initial_length().
9849
9850    [ Note:  read_initial_length() and read_offset() are based on the
9851      document entitled "DWARF Debugging Information Format", revision
9852      3, draft 8, dated November 19, 2001.  This document was obtained
9853      from:
9854
9855         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9856
9857      This document is only a draft and is subject to change.  (So beware.)
9858
9859      Details regarding the older, non-standard 64-bit format were
9860      determined empirically by examining 64-bit ELF files produced by
9861      the SGI toolchain on an IRIX 6.5 machine.
9862
9863      - Kevin, July 16, 2002
9864    ] */
9865
9866 static LONGEST
9867 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9868 {
9869   LONGEST length = bfd_get_32 (abfd, buf);
9870
9871   if (length == 0xffffffff)
9872     {
9873       length = bfd_get_64 (abfd, buf + 4);
9874       *bytes_read = 12;
9875     }
9876   else if (length == 0)
9877     {
9878       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9879       length = bfd_get_64 (abfd, buf);
9880       *bytes_read = 8;
9881     }
9882   else
9883     {
9884       *bytes_read = 4;
9885     }
9886
9887   return length;
9888 }
9889
9890 /* Cover function for read_initial_length.
9891    Returns the length of the object at BUF, and stores the size of the
9892    initial length in *BYTES_READ and stores the size that offsets will be in
9893    *OFFSET_SIZE.
9894    If the initial length size is not equivalent to that specified in
9895    CU_HEADER then issue a complaint.
9896    This is useful when reading non-comp-unit headers.  */
9897
9898 static LONGEST
9899 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9900                                         const struct comp_unit_head *cu_header,
9901                                         unsigned int *bytes_read,
9902                                         unsigned int *offset_size)
9903 {
9904   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9905
9906   gdb_assert (cu_header->initial_length_size == 4
9907               || cu_header->initial_length_size == 8
9908               || cu_header->initial_length_size == 12);
9909
9910   if (cu_header->initial_length_size != *bytes_read)
9911     complaint (&symfile_complaints,
9912                _("intermixed 32-bit and 64-bit DWARF sections"));
9913
9914   *offset_size = (*bytes_read == 4) ? 4 : 8;
9915   return length;
9916 }
9917
9918 /* Read an offset from the data stream.  The size of the offset is
9919    given by cu_header->offset_size.  */
9920
9921 static LONGEST
9922 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9923              unsigned int *bytes_read)
9924 {
9925   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9926
9927   *bytes_read = cu_header->offset_size;
9928   return offset;
9929 }
9930
9931 /* Read an offset from the data stream.  */
9932
9933 static LONGEST
9934 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9935 {
9936   LONGEST retval = 0;
9937
9938   switch (offset_size)
9939     {
9940     case 4:
9941       retval = bfd_get_32 (abfd, buf);
9942       break;
9943     case 8:
9944       retval = bfd_get_64 (abfd, buf);
9945       break;
9946     default:
9947       internal_error (__FILE__, __LINE__,
9948                       _("read_offset_1: bad switch [in module %s]"),
9949                       bfd_get_filename (abfd));
9950     }
9951
9952   return retval;
9953 }
9954
9955 static gdb_byte *
9956 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9957 {
9958   /* If the size of a host char is 8 bits, we can return a pointer
9959      to the buffer, otherwise we have to copy the data to a buffer
9960      allocated on the temporary obstack.  */
9961   gdb_assert (HOST_CHAR_BIT == 8);
9962   return buf;
9963 }
9964
9965 static char *
9966 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9967 {
9968   /* If the size of a host char is 8 bits, we can return a pointer
9969      to the string, otherwise we have to copy the string to a buffer
9970      allocated on the temporary obstack.  */
9971   gdb_assert (HOST_CHAR_BIT == 8);
9972   if (*buf == '\0')
9973     {
9974       *bytes_read_ptr = 1;
9975       return NULL;
9976     }
9977   *bytes_read_ptr = strlen ((char *) buf) + 1;
9978   return (char *) buf;
9979 }
9980
9981 static char *
9982 read_indirect_string (bfd *abfd, gdb_byte *buf,
9983                       const struct comp_unit_head *cu_header,
9984                       unsigned int *bytes_read_ptr)
9985 {
9986   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9987
9988   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9989   if (dwarf2_per_objfile->str.buffer == NULL)
9990     {
9991       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9992                       bfd_get_filename (abfd));
9993       return NULL;
9994     }
9995   if (str_offset >= dwarf2_per_objfile->str.size)
9996     {
9997       error (_("DW_FORM_strp pointing outside of "
9998                ".debug_str section [in module %s]"),
9999              bfd_get_filename (abfd));
10000       return NULL;
10001     }
10002   gdb_assert (HOST_CHAR_BIT == 8);
10003   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10004     return NULL;
10005   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10006 }
10007
10008 static unsigned long
10009 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10010 {
10011   unsigned long result;
10012   unsigned int num_read;
10013   int i, shift;
10014   unsigned char byte;
10015
10016   result = 0;
10017   shift = 0;
10018   num_read = 0;
10019   i = 0;
10020   while (1)
10021     {
10022       byte = bfd_get_8 (abfd, buf);
10023       buf++;
10024       num_read++;
10025       result |= ((unsigned long)(byte & 127) << shift);
10026       if ((byte & 128) == 0)
10027         {
10028           break;
10029         }
10030       shift += 7;
10031     }
10032   *bytes_read_ptr = num_read;
10033   return result;
10034 }
10035
10036 static long
10037 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10038 {
10039   long result;
10040   int i, shift, num_read;
10041   unsigned char byte;
10042
10043   result = 0;
10044   shift = 0;
10045   num_read = 0;
10046   i = 0;
10047   while (1)
10048     {
10049       byte = bfd_get_8 (abfd, buf);
10050       buf++;
10051       num_read++;
10052       result |= ((long)(byte & 127) << shift);
10053       shift += 7;
10054       if ((byte & 128) == 0)
10055         {
10056           break;
10057         }
10058     }
10059   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10060     result |= -(((long)1) << shift);
10061   *bytes_read_ptr = num_read;
10062   return result;
10063 }
10064
10065 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10066
10067 static gdb_byte *
10068 skip_leb128 (bfd *abfd, gdb_byte *buf)
10069 {
10070   int byte;
10071
10072   while (1)
10073     {
10074       byte = bfd_get_8 (abfd, buf);
10075       buf++;
10076       if ((byte & 128) == 0)
10077         return buf;
10078     }
10079 }
10080
10081 static void
10082 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10083 {
10084   switch (lang)
10085     {
10086     case DW_LANG_C89:
10087     case DW_LANG_C99:
10088     case DW_LANG_C:
10089       cu->language = language_c;
10090       break;
10091     case DW_LANG_C_plus_plus:
10092       cu->language = language_cplus;
10093       break;
10094     case DW_LANG_D:
10095       cu->language = language_d;
10096       break;
10097     case DW_LANG_Fortran77:
10098     case DW_LANG_Fortran90:
10099     case DW_LANG_Fortran95:
10100       cu->language = language_fortran;
10101       break;
10102     case DW_LANG_Mips_Assembler:
10103       cu->language = language_asm;
10104       break;
10105     case DW_LANG_Java:
10106       cu->language = language_java;
10107       break;
10108     case DW_LANG_Ada83:
10109     case DW_LANG_Ada95:
10110       cu->language = language_ada;
10111       break;
10112     case DW_LANG_Modula2:
10113       cu->language = language_m2;
10114       break;
10115     case DW_LANG_Pascal83:
10116       cu->language = language_pascal;
10117       break;
10118     case DW_LANG_ObjC:
10119       cu->language = language_objc;
10120       break;
10121     case DW_LANG_Cobol74:
10122     case DW_LANG_Cobol85:
10123     default:
10124       cu->language = language_minimal;
10125       break;
10126     }
10127   cu->language_defn = language_def (cu->language);
10128 }
10129
10130 /* Return the named attribute or NULL if not there.  */
10131
10132 static struct attribute *
10133 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10134 {
10135   unsigned int i;
10136   struct attribute *spec = NULL;
10137
10138   for (i = 0; i < die->num_attrs; ++i)
10139     {
10140       if (die->attrs[i].name == name)
10141         return &die->attrs[i];
10142       if (die->attrs[i].name == DW_AT_specification
10143           || die->attrs[i].name == DW_AT_abstract_origin)
10144         spec = &die->attrs[i];
10145     }
10146
10147   if (spec)
10148     {
10149       die = follow_die_ref (die, spec, &cu);
10150       return dwarf2_attr (die, name, cu);
10151     }
10152
10153   return NULL;
10154 }
10155
10156 /* Return the named attribute or NULL if not there,
10157    but do not follow DW_AT_specification, etc.
10158    This is for use in contexts where we're reading .debug_types dies.
10159    Following DW_AT_specification, DW_AT_abstract_origin will take us
10160    back up the chain, and we want to go down.  */
10161
10162 static struct attribute *
10163 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10164                        struct dwarf2_cu *cu)
10165 {
10166   unsigned int i;
10167
10168   for (i = 0; i < die->num_attrs; ++i)
10169     if (die->attrs[i].name == name)
10170       return &die->attrs[i];
10171
10172   return NULL;
10173 }
10174
10175 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10176    and holds a non-zero value.  This function should only be used for
10177    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10178
10179 static int
10180 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10181 {
10182   struct attribute *attr = dwarf2_attr (die, name, cu);
10183
10184   return (attr && DW_UNSND (attr));
10185 }
10186
10187 static int
10188 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10189 {
10190   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10191      which value is non-zero.  However, we have to be careful with
10192      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10193      (via dwarf2_flag_true_p) follows this attribute.  So we may
10194      end up accidently finding a declaration attribute that belongs
10195      to a different DIE referenced by the specification attribute,
10196      even though the given DIE does not have a declaration attribute.  */
10197   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10198           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10199 }
10200
10201 /* Return the die giving the specification for DIE, if there is
10202    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10203    containing the return value on output.  If there is no
10204    specification, but there is an abstract origin, that is
10205    returned.  */
10206
10207 static struct die_info *
10208 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10209 {
10210   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10211                                              *spec_cu);
10212
10213   if (spec_attr == NULL)
10214     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10215
10216   if (spec_attr == NULL)
10217     return NULL;
10218   else
10219     return follow_die_ref (die, spec_attr, spec_cu);
10220 }
10221
10222 /* Free the line_header structure *LH, and any arrays and strings it
10223    refers to.
10224    NOTE: This is also used as a "cleanup" function.  */
10225
10226 static void
10227 free_line_header (struct line_header *lh)
10228 {
10229   if (lh->standard_opcode_lengths)
10230     xfree (lh->standard_opcode_lengths);
10231
10232   /* Remember that all the lh->file_names[i].name pointers are
10233      pointers into debug_line_buffer, and don't need to be freed.  */
10234   if (lh->file_names)
10235     xfree (lh->file_names);
10236
10237   /* Similarly for the include directory names.  */
10238   if (lh->include_dirs)
10239     xfree (lh->include_dirs);
10240
10241   xfree (lh);
10242 }
10243
10244 /* Add an entry to LH's include directory table.  */
10245
10246 static void
10247 add_include_dir (struct line_header *lh, char *include_dir)
10248 {
10249   /* Grow the array if necessary.  */
10250   if (lh->include_dirs_size == 0)
10251     {
10252       lh->include_dirs_size = 1; /* for testing */
10253       lh->include_dirs = xmalloc (lh->include_dirs_size
10254                                   * sizeof (*lh->include_dirs));
10255     }
10256   else if (lh->num_include_dirs >= lh->include_dirs_size)
10257     {
10258       lh->include_dirs_size *= 2;
10259       lh->include_dirs = xrealloc (lh->include_dirs,
10260                                    (lh->include_dirs_size
10261                                     * sizeof (*lh->include_dirs)));
10262     }
10263
10264   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10265 }
10266
10267 /* Add an entry to LH's file name table.  */
10268
10269 static void
10270 add_file_name (struct line_header *lh,
10271                char *name,
10272                unsigned int dir_index,
10273                unsigned int mod_time,
10274                unsigned int length)
10275 {
10276   struct file_entry *fe;
10277
10278   /* Grow the array if necessary.  */
10279   if (lh->file_names_size == 0)
10280     {
10281       lh->file_names_size = 1; /* for testing */
10282       lh->file_names = xmalloc (lh->file_names_size
10283                                 * sizeof (*lh->file_names));
10284     }
10285   else if (lh->num_file_names >= lh->file_names_size)
10286     {
10287       lh->file_names_size *= 2;
10288       lh->file_names = xrealloc (lh->file_names,
10289                                  (lh->file_names_size
10290                                   * sizeof (*lh->file_names)));
10291     }
10292
10293   fe = &lh->file_names[lh->num_file_names++];
10294   fe->name = name;
10295   fe->dir_index = dir_index;
10296   fe->mod_time = mod_time;
10297   fe->length = length;
10298   fe->included_p = 0;
10299   fe->symtab = NULL;
10300 }
10301
10302 /* Read the statement program header starting at OFFSET in
10303    .debug_line, according to the endianness of ABFD.  Return a pointer
10304    to a struct line_header, allocated using xmalloc.
10305
10306    NOTE: the strings in the include directory and file name tables of
10307    the returned object point into debug_line_buffer, and must not be
10308    freed.  */
10309
10310 static struct line_header *
10311 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10312                           struct dwarf2_cu *cu)
10313 {
10314   struct cleanup *back_to;
10315   struct line_header *lh;
10316   gdb_byte *line_ptr;
10317   unsigned int bytes_read, offset_size;
10318   int i;
10319   char *cur_dir, *cur_file;
10320
10321   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10322   if (dwarf2_per_objfile->line.buffer == NULL)
10323     {
10324       complaint (&symfile_complaints, _("missing .debug_line section"));
10325       return 0;
10326     }
10327
10328   /* Make sure that at least there's room for the total_length field.
10329      That could be 12 bytes long, but we're just going to fudge that.  */
10330   if (offset + 4 >= dwarf2_per_objfile->line.size)
10331     {
10332       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10333       return 0;
10334     }
10335
10336   lh = xmalloc (sizeof (*lh));
10337   memset (lh, 0, sizeof (*lh));
10338   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10339                           (void *) lh);
10340
10341   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10342
10343   /* Read in the header.  */
10344   lh->total_length =
10345     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10346                                             &bytes_read, &offset_size);
10347   line_ptr += bytes_read;
10348   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10349                                      + dwarf2_per_objfile->line.size))
10350     {
10351       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10352       return 0;
10353     }
10354   lh->statement_program_end = line_ptr + lh->total_length;
10355   lh->version = read_2_bytes (abfd, line_ptr);
10356   line_ptr += 2;
10357   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10358   line_ptr += offset_size;
10359   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10360   line_ptr += 1;
10361   if (lh->version >= 4)
10362     {
10363       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10364       line_ptr += 1;
10365     }
10366   else
10367     lh->maximum_ops_per_instruction = 1;
10368
10369   if (lh->maximum_ops_per_instruction == 0)
10370     {
10371       lh->maximum_ops_per_instruction = 1;
10372       complaint (&symfile_complaints,
10373                  _("invalid maximum_ops_per_instruction "
10374                    "in `.debug_line' section"));
10375     }
10376
10377   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10378   line_ptr += 1;
10379   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10380   line_ptr += 1;
10381   lh->line_range = read_1_byte (abfd, line_ptr);
10382   line_ptr += 1;
10383   lh->opcode_base = read_1_byte (abfd, line_ptr);
10384   line_ptr += 1;
10385   lh->standard_opcode_lengths
10386     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10387
10388   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10389   for (i = 1; i < lh->opcode_base; ++i)
10390     {
10391       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10392       line_ptr += 1;
10393     }
10394
10395   /* Read directory table.  */
10396   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10397     {
10398       line_ptr += bytes_read;
10399       add_include_dir (lh, cur_dir);
10400     }
10401   line_ptr += bytes_read;
10402
10403   /* Read file name table.  */
10404   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10405     {
10406       unsigned int dir_index, mod_time, length;
10407
10408       line_ptr += bytes_read;
10409       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10410       line_ptr += bytes_read;
10411       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10412       line_ptr += bytes_read;
10413       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10414       line_ptr += bytes_read;
10415
10416       add_file_name (lh, cur_file, dir_index, mod_time, length);
10417     }
10418   line_ptr += bytes_read;
10419   lh->statement_program_start = line_ptr;
10420
10421   if (line_ptr > (dwarf2_per_objfile->line.buffer
10422                   + dwarf2_per_objfile->line.size))
10423     complaint (&symfile_complaints,
10424                _("line number info header doesn't "
10425                  "fit in `.debug_line' section"));
10426
10427   discard_cleanups (back_to);
10428   return lh;
10429 }
10430
10431 /* This function exists to work around a bug in certain compilers
10432    (particularly GCC 2.95), in which the first line number marker of a
10433    function does not show up until after the prologue, right before
10434    the second line number marker.  This function shifts ADDRESS down
10435    to the beginning of the function if necessary, and is called on
10436    addresses passed to record_line.  */
10437
10438 static CORE_ADDR
10439 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10440 {
10441   struct function_range *fn;
10442
10443   /* Find the function_range containing address.  */
10444   if (!cu->first_fn)
10445     return address;
10446
10447   if (!cu->cached_fn)
10448     cu->cached_fn = cu->first_fn;
10449
10450   fn = cu->cached_fn;
10451   while (fn)
10452     if (fn->lowpc <= address && fn->highpc > address)
10453       goto found;
10454     else
10455       fn = fn->next;
10456
10457   fn = cu->first_fn;
10458   while (fn && fn != cu->cached_fn)
10459     if (fn->lowpc <= address && fn->highpc > address)
10460       goto found;
10461     else
10462       fn = fn->next;
10463
10464   return address;
10465
10466  found:
10467   if (fn->seen_line)
10468     return address;
10469   if (address != fn->lowpc)
10470     complaint (&symfile_complaints,
10471                _("misplaced first line number at 0x%lx for '%s'"),
10472                (unsigned long) address, fn->name);
10473   fn->seen_line = 1;
10474   return fn->lowpc;
10475 }
10476
10477 /* Subroutine of dwarf_decode_lines to simplify it.
10478    Return the file name of the psymtab for included file FILE_INDEX
10479    in line header LH of PST.
10480    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10481    If space for the result is malloc'd, it will be freed by a cleanup.
10482    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10483
10484 static char *
10485 psymtab_include_file_name (const struct line_header *lh, int file_index,
10486                            const struct partial_symtab *pst,
10487                            const char *comp_dir)
10488 {
10489   const struct file_entry fe = lh->file_names [file_index];
10490   char *include_name = fe.name;
10491   char *include_name_to_compare = include_name;
10492   char *dir_name = NULL;
10493   const char *pst_filename;
10494   char *copied_name = NULL;
10495   int file_is_pst;
10496
10497   if (fe.dir_index)
10498     dir_name = lh->include_dirs[fe.dir_index - 1];
10499
10500   if (!IS_ABSOLUTE_PATH (include_name)
10501       && (dir_name != NULL || comp_dir != NULL))
10502     {
10503       /* Avoid creating a duplicate psymtab for PST.
10504          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10505          Before we do the comparison, however, we need to account
10506          for DIR_NAME and COMP_DIR.
10507          First prepend dir_name (if non-NULL).  If we still don't
10508          have an absolute path prepend comp_dir (if non-NULL).
10509          However, the directory we record in the include-file's
10510          psymtab does not contain COMP_DIR (to match the
10511          corresponding symtab(s)).
10512
10513          Example:
10514
10515          bash$ cd /tmp
10516          bash$ gcc -g ./hello.c
10517          include_name = "hello.c"
10518          dir_name = "."
10519          DW_AT_comp_dir = comp_dir = "/tmp"
10520          DW_AT_name = "./hello.c"  */
10521
10522       if (dir_name != NULL)
10523         {
10524           include_name = concat (dir_name, SLASH_STRING,
10525                                  include_name, (char *)NULL);
10526           include_name_to_compare = include_name;
10527           make_cleanup (xfree, include_name);
10528         }
10529       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10530         {
10531           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10532                                             include_name, (char *)NULL);
10533         }
10534     }
10535
10536   pst_filename = pst->filename;
10537   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10538     {
10539       copied_name = concat (pst->dirname, SLASH_STRING,
10540                             pst_filename, (char *)NULL);
10541       pst_filename = copied_name;
10542     }
10543
10544   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10545
10546   if (include_name_to_compare != include_name)
10547     xfree (include_name_to_compare);
10548   if (copied_name != NULL)
10549     xfree (copied_name);
10550
10551   if (file_is_pst)
10552     return NULL;
10553   return include_name;
10554 }
10555
10556 /* Ignore this record_line request.  */
10557
10558 static void
10559 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10560 {
10561   return;
10562 }
10563
10564 /* Decode the Line Number Program (LNP) for the given line_header
10565    structure and CU.  The actual information extracted and the type
10566    of structures created from the LNP depends on the value of PST.
10567
10568    1. If PST is NULL, then this procedure uses the data from the program
10569       to create all necessary symbol tables, and their linetables.
10570
10571    2. If PST is not NULL, this procedure reads the program to determine
10572       the list of files included by the unit represented by PST, and
10573       builds all the associated partial symbol tables.
10574
10575    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10576    It is used for relative paths in the line table.
10577    NOTE: When processing partial symtabs (pst != NULL),
10578    comp_dir == pst->dirname.
10579
10580    NOTE: It is important that psymtabs have the same file name (via strcmp)
10581    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10582    symtab we don't use it in the name of the psymtabs we create.
10583    E.g. expand_line_sal requires this when finding psymtabs to expand.
10584    A good testcase for this is mb-inline.exp.  */
10585
10586 static void
10587 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10588                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10589 {
10590   gdb_byte *line_ptr, *extended_end;
10591   gdb_byte *line_end;
10592   unsigned int bytes_read, extended_len;
10593   unsigned char op_code, extended_op, adj_opcode;
10594   CORE_ADDR baseaddr;
10595   struct objfile *objfile = cu->objfile;
10596   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10597   const int decode_for_pst_p = (pst != NULL);
10598   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10599   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10600     = record_line;
10601
10602   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10603
10604   line_ptr = lh->statement_program_start;
10605   line_end = lh->statement_program_end;
10606
10607   /* Read the statement sequences until there's nothing left.  */
10608   while (line_ptr < line_end)
10609     {
10610       /* state machine registers  */
10611       CORE_ADDR address = 0;
10612       unsigned int file = 1;
10613       unsigned int line = 1;
10614       unsigned int column = 0;
10615       int is_stmt = lh->default_is_stmt;
10616       int basic_block = 0;
10617       int end_sequence = 0;
10618       CORE_ADDR addr;
10619       unsigned char op_index = 0;
10620
10621       if (!decode_for_pst_p && lh->num_file_names >= file)
10622         {
10623           /* Start a subfile for the current file of the state machine.  */
10624           /* lh->include_dirs and lh->file_names are 0-based, but the
10625              directory and file name numbers in the statement program
10626              are 1-based.  */
10627           struct file_entry *fe = &lh->file_names[file - 1];
10628           char *dir = NULL;
10629
10630           if (fe->dir_index)
10631             dir = lh->include_dirs[fe->dir_index - 1];
10632
10633           dwarf2_start_subfile (fe->name, dir, comp_dir);
10634         }
10635
10636       /* Decode the table.  */
10637       while (!end_sequence)
10638         {
10639           op_code = read_1_byte (abfd, line_ptr);
10640           line_ptr += 1;
10641           if (line_ptr > line_end)
10642             {
10643               dwarf2_debug_line_missing_end_sequence_complaint ();
10644               break;
10645             }
10646
10647           if (op_code >= lh->opcode_base)
10648             {
10649               /* Special operand.  */
10650               adj_opcode = op_code - lh->opcode_base;
10651               address += (((op_index + (adj_opcode / lh->line_range))
10652                            / lh->maximum_ops_per_instruction)
10653                           * lh->minimum_instruction_length);
10654               op_index = ((op_index + (adj_opcode / lh->line_range))
10655                           % lh->maximum_ops_per_instruction);
10656               line += lh->line_base + (adj_opcode % lh->line_range);
10657               if (lh->num_file_names < file || file == 0)
10658                 dwarf2_debug_line_missing_file_complaint ();
10659               /* For now we ignore lines not starting on an
10660                  instruction boundary.  */
10661               else if (op_index == 0)
10662                 {
10663                   lh->file_names[file - 1].included_p = 1;
10664                   if (!decode_for_pst_p && is_stmt)
10665                     {
10666                       if (last_subfile != current_subfile)
10667                         {
10668                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10669                           if (last_subfile)
10670                             (*p_record_line) (last_subfile, 0, addr);
10671                           last_subfile = current_subfile;
10672                         }
10673                       /* Append row to matrix using current values.  */
10674                       addr = check_cu_functions (address, cu);
10675                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10676                       (*p_record_line) (current_subfile, line, addr);
10677                     }
10678                 }
10679               basic_block = 0;
10680             }
10681           else switch (op_code)
10682             {
10683             case DW_LNS_extended_op:
10684               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10685                                                    &bytes_read);
10686               line_ptr += bytes_read;
10687               extended_end = line_ptr + extended_len;
10688               extended_op = read_1_byte (abfd, line_ptr);
10689               line_ptr += 1;
10690               switch (extended_op)
10691                 {
10692                 case DW_LNE_end_sequence:
10693                   p_record_line = record_line;
10694                   end_sequence = 1;
10695                   break;
10696                 case DW_LNE_set_address:
10697                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10698
10699                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10700                     {
10701                       /* This line table is for a function which has been
10702                          GCd by the linker.  Ignore it.  PR gdb/12528 */
10703
10704                       long line_offset
10705                         = line_ptr - dwarf2_per_objfile->line.buffer;
10706
10707                       complaint (&symfile_complaints,
10708                                  _(".debug_line address at offset 0x%lx is 0 "
10709                                    "[in module %s]"),
10710                                  line_offset, cu->objfile->name);
10711                       p_record_line = noop_record_line;
10712                     }
10713
10714                   op_index = 0;
10715                   line_ptr += bytes_read;
10716                   address += baseaddr;
10717                   break;
10718                 case DW_LNE_define_file:
10719                   {
10720                     char *cur_file;
10721                     unsigned int dir_index, mod_time, length;
10722
10723                     cur_file = read_direct_string (abfd, line_ptr,
10724                                                    &bytes_read);
10725                     line_ptr += bytes_read;
10726                     dir_index =
10727                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10728                     line_ptr += bytes_read;
10729                     mod_time =
10730                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10731                     line_ptr += bytes_read;
10732                     length =
10733                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10734                     line_ptr += bytes_read;
10735                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10736                   }
10737                   break;
10738                 case DW_LNE_set_discriminator:
10739                   /* The discriminator is not interesting to the debugger;
10740                      just ignore it.  */
10741                   line_ptr = extended_end;
10742                   break;
10743                 default:
10744                   complaint (&symfile_complaints,
10745                              _("mangled .debug_line section"));
10746                   return;
10747                 }
10748               /* Make sure that we parsed the extended op correctly.  If e.g.
10749                  we expected a different address size than the producer used,
10750                  we may have read the wrong number of bytes.  */
10751               if (line_ptr != extended_end)
10752                 {
10753                   complaint (&symfile_complaints,
10754                              _("mangled .debug_line section"));
10755                   return;
10756                 }
10757               break;
10758             case DW_LNS_copy:
10759               if (lh->num_file_names < file || file == 0)
10760                 dwarf2_debug_line_missing_file_complaint ();
10761               else
10762                 {
10763                   lh->file_names[file - 1].included_p = 1;
10764                   if (!decode_for_pst_p && is_stmt)
10765                     {
10766                       if (last_subfile != current_subfile)
10767                         {
10768                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10769                           if (last_subfile)
10770                             (*p_record_line) (last_subfile, 0, addr);
10771                           last_subfile = current_subfile;
10772                         }
10773                       addr = check_cu_functions (address, cu);
10774                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10775                       (*p_record_line) (current_subfile, line, addr);
10776                     }
10777                 }
10778               basic_block = 0;
10779               break;
10780             case DW_LNS_advance_pc:
10781               {
10782                 CORE_ADDR adjust
10783                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10784
10785                 address += (((op_index + adjust)
10786                              / lh->maximum_ops_per_instruction)
10787                             * lh->minimum_instruction_length);
10788                 op_index = ((op_index + adjust)
10789                             % lh->maximum_ops_per_instruction);
10790                 line_ptr += bytes_read;
10791               }
10792               break;
10793             case DW_LNS_advance_line:
10794               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10795               line_ptr += bytes_read;
10796               break;
10797             case DW_LNS_set_file:
10798               {
10799                 /* The arrays lh->include_dirs and lh->file_names are
10800                    0-based, but the directory and file name numbers in
10801                    the statement program are 1-based.  */
10802                 struct file_entry *fe;
10803                 char *dir = NULL;
10804
10805                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10806                 line_ptr += bytes_read;
10807                 if (lh->num_file_names < file || file == 0)
10808                   dwarf2_debug_line_missing_file_complaint ();
10809                 else
10810                   {
10811                     fe = &lh->file_names[file - 1];
10812                     if (fe->dir_index)
10813                       dir = lh->include_dirs[fe->dir_index - 1];
10814                     if (!decode_for_pst_p)
10815                       {
10816                         last_subfile = current_subfile;
10817                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10818                       }
10819                   }
10820               }
10821               break;
10822             case DW_LNS_set_column:
10823               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10824               line_ptr += bytes_read;
10825               break;
10826             case DW_LNS_negate_stmt:
10827               is_stmt = (!is_stmt);
10828               break;
10829             case DW_LNS_set_basic_block:
10830               basic_block = 1;
10831               break;
10832             /* Add to the address register of the state machine the
10833                address increment value corresponding to special opcode
10834                255.  I.e., this value is scaled by the minimum
10835                instruction length since special opcode 255 would have
10836                scaled the increment.  */
10837             case DW_LNS_const_add_pc:
10838               {
10839                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10840
10841                 address += (((op_index + adjust)
10842                              / lh->maximum_ops_per_instruction)
10843                             * lh->minimum_instruction_length);
10844                 op_index = ((op_index + adjust)
10845                             % lh->maximum_ops_per_instruction);
10846               }
10847               break;
10848             case DW_LNS_fixed_advance_pc:
10849               address += read_2_bytes (abfd, line_ptr);
10850               op_index = 0;
10851               line_ptr += 2;
10852               break;
10853             default:
10854               {
10855                 /* Unknown standard opcode, ignore it.  */
10856                 int i;
10857
10858                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10859                   {
10860                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10861                     line_ptr += bytes_read;
10862                   }
10863               }
10864             }
10865         }
10866       if (lh->num_file_names < file || file == 0)
10867         dwarf2_debug_line_missing_file_complaint ();
10868       else
10869         {
10870           lh->file_names[file - 1].included_p = 1;
10871           if (!decode_for_pst_p)
10872             {
10873               addr = gdbarch_addr_bits_remove (gdbarch, address);
10874               (*p_record_line) (current_subfile, 0, addr);
10875             }
10876         }
10877     }
10878
10879   if (decode_for_pst_p)
10880     {
10881       int file_index;
10882
10883       /* Now that we're done scanning the Line Header Program, we can
10884          create the psymtab of each included file.  */
10885       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10886         if (lh->file_names[file_index].included_p == 1)
10887           {
10888             char *include_name =
10889               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10890             if (include_name != NULL)
10891               dwarf2_create_include_psymtab (include_name, pst, objfile);
10892           }
10893     }
10894   else
10895     {
10896       /* Make sure a symtab is created for every file, even files
10897          which contain only variables (i.e. no code with associated
10898          line numbers).  */
10899
10900       int i;
10901       struct file_entry *fe;
10902
10903       for (i = 0; i < lh->num_file_names; i++)
10904         {
10905           char *dir = NULL;
10906
10907           fe = &lh->file_names[i];
10908           if (fe->dir_index)
10909             dir = lh->include_dirs[fe->dir_index - 1];
10910           dwarf2_start_subfile (fe->name, dir, comp_dir);
10911
10912           /* Skip the main file; we don't need it, and it must be
10913              allocated last, so that it will show up before the
10914              non-primary symtabs in the objfile's symtab list.  */
10915           if (current_subfile == first_subfile)
10916             continue;
10917
10918           if (current_subfile->symtab == NULL)
10919             current_subfile->symtab = allocate_symtab (current_subfile->name,
10920                                                        cu->objfile);
10921           fe->symtab = current_subfile->symtab;
10922         }
10923     }
10924 }
10925
10926 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10927    DIRNAME the name of the source directory which contains FILENAME
10928    or NULL if not known.  COMP_DIR is the compilation directory for the
10929    linetable's compilation unit or NULL if not known.
10930    This routine tries to keep line numbers from identical absolute and
10931    relative file names in a common subfile.
10932
10933    Using the `list' example from the GDB testsuite, which resides in
10934    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10935    of /srcdir/list0.c yields the following debugging information for list0.c:
10936
10937    DW_AT_name:          /srcdir/list0.c
10938    DW_AT_comp_dir:              /compdir
10939    files.files[0].name: list0.h
10940    files.files[0].dir:  /srcdir
10941    files.files[1].name: list0.c
10942    files.files[1].dir:  /srcdir
10943
10944    The line number information for list0.c has to end up in a single
10945    subfile, so that `break /srcdir/list0.c:1' works as expected.
10946    start_subfile will ensure that this happens provided that we pass the
10947    concatenation of files.files[1].dir and files.files[1].name as the
10948    subfile's name.  */
10949
10950 static void
10951 dwarf2_start_subfile (char *filename, const char *dirname,
10952                       const char *comp_dir)
10953 {
10954   char *fullname;
10955
10956   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10957      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10958      second argument to start_subfile.  To be consistent, we do the
10959      same here.  In order not to lose the line information directory,
10960      we concatenate it to the filename when it makes sense.
10961      Note that the Dwarf3 standard says (speaking of filenames in line
10962      information): ``The directory index is ignored for file names
10963      that represent full path names''.  Thus ignoring dirname in the
10964      `else' branch below isn't an issue.  */
10965
10966   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10967     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10968   else
10969     fullname = filename;
10970
10971   start_subfile (fullname, comp_dir);
10972
10973   if (fullname != filename)
10974     xfree (fullname);
10975 }
10976
10977 static void
10978 var_decode_location (struct attribute *attr, struct symbol *sym,
10979                      struct dwarf2_cu *cu)
10980 {
10981   struct objfile *objfile = cu->objfile;
10982   struct comp_unit_head *cu_header = &cu->header;
10983
10984   /* NOTE drow/2003-01-30: There used to be a comment and some special
10985      code here to turn a symbol with DW_AT_external and a
10986      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10987      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10988      with some versions of binutils) where shared libraries could have
10989      relocations against symbols in their debug information - the
10990      minimal symbol would have the right address, but the debug info
10991      would not.  It's no longer necessary, because we will explicitly
10992      apply relocations when we read in the debug information now.  */
10993
10994   /* A DW_AT_location attribute with no contents indicates that a
10995      variable has been optimized away.  */
10996   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10997     {
10998       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10999       return;
11000     }
11001
11002   /* Handle one degenerate form of location expression specially, to
11003      preserve GDB's previous behavior when section offsets are
11004      specified.  If this is just a DW_OP_addr then mark this symbol
11005      as LOC_STATIC.  */
11006
11007   if (attr_form_is_block (attr)
11008       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11009       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11010     {
11011       unsigned int dummy;
11012
11013       SYMBOL_VALUE_ADDRESS (sym) =
11014         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11015       SYMBOL_CLASS (sym) = LOC_STATIC;
11016       fixup_symbol_section (sym, objfile);
11017       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11018                                               SYMBOL_SECTION (sym));
11019       return;
11020     }
11021
11022   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11023      expression evaluator, and use LOC_COMPUTED only when necessary
11024      (i.e. when the value of a register or memory location is
11025      referenced, or a thread-local block, etc.).  Then again, it might
11026      not be worthwhile.  I'm assuming that it isn't unless performance
11027      or memory numbers show me otherwise.  */
11028
11029   dwarf2_symbol_mark_computed (attr, sym, cu);
11030   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11031
11032   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11033     cu->has_loclist = 1;
11034 }
11035
11036 /* Given a pointer to a DWARF information entry, figure out if we need
11037    to make a symbol table entry for it, and if so, create a new entry
11038    and return a pointer to it.
11039    If TYPE is NULL, determine symbol type from the die, otherwise
11040    used the passed type.
11041    If SPACE is not NULL, use it to hold the new symbol.  If it is
11042    NULL, allocate a new symbol on the objfile's obstack.  */
11043
11044 static struct symbol *
11045 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11046                  struct symbol *space)
11047 {
11048   struct objfile *objfile = cu->objfile;
11049   struct symbol *sym = NULL;
11050   char *name;
11051   struct attribute *attr = NULL;
11052   struct attribute *attr2 = NULL;
11053   CORE_ADDR baseaddr;
11054   struct pending **list_to_add = NULL;
11055
11056   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11057
11058   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11059
11060   name = dwarf2_name (die, cu);
11061   if (name)
11062     {
11063       const char *linkagename;
11064       int suppress_add = 0;
11065
11066       if (space)
11067         sym = space;
11068       else
11069         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11070       OBJSTAT (objfile, n_syms++);
11071
11072       /* Cache this symbol's name and the name's demangled form (if any).  */
11073       SYMBOL_SET_LANGUAGE (sym, cu->language);
11074       linkagename = dwarf2_physname (name, die, cu);
11075       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11076
11077       /* Fortran does not have mangling standard and the mangling does differ
11078          between gfortran, iFort etc.  */
11079       if (cu->language == language_fortran
11080           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11081         symbol_set_demangled_name (&(sym->ginfo),
11082                                    (char *) dwarf2_full_name (name, die, cu),
11083                                    NULL);
11084
11085       /* Default assumptions.
11086          Use the passed type or decode it from the die.  */
11087       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11088       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11089       if (type != NULL)
11090         SYMBOL_TYPE (sym) = type;
11091       else
11092         SYMBOL_TYPE (sym) = die_type (die, cu);
11093       attr = dwarf2_attr (die,
11094                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11095                           cu);
11096       if (attr)
11097         {
11098           SYMBOL_LINE (sym) = DW_UNSND (attr);
11099         }
11100
11101       attr = dwarf2_attr (die,
11102                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11103                           cu);
11104       if (attr)
11105         {
11106           int file_index = DW_UNSND (attr);
11107
11108           if (cu->line_header == NULL
11109               || file_index > cu->line_header->num_file_names)
11110             complaint (&symfile_complaints,
11111                        _("file index out of range"));
11112           else if (file_index > 0)
11113             {
11114               struct file_entry *fe;
11115
11116               fe = &cu->line_header->file_names[file_index - 1];
11117               SYMBOL_SYMTAB (sym) = fe->symtab;
11118             }
11119         }
11120
11121       switch (die->tag)
11122         {
11123         case DW_TAG_label:
11124           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11125           if (attr)
11126             {
11127               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11128             }
11129           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11130           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11131           SYMBOL_CLASS (sym) = LOC_LABEL;
11132           add_symbol_to_list (sym, cu->list_in_scope);
11133           break;
11134         case DW_TAG_subprogram:
11135           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11136              finish_block.  */
11137           SYMBOL_CLASS (sym) = LOC_BLOCK;
11138           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11139           if ((attr2 && (DW_UNSND (attr2) != 0))
11140               || cu->language == language_ada)
11141             {
11142               /* Subprograms marked external are stored as a global symbol.
11143                  Ada subprograms, whether marked external or not, are always
11144                  stored as a global symbol, because we want to be able to
11145                  access them globally.  For instance, we want to be able
11146                  to break on a nested subprogram without having to
11147                  specify the context.  */
11148               list_to_add = &global_symbols;
11149             }
11150           else
11151             {
11152               list_to_add = cu->list_in_scope;
11153             }
11154           break;
11155         case DW_TAG_inlined_subroutine:
11156           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11157              finish_block.  */
11158           SYMBOL_CLASS (sym) = LOC_BLOCK;
11159           SYMBOL_INLINED (sym) = 1;
11160           /* Do not add the symbol to any lists.  It will be found via
11161              BLOCK_FUNCTION from the blockvector.  */
11162           break;
11163         case DW_TAG_template_value_param:
11164           suppress_add = 1;
11165           /* Fall through.  */
11166         case DW_TAG_constant:
11167         case DW_TAG_variable:
11168         case DW_TAG_member:
11169           /* Compilation with minimal debug info may result in
11170              variables with missing type entries.  Change the
11171              misleading `void' type to something sensible.  */
11172           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11173             SYMBOL_TYPE (sym)
11174               = objfile_type (objfile)->nodebug_data_symbol;
11175
11176           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11177           /* In the case of DW_TAG_member, we should only be called for
11178              static const members.  */
11179           if (die->tag == DW_TAG_member)
11180             {
11181               /* dwarf2_add_field uses die_is_declaration,
11182                  so we do the same.  */
11183               gdb_assert (die_is_declaration (die, cu));
11184               gdb_assert (attr);
11185             }
11186           if (attr)
11187             {
11188               dwarf2_const_value (attr, sym, cu);
11189               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11190               if (!suppress_add)
11191                 {
11192                   if (attr2 && (DW_UNSND (attr2) != 0))
11193                     list_to_add = &global_symbols;
11194                   else
11195                     list_to_add = cu->list_in_scope;
11196                 }
11197               break;
11198             }
11199           attr = dwarf2_attr (die, DW_AT_location, cu);
11200           if (attr)
11201             {
11202               var_decode_location (attr, sym, cu);
11203               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11204               if (SYMBOL_CLASS (sym) == LOC_STATIC
11205                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11206                   && !dwarf2_per_objfile->has_section_at_zero)
11207                 {
11208                   /* When a static variable is eliminated by the linker,
11209                      the corresponding debug information is not stripped
11210                      out, but the variable address is set to null;
11211                      do not add such variables into symbol table.  */
11212                 }
11213               else if (attr2 && (DW_UNSND (attr2) != 0))
11214                 {
11215                   /* Workaround gfortran PR debug/40040 - it uses
11216                      DW_AT_location for variables in -fPIC libraries which may
11217                      get overriden by other libraries/executable and get
11218                      a different address.  Resolve it by the minimal symbol
11219                      which may come from inferior's executable using copy
11220                      relocation.  Make this workaround only for gfortran as for
11221                      other compilers GDB cannot guess the minimal symbol
11222                      Fortran mangling kind.  */
11223                   if (cu->language == language_fortran && die->parent
11224                       && die->parent->tag == DW_TAG_module
11225                       && cu->producer
11226                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11227                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11228
11229                   /* A variable with DW_AT_external is never static,
11230                      but it may be block-scoped.  */
11231                   list_to_add = (cu->list_in_scope == &file_symbols
11232                                  ? &global_symbols : cu->list_in_scope);
11233                 }
11234               else
11235                 list_to_add = cu->list_in_scope;
11236             }
11237           else
11238             {
11239               /* We do not know the address of this symbol.
11240                  If it is an external symbol and we have type information
11241                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11242                  The address of the variable will then be determined from
11243                  the minimal symbol table whenever the variable is
11244                  referenced.  */
11245               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11246               if (attr2 && (DW_UNSND (attr2) != 0)
11247                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11248                 {
11249                   /* A variable with DW_AT_external is never static, but it
11250                      may be block-scoped.  */
11251                   list_to_add = (cu->list_in_scope == &file_symbols
11252                                  ? &global_symbols : cu->list_in_scope);
11253
11254                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11255                 }
11256               else if (!die_is_declaration (die, cu))
11257                 {
11258                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11259                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11260                   if (!suppress_add)
11261                     list_to_add = cu->list_in_scope;
11262                 }
11263             }
11264           break;
11265         case DW_TAG_formal_parameter:
11266           /* If we are inside a function, mark this as an argument.  If
11267              not, we might be looking at an argument to an inlined function
11268              when we do not have enough information to show inlined frames;
11269              pretend it's a local variable in that case so that the user can
11270              still see it.  */
11271           if (context_stack_depth > 0
11272               && context_stack[context_stack_depth - 1].name != NULL)
11273             SYMBOL_IS_ARGUMENT (sym) = 1;
11274           attr = dwarf2_attr (die, DW_AT_location, cu);
11275           if (attr)
11276             {
11277               var_decode_location (attr, sym, cu);
11278             }
11279           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11280           if (attr)
11281             {
11282               dwarf2_const_value (attr, sym, cu);
11283             }
11284           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11285           if (attr && DW_UNSND (attr))
11286             {
11287               struct type *ref_type;
11288
11289               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11290               SYMBOL_TYPE (sym) = ref_type;
11291             }
11292
11293           list_to_add = cu->list_in_scope;
11294           break;
11295         case DW_TAG_unspecified_parameters:
11296           /* From varargs functions; gdb doesn't seem to have any
11297              interest in this information, so just ignore it for now.
11298              (FIXME?) */
11299           break;
11300         case DW_TAG_template_type_param:
11301           suppress_add = 1;
11302           /* Fall through.  */
11303         case DW_TAG_class_type:
11304         case DW_TAG_interface_type:
11305         case DW_TAG_structure_type:
11306         case DW_TAG_union_type:
11307         case DW_TAG_set_type:
11308         case DW_TAG_enumeration_type:
11309           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11310           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11311
11312           {
11313             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11314                really ever be static objects: otherwise, if you try
11315                to, say, break of a class's method and you're in a file
11316                which doesn't mention that class, it won't work unless
11317                the check for all static symbols in lookup_symbol_aux
11318                saves you.  See the OtherFileClass tests in
11319                gdb.c++/namespace.exp.  */
11320
11321             if (!suppress_add)
11322               {
11323                 list_to_add = (cu->list_in_scope == &file_symbols
11324                                && (cu->language == language_cplus
11325                                    || cu->language == language_java)
11326                                ? &global_symbols : cu->list_in_scope);
11327
11328                 /* The semantics of C++ state that "struct foo {
11329                    ... }" also defines a typedef for "foo".  A Java
11330                    class declaration also defines a typedef for the
11331                    class.  */
11332                 if (cu->language == language_cplus
11333                     || cu->language == language_java
11334                     || cu->language == language_ada)
11335                   {
11336                     /* The symbol's name is already allocated along
11337                        with this objfile, so we don't need to
11338                        duplicate it for the type.  */
11339                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11340                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11341                   }
11342               }
11343           }
11344           break;
11345         case DW_TAG_typedef:
11346           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11347           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11348           list_to_add = cu->list_in_scope;
11349           break;
11350         case DW_TAG_base_type:
11351         case DW_TAG_subrange_type:
11352           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11353           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11354           list_to_add = cu->list_in_scope;
11355           break;
11356         case DW_TAG_enumerator:
11357           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11358           if (attr)
11359             {
11360               dwarf2_const_value (attr, sym, cu);
11361             }
11362           {
11363             /* NOTE: carlton/2003-11-10: See comment above in the
11364                DW_TAG_class_type, etc. block.  */
11365
11366             list_to_add = (cu->list_in_scope == &file_symbols
11367                            && (cu->language == language_cplus
11368                                || cu->language == language_java)
11369                            ? &global_symbols : cu->list_in_scope);
11370           }
11371           break;
11372         case DW_TAG_namespace:
11373           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11374           list_to_add = &global_symbols;
11375           break;
11376         default:
11377           /* Not a tag we recognize.  Hopefully we aren't processing
11378              trash data, but since we must specifically ignore things
11379              we don't recognize, there is nothing else we should do at
11380              this point.  */
11381           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11382                      dwarf_tag_name (die->tag));
11383           break;
11384         }
11385
11386       if (suppress_add)
11387         {
11388           sym->hash_next = objfile->template_symbols;
11389           objfile->template_symbols = sym;
11390           list_to_add = NULL;
11391         }
11392
11393       if (list_to_add != NULL)
11394         add_symbol_to_list (sym, list_to_add);
11395
11396       /* For the benefit of old versions of GCC, check for anonymous
11397          namespaces based on the demangled name.  */
11398       if (!processing_has_namespace_info
11399           && cu->language == language_cplus)
11400         cp_scan_for_anonymous_namespaces (sym);
11401     }
11402   return (sym);
11403 }
11404
11405 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11406
11407 static struct symbol *
11408 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11409 {
11410   return new_symbol_full (die, type, cu, NULL);
11411 }
11412
11413 /* Given an attr with a DW_FORM_dataN value in host byte order,
11414    zero-extend it as appropriate for the symbol's type.  The DWARF
11415    standard (v4) is not entirely clear about the meaning of using
11416    DW_FORM_dataN for a constant with a signed type, where the type is
11417    wider than the data.  The conclusion of a discussion on the DWARF
11418    list was that this is unspecified.  We choose to always zero-extend
11419    because that is the interpretation long in use by GCC.  */
11420
11421 static gdb_byte *
11422 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11423                          const char *name, struct obstack *obstack,
11424                          struct dwarf2_cu *cu, long *value, int bits)
11425 {
11426   struct objfile *objfile = cu->objfile;
11427   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11428                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11429   LONGEST l = DW_UNSND (attr);
11430
11431   if (bits < sizeof (*value) * 8)
11432     {
11433       l &= ((LONGEST) 1 << bits) - 1;
11434       *value = l;
11435     }
11436   else if (bits == sizeof (*value) * 8)
11437     *value = l;
11438   else
11439     {
11440       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11441       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11442       return bytes;
11443     }
11444
11445   return NULL;
11446 }
11447
11448 /* Read a constant value from an attribute.  Either set *VALUE, or if
11449    the value does not fit in *VALUE, set *BYTES - either already
11450    allocated on the objfile obstack, or newly allocated on OBSTACK,
11451    or, set *BATON, if we translated the constant to a location
11452    expression.  */
11453
11454 static void
11455 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11456                          const char *name, struct obstack *obstack,
11457                          struct dwarf2_cu *cu,
11458                          long *value, gdb_byte **bytes,
11459                          struct dwarf2_locexpr_baton **baton)
11460 {
11461   struct objfile *objfile = cu->objfile;
11462   struct comp_unit_head *cu_header = &cu->header;
11463   struct dwarf_block *blk;
11464   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11465                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11466
11467   *value = 0;
11468   *bytes = NULL;
11469   *baton = NULL;
11470
11471   switch (attr->form)
11472     {
11473     case DW_FORM_addr:
11474       {
11475         gdb_byte *data;
11476
11477         if (TYPE_LENGTH (type) != cu_header->addr_size)
11478           dwarf2_const_value_length_mismatch_complaint (name,
11479                                                         cu_header->addr_size,
11480                                                         TYPE_LENGTH (type));
11481         /* Symbols of this form are reasonably rare, so we just
11482            piggyback on the existing location code rather than writing
11483            a new implementation of symbol_computed_ops.  */
11484         *baton = obstack_alloc (&objfile->objfile_obstack,
11485                                 sizeof (struct dwarf2_locexpr_baton));
11486         (*baton)->per_cu = cu->per_cu;
11487         gdb_assert ((*baton)->per_cu);
11488
11489         (*baton)->size = 2 + cu_header->addr_size;
11490         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11491         (*baton)->data = data;
11492
11493         data[0] = DW_OP_addr;
11494         store_unsigned_integer (&data[1], cu_header->addr_size,
11495                                 byte_order, DW_ADDR (attr));
11496         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11497       }
11498       break;
11499     case DW_FORM_string:
11500     case DW_FORM_strp:
11501       /* DW_STRING is already allocated on the objfile obstack, point
11502          directly to it.  */
11503       *bytes = (gdb_byte *) DW_STRING (attr);
11504       break;
11505     case DW_FORM_block1:
11506     case DW_FORM_block2:
11507     case DW_FORM_block4:
11508     case DW_FORM_block:
11509     case DW_FORM_exprloc:
11510       blk = DW_BLOCK (attr);
11511       if (TYPE_LENGTH (type) != blk->size)
11512         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11513                                                       TYPE_LENGTH (type));
11514       *bytes = blk->data;
11515       break;
11516
11517       /* The DW_AT_const_value attributes are supposed to carry the
11518          symbol's value "represented as it would be on the target
11519          architecture."  By the time we get here, it's already been
11520          converted to host endianness, so we just need to sign- or
11521          zero-extend it as appropriate.  */
11522     case DW_FORM_data1:
11523       *bytes = dwarf2_const_value_data (attr, type, name,
11524                                         obstack, cu, value, 8);
11525       break;
11526     case DW_FORM_data2:
11527       *bytes = dwarf2_const_value_data (attr, type, name,
11528                                         obstack, cu, value, 16);
11529       break;
11530     case DW_FORM_data4:
11531       *bytes = dwarf2_const_value_data (attr, type, name,
11532                                         obstack, cu, value, 32);
11533       break;
11534     case DW_FORM_data8:
11535       *bytes = dwarf2_const_value_data (attr, type, name,
11536                                         obstack, cu, value, 64);
11537       break;
11538
11539     case DW_FORM_sdata:
11540       *value = DW_SND (attr);
11541       break;
11542
11543     case DW_FORM_udata:
11544       *value = DW_UNSND (attr);
11545       break;
11546
11547     default:
11548       complaint (&symfile_complaints,
11549                  _("unsupported const value attribute form: '%s'"),
11550                  dwarf_form_name (attr->form));
11551       *value = 0;
11552       break;
11553     }
11554 }
11555
11556
11557 /* Copy constant value from an attribute to a symbol.  */
11558
11559 static void
11560 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11561                     struct dwarf2_cu *cu)
11562 {
11563   struct objfile *objfile = cu->objfile;
11564   struct comp_unit_head *cu_header = &cu->header;
11565   long value;
11566   gdb_byte *bytes;
11567   struct dwarf2_locexpr_baton *baton;
11568
11569   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11570                            SYMBOL_PRINT_NAME (sym),
11571                            &objfile->objfile_obstack, cu,
11572                            &value, &bytes, &baton);
11573
11574   if (baton != NULL)
11575     {
11576       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11577       SYMBOL_LOCATION_BATON (sym) = baton;
11578       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11579     }
11580   else if (bytes != NULL)
11581      {
11582       SYMBOL_VALUE_BYTES (sym) = bytes;
11583       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11584     }
11585   else
11586     {
11587       SYMBOL_VALUE (sym) = value;
11588       SYMBOL_CLASS (sym) = LOC_CONST;
11589     }
11590 }
11591
11592 /* Return the type of the die in question using its DW_AT_type attribute.  */
11593
11594 static struct type *
11595 die_type (struct die_info *die, struct dwarf2_cu *cu)
11596 {
11597   struct attribute *type_attr;
11598
11599   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11600   if (!type_attr)
11601     {
11602       /* A missing DW_AT_type represents a void type.  */
11603       return objfile_type (cu->objfile)->builtin_void;
11604     }
11605
11606   return lookup_die_type (die, type_attr, cu);
11607 }
11608
11609 /* True iff CU's producer generates GNAT Ada auxiliary information
11610    that allows to find parallel types through that information instead
11611    of having to do expensive parallel lookups by type name.  */
11612
11613 static int
11614 need_gnat_info (struct dwarf2_cu *cu)
11615 {
11616   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11617      of GNAT produces this auxiliary information, without any indication
11618      that it is produced.  Part of enhancing the FSF version of GNAT
11619      to produce that information will be to put in place an indicator
11620      that we can use in order to determine whether the descriptive type
11621      info is available or not.  One suggestion that has been made is
11622      to use a new attribute, attached to the CU die.  For now, assume
11623      that the descriptive type info is not available.  */
11624   return 0;
11625 }
11626
11627 /* Return the auxiliary type of the die in question using its
11628    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11629    attribute is not present.  */
11630
11631 static struct type *
11632 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11633 {
11634   struct attribute *type_attr;
11635
11636   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11637   if (!type_attr)
11638     return NULL;
11639
11640   return lookup_die_type (die, type_attr, cu);
11641 }
11642
11643 /* If DIE has a descriptive_type attribute, then set the TYPE's
11644    descriptive type accordingly.  */
11645
11646 static void
11647 set_descriptive_type (struct type *type, struct die_info *die,
11648                       struct dwarf2_cu *cu)
11649 {
11650   struct type *descriptive_type = die_descriptive_type (die, cu);
11651
11652   if (descriptive_type)
11653     {
11654       ALLOCATE_GNAT_AUX_TYPE (type);
11655       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11656     }
11657 }
11658
11659 /* Return the containing type of the die in question using its
11660    DW_AT_containing_type attribute.  */
11661
11662 static struct type *
11663 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11664 {
11665   struct attribute *type_attr;
11666
11667   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11668   if (!type_attr)
11669     error (_("Dwarf Error: Problem turning containing type into gdb type "
11670              "[in module %s]"), cu->objfile->name);
11671
11672   return lookup_die_type (die, type_attr, cu);
11673 }
11674
11675 /* Look up the type of DIE in CU using its type attribute ATTR.
11676    If there is no type substitute an error marker.  */
11677
11678 static struct type *
11679 lookup_die_type (struct die_info *die, struct attribute *attr,
11680                  struct dwarf2_cu *cu)
11681 {
11682   struct type *this_type;
11683
11684   /* First see if we have it cached.  */
11685
11686   if (is_ref_attr (attr))
11687     {
11688       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11689
11690       this_type = get_die_type_at_offset (offset, cu->per_cu);
11691     }
11692   else if (attr->form == DW_FORM_ref_sig8)
11693     {
11694       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11695       struct dwarf2_cu *sig_cu;
11696       unsigned int offset;
11697
11698       /* sig_type will be NULL if the signatured type is missing from
11699          the debug info.  */
11700       if (sig_type == NULL)
11701         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11702                  "at 0x%x [in module %s]"),
11703                die->offset, cu->objfile->name);
11704
11705       gdb_assert (sig_type->per_cu.from_debug_types);
11706       offset = sig_type->offset + sig_type->type_offset;
11707       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11708     }
11709   else
11710     {
11711       dump_die_for_error (die);
11712       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11713              dwarf_attr_name (attr->name), cu->objfile->name);
11714     }
11715
11716   /* If not cached we need to read it in.  */
11717
11718   if (this_type == NULL)
11719     {
11720       struct die_info *type_die;
11721       struct dwarf2_cu *type_cu = cu;
11722
11723       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11724       /* If the type is cached, we should have found it above.  */
11725       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11726       this_type = read_type_die_1 (type_die, type_cu);
11727     }
11728
11729   /* If we still don't have a type use an error marker.  */
11730
11731   if (this_type == NULL)
11732     {
11733       char *message, *saved;
11734
11735       /* read_type_die already issued a complaint.  */
11736       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11737                             cu->objfile->name,
11738                             cu->header.offset,
11739                             die->offset);
11740       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11741                              message, strlen (message));
11742       xfree (message);
11743
11744       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11745     }
11746
11747   return this_type;
11748 }
11749
11750 /* Return the type in DIE, CU.
11751    Returns NULL for invalid types.
11752
11753    This first does a lookup in the appropriate type_hash table,
11754    and only reads the die in if necessary.
11755
11756    NOTE: This can be called when reading in partial or full symbols.  */
11757
11758 static struct type *
11759 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11760 {
11761   struct type *this_type;
11762
11763   this_type = get_die_type (die, cu);
11764   if (this_type)
11765     return this_type;
11766
11767   return read_type_die_1 (die, cu);
11768 }
11769
11770 /* Read the type in DIE, CU.
11771    Returns NULL for invalid types.  */
11772
11773 static struct type *
11774 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11775 {
11776   struct type *this_type = NULL;
11777
11778   switch (die->tag)
11779     {
11780     case DW_TAG_class_type:
11781     case DW_TAG_interface_type:
11782     case DW_TAG_structure_type:
11783     case DW_TAG_union_type:
11784       this_type = read_structure_type (die, cu);
11785       break;
11786     case DW_TAG_enumeration_type:
11787       this_type = read_enumeration_type (die, cu);
11788       break;
11789     case DW_TAG_subprogram:
11790     case DW_TAG_subroutine_type:
11791     case DW_TAG_inlined_subroutine:
11792       this_type = read_subroutine_type (die, cu);
11793       break;
11794     case DW_TAG_array_type:
11795       this_type = read_array_type (die, cu);
11796       break;
11797     case DW_TAG_set_type:
11798       this_type = read_set_type (die, cu);
11799       break;
11800     case DW_TAG_pointer_type:
11801       this_type = read_tag_pointer_type (die, cu);
11802       break;
11803     case DW_TAG_ptr_to_member_type:
11804       this_type = read_tag_ptr_to_member_type (die, cu);
11805       break;
11806     case DW_TAG_reference_type:
11807       this_type = read_tag_reference_type (die, cu);
11808       break;
11809     case DW_TAG_const_type:
11810       this_type = read_tag_const_type (die, cu);
11811       break;
11812     case DW_TAG_volatile_type:
11813       this_type = read_tag_volatile_type (die, cu);
11814       break;
11815     case DW_TAG_string_type:
11816       this_type = read_tag_string_type (die, cu);
11817       break;
11818     case DW_TAG_typedef:
11819       this_type = read_typedef (die, cu);
11820       break;
11821     case DW_TAG_subrange_type:
11822       this_type = read_subrange_type (die, cu);
11823       break;
11824     case DW_TAG_base_type:
11825       this_type = read_base_type (die, cu);
11826       break;
11827     case DW_TAG_unspecified_type:
11828       this_type = read_unspecified_type (die, cu);
11829       break;
11830     case DW_TAG_namespace:
11831       this_type = read_namespace_type (die, cu);
11832       break;
11833     case DW_TAG_module:
11834       this_type = read_module_type (die, cu);
11835       break;
11836     default:
11837       complaint (&symfile_complaints,
11838                  _("unexpected tag in read_type_die: '%s'"),
11839                  dwarf_tag_name (die->tag));
11840       break;
11841     }
11842
11843   return this_type;
11844 }
11845
11846 /* See if we can figure out if the class lives in a namespace.  We do
11847    this by looking for a member function; its demangled name will
11848    contain namespace info, if there is any.
11849    Return the computed name or NULL.
11850    Space for the result is allocated on the objfile's obstack.
11851    This is the full-die version of guess_partial_die_structure_name.
11852    In this case we know DIE has no useful parent.  */
11853
11854 static char *
11855 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11856 {
11857   struct die_info *spec_die;
11858   struct dwarf2_cu *spec_cu;
11859   struct die_info *child;
11860
11861   spec_cu = cu;
11862   spec_die = die_specification (die, &spec_cu);
11863   if (spec_die != NULL)
11864     {
11865       die = spec_die;
11866       cu = spec_cu;
11867     }
11868
11869   for (child = die->child;
11870        child != NULL;
11871        child = child->sibling)
11872     {
11873       if (child->tag == DW_TAG_subprogram)
11874         {
11875           struct attribute *attr;
11876
11877           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11878           if (attr == NULL)
11879             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11880           if (attr != NULL)
11881             {
11882               char *actual_name
11883                 = language_class_name_from_physname (cu->language_defn,
11884                                                      DW_STRING (attr));
11885               char *name = NULL;
11886
11887               if (actual_name != NULL)
11888                 {
11889                   char *die_name = dwarf2_name (die, cu);
11890
11891                   if (die_name != NULL
11892                       && strcmp (die_name, actual_name) != 0)
11893                     {
11894                       /* Strip off the class name from the full name.
11895                          We want the prefix.  */
11896                       int die_name_len = strlen (die_name);
11897                       int actual_name_len = strlen (actual_name);
11898
11899                       /* Test for '::' as a sanity check.  */
11900                       if (actual_name_len > die_name_len + 2
11901                           && actual_name[actual_name_len
11902                                          - die_name_len - 1] == ':')
11903                         name =
11904                           obsavestring (actual_name,
11905                                         actual_name_len - die_name_len - 2,
11906                                         &cu->objfile->objfile_obstack);
11907                     }
11908                 }
11909               xfree (actual_name);
11910               return name;
11911             }
11912         }
11913     }
11914
11915   return NULL;
11916 }
11917
11918 /* Return the name of the namespace/class that DIE is defined within,
11919    or "" if we can't tell.  The caller should not xfree the result.
11920
11921    For example, if we're within the method foo() in the following
11922    code:
11923
11924    namespace N {
11925      class C {
11926        void foo () {
11927        }
11928      };
11929    }
11930
11931    then determine_prefix on foo's die will return "N::C".  */
11932
11933 static char *
11934 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11935 {
11936   struct die_info *parent, *spec_die;
11937   struct dwarf2_cu *spec_cu;
11938   struct type *parent_type;
11939
11940   if (cu->language != language_cplus && cu->language != language_java
11941       && cu->language != language_fortran)
11942     return "";
11943
11944   /* We have to be careful in the presence of DW_AT_specification.
11945      For example, with GCC 3.4, given the code
11946
11947      namespace N {
11948        void foo() {
11949          // Definition of N::foo.
11950        }
11951      }
11952
11953      then we'll have a tree of DIEs like this:
11954
11955      1: DW_TAG_compile_unit
11956        2: DW_TAG_namespace        // N
11957          3: DW_TAG_subprogram     // declaration of N::foo
11958        4: DW_TAG_subprogram       // definition of N::foo
11959             DW_AT_specification   // refers to die #3
11960
11961      Thus, when processing die #4, we have to pretend that we're in
11962      the context of its DW_AT_specification, namely the contex of die
11963      #3.  */
11964   spec_cu = cu;
11965   spec_die = die_specification (die, &spec_cu);
11966   if (spec_die == NULL)
11967     parent = die->parent;
11968   else
11969     {
11970       parent = spec_die->parent;
11971       cu = spec_cu;
11972     }
11973
11974   if (parent == NULL)
11975     return "";
11976   else if (parent->building_fullname)
11977     {
11978       const char *name;
11979       const char *parent_name;
11980
11981       /* It has been seen on RealView 2.2 built binaries,
11982          DW_TAG_template_type_param types actually _defined_ as
11983          children of the parent class:
11984
11985          enum E {};
11986          template class <class Enum> Class{};
11987          Class<enum E> class_e;
11988
11989          1: DW_TAG_class_type (Class)
11990            2: DW_TAG_enumeration_type (E)
11991              3: DW_TAG_enumerator (enum1:0)
11992              3: DW_TAG_enumerator (enum2:1)
11993              ...
11994            2: DW_TAG_template_type_param
11995               DW_AT_type  DW_FORM_ref_udata (E)
11996
11997          Besides being broken debug info, it can put GDB into an
11998          infinite loop.  Consider:
11999
12000          When we're building the full name for Class<E>, we'll start
12001          at Class, and go look over its template type parameters,
12002          finding E.  We'll then try to build the full name of E, and
12003          reach here.  We're now trying to build the full name of E,
12004          and look over the parent DIE for containing scope.  In the
12005          broken case, if we followed the parent DIE of E, we'd again
12006          find Class, and once again go look at its template type
12007          arguments, etc., etc.  Simply don't consider such parent die
12008          as source-level parent of this die (it can't be, the language
12009          doesn't allow it), and break the loop here.  */
12010       name = dwarf2_name (die, cu);
12011       parent_name = dwarf2_name (parent, cu);
12012       complaint (&symfile_complaints,
12013                  _("template param type '%s' defined within parent '%s'"),
12014                  name ? name : "<unknown>",
12015                  parent_name ? parent_name : "<unknown>");
12016       return "";
12017     }
12018   else
12019     switch (parent->tag)
12020       {
12021       case DW_TAG_namespace:
12022         parent_type = read_type_die (parent, cu);
12023         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12024            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12025            Work around this problem here.  */
12026         if (cu->language == language_cplus
12027             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12028           return "";
12029         /* We give a name to even anonymous namespaces.  */
12030         return TYPE_TAG_NAME (parent_type);
12031       case DW_TAG_class_type:
12032       case DW_TAG_interface_type:
12033       case DW_TAG_structure_type:
12034       case DW_TAG_union_type:
12035       case DW_TAG_module:
12036         parent_type = read_type_die (parent, cu);
12037         if (TYPE_TAG_NAME (parent_type) != NULL)
12038           return TYPE_TAG_NAME (parent_type);
12039         else
12040           /* An anonymous structure is only allowed non-static data
12041              members; no typedefs, no member functions, et cetera.
12042              So it does not need a prefix.  */
12043           return "";
12044       case DW_TAG_compile_unit:
12045         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12046         if (cu->language == language_cplus
12047             && dwarf2_per_objfile->types.asection != NULL
12048             && die->child != NULL
12049             && (die->tag == DW_TAG_class_type
12050                 || die->tag == DW_TAG_structure_type
12051                 || die->tag == DW_TAG_union_type))
12052           {
12053             char *name = guess_full_die_structure_name (die, cu);
12054             if (name != NULL)
12055               return name;
12056           }
12057         return "";
12058       default:
12059         return determine_prefix (parent, cu);
12060       }
12061 }
12062
12063 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12064    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12065    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12066    an obconcat, otherwise allocate storage for the result.  The CU argument is
12067    used to determine the language and hence, the appropriate separator.  */
12068
12069 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12070
12071 static char *
12072 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12073                  int physname, struct dwarf2_cu *cu)
12074 {
12075   const char *lead = "";
12076   const char *sep;
12077
12078   if (suffix == NULL || suffix[0] == '\0'
12079       || prefix == NULL || prefix[0] == '\0')
12080     sep = "";
12081   else if (cu->language == language_java)
12082     sep = ".";
12083   else if (cu->language == language_fortran && physname)
12084     {
12085       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12086          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12087
12088       lead = "__";
12089       sep = "_MOD_";
12090     }
12091   else
12092     sep = "::";
12093
12094   if (prefix == NULL)
12095     prefix = "";
12096   if (suffix == NULL)
12097     suffix = "";
12098
12099   if (obs == NULL)
12100     {
12101       char *retval
12102         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12103
12104       strcpy (retval, lead);
12105       strcat (retval, prefix);
12106       strcat (retval, sep);
12107       strcat (retval, suffix);
12108       return retval;
12109     }
12110   else
12111     {
12112       /* We have an obstack.  */
12113       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12114     }
12115 }
12116
12117 /* Return sibling of die, NULL if no sibling.  */
12118
12119 static struct die_info *
12120 sibling_die (struct die_info *die)
12121 {
12122   return die->sibling;
12123 }
12124
12125 /* Get name of a die, return NULL if not found.  */
12126
12127 static char *
12128 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12129                           struct obstack *obstack)
12130 {
12131   if (name && cu->language == language_cplus)
12132     {
12133       char *canon_name = cp_canonicalize_string (name);
12134
12135       if (canon_name != NULL)
12136         {
12137           if (strcmp (canon_name, name) != 0)
12138             name = obsavestring (canon_name, strlen (canon_name),
12139                                  obstack);
12140           xfree (canon_name);
12141         }
12142     }
12143
12144   return name;
12145 }
12146
12147 /* Get name of a die, return NULL if not found.  */
12148
12149 static char *
12150 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12151 {
12152   struct attribute *attr;
12153
12154   attr = dwarf2_attr (die, DW_AT_name, cu);
12155   if ((!attr || !DW_STRING (attr))
12156       && die->tag != DW_TAG_class_type
12157       && die->tag != DW_TAG_interface_type
12158       && die->tag != DW_TAG_structure_type
12159       && die->tag != DW_TAG_union_type)
12160     return NULL;
12161
12162   switch (die->tag)
12163     {
12164     case DW_TAG_compile_unit:
12165       /* Compilation units have a DW_AT_name that is a filename, not
12166          a source language identifier.  */
12167     case DW_TAG_enumeration_type:
12168     case DW_TAG_enumerator:
12169       /* These tags always have simple identifiers already; no need
12170          to canonicalize them.  */
12171       return DW_STRING (attr);
12172
12173     case DW_TAG_subprogram:
12174       /* Java constructors will all be named "<init>", so return
12175          the class name when we see this special case.  */
12176       if (cu->language == language_java
12177           && DW_STRING (attr) != NULL
12178           && strcmp (DW_STRING (attr), "<init>") == 0)
12179         {
12180           struct dwarf2_cu *spec_cu = cu;
12181           struct die_info *spec_die;
12182
12183           /* GCJ will output '<init>' for Java constructor names.
12184              For this special case, return the name of the parent class.  */
12185
12186           /* GCJ may output suprogram DIEs with AT_specification set.
12187              If so, use the name of the specified DIE.  */
12188           spec_die = die_specification (die, &spec_cu);
12189           if (spec_die != NULL)
12190             return dwarf2_name (spec_die, spec_cu);
12191
12192           do
12193             {
12194               die = die->parent;
12195               if (die->tag == DW_TAG_class_type)
12196                 return dwarf2_name (die, cu);
12197             }
12198           while (die->tag != DW_TAG_compile_unit);
12199         }
12200       break;
12201
12202     case DW_TAG_class_type:
12203     case DW_TAG_interface_type:
12204     case DW_TAG_structure_type:
12205     case DW_TAG_union_type:
12206       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12207          structures or unions.  These were of the form "._%d" in GCC 4.1,
12208          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12209          and GCC 4.4.  We work around this problem by ignoring these.  */
12210       if (attr && DW_STRING (attr)
12211           && (strncmp (DW_STRING (attr), "._", 2) == 0
12212               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12213         return NULL;
12214
12215       /* GCC might emit a nameless typedef that has a linkage name.  See
12216          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12217       if (!attr || DW_STRING (attr) == NULL)
12218         {
12219           char *demangled = NULL;
12220
12221           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12222           if (attr == NULL)
12223             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12224
12225           if (attr == NULL || DW_STRING (attr) == NULL)
12226             return NULL;
12227
12228           /* Avoid demangling DW_STRING (attr) the second time on a second
12229              call for the same DIE.  */
12230           if (!DW_STRING_IS_CANONICAL (attr))
12231             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12232
12233           if (demangled)
12234             {
12235               /* FIXME: we already did this for the partial symbol... */
12236               DW_STRING (attr)
12237                 = obsavestring (demangled, strlen (demangled),
12238                                 &cu->objfile->objfile_obstack);
12239               DW_STRING_IS_CANONICAL (attr) = 1;
12240               xfree (demangled);
12241             }
12242         }
12243       break;
12244
12245     default:
12246       break;
12247     }
12248
12249   if (!DW_STRING_IS_CANONICAL (attr))
12250     {
12251       DW_STRING (attr)
12252         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12253                                     &cu->objfile->objfile_obstack);
12254       DW_STRING_IS_CANONICAL (attr) = 1;
12255     }
12256   return DW_STRING (attr);
12257 }
12258
12259 /* Return the die that this die in an extension of, or NULL if there
12260    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12261    containing the return value on output.  */
12262
12263 static struct die_info *
12264 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12265 {
12266   struct attribute *attr;
12267
12268   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12269   if (attr == NULL)
12270     return NULL;
12271
12272   return follow_die_ref (die, attr, ext_cu);
12273 }
12274
12275 /* Convert a DIE tag into its string name.  */
12276
12277 static char *
12278 dwarf_tag_name (unsigned tag)
12279 {
12280   switch (tag)
12281     {
12282     case DW_TAG_padding:
12283       return "DW_TAG_padding";
12284     case DW_TAG_array_type:
12285       return "DW_TAG_array_type";
12286     case DW_TAG_class_type:
12287       return "DW_TAG_class_type";
12288     case DW_TAG_entry_point:
12289       return "DW_TAG_entry_point";
12290     case DW_TAG_enumeration_type:
12291       return "DW_TAG_enumeration_type";
12292     case DW_TAG_formal_parameter:
12293       return "DW_TAG_formal_parameter";
12294     case DW_TAG_imported_declaration:
12295       return "DW_TAG_imported_declaration";
12296     case DW_TAG_label:
12297       return "DW_TAG_label";
12298     case DW_TAG_lexical_block:
12299       return "DW_TAG_lexical_block";
12300     case DW_TAG_member:
12301       return "DW_TAG_member";
12302     case DW_TAG_pointer_type:
12303       return "DW_TAG_pointer_type";
12304     case DW_TAG_reference_type:
12305       return "DW_TAG_reference_type";
12306     case DW_TAG_compile_unit:
12307       return "DW_TAG_compile_unit";
12308     case DW_TAG_string_type:
12309       return "DW_TAG_string_type";
12310     case DW_TAG_structure_type:
12311       return "DW_TAG_structure_type";
12312     case DW_TAG_subroutine_type:
12313       return "DW_TAG_subroutine_type";
12314     case DW_TAG_typedef:
12315       return "DW_TAG_typedef";
12316     case DW_TAG_union_type:
12317       return "DW_TAG_union_type";
12318     case DW_TAG_unspecified_parameters:
12319       return "DW_TAG_unspecified_parameters";
12320     case DW_TAG_variant:
12321       return "DW_TAG_variant";
12322     case DW_TAG_common_block:
12323       return "DW_TAG_common_block";
12324     case DW_TAG_common_inclusion:
12325       return "DW_TAG_common_inclusion";
12326     case DW_TAG_inheritance:
12327       return "DW_TAG_inheritance";
12328     case DW_TAG_inlined_subroutine:
12329       return "DW_TAG_inlined_subroutine";
12330     case DW_TAG_module:
12331       return "DW_TAG_module";
12332     case DW_TAG_ptr_to_member_type:
12333       return "DW_TAG_ptr_to_member_type";
12334     case DW_TAG_set_type:
12335       return "DW_TAG_set_type";
12336     case DW_TAG_subrange_type:
12337       return "DW_TAG_subrange_type";
12338     case DW_TAG_with_stmt:
12339       return "DW_TAG_with_stmt";
12340     case DW_TAG_access_declaration:
12341       return "DW_TAG_access_declaration";
12342     case DW_TAG_base_type:
12343       return "DW_TAG_base_type";
12344     case DW_TAG_catch_block:
12345       return "DW_TAG_catch_block";
12346     case DW_TAG_const_type:
12347       return "DW_TAG_const_type";
12348     case DW_TAG_constant:
12349       return "DW_TAG_constant";
12350     case DW_TAG_enumerator:
12351       return "DW_TAG_enumerator";
12352     case DW_TAG_file_type:
12353       return "DW_TAG_file_type";
12354     case DW_TAG_friend:
12355       return "DW_TAG_friend";
12356     case DW_TAG_namelist:
12357       return "DW_TAG_namelist";
12358     case DW_TAG_namelist_item:
12359       return "DW_TAG_namelist_item";
12360     case DW_TAG_packed_type:
12361       return "DW_TAG_packed_type";
12362     case DW_TAG_subprogram:
12363       return "DW_TAG_subprogram";
12364     case DW_TAG_template_type_param:
12365       return "DW_TAG_template_type_param";
12366     case DW_TAG_template_value_param:
12367       return "DW_TAG_template_value_param";
12368     case DW_TAG_thrown_type:
12369       return "DW_TAG_thrown_type";
12370     case DW_TAG_try_block:
12371       return "DW_TAG_try_block";
12372     case DW_TAG_variant_part:
12373       return "DW_TAG_variant_part";
12374     case DW_TAG_variable:
12375       return "DW_TAG_variable";
12376     case DW_TAG_volatile_type:
12377       return "DW_TAG_volatile_type";
12378     case DW_TAG_dwarf_procedure:
12379       return "DW_TAG_dwarf_procedure";
12380     case DW_TAG_restrict_type:
12381       return "DW_TAG_restrict_type";
12382     case DW_TAG_interface_type:
12383       return "DW_TAG_interface_type";
12384     case DW_TAG_namespace:
12385       return "DW_TAG_namespace";
12386     case DW_TAG_imported_module:
12387       return "DW_TAG_imported_module";
12388     case DW_TAG_unspecified_type:
12389       return "DW_TAG_unspecified_type";
12390     case DW_TAG_partial_unit:
12391       return "DW_TAG_partial_unit";
12392     case DW_TAG_imported_unit:
12393       return "DW_TAG_imported_unit";
12394     case DW_TAG_condition:
12395       return "DW_TAG_condition";
12396     case DW_TAG_shared_type:
12397       return "DW_TAG_shared_type";
12398     case DW_TAG_type_unit:
12399       return "DW_TAG_type_unit";
12400     case DW_TAG_MIPS_loop:
12401       return "DW_TAG_MIPS_loop";
12402     case DW_TAG_HP_array_descriptor:
12403       return "DW_TAG_HP_array_descriptor";
12404     case DW_TAG_format_label:
12405       return "DW_TAG_format_label";
12406     case DW_TAG_function_template:
12407       return "DW_TAG_function_template";
12408     case DW_TAG_class_template:
12409       return "DW_TAG_class_template";
12410     case DW_TAG_GNU_BINCL:
12411       return "DW_TAG_GNU_BINCL";
12412     case DW_TAG_GNU_EINCL:
12413       return "DW_TAG_GNU_EINCL";
12414     case DW_TAG_upc_shared_type:
12415       return "DW_TAG_upc_shared_type";
12416     case DW_TAG_upc_strict_type:
12417       return "DW_TAG_upc_strict_type";
12418     case DW_TAG_upc_relaxed_type:
12419       return "DW_TAG_upc_relaxed_type";
12420     case DW_TAG_PGI_kanji_type:
12421       return "DW_TAG_PGI_kanji_type";
12422     case DW_TAG_PGI_interface_block:
12423       return "DW_TAG_PGI_interface_block";
12424     default:
12425       return "DW_TAG_<unknown>";
12426     }
12427 }
12428
12429 /* Convert a DWARF attribute code into its string name.  */
12430
12431 static char *
12432 dwarf_attr_name (unsigned attr)
12433 {
12434   switch (attr)
12435     {
12436     case DW_AT_sibling:
12437       return "DW_AT_sibling";
12438     case DW_AT_location:
12439       return "DW_AT_location";
12440     case DW_AT_name:
12441       return "DW_AT_name";
12442     case DW_AT_ordering:
12443       return "DW_AT_ordering";
12444     case DW_AT_subscr_data:
12445       return "DW_AT_subscr_data";
12446     case DW_AT_byte_size:
12447       return "DW_AT_byte_size";
12448     case DW_AT_bit_offset:
12449       return "DW_AT_bit_offset";
12450     case DW_AT_bit_size:
12451       return "DW_AT_bit_size";
12452     case DW_AT_element_list:
12453       return "DW_AT_element_list";
12454     case DW_AT_stmt_list:
12455       return "DW_AT_stmt_list";
12456     case DW_AT_low_pc:
12457       return "DW_AT_low_pc";
12458     case DW_AT_high_pc:
12459       return "DW_AT_high_pc";
12460     case DW_AT_language:
12461       return "DW_AT_language";
12462     case DW_AT_member:
12463       return "DW_AT_member";
12464     case DW_AT_discr:
12465       return "DW_AT_discr";
12466     case DW_AT_discr_value:
12467       return "DW_AT_discr_value";
12468     case DW_AT_visibility:
12469       return "DW_AT_visibility";
12470     case DW_AT_import:
12471       return "DW_AT_import";
12472     case DW_AT_string_length:
12473       return "DW_AT_string_length";
12474     case DW_AT_common_reference:
12475       return "DW_AT_common_reference";
12476     case DW_AT_comp_dir:
12477       return "DW_AT_comp_dir";
12478     case DW_AT_const_value:
12479       return "DW_AT_const_value";
12480     case DW_AT_containing_type:
12481       return "DW_AT_containing_type";
12482     case DW_AT_default_value:
12483       return "DW_AT_default_value";
12484     case DW_AT_inline:
12485       return "DW_AT_inline";
12486     case DW_AT_is_optional:
12487       return "DW_AT_is_optional";
12488     case DW_AT_lower_bound:
12489       return "DW_AT_lower_bound";
12490     case DW_AT_producer:
12491       return "DW_AT_producer";
12492     case DW_AT_prototyped:
12493       return "DW_AT_prototyped";
12494     case DW_AT_return_addr:
12495       return "DW_AT_return_addr";
12496     case DW_AT_start_scope:
12497       return "DW_AT_start_scope";
12498     case DW_AT_bit_stride:
12499       return "DW_AT_bit_stride";
12500     case DW_AT_upper_bound:
12501       return "DW_AT_upper_bound";
12502     case DW_AT_abstract_origin:
12503       return "DW_AT_abstract_origin";
12504     case DW_AT_accessibility:
12505       return "DW_AT_accessibility";
12506     case DW_AT_address_class:
12507       return "DW_AT_address_class";
12508     case DW_AT_artificial:
12509       return "DW_AT_artificial";
12510     case DW_AT_base_types:
12511       return "DW_AT_base_types";
12512     case DW_AT_calling_convention:
12513       return "DW_AT_calling_convention";
12514     case DW_AT_count:
12515       return "DW_AT_count";
12516     case DW_AT_data_member_location:
12517       return "DW_AT_data_member_location";
12518     case DW_AT_decl_column:
12519       return "DW_AT_decl_column";
12520     case DW_AT_decl_file:
12521       return "DW_AT_decl_file";
12522     case DW_AT_decl_line:
12523       return "DW_AT_decl_line";
12524     case DW_AT_declaration:
12525       return "DW_AT_declaration";
12526     case DW_AT_discr_list:
12527       return "DW_AT_discr_list";
12528     case DW_AT_encoding:
12529       return "DW_AT_encoding";
12530     case DW_AT_external:
12531       return "DW_AT_external";
12532     case DW_AT_frame_base:
12533       return "DW_AT_frame_base";
12534     case DW_AT_friend:
12535       return "DW_AT_friend";
12536     case DW_AT_identifier_case:
12537       return "DW_AT_identifier_case";
12538     case DW_AT_macro_info:
12539       return "DW_AT_macro_info";
12540     case DW_AT_namelist_items:
12541       return "DW_AT_namelist_items";
12542     case DW_AT_priority:
12543       return "DW_AT_priority";
12544     case DW_AT_segment:
12545       return "DW_AT_segment";
12546     case DW_AT_specification:
12547       return "DW_AT_specification";
12548     case DW_AT_static_link:
12549       return "DW_AT_static_link";
12550     case DW_AT_type:
12551       return "DW_AT_type";
12552     case DW_AT_use_location:
12553       return "DW_AT_use_location";
12554     case DW_AT_variable_parameter:
12555       return "DW_AT_variable_parameter";
12556     case DW_AT_virtuality:
12557       return "DW_AT_virtuality";
12558     case DW_AT_vtable_elem_location:
12559       return "DW_AT_vtable_elem_location";
12560     /* DWARF 3 values.  */
12561     case DW_AT_allocated:
12562       return "DW_AT_allocated";
12563     case DW_AT_associated:
12564       return "DW_AT_associated";
12565     case DW_AT_data_location:
12566       return "DW_AT_data_location";
12567     case DW_AT_byte_stride:
12568       return "DW_AT_byte_stride";
12569     case DW_AT_entry_pc:
12570       return "DW_AT_entry_pc";
12571     case DW_AT_use_UTF8:
12572       return "DW_AT_use_UTF8";
12573     case DW_AT_extension:
12574       return "DW_AT_extension";
12575     case DW_AT_ranges:
12576       return "DW_AT_ranges";
12577     case DW_AT_trampoline:
12578       return "DW_AT_trampoline";
12579     case DW_AT_call_column:
12580       return "DW_AT_call_column";
12581     case DW_AT_call_file:
12582       return "DW_AT_call_file";
12583     case DW_AT_call_line:
12584       return "DW_AT_call_line";
12585     case DW_AT_description:
12586       return "DW_AT_description";
12587     case DW_AT_binary_scale:
12588       return "DW_AT_binary_scale";
12589     case DW_AT_decimal_scale:
12590       return "DW_AT_decimal_scale";
12591     case DW_AT_small:
12592       return "DW_AT_small";
12593     case DW_AT_decimal_sign:
12594       return "DW_AT_decimal_sign";
12595     case DW_AT_digit_count:
12596       return "DW_AT_digit_count";
12597     case DW_AT_picture_string:
12598       return "DW_AT_picture_string";
12599     case DW_AT_mutable:
12600       return "DW_AT_mutable";
12601     case DW_AT_threads_scaled:
12602       return "DW_AT_threads_scaled";
12603     case DW_AT_explicit:
12604       return "DW_AT_explicit";
12605     case DW_AT_object_pointer:
12606       return "DW_AT_object_pointer";
12607     case DW_AT_endianity:
12608       return "DW_AT_endianity";
12609     case DW_AT_elemental:
12610       return "DW_AT_elemental";
12611     case DW_AT_pure:
12612       return "DW_AT_pure";
12613     case DW_AT_recursive:
12614       return "DW_AT_recursive";
12615     /* DWARF 4 values.  */
12616     case DW_AT_signature:
12617       return "DW_AT_signature";
12618     case DW_AT_linkage_name:
12619       return "DW_AT_linkage_name";
12620     /* SGI/MIPS extensions.  */
12621 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12622     case DW_AT_MIPS_fde:
12623       return "DW_AT_MIPS_fde";
12624 #endif
12625     case DW_AT_MIPS_loop_begin:
12626       return "DW_AT_MIPS_loop_begin";
12627     case DW_AT_MIPS_tail_loop_begin:
12628       return "DW_AT_MIPS_tail_loop_begin";
12629     case DW_AT_MIPS_epilog_begin:
12630       return "DW_AT_MIPS_epilog_begin";
12631     case DW_AT_MIPS_loop_unroll_factor:
12632       return "DW_AT_MIPS_loop_unroll_factor";
12633     case DW_AT_MIPS_software_pipeline_depth:
12634       return "DW_AT_MIPS_software_pipeline_depth";
12635     case DW_AT_MIPS_linkage_name:
12636       return "DW_AT_MIPS_linkage_name";
12637     case DW_AT_MIPS_stride:
12638       return "DW_AT_MIPS_stride";
12639     case DW_AT_MIPS_abstract_name:
12640       return "DW_AT_MIPS_abstract_name";
12641     case DW_AT_MIPS_clone_origin:
12642       return "DW_AT_MIPS_clone_origin";
12643     case DW_AT_MIPS_has_inlines:
12644       return "DW_AT_MIPS_has_inlines";
12645     /* HP extensions.  */
12646 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12647     case DW_AT_HP_block_index:
12648       return "DW_AT_HP_block_index";
12649 #endif
12650     case DW_AT_HP_unmodifiable:
12651       return "DW_AT_HP_unmodifiable";
12652     case DW_AT_HP_actuals_stmt_list:
12653       return "DW_AT_HP_actuals_stmt_list";
12654     case DW_AT_HP_proc_per_section:
12655       return "DW_AT_HP_proc_per_section";
12656     case DW_AT_HP_raw_data_ptr:
12657       return "DW_AT_HP_raw_data_ptr";
12658     case DW_AT_HP_pass_by_reference:
12659       return "DW_AT_HP_pass_by_reference";
12660     case DW_AT_HP_opt_level:
12661       return "DW_AT_HP_opt_level";
12662     case DW_AT_HP_prof_version_id:
12663       return "DW_AT_HP_prof_version_id";
12664     case DW_AT_HP_opt_flags:
12665       return "DW_AT_HP_opt_flags";
12666     case DW_AT_HP_cold_region_low_pc:
12667       return "DW_AT_HP_cold_region_low_pc";
12668     case DW_AT_HP_cold_region_high_pc:
12669       return "DW_AT_HP_cold_region_high_pc";
12670     case DW_AT_HP_all_variables_modifiable:
12671       return "DW_AT_HP_all_variables_modifiable";
12672     case DW_AT_HP_linkage_name:
12673       return "DW_AT_HP_linkage_name";
12674     case DW_AT_HP_prof_flags:
12675       return "DW_AT_HP_prof_flags";
12676     /* GNU extensions.  */
12677     case DW_AT_sf_names:
12678       return "DW_AT_sf_names";
12679     case DW_AT_src_info:
12680       return "DW_AT_src_info";
12681     case DW_AT_mac_info:
12682       return "DW_AT_mac_info";
12683     case DW_AT_src_coords:
12684       return "DW_AT_src_coords";
12685     case DW_AT_body_begin:
12686       return "DW_AT_body_begin";
12687     case DW_AT_body_end:
12688       return "DW_AT_body_end";
12689     case DW_AT_GNU_vector:
12690       return "DW_AT_GNU_vector";
12691     case DW_AT_GNU_odr_signature:
12692       return "DW_AT_GNU_odr_signature";
12693     /* VMS extensions.  */
12694     case DW_AT_VMS_rtnbeg_pd_address:
12695       return "DW_AT_VMS_rtnbeg_pd_address";
12696     /* UPC extension.  */
12697     case DW_AT_upc_threads_scaled:
12698       return "DW_AT_upc_threads_scaled";
12699     /* PGI (STMicroelectronics) extensions.  */
12700     case DW_AT_PGI_lbase:
12701       return "DW_AT_PGI_lbase";
12702     case DW_AT_PGI_soffset:
12703       return "DW_AT_PGI_soffset";
12704     case DW_AT_PGI_lstride:
12705       return "DW_AT_PGI_lstride";
12706     default:
12707       return "DW_AT_<unknown>";
12708     }
12709 }
12710
12711 /* Convert a DWARF value form code into its string name.  */
12712
12713 static char *
12714 dwarf_form_name (unsigned form)
12715 {
12716   switch (form)
12717     {
12718     case DW_FORM_addr:
12719       return "DW_FORM_addr";
12720     case DW_FORM_block2:
12721       return "DW_FORM_block2";
12722     case DW_FORM_block4:
12723       return "DW_FORM_block4";
12724     case DW_FORM_data2:
12725       return "DW_FORM_data2";
12726     case DW_FORM_data4:
12727       return "DW_FORM_data4";
12728     case DW_FORM_data8:
12729       return "DW_FORM_data8";
12730     case DW_FORM_string:
12731       return "DW_FORM_string";
12732     case DW_FORM_block:
12733       return "DW_FORM_block";
12734     case DW_FORM_block1:
12735       return "DW_FORM_block1";
12736     case DW_FORM_data1:
12737       return "DW_FORM_data1";
12738     case DW_FORM_flag:
12739       return "DW_FORM_flag";
12740     case DW_FORM_sdata:
12741       return "DW_FORM_sdata";
12742     case DW_FORM_strp:
12743       return "DW_FORM_strp";
12744     case DW_FORM_udata:
12745       return "DW_FORM_udata";
12746     case DW_FORM_ref_addr:
12747       return "DW_FORM_ref_addr";
12748     case DW_FORM_ref1:
12749       return "DW_FORM_ref1";
12750     case DW_FORM_ref2:
12751       return "DW_FORM_ref2";
12752     case DW_FORM_ref4:
12753       return "DW_FORM_ref4";
12754     case DW_FORM_ref8:
12755       return "DW_FORM_ref8";
12756     case DW_FORM_ref_udata:
12757       return "DW_FORM_ref_udata";
12758     case DW_FORM_indirect:
12759       return "DW_FORM_indirect";
12760     case DW_FORM_sec_offset:
12761       return "DW_FORM_sec_offset";
12762     case DW_FORM_exprloc:
12763       return "DW_FORM_exprloc";
12764     case DW_FORM_flag_present:
12765       return "DW_FORM_flag_present";
12766     case DW_FORM_ref_sig8:
12767       return "DW_FORM_ref_sig8";
12768     default:
12769       return "DW_FORM_<unknown>";
12770     }
12771 }
12772
12773 /* Convert a DWARF stack opcode into its string name.  */
12774
12775 const char *
12776 dwarf_stack_op_name (unsigned op)
12777 {
12778   switch (op)
12779     {
12780     case DW_OP_addr:
12781       return "DW_OP_addr";
12782     case DW_OP_deref:
12783       return "DW_OP_deref";
12784     case DW_OP_const1u:
12785       return "DW_OP_const1u";
12786     case DW_OP_const1s:
12787       return "DW_OP_const1s";
12788     case DW_OP_const2u:
12789       return "DW_OP_const2u";
12790     case DW_OP_const2s:
12791       return "DW_OP_const2s";
12792     case DW_OP_const4u:
12793       return "DW_OP_const4u";
12794     case DW_OP_const4s:
12795       return "DW_OP_const4s";
12796     case DW_OP_const8u:
12797       return "DW_OP_const8u";
12798     case DW_OP_const8s:
12799       return "DW_OP_const8s";
12800     case DW_OP_constu:
12801       return "DW_OP_constu";
12802     case DW_OP_consts:
12803       return "DW_OP_consts";
12804     case DW_OP_dup:
12805       return "DW_OP_dup";
12806     case DW_OP_drop:
12807       return "DW_OP_drop";
12808     case DW_OP_over:
12809       return "DW_OP_over";
12810     case DW_OP_pick:
12811       return "DW_OP_pick";
12812     case DW_OP_swap:
12813       return "DW_OP_swap";
12814     case DW_OP_rot:
12815       return "DW_OP_rot";
12816     case DW_OP_xderef:
12817       return "DW_OP_xderef";
12818     case DW_OP_abs:
12819       return "DW_OP_abs";
12820     case DW_OP_and:
12821       return "DW_OP_and";
12822     case DW_OP_div:
12823       return "DW_OP_div";
12824     case DW_OP_minus:
12825       return "DW_OP_minus";
12826     case DW_OP_mod:
12827       return "DW_OP_mod";
12828     case DW_OP_mul:
12829       return "DW_OP_mul";
12830     case DW_OP_neg:
12831       return "DW_OP_neg";
12832     case DW_OP_not:
12833       return "DW_OP_not";
12834     case DW_OP_or:
12835       return "DW_OP_or";
12836     case DW_OP_plus:
12837       return "DW_OP_plus";
12838     case DW_OP_plus_uconst:
12839       return "DW_OP_plus_uconst";
12840     case DW_OP_shl:
12841       return "DW_OP_shl";
12842     case DW_OP_shr:
12843       return "DW_OP_shr";
12844     case DW_OP_shra:
12845       return "DW_OP_shra";
12846     case DW_OP_xor:
12847       return "DW_OP_xor";
12848     case DW_OP_bra:
12849       return "DW_OP_bra";
12850     case DW_OP_eq:
12851       return "DW_OP_eq";
12852     case DW_OP_ge:
12853       return "DW_OP_ge";
12854     case DW_OP_gt:
12855       return "DW_OP_gt";
12856     case DW_OP_le:
12857       return "DW_OP_le";
12858     case DW_OP_lt:
12859       return "DW_OP_lt";
12860     case DW_OP_ne:
12861       return "DW_OP_ne";
12862     case DW_OP_skip:
12863       return "DW_OP_skip";
12864     case DW_OP_lit0:
12865       return "DW_OP_lit0";
12866     case DW_OP_lit1:
12867       return "DW_OP_lit1";
12868     case DW_OP_lit2:
12869       return "DW_OP_lit2";
12870     case DW_OP_lit3:
12871       return "DW_OP_lit3";
12872     case DW_OP_lit4:
12873       return "DW_OP_lit4";
12874     case DW_OP_lit5:
12875       return "DW_OP_lit5";
12876     case DW_OP_lit6:
12877       return "DW_OP_lit6";
12878     case DW_OP_lit7:
12879       return "DW_OP_lit7";
12880     case DW_OP_lit8:
12881       return "DW_OP_lit8";
12882     case DW_OP_lit9:
12883       return "DW_OP_lit9";
12884     case DW_OP_lit10:
12885       return "DW_OP_lit10";
12886     case DW_OP_lit11:
12887       return "DW_OP_lit11";
12888     case DW_OP_lit12:
12889       return "DW_OP_lit12";
12890     case DW_OP_lit13:
12891       return "DW_OP_lit13";
12892     case DW_OP_lit14:
12893       return "DW_OP_lit14";
12894     case DW_OP_lit15:
12895       return "DW_OP_lit15";
12896     case DW_OP_lit16:
12897       return "DW_OP_lit16";
12898     case DW_OP_lit17:
12899       return "DW_OP_lit17";
12900     case DW_OP_lit18:
12901       return "DW_OP_lit18";
12902     case DW_OP_lit19:
12903       return "DW_OP_lit19";
12904     case DW_OP_lit20:
12905       return "DW_OP_lit20";
12906     case DW_OP_lit21:
12907       return "DW_OP_lit21";
12908     case DW_OP_lit22:
12909       return "DW_OP_lit22";
12910     case DW_OP_lit23:
12911       return "DW_OP_lit23";
12912     case DW_OP_lit24:
12913       return "DW_OP_lit24";
12914     case DW_OP_lit25:
12915       return "DW_OP_lit25";
12916     case DW_OP_lit26:
12917       return "DW_OP_lit26";
12918     case DW_OP_lit27:
12919       return "DW_OP_lit27";
12920     case DW_OP_lit28:
12921       return "DW_OP_lit28";
12922     case DW_OP_lit29:
12923       return "DW_OP_lit29";
12924     case DW_OP_lit30:
12925       return "DW_OP_lit30";
12926     case DW_OP_lit31:
12927       return "DW_OP_lit31";
12928     case DW_OP_reg0:
12929       return "DW_OP_reg0";
12930     case DW_OP_reg1:
12931       return "DW_OP_reg1";
12932     case DW_OP_reg2:
12933       return "DW_OP_reg2";
12934     case DW_OP_reg3:
12935       return "DW_OP_reg3";
12936     case DW_OP_reg4:
12937       return "DW_OP_reg4";
12938     case DW_OP_reg5:
12939       return "DW_OP_reg5";
12940     case DW_OP_reg6:
12941       return "DW_OP_reg6";
12942     case DW_OP_reg7:
12943       return "DW_OP_reg7";
12944     case DW_OP_reg8:
12945       return "DW_OP_reg8";
12946     case DW_OP_reg9:
12947       return "DW_OP_reg9";
12948     case DW_OP_reg10:
12949       return "DW_OP_reg10";
12950     case DW_OP_reg11:
12951       return "DW_OP_reg11";
12952     case DW_OP_reg12:
12953       return "DW_OP_reg12";
12954     case DW_OP_reg13:
12955       return "DW_OP_reg13";
12956     case DW_OP_reg14:
12957       return "DW_OP_reg14";
12958     case DW_OP_reg15:
12959       return "DW_OP_reg15";
12960     case DW_OP_reg16:
12961       return "DW_OP_reg16";
12962     case DW_OP_reg17:
12963       return "DW_OP_reg17";
12964     case DW_OP_reg18:
12965       return "DW_OP_reg18";
12966     case DW_OP_reg19:
12967       return "DW_OP_reg19";
12968     case DW_OP_reg20:
12969       return "DW_OP_reg20";
12970     case DW_OP_reg21:
12971       return "DW_OP_reg21";
12972     case DW_OP_reg22:
12973       return "DW_OP_reg22";
12974     case DW_OP_reg23:
12975       return "DW_OP_reg23";
12976     case DW_OP_reg24:
12977       return "DW_OP_reg24";
12978     case DW_OP_reg25:
12979       return "DW_OP_reg25";
12980     case DW_OP_reg26:
12981       return "DW_OP_reg26";
12982     case DW_OP_reg27:
12983       return "DW_OP_reg27";
12984     case DW_OP_reg28:
12985       return "DW_OP_reg28";
12986     case DW_OP_reg29:
12987       return "DW_OP_reg29";
12988     case DW_OP_reg30:
12989       return "DW_OP_reg30";
12990     case DW_OP_reg31:
12991       return "DW_OP_reg31";
12992     case DW_OP_breg0:
12993       return "DW_OP_breg0";
12994     case DW_OP_breg1:
12995       return "DW_OP_breg1";
12996     case DW_OP_breg2:
12997       return "DW_OP_breg2";
12998     case DW_OP_breg3:
12999       return "DW_OP_breg3";
13000     case DW_OP_breg4:
13001       return "DW_OP_breg4";
13002     case DW_OP_breg5:
13003       return "DW_OP_breg5";
13004     case DW_OP_breg6:
13005       return "DW_OP_breg6";
13006     case DW_OP_breg7:
13007       return "DW_OP_breg7";
13008     case DW_OP_breg8:
13009       return "DW_OP_breg8";
13010     case DW_OP_breg9:
13011       return "DW_OP_breg9";
13012     case DW_OP_breg10:
13013       return "DW_OP_breg10";
13014     case DW_OP_breg11:
13015       return "DW_OP_breg11";
13016     case DW_OP_breg12:
13017       return "DW_OP_breg12";
13018     case DW_OP_breg13:
13019       return "DW_OP_breg13";
13020     case DW_OP_breg14:
13021       return "DW_OP_breg14";
13022     case DW_OP_breg15:
13023       return "DW_OP_breg15";
13024     case DW_OP_breg16:
13025       return "DW_OP_breg16";
13026     case DW_OP_breg17:
13027       return "DW_OP_breg17";
13028     case DW_OP_breg18:
13029       return "DW_OP_breg18";
13030     case DW_OP_breg19:
13031       return "DW_OP_breg19";
13032     case DW_OP_breg20:
13033       return "DW_OP_breg20";
13034     case DW_OP_breg21:
13035       return "DW_OP_breg21";
13036     case DW_OP_breg22:
13037       return "DW_OP_breg22";
13038     case DW_OP_breg23:
13039       return "DW_OP_breg23";
13040     case DW_OP_breg24:
13041       return "DW_OP_breg24";
13042     case DW_OP_breg25:
13043       return "DW_OP_breg25";
13044     case DW_OP_breg26:
13045       return "DW_OP_breg26";
13046     case DW_OP_breg27:
13047       return "DW_OP_breg27";
13048     case DW_OP_breg28:
13049       return "DW_OP_breg28";
13050     case DW_OP_breg29:
13051       return "DW_OP_breg29";
13052     case DW_OP_breg30:
13053       return "DW_OP_breg30";
13054     case DW_OP_breg31:
13055       return "DW_OP_breg31";
13056     case DW_OP_regx:
13057       return "DW_OP_regx";
13058     case DW_OP_fbreg:
13059       return "DW_OP_fbreg";
13060     case DW_OP_bregx:
13061       return "DW_OP_bregx";
13062     case DW_OP_piece:
13063       return "DW_OP_piece";
13064     case DW_OP_deref_size:
13065       return "DW_OP_deref_size";
13066     case DW_OP_xderef_size:
13067       return "DW_OP_xderef_size";
13068     case DW_OP_nop:
13069       return "DW_OP_nop";
13070     /* DWARF 3 extensions.  */
13071     case DW_OP_push_object_address:
13072       return "DW_OP_push_object_address";
13073     case DW_OP_call2:
13074       return "DW_OP_call2";
13075     case DW_OP_call4:
13076       return "DW_OP_call4";
13077     case DW_OP_call_ref:
13078       return "DW_OP_call_ref";
13079     case DW_OP_form_tls_address:
13080       return "DW_OP_form_tls_address";
13081     case DW_OP_call_frame_cfa:
13082       return "DW_OP_call_frame_cfa";
13083     case DW_OP_bit_piece:
13084       return "DW_OP_bit_piece";
13085     /* DWARF 4 extensions.  */
13086     case DW_OP_implicit_value:
13087       return "DW_OP_implicit_value";
13088     case DW_OP_stack_value:
13089       return "DW_OP_stack_value";
13090     /* GNU extensions.  */
13091     case DW_OP_GNU_push_tls_address:
13092       return "DW_OP_GNU_push_tls_address";
13093     case DW_OP_GNU_uninit:
13094       return "DW_OP_GNU_uninit";
13095     case DW_OP_GNU_implicit_pointer:
13096       return "DW_OP_GNU_implicit_pointer";
13097     case DW_OP_GNU_entry_value:
13098       return "DW_OP_GNU_entry_value";
13099     case DW_OP_GNU_const_type:
13100       return "DW_OP_GNU_const_type";
13101     case DW_OP_GNU_regval_type:
13102       return "DW_OP_GNU_regval_type";
13103     case DW_OP_GNU_deref_type:
13104       return "DW_OP_GNU_deref_type";
13105     case DW_OP_GNU_convert:
13106       return "DW_OP_GNU_convert";
13107     case DW_OP_GNU_reinterpret:
13108       return "DW_OP_GNU_reinterpret";
13109     default:
13110       return NULL;
13111     }
13112 }
13113
13114 static char *
13115 dwarf_bool_name (unsigned mybool)
13116 {
13117   if (mybool)
13118     return "TRUE";
13119   else
13120     return "FALSE";
13121 }
13122
13123 /* Convert a DWARF type code into its string name.  */
13124
13125 static char *
13126 dwarf_type_encoding_name (unsigned enc)
13127 {
13128   switch (enc)
13129     {
13130     case DW_ATE_void:
13131       return "DW_ATE_void";
13132     case DW_ATE_address:
13133       return "DW_ATE_address";
13134     case DW_ATE_boolean:
13135       return "DW_ATE_boolean";
13136     case DW_ATE_complex_float:
13137       return "DW_ATE_complex_float";
13138     case DW_ATE_float:
13139       return "DW_ATE_float";
13140     case DW_ATE_signed:
13141       return "DW_ATE_signed";
13142     case DW_ATE_signed_char:
13143       return "DW_ATE_signed_char";
13144     case DW_ATE_unsigned:
13145       return "DW_ATE_unsigned";
13146     case DW_ATE_unsigned_char:
13147       return "DW_ATE_unsigned_char";
13148     /* DWARF 3.  */
13149     case DW_ATE_imaginary_float:
13150       return "DW_ATE_imaginary_float";
13151     case DW_ATE_packed_decimal:
13152       return "DW_ATE_packed_decimal";
13153     case DW_ATE_numeric_string:
13154       return "DW_ATE_numeric_string";
13155     case DW_ATE_edited:
13156       return "DW_ATE_edited";
13157     case DW_ATE_signed_fixed:
13158       return "DW_ATE_signed_fixed";
13159     case DW_ATE_unsigned_fixed:
13160       return "DW_ATE_unsigned_fixed";
13161     case DW_ATE_decimal_float:
13162       return "DW_ATE_decimal_float";
13163     /* DWARF 4.  */
13164     case DW_ATE_UTF:
13165       return "DW_ATE_UTF";
13166     /* HP extensions.  */
13167     case DW_ATE_HP_float80:
13168       return "DW_ATE_HP_float80";
13169     case DW_ATE_HP_complex_float80:
13170       return "DW_ATE_HP_complex_float80";
13171     case DW_ATE_HP_float128:
13172       return "DW_ATE_HP_float128";
13173     case DW_ATE_HP_complex_float128:
13174       return "DW_ATE_HP_complex_float128";
13175     case DW_ATE_HP_floathpintel:
13176       return "DW_ATE_HP_floathpintel";
13177     case DW_ATE_HP_imaginary_float80:
13178       return "DW_ATE_HP_imaginary_float80";
13179     case DW_ATE_HP_imaginary_float128:
13180       return "DW_ATE_HP_imaginary_float128";
13181     default:
13182       return "DW_ATE_<unknown>";
13183     }
13184 }
13185
13186 /* Convert a DWARF call frame info operation to its string name.  */
13187
13188 #if 0
13189 static char *
13190 dwarf_cfi_name (unsigned cfi_opc)
13191 {
13192   switch (cfi_opc)
13193     {
13194     case DW_CFA_advance_loc:
13195       return "DW_CFA_advance_loc";
13196     case DW_CFA_offset:
13197       return "DW_CFA_offset";
13198     case DW_CFA_restore:
13199       return "DW_CFA_restore";
13200     case DW_CFA_nop:
13201       return "DW_CFA_nop";
13202     case DW_CFA_set_loc:
13203       return "DW_CFA_set_loc";
13204     case DW_CFA_advance_loc1:
13205       return "DW_CFA_advance_loc1";
13206     case DW_CFA_advance_loc2:
13207       return "DW_CFA_advance_loc2";
13208     case DW_CFA_advance_loc4:
13209       return "DW_CFA_advance_loc4";
13210     case DW_CFA_offset_extended:
13211       return "DW_CFA_offset_extended";
13212     case DW_CFA_restore_extended:
13213       return "DW_CFA_restore_extended";
13214     case DW_CFA_undefined:
13215       return "DW_CFA_undefined";
13216     case DW_CFA_same_value:
13217       return "DW_CFA_same_value";
13218     case DW_CFA_register:
13219       return "DW_CFA_register";
13220     case DW_CFA_remember_state:
13221       return "DW_CFA_remember_state";
13222     case DW_CFA_restore_state:
13223       return "DW_CFA_restore_state";
13224     case DW_CFA_def_cfa:
13225       return "DW_CFA_def_cfa";
13226     case DW_CFA_def_cfa_register:
13227       return "DW_CFA_def_cfa_register";
13228     case DW_CFA_def_cfa_offset:
13229       return "DW_CFA_def_cfa_offset";
13230     /* DWARF 3.  */
13231     case DW_CFA_def_cfa_expression:
13232       return "DW_CFA_def_cfa_expression";
13233     case DW_CFA_expression:
13234       return "DW_CFA_expression";
13235     case DW_CFA_offset_extended_sf:
13236       return "DW_CFA_offset_extended_sf";
13237     case DW_CFA_def_cfa_sf:
13238       return "DW_CFA_def_cfa_sf";
13239     case DW_CFA_def_cfa_offset_sf:
13240       return "DW_CFA_def_cfa_offset_sf";
13241     case DW_CFA_val_offset:
13242       return "DW_CFA_val_offset";
13243     case DW_CFA_val_offset_sf:
13244       return "DW_CFA_val_offset_sf";
13245     case DW_CFA_val_expression:
13246       return "DW_CFA_val_expression";
13247     /* SGI/MIPS specific.  */
13248     case DW_CFA_MIPS_advance_loc8:
13249       return "DW_CFA_MIPS_advance_loc8";
13250     /* GNU extensions.  */
13251     case DW_CFA_GNU_window_save:
13252       return "DW_CFA_GNU_window_save";
13253     case DW_CFA_GNU_args_size:
13254       return "DW_CFA_GNU_args_size";
13255     case DW_CFA_GNU_negative_offset_extended:
13256       return "DW_CFA_GNU_negative_offset_extended";
13257     default:
13258       return "DW_CFA_<unknown>";
13259     }
13260 }
13261 #endif
13262
13263 static void
13264 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13265 {
13266   unsigned int i;
13267
13268   print_spaces (indent, f);
13269   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13270            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13271
13272   if (die->parent != NULL)
13273     {
13274       print_spaces (indent, f);
13275       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13276                           die->parent->offset);
13277     }
13278
13279   print_spaces (indent, f);
13280   fprintf_unfiltered (f, "  has children: %s\n",
13281            dwarf_bool_name (die->child != NULL));
13282
13283   print_spaces (indent, f);
13284   fprintf_unfiltered (f, "  attributes:\n");
13285
13286   for (i = 0; i < die->num_attrs; ++i)
13287     {
13288       print_spaces (indent, f);
13289       fprintf_unfiltered (f, "    %s (%s) ",
13290                dwarf_attr_name (die->attrs[i].name),
13291                dwarf_form_name (die->attrs[i].form));
13292
13293       switch (die->attrs[i].form)
13294         {
13295         case DW_FORM_ref_addr:
13296         case DW_FORM_addr:
13297           fprintf_unfiltered (f, "address: ");
13298           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13299           break;
13300         case DW_FORM_block2:
13301         case DW_FORM_block4:
13302         case DW_FORM_block:
13303         case DW_FORM_block1:
13304           fprintf_unfiltered (f, "block: size %d",
13305                               DW_BLOCK (&die->attrs[i])->size);
13306           break;
13307         case DW_FORM_exprloc:
13308           fprintf_unfiltered (f, "expression: size %u",
13309                               DW_BLOCK (&die->attrs[i])->size);
13310           break;
13311         case DW_FORM_ref1:
13312         case DW_FORM_ref2:
13313         case DW_FORM_ref4:
13314           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13315                               (long) (DW_ADDR (&die->attrs[i])));
13316           break;
13317         case DW_FORM_data1:
13318         case DW_FORM_data2:
13319         case DW_FORM_data4:
13320         case DW_FORM_data8:
13321         case DW_FORM_udata:
13322         case DW_FORM_sdata:
13323           fprintf_unfiltered (f, "constant: %s",
13324                               pulongest (DW_UNSND (&die->attrs[i])));
13325           break;
13326         case DW_FORM_sec_offset:
13327           fprintf_unfiltered (f, "section offset: %s",
13328                               pulongest (DW_UNSND (&die->attrs[i])));
13329           break;
13330         case DW_FORM_ref_sig8:
13331           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13332             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13333                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13334           else
13335             fprintf_unfiltered (f, "signatured type, offset: unknown");
13336           break;
13337         case DW_FORM_string:
13338         case DW_FORM_strp:
13339           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13340                    DW_STRING (&die->attrs[i])
13341                    ? DW_STRING (&die->attrs[i]) : "",
13342                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13343           break;
13344         case DW_FORM_flag:
13345           if (DW_UNSND (&die->attrs[i]))
13346             fprintf_unfiltered (f, "flag: TRUE");
13347           else
13348             fprintf_unfiltered (f, "flag: FALSE");
13349           break;
13350         case DW_FORM_flag_present:
13351           fprintf_unfiltered (f, "flag: TRUE");
13352           break;
13353         case DW_FORM_indirect:
13354           /* The reader will have reduced the indirect form to
13355              the "base form" so this form should not occur.  */
13356           fprintf_unfiltered (f, 
13357                               "unexpected attribute form: DW_FORM_indirect");
13358           break;
13359         default:
13360           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13361                    die->attrs[i].form);
13362           break;
13363         }
13364       fprintf_unfiltered (f, "\n");
13365     }
13366 }
13367
13368 static void
13369 dump_die_for_error (struct die_info *die)
13370 {
13371   dump_die_shallow (gdb_stderr, 0, die);
13372 }
13373
13374 static void
13375 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13376 {
13377   int indent = level * 4;
13378
13379   gdb_assert (die != NULL);
13380
13381   if (level >= max_level)
13382     return;
13383
13384   dump_die_shallow (f, indent, die);
13385
13386   if (die->child != NULL)
13387     {
13388       print_spaces (indent, f);
13389       fprintf_unfiltered (f, "  Children:");
13390       if (level + 1 < max_level)
13391         {
13392           fprintf_unfiltered (f, "\n");
13393           dump_die_1 (f, level + 1, max_level, die->child);
13394         }
13395       else
13396         {
13397           fprintf_unfiltered (f,
13398                               " [not printed, max nesting level reached]\n");
13399         }
13400     }
13401
13402   if (die->sibling != NULL && level > 0)
13403     {
13404       dump_die_1 (f, level, max_level, die->sibling);
13405     }
13406 }
13407
13408 /* This is called from the pdie macro in gdbinit.in.
13409    It's not static so gcc will keep a copy callable from gdb.  */
13410
13411 void
13412 dump_die (struct die_info *die, int max_level)
13413 {
13414   dump_die_1 (gdb_stdlog, 0, max_level, die);
13415 }
13416
13417 static void
13418 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13419 {
13420   void **slot;
13421
13422   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13423
13424   *slot = die;
13425 }
13426
13427 static int
13428 is_ref_attr (struct attribute *attr)
13429 {
13430   switch (attr->form)
13431     {
13432     case DW_FORM_ref_addr:
13433     case DW_FORM_ref1:
13434     case DW_FORM_ref2:
13435     case DW_FORM_ref4:
13436     case DW_FORM_ref8:
13437     case DW_FORM_ref_udata:
13438       return 1;
13439     default:
13440       return 0;
13441     }
13442 }
13443
13444 static unsigned int
13445 dwarf2_get_ref_die_offset (struct attribute *attr)
13446 {
13447   if (is_ref_attr (attr))
13448     return DW_ADDR (attr);
13449
13450   complaint (&symfile_complaints,
13451              _("unsupported die ref attribute form: '%s'"),
13452              dwarf_form_name (attr->form));
13453   return 0;
13454 }
13455
13456 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13457  * the value held by the attribute is not constant.  */
13458
13459 static LONGEST
13460 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13461 {
13462   if (attr->form == DW_FORM_sdata)
13463     return DW_SND (attr);
13464   else if (attr->form == DW_FORM_udata
13465            || attr->form == DW_FORM_data1
13466            || attr->form == DW_FORM_data2
13467            || attr->form == DW_FORM_data4
13468            || attr->form == DW_FORM_data8)
13469     return DW_UNSND (attr);
13470   else
13471     {
13472       complaint (&symfile_complaints,
13473                  _("Attribute value is not a constant (%s)"),
13474                  dwarf_form_name (attr->form));
13475       return default_value;
13476     }
13477 }
13478
13479 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13480    unit and add it to our queue.
13481    The result is non-zero if PER_CU was queued, otherwise the result is zero
13482    meaning either PER_CU is already queued or it is already loaded.  */
13483
13484 static int
13485 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13486                        struct dwarf2_per_cu_data *per_cu)
13487 {
13488   /* We may arrive here during partial symbol reading, if we need full
13489      DIEs to process an unusual case (e.g. template arguments).  Do
13490      not queue PER_CU, just tell our caller to load its DIEs.  */
13491   if (dwarf2_per_objfile->reading_partial_symbols)
13492     {
13493       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13494         return 1;
13495       return 0;
13496     }
13497
13498   /* Mark the dependence relation so that we don't flush PER_CU
13499      too early.  */
13500   dwarf2_add_dependence (this_cu, per_cu);
13501
13502   /* If it's already on the queue, we have nothing to do.  */
13503   if (per_cu->queued)
13504     return 0;
13505
13506   /* If the compilation unit is already loaded, just mark it as
13507      used.  */
13508   if (per_cu->cu != NULL)
13509     {
13510       per_cu->cu->last_used = 0;
13511       return 0;
13512     }
13513
13514   /* Add it to the queue.  */
13515   queue_comp_unit (per_cu, this_cu->objfile);
13516
13517   return 1;
13518 }
13519
13520 /* Follow reference or signature attribute ATTR of SRC_DIE.
13521    On entry *REF_CU is the CU of SRC_DIE.
13522    On exit *REF_CU is the CU of the result.  */
13523
13524 static struct die_info *
13525 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13526                        struct dwarf2_cu **ref_cu)
13527 {
13528   struct die_info *die;
13529
13530   if (is_ref_attr (attr))
13531     die = follow_die_ref (src_die, attr, ref_cu);
13532   else if (attr->form == DW_FORM_ref_sig8)
13533     die = follow_die_sig (src_die, attr, ref_cu);
13534   else
13535     {
13536       dump_die_for_error (src_die);
13537       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13538              (*ref_cu)->objfile->name);
13539     }
13540
13541   return die;
13542 }
13543
13544 /* Follow reference OFFSET.
13545    On entry *REF_CU is the CU of the source die referencing OFFSET.
13546    On exit *REF_CU is the CU of the result.
13547    Returns NULL if OFFSET is invalid.  */
13548
13549 static struct die_info *
13550 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13551 {
13552   struct die_info temp_die;
13553   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13554
13555   gdb_assert (cu->per_cu != NULL);
13556
13557   target_cu = cu;
13558
13559   if (cu->per_cu->from_debug_types)
13560     {
13561       /* .debug_types CUs cannot reference anything outside their CU.
13562          If they need to, they have to reference a signatured type via
13563          DW_FORM_ref_sig8.  */
13564       if (! offset_in_cu_p (&cu->header, offset))
13565         return NULL;
13566     }
13567   else if (! offset_in_cu_p (&cu->header, offset))
13568     {
13569       struct dwarf2_per_cu_data *per_cu;
13570
13571       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13572
13573       /* If necessary, add it to the queue and load its DIEs.  */
13574       if (maybe_queue_comp_unit (cu, per_cu))
13575         load_full_comp_unit (per_cu, cu->objfile);
13576
13577       target_cu = per_cu->cu;
13578     }
13579   else if (cu->dies == NULL)
13580     {
13581       /* We're loading full DIEs during partial symbol reading.  */
13582       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13583       load_full_comp_unit (cu->per_cu, cu->objfile);
13584     }
13585
13586   *ref_cu = target_cu;
13587   temp_die.offset = offset;
13588   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13589 }
13590
13591 /* Follow reference attribute ATTR of SRC_DIE.
13592    On entry *REF_CU is the CU of SRC_DIE.
13593    On exit *REF_CU is the CU of the result.  */
13594
13595 static struct die_info *
13596 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13597                 struct dwarf2_cu **ref_cu)
13598 {
13599   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13600   struct dwarf2_cu *cu = *ref_cu;
13601   struct die_info *die;
13602
13603   die = follow_die_offset (offset, ref_cu);
13604   if (!die)
13605     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13606            "at 0x%x [in module %s]"),
13607            offset, src_die->offset, cu->objfile->name);
13608
13609   return die;
13610 }
13611
13612 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13613    value is intended for DW_OP_call*.  */
13614
13615 struct dwarf2_locexpr_baton
13616 dwarf2_fetch_die_location_block (unsigned int offset,
13617                                  struct dwarf2_per_cu_data *per_cu,
13618                                  CORE_ADDR (*get_frame_pc) (void *baton),
13619                                  void *baton)
13620 {
13621   struct dwarf2_cu *cu = per_cu->cu;
13622   struct die_info *die;
13623   struct attribute *attr;
13624   struct dwarf2_locexpr_baton retval;
13625
13626   dw2_setup (per_cu->objfile);
13627
13628   die = follow_die_offset (offset, &cu);
13629   if (!die)
13630     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13631            offset, per_cu->cu->objfile->name);
13632
13633   attr = dwarf2_attr (die, DW_AT_location, cu);
13634   if (!attr)
13635     {
13636       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13637
13638       retval.data = NULL;
13639       retval.size = 0;
13640     }
13641   else if (attr_form_is_section_offset (attr))
13642     {
13643       struct dwarf2_loclist_baton loclist_baton;
13644       CORE_ADDR pc = (*get_frame_pc) (baton);
13645       size_t size;
13646
13647       fill_in_loclist_baton (cu, &loclist_baton, attr);
13648
13649       retval.data = dwarf2_find_location_expression (&loclist_baton,
13650                                                      &size, pc);
13651       retval.size = size;
13652     }
13653   else
13654     {
13655       if (!attr_form_is_block (attr))
13656         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13657                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13658                offset, per_cu->cu->objfile->name);
13659
13660       retval.data = DW_BLOCK (attr)->data;
13661       retval.size = DW_BLOCK (attr)->size;
13662     }
13663   retval.per_cu = cu->per_cu;
13664   return retval;
13665 }
13666
13667 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13668    PER_CU.  */
13669
13670 struct type *
13671 dwarf2_get_die_type (unsigned int die_offset,
13672                      struct dwarf2_per_cu_data *per_cu)
13673 {
13674   dw2_setup (per_cu->objfile);
13675   return get_die_type_at_offset (die_offset, per_cu);
13676 }
13677
13678 /* Follow the signature attribute ATTR in SRC_DIE.
13679    On entry *REF_CU is the CU of SRC_DIE.
13680    On exit *REF_CU is the CU of the result.  */
13681
13682 static struct die_info *
13683 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13684                 struct dwarf2_cu **ref_cu)
13685 {
13686   struct objfile *objfile = (*ref_cu)->objfile;
13687   struct die_info temp_die;
13688   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13689   struct dwarf2_cu *sig_cu;
13690   struct die_info *die;
13691
13692   /* sig_type will be NULL if the signatured type is missing from
13693      the debug info.  */
13694   if (sig_type == NULL)
13695     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13696              "at 0x%x [in module %s]"),
13697            src_die->offset, objfile->name);
13698
13699   /* If necessary, add it to the queue and load its DIEs.  */
13700
13701   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13702     read_signatured_type (objfile, sig_type);
13703
13704   gdb_assert (sig_type->per_cu.cu != NULL);
13705
13706   sig_cu = sig_type->per_cu.cu;
13707   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13708   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13709   if (die)
13710     {
13711       *ref_cu = sig_cu;
13712       return die;
13713     }
13714
13715   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13716          "from DIE at 0x%x [in module %s]"),
13717          sig_type->type_offset, src_die->offset, objfile->name);
13718 }
13719
13720 /* Given an offset of a signatured type, return its signatured_type.  */
13721
13722 static struct signatured_type *
13723 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13724 {
13725   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13726   unsigned int length, initial_length_size;
13727   unsigned int sig_offset;
13728   struct signatured_type find_entry, *type_sig;
13729
13730   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13731   sig_offset = (initial_length_size
13732                 + 2 /*version*/
13733                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13734                 + 1 /*address_size*/);
13735   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13736   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13737
13738   /* This is only used to lookup previously recorded types.
13739      If we didn't find it, it's our bug.  */
13740   gdb_assert (type_sig != NULL);
13741   gdb_assert (offset == type_sig->offset);
13742
13743   return type_sig;
13744 }
13745
13746 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13747
13748 static void
13749 read_signatured_type_at_offset (struct objfile *objfile,
13750                                 unsigned int offset)
13751 {
13752   struct signatured_type *type_sig;
13753
13754   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13755
13756   /* We have the section offset, but we need the signature to do the
13757      hash table lookup.  */
13758   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13759
13760   gdb_assert (type_sig->per_cu.cu == NULL);
13761
13762   read_signatured_type (objfile, type_sig);
13763
13764   gdb_assert (type_sig->per_cu.cu != NULL);
13765 }
13766
13767 /* Read in a signatured type and build its CU and DIEs.  */
13768
13769 static void
13770 read_signatured_type (struct objfile *objfile,
13771                       struct signatured_type *type_sig)
13772 {
13773   gdb_byte *types_ptr;
13774   struct die_reader_specs reader_specs;
13775   struct dwarf2_cu *cu;
13776   ULONGEST signature;
13777   struct cleanup *back_to, *free_cu_cleanup;
13778
13779   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13780   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13781
13782   gdb_assert (type_sig->per_cu.cu == NULL);
13783
13784   cu = xmalloc (sizeof (*cu));
13785   init_one_comp_unit (cu, objfile);
13786
13787   type_sig->per_cu.cu = cu;
13788   cu->per_cu = &type_sig->per_cu;
13789
13790   /* If an error occurs while loading, release our storage.  */
13791   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13792
13793   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13794                                         types_ptr, objfile->obfd);
13795   gdb_assert (signature == type_sig->signature);
13796
13797   cu->die_hash
13798     = htab_create_alloc_ex (cu->header.length / 12,
13799                             die_hash,
13800                             die_eq,
13801                             NULL,
13802                             &cu->comp_unit_obstack,
13803                             hashtab_obstack_allocate,
13804                             dummy_obstack_deallocate);
13805
13806   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13807   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13808
13809   init_cu_die_reader (&reader_specs, cu);
13810
13811   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13812                                     NULL /*parent*/);
13813
13814   /* We try not to read any attributes in this function, because not
13815      all objfiles needed for references have been loaded yet, and symbol
13816      table processing isn't initialized.  But we have to set the CU language,
13817      or we won't be able to build types correctly.  */
13818   prepare_one_comp_unit (cu, cu->dies);
13819
13820   do_cleanups (back_to);
13821
13822   /* We've successfully allocated this compilation unit.  Let our caller
13823      clean it up when finished with it.  */
13824   discard_cleanups (free_cu_cleanup);
13825
13826   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13827   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13828 }
13829
13830 /* Decode simple location descriptions.
13831    Given a pointer to a dwarf block that defines a location, compute
13832    the location and return the value.
13833
13834    NOTE drow/2003-11-18: This function is called in two situations
13835    now: for the address of static or global variables (partial symbols
13836    only) and for offsets into structures which are expected to be
13837    (more or less) constant.  The partial symbol case should go away,
13838    and only the constant case should remain.  That will let this
13839    function complain more accurately.  A few special modes are allowed
13840    without complaint for global variables (for instance, global
13841    register values and thread-local values).
13842
13843    A location description containing no operations indicates that the
13844    object is optimized out.  The return value is 0 for that case.
13845    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13846    callers will only want a very basic result and this can become a
13847    complaint.
13848
13849    Note that stack[0] is unused except as a default error return.  */
13850
13851 static CORE_ADDR
13852 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13853 {
13854   struct objfile *objfile = cu->objfile;
13855   int i;
13856   int size = blk->size;
13857   gdb_byte *data = blk->data;
13858   CORE_ADDR stack[64];
13859   int stacki;
13860   unsigned int bytes_read, unsnd;
13861   gdb_byte op;
13862
13863   i = 0;
13864   stacki = 0;
13865   stack[stacki] = 0;
13866   stack[++stacki] = 0;
13867
13868   while (i < size)
13869     {
13870       op = data[i++];
13871       switch (op)
13872         {
13873         case DW_OP_lit0:
13874         case DW_OP_lit1:
13875         case DW_OP_lit2:
13876         case DW_OP_lit3:
13877         case DW_OP_lit4:
13878         case DW_OP_lit5:
13879         case DW_OP_lit6:
13880         case DW_OP_lit7:
13881         case DW_OP_lit8:
13882         case DW_OP_lit9:
13883         case DW_OP_lit10:
13884         case DW_OP_lit11:
13885         case DW_OP_lit12:
13886         case DW_OP_lit13:
13887         case DW_OP_lit14:
13888         case DW_OP_lit15:
13889         case DW_OP_lit16:
13890         case DW_OP_lit17:
13891         case DW_OP_lit18:
13892         case DW_OP_lit19:
13893         case DW_OP_lit20:
13894         case DW_OP_lit21:
13895         case DW_OP_lit22:
13896         case DW_OP_lit23:
13897         case DW_OP_lit24:
13898         case DW_OP_lit25:
13899         case DW_OP_lit26:
13900         case DW_OP_lit27:
13901         case DW_OP_lit28:
13902         case DW_OP_lit29:
13903         case DW_OP_lit30:
13904         case DW_OP_lit31:
13905           stack[++stacki] = op - DW_OP_lit0;
13906           break;
13907
13908         case DW_OP_reg0:
13909         case DW_OP_reg1:
13910         case DW_OP_reg2:
13911         case DW_OP_reg3:
13912         case DW_OP_reg4:
13913         case DW_OP_reg5:
13914         case DW_OP_reg6:
13915         case DW_OP_reg7:
13916         case DW_OP_reg8:
13917         case DW_OP_reg9:
13918         case DW_OP_reg10:
13919         case DW_OP_reg11:
13920         case DW_OP_reg12:
13921         case DW_OP_reg13:
13922         case DW_OP_reg14:
13923         case DW_OP_reg15:
13924         case DW_OP_reg16:
13925         case DW_OP_reg17:
13926         case DW_OP_reg18:
13927         case DW_OP_reg19:
13928         case DW_OP_reg20:
13929         case DW_OP_reg21:
13930         case DW_OP_reg22:
13931         case DW_OP_reg23:
13932         case DW_OP_reg24:
13933         case DW_OP_reg25:
13934         case DW_OP_reg26:
13935         case DW_OP_reg27:
13936         case DW_OP_reg28:
13937         case DW_OP_reg29:
13938         case DW_OP_reg30:
13939         case DW_OP_reg31:
13940           stack[++stacki] = op - DW_OP_reg0;
13941           if (i < size)
13942             dwarf2_complex_location_expr_complaint ();
13943           break;
13944
13945         case DW_OP_regx:
13946           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13947           i += bytes_read;
13948           stack[++stacki] = unsnd;
13949           if (i < size)
13950             dwarf2_complex_location_expr_complaint ();
13951           break;
13952
13953         case DW_OP_addr:
13954           stack[++stacki] = read_address (objfile->obfd, &data[i],
13955                                           cu, &bytes_read);
13956           i += bytes_read;
13957           break;
13958
13959         case DW_OP_const1u:
13960           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13961           i += 1;
13962           break;
13963
13964         case DW_OP_const1s:
13965           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13966           i += 1;
13967           break;
13968
13969         case DW_OP_const2u:
13970           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13971           i += 2;
13972           break;
13973
13974         case DW_OP_const2s:
13975           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13976           i += 2;
13977           break;
13978
13979         case DW_OP_const4u:
13980           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13981           i += 4;
13982           break;
13983
13984         case DW_OP_const4s:
13985           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13986           i += 4;
13987           break;
13988
13989         case DW_OP_constu:
13990           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13991                                                   &bytes_read);
13992           i += bytes_read;
13993           break;
13994
13995         case DW_OP_consts:
13996           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13997           i += bytes_read;
13998           break;
13999
14000         case DW_OP_dup:
14001           stack[stacki + 1] = stack[stacki];
14002           stacki++;
14003           break;
14004
14005         case DW_OP_plus:
14006           stack[stacki - 1] += stack[stacki];
14007           stacki--;
14008           break;
14009
14010         case DW_OP_plus_uconst:
14011           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14012                                                  &bytes_read);
14013           i += bytes_read;
14014           break;
14015
14016         case DW_OP_minus:
14017           stack[stacki - 1] -= stack[stacki];
14018           stacki--;
14019           break;
14020
14021         case DW_OP_deref:
14022           /* If we're not the last op, then we definitely can't encode
14023              this using GDB's address_class enum.  This is valid for partial
14024              global symbols, although the variable's address will be bogus
14025              in the psymtab.  */
14026           if (i < size)
14027             dwarf2_complex_location_expr_complaint ();
14028           break;
14029
14030         case DW_OP_GNU_push_tls_address:
14031           /* The top of the stack has the offset from the beginning
14032              of the thread control block at which the variable is located.  */
14033           /* Nothing should follow this operator, so the top of stack would
14034              be returned.  */
14035           /* This is valid for partial global symbols, but the variable's
14036              address will be bogus in the psymtab.  */
14037           if (i < size)
14038             dwarf2_complex_location_expr_complaint ();
14039           break;
14040
14041         case DW_OP_GNU_uninit:
14042           break;
14043
14044         default:
14045           {
14046             const char *name = dwarf_stack_op_name (op);
14047
14048             if (name)
14049               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14050                          name);
14051             else
14052               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14053                          op);
14054           }
14055
14056           return (stack[stacki]);
14057         }
14058
14059       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14060          outside of the allocated space.  Also enforce minimum>0.  */
14061       if (stacki >= ARRAY_SIZE (stack) - 1)
14062         {
14063           complaint (&symfile_complaints,
14064                      _("location description stack overflow"));
14065           return 0;
14066         }
14067
14068       if (stacki <= 0)
14069         {
14070           complaint (&symfile_complaints,
14071                      _("location description stack underflow"));
14072           return 0;
14073         }
14074     }
14075   return (stack[stacki]);
14076 }
14077
14078 /* memory allocation interface */
14079
14080 static struct dwarf_block *
14081 dwarf_alloc_block (struct dwarf2_cu *cu)
14082 {
14083   struct dwarf_block *blk;
14084
14085   blk = (struct dwarf_block *)
14086     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14087   return (blk);
14088 }
14089
14090 static struct abbrev_info *
14091 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14092 {
14093   struct abbrev_info *abbrev;
14094
14095   abbrev = (struct abbrev_info *)
14096     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14097   memset (abbrev, 0, sizeof (struct abbrev_info));
14098   return (abbrev);
14099 }
14100
14101 static struct die_info *
14102 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14103 {
14104   struct die_info *die;
14105   size_t size = sizeof (struct die_info);
14106
14107   if (num_attrs > 1)
14108     size += (num_attrs - 1) * sizeof (struct attribute);
14109
14110   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14111   memset (die, 0, sizeof (struct die_info));
14112   return (die);
14113 }
14114
14115 \f
14116 /* Macro support.  */
14117
14118 /* Return the full name of file number I in *LH's file name table.
14119    Use COMP_DIR as the name of the current directory of the
14120    compilation.  The result is allocated using xmalloc; the caller is
14121    responsible for freeing it.  */
14122 static char *
14123 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14124 {
14125   /* Is the file number a valid index into the line header's file name
14126      table?  Remember that file numbers start with one, not zero.  */
14127   if (1 <= file && file <= lh->num_file_names)
14128     {
14129       struct file_entry *fe = &lh->file_names[file - 1];
14130
14131       if (IS_ABSOLUTE_PATH (fe->name))
14132         return xstrdup (fe->name);
14133       else
14134         {
14135           const char *dir;
14136           int dir_len;
14137           char *full_name;
14138
14139           if (fe->dir_index)
14140             dir = lh->include_dirs[fe->dir_index - 1];
14141           else
14142             dir = comp_dir;
14143
14144           if (dir)
14145             {
14146               dir_len = strlen (dir);
14147               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14148               strcpy (full_name, dir);
14149               full_name[dir_len] = '/';
14150               strcpy (full_name + dir_len + 1, fe->name);
14151               return full_name;
14152             }
14153           else
14154             return xstrdup (fe->name);
14155         }
14156     }
14157   else
14158     {
14159       /* The compiler produced a bogus file number.  We can at least
14160          record the macro definitions made in the file, even if we
14161          won't be able to find the file by name.  */
14162       char fake_name[80];
14163
14164       sprintf (fake_name, "<bad macro file number %d>", file);
14165
14166       complaint (&symfile_complaints,
14167                  _("bad file number in macro information (%d)"),
14168                  file);
14169
14170       return xstrdup (fake_name);
14171     }
14172 }
14173
14174
14175 static struct macro_source_file *
14176 macro_start_file (int file, int line,
14177                   struct macro_source_file *current_file,
14178                   const char *comp_dir,
14179                   struct line_header *lh, struct objfile *objfile)
14180 {
14181   /* The full name of this source file.  */
14182   char *full_name = file_full_name (file, lh, comp_dir);
14183
14184   /* We don't create a macro table for this compilation unit
14185      at all until we actually get a filename.  */
14186   if (! pending_macros)
14187     pending_macros = new_macro_table (&objfile->objfile_obstack,
14188                                       objfile->macro_cache);
14189
14190   if (! current_file)
14191     /* If we have no current file, then this must be the start_file
14192        directive for the compilation unit's main source file.  */
14193     current_file = macro_set_main (pending_macros, full_name);
14194   else
14195     current_file = macro_include (current_file, line, full_name);
14196
14197   xfree (full_name);
14198
14199   return current_file;
14200 }
14201
14202
14203 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14204    followed by a null byte.  */
14205 static char *
14206 copy_string (const char *buf, int len)
14207 {
14208   char *s = xmalloc (len + 1);
14209
14210   memcpy (s, buf, len);
14211   s[len] = '\0';
14212   return s;
14213 }
14214
14215
14216 static const char *
14217 consume_improper_spaces (const char *p, const char *body)
14218 {
14219   if (*p == ' ')
14220     {
14221       complaint (&symfile_complaints,
14222                  _("macro definition contains spaces "
14223                    "in formal argument list:\n`%s'"),
14224                  body);
14225
14226       while (*p == ' ')
14227         p++;
14228     }
14229
14230   return p;
14231 }
14232
14233
14234 static void
14235 parse_macro_definition (struct macro_source_file *file, int line,
14236                         const char *body)
14237 {
14238   const char *p;
14239
14240   /* The body string takes one of two forms.  For object-like macro
14241      definitions, it should be:
14242
14243         <macro name> " " <definition>
14244
14245      For function-like macro definitions, it should be:
14246
14247         <macro name> "() " <definition>
14248      or
14249         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14250
14251      Spaces may appear only where explicitly indicated, and in the
14252      <definition>.
14253
14254      The Dwarf 2 spec says that an object-like macro's name is always
14255      followed by a space, but versions of GCC around March 2002 omit
14256      the space when the macro's definition is the empty string.
14257
14258      The Dwarf 2 spec says that there should be no spaces between the
14259      formal arguments in a function-like macro's formal argument list,
14260      but versions of GCC around March 2002 include spaces after the
14261      commas.  */
14262
14263
14264   /* Find the extent of the macro name.  The macro name is terminated
14265      by either a space or null character (for an object-like macro) or
14266      an opening paren (for a function-like macro).  */
14267   for (p = body; *p; p++)
14268     if (*p == ' ' || *p == '(')
14269       break;
14270
14271   if (*p == ' ' || *p == '\0')
14272     {
14273       /* It's an object-like macro.  */
14274       int name_len = p - body;
14275       char *name = copy_string (body, name_len);
14276       const char *replacement;
14277
14278       if (*p == ' ')
14279         replacement = body + name_len + 1;
14280       else
14281         {
14282           dwarf2_macro_malformed_definition_complaint (body);
14283           replacement = body + name_len;
14284         }
14285
14286       macro_define_object (file, line, name, replacement);
14287
14288       xfree (name);
14289     }
14290   else if (*p == '(')
14291     {
14292       /* It's a function-like macro.  */
14293       char *name = copy_string (body, p - body);
14294       int argc = 0;
14295       int argv_size = 1;
14296       char **argv = xmalloc (argv_size * sizeof (*argv));
14297
14298       p++;
14299
14300       p = consume_improper_spaces (p, body);
14301
14302       /* Parse the formal argument list.  */
14303       while (*p && *p != ')')
14304         {
14305           /* Find the extent of the current argument name.  */
14306           const char *arg_start = p;
14307
14308           while (*p && *p != ',' && *p != ')' && *p != ' ')
14309             p++;
14310
14311           if (! *p || p == arg_start)
14312             dwarf2_macro_malformed_definition_complaint (body);
14313           else
14314             {
14315               /* Make sure argv has room for the new argument.  */
14316               if (argc >= argv_size)
14317                 {
14318                   argv_size *= 2;
14319                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14320                 }
14321
14322               argv[argc++] = copy_string (arg_start, p - arg_start);
14323             }
14324
14325           p = consume_improper_spaces (p, body);
14326
14327           /* Consume the comma, if present.  */
14328           if (*p == ',')
14329             {
14330               p++;
14331
14332               p = consume_improper_spaces (p, body);
14333             }
14334         }
14335
14336       if (*p == ')')
14337         {
14338           p++;
14339
14340           if (*p == ' ')
14341             /* Perfectly formed definition, no complaints.  */
14342             macro_define_function (file, line, name,
14343                                    argc, (const char **) argv,
14344                                    p + 1);
14345           else if (*p == '\0')
14346             {
14347               /* Complain, but do define it.  */
14348               dwarf2_macro_malformed_definition_complaint (body);
14349               macro_define_function (file, line, name,
14350                                      argc, (const char **) argv,
14351                                      p);
14352             }
14353           else
14354             /* Just complain.  */
14355             dwarf2_macro_malformed_definition_complaint (body);
14356         }
14357       else
14358         /* Just complain.  */
14359         dwarf2_macro_malformed_definition_complaint (body);
14360
14361       xfree (name);
14362       {
14363         int i;
14364
14365         for (i = 0; i < argc; i++)
14366           xfree (argv[i]);
14367       }
14368       xfree (argv);
14369     }
14370   else
14371     dwarf2_macro_malformed_definition_complaint (body);
14372 }
14373
14374
14375 static void
14376 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14377                      char *comp_dir, bfd *abfd,
14378                      struct dwarf2_cu *cu)
14379 {
14380   gdb_byte *mac_ptr, *mac_end;
14381   struct macro_source_file *current_file = 0;
14382   enum dwarf_macinfo_record_type macinfo_type;
14383   int at_commandline;
14384
14385   dwarf2_read_section (dwarf2_per_objfile->objfile,
14386                        &dwarf2_per_objfile->macinfo);
14387   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14388     {
14389       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14390       return;
14391     }
14392
14393   /* First pass: Find the name of the base filename.
14394      This filename is needed in order to process all macros whose definition
14395      (or undefinition) comes from the command line.  These macros are defined
14396      before the first DW_MACINFO_start_file entry, and yet still need to be
14397      associated to the base file.
14398
14399      To determine the base file name, we scan the macro definitions until we
14400      reach the first DW_MACINFO_start_file entry.  We then initialize
14401      CURRENT_FILE accordingly so that any macro definition found before the
14402      first DW_MACINFO_start_file can still be associated to the base file.  */
14403
14404   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14405   mac_end = dwarf2_per_objfile->macinfo.buffer
14406     + dwarf2_per_objfile->macinfo.size;
14407
14408   do
14409     {
14410       /* Do we at least have room for a macinfo type byte?  */
14411       if (mac_ptr >= mac_end)
14412         {
14413           /* Complaint is printed during the second pass as GDB will probably
14414              stop the first pass earlier upon finding
14415              DW_MACINFO_start_file.  */
14416           break;
14417         }
14418
14419       macinfo_type = read_1_byte (abfd, mac_ptr);
14420       mac_ptr++;
14421
14422       switch (macinfo_type)
14423         {
14424           /* A zero macinfo type indicates the end of the macro
14425              information.  */
14426         case 0:
14427           break;
14428
14429         case DW_MACINFO_define:
14430         case DW_MACINFO_undef:
14431           /* Only skip the data by MAC_PTR.  */
14432           {
14433             unsigned int bytes_read;
14434
14435             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14436             mac_ptr += bytes_read;
14437             read_direct_string (abfd, mac_ptr, &bytes_read);
14438             mac_ptr += bytes_read;
14439           }
14440           break;
14441
14442         case DW_MACINFO_start_file:
14443           {
14444             unsigned int bytes_read;
14445             int line, file;
14446
14447             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14448             mac_ptr += bytes_read;
14449             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14450             mac_ptr += bytes_read;
14451
14452             current_file = macro_start_file (file, line, current_file,
14453                                              comp_dir, lh, cu->objfile);
14454           }
14455           break;
14456
14457         case DW_MACINFO_end_file:
14458           /* No data to skip by MAC_PTR.  */
14459           break;
14460
14461         case DW_MACINFO_vendor_ext:
14462           /* Only skip the data by MAC_PTR.  */
14463           {
14464             unsigned int bytes_read;
14465
14466             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14467             mac_ptr += bytes_read;
14468             read_direct_string (abfd, mac_ptr, &bytes_read);
14469             mac_ptr += bytes_read;
14470           }
14471           break;
14472
14473         default:
14474           break;
14475         }
14476     } while (macinfo_type != 0 && current_file == NULL);
14477
14478   /* Second pass: Process all entries.
14479
14480      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14481      command-line macro definitions/undefinitions.  This flag is unset when we
14482      reach the first DW_MACINFO_start_file entry.  */
14483
14484   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14485
14486   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14487      GDB is still reading the definitions from command line.  First
14488      DW_MACINFO_start_file will need to be ignored as it was already executed
14489      to create CURRENT_FILE for the main source holding also the command line
14490      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14491      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14492
14493   at_commandline = 1;
14494
14495   do
14496     {
14497       /* Do we at least have room for a macinfo type byte?  */
14498       if (mac_ptr >= mac_end)
14499         {
14500           dwarf2_macros_too_long_complaint ();
14501           break;
14502         }
14503
14504       macinfo_type = read_1_byte (abfd, mac_ptr);
14505       mac_ptr++;
14506
14507       switch (macinfo_type)
14508         {
14509           /* A zero macinfo type indicates the end of the macro
14510              information.  */
14511         case 0:
14512           break;
14513
14514         case DW_MACINFO_define:
14515         case DW_MACINFO_undef:
14516           {
14517             unsigned int bytes_read;
14518             int line;
14519             char *body;
14520
14521             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14522             mac_ptr += bytes_read;
14523             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14524             mac_ptr += bytes_read;
14525
14526             if (! current_file)
14527               {
14528                 /* DWARF violation as no main source is present.  */
14529                 complaint (&symfile_complaints,
14530                            _("debug info with no main source gives macro %s "
14531                              "on line %d: %s"),
14532                            macinfo_type == DW_MACINFO_define ?
14533                              _("definition") :
14534                                macinfo_type == DW_MACINFO_undef ?
14535                                  _("undefinition") :
14536                                  _("something-or-other"), line, body);
14537                 break;
14538               }
14539             if ((line == 0 && !at_commandline)
14540                 || (line != 0 && at_commandline))
14541               complaint (&symfile_complaints,
14542                          _("debug info gives %s macro %s with %s line %d: %s"),
14543                          at_commandline ? _("command-line") : _("in-file"),
14544                          macinfo_type == DW_MACINFO_define ?
14545                            _("definition") :
14546                              macinfo_type == DW_MACINFO_undef ?
14547                                _("undefinition") :
14548                                _("something-or-other"),
14549                          line == 0 ? _("zero") : _("non-zero"), line, body);
14550
14551             if (macinfo_type == DW_MACINFO_define)
14552               parse_macro_definition (current_file, line, body);
14553             else if (macinfo_type == DW_MACINFO_undef)
14554               macro_undef (current_file, line, body);
14555           }
14556           break;
14557
14558         case DW_MACINFO_start_file:
14559           {
14560             unsigned int bytes_read;
14561             int line, file;
14562
14563             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14564             mac_ptr += bytes_read;
14565             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14566             mac_ptr += bytes_read;
14567
14568             if ((line == 0 && !at_commandline)
14569                 || (line != 0 && at_commandline))
14570               complaint (&symfile_complaints,
14571                          _("debug info gives source %d included "
14572                            "from %s at %s line %d"),
14573                          file, at_commandline ? _("command-line") : _("file"),
14574                          line == 0 ? _("zero") : _("non-zero"), line);
14575
14576             if (at_commandline)
14577               {
14578                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14579                 at_commandline = 0;
14580               }
14581             else
14582               current_file = macro_start_file (file, line,
14583                                                current_file, comp_dir,
14584                                                lh, cu->objfile);
14585           }
14586           break;
14587
14588         case DW_MACINFO_end_file:
14589           if (! current_file)
14590             complaint (&symfile_complaints,
14591                        _("macro debug info has an unmatched "
14592                          "`close_file' directive"));
14593           else
14594             {
14595               current_file = current_file->included_by;
14596               if (! current_file)
14597                 {
14598                   enum dwarf_macinfo_record_type next_type;
14599
14600                   /* GCC circa March 2002 doesn't produce the zero
14601                      type byte marking the end of the compilation
14602                      unit.  Complain if it's not there, but exit no
14603                      matter what.  */
14604
14605                   /* Do we at least have room for a macinfo type byte?  */
14606                   if (mac_ptr >= mac_end)
14607                     {
14608                       dwarf2_macros_too_long_complaint ();
14609                       return;
14610                     }
14611
14612                   /* We don't increment mac_ptr here, so this is just
14613                      a look-ahead.  */
14614                   next_type = read_1_byte (abfd, mac_ptr);
14615                   if (next_type != 0)
14616                     complaint (&symfile_complaints,
14617                                _("no terminating 0-type entry for "
14618                                  "macros in `.debug_macinfo' section"));
14619
14620                   return;
14621                 }
14622             }
14623           break;
14624
14625         case DW_MACINFO_vendor_ext:
14626           {
14627             unsigned int bytes_read;
14628             int constant;
14629
14630             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14631             mac_ptr += bytes_read;
14632             read_direct_string (abfd, mac_ptr, &bytes_read);
14633             mac_ptr += bytes_read;
14634
14635             /* We don't recognize any vendor extensions.  */
14636           }
14637           break;
14638         }
14639     } while (macinfo_type != 0);
14640 }
14641
14642 /* Check if the attribute's form is a DW_FORM_block*
14643    if so return true else false.  */
14644 static int
14645 attr_form_is_block (struct attribute *attr)
14646 {
14647   return (attr == NULL ? 0 :
14648       attr->form == DW_FORM_block1
14649       || attr->form == DW_FORM_block2
14650       || attr->form == DW_FORM_block4
14651       || attr->form == DW_FORM_block
14652       || attr->form == DW_FORM_exprloc);
14653 }
14654
14655 /* Return non-zero if ATTR's value is a section offset --- classes
14656    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14657    You may use DW_UNSND (attr) to retrieve such offsets.
14658
14659    Section 7.5.4, "Attribute Encodings", explains that no attribute
14660    may have a value that belongs to more than one of these classes; it
14661    would be ambiguous if we did, because we use the same forms for all
14662    of them.  */
14663 static int
14664 attr_form_is_section_offset (struct attribute *attr)
14665 {
14666   return (attr->form == DW_FORM_data4
14667           || attr->form == DW_FORM_data8
14668           || attr->form == DW_FORM_sec_offset);
14669 }
14670
14671
14672 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14673    zero otherwise.  When this function returns true, you can apply
14674    dwarf2_get_attr_constant_value to it.
14675
14676    However, note that for some attributes you must check
14677    attr_form_is_section_offset before using this test.  DW_FORM_data4
14678    and DW_FORM_data8 are members of both the constant class, and of
14679    the classes that contain offsets into other debug sections
14680    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14681    that, if an attribute's can be either a constant or one of the
14682    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14683    taken as section offsets, not constants.  */
14684 static int
14685 attr_form_is_constant (struct attribute *attr)
14686 {
14687   switch (attr->form)
14688     {
14689     case DW_FORM_sdata:
14690     case DW_FORM_udata:
14691     case DW_FORM_data1:
14692     case DW_FORM_data2:
14693     case DW_FORM_data4:
14694     case DW_FORM_data8:
14695       return 1;
14696     default:
14697       return 0;
14698     }
14699 }
14700
14701 /* A helper function that fills in a dwarf2_loclist_baton.  */
14702
14703 static void
14704 fill_in_loclist_baton (struct dwarf2_cu *cu,
14705                        struct dwarf2_loclist_baton *baton,
14706                        struct attribute *attr)
14707 {
14708   dwarf2_read_section (dwarf2_per_objfile->objfile,
14709                        &dwarf2_per_objfile->loc);
14710
14711   baton->per_cu = cu->per_cu;
14712   gdb_assert (baton->per_cu);
14713   /* We don't know how long the location list is, but make sure we
14714      don't run off the edge of the section.  */
14715   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14716   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14717   baton->base_address = cu->base_address;
14718 }
14719
14720 static void
14721 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14722                              struct dwarf2_cu *cu)
14723 {
14724   if (attr_form_is_section_offset (attr)
14725       /* ".debug_loc" may not exist at all, or the offset may be outside
14726          the section.  If so, fall through to the complaint in the
14727          other branch.  */
14728       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14729                                                 &dwarf2_per_objfile->loc))
14730     {
14731       struct dwarf2_loclist_baton *baton;
14732
14733       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14734                              sizeof (struct dwarf2_loclist_baton));
14735
14736       fill_in_loclist_baton (cu, baton, attr);
14737
14738       if (cu->base_known == 0)
14739         complaint (&symfile_complaints,
14740                    _("Location list used without "
14741                      "specifying the CU base address."));
14742
14743       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14744       SYMBOL_LOCATION_BATON (sym) = baton;
14745     }
14746   else
14747     {
14748       struct dwarf2_locexpr_baton *baton;
14749
14750       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14751                              sizeof (struct dwarf2_locexpr_baton));
14752       baton->per_cu = cu->per_cu;
14753       gdb_assert (baton->per_cu);
14754
14755       if (attr_form_is_block (attr))
14756         {
14757           /* Note that we're just copying the block's data pointer
14758              here, not the actual data.  We're still pointing into the
14759              info_buffer for SYM's objfile; right now we never release
14760              that buffer, but when we do clean up properly this may
14761              need to change.  */
14762           baton->size = DW_BLOCK (attr)->size;
14763           baton->data = DW_BLOCK (attr)->data;
14764         }
14765       else
14766         {
14767           dwarf2_invalid_attrib_class_complaint ("location description",
14768                                                  SYMBOL_NATURAL_NAME (sym));
14769           baton->size = 0;
14770           baton->data = NULL;
14771         }
14772
14773       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14774       SYMBOL_LOCATION_BATON (sym) = baton;
14775     }
14776 }
14777
14778 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14779    came from a separate debuginfo file, then the master objfile is
14780    returned.  */
14781
14782 struct objfile *
14783 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14784 {
14785   struct objfile *objfile = per_cu->objfile;
14786
14787   /* Return the master objfile, so that we can report and look up the
14788      correct file containing this variable.  */
14789   if (objfile->separate_debug_objfile_backlink)
14790     objfile = objfile->separate_debug_objfile_backlink;
14791
14792   return objfile;
14793 }
14794
14795 /* Return the address size given in the compilation unit header for CU.  */
14796
14797 CORE_ADDR
14798 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14799 {
14800   if (per_cu->cu)
14801     return per_cu->cu->header.addr_size;
14802   else
14803     {
14804       /* If the CU is not currently read in, we re-read its header.  */
14805       struct objfile *objfile = per_cu->objfile;
14806       struct dwarf2_per_objfile *per_objfile
14807         = objfile_data (objfile, dwarf2_objfile_data_key);
14808       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14809       struct comp_unit_head cu_header;
14810
14811       memset (&cu_header, 0, sizeof cu_header);
14812       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14813       return cu_header.addr_size;
14814     }
14815 }
14816
14817 /* Return the offset size given in the compilation unit header for CU.  */
14818
14819 int
14820 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14821 {
14822   if (per_cu->cu)
14823     return per_cu->cu->header.offset_size;
14824   else
14825     {
14826       /* If the CU is not currently read in, we re-read its header.  */
14827       struct objfile *objfile = per_cu->objfile;
14828       struct dwarf2_per_objfile *per_objfile
14829         = objfile_data (objfile, dwarf2_objfile_data_key);
14830       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14831       struct comp_unit_head cu_header;
14832
14833       memset (&cu_header, 0, sizeof cu_header);
14834       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14835       return cu_header.offset_size;
14836     }
14837 }
14838
14839 /* Return the text offset of the CU.  The returned offset comes from
14840    this CU's objfile.  If this objfile came from a separate debuginfo
14841    file, then the offset may be different from the corresponding
14842    offset in the parent objfile.  */
14843
14844 CORE_ADDR
14845 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14846 {
14847   struct objfile *objfile = per_cu->objfile;
14848
14849   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14850 }
14851
14852 /* Locate the .debug_info compilation unit from CU's objfile which contains
14853    the DIE at OFFSET.  Raises an error on failure.  */
14854
14855 static struct dwarf2_per_cu_data *
14856 dwarf2_find_containing_comp_unit (unsigned int offset,
14857                                   struct objfile *objfile)
14858 {
14859   struct dwarf2_per_cu_data *this_cu;
14860   int low, high;
14861
14862   low = 0;
14863   high = dwarf2_per_objfile->n_comp_units - 1;
14864   while (high > low)
14865     {
14866       int mid = low + (high - low) / 2;
14867
14868       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14869         high = mid;
14870       else
14871         low = mid + 1;
14872     }
14873   gdb_assert (low == high);
14874   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14875     {
14876       if (low == 0)
14877         error (_("Dwarf Error: could not find partial DIE containing "
14878                "offset 0x%lx [in module %s]"),
14879                (long) offset, bfd_get_filename (objfile->obfd));
14880
14881       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14882       return dwarf2_per_objfile->all_comp_units[low-1];
14883     }
14884   else
14885     {
14886       this_cu = dwarf2_per_objfile->all_comp_units[low];
14887       if (low == dwarf2_per_objfile->n_comp_units - 1
14888           && offset >= this_cu->offset + this_cu->length)
14889         error (_("invalid dwarf2 offset %u"), offset);
14890       gdb_assert (offset < this_cu->offset + this_cu->length);
14891       return this_cu;
14892     }
14893 }
14894
14895 /* Locate the compilation unit from OBJFILE which is located at exactly
14896    OFFSET.  Raises an error on failure.  */
14897
14898 static struct dwarf2_per_cu_data *
14899 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14900 {
14901   struct dwarf2_per_cu_data *this_cu;
14902
14903   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14904   if (this_cu->offset != offset)
14905     error (_("no compilation unit with offset %u."), offset);
14906   return this_cu;
14907 }
14908
14909 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14910
14911 static void
14912 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14913 {
14914   memset (cu, 0, sizeof (*cu));
14915   cu->objfile = objfile;
14916   obstack_init (&cu->comp_unit_obstack);
14917 }
14918
14919 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14920
14921 static void
14922 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14923 {
14924   struct attribute *attr;
14925
14926   /* Set the language we're debugging.  */
14927   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14928   if (attr)
14929     set_cu_language (DW_UNSND (attr), cu);
14930   else
14931     {
14932       cu->language = language_minimal;
14933       cu->language_defn = language_def (cu->language);
14934     }
14935 }
14936
14937 /* Release one cached compilation unit, CU.  We unlink it from the tree
14938    of compilation units, but we don't remove it from the read_in_chain;
14939    the caller is responsible for that.
14940    NOTE: DATA is a void * because this function is also used as a
14941    cleanup routine.  */
14942
14943 static void
14944 free_one_comp_unit (void *data)
14945 {
14946   struct dwarf2_cu *cu = data;
14947
14948   if (cu->per_cu != NULL)
14949     cu->per_cu->cu = NULL;
14950   cu->per_cu = NULL;
14951
14952   obstack_free (&cu->comp_unit_obstack, NULL);
14953
14954   xfree (cu);
14955 }
14956
14957 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14958    when we're finished with it.  We can't free the pointer itself, but be
14959    sure to unlink it from the cache.  Also release any associated storage
14960    and perform cache maintenance.
14961
14962    Only used during partial symbol parsing.  */
14963
14964 static void
14965 free_stack_comp_unit (void *data)
14966 {
14967   struct dwarf2_cu *cu = data;
14968
14969   obstack_free (&cu->comp_unit_obstack, NULL);
14970   cu->partial_dies = NULL;
14971
14972   if (cu->per_cu != NULL)
14973     {
14974       /* This compilation unit is on the stack in our caller, so we
14975          should not xfree it.  Just unlink it.  */
14976       cu->per_cu->cu = NULL;
14977       cu->per_cu = NULL;
14978
14979       /* If we had a per-cu pointer, then we may have other compilation
14980          units loaded, so age them now.  */
14981       age_cached_comp_units ();
14982     }
14983 }
14984
14985 /* Free all cached compilation units.  */
14986
14987 static void
14988 free_cached_comp_units (void *data)
14989 {
14990   struct dwarf2_per_cu_data *per_cu, **last_chain;
14991
14992   per_cu = dwarf2_per_objfile->read_in_chain;
14993   last_chain = &dwarf2_per_objfile->read_in_chain;
14994   while (per_cu != NULL)
14995     {
14996       struct dwarf2_per_cu_data *next_cu;
14997
14998       next_cu = per_cu->cu->read_in_chain;
14999
15000       free_one_comp_unit (per_cu->cu);
15001       *last_chain = next_cu;
15002
15003       per_cu = next_cu;
15004     }
15005 }
15006
15007 /* Increase the age counter on each cached compilation unit, and free
15008    any that are too old.  */
15009
15010 static void
15011 age_cached_comp_units (void)
15012 {
15013   struct dwarf2_per_cu_data *per_cu, **last_chain;
15014
15015   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15016   per_cu = dwarf2_per_objfile->read_in_chain;
15017   while (per_cu != NULL)
15018     {
15019       per_cu->cu->last_used ++;
15020       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15021         dwarf2_mark (per_cu->cu);
15022       per_cu = per_cu->cu->read_in_chain;
15023     }
15024
15025   per_cu = dwarf2_per_objfile->read_in_chain;
15026   last_chain = &dwarf2_per_objfile->read_in_chain;
15027   while (per_cu != NULL)
15028     {
15029       struct dwarf2_per_cu_data *next_cu;
15030
15031       next_cu = per_cu->cu->read_in_chain;
15032
15033       if (!per_cu->cu->mark)
15034         {
15035           free_one_comp_unit (per_cu->cu);
15036           *last_chain = next_cu;
15037         }
15038       else
15039         last_chain = &per_cu->cu->read_in_chain;
15040
15041       per_cu = next_cu;
15042     }
15043 }
15044
15045 /* Remove a single compilation unit from the cache.  */
15046
15047 static void
15048 free_one_cached_comp_unit (void *target_cu)
15049 {
15050   struct dwarf2_per_cu_data *per_cu, **last_chain;
15051
15052   per_cu = dwarf2_per_objfile->read_in_chain;
15053   last_chain = &dwarf2_per_objfile->read_in_chain;
15054   while (per_cu != NULL)
15055     {
15056       struct dwarf2_per_cu_data *next_cu;
15057
15058       next_cu = per_cu->cu->read_in_chain;
15059
15060       if (per_cu->cu == target_cu)
15061         {
15062           free_one_comp_unit (per_cu->cu);
15063           *last_chain = next_cu;
15064           break;
15065         }
15066       else
15067         last_chain = &per_cu->cu->read_in_chain;
15068
15069       per_cu = next_cu;
15070     }
15071 }
15072
15073 /* Release all extra memory associated with OBJFILE.  */
15074
15075 void
15076 dwarf2_free_objfile (struct objfile *objfile)
15077 {
15078   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15079
15080   if (dwarf2_per_objfile == NULL)
15081     return;
15082
15083   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
15084   free_cached_comp_units (NULL);
15085
15086   if (dwarf2_per_objfile->quick_file_names_table)
15087     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15088
15089   /* Everything else should be on the objfile obstack.  */
15090 }
15091
15092 /* A pair of DIE offset and GDB type pointer.  We store these
15093    in a hash table separate from the DIEs, and preserve them
15094    when the DIEs are flushed out of cache.  */
15095
15096 struct dwarf2_offset_and_type
15097 {
15098   unsigned int offset;
15099   struct type *type;
15100 };
15101
15102 /* Hash function for a dwarf2_offset_and_type.  */
15103
15104 static hashval_t
15105 offset_and_type_hash (const void *item)
15106 {
15107   const struct dwarf2_offset_and_type *ofs = item;
15108
15109   return ofs->offset;
15110 }
15111
15112 /* Equality function for a dwarf2_offset_and_type.  */
15113
15114 static int
15115 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15116 {
15117   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15118   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15119
15120   return ofs_lhs->offset == ofs_rhs->offset;
15121 }
15122
15123 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15124    table if necessary.  For convenience, return TYPE.
15125
15126    The DIEs reading must have careful ordering to:
15127     * Not cause infite loops trying to read in DIEs as a prerequisite for
15128       reading current DIE.
15129     * Not trying to dereference contents of still incompletely read in types
15130       while reading in other DIEs.
15131     * Enable referencing still incompletely read in types just by a pointer to
15132       the type without accessing its fields.
15133
15134    Therefore caller should follow these rules:
15135      * Try to fetch any prerequisite types we may need to build this DIE type
15136        before building the type and calling set_die_type.
15137      * After building type call set_die_type for current DIE as soon as
15138        possible before fetching more types to complete the current type.
15139      * Make the type as complete as possible before fetching more types.  */
15140
15141 static struct type *
15142 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15143 {
15144   struct dwarf2_offset_and_type **slot, ofs;
15145   struct objfile *objfile = cu->objfile;
15146   htab_t *type_hash_ptr;
15147
15148   /* For Ada types, make sure that the gnat-specific data is always
15149      initialized (if not already set).  There are a few types where
15150      we should not be doing so, because the type-specific area is
15151      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15152      where the type-specific area is used to store the floatformat).
15153      But this is not a problem, because the gnat-specific information
15154      is actually not needed for these types.  */
15155   if (need_gnat_info (cu)
15156       && TYPE_CODE (type) != TYPE_CODE_FUNC
15157       && TYPE_CODE (type) != TYPE_CODE_FLT
15158       && !HAVE_GNAT_AUX_INFO (type))
15159     INIT_GNAT_SPECIFIC (type);
15160
15161   if (cu->per_cu->from_debug_types)
15162     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15163   else
15164     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15165
15166   if (*type_hash_ptr == NULL)
15167     {
15168       *type_hash_ptr
15169         = htab_create_alloc_ex (127,
15170                                 offset_and_type_hash,
15171                                 offset_and_type_eq,
15172                                 NULL,
15173                                 &objfile->objfile_obstack,
15174                                 hashtab_obstack_allocate,
15175                                 dummy_obstack_deallocate);
15176     }
15177
15178   ofs.offset = die->offset;
15179   ofs.type = type;
15180   slot = (struct dwarf2_offset_and_type **)
15181     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15182   if (*slot)
15183     complaint (&symfile_complaints,
15184                _("A problem internal to GDB: DIE 0x%x has type already set"),
15185                die->offset);
15186   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15187   **slot = ofs;
15188   return type;
15189 }
15190
15191 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15192    table, or return NULL if the die does not have a saved type.  */
15193
15194 static struct type *
15195 get_die_type_at_offset (unsigned int offset,
15196                         struct dwarf2_per_cu_data *per_cu)
15197 {
15198   struct dwarf2_offset_and_type *slot, ofs;
15199   htab_t type_hash;
15200
15201   if (per_cu->from_debug_types)
15202     type_hash = dwarf2_per_objfile->debug_types_type_hash;
15203   else
15204     type_hash = dwarf2_per_objfile->debug_info_type_hash;
15205   if (type_hash == NULL)
15206     return NULL;
15207
15208   ofs.offset = offset;
15209   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15210   if (slot)
15211     return slot->type;
15212   else
15213     return NULL;
15214 }
15215
15216 /* Look up the type for DIE in the appropriate type_hash table,
15217    or return NULL if DIE does not have a saved type.  */
15218
15219 static struct type *
15220 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15221 {
15222   return get_die_type_at_offset (die->offset, cu->per_cu);
15223 }
15224
15225 /* Add a dependence relationship from CU to REF_PER_CU.  */
15226
15227 static void
15228 dwarf2_add_dependence (struct dwarf2_cu *cu,
15229                        struct dwarf2_per_cu_data *ref_per_cu)
15230 {
15231   void **slot;
15232
15233   if (cu->dependencies == NULL)
15234     cu->dependencies
15235       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15236                               NULL, &cu->comp_unit_obstack,
15237                               hashtab_obstack_allocate,
15238                               dummy_obstack_deallocate);
15239
15240   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15241   if (*slot == NULL)
15242     *slot = ref_per_cu;
15243 }
15244
15245 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15246    Set the mark field in every compilation unit in the
15247    cache that we must keep because we are keeping CU.  */
15248
15249 static int
15250 dwarf2_mark_helper (void **slot, void *data)
15251 {
15252   struct dwarf2_per_cu_data *per_cu;
15253
15254   per_cu = (struct dwarf2_per_cu_data *) *slot;
15255   if (per_cu->cu->mark)
15256     return 1;
15257   per_cu->cu->mark = 1;
15258
15259   if (per_cu->cu->dependencies != NULL)
15260     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15261
15262   return 1;
15263 }
15264
15265 /* Set the mark field in CU and in every other compilation unit in the
15266    cache that we must keep because we are keeping CU.  */
15267
15268 static void
15269 dwarf2_mark (struct dwarf2_cu *cu)
15270 {
15271   if (cu->mark)
15272     return;
15273   cu->mark = 1;
15274   if (cu->dependencies != NULL)
15275     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15276 }
15277
15278 static void
15279 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15280 {
15281   while (per_cu)
15282     {
15283       per_cu->cu->mark = 0;
15284       per_cu = per_cu->cu->read_in_chain;
15285     }
15286 }
15287
15288 /* Trivial hash function for partial_die_info: the hash value of a DIE
15289    is its offset in .debug_info for this objfile.  */
15290
15291 static hashval_t
15292 partial_die_hash (const void *item)
15293 {
15294   const struct partial_die_info *part_die = item;
15295
15296   return part_die->offset;
15297 }
15298
15299 /* Trivial comparison function for partial_die_info structures: two DIEs
15300    are equal if they have the same offset.  */
15301
15302 static int
15303 partial_die_eq (const void *item_lhs, const void *item_rhs)
15304 {
15305   const struct partial_die_info *part_die_lhs = item_lhs;
15306   const struct partial_die_info *part_die_rhs = item_rhs;
15307
15308   return part_die_lhs->offset == part_die_rhs->offset;
15309 }
15310
15311 static struct cmd_list_element *set_dwarf2_cmdlist;
15312 static struct cmd_list_element *show_dwarf2_cmdlist;
15313
15314 static void
15315 set_dwarf2_cmd (char *args, int from_tty)
15316 {
15317   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15318 }
15319
15320 static void
15321 show_dwarf2_cmd (char *args, int from_tty)
15322 {
15323   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15324 }
15325
15326 /* If section described by INFO was mmapped, munmap it now.  */
15327
15328 static void
15329 munmap_section_buffer (struct dwarf2_section_info *info)
15330 {
15331   if (info->was_mmapped)
15332     {
15333 #ifdef HAVE_MMAP
15334       intptr_t begin = (intptr_t) info->buffer;
15335       intptr_t map_begin = begin & ~(pagesize - 1);
15336       size_t map_length = info->size + begin - map_begin;
15337
15338       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15339 #else
15340       /* Without HAVE_MMAP, we should never be here to begin with.  */
15341       gdb_assert_not_reached ("no mmap support");
15342 #endif
15343     }
15344 }
15345
15346 /* munmap debug sections for OBJFILE, if necessary.  */
15347
15348 static void
15349 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15350 {
15351   struct dwarf2_per_objfile *data = d;
15352
15353   /* This is sorted according to the order they're defined in to make it easier
15354      to keep in sync.  */
15355   munmap_section_buffer (&data->info);
15356   munmap_section_buffer (&data->abbrev);
15357   munmap_section_buffer (&data->line);
15358   munmap_section_buffer (&data->loc);
15359   munmap_section_buffer (&data->macinfo);
15360   munmap_section_buffer (&data->str);
15361   munmap_section_buffer (&data->ranges);
15362   munmap_section_buffer (&data->types);
15363   munmap_section_buffer (&data->frame);
15364   munmap_section_buffer (&data->eh_frame);
15365   munmap_section_buffer (&data->gdb_index);
15366 }
15367
15368 \f
15369 /* The "save gdb-index" command.  */
15370
15371 /* The contents of the hash table we create when building the string
15372    table.  */
15373 struct strtab_entry
15374 {
15375   offset_type offset;
15376   const char *str;
15377 };
15378
15379 /* Hash function for a strtab_entry.
15380
15381    Function is used only during write_hash_table so no index format backward
15382    compatibility is needed.  */
15383
15384 static hashval_t
15385 hash_strtab_entry (const void *e)
15386 {
15387   const struct strtab_entry *entry = e;
15388   return mapped_index_string_hash (INT_MAX, entry->str);
15389 }
15390
15391 /* Equality function for a strtab_entry.  */
15392
15393 static int
15394 eq_strtab_entry (const void *a, const void *b)
15395 {
15396   const struct strtab_entry *ea = a;
15397   const struct strtab_entry *eb = b;
15398   return !strcmp (ea->str, eb->str);
15399 }
15400
15401 /* Create a strtab_entry hash table.  */
15402
15403 static htab_t
15404 create_strtab (void)
15405 {
15406   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15407                             xfree, xcalloc, xfree);
15408 }
15409
15410 /* Add a string to the constant pool.  Return the string's offset in
15411    host order.  */
15412
15413 static offset_type
15414 add_string (htab_t table, struct obstack *cpool, const char *str)
15415 {
15416   void **slot;
15417   struct strtab_entry entry;
15418   struct strtab_entry *result;
15419
15420   entry.str = str;
15421   slot = htab_find_slot (table, &entry, INSERT);
15422   if (*slot)
15423     result = *slot;
15424   else
15425     {
15426       result = XNEW (struct strtab_entry);
15427       result->offset = obstack_object_size (cpool);
15428       result->str = str;
15429       obstack_grow_str0 (cpool, str);
15430       *slot = result;
15431     }
15432   return result->offset;
15433 }
15434
15435 /* An entry in the symbol table.  */
15436 struct symtab_index_entry
15437 {
15438   /* The name of the symbol.  */
15439   const char *name;
15440   /* The offset of the name in the constant pool.  */
15441   offset_type index_offset;
15442   /* A sorted vector of the indices of all the CUs that hold an object
15443      of this name.  */
15444   VEC (offset_type) *cu_indices;
15445 };
15446
15447 /* The symbol table.  This is a power-of-2-sized hash table.  */
15448 struct mapped_symtab
15449 {
15450   offset_type n_elements;
15451   offset_type size;
15452   struct symtab_index_entry **data;
15453 };
15454
15455 /* Hash function for a symtab_index_entry.  */
15456
15457 static hashval_t
15458 hash_symtab_entry (const void *e)
15459 {
15460   const struct symtab_index_entry *entry = e;
15461   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15462                          sizeof (offset_type) * VEC_length (offset_type,
15463                                                             entry->cu_indices),
15464                          0);
15465 }
15466
15467 /* Equality function for a symtab_index_entry.  */
15468
15469 static int
15470 eq_symtab_entry (const void *a, const void *b)
15471 {
15472   const struct symtab_index_entry *ea = a;
15473   const struct symtab_index_entry *eb = b;
15474   int len = VEC_length (offset_type, ea->cu_indices);
15475   if (len != VEC_length (offset_type, eb->cu_indices))
15476     return 0;
15477   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15478                   VEC_address (offset_type, eb->cu_indices),
15479                   sizeof (offset_type) * len);
15480 }
15481
15482 /* Destroy a symtab_index_entry.  */
15483
15484 static void
15485 delete_symtab_entry (void *p)
15486 {
15487   struct symtab_index_entry *entry = p;
15488   VEC_free (offset_type, entry->cu_indices);
15489   xfree (entry);
15490 }
15491
15492 /* Create a hash table holding symtab_index_entry objects.  */
15493
15494 static htab_t
15495 create_symbol_hash_table (void)
15496 {
15497   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15498                             delete_symtab_entry, xcalloc, xfree);
15499 }
15500
15501 /* Create a new mapped symtab object.  */
15502
15503 static struct mapped_symtab *
15504 create_mapped_symtab (void)
15505 {
15506   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15507   symtab->n_elements = 0;
15508   symtab->size = 1024;
15509   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15510   return symtab;
15511 }
15512
15513 /* Destroy a mapped_symtab.  */
15514
15515 static void
15516 cleanup_mapped_symtab (void *p)
15517 {
15518   struct mapped_symtab *symtab = p;
15519   /* The contents of the array are freed when the other hash table is
15520      destroyed.  */
15521   xfree (symtab->data);
15522   xfree (symtab);
15523 }
15524
15525 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15526    the slot.
15527    
15528    Function is used only during write_hash_table so no index format backward
15529    compatibility is needed.  */
15530
15531 static struct symtab_index_entry **
15532 find_slot (struct mapped_symtab *symtab, const char *name)
15533 {
15534   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15535
15536   index = hash & (symtab->size - 1);
15537   step = ((hash * 17) & (symtab->size - 1)) | 1;
15538
15539   for (;;)
15540     {
15541       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15542         return &symtab->data[index];
15543       index = (index + step) & (symtab->size - 1);
15544     }
15545 }
15546
15547 /* Expand SYMTAB's hash table.  */
15548
15549 static void
15550 hash_expand (struct mapped_symtab *symtab)
15551 {
15552   offset_type old_size = symtab->size;
15553   offset_type i;
15554   struct symtab_index_entry **old_entries = symtab->data;
15555
15556   symtab->size *= 2;
15557   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15558
15559   for (i = 0; i < old_size; ++i)
15560     {
15561       if (old_entries[i])
15562         {
15563           struct symtab_index_entry **slot = find_slot (symtab,
15564                                                         old_entries[i]->name);
15565           *slot = old_entries[i];
15566         }
15567     }
15568
15569   xfree (old_entries);
15570 }
15571
15572 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15573    is the index of the CU in which the symbol appears.  */
15574
15575 static void
15576 add_index_entry (struct mapped_symtab *symtab, const char *name,
15577                  offset_type cu_index)
15578 {
15579   struct symtab_index_entry **slot;
15580
15581   ++symtab->n_elements;
15582   if (4 * symtab->n_elements / 3 >= symtab->size)
15583     hash_expand (symtab);
15584
15585   slot = find_slot (symtab, name);
15586   if (!*slot)
15587     {
15588       *slot = XNEW (struct symtab_index_entry);
15589       (*slot)->name = name;
15590       (*slot)->cu_indices = NULL;
15591     }
15592   /* Don't push an index twice.  Due to how we add entries we only
15593      have to check the last one.  */ 
15594   if (VEC_empty (offset_type, (*slot)->cu_indices)
15595       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15596     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15597 }
15598
15599 /* Add a vector of indices to the constant pool.  */
15600
15601 static offset_type
15602 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15603                       struct symtab_index_entry *entry)
15604 {
15605   void **slot;
15606
15607   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15608   if (!*slot)
15609     {
15610       offset_type len = VEC_length (offset_type, entry->cu_indices);
15611       offset_type val = MAYBE_SWAP (len);
15612       offset_type iter;
15613       int i;
15614
15615       *slot = entry;
15616       entry->index_offset = obstack_object_size (cpool);
15617
15618       obstack_grow (cpool, &val, sizeof (val));
15619       for (i = 0;
15620            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15621            ++i)
15622         {
15623           val = MAYBE_SWAP (iter);
15624           obstack_grow (cpool, &val, sizeof (val));
15625         }
15626     }
15627   else
15628     {
15629       struct symtab_index_entry *old_entry = *slot;
15630       entry->index_offset = old_entry->index_offset;
15631       entry = old_entry;
15632     }
15633   return entry->index_offset;
15634 }
15635
15636 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15637    constant pool entries going into the obstack CPOOL.  */
15638
15639 static void
15640 write_hash_table (struct mapped_symtab *symtab,
15641                   struct obstack *output, struct obstack *cpool)
15642 {
15643   offset_type i;
15644   htab_t symbol_hash_table;
15645   htab_t str_table;
15646
15647   symbol_hash_table = create_symbol_hash_table ();
15648   str_table = create_strtab ();
15649
15650   /* We add all the index vectors to the constant pool first, to
15651      ensure alignment is ok.  */
15652   for (i = 0; i < symtab->size; ++i)
15653     {
15654       if (symtab->data[i])
15655         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15656     }
15657
15658   /* Now write out the hash table.  */
15659   for (i = 0; i < symtab->size; ++i)
15660     {
15661       offset_type str_off, vec_off;
15662
15663       if (symtab->data[i])
15664         {
15665           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15666           vec_off = symtab->data[i]->index_offset;
15667         }
15668       else
15669         {
15670           /* While 0 is a valid constant pool index, it is not valid
15671              to have 0 for both offsets.  */
15672           str_off = 0;
15673           vec_off = 0;
15674         }
15675
15676       str_off = MAYBE_SWAP (str_off);
15677       vec_off = MAYBE_SWAP (vec_off);
15678
15679       obstack_grow (output, &str_off, sizeof (str_off));
15680       obstack_grow (output, &vec_off, sizeof (vec_off));
15681     }
15682
15683   htab_delete (str_table);
15684   htab_delete (symbol_hash_table);
15685 }
15686
15687 /* Struct to map psymtab to CU index in the index file.  */
15688 struct psymtab_cu_index_map
15689 {
15690   struct partial_symtab *psymtab;
15691   unsigned int cu_index;
15692 };
15693
15694 static hashval_t
15695 hash_psymtab_cu_index (const void *item)
15696 {
15697   const struct psymtab_cu_index_map *map = item;
15698
15699   return htab_hash_pointer (map->psymtab);
15700 }
15701
15702 static int
15703 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15704 {
15705   const struct psymtab_cu_index_map *lhs = item_lhs;
15706   const struct psymtab_cu_index_map *rhs = item_rhs;
15707
15708   return lhs->psymtab == rhs->psymtab;
15709 }
15710
15711 /* Helper struct for building the address table.  */
15712 struct addrmap_index_data
15713 {
15714   struct objfile *objfile;
15715   struct obstack *addr_obstack;
15716   htab_t cu_index_htab;
15717
15718   /* Non-zero if the previous_* fields are valid.
15719      We can't write an entry until we see the next entry (since it is only then
15720      that we know the end of the entry).  */
15721   int previous_valid;
15722   /* Index of the CU in the table of all CUs in the index file.  */
15723   unsigned int previous_cu_index;
15724   /* Start address of the CU.  */
15725   CORE_ADDR previous_cu_start;
15726 };
15727
15728 /* Write an address entry to OBSTACK.  */
15729
15730 static void
15731 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15732                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15733 {
15734   offset_type cu_index_to_write;
15735   char addr[8];
15736   CORE_ADDR baseaddr;
15737
15738   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15739
15740   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15741   obstack_grow (obstack, addr, 8);
15742   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15743   obstack_grow (obstack, addr, 8);
15744   cu_index_to_write = MAYBE_SWAP (cu_index);
15745   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15746 }
15747
15748 /* Worker function for traversing an addrmap to build the address table.  */
15749
15750 static int
15751 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15752 {
15753   struct addrmap_index_data *data = datap;
15754   struct partial_symtab *pst = obj;
15755   offset_type cu_index;
15756   void **slot;
15757
15758   if (data->previous_valid)
15759     add_address_entry (data->objfile, data->addr_obstack,
15760                        data->previous_cu_start, start_addr,
15761                        data->previous_cu_index);
15762
15763   data->previous_cu_start = start_addr;
15764   if (pst != NULL)
15765     {
15766       struct psymtab_cu_index_map find_map, *map;
15767       find_map.psymtab = pst;
15768       map = htab_find (data->cu_index_htab, &find_map);
15769       gdb_assert (map != NULL);
15770       data->previous_cu_index = map->cu_index;
15771       data->previous_valid = 1;
15772     }
15773   else
15774       data->previous_valid = 0;
15775
15776   return 0;
15777 }
15778
15779 /* Write OBJFILE's address map to OBSTACK.
15780    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15781    in the index file.  */
15782
15783 static void
15784 write_address_map (struct objfile *objfile, struct obstack *obstack,
15785                    htab_t cu_index_htab)
15786 {
15787   struct addrmap_index_data addrmap_index_data;
15788
15789   /* When writing the address table, we have to cope with the fact that
15790      the addrmap iterator only provides the start of a region; we have to
15791      wait until the next invocation to get the start of the next region.  */
15792
15793   addrmap_index_data.objfile = objfile;
15794   addrmap_index_data.addr_obstack = obstack;
15795   addrmap_index_data.cu_index_htab = cu_index_htab;
15796   addrmap_index_data.previous_valid = 0;
15797
15798   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15799                    &addrmap_index_data);
15800
15801   /* It's highly unlikely the last entry (end address = 0xff...ff)
15802      is valid, but we should still handle it.
15803      The end address is recorded as the start of the next region, but that
15804      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15805      anyway.  */
15806   if (addrmap_index_data.previous_valid)
15807     add_address_entry (objfile, obstack,
15808                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15809                        addrmap_index_data.previous_cu_index);
15810 }
15811
15812 /* Add a list of partial symbols to SYMTAB.  */
15813
15814 static void
15815 write_psymbols (struct mapped_symtab *symtab,
15816                 htab_t psyms_seen,
15817                 struct partial_symbol **psymp,
15818                 int count,
15819                 offset_type cu_index,
15820                 int is_static)
15821 {
15822   for (; count-- > 0; ++psymp)
15823     {
15824       void **slot, *lookup;
15825
15826       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15827         error (_("Ada is not currently supported by the index"));
15828
15829       /* We only want to add a given psymbol once.  However, we also
15830          want to account for whether it is global or static.  So, we
15831          may add it twice, using slightly different values.  */
15832       if (is_static)
15833         {
15834           uintptr_t val = 1 | (uintptr_t) *psymp;
15835
15836           lookup = (void *) val;
15837         }
15838       else
15839         lookup = *psymp;
15840
15841       /* Only add a given psymbol once.  */
15842       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15843       if (!*slot)
15844         {
15845           *slot = lookup;
15846           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15847         }
15848     }
15849 }
15850
15851 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15852    exception if there is an error.  */
15853
15854 static void
15855 write_obstack (FILE *file, struct obstack *obstack)
15856 {
15857   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15858               file)
15859       != obstack_object_size (obstack))
15860     error (_("couldn't data write to file"));
15861 }
15862
15863 /* Unlink a file if the argument is not NULL.  */
15864
15865 static void
15866 unlink_if_set (void *p)
15867 {
15868   char **filename = p;
15869   if (*filename)
15870     unlink (*filename);
15871 }
15872
15873 /* A helper struct used when iterating over debug_types.  */
15874 struct signatured_type_index_data
15875 {
15876   struct objfile *objfile;
15877   struct mapped_symtab *symtab;
15878   struct obstack *types_list;
15879   htab_t psyms_seen;
15880   int cu_index;
15881 };
15882
15883 /* A helper function that writes a single signatured_type to an
15884    obstack.  */
15885
15886 static int
15887 write_one_signatured_type (void **slot, void *d)
15888 {
15889   struct signatured_type_index_data *info = d;
15890   struct signatured_type *entry = (struct signatured_type *) *slot;
15891   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15892   struct partial_symtab *psymtab = per_cu->v.psymtab;
15893   gdb_byte val[8];
15894
15895   write_psymbols (info->symtab,
15896                   info->psyms_seen,
15897                   info->objfile->global_psymbols.list
15898                   + psymtab->globals_offset,
15899                   psymtab->n_global_syms, info->cu_index,
15900                   0);
15901   write_psymbols (info->symtab,
15902                   info->psyms_seen,
15903                   info->objfile->static_psymbols.list
15904                   + psymtab->statics_offset,
15905                   psymtab->n_static_syms, info->cu_index,
15906                   1);
15907
15908   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15909   obstack_grow (info->types_list, val, 8);
15910   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15911   obstack_grow (info->types_list, val, 8);
15912   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15913   obstack_grow (info->types_list, val, 8);
15914
15915   ++info->cu_index;
15916
15917   return 1;
15918 }
15919
15920 /* A cleanup function for an htab_t.  */
15921
15922 static void
15923 cleanup_htab (void *arg)
15924 {
15925   htab_delete (arg);
15926 }
15927
15928 /* Create an index file for OBJFILE in the directory DIR.  */
15929
15930 static void
15931 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15932 {
15933   struct cleanup *cleanup;
15934   char *filename, *cleanup_filename;
15935   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15936   struct obstack cu_list, types_cu_list;
15937   int i;
15938   FILE *out_file;
15939   struct mapped_symtab *symtab;
15940   offset_type val, size_of_contents, total_len;
15941   struct stat st;
15942   char buf[8];
15943   htab_t psyms_seen;
15944   htab_t cu_index_htab;
15945   struct psymtab_cu_index_map *psymtab_cu_index_map;
15946
15947   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15948     return;
15949
15950   if (dwarf2_per_objfile->using_index)
15951     error (_("Cannot use an index to create the index"));
15952
15953   if (stat (objfile->name, &st) < 0)
15954     perror_with_name (objfile->name);
15955
15956   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15957                      INDEX_SUFFIX, (char *) NULL);
15958   cleanup = make_cleanup (xfree, filename);
15959
15960   out_file = fopen (filename, "wb");
15961   if (!out_file)
15962     error (_("Can't open `%s' for writing"), filename);
15963
15964   cleanup_filename = filename;
15965   make_cleanup (unlink_if_set, &cleanup_filename);
15966
15967   symtab = create_mapped_symtab ();
15968   make_cleanup (cleanup_mapped_symtab, symtab);
15969
15970   obstack_init (&addr_obstack);
15971   make_cleanup_obstack_free (&addr_obstack);
15972
15973   obstack_init (&cu_list);
15974   make_cleanup_obstack_free (&cu_list);
15975
15976   obstack_init (&types_cu_list);
15977   make_cleanup_obstack_free (&types_cu_list);
15978
15979   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15980                                   NULL, xcalloc, xfree);
15981   make_cleanup (cleanup_htab, psyms_seen);
15982
15983   /* While we're scanning CU's create a table that maps a psymtab pointer
15984      (which is what addrmap records) to its index (which is what is recorded
15985      in the index file).  This will later be needed to write the address
15986      table.  */
15987   cu_index_htab = htab_create_alloc (100,
15988                                      hash_psymtab_cu_index,
15989                                      eq_psymtab_cu_index,
15990                                      NULL, xcalloc, xfree);
15991   make_cleanup (cleanup_htab, cu_index_htab);
15992   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15993     xmalloc (sizeof (struct psymtab_cu_index_map)
15994              * dwarf2_per_objfile->n_comp_units);
15995   make_cleanup (xfree, psymtab_cu_index_map);
15996
15997   /* The CU list is already sorted, so we don't need to do additional
15998      work here.  Also, the debug_types entries do not appear in
15999      all_comp_units, but only in their own hash table.  */
16000   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16001     {
16002       struct dwarf2_per_cu_data *per_cu
16003         = dwarf2_per_objfile->all_comp_units[i];
16004       struct partial_symtab *psymtab = per_cu->v.psymtab;
16005       gdb_byte val[8];
16006       struct psymtab_cu_index_map *map;
16007       void **slot;
16008
16009       write_psymbols (symtab,
16010                       psyms_seen,
16011                       objfile->global_psymbols.list + psymtab->globals_offset,
16012                       psymtab->n_global_syms, i,
16013                       0);
16014       write_psymbols (symtab,
16015                       psyms_seen,
16016                       objfile->static_psymbols.list + psymtab->statics_offset,
16017                       psymtab->n_static_syms, i,
16018                       1);
16019
16020       map = &psymtab_cu_index_map[i];
16021       map->psymtab = psymtab;
16022       map->cu_index = i;
16023       slot = htab_find_slot (cu_index_htab, map, INSERT);
16024       gdb_assert (slot != NULL);
16025       gdb_assert (*slot == NULL);
16026       *slot = map;
16027
16028       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16029       obstack_grow (&cu_list, val, 8);
16030       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16031       obstack_grow (&cu_list, val, 8);
16032     }
16033
16034   /* Dump the address map.  */
16035   write_address_map (objfile, &addr_obstack, cu_index_htab);
16036
16037   /* Write out the .debug_type entries, if any.  */
16038   if (dwarf2_per_objfile->signatured_types)
16039     {
16040       struct signatured_type_index_data sig_data;
16041
16042       sig_data.objfile = objfile;
16043       sig_data.symtab = symtab;
16044       sig_data.types_list = &types_cu_list;
16045       sig_data.psyms_seen = psyms_seen;
16046       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16047       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16048                               write_one_signatured_type, &sig_data);
16049     }
16050
16051   obstack_init (&constant_pool);
16052   make_cleanup_obstack_free (&constant_pool);
16053   obstack_init (&symtab_obstack);
16054   make_cleanup_obstack_free (&symtab_obstack);
16055   write_hash_table (symtab, &symtab_obstack, &constant_pool);
16056
16057   obstack_init (&contents);
16058   make_cleanup_obstack_free (&contents);
16059   size_of_contents = 6 * sizeof (offset_type);
16060   total_len = size_of_contents;
16061
16062   /* The version number.  */
16063   val = MAYBE_SWAP (5);
16064   obstack_grow (&contents, &val, sizeof (val));
16065
16066   /* The offset of the CU list from the start of the file.  */
16067   val = MAYBE_SWAP (total_len);
16068   obstack_grow (&contents, &val, sizeof (val));
16069   total_len += obstack_object_size (&cu_list);
16070
16071   /* The offset of the types CU list from the start of the file.  */
16072   val = MAYBE_SWAP (total_len);
16073   obstack_grow (&contents, &val, sizeof (val));
16074   total_len += obstack_object_size (&types_cu_list);
16075
16076   /* The offset of the address table from the start of the file.  */
16077   val = MAYBE_SWAP (total_len);
16078   obstack_grow (&contents, &val, sizeof (val));
16079   total_len += obstack_object_size (&addr_obstack);
16080
16081   /* The offset of the symbol table from the start of the file.  */
16082   val = MAYBE_SWAP (total_len);
16083   obstack_grow (&contents, &val, sizeof (val));
16084   total_len += obstack_object_size (&symtab_obstack);
16085
16086   /* The offset of the constant pool from the start of the file.  */
16087   val = MAYBE_SWAP (total_len);
16088   obstack_grow (&contents, &val, sizeof (val));
16089   total_len += obstack_object_size (&constant_pool);
16090
16091   gdb_assert (obstack_object_size (&contents) == size_of_contents);
16092
16093   write_obstack (out_file, &contents);
16094   write_obstack (out_file, &cu_list);
16095   write_obstack (out_file, &types_cu_list);
16096   write_obstack (out_file, &addr_obstack);
16097   write_obstack (out_file, &symtab_obstack);
16098   write_obstack (out_file, &constant_pool);
16099
16100   fclose (out_file);
16101
16102   /* We want to keep the file, so we set cleanup_filename to NULL
16103      here.  See unlink_if_set.  */
16104   cleanup_filename = NULL;
16105
16106   do_cleanups (cleanup);
16107 }
16108
16109 /* Implementation of the `save gdb-index' command.
16110    
16111    Note that the file format used by this command is documented in the
16112    GDB manual.  Any changes here must be documented there.  */
16113
16114 static void
16115 save_gdb_index_command (char *arg, int from_tty)
16116 {
16117   struct objfile *objfile;
16118
16119   if (!arg || !*arg)
16120     error (_("usage: save gdb-index DIRECTORY"));
16121
16122   ALL_OBJFILES (objfile)
16123   {
16124     struct stat st;
16125
16126     /* If the objfile does not correspond to an actual file, skip it.  */
16127     if (stat (objfile->name, &st) < 0)
16128       continue;
16129
16130     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16131     if (dwarf2_per_objfile)
16132       {
16133         volatile struct gdb_exception except;
16134
16135         TRY_CATCH (except, RETURN_MASK_ERROR)
16136           {
16137             write_psymtabs_to_index (objfile, arg);
16138           }
16139         if (except.reason < 0)
16140           exception_fprintf (gdb_stderr, except,
16141                              _("Error while writing index for `%s': "),
16142                              objfile->name);
16143       }
16144   }
16145 }
16146
16147 \f
16148
16149 int dwarf2_always_disassemble;
16150
16151 static void
16152 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16153                                 struct cmd_list_element *c, const char *value)
16154 {
16155   fprintf_filtered (file,
16156                     _("Whether to always disassemble "
16157                       "DWARF expressions is %s.\n"),
16158                     value);
16159 }
16160
16161 void _initialize_dwarf2_read (void);
16162
16163 void
16164 _initialize_dwarf2_read (void)
16165 {
16166   struct cmd_list_element *c;
16167
16168   dwarf2_objfile_data_key
16169     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16170
16171   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16172 Set DWARF 2 specific variables.\n\
16173 Configure DWARF 2 variables such as the cache size"),
16174                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16175                   0/*allow-unknown*/, &maintenance_set_cmdlist);
16176
16177   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16178 Show DWARF 2 specific variables\n\
16179 Show DWARF 2 variables such as the cache size"),
16180                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16181                   0/*allow-unknown*/, &maintenance_show_cmdlist);
16182
16183   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16184                             &dwarf2_max_cache_age, _("\
16185 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16186 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16187 A higher limit means that cached compilation units will be stored\n\
16188 in memory longer, and more total memory will be used.  Zero disables\n\
16189 caching, which can slow down startup."),
16190                             NULL,
16191                             show_dwarf2_max_cache_age,
16192                             &set_dwarf2_cmdlist,
16193                             &show_dwarf2_cmdlist);
16194
16195   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16196                            &dwarf2_always_disassemble, _("\
16197 Set whether `info address' always disassembles DWARF expressions."), _("\
16198 Show whether `info address' always disassembles DWARF expressions."), _("\
16199 When enabled, DWARF expressions are always printed in an assembly-like\n\
16200 syntax.  When disabled, expressions will be printed in a more\n\
16201 conversational style, when possible."),
16202                            NULL,
16203                            show_dwarf2_always_disassemble,
16204                            &set_dwarf2_cmdlist,
16205                            &show_dwarf2_cmdlist);
16206
16207   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16208 Set debugging of the dwarf2 DIE reader."), _("\
16209 Show debugging of the dwarf2 DIE reader."), _("\
16210 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16211 The value is the maximum depth to print."),
16212                             NULL,
16213                             NULL,
16214                             &setdebuglist, &showdebuglist);
16215
16216   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16217                _("\
16218 Save a gdb-index file.\n\
16219 Usage: save gdb-index DIRECTORY"),
16220                &save_cmdlist);
16221   set_cmd_completer (c, filename_completer);
16222 }