* i386-tdep.c (i386_match_insn_block): Use length of the proper
[platform/upstream/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 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 /* Default 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 static const struct dwarf2_debug_sections dwarf2_elf_names = {
252   { ".debug_info", ".zdebug_info" },
253   { ".debug_abbrev", ".zdebug_abbrev" },
254   { ".debug_line", ".zdebug_line" },
255   { ".debug_loc", ".zdebug_loc" },
256   { ".debug_macinfo", ".zdebug_macinfo" },
257   { ".debug_str", ".zdebug_str" },
258   { ".debug_ranges", ".zdebug_ranges" },
259   { ".debug_types", ".zdebug_types" },
260   { ".debug_frame", ".zdebug_frame" },
261   { ".eh_frame", NULL },
262   { ".gdb_index", ".zgdb_index" }
263 };
264
265 /* local data types */
266
267 /* We hold several abbreviation tables in memory at the same time.  */
268 #ifndef ABBREV_HASH_SIZE
269 #define ABBREV_HASH_SIZE 121
270 #endif
271
272 /* The data in a compilation unit header, after target2host
273    translation, looks like this.  */
274 struct comp_unit_head
275 {
276   unsigned int length;
277   short version;
278   unsigned char addr_size;
279   unsigned char signed_addr_p;
280   unsigned int abbrev_offset;
281
282   /* Size of file offsets; either 4 or 8.  */
283   unsigned int offset_size;
284
285   /* Size of the length field; either 4 or 12.  */
286   unsigned int initial_length_size;
287
288   /* Offset to the first byte of this compilation unit header in the
289      .debug_info section, for resolving relative reference dies.  */
290   unsigned int offset;
291
292   /* Offset to first die in this cu from the start of the cu.
293      This will be the first byte following the compilation unit header.  */
294   unsigned int first_die_offset;
295 };
296
297 /* Type used for delaying computation of method physnames.
298    See comments for compute_delayed_physnames.  */
299 struct delayed_method_info
300 {
301   /* The type to which the method is attached, i.e., its parent class.  */
302   struct type *type;
303
304   /* The index of the method in the type's function fieldlists.  */
305   int fnfield_index;
306
307   /* The index of the method in the fieldlist.  */
308   int index;
309
310   /* The name of the DIE.  */
311   const char *name;
312
313   /*  The DIE associated with this method.  */
314   struct die_info *die;
315 };
316
317 typedef struct delayed_method_info delayed_method_info;
318 DEF_VEC_O (delayed_method_info);
319
320 /* Internal state when decoding a particular compilation unit.  */
321 struct dwarf2_cu
322 {
323   /* The objfile containing this compilation unit.  */
324   struct objfile *objfile;
325
326   /* The header of the compilation unit.  */
327   struct comp_unit_head header;
328
329   /* Base address of this compilation unit.  */
330   CORE_ADDR base_address;
331
332   /* Non-zero if base_address has been set.  */
333   int base_known;
334
335   struct function_range *first_fn, *last_fn, *cached_fn;
336
337   /* The language we are debugging.  */
338   enum language language;
339   const struct language_defn *language_defn;
340
341   const char *producer;
342
343   /* The generic symbol table building routines have separate lists for
344      file scope symbols and all all other scopes (local scopes).  So
345      we need to select the right one to pass to add_symbol_to_list().
346      We do it by keeping a pointer to the correct list in list_in_scope.
347
348      FIXME: The original dwarf code just treated the file scope as the
349      first local scope, and all other local scopes as nested local
350      scopes, and worked fine.  Check to see if we really need to
351      distinguish these in buildsym.c.  */
352   struct pending **list_in_scope;
353
354   /* DWARF abbreviation table associated with this compilation unit.  */
355   struct abbrev_info **dwarf2_abbrevs;
356
357   /* Storage for the abbrev table.  */
358   struct obstack abbrev_obstack;
359
360   /* Hash table holding all the loaded partial DIEs.  */
361   htab_t partial_dies;
362
363   /* Storage for things with the same lifetime as this read-in compilation
364      unit, including partial DIEs.  */
365   struct obstack comp_unit_obstack;
366
367   /* When multiple dwarf2_cu structures are living in memory, this field
368      chains them all together, so that they can be released efficiently.
369      We will probably also want a generation counter so that most-recently-used
370      compilation units are cached...  */
371   struct dwarf2_per_cu_data *read_in_chain;
372
373   /* Backchain to our per_cu entry if the tree has been built.  */
374   struct dwarf2_per_cu_data *per_cu;
375
376   /* How many compilation units ago was this CU last referenced?  */
377   int last_used;
378
379   /* A hash table of die offsets for following references.  */
380   htab_t die_hash;
381
382   /* Full DIEs if read in.  */
383   struct die_info *dies;
384
385   /* A set of pointers to dwarf2_per_cu_data objects for compilation
386      units referenced by this one.  Only set during full symbol processing;
387      partial symbol tables do not have dependencies.  */
388   htab_t dependencies;
389
390   /* Header data from the line table, during full symbol processing.  */
391   struct line_header *line_header;
392
393   /* A list of methods which need to have physnames computed
394      after all type information has been read.  */
395   VEC (delayed_method_info) *method_list;
396
397   /* Mark used when releasing cached dies.  */
398   unsigned int mark : 1;
399
400   /* This flag will be set if this compilation unit might include
401      inter-compilation-unit references.  */
402   unsigned int has_form_ref_addr : 1;
403
404   /* This flag will be set if this compilation unit includes any
405      DW_TAG_namespace DIEs.  If we know that there are explicit
406      DIEs for namespaces, we don't need to try to infer them
407      from mangled names.  */
408   unsigned int has_namespace_info : 1;
409
410   /* This CU references .debug_loc.  See the symtab->locations_valid field.
411      This test is imperfect as there may exist optimized debug code not using
412      any location list and still facing inlining issues if handled as
413      unoptimized code.  For a future better test see GCC PR other/32998.  */
414
415   unsigned int has_loclist : 1;
416 };
417
418 /* Persistent data held for a compilation unit, even when not
419    processing it.  We put a pointer to this structure in the
420    read_symtab_private field of the psymtab.  If we encounter
421    inter-compilation-unit references, we also maintain a sorted
422    list of all compilation units.  */
423
424 struct dwarf2_per_cu_data
425 {
426   /* The start offset and length of this compilation unit.  2**29-1
427      bytes should suffice to store the length of any compilation unit
428      - if it doesn't, GDB will fall over anyway.
429      NOTE: Unlike comp_unit_head.length, this length includes
430      initial_length_size.  */
431   unsigned int offset;
432   unsigned int length : 29;
433
434   /* Flag indicating this compilation unit will be read in before
435      any of the current compilation units are processed.  */
436   unsigned int queued : 1;
437
438   /* This flag will be set if we need to load absolutely all DIEs
439      for this compilation unit, instead of just the ones we think
440      are interesting.  It gets set if we look for a DIE in the
441      hash table and don't find it.  */
442   unsigned int load_all_dies : 1;
443
444   /* Non-zero if this CU is from .debug_types.
445      Otherwise it's from .debug_info.  */
446   unsigned int from_debug_types : 1;
447
448   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
449      of the CU cache it gets reset to NULL again.  */
450   struct dwarf2_cu *cu;
451
452   /* The corresponding objfile.  */
453   struct objfile *objfile;
454
455   /* When using partial symbol tables, the 'psymtab' field is active.
456      Otherwise the 'quick' field is active.  */
457   union
458   {
459     /* The partial symbol table associated with this compilation unit,
460        or NULL for partial units (which do not have an associated
461        symtab).  */
462     struct partial_symtab *psymtab;
463
464     /* Data needed by the "quick" functions.  */
465     struct dwarf2_per_cu_quick_data *quick;
466   } v;
467 };
468
469 /* Entry in the signatured_types hash table.  */
470
471 struct signatured_type
472 {
473   ULONGEST signature;
474
475   /* Offset in .debug_types of the type defined by this TU.  */
476   unsigned int type_offset;
477
478   /* The CU(/TU) of this type.  */
479   struct dwarf2_per_cu_data per_cu;
480 };
481
482 /* Struct used to pass misc. parameters to read_die_and_children, et
483    al.  which are used for both .debug_info and .debug_types dies.
484    All parameters here are unchanging for the life of the call.  This
485    struct exists to abstract away the constant parameters of die
486    reading.  */
487
488 struct die_reader_specs
489 {
490   /* The bfd of this objfile.  */
491   bfd* abfd;
492
493   /* The CU of the DIE we are parsing.  */
494   struct dwarf2_cu *cu;
495
496   /* Pointer to start of section buffer.
497      This is either the start of .debug_info or .debug_types.  */
498   const gdb_byte *buffer;
499 };
500
501 /* The line number information for a compilation unit (found in the
502    .debug_line section) begins with a "statement program header",
503    which contains the following information.  */
504 struct line_header
505 {
506   unsigned int total_length;
507   unsigned short version;
508   unsigned int header_length;
509   unsigned char minimum_instruction_length;
510   unsigned char maximum_ops_per_instruction;
511   unsigned char default_is_stmt;
512   int line_base;
513   unsigned char line_range;
514   unsigned char opcode_base;
515
516   /* standard_opcode_lengths[i] is the number of operands for the
517      standard opcode whose value is i.  This means that
518      standard_opcode_lengths[0] is unused, and the last meaningful
519      element is standard_opcode_lengths[opcode_base - 1].  */
520   unsigned char *standard_opcode_lengths;
521
522   /* The include_directories table.  NOTE!  These strings are not
523      allocated with xmalloc; instead, they are pointers into
524      debug_line_buffer.  If you try to free them, `free' will get
525      indigestion.  */
526   unsigned int num_include_dirs, include_dirs_size;
527   char **include_dirs;
528
529   /* The file_names table.  NOTE!  These strings are not allocated
530      with xmalloc; instead, they are pointers into debug_line_buffer.
531      Don't try to free them directly.  */
532   unsigned int num_file_names, file_names_size;
533   struct file_entry
534   {
535     char *name;
536     unsigned int dir_index;
537     unsigned int mod_time;
538     unsigned int length;
539     int included_p; /* Non-zero if referenced by the Line Number Program.  */
540     struct symtab *symtab; /* The associated symbol table, if any.  */
541   } *file_names;
542
543   /* The start and end of the statement program following this
544      header.  These point into dwarf2_per_objfile->line_buffer.  */
545   gdb_byte *statement_program_start, *statement_program_end;
546 };
547
548 /* When we construct a partial symbol table entry we only
549    need this much information.  */
550 struct partial_die_info
551   {
552     /* Offset of this DIE.  */
553     unsigned int offset;
554
555     /* DWARF-2 tag for this DIE.  */
556     ENUM_BITFIELD(dwarf_tag) tag : 16;
557
558     /* Assorted flags describing the data found in this DIE.  */
559     unsigned int has_children : 1;
560     unsigned int is_external : 1;
561     unsigned int is_declaration : 1;
562     unsigned int has_type : 1;
563     unsigned int has_specification : 1;
564     unsigned int has_pc_info : 1;
565
566     /* Flag set if the SCOPE field of this structure has been
567        computed.  */
568     unsigned int scope_set : 1;
569
570     /* Flag set if the DIE has a byte_size attribute.  */
571     unsigned int has_byte_size : 1;
572
573     /* Flag set if any of the DIE's children are template arguments.  */
574     unsigned int has_template_arguments : 1;
575
576     /* Flag set if fixup_partial_die has been called on this die.  */
577     unsigned int fixup_called : 1;
578
579     /* The name of this DIE.  Normally the value of DW_AT_name, but
580        sometimes a default name for unnamed DIEs.  */
581     char *name;
582
583     /* The linkage name, if present.  */
584     const char *linkage_name;
585
586     /* The scope to prepend to our children.  This is generally
587        allocated on the comp_unit_obstack, so will disappear
588        when this compilation unit leaves the cache.  */
589     char *scope;
590
591     /* The location description associated with this DIE, if any.  */
592     struct dwarf_block *locdesc;
593
594     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
595     CORE_ADDR lowpc;
596     CORE_ADDR highpc;
597
598     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
599        DW_AT_sibling, if any.  */
600     /* NOTE: This member isn't strictly necessary, read_partial_die could
601        return DW_AT_sibling values to its caller load_partial_dies.  */
602     gdb_byte *sibling;
603
604     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
605        DW_AT_specification (or DW_AT_abstract_origin or
606        DW_AT_extension).  */
607     unsigned int spec_offset;
608
609     /* Pointers to this DIE's parent, first child, and next sibling,
610        if any.  */
611     struct partial_die_info *die_parent, *die_child, *die_sibling;
612   };
613
614 /* This data structure holds the information of an abbrev.  */
615 struct abbrev_info
616   {
617     unsigned int number;        /* number identifying abbrev */
618     enum dwarf_tag tag;         /* dwarf tag */
619     unsigned short has_children;                /* boolean */
620     unsigned short num_attrs;   /* number of attributes */
621     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
622     struct abbrev_info *next;   /* next in chain */
623   };
624
625 struct attr_abbrev
626   {
627     ENUM_BITFIELD(dwarf_attribute) name : 16;
628     ENUM_BITFIELD(dwarf_form) form : 16;
629   };
630
631 /* Attributes have a name and a value.  */
632 struct attribute
633   {
634     ENUM_BITFIELD(dwarf_attribute) name : 16;
635     ENUM_BITFIELD(dwarf_form) form : 15;
636
637     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
638        field should be in u.str (existing only for DW_STRING) but it is kept
639        here for better struct attribute alignment.  */
640     unsigned int string_is_canonical : 1;
641
642     union
643       {
644         char *str;
645         struct dwarf_block *blk;
646         ULONGEST unsnd;
647         LONGEST snd;
648         CORE_ADDR addr;
649         struct signatured_type *signatured_type;
650       }
651     u;
652   };
653
654 /* This data structure holds a complete die structure.  */
655 struct die_info
656   {
657     /* DWARF-2 tag for this DIE.  */
658     ENUM_BITFIELD(dwarf_tag) tag : 16;
659
660     /* Number of attributes */
661     unsigned char num_attrs;
662
663     /* True if we're presently building the full type name for the
664        type derived from this DIE.  */
665     unsigned char building_fullname : 1;
666
667     /* Abbrev number */
668     unsigned int abbrev;
669
670     /* Offset in .debug_info or .debug_types section.  */
671     unsigned int offset;
672
673     /* The dies in a compilation unit form an n-ary tree.  PARENT
674        points to this die's parent; CHILD points to the first child of
675        this node; and all the children of a given node are chained
676        together via their SIBLING fields.  */
677     struct die_info *child;     /* Its first child, if any.  */
678     struct die_info *sibling;   /* Its next sibling, if any.  */
679     struct die_info *parent;    /* Its parent, if any.  */
680
681     /* An array of attributes, with NUM_ATTRS elements.  There may be
682        zero, but it's not common and zero-sized arrays are not
683        sufficiently portable C.  */
684     struct attribute attrs[1];
685   };
686
687 struct function_range
688 {
689   const char *name;
690   CORE_ADDR lowpc, highpc;
691   int seen_line;
692   struct function_range *next;
693 };
694
695 /* Get at parts of an attribute structure.  */
696
697 #define DW_STRING(attr)    ((attr)->u.str)
698 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
699 #define DW_UNSND(attr)     ((attr)->u.unsnd)
700 #define DW_BLOCK(attr)     ((attr)->u.blk)
701 #define DW_SND(attr)       ((attr)->u.snd)
702 #define DW_ADDR(attr)      ((attr)->u.addr)
703 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
704
705 /* Blocks are a bunch of untyped bytes.  */
706 struct dwarf_block
707   {
708     unsigned int size;
709     gdb_byte *data;
710   };
711
712 #ifndef ATTR_ALLOC_CHUNK
713 #define ATTR_ALLOC_CHUNK 4
714 #endif
715
716 /* Allocate fields for structs, unions and enums in this size.  */
717 #ifndef DW_FIELD_ALLOC_CHUNK
718 #define DW_FIELD_ALLOC_CHUNK 4
719 #endif
720
721 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
722    but this would require a corresponding change in unpack_field_as_long
723    and friends.  */
724 static int bits_per_byte = 8;
725
726 /* The routines that read and process dies for a C struct or C++ class
727    pass lists of data member fields and lists of member function fields
728    in an instance of a field_info structure, as defined below.  */
729 struct field_info
730   {
731     /* List of data member and baseclasses fields.  */
732     struct nextfield
733       {
734         struct nextfield *next;
735         int accessibility;
736         int virtuality;
737         struct field field;
738       }
739      *fields, *baseclasses;
740
741     /* Number of fields (including baseclasses).  */
742     int nfields;
743
744     /* Number of baseclasses.  */
745     int nbaseclasses;
746
747     /* Set if the accesibility of one of the fields is not public.  */
748     int non_public_fields;
749
750     /* Member function fields array, entries are allocated in the order they
751        are encountered in the object file.  */
752     struct nextfnfield
753       {
754         struct nextfnfield *next;
755         struct fn_field fnfield;
756       }
757      *fnfields;
758
759     /* Member function fieldlist array, contains name of possibly overloaded
760        member function, number of overloaded member functions and a pointer
761        to the head of the member function field chain.  */
762     struct fnfieldlist
763       {
764         char *name;
765         int length;
766         struct nextfnfield *head;
767       }
768      *fnfieldlists;
769
770     /* Number of entries in the fnfieldlists array.  */
771     int nfnfields;
772
773     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
774        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
775     struct typedef_field_list
776       {
777         struct typedef_field field;
778         struct typedef_field_list *next;
779       }
780     *typedef_field_list;
781     unsigned typedef_field_list_count;
782   };
783
784 /* One item on the queue of compilation units to read in full symbols
785    for.  */
786 struct dwarf2_queue_item
787 {
788   struct dwarf2_per_cu_data *per_cu;
789   struct dwarf2_queue_item *next;
790 };
791
792 /* The current queue.  */
793 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
794
795 /* Loaded secondary compilation units are kept in memory until they
796    have not been referenced for the processing of this many
797    compilation units.  Set this to zero to disable caching.  Cache
798    sizes of up to at least twenty will improve startup time for
799    typical inter-CU-reference binaries, at an obvious memory cost.  */
800 static int dwarf2_max_cache_age = 5;
801 static void
802 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
803                            struct cmd_list_element *c, const char *value)
804 {
805   fprintf_filtered (file, _("The upper bound on the age of cached "
806                             "dwarf2 compilation units is %s.\n"),
807                     value);
808 }
809
810
811 /* Various complaints about symbol reading that don't abort the process.  */
812
813 static void
814 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
815 {
816   complaint (&symfile_complaints,
817              _("statement list doesn't fit in .debug_line section"));
818 }
819
820 static void
821 dwarf2_debug_line_missing_file_complaint (void)
822 {
823   complaint (&symfile_complaints,
824              _(".debug_line section has line data without a file"));
825 }
826
827 static void
828 dwarf2_debug_line_missing_end_sequence_complaint (void)
829 {
830   complaint (&symfile_complaints,
831              _(".debug_line section has line "
832                "program sequence without an end"));
833 }
834
835 static void
836 dwarf2_complex_location_expr_complaint (void)
837 {
838   complaint (&symfile_complaints, _("location expression too complex"));
839 }
840
841 static void
842 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
843                                               int arg3)
844 {
845   complaint (&symfile_complaints,
846              _("const value length mismatch for '%s', got %d, expected %d"),
847              arg1, arg2, arg3);
848 }
849
850 static void
851 dwarf2_macros_too_long_complaint (void)
852 {
853   complaint (&symfile_complaints,
854              _("macro info runs off end of `.debug_macinfo' section"));
855 }
856
857 static void
858 dwarf2_macro_malformed_definition_complaint (const char *arg1)
859 {
860   complaint (&symfile_complaints,
861              _("macro debug info contains a "
862                "malformed macro definition:\n`%s'"),
863              arg1);
864 }
865
866 static void
867 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
868 {
869   complaint (&symfile_complaints,
870              _("invalid attribute class or form for '%s' in '%s'"),
871              arg1, arg2);
872 }
873
874 /* local function prototypes */
875
876 static void dwarf2_locate_sections (bfd *, asection *, void *);
877
878 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
879                                            struct objfile *);
880
881 static void dwarf2_build_psymtabs_hard (struct objfile *);
882
883 static void scan_partial_symbols (struct partial_die_info *,
884                                   CORE_ADDR *, CORE_ADDR *,
885                                   int, struct dwarf2_cu *);
886
887 static void add_partial_symbol (struct partial_die_info *,
888                                 struct dwarf2_cu *);
889
890 static void add_partial_namespace (struct partial_die_info *pdi,
891                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
892                                    int need_pc, struct dwarf2_cu *cu);
893
894 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
895                                 CORE_ADDR *highpc, int need_pc,
896                                 struct dwarf2_cu *cu);
897
898 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
899                                      struct dwarf2_cu *cu);
900
901 static void add_partial_subprogram (struct partial_die_info *pdi,
902                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
903                                     int need_pc, struct dwarf2_cu *cu);
904
905 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
906                                      gdb_byte *buffer, gdb_byte *info_ptr,
907                                      bfd *abfd, struct dwarf2_cu *cu);
908
909 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
910
911 static void psymtab_to_symtab_1 (struct partial_symtab *);
912
913 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
914
915 static void dwarf2_free_abbrev_table (void *);
916
917 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
918                                             struct dwarf2_cu *);
919
920 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
921                                                  struct dwarf2_cu *);
922
923 static struct partial_die_info *load_partial_dies (bfd *,
924                                                    gdb_byte *, gdb_byte *,
925                                                    int, struct dwarf2_cu *);
926
927 static gdb_byte *read_partial_die (struct partial_die_info *,
928                                    struct abbrev_info *abbrev,
929                                    unsigned int, bfd *,
930                                    gdb_byte *, gdb_byte *,
931                                    struct dwarf2_cu *);
932
933 static struct partial_die_info *find_partial_die (unsigned int,
934                                                   struct dwarf2_cu *);
935
936 static void fixup_partial_die (struct partial_die_info *,
937                                struct dwarf2_cu *);
938
939 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
940                                  bfd *, gdb_byte *, struct dwarf2_cu *);
941
942 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
943                                        bfd *, gdb_byte *, struct dwarf2_cu *);
944
945 static unsigned int read_1_byte (bfd *, gdb_byte *);
946
947 static int read_1_signed_byte (bfd *, gdb_byte *);
948
949 static unsigned int read_2_bytes (bfd *, gdb_byte *);
950
951 static unsigned int read_4_bytes (bfd *, gdb_byte *);
952
953 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
954
955 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
956                                unsigned int *);
957
958 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
959
960 static LONGEST read_checked_initial_length_and_offset
961   (bfd *, gdb_byte *, const struct comp_unit_head *,
962    unsigned int *, unsigned int *);
963
964 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
965                             unsigned int *);
966
967 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
968
969 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
970
971 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
972
973 static char *read_indirect_string (bfd *, gdb_byte *,
974                                    const struct comp_unit_head *,
975                                    unsigned int *);
976
977 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
978
979 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
980
981 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
982
983 static void set_cu_language (unsigned int, struct dwarf2_cu *);
984
985 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
986                                       struct dwarf2_cu *);
987
988 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
989                                                 unsigned int,
990                                                 struct dwarf2_cu *);
991
992 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
993                                struct dwarf2_cu *cu);
994
995 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
996
997 static struct die_info *die_specification (struct die_info *die,
998                                            struct dwarf2_cu **);
999
1000 static void free_line_header (struct line_header *lh);
1001
1002 static void add_file_name (struct line_header *, char *, unsigned int,
1003                            unsigned int, unsigned int);
1004
1005 static struct line_header *(dwarf_decode_line_header
1006                             (unsigned int offset,
1007                              bfd *abfd, struct dwarf2_cu *cu));
1008
1009 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1010                                 struct dwarf2_cu *, struct partial_symtab *);
1011
1012 static void dwarf2_start_subfile (char *, const char *, const char *);
1013
1014 static struct symbol *new_symbol (struct die_info *, struct type *,
1015                                   struct dwarf2_cu *);
1016
1017 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1018                                        struct dwarf2_cu *, struct symbol *);
1019
1020 static void dwarf2_const_value (struct attribute *, struct symbol *,
1021                                 struct dwarf2_cu *);
1022
1023 static void dwarf2_const_value_attr (struct attribute *attr,
1024                                      struct type *type,
1025                                      const char *name,
1026                                      struct obstack *obstack,
1027                                      struct dwarf2_cu *cu, long *value,
1028                                      gdb_byte **bytes,
1029                                      struct dwarf2_locexpr_baton **baton);
1030
1031 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1032
1033 static int need_gnat_info (struct dwarf2_cu *);
1034
1035 static struct type *die_descriptive_type (struct die_info *,
1036                                           struct dwarf2_cu *);
1037
1038 static void set_descriptive_type (struct type *, struct die_info *,
1039                                   struct dwarf2_cu *);
1040
1041 static struct type *die_containing_type (struct die_info *,
1042                                          struct dwarf2_cu *);
1043
1044 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045                                      struct dwarf2_cu *);
1046
1047 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1048
1049 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
1051 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1052
1053 static char *typename_concat (struct obstack *obs, const char *prefix,
1054                               const char *suffix, int physname,
1055                               struct dwarf2_cu *cu);
1056
1057 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1062
1063 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1064
1065 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066                                struct dwarf2_cu *, struct partial_symtab *);
1067
1068 static int dwarf2_get_pc_bounds (struct die_info *,
1069                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070                                  struct partial_symtab *);
1071
1072 static void get_scope_pc_bounds (struct die_info *,
1073                                  CORE_ADDR *, CORE_ADDR *,
1074                                  struct dwarf2_cu *);
1075
1076 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077                                         CORE_ADDR, struct dwarf2_cu *);
1078
1079 static void dwarf2_add_field (struct field_info *, struct die_info *,
1080                               struct dwarf2_cu *);
1081
1082 static void dwarf2_attach_fields_to_type (struct field_info *,
1083                                           struct type *, struct dwarf2_cu *);
1084
1085 static void dwarf2_add_member_fn (struct field_info *,
1086                                   struct die_info *, struct type *,
1087                                   struct dwarf2_cu *);
1088
1089 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1090                                              struct type *,
1091                                              struct dwarf2_cu *);
1092
1093 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1094
1095 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1096
1097 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1098
1099 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1100
1101 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1102
1103 static struct type *read_module_type (struct die_info *die,
1104                                       struct dwarf2_cu *cu);
1105
1106 static const char *namespace_name (struct die_info *die,
1107                                    int *is_anonymous, struct dwarf2_cu *);
1108
1109 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1110
1111 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1112
1113 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1114                                                        struct dwarf2_cu *);
1115
1116 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1117
1118 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1119                                                  gdb_byte *info_ptr,
1120                                                  gdb_byte **new_info_ptr,
1121                                                  struct die_info *parent);
1122
1123 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1124                                                gdb_byte *info_ptr,
1125                                                gdb_byte **new_info_ptr,
1126                                                struct die_info *parent);
1127
1128 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1129                                                gdb_byte *info_ptr,
1130                                                gdb_byte **new_info_ptr,
1131                                                struct die_info *parent);
1132
1133 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1134                                 struct die_info **, gdb_byte *,
1135                                 int *);
1136
1137 static void process_die (struct die_info *, struct dwarf2_cu *);
1138
1139 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1140                                        struct obstack *);
1141
1142 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1143
1144 static const char *dwarf2_full_name (char *name,
1145                                      struct die_info *die,
1146                                      struct dwarf2_cu *cu);
1147
1148 static struct die_info *dwarf2_extension (struct die_info *die,
1149                                           struct dwarf2_cu **);
1150
1151 static char *dwarf_tag_name (unsigned int);
1152
1153 static char *dwarf_attr_name (unsigned int);
1154
1155 static char *dwarf_form_name (unsigned int);
1156
1157 static char *dwarf_bool_name (unsigned int);
1158
1159 static char *dwarf_type_encoding_name (unsigned int);
1160
1161 #if 0
1162 static char *dwarf_cfi_name (unsigned int);
1163 #endif
1164
1165 static struct die_info *sibling_die (struct die_info *);
1166
1167 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1168
1169 static void dump_die_for_error (struct die_info *);
1170
1171 static void dump_die_1 (struct ui_file *, int level, int max_level,
1172                         struct die_info *);
1173
1174 /*static*/ void dump_die (struct die_info *, int max_level);
1175
1176 static void store_in_ref_table (struct die_info *,
1177                                 struct dwarf2_cu *);
1178
1179 static int is_ref_attr (struct attribute *);
1180
1181 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1182
1183 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1184
1185 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1186                                                struct attribute *,
1187                                                struct dwarf2_cu **);
1188
1189 static struct die_info *follow_die_ref (struct die_info *,
1190                                         struct attribute *,
1191                                         struct dwarf2_cu **);
1192
1193 static struct die_info *follow_die_sig (struct die_info *,
1194                                         struct attribute *,
1195                                         struct dwarf2_cu **);
1196
1197 static void read_signatured_type_at_offset (struct objfile *objfile,
1198                                             unsigned int offset);
1199
1200 static void read_signatured_type (struct objfile *,
1201                                   struct signatured_type *type_sig);
1202
1203 /* memory allocation interface */
1204
1205 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1206
1207 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1208
1209 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1210
1211 static void initialize_cu_func_list (struct dwarf2_cu *);
1212
1213 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1214                                  struct dwarf2_cu *);
1215
1216 static void dwarf_decode_macros (struct line_header *, unsigned int,
1217                                  char *, bfd *, struct dwarf2_cu *);
1218
1219 static int attr_form_is_block (struct attribute *);
1220
1221 static int attr_form_is_section_offset (struct attribute *);
1222
1223 static int attr_form_is_constant (struct attribute *);
1224
1225 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1226                                    struct dwarf2_loclist_baton *baton,
1227                                    struct attribute *attr);
1228
1229 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1230                                          struct symbol *sym,
1231                                          struct dwarf2_cu *cu);
1232
1233 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1234                                struct abbrev_info *abbrev,
1235                                struct dwarf2_cu *cu);
1236
1237 static void free_stack_comp_unit (void *);
1238
1239 static hashval_t partial_die_hash (const void *item);
1240
1241 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1242
1243 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1244   (unsigned int offset, struct objfile *objfile);
1245
1246 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1247   (unsigned int offset, struct objfile *objfile);
1248
1249 static void init_one_comp_unit (struct dwarf2_cu *cu,
1250                                 struct objfile *objfile);
1251
1252 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1253                                    struct die_info *comp_unit_die);
1254
1255 static void free_one_comp_unit (void *);
1256
1257 static void free_cached_comp_units (void *);
1258
1259 static void age_cached_comp_units (void);
1260
1261 static void free_one_cached_comp_unit (void *);
1262
1263 static struct type *set_die_type (struct die_info *, struct type *,
1264                                   struct dwarf2_cu *);
1265
1266 static void create_all_comp_units (struct objfile *);
1267
1268 static int create_debug_types_hash_table (struct objfile *objfile);
1269
1270 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1271                                  struct objfile *);
1272
1273 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275 static void dwarf2_add_dependence (struct dwarf2_cu *,
1276                                    struct dwarf2_per_cu_data *);
1277
1278 static void dwarf2_mark (struct dwarf2_cu *);
1279
1280 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
1282 static struct type *get_die_type_at_offset (unsigned int,
1283                                             struct dwarf2_per_cu_data *per_cu);
1284
1285 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static void dwarf2_release_queue (void *dummy);
1288
1289 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1290                              struct objfile *objfile);
1291
1292 static void process_queue (struct objfile *objfile);
1293
1294 static void find_file_and_directory (struct die_info *die,
1295                                      struct dwarf2_cu *cu,
1296                                      char **name, char **comp_dir);
1297
1298 static char *file_full_name (int file, struct line_header *lh,
1299                              const char *comp_dir);
1300
1301 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1302                                               gdb_byte *info_ptr,
1303                                               gdb_byte *buffer,
1304                                               unsigned int buffer_size,
1305                                               bfd *abfd);
1306
1307 static void init_cu_die_reader (struct die_reader_specs *reader,
1308                                 struct dwarf2_cu *cu);
1309
1310 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1311
1312 #if WORDS_BIGENDIAN
1313
1314 /* Convert VALUE between big- and little-endian.  */
1315 static offset_type
1316 byte_swap (offset_type value)
1317 {
1318   offset_type result;
1319
1320   result = (value & 0xff) << 24;
1321   result |= (value & 0xff00) << 8;
1322   result |= (value & 0xff0000) >> 8;
1323   result |= (value & 0xff000000) >> 24;
1324   return result;
1325 }
1326
1327 #define MAYBE_SWAP(V)  byte_swap (V)
1328
1329 #else
1330 #define MAYBE_SWAP(V) (V)
1331 #endif /* WORDS_BIGENDIAN */
1332
1333 /* The suffix for an index file.  */
1334 #define INDEX_SUFFIX ".gdb-index"
1335
1336 static const char *dwarf2_physname (char *name, struct die_info *die,
1337                                     struct dwarf2_cu *cu);
1338
1339 /* Try to locate the sections we need for DWARF 2 debugging
1340    information and return true if we have enough to do something.
1341    NAMES points to the dwarf2 section names, or is NULL if the standard
1342    ELF names are used.  */
1343
1344 int
1345 dwarf2_has_info (struct objfile *objfile,
1346                  const struct dwarf2_debug_sections *names)
1347 {
1348   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1349   if (!dwarf2_per_objfile)
1350     {
1351       /* Initialize per-objfile state.  */
1352       struct dwarf2_per_objfile *data
1353         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1354
1355       memset (data, 0, sizeof (*data));
1356       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1357       dwarf2_per_objfile = data;
1358
1359       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1360                              (void *) names);
1361       dwarf2_per_objfile->objfile = objfile;
1362     }
1363   return (dwarf2_per_objfile->info.asection != NULL
1364           && dwarf2_per_objfile->abbrev.asection != NULL);
1365 }
1366
1367 /* When loading sections, we look either for uncompressed section or for
1368    compressed section names.  */
1369
1370 static int
1371 section_is_p (const char *section_name,
1372               const struct dwarf2_section_names *names)
1373 {
1374   if (names->normal != NULL
1375       && strcmp (section_name, names->normal) == 0)
1376     return 1;
1377   if (names->compressed != NULL
1378       && strcmp (section_name, names->compressed) == 0)
1379     return 1;
1380   return 0;
1381 }
1382
1383 /* This function is mapped across the sections and remembers the
1384    offset and size of each of the debugging sections we are interested
1385    in.  */
1386
1387 static void
1388 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1389 {
1390   const struct dwarf2_debug_sections *names;
1391
1392   if (vnames == NULL)
1393     names = &dwarf2_elf_names;
1394   else
1395     names = (const struct dwarf2_debug_sections *) vnames;
1396
1397   if (section_is_p (sectp->name, &names->info))
1398     {
1399       dwarf2_per_objfile->info.asection = sectp;
1400       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1401     }
1402   else if (section_is_p (sectp->name, &names->abbrev))
1403     {
1404       dwarf2_per_objfile->abbrev.asection = sectp;
1405       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1406     }
1407   else if (section_is_p (sectp->name, &names->line))
1408     {
1409       dwarf2_per_objfile->line.asection = sectp;
1410       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1411     }
1412   else if (section_is_p (sectp->name, &names->loc))
1413     {
1414       dwarf2_per_objfile->loc.asection = sectp;
1415       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1416     }
1417   else if (section_is_p (sectp->name, &names->macinfo))
1418     {
1419       dwarf2_per_objfile->macinfo.asection = sectp;
1420       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1421     }
1422   else if (section_is_p (sectp->name, &names->str))
1423     {
1424       dwarf2_per_objfile->str.asection = sectp;
1425       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1426     }
1427   else if (section_is_p (sectp->name, &names->frame))
1428     {
1429       dwarf2_per_objfile->frame.asection = sectp;
1430       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1431     }
1432   else if (section_is_p (sectp->name, &names->eh_frame))
1433     {
1434       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1435
1436       if (aflag & SEC_HAS_CONTENTS)
1437         {
1438           dwarf2_per_objfile->eh_frame.asection = sectp;
1439           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1440         }
1441     }
1442   else if (section_is_p (sectp->name, &names->ranges))
1443     {
1444       dwarf2_per_objfile->ranges.asection = sectp;
1445       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1446     }
1447   else if (section_is_p (sectp->name, &names->types))
1448     {
1449       dwarf2_per_objfile->types.asection = sectp;
1450       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1451     }
1452   else if (section_is_p (sectp->name, &names->gdb_index))
1453     {
1454       dwarf2_per_objfile->gdb_index.asection = sectp;
1455       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1456     }
1457
1458   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1459       && bfd_section_vma (abfd, sectp) == 0)
1460     dwarf2_per_objfile->has_section_at_zero = 1;
1461 }
1462
1463 /* Decompress a section that was compressed using zlib.  Store the
1464    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1465
1466 static void
1467 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1468                          gdb_byte **outbuf, bfd_size_type *outsize)
1469 {
1470   bfd *abfd = objfile->obfd;
1471 #ifndef HAVE_ZLIB_H
1472   error (_("Support for zlib-compressed DWARF data (from '%s') "
1473            "is disabled in this copy of GDB"),
1474          bfd_get_filename (abfd));
1475 #else
1476   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1477   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1478   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1479   bfd_size_type uncompressed_size;
1480   gdb_byte *uncompressed_buffer;
1481   z_stream strm;
1482   int rc;
1483   int header_size = 12;
1484
1485   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1486       || bfd_bread (compressed_buffer,
1487                     compressed_size, abfd) != compressed_size)
1488     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1489            bfd_get_filename (abfd));
1490
1491   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1492      by the uncompressed section size, 8 bytes in big-endian order.  */
1493   if (compressed_size < header_size
1494       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1495     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1496            bfd_get_filename (abfd));
1497   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1498   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1499   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1500   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1501   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1502   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1503   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1504   uncompressed_size += compressed_buffer[11];
1505
1506   /* It is possible the section consists of several compressed
1507      buffers concatenated together, so we uncompress in a loop.  */
1508   strm.zalloc = NULL;
1509   strm.zfree = NULL;
1510   strm.opaque = NULL;
1511   strm.avail_in = compressed_size - header_size;
1512   strm.next_in = (Bytef*) compressed_buffer + header_size;
1513   strm.avail_out = uncompressed_size;
1514   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1515                                        uncompressed_size);
1516   rc = inflateInit (&strm);
1517   while (strm.avail_in > 0)
1518     {
1519       if (rc != Z_OK)
1520         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1521                bfd_get_filename (abfd), rc);
1522       strm.next_out = ((Bytef*) uncompressed_buffer
1523                        + (uncompressed_size - strm.avail_out));
1524       rc = inflate (&strm, Z_FINISH);
1525       if (rc != Z_STREAM_END)
1526         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1527                bfd_get_filename (abfd), rc);
1528       rc = inflateReset (&strm);
1529     }
1530   rc = inflateEnd (&strm);
1531   if (rc != Z_OK
1532       || strm.avail_out != 0)
1533     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1534            bfd_get_filename (abfd), rc);
1535
1536   do_cleanups (cleanup);
1537   *outbuf = uncompressed_buffer;
1538   *outsize = uncompressed_size;
1539 #endif
1540 }
1541
1542 /* A helper function that decides whether a section is empty.  */
1543
1544 static int
1545 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1546 {
1547   return info->asection == NULL || info->size == 0;
1548 }
1549
1550 /* Read the contents of the section SECTP from object file specified by
1551    OBJFILE, store info about the section into INFO.
1552    If the section is compressed, uncompress it before returning.  */
1553
1554 static void
1555 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1556 {
1557   bfd *abfd = objfile->obfd;
1558   asection *sectp = info->asection;
1559   gdb_byte *buf, *retbuf;
1560   unsigned char header[4];
1561
1562   if (info->readin)
1563     return;
1564   info->buffer = NULL;
1565   info->was_mmapped = 0;
1566   info->readin = 1;
1567
1568   if (dwarf2_section_empty_p (info))
1569     return;
1570
1571   /* Check if the file has a 4-byte header indicating compression.  */
1572   if (info->size > sizeof (header)
1573       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1574       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1575     {
1576       /* Upon decompression, update the buffer and its size.  */
1577       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1578         {
1579           zlib_decompress_section (objfile, sectp, &info->buffer,
1580                                    &info->size);
1581           return;
1582         }
1583     }
1584
1585 #ifdef HAVE_MMAP
1586   if (pagesize == 0)
1587     pagesize = getpagesize ();
1588
1589   /* Only try to mmap sections which are large enough: we don't want to
1590      waste space due to fragmentation.  Also, only try mmap for sections
1591      without relocations.  */
1592
1593   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1594     {
1595       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1596       size_t map_length = info->size + sectp->filepos - pg_offset;
1597       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1598                                  MAP_PRIVATE, pg_offset);
1599
1600       if (retbuf != MAP_FAILED)
1601         {
1602           info->was_mmapped = 1;
1603           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1604 #if HAVE_POSIX_MADVISE
1605           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1606 #endif
1607           return;
1608         }
1609     }
1610 #endif
1611
1612   /* If we get here, we are a normal, not-compressed section.  */
1613   info->buffer = buf
1614     = obstack_alloc (&objfile->objfile_obstack, info->size);
1615
1616   /* When debugging .o files, we may need to apply relocations; see
1617      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1618      We never compress sections in .o files, so we only need to
1619      try this when the section is not compressed.  */
1620   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1621   if (retbuf != NULL)
1622     {
1623       info->buffer = retbuf;
1624       return;
1625     }
1626
1627   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1628       || bfd_bread (buf, info->size, abfd) != info->size)
1629     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1630            bfd_get_filename (abfd));
1631 }
1632
1633 /* A helper function that returns the size of a section in a safe way.
1634    If you are positive that the section has been read before using the
1635    size, then it is safe to refer to the dwarf2_section_info object's
1636    "size" field directly.  In other cases, you must call this
1637    function, because for compressed sections the size field is not set
1638    correctly until the section has been read.  */
1639
1640 static bfd_size_type
1641 dwarf2_section_size (struct objfile *objfile,
1642                      struct dwarf2_section_info *info)
1643 {
1644   if (!info->readin)
1645     dwarf2_read_section (objfile, info);
1646   return info->size;
1647 }
1648
1649 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1650    SECTION_NAME.  */
1651
1652 void
1653 dwarf2_get_section_info (struct objfile *objfile,
1654                          enum dwarf2_section_enum sect,
1655                          asection **sectp, gdb_byte **bufp,
1656                          bfd_size_type *sizep)
1657 {
1658   struct dwarf2_per_objfile *data
1659     = objfile_data (objfile, dwarf2_objfile_data_key);
1660   struct dwarf2_section_info *info;
1661
1662   /* We may see an objfile without any DWARF, in which case we just
1663      return nothing.  */
1664   if (data == NULL)
1665     {
1666       *sectp = NULL;
1667       *bufp = NULL;
1668       *sizep = 0;
1669       return;
1670     }
1671   switch (sect)
1672     {
1673     case DWARF2_DEBUG_FRAME:
1674       info = &data->frame;
1675       break;
1676     case DWARF2_EH_FRAME:
1677       info = &data->eh_frame;
1678       break;
1679     default:
1680       gdb_assert_not_reached ("unexpected section");
1681     }
1682
1683   dwarf2_read_section (objfile, info);
1684
1685   *sectp = info->asection;
1686   *bufp = info->buffer;
1687   *sizep = info->size;
1688 }
1689
1690 \f
1691 /* DWARF quick_symbols_functions support.  */
1692
1693 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1694    unique line tables, so we maintain a separate table of all .debug_line
1695    derived entries to support the sharing.
1696    All the quick functions need is the list of file names.  We discard the
1697    line_header when we're done and don't need to record it here.  */
1698 struct quick_file_names
1699 {
1700   /* The offset in .debug_line of the line table.  We hash on this.  */
1701   unsigned int offset;
1702
1703   /* The number of entries in file_names, real_names.  */
1704   unsigned int num_file_names;
1705
1706   /* The file names from the line table, after being run through
1707      file_full_name.  */
1708   const char **file_names;
1709
1710   /* The file names from the line table after being run through
1711      gdb_realpath.  These are computed lazily.  */
1712   const char **real_names;
1713 };
1714
1715 /* When using the index (and thus not using psymtabs), each CU has an
1716    object of this type.  This is used to hold information needed by
1717    the various "quick" methods.  */
1718 struct dwarf2_per_cu_quick_data
1719 {
1720   /* The file table.  This can be NULL if there was no file table
1721      or it's currently not read in.
1722      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1723   struct quick_file_names *file_names;
1724
1725   /* The corresponding symbol table.  This is NULL if symbols for this
1726      CU have not yet been read.  */
1727   struct symtab *symtab;
1728
1729   /* A temporary mark bit used when iterating over all CUs in
1730      expand_symtabs_matching.  */
1731   unsigned int mark : 1;
1732
1733   /* True if we've tried to read the file table and found there isn't one.
1734      There will be no point in trying to read it again next time.  */
1735   unsigned int no_file_data : 1;
1736 };
1737
1738 /* Hash function for a quick_file_names.  */
1739
1740 static hashval_t
1741 hash_file_name_entry (const void *e)
1742 {
1743   const struct quick_file_names *file_data = e;
1744
1745   return file_data->offset;
1746 }
1747
1748 /* Equality function for a quick_file_names.  */
1749
1750 static int
1751 eq_file_name_entry (const void *a, const void *b)
1752 {
1753   const struct quick_file_names *ea = a;
1754   const struct quick_file_names *eb = b;
1755
1756   return ea->offset == eb->offset;
1757 }
1758
1759 /* Delete function for a quick_file_names.  */
1760
1761 static void
1762 delete_file_name_entry (void *e)
1763 {
1764   struct quick_file_names *file_data = e;
1765   int i;
1766
1767   for (i = 0; i < file_data->num_file_names; ++i)
1768     {
1769       xfree ((void*) file_data->file_names[i]);
1770       if (file_data->real_names)
1771         xfree ((void*) file_data->real_names[i]);
1772     }
1773
1774   /* The space for the struct itself lives on objfile_obstack,
1775      so we don't free it here.  */
1776 }
1777
1778 /* Create a quick_file_names hash table.  */
1779
1780 static htab_t
1781 create_quick_file_names_table (unsigned int nr_initial_entries)
1782 {
1783   return htab_create_alloc (nr_initial_entries,
1784                             hash_file_name_entry, eq_file_name_entry,
1785                             delete_file_name_entry, xcalloc, xfree);
1786 }
1787
1788 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1789    this CU came.  */
1790
1791 static void
1792 dw2_do_instantiate_symtab (struct objfile *objfile,
1793                            struct dwarf2_per_cu_data *per_cu)
1794 {
1795   struct cleanup *back_to;
1796
1797   back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799   queue_comp_unit (per_cu, objfile);
1800
1801   if (per_cu->from_debug_types)
1802     read_signatured_type_at_offset (objfile, per_cu->offset);
1803   else
1804     load_full_comp_unit (per_cu, objfile);
1805
1806   process_queue (objfile);
1807
1808   /* Age the cache, releasing compilation units that have not
1809      been used recently.  */
1810   age_cached_comp_units ();
1811
1812   do_cleanups (back_to);
1813 }
1814
1815 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1816    the objfile from which this CU came.  Returns the resulting symbol
1817    table.  */
1818
1819 static struct symtab *
1820 dw2_instantiate_symtab (struct objfile *objfile,
1821                         struct dwarf2_per_cu_data *per_cu)
1822 {
1823   if (!per_cu->v.quick->symtab)
1824     {
1825       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1826       increment_reading_symtab ();
1827       dw2_do_instantiate_symtab (objfile, per_cu);
1828       do_cleanups (back_to);
1829     }
1830   return per_cu->v.quick->symtab;
1831 }
1832
1833 /* Return the CU given its index.  */
1834
1835 static struct dwarf2_per_cu_data *
1836 dw2_get_cu (int index)
1837 {
1838   if (index >= dwarf2_per_objfile->n_comp_units)
1839     {
1840       index -= dwarf2_per_objfile->n_comp_units;
1841       return dwarf2_per_objfile->type_comp_units[index];
1842     }
1843   return dwarf2_per_objfile->all_comp_units[index];
1844 }
1845
1846 /* A helper function that knows how to read a 64-bit value in a way
1847    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1848    otherwise.  */
1849
1850 static int
1851 extract_cu_value (const char *bytes, ULONGEST *result)
1852 {
1853   if (sizeof (ULONGEST) < 8)
1854     {
1855       int i;
1856
1857       /* Ignore the upper 4 bytes if they are all zero.  */
1858       for (i = 0; i < 4; ++i)
1859         if (bytes[i + 4] != 0)
1860           return 0;
1861
1862       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1863     }
1864   else
1865     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1866   return 1;
1867 }
1868
1869 /* Read the CU list from the mapped index, and use it to create all
1870    the CU objects for this objfile.  Return 0 if something went wrong,
1871    1 if everything went ok.  */
1872
1873 static int
1874 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1875                        offset_type cu_list_elements)
1876 {
1877   offset_type i;
1878
1879   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1880   dwarf2_per_objfile->all_comp_units
1881     = obstack_alloc (&objfile->objfile_obstack,
1882                      dwarf2_per_objfile->n_comp_units
1883                      * sizeof (struct dwarf2_per_cu_data *));
1884
1885   for (i = 0; i < cu_list_elements; i += 2)
1886     {
1887       struct dwarf2_per_cu_data *the_cu;
1888       ULONGEST offset, length;
1889
1890       if (!extract_cu_value (cu_list, &offset)
1891           || !extract_cu_value (cu_list + 8, &length))
1892         return 0;
1893       cu_list += 2 * 8;
1894
1895       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1896                                struct dwarf2_per_cu_data);
1897       the_cu->offset = offset;
1898       the_cu->length = length;
1899       the_cu->objfile = objfile;
1900       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1901                                         struct dwarf2_per_cu_quick_data);
1902       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1903     }
1904
1905   return 1;
1906 }
1907
1908 /* Create the signatured type hash table from the index.  */
1909
1910 static int
1911 create_signatured_type_table_from_index (struct objfile *objfile,
1912                                          const gdb_byte *bytes,
1913                                          offset_type elements)
1914 {
1915   offset_type i;
1916   htab_t sig_types_hash;
1917
1918   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1919   dwarf2_per_objfile->type_comp_units
1920     = obstack_alloc (&objfile->objfile_obstack,
1921                      dwarf2_per_objfile->n_type_comp_units
1922                      * sizeof (struct dwarf2_per_cu_data *));
1923
1924   sig_types_hash = allocate_signatured_type_table (objfile);
1925
1926   for (i = 0; i < elements; i += 3)
1927     {
1928       struct signatured_type *type_sig;
1929       ULONGEST offset, type_offset, signature;
1930       void **slot;
1931
1932       if (!extract_cu_value (bytes, &offset)
1933           || !extract_cu_value (bytes + 8, &type_offset))
1934         return 0;
1935       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936       bytes += 3 * 8;
1937
1938       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939                                  struct signatured_type);
1940       type_sig->signature = signature;
1941       type_sig->type_offset = type_offset;
1942       type_sig->per_cu.from_debug_types = 1;
1943       type_sig->per_cu.offset = offset;
1944       type_sig->per_cu.objfile = objfile;
1945       type_sig->per_cu.v.quick
1946         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947                           struct dwarf2_per_cu_quick_data);
1948
1949       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1950       *slot = type_sig;
1951
1952       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1953     }
1954
1955   dwarf2_per_objfile->signatured_types = sig_types_hash;
1956
1957   return 1;
1958 }
1959
1960 /* Read the address map data from the mapped index, and use it to
1961    populate the objfile's psymtabs_addrmap.  */
1962
1963 static void
1964 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965 {
1966   const gdb_byte *iter, *end;
1967   struct obstack temp_obstack;
1968   struct addrmap *mutable_map;
1969   struct cleanup *cleanup;
1970   CORE_ADDR baseaddr;
1971
1972   obstack_init (&temp_obstack);
1973   cleanup = make_cleanup_obstack_free (&temp_obstack);
1974   mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976   iter = index->address_table;
1977   end = iter + index->address_table_size;
1978
1979   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981   while (iter < end)
1982     {
1983       ULONGEST hi, lo, cu_index;
1984       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985       iter += 8;
1986       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987       iter += 8;
1988       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989       iter += 4;
1990       
1991       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1992                          dw2_get_cu (cu_index));
1993     }
1994
1995   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996                                                     &objfile->objfile_obstack);
1997   do_cleanups (cleanup);
1998 }
1999
2000 /* The hash function for strings in the mapped index.  This is the same as
2001    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002    implementation.  This is necessary because the hash function is tied to the
2003    format of the mapped index file.  The hash values do not have to match with
2004    SYMBOL_HASH_NEXT.
2005    
2006    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2007
2008 static hashval_t
2009 mapped_index_string_hash (int index_version, const void *p)
2010 {
2011   const unsigned char *str = (const unsigned char *) p;
2012   hashval_t r = 0;
2013   unsigned char c;
2014
2015   while ((c = *str++) != 0)
2016     {
2017       if (index_version >= 5)
2018         c = tolower (c);
2019       r = r * 67 + c - 113;
2020     }
2021
2022   return r;
2023 }
2024
2025 /* Find a slot in the mapped index INDEX for the object named NAME.
2026    If NAME is found, set *VEC_OUT to point to the CU vector in the
2027    constant pool and return 1.  If NAME cannot be found, return 0.  */
2028
2029 static int
2030 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031                           offset_type **vec_out)
2032 {
2033   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034   offset_type hash;
2035   offset_type slot, step;
2036   int (*cmp) (const char *, const char *);
2037
2038   if (current_language->la_language == language_cplus
2039       || current_language->la_language == language_java
2040       || current_language->la_language == language_fortran)
2041     {
2042       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2043          not contain any.  */
2044       const char *paren = strchr (name, '(');
2045
2046       if (paren)
2047         {
2048           char *dup;
2049
2050           dup = xmalloc (paren - name + 1);
2051           memcpy (dup, name, paren - name);
2052           dup[paren - name] = 0;
2053
2054           make_cleanup (xfree, dup);
2055           name = dup;
2056         }
2057     }
2058
2059   /* Index version 4 did not support case insensitive searches.  But the
2060      indexes for case insensitive languages are built in lowercase, therefore
2061      simulate our NAME being searched is also lowercased.  */
2062   hash = mapped_index_string_hash ((index->version == 4
2063                                     && case_sensitivity == case_sensitive_off
2064                                     ? 5 : index->version),
2065                                    name);
2066
2067   slot = hash & (index->symbol_table_slots - 1);
2068   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2069   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2070
2071   for (;;)
2072     {
2073       /* Convert a slot number to an offset into the table.  */
2074       offset_type i = 2 * slot;
2075       const char *str;
2076       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2077         {
2078           do_cleanups (back_to);
2079           return 0;
2080         }
2081
2082       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2083       if (!cmp (name, str))
2084         {
2085           *vec_out = (offset_type *) (index->constant_pool
2086                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2087           do_cleanups (back_to);
2088           return 1;
2089         }
2090
2091       slot = (slot + step) & (index->symbol_table_slots - 1);
2092     }
2093 }
2094
2095 /* Read the index file.  If everything went ok, initialize the "quick"
2096    elements of all the CUs and return 1.  Otherwise, return 0.  */
2097
2098 static int
2099 dwarf2_read_index (struct objfile *objfile)
2100 {
2101   char *addr;
2102   struct mapped_index *map;
2103   offset_type *metadata;
2104   const gdb_byte *cu_list;
2105   const gdb_byte *types_list = NULL;
2106   offset_type version, cu_list_elements;
2107   offset_type types_list_elements = 0;
2108   int i;
2109
2110   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2111     return 0;
2112
2113   /* Older elfutils strip versions could keep the section in the main
2114      executable while splitting it for the separate debug info file.  */
2115   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116        & SEC_HAS_CONTENTS) == 0)
2117     return 0;
2118
2119   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121   addr = dwarf2_per_objfile->gdb_index.buffer;
2122   /* Version check.  */
2123   version = MAYBE_SWAP (*(offset_type *) addr);
2124   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2125      causes the index to behave very poorly for certain requests.  Version 3
2126      contained incomplete addrmap.  So, it seems better to just ignore such
2127      indices.  Index version 4 uses a different hash function than index
2128      version 5 and later.  */
2129   if (version < 4)
2130     return 0;
2131   /* Indexes with higher version than the one supported by GDB may be no
2132      longer backward compatible.  */
2133   if (version > 5)
2134     return 0;
2135
2136   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2137   map->version = version;
2138   map->total_size = dwarf2_per_objfile->gdb_index.size;
2139
2140   metadata = (offset_type *) (addr + sizeof (offset_type));
2141
2142   i = 0;
2143   cu_list = addr + MAYBE_SWAP (metadata[i]);
2144   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2145                       / 8);
2146   ++i;
2147
2148   types_list = addr + MAYBE_SWAP (metadata[i]);
2149   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2150                           - MAYBE_SWAP (metadata[i]))
2151                          / 8);
2152   ++i;
2153
2154   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2155   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2156                              - MAYBE_SWAP (metadata[i]));
2157   ++i;
2158
2159   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2160   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2161                               - MAYBE_SWAP (metadata[i]))
2162                              / (2 * sizeof (offset_type)));
2163   ++i;
2164
2165   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2166
2167   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2168     return 0;
2169
2170   if (types_list_elements
2171       && !create_signatured_type_table_from_index (objfile, types_list,
2172                                                    types_list_elements))
2173     return 0;
2174
2175   create_addrmap_from_index (objfile, map);
2176
2177   dwarf2_per_objfile->index_table = map;
2178   dwarf2_per_objfile->using_index = 1;
2179   dwarf2_per_objfile->quick_file_names_table =
2180     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2181
2182   return 1;
2183 }
2184
2185 /* A helper for the "quick" functions which sets the global
2186    dwarf2_per_objfile according to OBJFILE.  */
2187
2188 static void
2189 dw2_setup (struct objfile *objfile)
2190 {
2191   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2192   gdb_assert (dwarf2_per_objfile);
2193 }
2194
2195 /* A helper for the "quick" functions which attempts to read the line
2196    table for THIS_CU.  */
2197
2198 static struct quick_file_names *
2199 dw2_get_file_names (struct objfile *objfile,
2200                     struct dwarf2_per_cu_data *this_cu)
2201 {
2202   bfd *abfd = objfile->obfd;
2203   struct line_header *lh;
2204   struct attribute *attr;
2205   struct cleanup *cleanups;
2206   struct die_info *comp_unit_die;
2207   struct dwarf2_section_info* sec;
2208   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2209   int has_children, i;
2210   struct dwarf2_cu cu;
2211   unsigned int bytes_read, buffer_size;
2212   struct die_reader_specs reader_specs;
2213   char *name, *comp_dir;
2214   void **slot;
2215   struct quick_file_names *qfn;
2216   unsigned int line_offset;
2217
2218   if (this_cu->v.quick->file_names != NULL)
2219     return this_cu->v.quick->file_names;
2220   /* If we know there is no line data, no point in looking again.  */
2221   if (this_cu->v.quick->no_file_data)
2222     return NULL;
2223
2224   init_one_comp_unit (&cu, objfile);
2225   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2226
2227   if (this_cu->from_debug_types)
2228     sec = &dwarf2_per_objfile->types;
2229   else
2230     sec = &dwarf2_per_objfile->info;
2231   dwarf2_read_section (objfile, sec);
2232   buffer_size = sec->size;
2233   buffer = sec->buffer;
2234   info_ptr = buffer + this_cu->offset;
2235   beg_of_comp_unit = info_ptr;
2236
2237   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2238                                           buffer, buffer_size,
2239                                           abfd);
2240
2241   /* Complete the cu_header.  */
2242   cu.header.offset = beg_of_comp_unit - buffer;
2243   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2244
2245   this_cu->cu = &cu;
2246   cu.per_cu = this_cu;
2247
2248   dwarf2_read_abbrevs (abfd, &cu);
2249   make_cleanup (dwarf2_free_abbrev_table, &cu);
2250
2251   if (this_cu->from_debug_types)
2252     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2253   init_cu_die_reader (&reader_specs, &cu);
2254   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2255                  &has_children);
2256
2257   lh = NULL;
2258   slot = NULL;
2259   line_offset = 0;
2260   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2261   if (attr)
2262     {
2263       struct quick_file_names find_entry;
2264
2265       line_offset = DW_UNSND (attr);
2266
2267       /* We may have already read in this line header (TU line header sharing).
2268          If we have we're done.  */
2269       find_entry.offset = line_offset;
2270       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2271                              &find_entry, INSERT);
2272       if (*slot != NULL)
2273         {
2274           do_cleanups (cleanups);
2275           this_cu->v.quick->file_names = *slot;
2276           return *slot;
2277         }
2278
2279       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2280     }
2281   if (lh == NULL)
2282     {
2283       do_cleanups (cleanups);
2284       this_cu->v.quick->no_file_data = 1;
2285       return NULL;
2286     }
2287
2288   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2289   qfn->offset = line_offset;
2290   gdb_assert (slot != NULL);
2291   *slot = qfn;
2292
2293   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2294
2295   qfn->num_file_names = lh->num_file_names;
2296   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2297                                    lh->num_file_names * sizeof (char *));
2298   for (i = 0; i < lh->num_file_names; ++i)
2299     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2300   qfn->real_names = NULL;
2301
2302   free_line_header (lh);
2303   do_cleanups (cleanups);
2304
2305   this_cu->v.quick->file_names = qfn;
2306   return qfn;
2307 }
2308
2309 /* A helper for the "quick" functions which computes and caches the
2310    real path for a given file name from the line table.  */
2311
2312 static const char *
2313 dw2_get_real_path (struct objfile *objfile,
2314                    struct quick_file_names *qfn, int index)
2315 {
2316   if (qfn->real_names == NULL)
2317     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2318                                       qfn->num_file_names, sizeof (char *));
2319
2320   if (qfn->real_names[index] == NULL)
2321     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2322
2323   return qfn->real_names[index];
2324 }
2325
2326 static struct symtab *
2327 dw2_find_last_source_symtab (struct objfile *objfile)
2328 {
2329   int index;
2330
2331   dw2_setup (objfile);
2332   index = dwarf2_per_objfile->n_comp_units - 1;
2333   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2334 }
2335
2336 /* Traversal function for dw2_forget_cached_source_info.  */
2337
2338 static int
2339 dw2_free_cached_file_names (void **slot, void *info)
2340 {
2341   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2342
2343   if (file_data->real_names)
2344     {
2345       int i;
2346
2347       for (i = 0; i < file_data->num_file_names; ++i)
2348         {
2349           xfree ((void*) file_data->real_names[i]);
2350           file_data->real_names[i] = NULL;
2351         }
2352     }
2353
2354   return 1;
2355 }
2356
2357 static void
2358 dw2_forget_cached_source_info (struct objfile *objfile)
2359 {
2360   dw2_setup (objfile);
2361
2362   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2363                           dw2_free_cached_file_names, NULL);
2364 }
2365
2366 static int
2367 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2368                    const char *full_path, const char *real_path,
2369                    struct symtab **result)
2370 {
2371   int i;
2372   int check_basename = lbasename (name) == name;
2373   struct dwarf2_per_cu_data *base_cu = NULL;
2374
2375   dw2_setup (objfile);
2376
2377   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2378                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2379     {
2380       int j;
2381       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2382       struct quick_file_names *file_data;
2383
2384       if (per_cu->v.quick->symtab)
2385         continue;
2386
2387       file_data = dw2_get_file_names (objfile, per_cu);
2388       if (file_data == NULL)
2389         continue;
2390
2391       for (j = 0; j < file_data->num_file_names; ++j)
2392         {
2393           const char *this_name = file_data->file_names[j];
2394
2395           if (FILENAME_CMP (name, this_name) == 0)
2396             {
2397               *result = dw2_instantiate_symtab (objfile, per_cu);
2398               return 1;
2399             }
2400
2401           if (check_basename && ! base_cu
2402               && FILENAME_CMP (lbasename (this_name), name) == 0)
2403             base_cu = per_cu;
2404
2405           if (full_path != NULL)
2406             {
2407               const char *this_real_name = dw2_get_real_path (objfile,
2408                                                               file_data, j);
2409
2410               if (this_real_name != NULL
2411                   && FILENAME_CMP (full_path, this_real_name) == 0)
2412                 {
2413                   *result = dw2_instantiate_symtab (objfile, per_cu);
2414                   return 1;
2415                 }
2416             }
2417
2418           if (real_path != NULL)
2419             {
2420               const char *this_real_name = dw2_get_real_path (objfile,
2421                                                               file_data, j);
2422
2423               if (this_real_name != NULL
2424                   && FILENAME_CMP (real_path, this_real_name) == 0)
2425                 {
2426                   *result = dw2_instantiate_symtab (objfile, per_cu);
2427                   return 1;
2428                 }
2429             }
2430         }
2431     }
2432
2433   if (base_cu)
2434     {
2435       *result = dw2_instantiate_symtab (objfile, base_cu);
2436       return 1;
2437     }
2438
2439   return 0;
2440 }
2441
2442 static struct symtab *
2443 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2444                    const char *name, domain_enum domain)
2445 {
2446   /* We do all the work in the pre_expand_symtabs_matching hook
2447      instead.  */
2448   return NULL;
2449 }
2450
2451 /* A helper function that expands all symtabs that hold an object
2452    named NAME.  */
2453
2454 static void
2455 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2456 {
2457   dw2_setup (objfile);
2458
2459   /* index_table is NULL if OBJF_READNOW.  */
2460   if (dwarf2_per_objfile->index_table)
2461     {
2462       offset_type *vec;
2463
2464       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2465                                     name, &vec))
2466         {
2467           offset_type i, len = MAYBE_SWAP (*vec);
2468           for (i = 0; i < len; ++i)
2469             {
2470               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2471               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2472
2473               dw2_instantiate_symtab (objfile, per_cu);
2474             }
2475         }
2476     }
2477 }
2478
2479 static void
2480 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2481                                  enum block_enum block_kind, const char *name,
2482                                  domain_enum domain)
2483 {
2484   dw2_do_expand_symtabs_matching (objfile, name);
2485 }
2486
2487 static void
2488 dw2_print_stats (struct objfile *objfile)
2489 {
2490   int i, count;
2491
2492   dw2_setup (objfile);
2493   count = 0;
2494   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2495                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2496     {
2497       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2498
2499       if (!per_cu->v.quick->symtab)
2500         ++count;
2501     }
2502   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2503 }
2504
2505 static void
2506 dw2_dump (struct objfile *objfile)
2507 {
2508   /* Nothing worth printing.  */
2509 }
2510
2511 static void
2512 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2513               struct section_offsets *delta)
2514 {
2515   /* There's nothing to relocate here.  */
2516 }
2517
2518 static void
2519 dw2_expand_symtabs_for_function (struct objfile *objfile,
2520                                  const char *func_name)
2521 {
2522   dw2_do_expand_symtabs_matching (objfile, func_name);
2523 }
2524
2525 static void
2526 dw2_expand_all_symtabs (struct objfile *objfile)
2527 {
2528   int i;
2529
2530   dw2_setup (objfile);
2531
2532   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2533                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2534     {
2535       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2536
2537       dw2_instantiate_symtab (objfile, per_cu);
2538     }
2539 }
2540
2541 static void
2542 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2543                                   const char *filename)
2544 {
2545   int i;
2546
2547   dw2_setup (objfile);
2548
2549   /* We don't need to consider type units here.
2550      This is only called for examining code, e.g. expand_line_sal.
2551      There can be an order of magnitude (or more) more type units
2552      than comp units, and we avoid them if we can.  */
2553
2554   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2555     {
2556       int j;
2557       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2558       struct quick_file_names *file_data;
2559
2560       if (per_cu->v.quick->symtab)
2561         continue;
2562
2563       file_data = dw2_get_file_names (objfile, per_cu);
2564       if (file_data == NULL)
2565         continue;
2566
2567       for (j = 0; j < file_data->num_file_names; ++j)
2568         {
2569           const char *this_name = file_data->file_names[j];
2570           if (FILENAME_CMP (this_name, filename) == 0)
2571             {
2572               dw2_instantiate_symtab (objfile, per_cu);
2573               break;
2574             }
2575         }
2576     }
2577 }
2578
2579 static const char *
2580 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2581 {
2582   struct dwarf2_per_cu_data *per_cu;
2583   offset_type *vec;
2584   struct quick_file_names *file_data;
2585
2586   dw2_setup (objfile);
2587
2588   /* index_table is NULL if OBJF_READNOW.  */
2589   if (!dwarf2_per_objfile->index_table)
2590     return NULL;
2591
2592   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2593                                  name, &vec))
2594     return NULL;
2595
2596   /* Note that this just looks at the very first one named NAME -- but
2597      actually we are looking for a function.  find_main_filename
2598      should be rewritten so that it doesn't require a custom hook.  It
2599      could just use the ordinary symbol tables.  */
2600   /* vec[0] is the length, which must always be >0.  */
2601   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2602
2603   file_data = dw2_get_file_names (objfile, per_cu);
2604   if (file_data == NULL)
2605     return NULL;
2606
2607   return file_data->file_names[file_data->num_file_names - 1];
2608 }
2609
2610 static void
2611 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2612                           struct objfile *objfile, int global,
2613                           int (*callback) (struct block *,
2614                                            struct symbol *, void *),
2615                           void *data, symbol_compare_ftype *match,
2616                           symbol_compare_ftype *ordered_compare)
2617 {
2618   /* Currently unimplemented; used for Ada.  The function can be called if the
2619      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2620      does not look for non-Ada symbols this function should just return.  */
2621 }
2622
2623 static void
2624 dw2_expand_symtabs_matching (struct objfile *objfile,
2625                              int (*file_matcher) (const char *, void *),
2626                              int (*name_matcher) (const char *, void *),
2627                              enum search_domain kind,
2628                              void *data)
2629 {
2630   int i;
2631   offset_type iter;
2632   struct mapped_index *index;
2633
2634   dw2_setup (objfile);
2635
2636   /* index_table is NULL if OBJF_READNOW.  */
2637   if (!dwarf2_per_objfile->index_table)
2638     return;
2639   index = dwarf2_per_objfile->index_table;
2640
2641   if (file_matcher != NULL)
2642     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2643                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2644       {
2645         int j;
2646         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647         struct quick_file_names *file_data;
2648
2649         per_cu->v.quick->mark = 0;
2650         if (per_cu->v.quick->symtab)
2651           continue;
2652
2653         file_data = dw2_get_file_names (objfile, per_cu);
2654         if (file_data == NULL)
2655           continue;
2656
2657         for (j = 0; j < file_data->num_file_names; ++j)
2658           {
2659             if (file_matcher (file_data->file_names[j], data))
2660               {
2661                 per_cu->v.quick->mark = 1;
2662                 break;
2663               }
2664           }
2665       }
2666
2667   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2668     {
2669       offset_type idx = 2 * iter;
2670       const char *name;
2671       offset_type *vec, vec_len, vec_idx;
2672
2673       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2674         continue;
2675
2676       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2677
2678       if (! (*name_matcher) (name, data))
2679         continue;
2680
2681       /* The name was matched, now expand corresponding CUs that were
2682          marked.  */
2683       vec = (offset_type *) (index->constant_pool
2684                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2685       vec_len = MAYBE_SWAP (vec[0]);
2686       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2687         {
2688           struct dwarf2_per_cu_data *per_cu;
2689
2690           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2691           if (file_matcher == NULL || per_cu->v.quick->mark)
2692             dw2_instantiate_symtab (objfile, per_cu);
2693         }
2694     }
2695 }
2696
2697 static struct symtab *
2698 dw2_find_pc_sect_symtab (struct objfile *objfile,
2699                          struct minimal_symbol *msymbol,
2700                          CORE_ADDR pc,
2701                          struct obj_section *section,
2702                          int warn_if_readin)
2703 {
2704   struct dwarf2_per_cu_data *data;
2705
2706   dw2_setup (objfile);
2707
2708   if (!objfile->psymtabs_addrmap)
2709     return NULL;
2710
2711   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2712   if (!data)
2713     return NULL;
2714
2715   if (warn_if_readin && data->v.quick->symtab)
2716     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2717              paddress (get_objfile_arch (objfile), pc));
2718
2719   return dw2_instantiate_symtab (objfile, data);
2720 }
2721
2722 static void
2723 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2724                           void *data)
2725 {
2726   int i;
2727
2728   dw2_setup (objfile);
2729
2730   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2731                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2732     {
2733       int j;
2734       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2735       struct quick_file_names *file_data;
2736
2737       if (per_cu->v.quick->symtab)
2738         continue;
2739
2740       file_data = dw2_get_file_names (objfile, per_cu);
2741       if (file_data == NULL)
2742         continue;
2743
2744       for (j = 0; j < file_data->num_file_names; ++j)
2745         {
2746           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2747                                                           j);
2748           (*fun) (file_data->file_names[j], this_real_name, data);
2749         }
2750     }
2751 }
2752
2753 static int
2754 dw2_has_symbols (struct objfile *objfile)
2755 {
2756   return 1;
2757 }
2758
2759 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2760 {
2761   dw2_has_symbols,
2762   dw2_find_last_source_symtab,
2763   dw2_forget_cached_source_info,
2764   dw2_lookup_symtab,
2765   dw2_lookup_symbol,
2766   dw2_pre_expand_symtabs_matching,
2767   dw2_print_stats,
2768   dw2_dump,
2769   dw2_relocate,
2770   dw2_expand_symtabs_for_function,
2771   dw2_expand_all_symtabs,
2772   dw2_expand_symtabs_with_filename,
2773   dw2_find_symbol_file,
2774   dw2_map_matching_symbols,
2775   dw2_expand_symtabs_matching,
2776   dw2_find_pc_sect_symtab,
2777   dw2_map_symbol_filenames
2778 };
2779
2780 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2781    file will use psymtabs, or 1 if using the GNU index.  */
2782
2783 int
2784 dwarf2_initialize_objfile (struct objfile *objfile)
2785 {
2786   /* If we're about to read full symbols, don't bother with the
2787      indices.  In this case we also don't care if some other debug
2788      format is making psymtabs, because they are all about to be
2789      expanded anyway.  */
2790   if ((objfile->flags & OBJF_READNOW))
2791     {
2792       int i;
2793
2794       dwarf2_per_objfile->using_index = 1;
2795       create_all_comp_units (objfile);
2796       create_debug_types_hash_table (objfile);
2797       dwarf2_per_objfile->quick_file_names_table =
2798         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2799
2800       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2801                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2802         {
2803           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2804
2805           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2806                                             struct dwarf2_per_cu_quick_data);
2807         }
2808
2809       /* Return 1 so that gdb sees the "quick" functions.  However,
2810          these functions will be no-ops because we will have expanded
2811          all symtabs.  */
2812       return 1;
2813     }
2814
2815   if (dwarf2_read_index (objfile))
2816     return 1;
2817
2818   return 0;
2819 }
2820
2821 \f
2822
2823 /* Build a partial symbol table.  */
2824
2825 void
2826 dwarf2_build_psymtabs (struct objfile *objfile)
2827 {
2828   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2829     {
2830       init_psymbol_list (objfile, 1024);
2831     }
2832
2833   dwarf2_build_psymtabs_hard (objfile);
2834 }
2835
2836 /* Return TRUE if OFFSET is within CU_HEADER.  */
2837
2838 static inline int
2839 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2840 {
2841   unsigned int bottom = cu_header->offset;
2842   unsigned int top = (cu_header->offset
2843                       + cu_header->length
2844                       + cu_header->initial_length_size);
2845
2846   return (offset >= bottom && offset < top);
2847 }
2848
2849 /* Read in the comp unit header information from the debug_info at info_ptr.
2850    NOTE: This leaves members offset, first_die_offset to be filled in
2851    by the caller.  */
2852
2853 static gdb_byte *
2854 read_comp_unit_head (struct comp_unit_head *cu_header,
2855                      gdb_byte *info_ptr, bfd *abfd)
2856 {
2857   int signed_addr;
2858   unsigned int bytes_read;
2859
2860   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2861   cu_header->initial_length_size = bytes_read;
2862   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2863   info_ptr += bytes_read;
2864   cu_header->version = read_2_bytes (abfd, info_ptr);
2865   info_ptr += 2;
2866   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2867                                           &bytes_read);
2868   info_ptr += bytes_read;
2869   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2870   info_ptr += 1;
2871   signed_addr = bfd_get_sign_extend_vma (abfd);
2872   if (signed_addr < 0)
2873     internal_error (__FILE__, __LINE__,
2874                     _("read_comp_unit_head: dwarf from non elf file"));
2875   cu_header->signed_addr_p = signed_addr;
2876
2877   return info_ptr;
2878 }
2879
2880 static gdb_byte *
2881 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2882                              gdb_byte *buffer, unsigned int buffer_size,
2883                              bfd *abfd)
2884 {
2885   gdb_byte *beg_of_comp_unit = info_ptr;
2886
2887   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2888
2889   if (header->version != 2 && header->version != 3 && header->version != 4)
2890     error (_("Dwarf Error: wrong version in compilation unit header "
2891            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2892            bfd_get_filename (abfd));
2893
2894   if (header->abbrev_offset
2895       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2896                               &dwarf2_per_objfile->abbrev))
2897     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2898            "(offset 0x%lx + 6) [in module %s]"),
2899            (long) header->abbrev_offset,
2900            (long) (beg_of_comp_unit - buffer),
2901            bfd_get_filename (abfd));
2902
2903   if (beg_of_comp_unit + header->length + header->initial_length_size
2904       > buffer + buffer_size)
2905     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2906            "(offset 0x%lx + 0) [in module %s]"),
2907            (long) header->length,
2908            (long) (beg_of_comp_unit - buffer),
2909            bfd_get_filename (abfd));
2910
2911   return info_ptr;
2912 }
2913
2914 /* Read in the types comp unit header information from .debug_types entry at
2915    types_ptr.  The result is a pointer to one past the end of the header.  */
2916
2917 static gdb_byte *
2918 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2919                           ULONGEST *signature,
2920                           gdb_byte *types_ptr, bfd *abfd)
2921 {
2922   gdb_byte *initial_types_ptr = types_ptr;
2923
2924   dwarf2_read_section (dwarf2_per_objfile->objfile,
2925                        &dwarf2_per_objfile->types);
2926   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2927
2928   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2929
2930   *signature = read_8_bytes (abfd, types_ptr);
2931   types_ptr += 8;
2932   types_ptr += cu_header->offset_size;
2933   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2934
2935   return types_ptr;
2936 }
2937
2938 /* Allocate a new partial symtab for file named NAME and mark this new
2939    partial symtab as being an include of PST.  */
2940
2941 static void
2942 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2943                                struct objfile *objfile)
2944 {
2945   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2946
2947   subpst->section_offsets = pst->section_offsets;
2948   subpst->textlow = 0;
2949   subpst->texthigh = 0;
2950
2951   subpst->dependencies = (struct partial_symtab **)
2952     obstack_alloc (&objfile->objfile_obstack,
2953                    sizeof (struct partial_symtab *));
2954   subpst->dependencies[0] = pst;
2955   subpst->number_of_dependencies = 1;
2956
2957   subpst->globals_offset = 0;
2958   subpst->n_global_syms = 0;
2959   subpst->statics_offset = 0;
2960   subpst->n_static_syms = 0;
2961   subpst->symtab = NULL;
2962   subpst->read_symtab = pst->read_symtab;
2963   subpst->readin = 0;
2964
2965   /* No private part is necessary for include psymtabs.  This property
2966      can be used to differentiate between such include psymtabs and
2967      the regular ones.  */
2968   subpst->read_symtab_private = NULL;
2969 }
2970
2971 /* Read the Line Number Program data and extract the list of files
2972    included by the source file represented by PST.  Build an include
2973    partial symtab for each of these included files.  */
2974
2975 static void
2976 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2977                                struct die_info *die,
2978                                struct partial_symtab *pst)
2979 {
2980   struct objfile *objfile = cu->objfile;
2981   bfd *abfd = objfile->obfd;
2982   struct line_header *lh = NULL;
2983   struct attribute *attr;
2984
2985   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2986   if (attr)
2987     {
2988       unsigned int line_offset = DW_UNSND (attr);
2989
2990       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2991     }
2992   if (lh == NULL)
2993     return;  /* No linetable, so no includes.  */
2994
2995   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
2996   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2997
2998   free_line_header (lh);
2999 }
3000
3001 static hashval_t
3002 hash_type_signature (const void *item)
3003 {
3004   const struct signatured_type *type_sig = item;
3005
3006   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3007   return type_sig->signature;
3008 }
3009
3010 static int
3011 eq_type_signature (const void *item_lhs, const void *item_rhs)
3012 {
3013   const struct signatured_type *lhs = item_lhs;
3014   const struct signatured_type *rhs = item_rhs;
3015
3016   return lhs->signature == rhs->signature;
3017 }
3018
3019 /* Allocate a hash table for signatured types.  */
3020
3021 static htab_t
3022 allocate_signatured_type_table (struct objfile *objfile)
3023 {
3024   return htab_create_alloc_ex (41,
3025                                hash_type_signature,
3026                                eq_type_signature,
3027                                NULL,
3028                                &objfile->objfile_obstack,
3029                                hashtab_obstack_allocate,
3030                                dummy_obstack_deallocate);
3031 }
3032
3033 /* A helper function to add a signatured type CU to a list.  */
3034
3035 static int
3036 add_signatured_type_cu_to_list (void **slot, void *datum)
3037 {
3038   struct signatured_type *sigt = *slot;
3039   struct dwarf2_per_cu_data ***datap = datum;
3040
3041   **datap = &sigt->per_cu;
3042   ++*datap;
3043
3044   return 1;
3045 }
3046
3047 /* Create the hash table of all entries in the .debug_types section.
3048    The result is zero if there is an error (e.g. missing .debug_types section),
3049    otherwise non-zero.  */
3050
3051 static int
3052 create_debug_types_hash_table (struct objfile *objfile)
3053 {
3054   gdb_byte *info_ptr;
3055   htab_t types_htab;
3056   struct dwarf2_per_cu_data **iter;
3057
3058   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3059   info_ptr = dwarf2_per_objfile->types.buffer;
3060
3061   if (info_ptr == NULL)
3062     {
3063       dwarf2_per_objfile->signatured_types = NULL;
3064       return 0;
3065     }
3066
3067   types_htab = allocate_signatured_type_table (objfile);
3068
3069   if (dwarf2_die_debug)
3070     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3071
3072   while (info_ptr < dwarf2_per_objfile->types.buffer
3073          + dwarf2_per_objfile->types.size)
3074     {
3075       unsigned int offset;
3076       unsigned int offset_size;
3077       unsigned int type_offset;
3078       unsigned int length, initial_length_size;
3079       unsigned short version;
3080       ULONGEST signature;
3081       struct signatured_type *type_sig;
3082       void **slot;
3083       gdb_byte *ptr = info_ptr;
3084
3085       offset = ptr - dwarf2_per_objfile->types.buffer;
3086
3087       /* We need to read the type's signature in order to build the hash
3088          table, but we don't need to read anything else just yet.  */
3089
3090       /* Sanity check to ensure entire cu is present.  */
3091       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3092       if (ptr + length + initial_length_size
3093           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3094         {
3095           complaint (&symfile_complaints,
3096                      _("debug type entry runs off end "
3097                        "of `.debug_types' section, ignored"));
3098           break;
3099         }
3100
3101       offset_size = initial_length_size == 4 ? 4 : 8;
3102       ptr += initial_length_size;
3103       version = bfd_get_16 (objfile->obfd, ptr);
3104       ptr += 2;
3105       ptr += offset_size; /* abbrev offset */
3106       ptr += 1; /* address size */
3107       signature = bfd_get_64 (objfile->obfd, ptr);
3108       ptr += 8;
3109       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3110
3111       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3112       memset (type_sig, 0, sizeof (*type_sig));
3113       type_sig->signature = signature;
3114       type_sig->type_offset = type_offset;
3115       type_sig->per_cu.objfile = objfile;
3116       type_sig->per_cu.from_debug_types = 1;
3117       type_sig->per_cu.offset = offset;
3118
3119       slot = htab_find_slot (types_htab, type_sig, INSERT);
3120       gdb_assert (slot != NULL);
3121       if (*slot != NULL)
3122         {
3123           const struct signatured_type *dup_sig = *slot;
3124
3125           complaint (&symfile_complaints,
3126                      _("debug type entry at offset 0x%x is duplicate to the "
3127                        "entry at offset 0x%x, signature 0x%s"),
3128                      offset, dup_sig->per_cu.offset,
3129                      phex (signature, sizeof (signature)));
3130           gdb_assert (signature == dup_sig->signature);
3131         }
3132       *slot = type_sig;
3133
3134       if (dwarf2_die_debug)
3135         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3136                             offset, phex (signature, sizeof (signature)));
3137
3138       info_ptr = info_ptr + initial_length_size + length;
3139     }
3140
3141   dwarf2_per_objfile->signatured_types = types_htab;
3142
3143   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3144   dwarf2_per_objfile->type_comp_units
3145     = obstack_alloc (&objfile->objfile_obstack,
3146                      dwarf2_per_objfile->n_type_comp_units
3147                      * sizeof (struct dwarf2_per_cu_data *));
3148   iter = &dwarf2_per_objfile->type_comp_units[0];
3149   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3150   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3151               == dwarf2_per_objfile->n_type_comp_units);
3152
3153   return 1;
3154 }
3155
3156 /* Lookup a signature based type.
3157    Returns NULL if SIG is not present in the table.  */
3158
3159 static struct signatured_type *
3160 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3161 {
3162   struct signatured_type find_entry, *entry;
3163
3164   if (dwarf2_per_objfile->signatured_types == NULL)
3165     {
3166       complaint (&symfile_complaints,
3167                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3168       return 0;
3169     }
3170
3171   find_entry.signature = sig;
3172   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3173   return entry;
3174 }
3175
3176 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3177
3178 static void
3179 init_cu_die_reader (struct die_reader_specs *reader,
3180                     struct dwarf2_cu *cu)
3181 {
3182   reader->abfd = cu->objfile->obfd;
3183   reader->cu = cu;
3184   if (cu->per_cu->from_debug_types)
3185     {
3186       gdb_assert (dwarf2_per_objfile->types.readin);
3187       reader->buffer = dwarf2_per_objfile->types.buffer;
3188     }
3189   else
3190     {
3191       gdb_assert (dwarf2_per_objfile->info.readin);
3192       reader->buffer = dwarf2_per_objfile->info.buffer;
3193     }
3194 }
3195
3196 /* Find the base address of the compilation unit for range lists and
3197    location lists.  It will normally be specified by DW_AT_low_pc.
3198    In DWARF-3 draft 4, the base address could be overridden by
3199    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3200    compilation units with discontinuous ranges.  */
3201
3202 static void
3203 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3204 {
3205   struct attribute *attr;
3206
3207   cu->base_known = 0;
3208   cu->base_address = 0;
3209
3210   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3211   if (attr)
3212     {
3213       cu->base_address = DW_ADDR (attr);
3214       cu->base_known = 1;
3215     }
3216   else
3217     {
3218       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3219       if (attr)
3220         {
3221           cu->base_address = DW_ADDR (attr);
3222           cu->base_known = 1;
3223         }
3224     }
3225 }
3226
3227 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3228    to combine the common parts.
3229    Process a compilation unit for a psymtab.
3230    BUFFER is a pointer to the beginning of the dwarf section buffer,
3231    either .debug_info or debug_types.
3232    INFO_PTR is a pointer to the start of the CU.
3233    Returns a pointer to the next CU.  */
3234
3235 static gdb_byte *
3236 process_psymtab_comp_unit (struct objfile *objfile,
3237                            struct dwarf2_per_cu_data *this_cu,
3238                            gdb_byte *buffer, gdb_byte *info_ptr,
3239                            unsigned int buffer_size)
3240 {
3241   bfd *abfd = objfile->obfd;
3242   gdb_byte *beg_of_comp_unit = info_ptr;
3243   struct die_info *comp_unit_die;
3244   struct partial_symtab *pst;
3245   CORE_ADDR baseaddr;
3246   struct cleanup *back_to_inner;
3247   struct dwarf2_cu cu;
3248   int has_children, has_pc_info;
3249   struct attribute *attr;
3250   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3251   struct die_reader_specs reader_specs;
3252   const char *filename;
3253
3254   init_one_comp_unit (&cu, objfile);
3255   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3256
3257   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3258                                           buffer, buffer_size,
3259                                           abfd);
3260
3261   /* Complete the cu_header.  */
3262   cu.header.offset = beg_of_comp_unit - buffer;
3263   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3264
3265   cu.list_in_scope = &file_symbols;
3266
3267   /* If this compilation unit was already read in, free the
3268      cached copy in order to read it in again.  This is
3269      necessary because we skipped some symbols when we first
3270      read in the compilation unit (see load_partial_dies).
3271      This problem could be avoided, but the benefit is
3272      unclear.  */
3273   if (this_cu->cu != NULL)
3274     free_one_cached_comp_unit (this_cu->cu);
3275
3276   /* Note that this is a pointer to our stack frame, being
3277      added to a global data structure.  It will be cleaned up
3278      in free_stack_comp_unit when we finish with this
3279      compilation unit.  */
3280   this_cu->cu = &cu;
3281   cu.per_cu = this_cu;
3282
3283   /* Read the abbrevs for this compilation unit into a table.  */
3284   dwarf2_read_abbrevs (abfd, &cu);
3285   make_cleanup (dwarf2_free_abbrev_table, &cu);
3286
3287   /* Read the compilation unit die.  */
3288   if (this_cu->from_debug_types)
3289     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3290   init_cu_die_reader (&reader_specs, &cu);
3291   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3292                             &has_children);
3293
3294   if (this_cu->from_debug_types)
3295     {
3296       /* LENGTH has not been set yet for type units.  */
3297       gdb_assert (this_cu->offset == cu.header.offset);
3298       this_cu->length = cu.header.length + cu.header.initial_length_size;
3299     }
3300   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3301     {
3302       info_ptr = (beg_of_comp_unit + cu.header.length
3303                   + cu.header.initial_length_size);
3304       do_cleanups (back_to_inner);
3305       return info_ptr;
3306     }
3307
3308   prepare_one_comp_unit (&cu, comp_unit_die);
3309
3310   /* Allocate a new partial symbol table structure.  */
3311   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3312   if (attr == NULL || !DW_STRING (attr))
3313     filename = "";
3314   else
3315     filename = DW_STRING (attr);
3316   pst = start_psymtab_common (objfile, objfile->section_offsets,
3317                               filename,
3318                               /* TEXTLOW and TEXTHIGH are set below.  */
3319                               0,
3320                               objfile->global_psymbols.next,
3321                               objfile->static_psymbols.next);
3322
3323   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3324   if (attr != NULL)
3325     pst->dirname = DW_STRING (attr);
3326
3327   pst->read_symtab_private = this_cu;
3328
3329   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3330
3331   /* Store the function that reads in the rest of the symbol table.  */
3332   pst->read_symtab = dwarf2_psymtab_to_symtab;
3333
3334   this_cu->v.psymtab = pst;
3335
3336   dwarf2_find_base_address (comp_unit_die, &cu);
3337
3338   /* Possibly set the default values of LOWPC and HIGHPC from
3339      `DW_AT_ranges'.  */
3340   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3341                                       &best_highpc, &cu, pst);
3342   if (has_pc_info == 1 && best_lowpc < best_highpc)
3343     /* Store the contiguous range if it is not empty; it can be empty for
3344        CUs with no code.  */
3345     addrmap_set_empty (objfile->psymtabs_addrmap,
3346                        best_lowpc + baseaddr,
3347                        best_highpc + baseaddr - 1, pst);
3348
3349   /* Check if comp unit has_children.
3350      If so, read the rest of the partial symbols from this comp unit.
3351      If not, there's no more debug_info for this comp unit.  */
3352   if (has_children)
3353     {
3354       struct partial_die_info *first_die;
3355       CORE_ADDR lowpc, highpc;
3356
3357       lowpc = ((CORE_ADDR) -1);
3358       highpc = ((CORE_ADDR) 0);
3359
3360       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3361
3362       scan_partial_symbols (first_die, &lowpc, &highpc,
3363                             ! has_pc_info, &cu);
3364
3365       /* If we didn't find a lowpc, set it to highpc to avoid
3366          complaints from `maint check'.  */
3367       if (lowpc == ((CORE_ADDR) -1))
3368         lowpc = highpc;
3369
3370       /* If the compilation unit didn't have an explicit address range,
3371          then use the information extracted from its child dies.  */
3372       if (! has_pc_info)
3373         {
3374           best_lowpc = lowpc;
3375           best_highpc = highpc;
3376         }
3377     }
3378   pst->textlow = best_lowpc + baseaddr;
3379   pst->texthigh = best_highpc + baseaddr;
3380
3381   pst->n_global_syms = objfile->global_psymbols.next -
3382     (objfile->global_psymbols.list + pst->globals_offset);
3383   pst->n_static_syms = objfile->static_psymbols.next -
3384     (objfile->static_psymbols.list + pst->statics_offset);
3385   sort_pst_symbols (pst);
3386
3387   info_ptr = (beg_of_comp_unit + cu.header.length
3388               + cu.header.initial_length_size);
3389
3390   if (this_cu->from_debug_types)
3391     {
3392       /* It's not clear we want to do anything with stmt lists here.
3393          Waiting to see what gcc ultimately does.  */
3394     }
3395   else
3396     {
3397       /* Get the list of files included in the current compilation unit,
3398          and build a psymtab for each of them.  */
3399       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3400     }
3401
3402   do_cleanups (back_to_inner);
3403
3404   return info_ptr;
3405 }
3406
3407 /* Traversal function for htab_traverse_noresize.
3408    Process one .debug_types comp-unit.  */
3409
3410 static int
3411 process_type_comp_unit (void **slot, void *info)
3412 {
3413   struct signatured_type *entry = (struct signatured_type *) *slot;
3414   struct objfile *objfile = (struct objfile *) info;
3415   struct dwarf2_per_cu_data *this_cu;
3416
3417   this_cu = &entry->per_cu;
3418
3419   gdb_assert (dwarf2_per_objfile->types.readin);
3420   process_psymtab_comp_unit (objfile, this_cu,
3421                              dwarf2_per_objfile->types.buffer,
3422                              dwarf2_per_objfile->types.buffer + this_cu->offset,
3423                              dwarf2_per_objfile->types.size);
3424
3425   return 1;
3426 }
3427
3428 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3429    Build partial symbol tables for the .debug_types comp-units.  */
3430
3431 static void
3432 build_type_psymtabs (struct objfile *objfile)
3433 {
3434   if (! create_debug_types_hash_table (objfile))
3435     return;
3436
3437   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3438                           process_type_comp_unit, objfile);
3439 }
3440
3441 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3442
3443 static void
3444 psymtabs_addrmap_cleanup (void *o)
3445 {
3446   struct objfile *objfile = o;
3447
3448   objfile->psymtabs_addrmap = NULL;
3449 }
3450
3451 /* Build the partial symbol table by doing a quick pass through the
3452    .debug_info and .debug_abbrev sections.  */
3453
3454 static void
3455 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3456 {
3457   gdb_byte *info_ptr;
3458   struct cleanup *back_to, *addrmap_cleanup;
3459   struct obstack temp_obstack;
3460
3461   dwarf2_per_objfile->reading_partial_symbols = 1;
3462
3463   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3464   info_ptr = dwarf2_per_objfile->info.buffer;
3465
3466   /* Any cached compilation units will be linked by the per-objfile
3467      read_in_chain.  Make sure to free them when we're done.  */
3468   back_to = make_cleanup (free_cached_comp_units, NULL);
3469
3470   build_type_psymtabs (objfile);
3471
3472   create_all_comp_units (objfile);
3473
3474   /* Create a temporary address map on a temporary obstack.  We later
3475      copy this to the final obstack.  */
3476   obstack_init (&temp_obstack);
3477   make_cleanup_obstack_free (&temp_obstack);
3478   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3479   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3480
3481   /* Since the objects we're extracting from .debug_info vary in
3482      length, only the individual functions to extract them (like
3483      read_comp_unit_head and load_partial_die) can really know whether
3484      the buffer is large enough to hold another complete object.
3485
3486      At the moment, they don't actually check that.  If .debug_info
3487      holds just one extra byte after the last compilation unit's dies,
3488      then read_comp_unit_head will happily read off the end of the
3489      buffer.  read_partial_die is similarly casual.  Those functions
3490      should be fixed.
3491
3492      For this loop condition, simply checking whether there's any data
3493      left at all should be sufficient.  */
3494
3495   while (info_ptr < (dwarf2_per_objfile->info.buffer
3496                      + dwarf2_per_objfile->info.size))
3497     {
3498       struct dwarf2_per_cu_data *this_cu;
3499
3500       this_cu = dwarf2_find_comp_unit (info_ptr
3501                                        - dwarf2_per_objfile->info.buffer,
3502                                        objfile);
3503
3504       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3505                                             dwarf2_per_objfile->info.buffer,
3506                                             info_ptr,
3507                                             dwarf2_per_objfile->info.size);
3508     }
3509
3510   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3511                                                     &objfile->objfile_obstack);
3512   discard_cleanups (addrmap_cleanup);
3513
3514   do_cleanups (back_to);
3515 }
3516
3517 /* Load the partial DIEs for a secondary CU into memory.  */
3518
3519 static void
3520 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3521                         struct objfile *objfile)
3522 {
3523   bfd *abfd = objfile->obfd;
3524   gdb_byte *info_ptr, *beg_of_comp_unit;
3525   struct die_info *comp_unit_die;
3526   struct dwarf2_cu *cu;
3527   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3528   int has_children;
3529   struct die_reader_specs reader_specs;
3530   int read_cu = 0;
3531
3532   gdb_assert (! this_cu->from_debug_types);
3533
3534   gdb_assert (dwarf2_per_objfile->info.readin);
3535   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3536   beg_of_comp_unit = info_ptr;
3537
3538   if (this_cu->cu == NULL)
3539     {
3540       cu = xmalloc (sizeof (*cu));
3541       init_one_comp_unit (cu, objfile);
3542
3543       read_cu = 1;
3544
3545       /* If an error occurs while loading, release our storage.  */
3546       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3547
3548       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3549                                               dwarf2_per_objfile->info.buffer,
3550                                               dwarf2_per_objfile->info.size,
3551                                               abfd);
3552
3553       /* Complete the cu_header.  */
3554       cu->header.offset = this_cu->offset;
3555       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3556
3557       /* Link this compilation unit into the compilation unit tree.  */
3558       this_cu->cu = cu;
3559       cu->per_cu = this_cu;
3560
3561       /* Link this CU into read_in_chain.  */
3562       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3563       dwarf2_per_objfile->read_in_chain = this_cu;
3564     }
3565   else
3566     {
3567       cu = this_cu->cu;
3568       info_ptr += cu->header.first_die_offset;
3569     }
3570
3571   /* Read the abbrevs for this compilation unit into a table.  */
3572   gdb_assert (cu->dwarf2_abbrevs == NULL);
3573   dwarf2_read_abbrevs (abfd, cu);
3574   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3575
3576   /* Read the compilation unit die.  */
3577   init_cu_die_reader (&reader_specs, cu);
3578   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3579                             &has_children);
3580
3581   prepare_one_comp_unit (cu, comp_unit_die);
3582
3583   /* Check if comp unit has_children.
3584      If so, read the rest of the partial symbols from this comp unit.
3585      If not, there's no more debug_info for this comp unit.  */
3586   if (has_children)
3587     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3588
3589   do_cleanups (free_abbrevs_cleanup);
3590
3591   if (read_cu)
3592     {
3593       /* We've successfully allocated this compilation unit.  Let our
3594          caller clean it up when finished with it.  */
3595       discard_cleanups (free_cu_cleanup);
3596     }
3597 }
3598
3599 /* Create a list of all compilation units in OBJFILE.  We do this only
3600    if an inter-comp-unit reference is found; presumably if there is one,
3601    there will be many, and one will occur early in the .debug_info section.
3602    So there's no point in building this list incrementally.  */
3603
3604 static void
3605 create_all_comp_units (struct objfile *objfile)
3606 {
3607   int n_allocated;
3608   int n_comp_units;
3609   struct dwarf2_per_cu_data **all_comp_units;
3610   gdb_byte *info_ptr;
3611
3612   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3613   info_ptr = dwarf2_per_objfile->info.buffer;
3614
3615   n_comp_units = 0;
3616   n_allocated = 10;
3617   all_comp_units = xmalloc (n_allocated
3618                             * sizeof (struct dwarf2_per_cu_data *));
3619
3620   while (info_ptr < dwarf2_per_objfile->info.buffer
3621          + dwarf2_per_objfile->info.size)
3622     {
3623       unsigned int length, initial_length_size;
3624       struct dwarf2_per_cu_data *this_cu;
3625       unsigned int offset;
3626
3627       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3628
3629       /* Read just enough information to find out where the next
3630          compilation unit is.  */
3631       length = read_initial_length (objfile->obfd, info_ptr,
3632                                     &initial_length_size);
3633
3634       /* Save the compilation unit for later lookup.  */
3635       this_cu = obstack_alloc (&objfile->objfile_obstack,
3636                                sizeof (struct dwarf2_per_cu_data));
3637       memset (this_cu, 0, sizeof (*this_cu));
3638       this_cu->offset = offset;
3639       this_cu->length = length + initial_length_size;
3640       this_cu->objfile = objfile;
3641
3642       if (n_comp_units == n_allocated)
3643         {
3644           n_allocated *= 2;
3645           all_comp_units = xrealloc (all_comp_units,
3646                                      n_allocated
3647                                      * sizeof (struct dwarf2_per_cu_data *));
3648         }
3649       all_comp_units[n_comp_units++] = this_cu;
3650
3651       info_ptr = info_ptr + this_cu->length;
3652     }
3653
3654   dwarf2_per_objfile->all_comp_units
3655     = obstack_alloc (&objfile->objfile_obstack,
3656                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3657   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3658           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3659   xfree (all_comp_units);
3660   dwarf2_per_objfile->n_comp_units = n_comp_units;
3661 }
3662
3663 /* Process all loaded DIEs for compilation unit CU, starting at
3664    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3665    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3666    DW_AT_ranges).  If NEED_PC is set, then this function will set
3667    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3668    and record the covered ranges in the addrmap.  */
3669
3670 static void
3671 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3672                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3673 {
3674   struct partial_die_info *pdi;
3675
3676   /* Now, march along the PDI's, descending into ones which have
3677      interesting children but skipping the children of the other ones,
3678      until we reach the end of the compilation unit.  */
3679
3680   pdi = first_die;
3681
3682   while (pdi != NULL)
3683     {
3684       fixup_partial_die (pdi, cu);
3685
3686       /* Anonymous namespaces or modules have no name but have interesting
3687          children, so we need to look at them.  Ditto for anonymous
3688          enums.  */
3689
3690       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3691           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3692         {
3693           switch (pdi->tag)
3694             {
3695             case DW_TAG_subprogram:
3696               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3697               break;
3698             case DW_TAG_constant:
3699             case DW_TAG_variable:
3700             case DW_TAG_typedef:
3701             case DW_TAG_union_type:
3702               if (!pdi->is_declaration)
3703                 {
3704                   add_partial_symbol (pdi, cu);
3705                 }
3706               break;
3707             case DW_TAG_class_type:
3708             case DW_TAG_interface_type:
3709             case DW_TAG_structure_type:
3710               if (!pdi->is_declaration)
3711                 {
3712                   add_partial_symbol (pdi, cu);
3713                 }
3714               break;
3715             case DW_TAG_enumeration_type:
3716               if (!pdi->is_declaration)
3717                 add_partial_enumeration (pdi, cu);
3718               break;
3719             case DW_TAG_base_type:
3720             case DW_TAG_subrange_type:
3721               /* File scope base type definitions are added to the partial
3722                  symbol table.  */
3723               add_partial_symbol (pdi, cu);
3724               break;
3725             case DW_TAG_namespace:
3726               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3727               break;
3728             case DW_TAG_module:
3729               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3730               break;
3731             default:
3732               break;
3733             }
3734         }
3735
3736       /* If the die has a sibling, skip to the sibling.  */
3737
3738       pdi = pdi->die_sibling;
3739     }
3740 }
3741
3742 /* Functions used to compute the fully scoped name of a partial DIE.
3743
3744    Normally, this is simple.  For C++, the parent DIE's fully scoped
3745    name is concatenated with "::" and the partial DIE's name.  For
3746    Java, the same thing occurs except that "." is used instead of "::".
3747    Enumerators are an exception; they use the scope of their parent
3748    enumeration type, i.e. the name of the enumeration type is not
3749    prepended to the enumerator.
3750
3751    There are two complexities.  One is DW_AT_specification; in this
3752    case "parent" means the parent of the target of the specification,
3753    instead of the direct parent of the DIE.  The other is compilers
3754    which do not emit DW_TAG_namespace; in this case we try to guess
3755    the fully qualified name of structure types from their members'
3756    linkage names.  This must be done using the DIE's children rather
3757    than the children of any DW_AT_specification target.  We only need
3758    to do this for structures at the top level, i.e. if the target of
3759    any DW_AT_specification (if any; otherwise the DIE itself) does not
3760    have a parent.  */
3761
3762 /* Compute the scope prefix associated with PDI's parent, in
3763    compilation unit CU.  The result will be allocated on CU's
3764    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3765    field.  NULL is returned if no prefix is necessary.  */
3766 static char *
3767 partial_die_parent_scope (struct partial_die_info *pdi,
3768                           struct dwarf2_cu *cu)
3769 {
3770   char *grandparent_scope;
3771   struct partial_die_info *parent, *real_pdi;
3772
3773   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3774      then this means the parent of the specification DIE.  */
3775
3776   real_pdi = pdi;
3777   while (real_pdi->has_specification)
3778     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3779
3780   parent = real_pdi->die_parent;
3781   if (parent == NULL)
3782     return NULL;
3783
3784   if (parent->scope_set)
3785     return parent->scope;
3786
3787   fixup_partial_die (parent, cu);
3788
3789   grandparent_scope = partial_die_parent_scope (parent, cu);
3790
3791   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3792      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3793      Work around this problem here.  */
3794   if (cu->language == language_cplus
3795       && parent->tag == DW_TAG_namespace
3796       && strcmp (parent->name, "::") == 0
3797       && grandparent_scope == NULL)
3798     {
3799       parent->scope = NULL;
3800       parent->scope_set = 1;
3801       return NULL;
3802     }
3803
3804   if (parent->tag == DW_TAG_namespace
3805       || parent->tag == DW_TAG_module
3806       || parent->tag == DW_TAG_structure_type
3807       || parent->tag == DW_TAG_class_type
3808       || parent->tag == DW_TAG_interface_type
3809       || parent->tag == DW_TAG_union_type
3810       || parent->tag == DW_TAG_enumeration_type)
3811     {
3812       if (grandparent_scope == NULL)
3813         parent->scope = parent->name;
3814       else
3815         parent->scope = typename_concat (&cu->comp_unit_obstack,
3816                                          grandparent_scope,
3817                                          parent->name, 0, cu);
3818     }
3819   else if (parent->tag == DW_TAG_enumerator)
3820     /* Enumerators should not get the name of the enumeration as a prefix.  */
3821     parent->scope = grandparent_scope;
3822   else
3823     {
3824       /* FIXME drow/2004-04-01: What should we be doing with
3825          function-local names?  For partial symbols, we should probably be
3826          ignoring them.  */
3827       complaint (&symfile_complaints,
3828                  _("unhandled containing DIE tag %d for DIE at %d"),
3829                  parent->tag, pdi->offset);
3830       parent->scope = grandparent_scope;
3831     }
3832
3833   parent->scope_set = 1;
3834   return parent->scope;
3835 }
3836
3837 /* Return the fully scoped name associated with PDI, from compilation unit
3838    CU.  The result will be allocated with malloc.  */
3839 static char *
3840 partial_die_full_name (struct partial_die_info *pdi,
3841                        struct dwarf2_cu *cu)
3842 {
3843   char *parent_scope;
3844
3845   /* If this is a template instantiation, we can not work out the
3846      template arguments from partial DIEs.  So, unfortunately, we have
3847      to go through the full DIEs.  At least any work we do building
3848      types here will be reused if full symbols are loaded later.  */
3849   if (pdi->has_template_arguments)
3850     {
3851       fixup_partial_die (pdi, cu);
3852
3853       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3854         {
3855           struct die_info *die;
3856           struct attribute attr;
3857           struct dwarf2_cu *ref_cu = cu;
3858
3859           attr.name = 0;
3860           attr.form = DW_FORM_ref_addr;
3861           attr.u.addr = pdi->offset;
3862           die = follow_die_ref (NULL, &attr, &ref_cu);
3863
3864           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3865         }
3866     }
3867
3868   parent_scope = partial_die_parent_scope (pdi, cu);
3869   if (parent_scope == NULL)
3870     return NULL;
3871   else
3872     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3873 }
3874
3875 static void
3876 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3877 {
3878   struct objfile *objfile = cu->objfile;
3879   CORE_ADDR addr = 0;
3880   char *actual_name = NULL;
3881   const struct partial_symbol *psym = NULL;
3882   CORE_ADDR baseaddr;
3883   int built_actual_name = 0;
3884
3885   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3886
3887   actual_name = partial_die_full_name (pdi, cu);
3888   if (actual_name)
3889     built_actual_name = 1;
3890
3891   if (actual_name == NULL)
3892     actual_name = pdi->name;
3893
3894   switch (pdi->tag)
3895     {
3896     case DW_TAG_subprogram:
3897       if (pdi->is_external || cu->language == language_ada)
3898         {
3899           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3900              of the global scope.  But in Ada, we want to be able to access
3901              nested procedures globally.  So all Ada subprograms are stored
3902              in the global scope.  */
3903           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3904              mst_text, objfile); */
3905           add_psymbol_to_list (actual_name, strlen (actual_name),
3906                                built_actual_name,
3907                                VAR_DOMAIN, LOC_BLOCK,
3908                                &objfile->global_psymbols,
3909                                0, pdi->lowpc + baseaddr,
3910                                cu->language, objfile);
3911         }
3912       else
3913         {
3914           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3915              mst_file_text, objfile); */
3916           add_psymbol_to_list (actual_name, strlen (actual_name),
3917                                built_actual_name,
3918                                VAR_DOMAIN, LOC_BLOCK,
3919                                &objfile->static_psymbols,
3920                                0, pdi->lowpc + baseaddr,
3921                                cu->language, objfile);
3922         }
3923       break;
3924     case DW_TAG_constant:
3925       {
3926         struct psymbol_allocation_list *list;
3927
3928         if (pdi->is_external)
3929           list = &objfile->global_psymbols;
3930         else
3931           list = &objfile->static_psymbols;
3932         add_psymbol_to_list (actual_name, strlen (actual_name),
3933                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
3934                              list, 0, 0, cu->language, objfile);
3935       }
3936       break;
3937     case DW_TAG_variable:
3938       if (pdi->locdesc)
3939         addr = decode_locdesc (pdi->locdesc, cu);
3940
3941       if (pdi->locdesc
3942           && addr == 0
3943           && !dwarf2_per_objfile->has_section_at_zero)
3944         {
3945           /* A global or static variable may also have been stripped
3946              out by the linker if unused, in which case its address
3947              will be nullified; do not add such variables into partial
3948              symbol table then.  */
3949         }
3950       else if (pdi->is_external)
3951         {
3952           /* Global Variable.
3953              Don't enter into the minimal symbol tables as there is
3954              a minimal symbol table entry from the ELF symbols already.
3955              Enter into partial symbol table if it has a location
3956              descriptor or a type.
3957              If the location descriptor is missing, new_symbol will create
3958              a LOC_UNRESOLVED symbol, the address of the variable will then
3959              be determined from the minimal symbol table whenever the variable
3960              is referenced.
3961              The address for the partial symbol table entry is not
3962              used by GDB, but it comes in handy for debugging partial symbol
3963              table building.  */
3964
3965           if (pdi->locdesc || pdi->has_type)
3966             add_psymbol_to_list (actual_name, strlen (actual_name),
3967                                  built_actual_name,
3968                                  VAR_DOMAIN, LOC_STATIC,
3969                                  &objfile->global_psymbols,
3970                                  0, addr + baseaddr,
3971                                  cu->language, objfile);
3972         }
3973       else
3974         {
3975           /* Static Variable.  Skip symbols without location descriptors.  */
3976           if (pdi->locdesc == NULL)
3977             {
3978               if (built_actual_name)
3979                 xfree (actual_name);
3980               return;
3981             }
3982           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3983              mst_file_data, objfile); */
3984           add_psymbol_to_list (actual_name, strlen (actual_name),
3985                                built_actual_name,
3986                                VAR_DOMAIN, LOC_STATIC,
3987                                &objfile->static_psymbols,
3988                                0, addr + baseaddr,
3989                                cu->language, objfile);
3990         }
3991       break;
3992     case DW_TAG_typedef:
3993     case DW_TAG_base_type:
3994     case DW_TAG_subrange_type:
3995       add_psymbol_to_list (actual_name, strlen (actual_name),
3996                            built_actual_name,
3997                            VAR_DOMAIN, LOC_TYPEDEF,
3998                            &objfile->static_psymbols,
3999                            0, (CORE_ADDR) 0, cu->language, objfile);
4000       break;
4001     case DW_TAG_namespace:
4002       add_psymbol_to_list (actual_name, strlen (actual_name),
4003                            built_actual_name,
4004                            VAR_DOMAIN, LOC_TYPEDEF,
4005                            &objfile->global_psymbols,
4006                            0, (CORE_ADDR) 0, cu->language, objfile);
4007       break;
4008     case DW_TAG_class_type:
4009     case DW_TAG_interface_type:
4010     case DW_TAG_structure_type:
4011     case DW_TAG_union_type:
4012     case DW_TAG_enumeration_type:
4013       /* Skip external references.  The DWARF standard says in the section
4014          about "Structure, Union, and Class Type Entries": "An incomplete
4015          structure, union or class type is represented by a structure,
4016          union or class entry that does not have a byte size attribute
4017          and that has a DW_AT_declaration attribute."  */
4018       if (!pdi->has_byte_size && pdi->is_declaration)
4019         {
4020           if (built_actual_name)
4021             xfree (actual_name);
4022           return;
4023         }
4024
4025       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4026          static vs. global.  */
4027       add_psymbol_to_list (actual_name, strlen (actual_name),
4028                            built_actual_name,
4029                            STRUCT_DOMAIN, LOC_TYPEDEF,
4030                            (cu->language == language_cplus
4031                             || cu->language == language_java)
4032                            ? &objfile->global_psymbols
4033                            : &objfile->static_psymbols,
4034                            0, (CORE_ADDR) 0, cu->language, objfile);
4035
4036       break;
4037     case DW_TAG_enumerator:
4038       add_psymbol_to_list (actual_name, strlen (actual_name),
4039                            built_actual_name,
4040                            VAR_DOMAIN, LOC_CONST,
4041                            (cu->language == language_cplus
4042                             || cu->language == language_java)
4043                            ? &objfile->global_psymbols
4044                            : &objfile->static_psymbols,
4045                            0, (CORE_ADDR) 0, cu->language, objfile);
4046       break;
4047     default:
4048       break;
4049     }
4050
4051   if (built_actual_name)
4052     xfree (actual_name);
4053 }
4054
4055 /* Read a partial die corresponding to a namespace; also, add a symbol
4056    corresponding to that namespace to the symbol table.  NAMESPACE is
4057    the name of the enclosing namespace.  */
4058
4059 static void
4060 add_partial_namespace (struct partial_die_info *pdi,
4061                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4062                        int need_pc, struct dwarf2_cu *cu)
4063 {
4064   /* Add a symbol for the namespace.  */
4065
4066   add_partial_symbol (pdi, cu);
4067
4068   /* Now scan partial symbols in that namespace.  */
4069
4070   if (pdi->has_children)
4071     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4072 }
4073
4074 /* Read a partial die corresponding to a Fortran module.  */
4075
4076 static void
4077 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4078                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4079 {
4080   /* Now scan partial symbols in that module.  */
4081
4082   if (pdi->has_children)
4083     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4084 }
4085
4086 /* Read a partial die corresponding to a subprogram and create a partial
4087    symbol for that subprogram.  When the CU language allows it, this
4088    routine also defines a partial symbol for each nested subprogram
4089    that this subprogram contains.
4090
4091    DIE my also be a lexical block, in which case we simply search
4092    recursively for suprograms defined inside that lexical block.
4093    Again, this is only performed when the CU language allows this
4094    type of definitions.  */
4095
4096 static void
4097 add_partial_subprogram (struct partial_die_info *pdi,
4098                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4099                         int need_pc, struct dwarf2_cu *cu)
4100 {
4101   if (pdi->tag == DW_TAG_subprogram)
4102     {
4103       if (pdi->has_pc_info)
4104         {
4105           if (pdi->lowpc < *lowpc)
4106             *lowpc = pdi->lowpc;
4107           if (pdi->highpc > *highpc)
4108             *highpc = pdi->highpc;
4109           if (need_pc)
4110             {
4111               CORE_ADDR baseaddr;
4112               struct objfile *objfile = cu->objfile;
4113
4114               baseaddr = ANOFFSET (objfile->section_offsets,
4115                                    SECT_OFF_TEXT (objfile));
4116               addrmap_set_empty (objfile->psymtabs_addrmap,
4117                                  pdi->lowpc + baseaddr,
4118                                  pdi->highpc - 1 + baseaddr,
4119                                  cu->per_cu->v.psymtab);
4120             }
4121           if (!pdi->is_declaration)
4122             /* Ignore subprogram DIEs that do not have a name, they are
4123                illegal.  Do not emit a complaint at this point, we will
4124                do so when we convert this psymtab into a symtab.  */
4125             if (pdi->name)
4126               add_partial_symbol (pdi, cu);
4127         }
4128     }
4129
4130   if (! pdi->has_children)
4131     return;
4132
4133   if (cu->language == language_ada)
4134     {
4135       pdi = pdi->die_child;
4136       while (pdi != NULL)
4137         {
4138           fixup_partial_die (pdi, cu);
4139           if (pdi->tag == DW_TAG_subprogram
4140               || pdi->tag == DW_TAG_lexical_block)
4141             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4142           pdi = pdi->die_sibling;
4143         }
4144     }
4145 }
4146
4147 /* Read a partial die corresponding to an enumeration type.  */
4148
4149 static void
4150 add_partial_enumeration (struct partial_die_info *enum_pdi,
4151                          struct dwarf2_cu *cu)
4152 {
4153   struct partial_die_info *pdi;
4154
4155   if (enum_pdi->name != NULL)
4156     add_partial_symbol (enum_pdi, cu);
4157
4158   pdi = enum_pdi->die_child;
4159   while (pdi)
4160     {
4161       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4162         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4163       else
4164         add_partial_symbol (pdi, cu);
4165       pdi = pdi->die_sibling;
4166     }
4167 }
4168
4169 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4170    Return the corresponding abbrev, or NULL if the number is zero (indicating
4171    an empty DIE).  In either case *BYTES_READ will be set to the length of
4172    the initial number.  */
4173
4174 static struct abbrev_info *
4175 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4176                  struct dwarf2_cu *cu)
4177 {
4178   bfd *abfd = cu->objfile->obfd;
4179   unsigned int abbrev_number;
4180   struct abbrev_info *abbrev;
4181
4182   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4183
4184   if (abbrev_number == 0)
4185     return NULL;
4186
4187   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4188   if (!abbrev)
4189     {
4190       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4191              abbrev_number, bfd_get_filename (abfd));
4192     }
4193
4194   return abbrev;
4195 }
4196
4197 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4198    Returns a pointer to the end of a series of DIEs, terminated by an empty
4199    DIE.  Any children of the skipped DIEs will also be skipped.  */
4200
4201 static gdb_byte *
4202 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4203 {
4204   struct abbrev_info *abbrev;
4205   unsigned int bytes_read;
4206
4207   while (1)
4208     {
4209       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4210       if (abbrev == NULL)
4211         return info_ptr + bytes_read;
4212       else
4213         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4214     }
4215 }
4216
4217 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4218    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4219    abbrev corresponding to that skipped uleb128 should be passed in
4220    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4221    children.  */
4222
4223 static gdb_byte *
4224 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4225               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4226 {
4227   unsigned int bytes_read;
4228   struct attribute attr;
4229   bfd *abfd = cu->objfile->obfd;
4230   unsigned int form, i;
4231
4232   for (i = 0; i < abbrev->num_attrs; i++)
4233     {
4234       /* The only abbrev we care about is DW_AT_sibling.  */
4235       if (abbrev->attrs[i].name == DW_AT_sibling)
4236         {
4237           read_attribute (&attr, &abbrev->attrs[i],
4238                           abfd, info_ptr, cu);
4239           if (attr.form == DW_FORM_ref_addr)
4240             complaint (&symfile_complaints,
4241                        _("ignoring absolute DW_AT_sibling"));
4242           else
4243             return buffer + dwarf2_get_ref_die_offset (&attr);
4244         }
4245
4246       /* If it isn't DW_AT_sibling, skip this attribute.  */
4247       form = abbrev->attrs[i].form;
4248     skip_attribute:
4249       switch (form)
4250         {
4251         case DW_FORM_ref_addr:
4252           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4253              and later it is offset sized.  */
4254           if (cu->header.version == 2)
4255             info_ptr += cu->header.addr_size;
4256           else
4257             info_ptr += cu->header.offset_size;
4258           break;
4259         case DW_FORM_addr:
4260           info_ptr += cu->header.addr_size;
4261           break;
4262         case DW_FORM_data1:
4263         case DW_FORM_ref1:
4264         case DW_FORM_flag:
4265           info_ptr += 1;
4266           break;
4267         case DW_FORM_flag_present:
4268           break;
4269         case DW_FORM_data2:
4270         case DW_FORM_ref2:
4271           info_ptr += 2;
4272           break;
4273         case DW_FORM_data4:
4274         case DW_FORM_ref4:
4275           info_ptr += 4;
4276           break;
4277         case DW_FORM_data8:
4278         case DW_FORM_ref8:
4279         case DW_FORM_ref_sig8:
4280           info_ptr += 8;
4281           break;
4282         case DW_FORM_string:
4283           read_direct_string (abfd, info_ptr, &bytes_read);
4284           info_ptr += bytes_read;
4285           break;
4286         case DW_FORM_sec_offset:
4287         case DW_FORM_strp:
4288           info_ptr += cu->header.offset_size;
4289           break;
4290         case DW_FORM_exprloc:
4291         case DW_FORM_block:
4292           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4293           info_ptr += bytes_read;
4294           break;
4295         case DW_FORM_block1:
4296           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4297           break;
4298         case DW_FORM_block2:
4299           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4300           break;
4301         case DW_FORM_block4:
4302           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4303           break;
4304         case DW_FORM_sdata:
4305         case DW_FORM_udata:
4306         case DW_FORM_ref_udata:
4307           info_ptr = skip_leb128 (abfd, info_ptr);
4308           break;
4309         case DW_FORM_indirect:
4310           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4311           info_ptr += bytes_read;
4312           /* We need to continue parsing from here, so just go back to
4313              the top.  */
4314           goto skip_attribute;
4315
4316         default:
4317           error (_("Dwarf Error: Cannot handle %s "
4318                    "in DWARF reader [in module %s]"),
4319                  dwarf_form_name (form),
4320                  bfd_get_filename (abfd));
4321         }
4322     }
4323
4324   if (abbrev->has_children)
4325     return skip_children (buffer, info_ptr, cu);
4326   else
4327     return info_ptr;
4328 }
4329
4330 /* Locate ORIG_PDI's sibling.
4331    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4332    in BUFFER.  */
4333
4334 static gdb_byte *
4335 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4336                     gdb_byte *buffer, gdb_byte *info_ptr,
4337                     bfd *abfd, struct dwarf2_cu *cu)
4338 {
4339   /* Do we know the sibling already?  */
4340
4341   if (orig_pdi->sibling)
4342     return orig_pdi->sibling;
4343
4344   /* Are there any children to deal with?  */
4345
4346   if (!orig_pdi->has_children)
4347     return info_ptr;
4348
4349   /* Skip the children the long way.  */
4350
4351   return skip_children (buffer, info_ptr, cu);
4352 }
4353
4354 /* Expand this partial symbol table into a full symbol table.  */
4355
4356 static void
4357 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4358 {
4359   if (pst != NULL)
4360     {
4361       if (pst->readin)
4362         {
4363           warning (_("bug: psymtab for %s is already read in."),
4364                    pst->filename);
4365         }
4366       else
4367         {
4368           if (info_verbose)
4369             {
4370               printf_filtered (_("Reading in symbols for %s..."),
4371                                pst->filename);
4372               gdb_flush (gdb_stdout);
4373             }
4374
4375           /* Restore our global data.  */
4376           dwarf2_per_objfile = objfile_data (pst->objfile,
4377                                              dwarf2_objfile_data_key);
4378
4379           /* If this psymtab is constructed from a debug-only objfile, the
4380              has_section_at_zero flag will not necessarily be correct.  We
4381              can get the correct value for this flag by looking at the data
4382              associated with the (presumably stripped) associated objfile.  */
4383           if (pst->objfile->separate_debug_objfile_backlink)
4384             {
4385               struct dwarf2_per_objfile *dpo_backlink
4386                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4387                                 dwarf2_objfile_data_key);
4388
4389               dwarf2_per_objfile->has_section_at_zero
4390                 = dpo_backlink->has_section_at_zero;
4391             }
4392
4393           dwarf2_per_objfile->reading_partial_symbols = 0;
4394
4395           psymtab_to_symtab_1 (pst);
4396
4397           /* Finish up the debug error message.  */
4398           if (info_verbose)
4399             printf_filtered (_("done.\n"));
4400         }
4401     }
4402 }
4403
4404 /* Add PER_CU to the queue.  */
4405
4406 static void
4407 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4408 {
4409   struct dwarf2_queue_item *item;
4410
4411   per_cu->queued = 1;
4412   item = xmalloc (sizeof (*item));
4413   item->per_cu = per_cu;
4414   item->next = NULL;
4415
4416   if (dwarf2_queue == NULL)
4417     dwarf2_queue = item;
4418   else
4419     dwarf2_queue_tail->next = item;
4420
4421   dwarf2_queue_tail = item;
4422 }
4423
4424 /* Process the queue.  */
4425
4426 static void
4427 process_queue (struct objfile *objfile)
4428 {
4429   struct dwarf2_queue_item *item, *next_item;
4430
4431   /* The queue starts out with one item, but following a DIE reference
4432      may load a new CU, adding it to the end of the queue.  */
4433   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4434     {
4435       if (dwarf2_per_objfile->using_index
4436           ? !item->per_cu->v.quick->symtab
4437           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4438         process_full_comp_unit (item->per_cu);
4439
4440       item->per_cu->queued = 0;
4441       next_item = item->next;
4442       xfree (item);
4443     }
4444
4445   dwarf2_queue_tail = NULL;
4446 }
4447
4448 /* Free all allocated queue entries.  This function only releases anything if
4449    an error was thrown; if the queue was processed then it would have been
4450    freed as we went along.  */
4451
4452 static void
4453 dwarf2_release_queue (void *dummy)
4454 {
4455   struct dwarf2_queue_item *item, *last;
4456
4457   item = dwarf2_queue;
4458   while (item)
4459     {
4460       /* Anything still marked queued is likely to be in an
4461          inconsistent state, so discard it.  */
4462       if (item->per_cu->queued)
4463         {
4464           if (item->per_cu->cu != NULL)
4465             free_one_cached_comp_unit (item->per_cu->cu);
4466           item->per_cu->queued = 0;
4467         }
4468
4469       last = item;
4470       item = item->next;
4471       xfree (last);
4472     }
4473
4474   dwarf2_queue = dwarf2_queue_tail = NULL;
4475 }
4476
4477 /* Read in full symbols for PST, and anything it depends on.  */
4478
4479 static void
4480 psymtab_to_symtab_1 (struct partial_symtab *pst)
4481 {
4482   struct dwarf2_per_cu_data *per_cu;
4483   struct cleanup *back_to;
4484   int i;
4485
4486   for (i = 0; i < pst->number_of_dependencies; i++)
4487     if (!pst->dependencies[i]->readin)
4488       {
4489         /* Inform about additional files that need to be read in.  */
4490         if (info_verbose)
4491           {
4492             /* FIXME: i18n: Need to make this a single string.  */
4493             fputs_filtered (" ", gdb_stdout);
4494             wrap_here ("");
4495             fputs_filtered ("and ", gdb_stdout);
4496             wrap_here ("");
4497             printf_filtered ("%s...", pst->dependencies[i]->filename);
4498             wrap_here ("");     /* Flush output.  */
4499             gdb_flush (gdb_stdout);
4500           }
4501         psymtab_to_symtab_1 (pst->dependencies[i]);
4502       }
4503
4504   per_cu = pst->read_symtab_private;
4505
4506   if (per_cu == NULL)
4507     {
4508       /* It's an include file, no symbols to read for it.
4509          Everything is in the parent symtab.  */
4510       pst->readin = 1;
4511       return;
4512     }
4513
4514   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4515 }
4516
4517 /* Load the DIEs associated with PER_CU into memory.  */
4518
4519 static void
4520 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4521                      struct objfile *objfile)
4522 {
4523   bfd *abfd = objfile->obfd;
4524   struct dwarf2_cu *cu;
4525   unsigned int offset;
4526   gdb_byte *info_ptr, *beg_of_comp_unit;
4527   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4528   struct attribute *attr;
4529   int read_cu = 0;
4530
4531   gdb_assert (! per_cu->from_debug_types);
4532
4533   /* Set local variables from the partial symbol table info.  */
4534   offset = per_cu->offset;
4535
4536   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4537   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4538   beg_of_comp_unit = info_ptr;
4539
4540   if (per_cu->cu == NULL)
4541     {
4542       cu = xmalloc (sizeof (*cu));
4543       init_one_comp_unit (cu, objfile);
4544
4545       read_cu = 1;
4546
4547       /* If an error occurs while loading, release our storage.  */
4548       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4549
4550       /* Read in the comp_unit header.  */
4551       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4552
4553       /* Complete the cu_header.  */
4554       cu->header.offset = offset;
4555       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4556
4557       /* Read the abbrevs for this compilation unit.  */
4558       dwarf2_read_abbrevs (abfd, cu);
4559       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4560
4561       /* Link this compilation unit into the compilation unit tree.  */
4562       per_cu->cu = cu;
4563       cu->per_cu = per_cu;
4564
4565       /* Link this CU into read_in_chain.  */
4566       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4567       dwarf2_per_objfile->read_in_chain = per_cu;
4568     }
4569   else
4570     {
4571       cu = per_cu->cu;
4572       info_ptr += cu->header.first_die_offset;
4573     }
4574
4575   cu->dies = read_comp_unit (info_ptr, cu);
4576
4577   /* We try not to read any attributes in this function, because not
4578      all objfiles needed for references have been loaded yet, and symbol
4579      table processing isn't initialized.  But we have to set the CU language,
4580      or we won't be able to build types correctly.  */
4581   prepare_one_comp_unit (cu, cu->dies);
4582
4583   /* Similarly, if we do not read the producer, we can not apply
4584      producer-specific interpretation.  */
4585   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4586   if (attr)
4587     cu->producer = DW_STRING (attr);
4588
4589   if (read_cu)
4590     {
4591       do_cleanups (free_abbrevs_cleanup);
4592
4593       /* We've successfully allocated this compilation unit.  Let our
4594          caller clean it up when finished with it.  */
4595       discard_cleanups (free_cu_cleanup);
4596     }
4597 }
4598
4599 /* Add a DIE to the delayed physname list.  */
4600
4601 static void
4602 add_to_method_list (struct type *type, int fnfield_index, int index,
4603                     const char *name, struct die_info *die,
4604                     struct dwarf2_cu *cu)
4605 {
4606   struct delayed_method_info mi;
4607   mi.type = type;
4608   mi.fnfield_index = fnfield_index;
4609   mi.index = index;
4610   mi.name = name;
4611   mi.die = die;
4612   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4613 }
4614
4615 /* A cleanup for freeing the delayed method list.  */
4616
4617 static void
4618 free_delayed_list (void *ptr)
4619 {
4620   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4621   if (cu->method_list != NULL)
4622     {
4623       VEC_free (delayed_method_info, cu->method_list);
4624       cu->method_list = NULL;
4625     }
4626 }
4627
4628 /* Compute the physnames of any methods on the CU's method list.
4629
4630    The computation of method physnames is delayed in order to avoid the
4631    (bad) condition that one of the method's formal parameters is of an as yet
4632    incomplete type.  */
4633
4634 static void
4635 compute_delayed_physnames (struct dwarf2_cu *cu)
4636 {
4637   int i;
4638   struct delayed_method_info *mi;
4639   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4640     {
4641       const char *physname;
4642       struct fn_fieldlist *fn_flp
4643         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4644       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4645       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4646     }
4647 }
4648
4649 /* Check for GCC >= 4.0.  */
4650
4651 static int
4652 producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4653 {
4654   const char *cs;
4655   int major, minor;
4656
4657   if (cu->producer == NULL)
4658     {
4659       /* For unknown compilers expect their behavior is not compliant.  For GCC
4660          this case can also happen for -gdwarf-4 type units supported since
4661          gcc-4.5.  */
4662
4663       return 0;
4664     }
4665
4666   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
4667
4668   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4669     {
4670       /* For non-GCC compilers expect their behavior is not compliant.  */
4671
4672       return 0;
4673     }
4674   cs = &cu->producer[strlen ("GNU ")];
4675   while (*cs && !isdigit (*cs))
4676     cs++;
4677   if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4678     {
4679       /* Not recognized as GCC.  */
4680
4681       return 0;
4682     }
4683
4684   return major >= 4;
4685 }
4686
4687 /* Generate full symbol information for PST and CU, whose DIEs have
4688    already been loaded into memory.  */
4689
4690 static void
4691 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4692 {
4693   struct dwarf2_cu *cu = per_cu->cu;
4694   struct objfile *objfile = per_cu->objfile;
4695   CORE_ADDR lowpc, highpc;
4696   struct symtab *symtab;
4697   struct cleanup *back_to, *delayed_list_cleanup;
4698   CORE_ADDR baseaddr;
4699
4700   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4701
4702   buildsym_init ();
4703   back_to = make_cleanup (really_free_pendings, NULL);
4704   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4705
4706   cu->list_in_scope = &file_symbols;
4707
4708   dwarf2_find_base_address (cu->dies, cu);
4709
4710   /* Do line number decoding in read_file_scope () */
4711   process_die (cu->dies, cu);
4712
4713   /* Now that we have processed all the DIEs in the CU, all the types 
4714      should be complete, and it should now be safe to compute all of the
4715      physnames.  */
4716   compute_delayed_physnames (cu);
4717   do_cleanups (delayed_list_cleanup);
4718
4719   /* Some compilers don't define a DW_AT_high_pc attribute for the
4720      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4721      it, by scanning the DIE's below the compilation unit.  */
4722   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4723
4724   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4725
4726   if (symtab != NULL)
4727     {
4728       /* Set symtab language to language from DW_AT_language.  If the
4729          compilation is from a C file generated by language preprocessors, do
4730          not set the language if it was already deduced by start_subfile.  */
4731       if (!(cu->language == language_c && symtab->language != language_c))
4732         symtab->language = cu->language;
4733
4734       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4735          produce DW_AT_location with location lists but it can be possibly
4736          invalid without -fvar-tracking.
4737
4738          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4739          needed, it would be wrong due to missing DW_AT_producer there.
4740
4741          Still one can confuse GDB by using non-standard GCC compilation
4742          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4743          */ 
4744       if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4745         symtab->locations_valid = 1;
4746     }
4747
4748   if (dwarf2_per_objfile->using_index)
4749     per_cu->v.quick->symtab = symtab;
4750   else
4751     {
4752       struct partial_symtab *pst = per_cu->v.psymtab;
4753       pst->symtab = symtab;
4754       pst->readin = 1;
4755     }
4756
4757   do_cleanups (back_to);
4758 }
4759
4760 /* Process a die and its children.  */
4761
4762 static void
4763 process_die (struct die_info *die, struct dwarf2_cu *cu)
4764 {
4765   switch (die->tag)
4766     {
4767     case DW_TAG_padding:
4768       break;
4769     case DW_TAG_compile_unit:
4770       read_file_scope (die, cu);
4771       break;
4772     case DW_TAG_type_unit:
4773       read_type_unit_scope (die, cu);
4774       break;
4775     case DW_TAG_subprogram:
4776     case DW_TAG_inlined_subroutine:
4777       read_func_scope (die, cu);
4778       break;
4779     case DW_TAG_lexical_block:
4780     case DW_TAG_try_block:
4781     case DW_TAG_catch_block:
4782       read_lexical_block_scope (die, cu);
4783       break;
4784     case DW_TAG_class_type:
4785     case DW_TAG_interface_type:
4786     case DW_TAG_structure_type:
4787     case DW_TAG_union_type:
4788       process_structure_scope (die, cu);
4789       break;
4790     case DW_TAG_enumeration_type:
4791       process_enumeration_scope (die, cu);
4792       break;
4793
4794     /* These dies have a type, but processing them does not create
4795        a symbol or recurse to process the children.  Therefore we can
4796        read them on-demand through read_type_die.  */
4797     case DW_TAG_subroutine_type:
4798     case DW_TAG_set_type:
4799     case DW_TAG_array_type:
4800     case DW_TAG_pointer_type:
4801     case DW_TAG_ptr_to_member_type:
4802     case DW_TAG_reference_type:
4803     case DW_TAG_string_type:
4804       break;
4805
4806     case DW_TAG_base_type:
4807     case DW_TAG_subrange_type:
4808     case DW_TAG_typedef:
4809       /* Add a typedef symbol for the type definition, if it has a
4810          DW_AT_name.  */
4811       new_symbol (die, read_type_die (die, cu), cu);
4812       break;
4813     case DW_TAG_common_block:
4814       read_common_block (die, cu);
4815       break;
4816     case DW_TAG_common_inclusion:
4817       break;
4818     case DW_TAG_namespace:
4819       processing_has_namespace_info = 1;
4820       read_namespace (die, cu);
4821       break;
4822     case DW_TAG_module:
4823       processing_has_namespace_info = 1;
4824       read_module (die, cu);
4825       break;
4826     case DW_TAG_imported_declaration:
4827     case DW_TAG_imported_module:
4828       processing_has_namespace_info = 1;
4829       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4830                                  || cu->language != language_fortran))
4831         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4832                    dwarf_tag_name (die->tag));
4833       read_import_statement (die, cu);
4834       break;
4835     default:
4836       new_symbol (die, NULL, cu);
4837       break;
4838     }
4839 }
4840
4841 /* A helper function for dwarf2_compute_name which determines whether DIE
4842    needs to have the name of the scope prepended to the name listed in the
4843    die.  */
4844
4845 static int
4846 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4847 {
4848   struct attribute *attr;
4849
4850   switch (die->tag)
4851     {
4852     case DW_TAG_namespace:
4853     case DW_TAG_typedef:
4854     case DW_TAG_class_type:
4855     case DW_TAG_interface_type:
4856     case DW_TAG_structure_type:
4857     case DW_TAG_union_type:
4858     case DW_TAG_enumeration_type:
4859     case DW_TAG_enumerator:
4860     case DW_TAG_subprogram:
4861     case DW_TAG_member:
4862       return 1;
4863
4864     case DW_TAG_variable:
4865     case DW_TAG_constant:
4866       /* We only need to prefix "globally" visible variables.  These include
4867          any variable marked with DW_AT_external or any variable that
4868          lives in a namespace.  [Variables in anonymous namespaces
4869          require prefixing, but they are not DW_AT_external.]  */
4870
4871       if (dwarf2_attr (die, DW_AT_specification, cu))
4872         {
4873           struct dwarf2_cu *spec_cu = cu;
4874
4875           return die_needs_namespace (die_specification (die, &spec_cu),
4876                                       spec_cu);
4877         }
4878
4879       attr = dwarf2_attr (die, DW_AT_external, cu);
4880       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4881           && die->parent->tag != DW_TAG_module)
4882         return 0;
4883       /* A variable in a lexical block of some kind does not need a
4884          namespace, even though in C++ such variables may be external
4885          and have a mangled name.  */
4886       if (die->parent->tag ==  DW_TAG_lexical_block
4887           || die->parent->tag ==  DW_TAG_try_block
4888           || die->parent->tag ==  DW_TAG_catch_block
4889           || die->parent->tag == DW_TAG_subprogram)
4890         return 0;
4891       return 1;
4892
4893     default:
4894       return 0;
4895     }
4896 }
4897
4898 /* Retrieve the last character from a mem_file.  */
4899
4900 static void
4901 do_ui_file_peek_last (void *object, const char *buffer, long length)
4902 {
4903   char *last_char_p = (char *) object;
4904
4905   if (length > 0)
4906     *last_char_p = buffer[length - 1];
4907 }
4908
4909 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4910    compute the physname for the object, which include a method's
4911    formal parameters (C++/Java) and return type (Java).
4912
4913    For Ada, return the DIE's linkage name rather than the fully qualified
4914    name.  PHYSNAME is ignored..
4915
4916    The result is allocated on the objfile_obstack and canonicalized.  */
4917
4918 static const char *
4919 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4920                      int physname)
4921 {
4922   if (name == NULL)
4923     name = dwarf2_name (die, cu);
4924
4925   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4926      compute it by typename_concat inside GDB.  */
4927   if (cu->language == language_ada
4928       || (cu->language == language_fortran && physname))
4929     {
4930       /* For Ada unit, we prefer the linkage name over the name, as
4931          the former contains the exported name, which the user expects
4932          to be able to reference.  Ideally, we want the user to be able
4933          to reference this entity using either natural or linkage name,
4934          but we haven't started looking at this enhancement yet.  */
4935       struct attribute *attr;
4936
4937       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4938       if (attr == NULL)
4939         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4940       if (attr && DW_STRING (attr))
4941         return DW_STRING (attr);
4942     }
4943
4944   /* These are the only languages we know how to qualify names in.  */
4945   if (name != NULL
4946       && (cu->language == language_cplus || cu->language == language_java
4947           || cu->language == language_fortran))
4948     {
4949       if (die_needs_namespace (die, cu))
4950         {
4951           long length;
4952           char *prefix;
4953           struct ui_file *buf;
4954
4955           prefix = determine_prefix (die, cu);
4956           buf = mem_fileopen ();
4957           if (*prefix != '\0')
4958             {
4959               char *prefixed_name = typename_concat (NULL, prefix, name,
4960                                                      physname, cu);
4961
4962               fputs_unfiltered (prefixed_name, buf);
4963               xfree (prefixed_name);
4964             }
4965           else
4966             fputs_unfiltered (name, buf);
4967
4968           /* Template parameters may be specified in the DIE's DW_AT_name, or
4969              as children with DW_TAG_template_type_param or
4970              DW_TAG_value_type_param.  If the latter, add them to the name
4971              here.  If the name already has template parameters, then
4972              skip this step; some versions of GCC emit both, and
4973              it is more efficient to use the pre-computed name.
4974
4975              Something to keep in mind about this process: it is very
4976              unlikely, or in some cases downright impossible, to produce
4977              something that will match the mangled name of a function.
4978              If the definition of the function has the same debug info,
4979              we should be able to match up with it anyway.  But fallbacks
4980              using the minimal symbol, for instance to find a method
4981              implemented in a stripped copy of libstdc++, will not work.
4982              If we do not have debug info for the definition, we will have to
4983              match them up some other way.
4984
4985              When we do name matching there is a related problem with function
4986              templates; two instantiated function templates are allowed to
4987              differ only by their return types, which we do not add here.  */
4988
4989           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4990             {
4991               struct attribute *attr;
4992               struct die_info *child;
4993               int first = 1;
4994
4995               die->building_fullname = 1;
4996
4997               for (child = die->child; child != NULL; child = child->sibling)
4998                 {
4999                   struct type *type;
5000                   long value;
5001                   gdb_byte *bytes;
5002                   struct dwarf2_locexpr_baton *baton;
5003                   struct value *v;
5004
5005                   if (child->tag != DW_TAG_template_type_param
5006                       && child->tag != DW_TAG_template_value_param)
5007                     continue;
5008
5009                   if (first)
5010                     {
5011                       fputs_unfiltered ("<", buf);
5012                       first = 0;
5013                     }
5014                   else
5015                     fputs_unfiltered (", ", buf);
5016
5017                   attr = dwarf2_attr (child, DW_AT_type, cu);
5018                   if (attr == NULL)
5019                     {
5020                       complaint (&symfile_complaints,
5021                                  _("template parameter missing DW_AT_type"));
5022                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5023                       continue;
5024                     }
5025                   type = die_type (child, cu);
5026
5027                   if (child->tag == DW_TAG_template_type_param)
5028                     {
5029                       c_print_type (type, "", buf, -1, 0);
5030                       continue;
5031                     }
5032
5033                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5034                   if (attr == NULL)
5035                     {
5036                       complaint (&symfile_complaints,
5037                                  _("template parameter missing "
5038                                    "DW_AT_const_value"));
5039                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5040                       continue;
5041                     }
5042
5043                   dwarf2_const_value_attr (attr, type, name,
5044                                            &cu->comp_unit_obstack, cu,
5045                                            &value, &bytes, &baton);
5046
5047                   if (TYPE_NOSIGN (type))
5048                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5049                        changed, this can use value_print instead.  */
5050                     c_printchar (value, type, buf);
5051                   else
5052                     {
5053                       struct value_print_options opts;
5054
5055                       if (baton != NULL)
5056                         v = dwarf2_evaluate_loc_desc (type, NULL,
5057                                                       baton->data,
5058                                                       baton->size,
5059                                                       baton->per_cu);
5060                       else if (bytes != NULL)
5061                         {
5062                           v = allocate_value (type);
5063                           memcpy (value_contents_writeable (v), bytes,
5064                                   TYPE_LENGTH (type));
5065                         }
5066                       else
5067                         v = value_from_longest (type, value);
5068
5069                       /* Specify decimal so that we do not depend on
5070                          the radix.  */
5071                       get_formatted_print_options (&opts, 'd');
5072                       opts.raw = 1;
5073                       value_print (v, buf, &opts);
5074                       release_value (v);
5075                       value_free (v);
5076                     }
5077                 }
5078
5079               die->building_fullname = 0;
5080
5081               if (!first)
5082                 {
5083                   /* Close the argument list, with a space if necessary
5084                      (nested templates).  */
5085                   char last_char = '\0';
5086                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5087                   if (last_char == '>')
5088                     fputs_unfiltered (" >", buf);
5089                   else
5090                     fputs_unfiltered (">", buf);
5091                 }
5092             }
5093
5094           /* For Java and C++ methods, append formal parameter type
5095              information, if PHYSNAME.  */
5096
5097           if (physname && die->tag == DW_TAG_subprogram
5098               && (cu->language == language_cplus
5099                   || cu->language == language_java))
5100             {
5101               struct type *type = read_type_die (die, cu);
5102
5103               c_type_print_args (type, buf, 1, cu->language);
5104
5105               if (cu->language == language_java)
5106                 {
5107                   /* For java, we must append the return type to method
5108                      names.  */
5109                   if (die->tag == DW_TAG_subprogram)
5110                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5111                                      0, 0);
5112                 }
5113               else if (cu->language == language_cplus)
5114                 {
5115                   /* Assume that an artificial first parameter is
5116                      "this", but do not crash if it is not.  RealView
5117                      marks unnamed (and thus unused) parameters as
5118                      artificial; there is no way to differentiate
5119                      the two cases.  */
5120                   if (TYPE_NFIELDS (type) > 0
5121                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5122                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5123                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5124                                                                         0))))
5125                     fputs_unfiltered (" const", buf);
5126                 }
5127             }
5128
5129           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5130                                        &length);
5131           ui_file_delete (buf);
5132
5133           if (cu->language == language_cplus)
5134             {
5135               char *cname
5136                 = dwarf2_canonicalize_name (name, cu,
5137                                             &cu->objfile->objfile_obstack);
5138
5139               if (cname != NULL)
5140                 name = cname;
5141             }
5142         }
5143     }
5144
5145   return name;
5146 }
5147
5148 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5149    If scope qualifiers are appropriate they will be added.  The result
5150    will be allocated on the objfile_obstack, or NULL if the DIE does
5151    not have a name.  NAME may either be from a previous call to
5152    dwarf2_name or NULL.
5153
5154    The output string will be canonicalized (if C++/Java).  */
5155
5156 static const char *
5157 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5158 {
5159   return dwarf2_compute_name (name, die, cu, 0);
5160 }
5161
5162 /* Construct a physname for the given DIE in CU.  NAME may either be
5163    from a previous call to dwarf2_name or NULL.  The result will be
5164    allocated on the objfile_objstack or NULL if the DIE does not have a
5165    name.
5166
5167    The output string will be canonicalized (if C++/Java).  */
5168
5169 static const char *
5170 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5171 {
5172   return dwarf2_compute_name (name, die, cu, 1);
5173 }
5174
5175 /* Read the import statement specified by the given die and record it.  */
5176
5177 static void
5178 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5179 {
5180   struct attribute *import_attr;
5181   struct die_info *imported_die;
5182   struct dwarf2_cu *imported_cu;
5183   const char *imported_name;
5184   const char *imported_name_prefix;
5185   const char *canonical_name;
5186   const char *import_alias;
5187   const char *imported_declaration = NULL;
5188   const char *import_prefix;
5189
5190   char *temp;
5191
5192   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5193   if (import_attr == NULL)
5194     {
5195       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5196                  dwarf_tag_name (die->tag));
5197       return;
5198     }
5199
5200   imported_cu = cu;
5201   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5202   imported_name = dwarf2_name (imported_die, imported_cu);
5203   if (imported_name == NULL)
5204     {
5205       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5206
5207         The import in the following code:
5208         namespace A
5209           {
5210             typedef int B;
5211           }
5212
5213         int main ()
5214           {
5215             using A::B;
5216             B b;
5217             return b;
5218           }
5219
5220         ...
5221          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5222             <52>   DW_AT_decl_file   : 1
5223             <53>   DW_AT_decl_line   : 6
5224             <54>   DW_AT_import      : <0x75>
5225          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5226             <59>   DW_AT_name        : B
5227             <5b>   DW_AT_decl_file   : 1
5228             <5c>   DW_AT_decl_line   : 2
5229             <5d>   DW_AT_type        : <0x6e>
5230         ...
5231          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5232             <76>   DW_AT_byte_size   : 4
5233             <77>   DW_AT_encoding    : 5        (signed)
5234
5235         imports the wrong die ( 0x75 instead of 0x58 ).
5236         This case will be ignored until the gcc bug is fixed.  */
5237       return;
5238     }
5239
5240   /* Figure out the local name after import.  */
5241   import_alias = dwarf2_name (die, cu);
5242
5243   /* Figure out where the statement is being imported to.  */
5244   import_prefix = determine_prefix (die, cu);
5245
5246   /* Figure out what the scope of the imported die is and prepend it
5247      to the name of the imported die.  */
5248   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5249
5250   if (imported_die->tag != DW_TAG_namespace
5251       && imported_die->tag != DW_TAG_module)
5252     {
5253       imported_declaration = imported_name;
5254       canonical_name = imported_name_prefix;
5255     }
5256   else if (strlen (imported_name_prefix) > 0)
5257     {
5258       temp = alloca (strlen (imported_name_prefix)
5259                      + 2 + strlen (imported_name) + 1);
5260       strcpy (temp, imported_name_prefix);
5261       strcat (temp, "::");
5262       strcat (temp, imported_name);
5263       canonical_name = temp;
5264     }
5265   else
5266     canonical_name = imported_name;
5267
5268   cp_add_using_directive (import_prefix,
5269                           canonical_name,
5270                           import_alias,
5271                           imported_declaration,
5272                           &cu->objfile->objfile_obstack);
5273 }
5274
5275 static void
5276 initialize_cu_func_list (struct dwarf2_cu *cu)
5277 {
5278   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5279 }
5280
5281 /* Cleanup function for read_file_scope.  */
5282
5283 static void
5284 free_cu_line_header (void *arg)
5285 {
5286   struct dwarf2_cu *cu = arg;
5287
5288   free_line_header (cu->line_header);
5289   cu->line_header = NULL;
5290 }
5291
5292 static void
5293 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5294                          char **name, char **comp_dir)
5295 {
5296   struct attribute *attr;
5297
5298   *name = NULL;
5299   *comp_dir = NULL;
5300
5301   /* Find the filename.  Do not use dwarf2_name here, since the filename
5302      is not a source language identifier.  */
5303   attr = dwarf2_attr (die, DW_AT_name, cu);
5304   if (attr)
5305     {
5306       *name = DW_STRING (attr);
5307     }
5308
5309   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5310   if (attr)
5311     *comp_dir = DW_STRING (attr);
5312   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5313     {
5314       *comp_dir = ldirname (*name);
5315       if (*comp_dir != NULL)
5316         make_cleanup (xfree, *comp_dir);
5317     }
5318   if (*comp_dir != NULL)
5319     {
5320       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5321          directory, get rid of it.  */
5322       char *cp = strchr (*comp_dir, ':');
5323
5324       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5325         *comp_dir = cp + 1;
5326     }
5327
5328   if (*name == NULL)
5329     *name = "<unknown>";
5330 }
5331
5332 /* Process DW_TAG_compile_unit.  */
5333
5334 static void
5335 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5336 {
5337   struct objfile *objfile = cu->objfile;
5338   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5339   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5340   CORE_ADDR highpc = ((CORE_ADDR) 0);
5341   struct attribute *attr;
5342   char *name = NULL;
5343   char *comp_dir = NULL;
5344   struct die_info *child_die;
5345   bfd *abfd = objfile->obfd;
5346   struct line_header *line_header = 0;
5347   CORE_ADDR baseaddr;
5348
5349   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5350
5351   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5352
5353   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5354      from finish_block.  */
5355   if (lowpc == ((CORE_ADDR) -1))
5356     lowpc = highpc;
5357   lowpc += baseaddr;
5358   highpc += baseaddr;
5359
5360   find_file_and_directory (die, cu, &name, &comp_dir);
5361
5362   attr = dwarf2_attr (die, DW_AT_language, cu);
5363   if (attr)
5364     {
5365       set_cu_language (DW_UNSND (attr), cu);
5366     }
5367
5368   attr = dwarf2_attr (die, DW_AT_producer, cu);
5369   if (attr)
5370     cu->producer = DW_STRING (attr);
5371
5372   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5373      standardised yet.  As a workaround for the language detection we fall
5374      back to the DW_AT_producer string.  */
5375   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5376     cu->language = language_opencl;
5377
5378   /* We assume that we're processing GCC output.  */
5379   processing_gcc_compilation = 2;
5380
5381   processing_has_namespace_info = 0;
5382
5383   start_symtab (name, comp_dir, lowpc);
5384   record_debugformat ("DWARF 2");
5385   record_producer (cu->producer);
5386
5387   initialize_cu_func_list (cu);
5388
5389   /* Decode line number information if present.  We do this before
5390      processing child DIEs, so that the line header table is available
5391      for DW_AT_decl_file.  */
5392   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5393   if (attr)
5394     {
5395       unsigned int line_offset = DW_UNSND (attr);
5396       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5397       if (line_header)
5398         {
5399           cu->line_header = line_header;
5400           make_cleanup (free_cu_line_header, cu);
5401           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5402         }
5403     }
5404
5405   /* Process all dies in compilation unit.  */
5406   if (die->child != NULL)
5407     {
5408       child_die = die->child;
5409       while (child_die && child_die->tag)
5410         {
5411           process_die (child_die, cu);
5412           child_die = sibling_die (child_die);
5413         }
5414     }
5415
5416   /* Decode macro information, if present.  Dwarf 2 macro information
5417      refers to information in the line number info statement program
5418      header, so we can only read it if we've read the header
5419      successfully.  */
5420   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5421   if (attr && line_header)
5422     {
5423       unsigned int macro_offset = DW_UNSND (attr);
5424
5425       dwarf_decode_macros (line_header, macro_offset,
5426                            comp_dir, abfd, cu);
5427     }
5428   do_cleanups (back_to);
5429 }
5430
5431 /* Process DW_TAG_type_unit.
5432    For TUs we want to skip the first top level sibling if it's not the
5433    actual type being defined by this TU.  In this case the first top
5434    level sibling is there to provide context only.  */
5435
5436 static void
5437 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5438 {
5439   struct objfile *objfile = cu->objfile;
5440   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5441   CORE_ADDR lowpc;
5442   struct attribute *attr;
5443   char *name = NULL;
5444   char *comp_dir = NULL;
5445   struct die_info *child_die;
5446   bfd *abfd = objfile->obfd;
5447
5448   /* start_symtab needs a low pc, but we don't really have one.
5449      Do what read_file_scope would do in the absence of such info.  */
5450   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5451
5452   /* Find the filename.  Do not use dwarf2_name here, since the filename
5453      is not a source language identifier.  */
5454   attr = dwarf2_attr (die, DW_AT_name, cu);
5455   if (attr)
5456     name = DW_STRING (attr);
5457
5458   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5459   if (attr)
5460     comp_dir = DW_STRING (attr);
5461   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5462     {
5463       comp_dir = ldirname (name);
5464       if (comp_dir != NULL)
5465         make_cleanup (xfree, comp_dir);
5466     }
5467
5468   if (name == NULL)
5469     name = "<unknown>";
5470
5471   attr = dwarf2_attr (die, DW_AT_language, cu);
5472   if (attr)
5473     set_cu_language (DW_UNSND (attr), cu);
5474
5475   /* This isn't technically needed today.  It is done for symmetry
5476      with read_file_scope.  */
5477   attr = dwarf2_attr (die, DW_AT_producer, cu);
5478   if (attr)
5479     cu->producer = DW_STRING (attr);
5480
5481   /* We assume that we're processing GCC output.  */
5482   processing_gcc_compilation = 2;
5483
5484   processing_has_namespace_info = 0;
5485
5486   start_symtab (name, comp_dir, lowpc);
5487   record_debugformat ("DWARF 2");
5488   record_producer (cu->producer);
5489
5490   /* Process the dies in the type unit.  */
5491   if (die->child == NULL)
5492     {
5493       dump_die_for_error (die);
5494       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5495              bfd_get_filename (abfd));
5496     }
5497
5498   child_die = die->child;
5499
5500   while (child_die && child_die->tag)
5501     {
5502       process_die (child_die, cu);
5503
5504       child_die = sibling_die (child_die);
5505     }
5506
5507   do_cleanups (back_to);
5508 }
5509
5510 static void
5511 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5512                      struct dwarf2_cu *cu)
5513 {
5514   struct function_range *thisfn;
5515
5516   thisfn = (struct function_range *)
5517     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5518   thisfn->name = name;
5519   thisfn->lowpc = lowpc;
5520   thisfn->highpc = highpc;
5521   thisfn->seen_line = 0;
5522   thisfn->next = NULL;
5523
5524   if (cu->last_fn == NULL)
5525       cu->first_fn = thisfn;
5526   else
5527       cu->last_fn->next = thisfn;
5528
5529   cu->last_fn = thisfn;
5530 }
5531
5532 /* qsort helper for inherit_abstract_dies.  */
5533
5534 static int
5535 unsigned_int_compar (const void *ap, const void *bp)
5536 {
5537   unsigned int a = *(unsigned int *) ap;
5538   unsigned int b = *(unsigned int *) bp;
5539
5540   return (a > b) - (b > a);
5541 }
5542
5543 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5544    Inherit only the children of the DW_AT_abstract_origin DIE not being
5545    already referenced by DW_AT_abstract_origin from the children of the
5546    current DIE.  */
5547
5548 static void
5549 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5550 {
5551   struct die_info *child_die;
5552   unsigned die_children_count;
5553   /* CU offsets which were referenced by children of the current DIE.  */
5554   unsigned *offsets;
5555   unsigned *offsets_end, *offsetp;
5556   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5557   struct die_info *origin_die;
5558   /* Iterator of the ORIGIN_DIE children.  */
5559   struct die_info *origin_child_die;
5560   struct cleanup *cleanups;
5561   struct attribute *attr;
5562   struct dwarf2_cu *origin_cu;
5563   struct pending **origin_previous_list_in_scope;
5564
5565   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5566   if (!attr)
5567     return;
5568
5569   /* Note that following die references may follow to a die in a
5570      different cu.  */
5571
5572   origin_cu = cu;
5573   origin_die = follow_die_ref (die, attr, &origin_cu);
5574
5575   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5576      symbols in.  */
5577   origin_previous_list_in_scope = origin_cu->list_in_scope;
5578   origin_cu->list_in_scope = cu->list_in_scope;
5579
5580   if (die->tag != origin_die->tag
5581       && !(die->tag == DW_TAG_inlined_subroutine
5582            && origin_die->tag == DW_TAG_subprogram))
5583     complaint (&symfile_complaints,
5584                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5585                die->offset, origin_die->offset);
5586
5587   child_die = die->child;
5588   die_children_count = 0;
5589   while (child_die && child_die->tag)
5590     {
5591       child_die = sibling_die (child_die);
5592       die_children_count++;
5593     }
5594   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5595   cleanups = make_cleanup (xfree, offsets);
5596
5597   offsets_end = offsets;
5598   child_die = die->child;
5599   while (child_die && child_die->tag)
5600     {
5601       /* For each CHILD_DIE, find the corresponding child of
5602          ORIGIN_DIE.  If there is more than one layer of
5603          DW_AT_abstract_origin, follow them all; there shouldn't be,
5604          but GCC versions at least through 4.4 generate this (GCC PR
5605          40573).  */
5606       struct die_info *child_origin_die = child_die;
5607       struct dwarf2_cu *child_origin_cu = cu;
5608
5609       while (1)
5610         {
5611           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5612                               child_origin_cu);
5613           if (attr == NULL)
5614             break;
5615           child_origin_die = follow_die_ref (child_origin_die, attr,
5616                                              &child_origin_cu);
5617         }
5618
5619       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5620          counterpart may exist.  */
5621       if (child_origin_die != child_die)
5622         {
5623           if (child_die->tag != child_origin_die->tag
5624               && !(child_die->tag == DW_TAG_inlined_subroutine
5625                    && child_origin_die->tag == DW_TAG_subprogram))
5626             complaint (&symfile_complaints,
5627                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5628                          "different tags"), child_die->offset,
5629                        child_origin_die->offset);
5630           if (child_origin_die->parent != origin_die)
5631             complaint (&symfile_complaints,
5632                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5633                          "different parents"), child_die->offset,
5634                        child_origin_die->offset);
5635           else
5636             *offsets_end++ = child_origin_die->offset;
5637         }
5638       child_die = sibling_die (child_die);
5639     }
5640   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5641          unsigned_int_compar);
5642   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5643     if (offsetp[-1] == *offsetp)
5644       complaint (&symfile_complaints,
5645                  _("Multiple children of DIE 0x%x refer "
5646                    "to DIE 0x%x as their abstract origin"),
5647                  die->offset, *offsetp);
5648
5649   offsetp = offsets;
5650   origin_child_die = origin_die->child;
5651   while (origin_child_die && origin_child_die->tag)
5652     {
5653       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5654       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5655         offsetp++;
5656       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5657         {
5658           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5659           process_die (origin_child_die, origin_cu);
5660         }
5661       origin_child_die = sibling_die (origin_child_die);
5662     }
5663   origin_cu->list_in_scope = origin_previous_list_in_scope;
5664
5665   do_cleanups (cleanups);
5666 }
5667
5668 static void
5669 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5670 {
5671   struct objfile *objfile = cu->objfile;
5672   struct context_stack *new;
5673   CORE_ADDR lowpc;
5674   CORE_ADDR highpc;
5675   struct die_info *child_die;
5676   struct attribute *attr, *call_line, *call_file;
5677   char *name;
5678   CORE_ADDR baseaddr;
5679   struct block *block;
5680   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5681   VEC (symbolp) *template_args = NULL;
5682   struct template_symbol *templ_func = NULL;
5683
5684   if (inlined_func)
5685     {
5686       /* If we do not have call site information, we can't show the
5687          caller of this inlined function.  That's too confusing, so
5688          only use the scope for local variables.  */
5689       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5690       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5691       if (call_line == NULL || call_file == NULL)
5692         {
5693           read_lexical_block_scope (die, cu);
5694           return;
5695         }
5696     }
5697
5698   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5699
5700   name = dwarf2_name (die, cu);
5701
5702   /* Ignore functions with missing or empty names.  These are actually
5703      illegal according to the DWARF standard.  */
5704   if (name == NULL)
5705     {
5706       complaint (&symfile_complaints,
5707                  _("missing name for subprogram DIE at %d"), die->offset);
5708       return;
5709     }
5710
5711   /* Ignore functions with missing or invalid low and high pc attributes.  */
5712   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5713     {
5714       attr = dwarf2_attr (die, DW_AT_external, cu);
5715       if (!attr || !DW_UNSND (attr))
5716         complaint (&symfile_complaints,
5717                    _("cannot get low and high bounds "
5718                      "for subprogram DIE at %d"),
5719                    die->offset);
5720       return;
5721     }
5722
5723   lowpc += baseaddr;
5724   highpc += baseaddr;
5725
5726   /* Record the function range for dwarf_decode_lines.  */
5727   add_to_cu_func_list (name, lowpc, highpc, cu);
5728
5729   /* If we have any template arguments, then we must allocate a
5730      different sort of symbol.  */
5731   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5732     {
5733       if (child_die->tag == DW_TAG_template_type_param
5734           || child_die->tag == DW_TAG_template_value_param)
5735         {
5736           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5737                                        struct template_symbol);
5738           templ_func->base.is_cplus_template_function = 1;
5739           break;
5740         }
5741     }
5742
5743   new = push_context (0, lowpc);
5744   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5745                                (struct symbol *) templ_func);
5746
5747   /* If there is a location expression for DW_AT_frame_base, record
5748      it.  */
5749   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5750   if (attr)
5751     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5752        expression is being recorded directly in the function's symbol
5753        and not in a separate frame-base object.  I guess this hack is
5754        to avoid adding some sort of frame-base adjunct/annex to the
5755        function's symbol :-(.  The problem with doing this is that it
5756        results in a function symbol with a location expression that
5757        has nothing to do with the location of the function, ouch!  The
5758        relationship should be: a function's symbol has-a frame base; a
5759        frame-base has-a location expression.  */
5760     dwarf2_symbol_mark_computed (attr, new->name, cu);
5761
5762   cu->list_in_scope = &local_symbols;
5763
5764   if (die->child != NULL)
5765     {
5766       child_die = die->child;
5767       while (child_die && child_die->tag)
5768         {
5769           if (child_die->tag == DW_TAG_template_type_param
5770               || child_die->tag == DW_TAG_template_value_param)
5771             {
5772               struct symbol *arg = new_symbol (child_die, NULL, cu);
5773
5774               if (arg != NULL)
5775                 VEC_safe_push (symbolp, template_args, arg);
5776             }
5777           else
5778             process_die (child_die, cu);
5779           child_die = sibling_die (child_die);
5780         }
5781     }
5782
5783   inherit_abstract_dies (die, cu);
5784
5785   /* If we have a DW_AT_specification, we might need to import using
5786      directives from the context of the specification DIE.  See the
5787      comment in determine_prefix.  */
5788   if (cu->language == language_cplus
5789       && dwarf2_attr (die, DW_AT_specification, cu))
5790     {
5791       struct dwarf2_cu *spec_cu = cu;
5792       struct die_info *spec_die = die_specification (die, &spec_cu);
5793
5794       while (spec_die)
5795         {
5796           child_die = spec_die->child;
5797           while (child_die && child_die->tag)
5798             {
5799               if (child_die->tag == DW_TAG_imported_module)
5800                 process_die (child_die, spec_cu);
5801               child_die = sibling_die (child_die);
5802             }
5803
5804           /* In some cases, GCC generates specification DIEs that
5805              themselves contain DW_AT_specification attributes.  */
5806           spec_die = die_specification (spec_die, &spec_cu);
5807         }
5808     }
5809
5810   new = pop_context ();
5811   /* Make a block for the local symbols within.  */
5812   block = finish_block (new->name, &local_symbols, new->old_blocks,
5813                         lowpc, highpc, objfile);
5814
5815   /* For C++, set the block's scope.  */
5816   if (cu->language == language_cplus || cu->language == language_fortran)
5817     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5818                         determine_prefix (die, cu),
5819                         processing_has_namespace_info);
5820
5821   /* If we have address ranges, record them.  */
5822   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5823
5824   /* Attach template arguments to function.  */
5825   if (! VEC_empty (symbolp, template_args))
5826     {
5827       gdb_assert (templ_func != NULL);
5828
5829       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5830       templ_func->template_arguments
5831         = obstack_alloc (&objfile->objfile_obstack,
5832                          (templ_func->n_template_arguments
5833                           * sizeof (struct symbol *)));
5834       memcpy (templ_func->template_arguments,
5835               VEC_address (symbolp, template_args),
5836               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5837       VEC_free (symbolp, template_args);
5838     }
5839
5840   /* In C++, we can have functions nested inside functions (e.g., when
5841      a function declares a class that has methods).  This means that
5842      when we finish processing a function scope, we may need to go
5843      back to building a containing block's symbol lists.  */
5844   local_symbols = new->locals;
5845   param_symbols = new->params;
5846   using_directives = new->using_directives;
5847
5848   /* If we've finished processing a top-level function, subsequent
5849      symbols go in the file symbol list.  */
5850   if (outermost_context_p ())
5851     cu->list_in_scope = &file_symbols;
5852 }
5853
5854 /* Process all the DIES contained within a lexical block scope.  Start
5855    a new scope, process the dies, and then close the scope.  */
5856
5857 static void
5858 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5859 {
5860   struct objfile *objfile = cu->objfile;
5861   struct context_stack *new;
5862   CORE_ADDR lowpc, highpc;
5863   struct die_info *child_die;
5864   CORE_ADDR baseaddr;
5865
5866   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5867
5868   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5869   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5870      as multiple lexical blocks?  Handling children in a sane way would
5871      be nasty.  Might be easier to properly extend generic blocks to
5872      describe ranges.  */
5873   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5874     return;
5875   lowpc += baseaddr;
5876   highpc += baseaddr;
5877
5878   push_context (0, lowpc);
5879   if (die->child != NULL)
5880     {
5881       child_die = die->child;
5882       while (child_die && child_die->tag)
5883         {
5884           process_die (child_die, cu);
5885           child_die = sibling_die (child_die);
5886         }
5887     }
5888   new = pop_context ();
5889
5890   if (local_symbols != NULL || using_directives != NULL)
5891     {
5892       struct block *block
5893         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5894                         highpc, objfile);
5895
5896       /* Note that recording ranges after traversing children, as we
5897          do here, means that recording a parent's ranges entails
5898          walking across all its children's ranges as they appear in
5899          the address map, which is quadratic behavior.
5900
5901          It would be nicer to record the parent's ranges before
5902          traversing its children, simply overriding whatever you find
5903          there.  But since we don't even decide whether to create a
5904          block until after we've traversed its children, that's hard
5905          to do.  */
5906       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5907     }
5908   local_symbols = new->locals;
5909   using_directives = new->using_directives;
5910 }
5911
5912 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5913    Return 1 if the attributes are present and valid, otherwise, return 0.
5914    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5915
5916 static int
5917 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5918                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5919                     struct partial_symtab *ranges_pst)
5920 {
5921   struct objfile *objfile = cu->objfile;
5922   struct comp_unit_head *cu_header = &cu->header;
5923   bfd *obfd = objfile->obfd;
5924   unsigned int addr_size = cu_header->addr_size;
5925   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5926   /* Base address selection entry.  */
5927   CORE_ADDR base;
5928   int found_base;
5929   unsigned int dummy;
5930   gdb_byte *buffer;
5931   CORE_ADDR marker;
5932   int low_set;
5933   CORE_ADDR low = 0;
5934   CORE_ADDR high = 0;
5935   CORE_ADDR baseaddr;
5936
5937   found_base = cu->base_known;
5938   base = cu->base_address;
5939
5940   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5941   if (offset >= dwarf2_per_objfile->ranges.size)
5942     {
5943       complaint (&symfile_complaints,
5944                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5945                  offset);
5946       return 0;
5947     }
5948   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5949
5950   /* Read in the largest possible address.  */
5951   marker = read_address (obfd, buffer, cu, &dummy);
5952   if ((marker & mask) == mask)
5953     {
5954       /* If we found the largest possible address, then
5955          read the base address.  */
5956       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5957       buffer += 2 * addr_size;
5958       offset += 2 * addr_size;
5959       found_base = 1;
5960     }
5961
5962   low_set = 0;
5963
5964   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5965
5966   while (1)
5967     {
5968       CORE_ADDR range_beginning, range_end;
5969
5970       range_beginning = read_address (obfd, buffer, cu, &dummy);
5971       buffer += addr_size;
5972       range_end = read_address (obfd, buffer, cu, &dummy);
5973       buffer += addr_size;
5974       offset += 2 * addr_size;
5975
5976       /* An end of list marker is a pair of zero addresses.  */
5977       if (range_beginning == 0 && range_end == 0)
5978         /* Found the end of list entry.  */
5979         break;
5980
5981       /* Each base address selection entry is a pair of 2 values.
5982          The first is the largest possible address, the second is
5983          the base address.  Check for a base address here.  */
5984       if ((range_beginning & mask) == mask)
5985         {
5986           /* If we found the largest possible address, then
5987              read the base address.  */
5988           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5989           found_base = 1;
5990           continue;
5991         }
5992
5993       if (!found_base)
5994         {
5995           /* We have no valid base address for the ranges
5996              data.  */
5997           complaint (&symfile_complaints,
5998                      _("Invalid .debug_ranges data (no base address)"));
5999           return 0;
6000         }
6001
6002       if (range_beginning > range_end)
6003         {
6004           /* Inverted range entries are invalid.  */
6005           complaint (&symfile_complaints,
6006                      _("Invalid .debug_ranges data (inverted range)"));
6007           return 0;
6008         }
6009
6010       /* Empty range entries have no effect.  */
6011       if (range_beginning == range_end)
6012         continue;
6013
6014       range_beginning += base;
6015       range_end += base;
6016
6017       if (ranges_pst != NULL)
6018         addrmap_set_empty (objfile->psymtabs_addrmap,
6019                            range_beginning + baseaddr,
6020                            range_end - 1 + baseaddr,
6021                            ranges_pst);
6022
6023       /* FIXME: This is recording everything as a low-high
6024          segment of consecutive addresses.  We should have a
6025          data structure for discontiguous block ranges
6026          instead.  */
6027       if (! low_set)
6028         {
6029           low = range_beginning;
6030           high = range_end;
6031           low_set = 1;
6032         }
6033       else
6034         {
6035           if (range_beginning < low)
6036             low = range_beginning;
6037           if (range_end > high)
6038             high = range_end;
6039         }
6040     }
6041
6042   if (! low_set)
6043     /* If the first entry is an end-of-list marker, the range
6044        describes an empty scope, i.e. no instructions.  */
6045     return 0;
6046
6047   if (low_return)
6048     *low_return = low;
6049   if (high_return)
6050     *high_return = high;
6051   return 1;
6052 }
6053
6054 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6055    are present and valid, otherwise, return 0.  Return -1 if the range is
6056    discontinuous, i.e. derived from DW_AT_ranges information.  */
6057 static int
6058 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6059                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6060                       struct partial_symtab *pst)
6061 {
6062   struct attribute *attr;
6063   CORE_ADDR low = 0;
6064   CORE_ADDR high = 0;
6065   int ret = 0;
6066
6067   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6068   if (attr)
6069     {
6070       high = DW_ADDR (attr);
6071       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6072       if (attr)
6073         low = DW_ADDR (attr);
6074       else
6075         /* Found high w/o low attribute.  */
6076         return 0;
6077
6078       /* Found consecutive range of addresses.  */
6079       ret = 1;
6080     }
6081   else
6082     {
6083       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6084       if (attr != NULL)
6085         {
6086           /* Value of the DW_AT_ranges attribute is the offset in the
6087              .debug_ranges section.  */
6088           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6089             return 0;
6090           /* Found discontinuous range of addresses.  */
6091           ret = -1;
6092         }
6093     }
6094
6095   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6096   if (high <= low)
6097     return 0;
6098
6099   /* When using the GNU linker, .gnu.linkonce. sections are used to
6100      eliminate duplicate copies of functions and vtables and such.
6101      The linker will arbitrarily choose one and discard the others.
6102      The AT_*_pc values for such functions refer to local labels in
6103      these sections.  If the section from that file was discarded, the
6104      labels are not in the output, so the relocs get a value of 0.
6105      If this is a discarded function, mark the pc bounds as invalid,
6106      so that GDB will ignore it.  */
6107   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6108     return 0;
6109
6110   *lowpc = low;
6111   *highpc = high;
6112   return ret;
6113 }
6114
6115 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6116    its low and high PC addresses.  Do nothing if these addresses could not
6117    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6118    and HIGHPC to the high address if greater than HIGHPC.  */
6119
6120 static void
6121 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6122                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6123                                  struct dwarf2_cu *cu)
6124 {
6125   CORE_ADDR low, high;
6126   struct die_info *child = die->child;
6127
6128   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6129     {
6130       *lowpc = min (*lowpc, low);
6131       *highpc = max (*highpc, high);
6132     }
6133
6134   /* If the language does not allow nested subprograms (either inside
6135      subprograms or lexical blocks), we're done.  */
6136   if (cu->language != language_ada)
6137     return;
6138
6139   /* Check all the children of the given DIE.  If it contains nested
6140      subprograms, then check their pc bounds.  Likewise, we need to
6141      check lexical blocks as well, as they may also contain subprogram
6142      definitions.  */
6143   while (child && child->tag)
6144     {
6145       if (child->tag == DW_TAG_subprogram
6146           || child->tag == DW_TAG_lexical_block)
6147         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6148       child = sibling_die (child);
6149     }
6150 }
6151
6152 /* Get the low and high pc's represented by the scope DIE, and store
6153    them in *LOWPC and *HIGHPC.  If the correct values can't be
6154    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6155
6156 static void
6157 get_scope_pc_bounds (struct die_info *die,
6158                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6159                      struct dwarf2_cu *cu)
6160 {
6161   CORE_ADDR best_low = (CORE_ADDR) -1;
6162   CORE_ADDR best_high = (CORE_ADDR) 0;
6163   CORE_ADDR current_low, current_high;
6164
6165   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6166     {
6167       best_low = current_low;
6168       best_high = current_high;
6169     }
6170   else
6171     {
6172       struct die_info *child = die->child;
6173
6174       while (child && child->tag)
6175         {
6176           switch (child->tag) {
6177           case DW_TAG_subprogram:
6178             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6179             break;
6180           case DW_TAG_namespace:
6181           case DW_TAG_module:
6182             /* FIXME: carlton/2004-01-16: Should we do this for
6183                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6184                that current GCC's always emit the DIEs corresponding
6185                to definitions of methods of classes as children of a
6186                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6187                the DIEs giving the declarations, which could be
6188                anywhere).  But I don't see any reason why the
6189                standards says that they have to be there.  */
6190             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6191
6192             if (current_low != ((CORE_ADDR) -1))
6193               {
6194                 best_low = min (best_low, current_low);
6195                 best_high = max (best_high, current_high);
6196               }
6197             break;
6198           default:
6199             /* Ignore.  */
6200             break;
6201           }
6202
6203           child = sibling_die (child);
6204         }
6205     }
6206
6207   *lowpc = best_low;
6208   *highpc = best_high;
6209 }
6210
6211 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6212    in DIE.  */
6213 static void
6214 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6215                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6216 {
6217   struct attribute *attr;
6218
6219   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6220   if (attr)
6221     {
6222       CORE_ADDR high = DW_ADDR (attr);
6223
6224       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6225       if (attr)
6226         {
6227           CORE_ADDR low = DW_ADDR (attr);
6228
6229           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6230         }
6231     }
6232
6233   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6234   if (attr)
6235     {
6236       bfd *obfd = cu->objfile->obfd;
6237
6238       /* The value of the DW_AT_ranges attribute is the offset of the
6239          address range list in the .debug_ranges section.  */
6240       unsigned long offset = DW_UNSND (attr);
6241       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6242
6243       /* For some target architectures, but not others, the
6244          read_address function sign-extends the addresses it returns.
6245          To recognize base address selection entries, we need a
6246          mask.  */
6247       unsigned int addr_size = cu->header.addr_size;
6248       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6249
6250       /* The base address, to which the next pair is relative.  Note
6251          that this 'base' is a DWARF concept: most entries in a range
6252          list are relative, to reduce the number of relocs against the
6253          debugging information.  This is separate from this function's
6254          'baseaddr' argument, which GDB uses to relocate debugging
6255          information from a shared library based on the address at
6256          which the library was loaded.  */
6257       CORE_ADDR base = cu->base_address;
6258       int base_known = cu->base_known;
6259
6260       gdb_assert (dwarf2_per_objfile->ranges.readin);
6261       if (offset >= dwarf2_per_objfile->ranges.size)
6262         {
6263           complaint (&symfile_complaints,
6264                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6265                      offset);
6266           return;
6267         }
6268
6269       for (;;)
6270         {
6271           unsigned int bytes_read;
6272           CORE_ADDR start, end;
6273
6274           start = read_address (obfd, buffer, cu, &bytes_read);
6275           buffer += bytes_read;
6276           end = read_address (obfd, buffer, cu, &bytes_read);
6277           buffer += bytes_read;
6278
6279           /* Did we find the end of the range list?  */
6280           if (start == 0 && end == 0)
6281             break;
6282
6283           /* Did we find a base address selection entry?  */
6284           else if ((start & base_select_mask) == base_select_mask)
6285             {
6286               base = end;
6287               base_known = 1;
6288             }
6289
6290           /* We found an ordinary address range.  */
6291           else
6292             {
6293               if (!base_known)
6294                 {
6295                   complaint (&symfile_complaints,
6296                              _("Invalid .debug_ranges data "
6297                                "(no base address)"));
6298                   return;
6299                 }
6300
6301               if (start > end)
6302                 {
6303                   /* Inverted range entries are invalid.  */
6304                   complaint (&symfile_complaints,
6305                              _("Invalid .debug_ranges data "
6306                                "(inverted range)"));
6307                   return;
6308                 }
6309
6310               /* Empty range entries have no effect.  */
6311               if (start == end)
6312                 continue;
6313
6314               record_block_range (block,
6315                                   baseaddr + base + start,
6316                                   baseaddr + base + end - 1);
6317             }
6318         }
6319     }
6320 }
6321
6322 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6323    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6324    during 4.6.0 experimental.  */
6325
6326 static int
6327 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6328 {
6329   const char *cs;
6330   int major, minor, release;
6331
6332   if (cu->producer == NULL)
6333     {
6334       /* For unknown compilers expect their behavior is DWARF version
6335          compliant.
6336
6337          GCC started to support .debug_types sections by -gdwarf-4 since
6338          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6339          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6340          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6341          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6342
6343       return 0;
6344     }
6345
6346   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6347
6348   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6349     {
6350       /* For non-GCC compilers expect their behavior is DWARF version
6351          compliant.  */
6352
6353       return 0;
6354     }
6355   cs = &cu->producer[strlen ("GNU ")];
6356   while (*cs && !isdigit (*cs))
6357     cs++;
6358   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6359     {
6360       /* Not recognized as GCC.  */
6361
6362       return 0;
6363     }
6364
6365   return major < 4 || (major == 4 && minor < 6);
6366 }
6367
6368 /* Return the default accessibility type if it is not overriden by
6369    DW_AT_accessibility.  */
6370
6371 static enum dwarf_access_attribute
6372 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6373 {
6374   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6375     {
6376       /* The default DWARF 2 accessibility for members is public, the default
6377          accessibility for inheritance is private.  */
6378
6379       if (die->tag != DW_TAG_inheritance)
6380         return DW_ACCESS_public;
6381       else
6382         return DW_ACCESS_private;
6383     }
6384   else
6385     {
6386       /* DWARF 3+ defines the default accessibility a different way.  The same
6387          rules apply now for DW_TAG_inheritance as for the members and it only
6388          depends on the container kind.  */
6389
6390       if (die->parent->tag == DW_TAG_class_type)
6391         return DW_ACCESS_private;
6392       else
6393         return DW_ACCESS_public;
6394     }
6395 }
6396
6397 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6398    offset.  If the attribute was not found return 0, otherwise return
6399    1.  If it was found but could not properly be handled, set *OFFSET
6400    to 0.  */
6401
6402 static int
6403 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6404                              LONGEST *offset)
6405 {
6406   struct attribute *attr;
6407
6408   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6409   if (attr != NULL)
6410     {
6411       *offset = 0;
6412
6413       /* Note that we do not check for a section offset first here.
6414          This is because DW_AT_data_member_location is new in DWARF 4,
6415          so if we see it, we can assume that a constant form is really
6416          a constant and not a section offset.  */
6417       if (attr_form_is_constant (attr))
6418         *offset = dwarf2_get_attr_constant_value (attr, 0);
6419       else if (attr_form_is_section_offset (attr))
6420         dwarf2_complex_location_expr_complaint ();
6421       else if (attr_form_is_block (attr))
6422         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6423       else
6424         dwarf2_complex_location_expr_complaint ();
6425
6426       return 1;
6427     }
6428
6429   return 0;
6430 }
6431
6432 /* Add an aggregate field to the field list.  */
6433
6434 static void
6435 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6436                   struct dwarf2_cu *cu)
6437 {
6438   struct objfile *objfile = cu->objfile;
6439   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6440   struct nextfield *new_field;
6441   struct attribute *attr;
6442   struct field *fp;
6443   char *fieldname = "";
6444
6445   /* Allocate a new field list entry and link it in.  */
6446   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6447   make_cleanup (xfree, new_field);
6448   memset (new_field, 0, sizeof (struct nextfield));
6449
6450   if (die->tag == DW_TAG_inheritance)
6451     {
6452       new_field->next = fip->baseclasses;
6453       fip->baseclasses = new_field;
6454     }
6455   else
6456     {
6457       new_field->next = fip->fields;
6458       fip->fields = new_field;
6459     }
6460   fip->nfields++;
6461
6462   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6463   if (attr)
6464     new_field->accessibility = DW_UNSND (attr);
6465   else
6466     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6467   if (new_field->accessibility != DW_ACCESS_public)
6468     fip->non_public_fields = 1;
6469
6470   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6471   if (attr)
6472     new_field->virtuality = DW_UNSND (attr);
6473   else
6474     new_field->virtuality = DW_VIRTUALITY_none;
6475
6476   fp = &new_field->field;
6477
6478   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6479     {
6480       LONGEST offset;
6481
6482       /* Data member other than a C++ static data member.  */
6483
6484       /* Get type of field.  */
6485       fp->type = die_type (die, cu);
6486
6487       SET_FIELD_BITPOS (*fp, 0);
6488
6489       /* Get bit size of field (zero if none).  */
6490       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6491       if (attr)
6492         {
6493           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6494         }
6495       else
6496         {
6497           FIELD_BITSIZE (*fp) = 0;
6498         }
6499
6500       /* Get bit offset of field.  */
6501       if (handle_data_member_location (die, cu, &offset))
6502         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6503       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6504       if (attr)
6505         {
6506           if (gdbarch_bits_big_endian (gdbarch))
6507             {
6508               /* For big endian bits, the DW_AT_bit_offset gives the
6509                  additional bit offset from the MSB of the containing
6510                  anonymous object to the MSB of the field.  We don't
6511                  have to do anything special since we don't need to
6512                  know the size of the anonymous object.  */
6513               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6514             }
6515           else
6516             {
6517               /* For little endian bits, compute the bit offset to the
6518                  MSB of the anonymous object, subtract off the number of
6519                  bits from the MSB of the field to the MSB of the
6520                  object, and then subtract off the number of bits of
6521                  the field itself.  The result is the bit offset of
6522                  the LSB of the field.  */
6523               int anonymous_size;
6524               int bit_offset = DW_UNSND (attr);
6525
6526               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6527               if (attr)
6528                 {
6529                   /* The size of the anonymous object containing
6530                      the bit field is explicit, so use the
6531                      indicated size (in bytes).  */
6532                   anonymous_size = DW_UNSND (attr);
6533                 }
6534               else
6535                 {
6536                   /* The size of the anonymous object containing
6537                      the bit field must be inferred from the type
6538                      attribute of the data member containing the
6539                      bit field.  */
6540                   anonymous_size = TYPE_LENGTH (fp->type);
6541                 }
6542               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6543                 - bit_offset - FIELD_BITSIZE (*fp);
6544             }
6545         }
6546
6547       /* Get name of field.  */
6548       fieldname = dwarf2_name (die, cu);
6549       if (fieldname == NULL)
6550         fieldname = "";
6551
6552       /* The name is already allocated along with this objfile, so we don't
6553          need to duplicate it for the type.  */
6554       fp->name = fieldname;
6555
6556       /* Change accessibility for artificial fields (e.g. virtual table
6557          pointer or virtual base class pointer) to private.  */
6558       if (dwarf2_attr (die, DW_AT_artificial, cu))
6559         {
6560           FIELD_ARTIFICIAL (*fp) = 1;
6561           new_field->accessibility = DW_ACCESS_private;
6562           fip->non_public_fields = 1;
6563         }
6564     }
6565   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6566     {
6567       /* C++ static member.  */
6568
6569       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6570          is a declaration, but all versions of G++ as of this writing
6571          (so through at least 3.2.1) incorrectly generate
6572          DW_TAG_variable tags.  */
6573
6574       const char *physname;
6575
6576       /* Get name of field.  */
6577       fieldname = dwarf2_name (die, cu);
6578       if (fieldname == NULL)
6579         return;
6580
6581       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6582       if (attr
6583           /* Only create a symbol if this is an external value.
6584              new_symbol checks this and puts the value in the global symbol
6585              table, which we want.  If it is not external, new_symbol
6586              will try to put the value in cu->list_in_scope which is wrong.  */
6587           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6588         {
6589           /* A static const member, not much different than an enum as far as
6590              we're concerned, except that we can support more types.  */
6591           new_symbol (die, NULL, cu);
6592         }
6593
6594       /* Get physical name.  */
6595       physname = dwarf2_physname (fieldname, die, cu);
6596
6597       /* The name is already allocated along with this objfile, so we don't
6598          need to duplicate it for the type.  */
6599       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6600       FIELD_TYPE (*fp) = die_type (die, cu);
6601       FIELD_NAME (*fp) = fieldname;
6602     }
6603   else if (die->tag == DW_TAG_inheritance)
6604     {
6605       LONGEST offset;
6606
6607       /* C++ base class field.  */
6608       if (handle_data_member_location (die, cu, &offset))
6609         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6610       FIELD_BITSIZE (*fp) = 0;
6611       FIELD_TYPE (*fp) = die_type (die, cu);
6612       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6613       fip->nbaseclasses++;
6614     }
6615 }
6616
6617 /* Add a typedef defined in the scope of the FIP's class.  */
6618
6619 static void
6620 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6621                     struct dwarf2_cu *cu)
6622 {
6623   struct objfile *objfile = cu->objfile;
6624   struct typedef_field_list *new_field;
6625   struct attribute *attr;
6626   struct typedef_field *fp;
6627   char *fieldname = "";
6628
6629   /* Allocate a new field list entry and link it in.  */
6630   new_field = xzalloc (sizeof (*new_field));
6631   make_cleanup (xfree, new_field);
6632
6633   gdb_assert (die->tag == DW_TAG_typedef);
6634
6635   fp = &new_field->field;
6636
6637   /* Get name of field.  */
6638   fp->name = dwarf2_name (die, cu);
6639   if (fp->name == NULL)
6640     return;
6641
6642   fp->type = read_type_die (die, cu);
6643
6644   new_field->next = fip->typedef_field_list;
6645   fip->typedef_field_list = new_field;
6646   fip->typedef_field_list_count++;
6647 }
6648
6649 /* Create the vector of fields, and attach it to the type.  */
6650
6651 static void
6652 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6653                               struct dwarf2_cu *cu)
6654 {
6655   int nfields = fip->nfields;
6656
6657   /* Record the field count, allocate space for the array of fields,
6658      and create blank accessibility bitfields if necessary.  */
6659   TYPE_NFIELDS (type) = nfields;
6660   TYPE_FIELDS (type) = (struct field *)
6661     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6662   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6663
6664   if (fip->non_public_fields && cu->language != language_ada)
6665     {
6666       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6667
6668       TYPE_FIELD_PRIVATE_BITS (type) =
6669         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6670       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6671
6672       TYPE_FIELD_PROTECTED_BITS (type) =
6673         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6674       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6675
6676       TYPE_FIELD_IGNORE_BITS (type) =
6677         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6678       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6679     }
6680
6681   /* If the type has baseclasses, allocate and clear a bit vector for
6682      TYPE_FIELD_VIRTUAL_BITS.  */
6683   if (fip->nbaseclasses && cu->language != language_ada)
6684     {
6685       int num_bytes = B_BYTES (fip->nbaseclasses);
6686       unsigned char *pointer;
6687
6688       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6689       pointer = TYPE_ALLOC (type, num_bytes);
6690       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6691       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6692       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6693     }
6694
6695   /* Copy the saved-up fields into the field vector.  Start from the head of
6696      the list, adding to the tail of the field array, so that they end up in
6697      the same order in the array in which they were added to the list.  */
6698   while (nfields-- > 0)
6699     {
6700       struct nextfield *fieldp;
6701
6702       if (fip->fields)
6703         {
6704           fieldp = fip->fields;
6705           fip->fields = fieldp->next;
6706         }
6707       else
6708         {
6709           fieldp = fip->baseclasses;
6710           fip->baseclasses = fieldp->next;
6711         }
6712
6713       TYPE_FIELD (type, nfields) = fieldp->field;
6714       switch (fieldp->accessibility)
6715         {
6716         case DW_ACCESS_private:
6717           if (cu->language != language_ada)
6718             SET_TYPE_FIELD_PRIVATE (type, nfields);
6719           break;
6720
6721         case DW_ACCESS_protected:
6722           if (cu->language != language_ada)
6723             SET_TYPE_FIELD_PROTECTED (type, nfields);
6724           break;
6725
6726         case DW_ACCESS_public:
6727           break;
6728
6729         default:
6730           /* Unknown accessibility.  Complain and treat it as public.  */
6731           {
6732             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6733                        fieldp->accessibility);
6734           }
6735           break;
6736         }
6737       if (nfields < fip->nbaseclasses)
6738         {
6739           switch (fieldp->virtuality)
6740             {
6741             case DW_VIRTUALITY_virtual:
6742             case DW_VIRTUALITY_pure_virtual:
6743               if (cu->language == language_ada)
6744                 error (_("unexpected virtuality in component of Ada type"));
6745               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6746               break;
6747             }
6748         }
6749     }
6750 }
6751
6752 /* Add a member function to the proper fieldlist.  */
6753
6754 static void
6755 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6756                       struct type *type, struct dwarf2_cu *cu)
6757 {
6758   struct objfile *objfile = cu->objfile;
6759   struct attribute *attr;
6760   struct fnfieldlist *flp;
6761   int i;
6762   struct fn_field *fnp;
6763   char *fieldname;
6764   struct nextfnfield *new_fnfield;
6765   struct type *this_type;
6766   enum dwarf_access_attribute accessibility;
6767
6768   if (cu->language == language_ada)
6769     error (_("unexpected member function in Ada type"));
6770
6771   /* Get name of member function.  */
6772   fieldname = dwarf2_name (die, cu);
6773   if (fieldname == NULL)
6774     return;
6775
6776   /* Look up member function name in fieldlist.  */
6777   for (i = 0; i < fip->nfnfields; i++)
6778     {
6779       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6780         break;
6781     }
6782
6783   /* Create new list element if necessary.  */
6784   if (i < fip->nfnfields)
6785     flp = &fip->fnfieldlists[i];
6786   else
6787     {
6788       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6789         {
6790           fip->fnfieldlists = (struct fnfieldlist *)
6791             xrealloc (fip->fnfieldlists,
6792                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6793                       * sizeof (struct fnfieldlist));
6794           if (fip->nfnfields == 0)
6795             make_cleanup (free_current_contents, &fip->fnfieldlists);
6796         }
6797       flp = &fip->fnfieldlists[fip->nfnfields];
6798       flp->name = fieldname;
6799       flp->length = 0;
6800       flp->head = NULL;
6801       i = fip->nfnfields++;
6802     }
6803
6804   /* Create a new member function field and chain it to the field list
6805      entry.  */
6806   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6807   make_cleanup (xfree, new_fnfield);
6808   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6809   new_fnfield->next = flp->head;
6810   flp->head = new_fnfield;
6811   flp->length++;
6812
6813   /* Fill in the member function field info.  */
6814   fnp = &new_fnfield->fnfield;
6815
6816   /* Delay processing of the physname until later.  */
6817   if (cu->language == language_cplus || cu->language == language_java)
6818     {
6819       add_to_method_list (type, i, flp->length - 1, fieldname,
6820                           die, cu);
6821     }
6822   else
6823     {
6824       const char *physname = dwarf2_physname (fieldname, die, cu);
6825       fnp->physname = physname ? physname : "";
6826     }
6827
6828   fnp->type = alloc_type (objfile);
6829   this_type = read_type_die (die, cu);
6830   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6831     {
6832       int nparams = TYPE_NFIELDS (this_type);
6833
6834       /* TYPE is the domain of this method, and THIS_TYPE is the type
6835            of the method itself (TYPE_CODE_METHOD).  */
6836       smash_to_method_type (fnp->type, type,
6837                             TYPE_TARGET_TYPE (this_type),
6838                             TYPE_FIELDS (this_type),
6839                             TYPE_NFIELDS (this_type),
6840                             TYPE_VARARGS (this_type));
6841
6842       /* Handle static member functions.
6843          Dwarf2 has no clean way to discern C++ static and non-static
6844          member functions.  G++ helps GDB by marking the first
6845          parameter for non-static member functions (which is the this
6846          pointer) as artificial.  We obtain this information from
6847          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6848       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6849         fnp->voffset = VOFFSET_STATIC;
6850     }
6851   else
6852     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6853                dwarf2_full_name (fieldname, die, cu));
6854
6855   /* Get fcontext from DW_AT_containing_type if present.  */
6856   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6857     fnp->fcontext = die_containing_type (die, cu);
6858
6859   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6860      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6861
6862   /* Get accessibility.  */
6863   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6864   if (attr)
6865     accessibility = DW_UNSND (attr);
6866   else
6867     accessibility = dwarf2_default_access_attribute (die, cu);
6868   switch (accessibility)
6869     {
6870     case DW_ACCESS_private:
6871       fnp->is_private = 1;
6872       break;
6873     case DW_ACCESS_protected:
6874       fnp->is_protected = 1;
6875       break;
6876     }
6877
6878   /* Check for artificial methods.  */
6879   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6880   if (attr && DW_UNSND (attr) != 0)
6881     fnp->is_artificial = 1;
6882
6883   /* Get index in virtual function table if it is a virtual member
6884      function.  For older versions of GCC, this is an offset in the
6885      appropriate virtual table, as specified by DW_AT_containing_type.
6886      For everyone else, it is an expression to be evaluated relative
6887      to the object address.  */
6888
6889   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6890   if (attr)
6891     {
6892       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6893         {
6894           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6895             {
6896               /* Old-style GCC.  */
6897               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6898             }
6899           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6900                    || (DW_BLOCK (attr)->size > 1
6901                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6902                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6903             {
6904               struct dwarf_block blk;
6905               int offset;
6906
6907               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6908                         ? 1 : 2);
6909               blk.size = DW_BLOCK (attr)->size - offset;
6910               blk.data = DW_BLOCK (attr)->data + offset;
6911               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6912               if ((fnp->voffset % cu->header.addr_size) != 0)
6913                 dwarf2_complex_location_expr_complaint ();
6914               else
6915                 fnp->voffset /= cu->header.addr_size;
6916               fnp->voffset += 2;
6917             }
6918           else
6919             dwarf2_complex_location_expr_complaint ();
6920
6921           if (!fnp->fcontext)
6922             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6923         }
6924       else if (attr_form_is_section_offset (attr))
6925         {
6926           dwarf2_complex_location_expr_complaint ();
6927         }
6928       else
6929         {
6930           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6931                                                  fieldname);
6932         }
6933     }
6934   else
6935     {
6936       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6937       if (attr && DW_UNSND (attr))
6938         {
6939           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6940           complaint (&symfile_complaints,
6941                      _("Member function \"%s\" (offset %d) is virtual "
6942                        "but the vtable offset is not specified"),
6943                      fieldname, die->offset);
6944           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6945           TYPE_CPLUS_DYNAMIC (type) = 1;
6946         }
6947     }
6948 }
6949
6950 /* Create the vector of member function fields, and attach it to the type.  */
6951
6952 static void
6953 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6954                                  struct dwarf2_cu *cu)
6955 {
6956   struct fnfieldlist *flp;
6957   int total_length = 0;
6958   int i;
6959
6960   if (cu->language == language_ada)
6961     error (_("unexpected member functions in Ada type"));
6962
6963   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6964   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6965     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6966
6967   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6968     {
6969       struct nextfnfield *nfp = flp->head;
6970       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6971       int k;
6972
6973       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6974       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6975       fn_flp->fn_fields = (struct fn_field *)
6976         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6977       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6978         fn_flp->fn_fields[k] = nfp->fnfield;
6979
6980       total_length += flp->length;
6981     }
6982
6983   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6984   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6985 }
6986
6987 /* Returns non-zero if NAME is the name of a vtable member in CU's
6988    language, zero otherwise.  */
6989 static int
6990 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6991 {
6992   static const char vptr[] = "_vptr";
6993   static const char vtable[] = "vtable";
6994
6995   /* Look for the C++ and Java forms of the vtable.  */
6996   if ((cu->language == language_java
6997        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6998        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6999        && is_cplus_marker (name[sizeof (vptr) - 1])))
7000     return 1;
7001
7002   return 0;
7003 }
7004
7005 /* GCC outputs unnamed structures that are really pointers to member
7006    functions, with the ABI-specified layout.  If TYPE describes
7007    such a structure, smash it into a member function type.
7008
7009    GCC shouldn't do this; it should just output pointer to member DIEs.
7010    This is GCC PR debug/28767.  */
7011
7012 static void
7013 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7014 {
7015   struct type *pfn_type, *domain_type, *new_type;
7016
7017   /* Check for a structure with no name and two children.  */
7018   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7019     return;
7020
7021   /* Check for __pfn and __delta members.  */
7022   if (TYPE_FIELD_NAME (type, 0) == NULL
7023       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7024       || TYPE_FIELD_NAME (type, 1) == NULL
7025       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7026     return;
7027
7028   /* Find the type of the method.  */
7029   pfn_type = TYPE_FIELD_TYPE (type, 0);
7030   if (pfn_type == NULL
7031       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7032       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7033     return;
7034
7035   /* Look for the "this" argument.  */
7036   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7037   if (TYPE_NFIELDS (pfn_type) == 0
7038       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7039       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7040     return;
7041
7042   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7043   new_type = alloc_type (objfile);
7044   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7045                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7046                         TYPE_VARARGS (pfn_type));
7047   smash_to_methodptr_type (type, new_type);
7048 }
7049
7050 /* Called when we find the DIE that starts a structure or union scope
7051    (definition) to create a type for the structure or union.  Fill in
7052    the type's name and general properties; the members will not be
7053    processed until process_structure_type.
7054
7055    NOTE: we need to call these functions regardless of whether or not the
7056    DIE has a DW_AT_name attribute, since it might be an anonymous
7057    structure or union.  This gets the type entered into our set of
7058    user defined types.
7059
7060    However, if the structure is incomplete (an opaque struct/union)
7061    then suppress creating a symbol table entry for it since gdb only
7062    wants to find the one with the complete definition.  Note that if
7063    it is complete, we just call new_symbol, which does it's own
7064    checking about whether the struct/union is anonymous or not (and
7065    suppresses creating a symbol table entry itself).  */
7066
7067 static struct type *
7068 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7069 {
7070   struct objfile *objfile = cu->objfile;
7071   struct type *type;
7072   struct attribute *attr;
7073   char *name;
7074
7075   /* If the definition of this type lives in .debug_types, read that type.
7076      Don't follow DW_AT_specification though, that will take us back up
7077      the chain and we want to go down.  */
7078   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7079   if (attr)
7080     {
7081       struct dwarf2_cu *type_cu = cu;
7082       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7083
7084       /* We could just recurse on read_structure_type, but we need to call
7085          get_die_type to ensure only one type for this DIE is created.
7086          This is important, for example, because for c++ classes we need
7087          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7088       type = read_type_die (type_die, type_cu);
7089
7090       /* TYPE_CU may not be the same as CU.
7091          Ensure TYPE is recorded in CU's type_hash table.  */
7092       return set_die_type (die, type, cu);
7093     }
7094
7095   type = alloc_type (objfile);
7096   INIT_CPLUS_SPECIFIC (type);
7097
7098   name = dwarf2_name (die, cu);
7099   if (name != NULL)
7100     {
7101       if (cu->language == language_cplus
7102           || cu->language == language_java)
7103         {
7104           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7105
7106           /* dwarf2_full_name might have already finished building the DIE's
7107              type.  If so, there is no need to continue.  */
7108           if (get_die_type (die, cu) != NULL)
7109             return get_die_type (die, cu);
7110
7111           TYPE_TAG_NAME (type) = full_name;
7112           if (die->tag == DW_TAG_structure_type
7113               || die->tag == DW_TAG_class_type)
7114             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7115         }
7116       else
7117         {
7118           /* The name is already allocated along with this objfile, so
7119              we don't need to duplicate it for the type.  */
7120           TYPE_TAG_NAME (type) = (char *) name;
7121           if (die->tag == DW_TAG_class_type)
7122             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7123         }
7124     }
7125
7126   if (die->tag == DW_TAG_structure_type)
7127     {
7128       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7129     }
7130   else if (die->tag == DW_TAG_union_type)
7131     {
7132       TYPE_CODE (type) = TYPE_CODE_UNION;
7133     }
7134   else
7135     {
7136       TYPE_CODE (type) = TYPE_CODE_CLASS;
7137     }
7138
7139   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7140     TYPE_DECLARED_CLASS (type) = 1;
7141
7142   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7143   if (attr)
7144     {
7145       TYPE_LENGTH (type) = DW_UNSND (attr);
7146     }
7147   else
7148     {
7149       TYPE_LENGTH (type) = 0;
7150     }
7151
7152   TYPE_STUB_SUPPORTED (type) = 1;
7153   if (die_is_declaration (die, cu))
7154     TYPE_STUB (type) = 1;
7155   else if (attr == NULL && die->child == NULL
7156            && producer_is_realview (cu->producer))
7157     /* RealView does not output the required DW_AT_declaration
7158        on incomplete types.  */
7159     TYPE_STUB (type) = 1;
7160
7161   /* We need to add the type field to the die immediately so we don't
7162      infinitely recurse when dealing with pointers to the structure
7163      type within the structure itself.  */
7164   set_die_type (die, type, cu);
7165
7166   /* set_die_type should be already done.  */
7167   set_descriptive_type (type, die, cu);
7168
7169   return type;
7170 }
7171
7172 /* Finish creating a structure or union type, including filling in
7173    its members and creating a symbol for it.  */
7174
7175 static void
7176 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7177 {
7178   struct objfile *objfile = cu->objfile;
7179   struct die_info *child_die = die->child;
7180   struct type *type;
7181
7182   type = get_die_type (die, cu);
7183   if (type == NULL)
7184     type = read_structure_type (die, cu);
7185
7186   if (die->child != NULL && ! die_is_declaration (die, cu))
7187     {
7188       struct field_info fi;
7189       struct die_info *child_die;
7190       VEC (symbolp) *template_args = NULL;
7191       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7192
7193       memset (&fi, 0, sizeof (struct field_info));
7194
7195       child_die = die->child;
7196
7197       while (child_die && child_die->tag)
7198         {
7199           if (child_die->tag == DW_TAG_member
7200               || child_die->tag == DW_TAG_variable)
7201             {
7202               /* NOTE: carlton/2002-11-05: A C++ static data member
7203                  should be a DW_TAG_member that is a declaration, but
7204                  all versions of G++ as of this writing (so through at
7205                  least 3.2.1) incorrectly generate DW_TAG_variable
7206                  tags for them instead.  */
7207               dwarf2_add_field (&fi, child_die, cu);
7208             }
7209           else if (child_die->tag == DW_TAG_subprogram)
7210             {
7211               /* C++ member function.  */
7212               dwarf2_add_member_fn (&fi, child_die, type, cu);
7213             }
7214           else if (child_die->tag == DW_TAG_inheritance)
7215             {
7216               /* C++ base class field.  */
7217               dwarf2_add_field (&fi, child_die, cu);
7218             }
7219           else if (child_die->tag == DW_TAG_typedef)
7220             dwarf2_add_typedef (&fi, child_die, cu);
7221           else if (child_die->tag == DW_TAG_template_type_param
7222                    || child_die->tag == DW_TAG_template_value_param)
7223             {
7224               struct symbol *arg = new_symbol (child_die, NULL, cu);
7225
7226               if (arg != NULL)
7227                 VEC_safe_push (symbolp, template_args, arg);
7228             }
7229
7230           child_die = sibling_die (child_die);
7231         }
7232
7233       /* Attach template arguments to type.  */
7234       if (! VEC_empty (symbolp, template_args))
7235         {
7236           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7237           TYPE_N_TEMPLATE_ARGUMENTS (type)
7238             = VEC_length (symbolp, template_args);
7239           TYPE_TEMPLATE_ARGUMENTS (type)
7240             = obstack_alloc (&objfile->objfile_obstack,
7241                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7242                               * sizeof (struct symbol *)));
7243           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7244                   VEC_address (symbolp, template_args),
7245                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7246                    * sizeof (struct symbol *)));
7247           VEC_free (symbolp, template_args);
7248         }
7249
7250       /* Attach fields and member functions to the type.  */
7251       if (fi.nfields)
7252         dwarf2_attach_fields_to_type (&fi, type, cu);
7253       if (fi.nfnfields)
7254         {
7255           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7256
7257           /* Get the type which refers to the base class (possibly this
7258              class itself) which contains the vtable pointer for the current
7259              class from the DW_AT_containing_type attribute.  This use of
7260              DW_AT_containing_type is a GNU extension.  */
7261
7262           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7263             {
7264               struct type *t = die_containing_type (die, cu);
7265
7266               TYPE_VPTR_BASETYPE (type) = t;
7267               if (type == t)
7268                 {
7269                   int i;
7270
7271                   /* Our own class provides vtbl ptr.  */
7272                   for (i = TYPE_NFIELDS (t) - 1;
7273                        i >= TYPE_N_BASECLASSES (t);
7274                        --i)
7275                     {
7276                       char *fieldname = TYPE_FIELD_NAME (t, i);
7277
7278                       if (is_vtable_name (fieldname, cu))
7279                         {
7280                           TYPE_VPTR_FIELDNO (type) = i;
7281                           break;
7282                         }
7283                     }
7284
7285                   /* Complain if virtual function table field not found.  */
7286                   if (i < TYPE_N_BASECLASSES (t))
7287                     complaint (&symfile_complaints,
7288                                _("virtual function table pointer "
7289                                  "not found when defining class '%s'"),
7290                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7291                                "");
7292                 }
7293               else
7294                 {
7295                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7296                 }
7297             }
7298           else if (cu->producer
7299                    && strncmp (cu->producer,
7300                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7301             {
7302               /* The IBM XLC compiler does not provide direct indication
7303                  of the containing type, but the vtable pointer is
7304                  always named __vfp.  */
7305
7306               int i;
7307
7308               for (i = TYPE_NFIELDS (type) - 1;
7309                    i >= TYPE_N_BASECLASSES (type);
7310                    --i)
7311                 {
7312                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7313                     {
7314                       TYPE_VPTR_FIELDNO (type) = i;
7315                       TYPE_VPTR_BASETYPE (type) = type;
7316                       break;
7317                     }
7318                 }
7319             }
7320         }
7321
7322       /* Copy fi.typedef_field_list linked list elements content into the
7323          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7324       if (fi.typedef_field_list)
7325         {
7326           int i = fi.typedef_field_list_count;
7327
7328           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7329           TYPE_TYPEDEF_FIELD_ARRAY (type)
7330             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7331           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7332
7333           /* Reverse the list order to keep the debug info elements order.  */
7334           while (--i >= 0)
7335             {
7336               struct typedef_field *dest, *src;
7337
7338               dest = &TYPE_TYPEDEF_FIELD (type, i);
7339               src = &fi.typedef_field_list->field;
7340               fi.typedef_field_list = fi.typedef_field_list->next;
7341               *dest = *src;
7342             }
7343         }
7344
7345       do_cleanups (back_to);
7346     }
7347
7348   quirk_gcc_member_function_pointer (type, cu->objfile);
7349
7350   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7351      snapshots) has been known to create a die giving a declaration
7352      for a class that has, as a child, a die giving a definition for a
7353      nested class.  So we have to process our children even if the
7354      current die is a declaration.  Normally, of course, a declaration
7355      won't have any children at all.  */
7356
7357   while (child_die != NULL && child_die->tag)
7358     {
7359       if (child_die->tag == DW_TAG_member
7360           || child_die->tag == DW_TAG_variable
7361           || child_die->tag == DW_TAG_inheritance
7362           || child_die->tag == DW_TAG_template_value_param
7363           || child_die->tag == DW_TAG_template_type_param)
7364         {
7365           /* Do nothing.  */
7366         }
7367       else
7368         process_die (child_die, cu);
7369
7370       child_die = sibling_die (child_die);
7371     }
7372
7373   /* Do not consider external references.  According to the DWARF standard,
7374      these DIEs are identified by the fact that they have no byte_size
7375      attribute, and a declaration attribute.  */
7376   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7377       || !die_is_declaration (die, cu))
7378     new_symbol (die, type, cu);
7379 }
7380
7381 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7382    complete the type's fields yet, or create any symbols.  */
7383
7384 static struct type *
7385 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7386 {
7387   struct objfile *objfile = cu->objfile;
7388   struct type *type;
7389   struct attribute *attr;
7390   const char *name;
7391
7392   /* If the definition of this type lives in .debug_types, read that type.
7393      Don't follow DW_AT_specification though, that will take us back up
7394      the chain and we want to go down.  */
7395   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7396   if (attr)
7397     {
7398       struct dwarf2_cu *type_cu = cu;
7399       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7400
7401       type = read_type_die (type_die, type_cu);
7402
7403       /* TYPE_CU may not be the same as CU.
7404          Ensure TYPE is recorded in CU's type_hash table.  */
7405       return set_die_type (die, type, cu);
7406     }
7407
7408   type = alloc_type (objfile);
7409
7410   TYPE_CODE (type) = TYPE_CODE_ENUM;
7411   name = dwarf2_full_name (NULL, die, cu);
7412   if (name != NULL)
7413     TYPE_TAG_NAME (type) = (char *) name;
7414
7415   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7416   if (attr)
7417     {
7418       TYPE_LENGTH (type) = DW_UNSND (attr);
7419     }
7420   else
7421     {
7422       TYPE_LENGTH (type) = 0;
7423     }
7424
7425   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7426      declared as private in the package spec, and then defined only
7427      inside the package body.  Such types are known as Taft Amendment
7428      Types.  When another package uses such a type, an incomplete DIE
7429      may be generated by the compiler.  */
7430   if (die_is_declaration (die, cu))
7431     TYPE_STUB (type) = 1;
7432
7433   return set_die_type (die, type, cu);
7434 }
7435
7436 /* Given a pointer to a die which begins an enumeration, process all
7437    the dies that define the members of the enumeration, and create the
7438    symbol for the enumeration type.
7439
7440    NOTE: We reverse the order of the element list.  */
7441
7442 static void
7443 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7444 {
7445   struct type *this_type;
7446
7447   this_type = get_die_type (die, cu);
7448   if (this_type == NULL)
7449     this_type = read_enumeration_type (die, cu);
7450
7451   if (die->child != NULL)
7452     {
7453       struct die_info *child_die;
7454       struct symbol *sym;
7455       struct field *fields = NULL;
7456       int num_fields = 0;
7457       int unsigned_enum = 1;
7458       char *name;
7459
7460       child_die = die->child;
7461       while (child_die && child_die->tag)
7462         {
7463           if (child_die->tag != DW_TAG_enumerator)
7464             {
7465               process_die (child_die, cu);
7466             }
7467           else
7468             {
7469               name = dwarf2_name (child_die, cu);
7470               if (name)
7471                 {
7472                   sym = new_symbol (child_die, this_type, cu);
7473                   if (SYMBOL_VALUE (sym) < 0)
7474                     unsigned_enum = 0;
7475
7476                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7477                     {
7478                       fields = (struct field *)
7479                         xrealloc (fields,
7480                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7481                                   * sizeof (struct field));
7482                     }
7483
7484                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7485                   FIELD_TYPE (fields[num_fields]) = NULL;
7486                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7487                   FIELD_BITSIZE (fields[num_fields]) = 0;
7488
7489                   num_fields++;
7490                 }
7491             }
7492
7493           child_die = sibling_die (child_die);
7494         }
7495
7496       if (num_fields)
7497         {
7498           TYPE_NFIELDS (this_type) = num_fields;
7499           TYPE_FIELDS (this_type) = (struct field *)
7500             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7501           memcpy (TYPE_FIELDS (this_type), fields,
7502                   sizeof (struct field) * num_fields);
7503           xfree (fields);
7504         }
7505       if (unsigned_enum)
7506         TYPE_UNSIGNED (this_type) = 1;
7507     }
7508
7509   new_symbol (die, this_type, cu);
7510 }
7511
7512 /* Extract all information from a DW_TAG_array_type DIE and put it in
7513    the DIE's type field.  For now, this only handles one dimensional
7514    arrays.  */
7515
7516 static struct type *
7517 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7518 {
7519   struct objfile *objfile = cu->objfile;
7520   struct die_info *child_die;
7521   struct type *type;
7522   struct type *element_type, *range_type, *index_type;
7523   struct type **range_types = NULL;
7524   struct attribute *attr;
7525   int ndim = 0;
7526   struct cleanup *back_to;
7527   char *name;
7528
7529   element_type = die_type (die, cu);
7530
7531   /* The die_type call above may have already set the type for this DIE.  */
7532   type = get_die_type (die, cu);
7533   if (type)
7534     return type;
7535
7536   /* Irix 6.2 native cc creates array types without children for
7537      arrays with unspecified length.  */
7538   if (die->child == NULL)
7539     {
7540       index_type = objfile_type (objfile)->builtin_int;
7541       range_type = create_range_type (NULL, index_type, 0, -1);
7542       type = create_array_type (NULL, element_type, range_type);
7543       return set_die_type (die, type, cu);
7544     }
7545
7546   back_to = make_cleanup (null_cleanup, NULL);
7547   child_die = die->child;
7548   while (child_die && child_die->tag)
7549     {
7550       if (child_die->tag == DW_TAG_subrange_type)
7551         {
7552           struct type *child_type = read_type_die (child_die, cu);
7553
7554           if (child_type != NULL)
7555             {
7556               /* The range type was succesfully read.  Save it for the
7557                  array type creation.  */
7558               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7559                 {
7560                   range_types = (struct type **)
7561                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7562                               * sizeof (struct type *));
7563                   if (ndim == 0)
7564                     make_cleanup (free_current_contents, &range_types);
7565                 }
7566               range_types[ndim++] = child_type;
7567             }
7568         }
7569       child_die = sibling_die (child_die);
7570     }
7571
7572   /* Dwarf2 dimensions are output from left to right, create the
7573      necessary array types in backwards order.  */
7574
7575   type = element_type;
7576
7577   if (read_array_order (die, cu) == DW_ORD_col_major)
7578     {
7579       int i = 0;
7580
7581       while (i < ndim)
7582         type = create_array_type (NULL, type, range_types[i++]);
7583     }
7584   else
7585     {
7586       while (ndim-- > 0)
7587         type = create_array_type (NULL, type, range_types[ndim]);
7588     }
7589
7590   /* Understand Dwarf2 support for vector types (like they occur on
7591      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7592      array type.  This is not part of the Dwarf2/3 standard yet, but a
7593      custom vendor extension.  The main difference between a regular
7594      array and the vector variant is that vectors are passed by value
7595      to functions.  */
7596   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7597   if (attr)
7598     make_vector_type (type);
7599
7600   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
7601      implementation may choose to implement triple vectors using this
7602      attribute.  */
7603   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7604   if (attr)
7605     {
7606       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7607         TYPE_LENGTH (type) = DW_UNSND (attr);
7608       else
7609         complaint (&symfile_complaints,
7610                    _("DW_AT_byte_size for array type smaller "
7611                      "than the total size of elements"));
7612     }
7613
7614   name = dwarf2_name (die, cu);
7615   if (name)
7616     TYPE_NAME (type) = name;
7617
7618   /* Install the type in the die.  */
7619   set_die_type (die, type, cu);
7620
7621   /* set_die_type should be already done.  */
7622   set_descriptive_type (type, die, cu);
7623
7624   do_cleanups (back_to);
7625
7626   return type;
7627 }
7628
7629 static enum dwarf_array_dim_ordering
7630 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7631 {
7632   struct attribute *attr;
7633
7634   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7635
7636   if (attr) return DW_SND (attr);
7637
7638   /* GNU F77 is a special case, as at 08/2004 array type info is the
7639      opposite order to the dwarf2 specification, but data is still
7640      laid out as per normal fortran.
7641
7642      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7643      version checking.  */
7644
7645   if (cu->language == language_fortran
7646       && cu->producer && strstr (cu->producer, "GNU F77"))
7647     {
7648       return DW_ORD_row_major;
7649     }
7650
7651   switch (cu->language_defn->la_array_ordering)
7652     {
7653     case array_column_major:
7654       return DW_ORD_col_major;
7655     case array_row_major:
7656     default:
7657       return DW_ORD_row_major;
7658     };
7659 }
7660
7661 /* Extract all information from a DW_TAG_set_type DIE and put it in
7662    the DIE's type field.  */
7663
7664 static struct type *
7665 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7666 {
7667   struct type *domain_type, *set_type;
7668   struct attribute *attr;
7669
7670   domain_type = die_type (die, cu);
7671
7672   /* The die_type call above may have already set the type for this DIE.  */
7673   set_type = get_die_type (die, cu);
7674   if (set_type)
7675     return set_type;
7676
7677   set_type = create_set_type (NULL, domain_type);
7678
7679   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7680   if (attr)
7681     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7682
7683   return set_die_type (die, set_type, cu);
7684 }
7685
7686 /* First cut: install each common block member as a global variable.  */
7687
7688 static void
7689 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7690 {
7691   struct die_info *child_die;
7692   struct attribute *attr;
7693   struct symbol *sym;
7694   CORE_ADDR base = (CORE_ADDR) 0;
7695
7696   attr = dwarf2_attr (die, DW_AT_location, cu);
7697   if (attr)
7698     {
7699       /* Support the .debug_loc offsets.  */
7700       if (attr_form_is_block (attr))
7701         {
7702           base = decode_locdesc (DW_BLOCK (attr), cu);
7703         }
7704       else if (attr_form_is_section_offset (attr))
7705         {
7706           dwarf2_complex_location_expr_complaint ();
7707         }
7708       else
7709         {
7710           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7711                                                  "common block member");
7712         }
7713     }
7714   if (die->child != NULL)
7715     {
7716       child_die = die->child;
7717       while (child_die && child_die->tag)
7718         {
7719           LONGEST offset;
7720
7721           sym = new_symbol (child_die, NULL, cu);
7722           if (sym != NULL
7723               && handle_data_member_location (child_die, cu, &offset))
7724             {
7725               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7726               add_symbol_to_list (sym, &global_symbols);
7727             }
7728           child_die = sibling_die (child_die);
7729         }
7730     }
7731 }
7732
7733 /* Create a type for a C++ namespace.  */
7734
7735 static struct type *
7736 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7737 {
7738   struct objfile *objfile = cu->objfile;
7739   const char *previous_prefix, *name;
7740   int is_anonymous;
7741   struct type *type;
7742
7743   /* For extensions, reuse the type of the original namespace.  */
7744   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7745     {
7746       struct die_info *ext_die;
7747       struct dwarf2_cu *ext_cu = cu;
7748
7749       ext_die = dwarf2_extension (die, &ext_cu);
7750       type = read_type_die (ext_die, ext_cu);
7751
7752       /* EXT_CU may not be the same as CU.
7753          Ensure TYPE is recorded in CU's type_hash table.  */
7754       return set_die_type (die, type, cu);
7755     }
7756
7757   name = namespace_name (die, &is_anonymous, cu);
7758
7759   /* Now build the name of the current namespace.  */
7760
7761   previous_prefix = determine_prefix (die, cu);
7762   if (previous_prefix[0] != '\0')
7763     name = typename_concat (&objfile->objfile_obstack,
7764                             previous_prefix, name, 0, cu);
7765
7766   /* Create the type.  */
7767   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7768                     objfile);
7769   TYPE_NAME (type) = (char *) name;
7770   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7771
7772   return set_die_type (die, type, cu);
7773 }
7774
7775 /* Read a C++ namespace.  */
7776
7777 static void
7778 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7779 {
7780   struct objfile *objfile = cu->objfile;
7781   int is_anonymous;
7782
7783   /* Add a symbol associated to this if we haven't seen the namespace
7784      before.  Also, add a using directive if it's an anonymous
7785      namespace.  */
7786
7787   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7788     {
7789       struct type *type;
7790
7791       type = read_type_die (die, cu);
7792       new_symbol (die, type, cu);
7793
7794       namespace_name (die, &is_anonymous, cu);
7795       if (is_anonymous)
7796         {
7797           const char *previous_prefix = determine_prefix (die, cu);
7798
7799           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7800                                   NULL, &objfile->objfile_obstack);
7801         }
7802     }
7803
7804   if (die->child != NULL)
7805     {
7806       struct die_info *child_die = die->child;
7807
7808       while (child_die && child_die->tag)
7809         {
7810           process_die (child_die, cu);
7811           child_die = sibling_die (child_die);
7812         }
7813     }
7814 }
7815
7816 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7817    imported module.  Still we need that type as local Fortran "use ... only"
7818    declaration imports depend on the created type in determine_prefix.  */
7819
7820 static struct type *
7821 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7822 {
7823   struct objfile *objfile = cu->objfile;
7824   char *module_name;
7825   struct type *type;
7826
7827   module_name = dwarf2_name (die, cu);
7828   if (!module_name)
7829     complaint (&symfile_complaints,
7830                _("DW_TAG_module has no name, offset 0x%x"),
7831                die->offset);
7832   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7833
7834   /* determine_prefix uses TYPE_TAG_NAME.  */
7835   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7836
7837   return set_die_type (die, type, cu);
7838 }
7839
7840 /* Read a Fortran module.  */
7841
7842 static void
7843 read_module (struct die_info *die, struct dwarf2_cu *cu)
7844 {
7845   struct die_info *child_die = die->child;
7846
7847   while (child_die && child_die->tag)
7848     {
7849       process_die (child_die, cu);
7850       child_die = sibling_die (child_die);
7851     }
7852 }
7853
7854 /* Return the name of the namespace represented by DIE.  Set
7855    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7856    namespace.  */
7857
7858 static const char *
7859 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7860 {
7861   struct die_info *current_die;
7862   const char *name = NULL;
7863
7864   /* Loop through the extensions until we find a name.  */
7865
7866   for (current_die = die;
7867        current_die != NULL;
7868        current_die = dwarf2_extension (die, &cu))
7869     {
7870       name = dwarf2_name (current_die, cu);
7871       if (name != NULL)
7872         break;
7873     }
7874
7875   /* Is it an anonymous namespace?  */
7876
7877   *is_anonymous = (name == NULL);
7878   if (*is_anonymous)
7879     name = CP_ANONYMOUS_NAMESPACE_STR;
7880
7881   return name;
7882 }
7883
7884 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7885    the user defined type vector.  */
7886
7887 static struct type *
7888 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7889 {
7890   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7891   struct comp_unit_head *cu_header = &cu->header;
7892   struct type *type;
7893   struct attribute *attr_byte_size;
7894   struct attribute *attr_address_class;
7895   int byte_size, addr_class;
7896   struct type *target_type;
7897
7898   target_type = die_type (die, cu);
7899
7900   /* The die_type call above may have already set the type for this DIE.  */
7901   type = get_die_type (die, cu);
7902   if (type)
7903     return type;
7904
7905   type = lookup_pointer_type (target_type);
7906
7907   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7908   if (attr_byte_size)
7909     byte_size = DW_UNSND (attr_byte_size);
7910   else
7911     byte_size = cu_header->addr_size;
7912
7913   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7914   if (attr_address_class)
7915     addr_class = DW_UNSND (attr_address_class);
7916   else
7917     addr_class = DW_ADDR_none;
7918
7919   /* If the pointer size or address class is different than the
7920      default, create a type variant marked as such and set the
7921      length accordingly.  */
7922   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7923     {
7924       if (gdbarch_address_class_type_flags_p (gdbarch))
7925         {
7926           int type_flags;
7927
7928           type_flags = gdbarch_address_class_type_flags
7929                          (gdbarch, byte_size, addr_class);
7930           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7931                       == 0);
7932           type = make_type_with_address_space (type, type_flags);
7933         }
7934       else if (TYPE_LENGTH (type) != byte_size)
7935         {
7936           complaint (&symfile_complaints,
7937                      _("invalid pointer size %d"), byte_size);
7938         }
7939       else
7940         {
7941           /* Should we also complain about unhandled address classes?  */
7942         }
7943     }
7944
7945   TYPE_LENGTH (type) = byte_size;
7946   return set_die_type (die, type, cu);
7947 }
7948
7949 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7950    the user defined type vector.  */
7951
7952 static struct type *
7953 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7954 {
7955   struct type *type;
7956   struct type *to_type;
7957   struct type *domain;
7958
7959   to_type = die_type (die, cu);
7960   domain = die_containing_type (die, cu);
7961
7962   /* The calls above may have already set the type for this DIE.  */
7963   type = get_die_type (die, cu);
7964   if (type)
7965     return type;
7966
7967   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7968     type = lookup_methodptr_type (to_type);
7969   else
7970     type = lookup_memberptr_type (to_type, domain);
7971
7972   return set_die_type (die, type, cu);
7973 }
7974
7975 /* Extract all information from a DW_TAG_reference_type DIE and add to
7976    the user defined type vector.  */
7977
7978 static struct type *
7979 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7980 {
7981   struct comp_unit_head *cu_header = &cu->header;
7982   struct type *type, *target_type;
7983   struct attribute *attr;
7984
7985   target_type = die_type (die, cu);
7986
7987   /* The die_type call above may have already set the type for this DIE.  */
7988   type = get_die_type (die, cu);
7989   if (type)
7990     return type;
7991
7992   type = lookup_reference_type (target_type);
7993   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7994   if (attr)
7995     {
7996       TYPE_LENGTH (type) = DW_UNSND (attr);
7997     }
7998   else
7999     {
8000       TYPE_LENGTH (type) = cu_header->addr_size;
8001     }
8002   return set_die_type (die, type, cu);
8003 }
8004
8005 static struct type *
8006 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8007 {
8008   struct type *base_type, *cv_type;
8009
8010   base_type = die_type (die, cu);
8011
8012   /* The die_type call above may have already set the type for this DIE.  */
8013   cv_type = get_die_type (die, cu);
8014   if (cv_type)
8015     return cv_type;
8016
8017   /* In case the const qualifier is applied to an array type, the element type
8018      is so qualified, not the array type (section 6.7.3 of C99).  */
8019   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8020     {
8021       struct type *el_type, *inner_array;
8022
8023       base_type = copy_type (base_type);
8024       inner_array = base_type;
8025
8026       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8027         {
8028           TYPE_TARGET_TYPE (inner_array) =
8029             copy_type (TYPE_TARGET_TYPE (inner_array));
8030           inner_array = TYPE_TARGET_TYPE (inner_array);
8031         }
8032
8033       el_type = TYPE_TARGET_TYPE (inner_array);
8034       TYPE_TARGET_TYPE (inner_array) =
8035         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8036
8037       return set_die_type (die, base_type, cu);
8038     }
8039
8040   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8041   return set_die_type (die, cv_type, cu);
8042 }
8043
8044 static struct type *
8045 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8046 {
8047   struct type *base_type, *cv_type;
8048
8049   base_type = die_type (die, cu);
8050
8051   /* The die_type call above may have already set the type for this DIE.  */
8052   cv_type = get_die_type (die, cu);
8053   if (cv_type)
8054     return cv_type;
8055
8056   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8057   return set_die_type (die, cv_type, cu);
8058 }
8059
8060 /* Extract all information from a DW_TAG_string_type DIE and add to
8061    the user defined type vector.  It isn't really a user defined type,
8062    but it behaves like one, with other DIE's using an AT_user_def_type
8063    attribute to reference it.  */
8064
8065 static struct type *
8066 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8067 {
8068   struct objfile *objfile = cu->objfile;
8069   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8070   struct type *type, *range_type, *index_type, *char_type;
8071   struct attribute *attr;
8072   unsigned int length;
8073
8074   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8075   if (attr)
8076     {
8077       length = DW_UNSND (attr);
8078     }
8079   else
8080     {
8081       /* Check for the DW_AT_byte_size attribute.  */
8082       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8083       if (attr)
8084         {
8085           length = DW_UNSND (attr);
8086         }
8087       else
8088         {
8089           length = 1;
8090         }
8091     }
8092
8093   index_type = objfile_type (objfile)->builtin_int;
8094   range_type = create_range_type (NULL, index_type, 1, length);
8095   char_type = language_string_char_type (cu->language_defn, gdbarch);
8096   type = create_string_type (NULL, char_type, range_type);
8097
8098   return set_die_type (die, type, cu);
8099 }
8100
8101 /* Handle DIES due to C code like:
8102
8103    struct foo
8104    {
8105    int (*funcp)(int a, long l);
8106    int b;
8107    };
8108
8109    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8110
8111 static struct type *
8112 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8113 {
8114   struct type *type;            /* Type that this function returns.  */
8115   struct type *ftype;           /* Function that returns above type.  */
8116   struct attribute *attr;
8117
8118   type = die_type (die, cu);
8119
8120   /* The die_type call above may have already set the type for this DIE.  */
8121   ftype = get_die_type (die, cu);
8122   if (ftype)
8123     return ftype;
8124
8125   ftype = lookup_function_type (type);
8126
8127   /* All functions in C++, Pascal and Java have prototypes.  */
8128   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8129   if ((attr && (DW_UNSND (attr) != 0))
8130       || cu->language == language_cplus
8131       || cu->language == language_java
8132       || cu->language == language_pascal)
8133     TYPE_PROTOTYPED (ftype) = 1;
8134   else if (producer_is_realview (cu->producer))
8135     /* RealView does not emit DW_AT_prototyped.  We can not
8136        distinguish prototyped and unprototyped functions; default to
8137        prototyped, since that is more common in modern code (and
8138        RealView warns about unprototyped functions).  */
8139     TYPE_PROTOTYPED (ftype) = 1;
8140
8141   /* Store the calling convention in the type if it's available in
8142      the subroutine die.  Otherwise set the calling convention to
8143      the default value DW_CC_normal.  */
8144   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8145   if (attr)
8146     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8147   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8148     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8149   else
8150     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8151
8152   /* We need to add the subroutine type to the die immediately so
8153      we don't infinitely recurse when dealing with parameters
8154      declared as the same subroutine type.  */
8155   set_die_type (die, ftype, cu);
8156
8157   if (die->child != NULL)
8158     {
8159       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8160       struct die_info *child_die;
8161       int nparams, iparams;
8162
8163       /* Count the number of parameters.
8164          FIXME: GDB currently ignores vararg functions, but knows about
8165          vararg member functions.  */
8166       nparams = 0;
8167       child_die = die->child;
8168       while (child_die && child_die->tag)
8169         {
8170           if (child_die->tag == DW_TAG_formal_parameter)
8171             nparams++;
8172           else if (child_die->tag == DW_TAG_unspecified_parameters)
8173             TYPE_VARARGS (ftype) = 1;
8174           child_die = sibling_die (child_die);
8175         }
8176
8177       /* Allocate storage for parameters and fill them in.  */
8178       TYPE_NFIELDS (ftype) = nparams;
8179       TYPE_FIELDS (ftype) = (struct field *)
8180         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8181
8182       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8183          even if we error out during the parameters reading below.  */
8184       for (iparams = 0; iparams < nparams; iparams++)
8185         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8186
8187       iparams = 0;
8188       child_die = die->child;
8189       while (child_die && child_die->tag)
8190         {
8191           if (child_die->tag == DW_TAG_formal_parameter)
8192             {
8193               struct type *arg_type;
8194
8195               /* DWARF version 2 has no clean way to discern C++
8196                  static and non-static member functions.  G++ helps
8197                  GDB by marking the first parameter for non-static
8198                  member functions (which is the this pointer) as
8199                  artificial.  We pass this information to
8200                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8201
8202                  DWARF version 3 added DW_AT_object_pointer, which GCC
8203                  4.5 does not yet generate.  */
8204               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8205               if (attr)
8206                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8207               else
8208                 {
8209                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8210
8211                   /* GCC/43521: In java, the formal parameter
8212                      "this" is sometimes not marked with DW_AT_artificial.  */
8213                   if (cu->language == language_java)
8214                     {
8215                       const char *name = dwarf2_name (child_die, cu);
8216
8217                       if (name && !strcmp (name, "this"))
8218                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8219                     }
8220                 }
8221               arg_type = die_type (child_die, cu);
8222
8223               /* RealView does not mark THIS as const, which the testsuite
8224                  expects.  GCC marks THIS as const in method definitions,
8225                  but not in the class specifications (GCC PR 43053).  */
8226               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8227                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8228                 {
8229                   int is_this = 0;
8230                   struct dwarf2_cu *arg_cu = cu;
8231                   const char *name = dwarf2_name (child_die, cu);
8232
8233                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8234                   if (attr)
8235                     {
8236                       /* If the compiler emits this, use it.  */
8237                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8238                         is_this = 1;
8239                     }
8240                   else if (name && strcmp (name, "this") == 0)
8241                     /* Function definitions will have the argument names.  */
8242                     is_this = 1;
8243                   else if (name == NULL && iparams == 0)
8244                     /* Declarations may not have the names, so like
8245                        elsewhere in GDB, assume an artificial first
8246                        argument is "this".  */
8247                     is_this = 1;
8248
8249                   if (is_this)
8250                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8251                                              arg_type, 0);
8252                 }
8253
8254               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8255               iparams++;
8256             }
8257           child_die = sibling_die (child_die);
8258         }
8259     }
8260
8261   return ftype;
8262 }
8263
8264 static struct type *
8265 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8266 {
8267   struct objfile *objfile = cu->objfile;
8268   const char *name = NULL;
8269   struct type *this_type;
8270
8271   name = dwarf2_full_name (NULL, die, cu);
8272   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8273                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8274   TYPE_NAME (this_type) = (char *) name;
8275   set_die_type (die, this_type, cu);
8276   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8277   return this_type;
8278 }
8279
8280 /* Find a representation of a given base type and install
8281    it in the TYPE field of the die.  */
8282
8283 static struct type *
8284 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8285 {
8286   struct objfile *objfile = cu->objfile;
8287   struct type *type;
8288   struct attribute *attr;
8289   int encoding = 0, size = 0;
8290   char *name;
8291   enum type_code code = TYPE_CODE_INT;
8292   int type_flags = 0;
8293   struct type *target_type = NULL;
8294
8295   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8296   if (attr)
8297     {
8298       encoding = DW_UNSND (attr);
8299     }
8300   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8301   if (attr)
8302     {
8303       size = DW_UNSND (attr);
8304     }
8305   name = dwarf2_name (die, cu);
8306   if (!name)
8307     {
8308       complaint (&symfile_complaints,
8309                  _("DW_AT_name missing from DW_TAG_base_type"));
8310     }
8311
8312   switch (encoding)
8313     {
8314       case DW_ATE_address:
8315         /* Turn DW_ATE_address into a void * pointer.  */
8316         code = TYPE_CODE_PTR;
8317         type_flags |= TYPE_FLAG_UNSIGNED;
8318         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8319         break;
8320       case DW_ATE_boolean:
8321         code = TYPE_CODE_BOOL;
8322         type_flags |= TYPE_FLAG_UNSIGNED;
8323         break;
8324       case DW_ATE_complex_float:
8325         code = TYPE_CODE_COMPLEX;
8326         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8327         break;
8328       case DW_ATE_decimal_float:
8329         code = TYPE_CODE_DECFLOAT;
8330         break;
8331       case DW_ATE_float:
8332         code = TYPE_CODE_FLT;
8333         break;
8334       case DW_ATE_signed:
8335         break;
8336       case DW_ATE_unsigned:
8337         type_flags |= TYPE_FLAG_UNSIGNED;
8338         break;
8339       case DW_ATE_signed_char:
8340         if (cu->language == language_ada || cu->language == language_m2
8341             || cu->language == language_pascal)
8342           code = TYPE_CODE_CHAR;
8343         break;
8344       case DW_ATE_unsigned_char:
8345         if (cu->language == language_ada || cu->language == language_m2
8346             || cu->language == language_pascal)
8347           code = TYPE_CODE_CHAR;
8348         type_flags |= TYPE_FLAG_UNSIGNED;
8349         break;
8350       case DW_ATE_UTF:
8351         /* We just treat this as an integer and then recognize the
8352            type by name elsewhere.  */
8353         break;
8354
8355       default:
8356         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8357                    dwarf_type_encoding_name (encoding));
8358         break;
8359     }
8360
8361   type = init_type (code, size, type_flags, NULL, objfile);
8362   TYPE_NAME (type) = name;
8363   TYPE_TARGET_TYPE (type) = target_type;
8364
8365   if (name && strcmp (name, "char") == 0)
8366     TYPE_NOSIGN (type) = 1;
8367
8368   return set_die_type (die, type, cu);
8369 }
8370
8371 /* Read the given DW_AT_subrange DIE.  */
8372
8373 static struct type *
8374 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8375 {
8376   struct type *base_type;
8377   struct type *range_type;
8378   struct attribute *attr;
8379   LONGEST low = 0;
8380   LONGEST high = -1;
8381   char *name;
8382   LONGEST negative_mask;
8383
8384   base_type = die_type (die, cu);
8385   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8386   check_typedef (base_type);
8387
8388   /* The die_type call above may have already set the type for this DIE.  */
8389   range_type = get_die_type (die, cu);
8390   if (range_type)
8391     return range_type;
8392
8393   if (cu->language == language_fortran)
8394     {
8395       /* FORTRAN implies a lower bound of 1, if not given.  */
8396       low = 1;
8397     }
8398
8399   /* FIXME: For variable sized arrays either of these could be
8400      a variable rather than a constant value.  We'll allow it,
8401      but we don't know how to handle it.  */
8402   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8403   if (attr)
8404     low = dwarf2_get_attr_constant_value (attr, 0);
8405
8406   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8407   if (attr)
8408     {
8409       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8410         {
8411           /* GCC encodes arrays with unspecified or dynamic length
8412              with a DW_FORM_block1 attribute or a reference attribute.
8413              FIXME: GDB does not yet know how to handle dynamic
8414              arrays properly, treat them as arrays with unspecified
8415              length for now.
8416
8417              FIXME: jimb/2003-09-22: GDB does not really know
8418              how to handle arrays of unspecified length
8419              either; we just represent them as zero-length
8420              arrays.  Choose an appropriate upper bound given
8421              the lower bound we've computed above.  */
8422           high = low - 1;
8423         }
8424       else
8425         high = dwarf2_get_attr_constant_value (attr, 1);
8426     }
8427   else
8428     {
8429       attr = dwarf2_attr (die, DW_AT_count, cu);
8430       if (attr)
8431         {
8432           int count = dwarf2_get_attr_constant_value (attr, 1);
8433           high = low + count - 1;
8434         }
8435       else
8436         {
8437           /* Unspecified array length.  */
8438           high = low - 1;
8439         }
8440     }
8441
8442   /* Dwarf-2 specifications explicitly allows to create subrange types
8443      without specifying a base type.
8444      In that case, the base type must be set to the type of
8445      the lower bound, upper bound or count, in that order, if any of these
8446      three attributes references an object that has a type.
8447      If no base type is found, the Dwarf-2 specifications say that
8448      a signed integer type of size equal to the size of an address should
8449      be used.
8450      For the following C code: `extern char gdb_int [];'
8451      GCC produces an empty range DIE.
8452      FIXME: muller/2010-05-28: Possible references to object for low bound,
8453      high bound or count are not yet handled by this code.  */
8454   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8455     {
8456       struct objfile *objfile = cu->objfile;
8457       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8458       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8459       struct type *int_type = objfile_type (objfile)->builtin_int;
8460
8461       /* Test "int", "long int", and "long long int" objfile types,
8462          and select the first one having a size above or equal to the
8463          architecture address size.  */
8464       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8465         base_type = int_type;
8466       else
8467         {
8468           int_type = objfile_type (objfile)->builtin_long;
8469           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8470             base_type = int_type;
8471           else
8472             {
8473               int_type = objfile_type (objfile)->builtin_long_long;
8474               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8475                 base_type = int_type;
8476             }
8477         }
8478     }
8479
8480   negative_mask =
8481     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8482   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8483     low |= negative_mask;
8484   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8485     high |= negative_mask;
8486
8487   range_type = create_range_type (NULL, base_type, low, high);
8488
8489   /* Mark arrays with dynamic length at least as an array of unspecified
8490      length.  GDB could check the boundary but before it gets implemented at
8491      least allow accessing the array elements.  */
8492   if (attr && attr->form == DW_FORM_block1)
8493     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8494
8495   /* Ada expects an empty array on no boundary attributes.  */
8496   if (attr == NULL && cu->language != language_ada)
8497     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8498
8499   name = dwarf2_name (die, cu);
8500   if (name)
8501     TYPE_NAME (range_type) = name;
8502
8503   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8504   if (attr)
8505     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8506
8507   set_die_type (die, range_type, cu);
8508
8509   /* set_die_type should be already done.  */
8510   set_descriptive_type (range_type, die, cu);
8511
8512   return range_type;
8513 }
8514
8515 static struct type *
8516 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8517 {
8518   struct type *type;
8519
8520   /* For now, we only support the C meaning of an unspecified type: void.  */
8521
8522   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8523   TYPE_NAME (type) = dwarf2_name (die, cu);
8524
8525   return set_die_type (die, type, cu);
8526 }
8527
8528 /* Trivial hash function for die_info: the hash value of a DIE
8529    is its offset in .debug_info for this objfile.  */
8530
8531 static hashval_t
8532 die_hash (const void *item)
8533 {
8534   const struct die_info *die = item;
8535
8536   return die->offset;
8537 }
8538
8539 /* Trivial comparison function for die_info structures: two DIEs
8540    are equal if they have the same offset.  */
8541
8542 static int
8543 die_eq (const void *item_lhs, const void *item_rhs)
8544 {
8545   const struct die_info *die_lhs = item_lhs;
8546   const struct die_info *die_rhs = item_rhs;
8547
8548   return die_lhs->offset == die_rhs->offset;
8549 }
8550
8551 /* Read a whole compilation unit into a linked list of dies.  */
8552
8553 static struct die_info *
8554 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8555 {
8556   struct die_reader_specs reader_specs;
8557   int read_abbrevs = 0;
8558   struct cleanup *back_to = NULL;
8559   struct die_info *die;
8560
8561   if (cu->dwarf2_abbrevs == NULL)
8562     {
8563       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8564       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8565       read_abbrevs = 1;
8566     }
8567
8568   gdb_assert (cu->die_hash == NULL);
8569   cu->die_hash
8570     = htab_create_alloc_ex (cu->header.length / 12,
8571                             die_hash,
8572                             die_eq,
8573                             NULL,
8574                             &cu->comp_unit_obstack,
8575                             hashtab_obstack_allocate,
8576                             dummy_obstack_deallocate);
8577
8578   init_cu_die_reader (&reader_specs, cu);
8579
8580   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8581
8582   if (read_abbrevs)
8583     do_cleanups (back_to);
8584
8585   return die;
8586 }
8587
8588 /* Main entry point for reading a DIE and all children.
8589    Read the DIE and dump it if requested.  */
8590
8591 static struct die_info *
8592 read_die_and_children (const struct die_reader_specs *reader,
8593                        gdb_byte *info_ptr,
8594                        gdb_byte **new_info_ptr,
8595                        struct die_info *parent)
8596 {
8597   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8598                                                      new_info_ptr, parent);
8599
8600   if (dwarf2_die_debug)
8601     {
8602       fprintf_unfiltered (gdb_stdlog,
8603                           "\nRead die from %s of %s:\n",
8604                           reader->buffer == dwarf2_per_objfile->info.buffer
8605                           ? ".debug_info"
8606                           : reader->buffer == dwarf2_per_objfile->types.buffer
8607                           ? ".debug_types"
8608                           : "unknown section",
8609                           reader->abfd->filename);
8610       dump_die (result, dwarf2_die_debug);
8611     }
8612
8613   return result;
8614 }
8615
8616 /* Read a single die and all its descendents.  Set the die's sibling
8617    field to NULL; set other fields in the die correctly, and set all
8618    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8619    location of the info_ptr after reading all of those dies.  PARENT
8620    is the parent of the die in question.  */
8621
8622 static struct die_info *
8623 read_die_and_children_1 (const struct die_reader_specs *reader,
8624                          gdb_byte *info_ptr,
8625                          gdb_byte **new_info_ptr,
8626                          struct die_info *parent)
8627 {
8628   struct die_info *die;
8629   gdb_byte *cur_ptr;
8630   int has_children;
8631
8632   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8633   if (die == NULL)
8634     {
8635       *new_info_ptr = cur_ptr;
8636       return NULL;
8637     }
8638   store_in_ref_table (die, reader->cu);
8639
8640   if (has_children)
8641     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8642   else
8643     {
8644       die->child = NULL;
8645       *new_info_ptr = cur_ptr;
8646     }
8647
8648   die->sibling = NULL;
8649   die->parent = parent;
8650   return die;
8651 }
8652
8653 /* Read a die, all of its descendents, and all of its siblings; set
8654    all of the fields of all of the dies correctly.  Arguments are as
8655    in read_die_and_children.  */
8656
8657 static struct die_info *
8658 read_die_and_siblings (const struct die_reader_specs *reader,
8659                        gdb_byte *info_ptr,
8660                        gdb_byte **new_info_ptr,
8661                        struct die_info *parent)
8662 {
8663   struct die_info *first_die, *last_sibling;
8664   gdb_byte *cur_ptr;
8665
8666   cur_ptr = info_ptr;
8667   first_die = last_sibling = NULL;
8668
8669   while (1)
8670     {
8671       struct die_info *die
8672         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8673
8674       if (die == NULL)
8675         {
8676           *new_info_ptr = cur_ptr;
8677           return first_die;
8678         }
8679
8680       if (!first_die)
8681         first_die = die;
8682       else
8683         last_sibling->sibling = die;
8684
8685       last_sibling = die;
8686     }
8687 }
8688
8689 /* Read the die from the .debug_info section buffer.  Set DIEP to
8690    point to a newly allocated die with its information, except for its
8691    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8692    whether the die has children or not.  */
8693
8694 static gdb_byte *
8695 read_full_die (const struct die_reader_specs *reader,
8696                struct die_info **diep, gdb_byte *info_ptr,
8697                int *has_children)
8698 {
8699   unsigned int abbrev_number, bytes_read, i, offset;
8700   struct abbrev_info *abbrev;
8701   struct die_info *die;
8702   struct dwarf2_cu *cu = reader->cu;
8703   bfd *abfd = reader->abfd;
8704
8705   offset = info_ptr - reader->buffer;
8706   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8707   info_ptr += bytes_read;
8708   if (!abbrev_number)
8709     {
8710       *diep = NULL;
8711       *has_children = 0;
8712       return info_ptr;
8713     }
8714
8715   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8716   if (!abbrev)
8717     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8718            abbrev_number,
8719            bfd_get_filename (abfd));
8720
8721   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8722   die->offset = offset;
8723   die->tag = abbrev->tag;
8724   die->abbrev = abbrev_number;
8725
8726   die->num_attrs = abbrev->num_attrs;
8727
8728   for (i = 0; i < abbrev->num_attrs; ++i)
8729     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8730                                abfd, info_ptr, cu);
8731
8732   *diep = die;
8733   *has_children = abbrev->has_children;
8734   return info_ptr;
8735 }
8736
8737 /* In DWARF version 2, the description of the debugging information is
8738    stored in a separate .debug_abbrev section.  Before we read any
8739    dies from a section we read in all abbreviations and install them
8740    in a hash table.  This function also sets flags in CU describing
8741    the data found in the abbrev table.  */
8742
8743 static void
8744 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8745 {
8746   struct comp_unit_head *cu_header = &cu->header;
8747   gdb_byte *abbrev_ptr;
8748   struct abbrev_info *cur_abbrev;
8749   unsigned int abbrev_number, bytes_read, abbrev_name;
8750   unsigned int abbrev_form, hash_number;
8751   struct attr_abbrev *cur_attrs;
8752   unsigned int allocated_attrs;
8753
8754   /* Initialize dwarf2 abbrevs.  */
8755   obstack_init (&cu->abbrev_obstack);
8756   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8757                                       (ABBREV_HASH_SIZE
8758                                        * sizeof (struct abbrev_info *)));
8759   memset (cu->dwarf2_abbrevs, 0,
8760           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8761
8762   dwarf2_read_section (dwarf2_per_objfile->objfile,
8763                        &dwarf2_per_objfile->abbrev);
8764   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8765   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8766   abbrev_ptr += bytes_read;
8767
8768   allocated_attrs = ATTR_ALLOC_CHUNK;
8769   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8770
8771   /* Loop until we reach an abbrev number of 0.  */
8772   while (abbrev_number)
8773     {
8774       cur_abbrev = dwarf_alloc_abbrev (cu);
8775
8776       /* read in abbrev header */
8777       cur_abbrev->number = abbrev_number;
8778       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8779       abbrev_ptr += bytes_read;
8780       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8781       abbrev_ptr += 1;
8782
8783       if (cur_abbrev->tag == DW_TAG_namespace)
8784         cu->has_namespace_info = 1;
8785
8786       /* now read in declarations */
8787       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8788       abbrev_ptr += bytes_read;
8789       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8790       abbrev_ptr += bytes_read;
8791       while (abbrev_name)
8792         {
8793           if (cur_abbrev->num_attrs == allocated_attrs)
8794             {
8795               allocated_attrs += ATTR_ALLOC_CHUNK;
8796               cur_attrs
8797                 = xrealloc (cur_attrs, (allocated_attrs
8798                                         * sizeof (struct attr_abbrev)));
8799             }
8800
8801           /* Record whether this compilation unit might have
8802              inter-compilation-unit references.  If we don't know what form
8803              this attribute will have, then it might potentially be a
8804              DW_FORM_ref_addr, so we conservatively expect inter-CU
8805              references.  */
8806
8807           if (abbrev_form == DW_FORM_ref_addr
8808               || abbrev_form == DW_FORM_indirect)
8809             cu->has_form_ref_addr = 1;
8810
8811           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8812           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8813           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8814           abbrev_ptr += bytes_read;
8815           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8816           abbrev_ptr += bytes_read;
8817         }
8818
8819       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8820                                          (cur_abbrev->num_attrs
8821                                           * sizeof (struct attr_abbrev)));
8822       memcpy (cur_abbrev->attrs, cur_attrs,
8823               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8824
8825       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8826       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8827       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8828
8829       /* Get next abbreviation.
8830          Under Irix6 the abbreviations for a compilation unit are not
8831          always properly terminated with an abbrev number of 0.
8832          Exit loop if we encounter an abbreviation which we have
8833          already read (which means we are about to read the abbreviations
8834          for the next compile unit) or if the end of the abbreviation
8835          table is reached.  */
8836       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8837           >= dwarf2_per_objfile->abbrev.size)
8838         break;
8839       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8840       abbrev_ptr += bytes_read;
8841       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8842         break;
8843     }
8844
8845   xfree (cur_attrs);
8846 }
8847
8848 /* Release the memory used by the abbrev table for a compilation unit.  */
8849
8850 static void
8851 dwarf2_free_abbrev_table (void *ptr_to_cu)
8852 {
8853   struct dwarf2_cu *cu = ptr_to_cu;
8854
8855   obstack_free (&cu->abbrev_obstack, NULL);
8856   cu->dwarf2_abbrevs = NULL;
8857 }
8858
8859 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8860
8861 static struct abbrev_info *
8862 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8863 {
8864   unsigned int hash_number;
8865   struct abbrev_info *abbrev;
8866
8867   hash_number = number % ABBREV_HASH_SIZE;
8868   abbrev = cu->dwarf2_abbrevs[hash_number];
8869
8870   while (abbrev)
8871     {
8872       if (abbrev->number == number)
8873         return abbrev;
8874       else
8875         abbrev = abbrev->next;
8876     }
8877   return NULL;
8878 }
8879
8880 /* Returns nonzero if TAG represents a type that we might generate a partial
8881    symbol for.  */
8882
8883 static int
8884 is_type_tag_for_partial (int tag)
8885 {
8886   switch (tag)
8887     {
8888 #if 0
8889     /* Some types that would be reasonable to generate partial symbols for,
8890        that we don't at present.  */
8891     case DW_TAG_array_type:
8892     case DW_TAG_file_type:
8893     case DW_TAG_ptr_to_member_type:
8894     case DW_TAG_set_type:
8895     case DW_TAG_string_type:
8896     case DW_TAG_subroutine_type:
8897 #endif
8898     case DW_TAG_base_type:
8899     case DW_TAG_class_type:
8900     case DW_TAG_interface_type:
8901     case DW_TAG_enumeration_type:
8902     case DW_TAG_structure_type:
8903     case DW_TAG_subrange_type:
8904     case DW_TAG_typedef:
8905     case DW_TAG_union_type:
8906       return 1;
8907     default:
8908       return 0;
8909     }
8910 }
8911
8912 /* Load all DIEs that are interesting for partial symbols into memory.  */
8913
8914 static struct partial_die_info *
8915 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8916                    int building_psymtab, struct dwarf2_cu *cu)
8917 {
8918   struct partial_die_info *part_die;
8919   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8920   struct abbrev_info *abbrev;
8921   unsigned int bytes_read;
8922   unsigned int load_all = 0;
8923
8924   int nesting_level = 1;
8925
8926   parent_die = NULL;
8927   last_die = NULL;
8928
8929   if (cu->per_cu && cu->per_cu->load_all_dies)
8930     load_all = 1;
8931
8932   cu->partial_dies
8933     = htab_create_alloc_ex (cu->header.length / 12,
8934                             partial_die_hash,
8935                             partial_die_eq,
8936                             NULL,
8937                             &cu->comp_unit_obstack,
8938                             hashtab_obstack_allocate,
8939                             dummy_obstack_deallocate);
8940
8941   part_die = obstack_alloc (&cu->comp_unit_obstack,
8942                             sizeof (struct partial_die_info));
8943
8944   while (1)
8945     {
8946       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8947
8948       /* A NULL abbrev means the end of a series of children.  */
8949       if (abbrev == NULL)
8950         {
8951           if (--nesting_level == 0)
8952             {
8953               /* PART_DIE was probably the last thing allocated on the
8954                  comp_unit_obstack, so we could call obstack_free
8955                  here.  We don't do that because the waste is small,
8956                  and will be cleaned up when we're done with this
8957                  compilation unit.  This way, we're also more robust
8958                  against other users of the comp_unit_obstack.  */
8959               return first_die;
8960             }
8961           info_ptr += bytes_read;
8962           last_die = parent_die;
8963           parent_die = parent_die->die_parent;
8964           continue;
8965         }
8966
8967       /* Check for template arguments.  We never save these; if
8968          they're seen, we just mark the parent, and go on our way.  */
8969       if (parent_die != NULL
8970           && cu->language == language_cplus
8971           && (abbrev->tag == DW_TAG_template_type_param
8972               || abbrev->tag == DW_TAG_template_value_param))
8973         {
8974           parent_die->has_template_arguments = 1;
8975
8976           if (!load_all)
8977             {
8978               /* We don't need a partial DIE for the template argument.  */
8979               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8980                                        cu);
8981               continue;
8982             }
8983         }
8984
8985       /* We only recurse into subprograms looking for template arguments.
8986          Skip their other children.  */
8987       if (!load_all
8988           && cu->language == language_cplus
8989           && parent_die != NULL
8990           && parent_die->tag == DW_TAG_subprogram)
8991         {
8992           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8993           continue;
8994         }
8995
8996       /* Check whether this DIE is interesting enough to save.  Normally
8997          we would not be interested in members here, but there may be
8998          later variables referencing them via DW_AT_specification (for
8999          static members).  */
9000       if (!load_all
9001           && !is_type_tag_for_partial (abbrev->tag)
9002           && abbrev->tag != DW_TAG_constant
9003           && abbrev->tag != DW_TAG_enumerator
9004           && abbrev->tag != DW_TAG_subprogram
9005           && abbrev->tag != DW_TAG_lexical_block
9006           && abbrev->tag != DW_TAG_variable
9007           && abbrev->tag != DW_TAG_namespace
9008           && abbrev->tag != DW_TAG_module
9009           && abbrev->tag != DW_TAG_member)
9010         {
9011           /* Otherwise we skip to the next sibling, if any.  */
9012           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9013           continue;
9014         }
9015
9016       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9017                                    buffer, info_ptr, cu);
9018
9019       /* This two-pass algorithm for processing partial symbols has a
9020          high cost in cache pressure.  Thus, handle some simple cases
9021          here which cover the majority of C partial symbols.  DIEs
9022          which neither have specification tags in them, nor could have
9023          specification tags elsewhere pointing at them, can simply be
9024          processed and discarded.
9025
9026          This segment is also optional; scan_partial_symbols and
9027          add_partial_symbol will handle these DIEs if we chain
9028          them in normally.  When compilers which do not emit large
9029          quantities of duplicate debug information are more common,
9030          this code can probably be removed.  */
9031
9032       /* Any complete simple types at the top level (pretty much all
9033          of them, for a language without namespaces), can be processed
9034          directly.  */
9035       if (parent_die == NULL
9036           && part_die->has_specification == 0
9037           && part_die->is_declaration == 0
9038           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9039               || part_die->tag == DW_TAG_base_type
9040               || part_die->tag == DW_TAG_subrange_type))
9041         {
9042           if (building_psymtab && part_die->name != NULL)
9043             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9044                                  VAR_DOMAIN, LOC_TYPEDEF,
9045                                  &cu->objfile->static_psymbols,
9046                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9047           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9048           continue;
9049         }
9050
9051       /* The exception for DW_TAG_typedef with has_children above is
9052          a workaround of GCC PR debug/47510.  In the case of this complaint
9053          type_name_no_tag_or_error will error on such types later.
9054
9055          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9056          it could not find the child DIEs referenced later, this is checked
9057          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9058
9059       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9060         complaint (&symfile_complaints,
9061                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9062                      "- DIE at 0x%x [in module %s]"),
9063                    part_die->offset, cu->objfile->name);
9064
9065       /* If we're at the second level, and we're an enumerator, and
9066          our parent has no specification (meaning possibly lives in a
9067          namespace elsewhere), then we can add the partial symbol now
9068          instead of queueing it.  */
9069       if (part_die->tag == DW_TAG_enumerator
9070           && parent_die != NULL
9071           && parent_die->die_parent == NULL
9072           && parent_die->tag == DW_TAG_enumeration_type
9073           && parent_die->has_specification == 0)
9074         {
9075           if (part_die->name == NULL)
9076             complaint (&symfile_complaints,
9077                        _("malformed enumerator DIE ignored"));
9078           else if (building_psymtab)
9079             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9080                                  VAR_DOMAIN, LOC_CONST,
9081                                  (cu->language == language_cplus
9082                                   || cu->language == language_java)
9083                                  ? &cu->objfile->global_psymbols
9084                                  : &cu->objfile->static_psymbols,
9085                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9086
9087           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9088           continue;
9089         }
9090
9091       /* We'll save this DIE so link it in.  */
9092       part_die->die_parent = parent_die;
9093       part_die->die_sibling = NULL;
9094       part_die->die_child = NULL;
9095
9096       if (last_die && last_die == parent_die)
9097         last_die->die_child = part_die;
9098       else if (last_die)
9099         last_die->die_sibling = part_die;
9100
9101       last_die = part_die;
9102
9103       if (first_die == NULL)
9104         first_die = part_die;
9105
9106       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9107          find interesting need to be in the hash table, because we
9108          also have the parent/sibling/child chains; only those that we
9109          might refer to by offset later during partial symbol reading.
9110
9111          For now this means things that might have be the target of a
9112          DW_AT_specification, DW_AT_abstract_origin, or
9113          DW_AT_extension.  DW_AT_extension will refer only to
9114          namespaces; DW_AT_abstract_origin refers to functions (and
9115          many things under the function DIE, but we do not recurse
9116          into function DIEs during partial symbol reading) and
9117          possibly variables as well; DW_AT_specification refers to
9118          declarations.  Declarations ought to have the DW_AT_declaration
9119          flag.  It happens that GCC forgets to put it in sometimes, but
9120          only for functions, not for types.
9121
9122          Adding more things than necessary to the hash table is harmless
9123          except for the performance cost.  Adding too few will result in
9124          wasted time in find_partial_die, when we reread the compilation
9125          unit with load_all_dies set.  */
9126
9127       if (load_all
9128           || abbrev->tag == DW_TAG_constant
9129           || abbrev->tag == DW_TAG_subprogram
9130           || abbrev->tag == DW_TAG_variable
9131           || abbrev->tag == DW_TAG_namespace
9132           || part_die->is_declaration)
9133         {
9134           void **slot;
9135
9136           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9137                                            part_die->offset, INSERT);
9138           *slot = part_die;
9139         }
9140
9141       part_die = obstack_alloc (&cu->comp_unit_obstack,
9142                                 sizeof (struct partial_die_info));
9143
9144       /* For some DIEs we want to follow their children (if any).  For C
9145          we have no reason to follow the children of structures; for other
9146          languages we have to, so that we can get at method physnames
9147          to infer fully qualified class names, for DW_AT_specification,
9148          and for C++ template arguments.  For C++, we also look one level
9149          inside functions to find template arguments (if the name of the
9150          function does not already contain the template arguments).
9151
9152          For Ada, we need to scan the children of subprograms and lexical
9153          blocks as well because Ada allows the definition of nested
9154          entities that could be interesting for the debugger, such as
9155          nested subprograms for instance.  */
9156       if (last_die->has_children
9157           && (load_all
9158               || last_die->tag == DW_TAG_namespace
9159               || last_die->tag == DW_TAG_module
9160               || last_die->tag == DW_TAG_enumeration_type
9161               || (cu->language == language_cplus
9162                   && last_die->tag == DW_TAG_subprogram
9163                   && (last_die->name == NULL
9164                       || strchr (last_die->name, '<') == NULL))
9165               || (cu->language != language_c
9166                   && (last_die->tag == DW_TAG_class_type
9167                       || last_die->tag == DW_TAG_interface_type
9168                       || last_die->tag == DW_TAG_structure_type
9169                       || last_die->tag == DW_TAG_union_type))
9170               || (cu->language == language_ada
9171                   && (last_die->tag == DW_TAG_subprogram
9172                       || last_die->tag == DW_TAG_lexical_block))))
9173         {
9174           nesting_level++;
9175           parent_die = last_die;
9176           continue;
9177         }
9178
9179       /* Otherwise we skip to the next sibling, if any.  */
9180       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9181
9182       /* Back to the top, do it again.  */
9183     }
9184 }
9185
9186 /* Read a minimal amount of information into the minimal die structure.  */
9187
9188 static gdb_byte *
9189 read_partial_die (struct partial_die_info *part_die,
9190                   struct abbrev_info *abbrev,
9191                   unsigned int abbrev_len, bfd *abfd,
9192                   gdb_byte *buffer, gdb_byte *info_ptr,
9193                   struct dwarf2_cu *cu)
9194 {
9195   unsigned int i;
9196   struct attribute attr;
9197   int has_low_pc_attr = 0;
9198   int has_high_pc_attr = 0;
9199
9200   memset (part_die, 0, sizeof (struct partial_die_info));
9201
9202   part_die->offset = info_ptr - buffer;
9203
9204   info_ptr += abbrev_len;
9205
9206   if (abbrev == NULL)
9207     return info_ptr;
9208
9209   part_die->tag = abbrev->tag;
9210   part_die->has_children = abbrev->has_children;
9211
9212   for (i = 0; i < abbrev->num_attrs; ++i)
9213     {
9214       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9215
9216       /* Store the data if it is of an attribute we want to keep in a
9217          partial symbol table.  */
9218       switch (attr.name)
9219         {
9220         case DW_AT_name:
9221           switch (part_die->tag)
9222             {
9223             case DW_TAG_compile_unit:
9224             case DW_TAG_type_unit:
9225               /* Compilation units have a DW_AT_name that is a filename, not
9226                  a source language identifier.  */
9227             case DW_TAG_enumeration_type:
9228             case DW_TAG_enumerator:
9229               /* These tags always have simple identifiers already; no need
9230                  to canonicalize them.  */
9231               part_die->name = DW_STRING (&attr);
9232               break;
9233             default:
9234               part_die->name
9235                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9236                                             &cu->objfile->objfile_obstack);
9237               break;
9238             }
9239           break;
9240         case DW_AT_linkage_name:
9241         case DW_AT_MIPS_linkage_name:
9242           /* Note that both forms of linkage name might appear.  We
9243              assume they will be the same, and we only store the last
9244              one we see.  */
9245           if (cu->language == language_ada)
9246             part_die->name = DW_STRING (&attr);
9247           part_die->linkage_name = DW_STRING (&attr);
9248           break;
9249         case DW_AT_low_pc:
9250           has_low_pc_attr = 1;
9251           part_die->lowpc = DW_ADDR (&attr);
9252           break;
9253         case DW_AT_high_pc:
9254           has_high_pc_attr = 1;
9255           part_die->highpc = DW_ADDR (&attr);
9256           break;
9257         case DW_AT_location:
9258           /* Support the .debug_loc offsets.  */
9259           if (attr_form_is_block (&attr))
9260             {
9261                part_die->locdesc = DW_BLOCK (&attr);
9262             }
9263           else if (attr_form_is_section_offset (&attr))
9264             {
9265               dwarf2_complex_location_expr_complaint ();
9266             }
9267           else
9268             {
9269               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9270                                                      "partial symbol information");
9271             }
9272           break;
9273         case DW_AT_external:
9274           part_die->is_external = DW_UNSND (&attr);
9275           break;
9276         case DW_AT_declaration:
9277           part_die->is_declaration = DW_UNSND (&attr);
9278           break;
9279         case DW_AT_type:
9280           part_die->has_type = 1;
9281           break;
9282         case DW_AT_abstract_origin:
9283         case DW_AT_specification:
9284         case DW_AT_extension:
9285           part_die->has_specification = 1;
9286           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9287           break;
9288         case DW_AT_sibling:
9289           /* Ignore absolute siblings, they might point outside of
9290              the current compile unit.  */
9291           if (attr.form == DW_FORM_ref_addr)
9292             complaint (&symfile_complaints,
9293                        _("ignoring absolute DW_AT_sibling"));
9294           else
9295             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9296           break;
9297         case DW_AT_byte_size:
9298           part_die->has_byte_size = 1;
9299           break;
9300         case DW_AT_calling_convention:
9301           /* DWARF doesn't provide a way to identify a program's source-level
9302              entry point.  DW_AT_calling_convention attributes are only meant
9303              to describe functions' calling conventions.
9304
9305              However, because it's a necessary piece of information in
9306              Fortran, and because DW_CC_program is the only piece of debugging
9307              information whose definition refers to a 'main program' at all,
9308              several compilers have begun marking Fortran main programs with
9309              DW_CC_program --- even when those functions use the standard
9310              calling conventions.
9311
9312              So until DWARF specifies a way to provide this information and
9313              compilers pick up the new representation, we'll support this
9314              practice.  */
9315           if (DW_UNSND (&attr) == DW_CC_program
9316               && cu->language == language_fortran)
9317             {
9318               set_main_name (part_die->name);
9319
9320               /* As this DIE has a static linkage the name would be difficult
9321                  to look up later.  */
9322               language_of_main = language_fortran;
9323             }
9324           break;
9325         default:
9326           break;
9327         }
9328     }
9329
9330   if (has_low_pc_attr && has_high_pc_attr)
9331     {
9332       /* When using the GNU linker, .gnu.linkonce. sections are used to
9333          eliminate duplicate copies of functions and vtables and such.
9334          The linker will arbitrarily choose one and discard the others.
9335          The AT_*_pc values for such functions refer to local labels in
9336          these sections.  If the section from that file was discarded, the
9337          labels are not in the output, so the relocs get a value of 0.
9338          If this is a discarded function, mark the pc bounds as invalid,
9339          so that GDB will ignore it.  */
9340       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9341         {
9342           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9343
9344           complaint (&symfile_complaints,
9345                      _("DW_AT_low_pc %s is zero "
9346                        "for DIE at 0x%x [in module %s]"),
9347                      paddress (gdbarch, part_die->lowpc),
9348                      part_die->offset, cu->objfile->name);
9349         }
9350       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9351       else if (part_die->lowpc >= part_die->highpc)
9352         {
9353           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9354
9355           complaint (&symfile_complaints,
9356                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9357                        "for DIE at 0x%x [in module %s]"),
9358                      paddress (gdbarch, part_die->lowpc),
9359                      paddress (gdbarch, part_die->highpc),
9360                      part_die->offset, cu->objfile->name);
9361         }
9362       else
9363         part_die->has_pc_info = 1;
9364     }
9365
9366   return info_ptr;
9367 }
9368
9369 /* Find a cached partial DIE at OFFSET in CU.  */
9370
9371 static struct partial_die_info *
9372 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9373 {
9374   struct partial_die_info *lookup_die = NULL;
9375   struct partial_die_info part_die;
9376
9377   part_die.offset = offset;
9378   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9379
9380   return lookup_die;
9381 }
9382
9383 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9384    except in the case of .debug_types DIEs which do not reference
9385    outside their CU (they do however referencing other types via
9386    DW_FORM_ref_sig8).  */
9387
9388 static struct partial_die_info *
9389 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9390 {
9391   struct dwarf2_per_cu_data *per_cu = NULL;
9392   struct partial_die_info *pd = NULL;
9393
9394   if (cu->per_cu->from_debug_types)
9395     {
9396       pd = find_partial_die_in_comp_unit (offset, cu);
9397       if (pd != NULL)
9398         return pd;
9399       goto not_found;
9400     }
9401
9402   if (offset_in_cu_p (&cu->header, offset))
9403     {
9404       pd = find_partial_die_in_comp_unit (offset, cu);
9405       if (pd != NULL)
9406         return pd;
9407     }
9408
9409   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9410
9411   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9412     load_partial_comp_unit (per_cu, cu->objfile);
9413
9414   per_cu->cu->last_used = 0;
9415   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9416
9417   if (pd == NULL && per_cu->load_all_dies == 0)
9418     {
9419       struct cleanup *back_to;
9420       struct partial_die_info comp_unit_die;
9421       struct abbrev_info *abbrev;
9422       unsigned int bytes_read;
9423       char *info_ptr;
9424
9425       per_cu->load_all_dies = 1;
9426
9427       /* Re-read the DIEs.  */
9428       back_to = make_cleanup (null_cleanup, 0);
9429       if (per_cu->cu->dwarf2_abbrevs == NULL)
9430         {
9431           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9432           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9433         }
9434       info_ptr = (dwarf2_per_objfile->info.buffer
9435                   + per_cu->cu->header.offset
9436                   + per_cu->cu->header.first_die_offset);
9437       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9438       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9439                                    per_cu->cu->objfile->obfd,
9440                                    dwarf2_per_objfile->info.buffer, info_ptr,
9441                                    per_cu->cu);
9442       if (comp_unit_die.has_children)
9443         load_partial_dies (per_cu->cu->objfile->obfd,
9444                            dwarf2_per_objfile->info.buffer, info_ptr,
9445                            0, per_cu->cu);
9446       do_cleanups (back_to);
9447
9448       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9449     }
9450
9451  not_found:
9452
9453   if (pd == NULL)
9454     internal_error (__FILE__, __LINE__,
9455                     _("could not find partial DIE 0x%x "
9456                       "in cache [from module %s]\n"),
9457                     offset, bfd_get_filename (cu->objfile->obfd));
9458   return pd;
9459 }
9460
9461 /* See if we can figure out if the class lives in a namespace.  We do
9462    this by looking for a member function; its demangled name will
9463    contain namespace info, if there is any.  */
9464
9465 static void
9466 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9467                                   struct dwarf2_cu *cu)
9468 {
9469   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9470      what template types look like, because the demangler
9471      frequently doesn't give the same name as the debug info.  We
9472      could fix this by only using the demangled name to get the
9473      prefix (but see comment in read_structure_type).  */
9474
9475   struct partial_die_info *real_pdi;
9476   struct partial_die_info *child_pdi;
9477
9478   /* If this DIE (this DIE's specification, if any) has a parent, then
9479      we should not do this.  We'll prepend the parent's fully qualified
9480      name when we create the partial symbol.  */
9481
9482   real_pdi = struct_pdi;
9483   while (real_pdi->has_specification)
9484     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9485
9486   if (real_pdi->die_parent != NULL)
9487     return;
9488
9489   for (child_pdi = struct_pdi->die_child;
9490        child_pdi != NULL;
9491        child_pdi = child_pdi->die_sibling)
9492     {
9493       if (child_pdi->tag == DW_TAG_subprogram
9494           && child_pdi->linkage_name != NULL)
9495         {
9496           char *actual_class_name
9497             = language_class_name_from_physname (cu->language_defn,
9498                                                  child_pdi->linkage_name);
9499           if (actual_class_name != NULL)
9500             {
9501               struct_pdi->name
9502                 = obsavestring (actual_class_name,
9503                                 strlen (actual_class_name),
9504                                 &cu->objfile->objfile_obstack);
9505               xfree (actual_class_name);
9506             }
9507           break;
9508         }
9509     }
9510 }
9511
9512 /* Adjust PART_DIE before generating a symbol for it.  This function
9513    may set the is_external flag or change the DIE's name.  */
9514
9515 static void
9516 fixup_partial_die (struct partial_die_info *part_die,
9517                    struct dwarf2_cu *cu)
9518 {
9519   /* Once we've fixed up a die, there's no point in doing so again.
9520      This also avoids a memory leak if we were to call
9521      guess_partial_die_structure_name multiple times.  */
9522   if (part_die->fixup_called)
9523     return;
9524
9525   /* If we found a reference attribute and the DIE has no name, try
9526      to find a name in the referred to DIE.  */
9527
9528   if (part_die->name == NULL && part_die->has_specification)
9529     {
9530       struct partial_die_info *spec_die;
9531
9532       spec_die = find_partial_die (part_die->spec_offset, cu);
9533
9534       fixup_partial_die (spec_die, cu);
9535
9536       if (spec_die->name)
9537         {
9538           part_die->name = spec_die->name;
9539
9540           /* Copy DW_AT_external attribute if it is set.  */
9541           if (spec_die->is_external)
9542             part_die->is_external = spec_die->is_external;
9543         }
9544     }
9545
9546   /* Set default names for some unnamed DIEs.  */
9547
9548   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9549     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9550
9551   /* If there is no parent die to provide a namespace, and there are
9552      children, see if we can determine the namespace from their linkage
9553      name.
9554      NOTE: We need to do this even if cu->has_namespace_info != 0.
9555      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
9556   if (cu->language == language_cplus
9557       && dwarf2_per_objfile->types.asection != NULL
9558       && part_die->die_parent == NULL
9559       && part_die->has_children
9560       && (part_die->tag == DW_TAG_class_type
9561           || part_die->tag == DW_TAG_structure_type
9562           || part_die->tag == DW_TAG_union_type))
9563     guess_partial_die_structure_name (part_die, cu);
9564
9565   /* GCC might emit a nameless struct or union that has a linkage
9566      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
9567   if (part_die->name == NULL
9568       && (part_die->tag == DW_TAG_structure_type
9569           || part_die->tag == DW_TAG_union_type
9570           || part_die->tag == DW_TAG_class_type)
9571       && part_die->linkage_name != NULL)
9572     {
9573       char *demangled;
9574
9575       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9576       if (demangled)
9577         {
9578           part_die->name = obsavestring (demangled, strlen (demangled),
9579                                          &cu->objfile->objfile_obstack);
9580           xfree (demangled);
9581         }
9582     }
9583
9584   part_die->fixup_called = 1;
9585 }
9586
9587 /* Read an attribute value described by an attribute form.  */
9588
9589 static gdb_byte *
9590 read_attribute_value (struct attribute *attr, unsigned form,
9591                       bfd *abfd, gdb_byte *info_ptr,
9592                       struct dwarf2_cu *cu)
9593 {
9594   struct comp_unit_head *cu_header = &cu->header;
9595   unsigned int bytes_read;
9596   struct dwarf_block *blk;
9597
9598   attr->form = form;
9599   switch (form)
9600     {
9601     case DW_FORM_ref_addr:
9602       if (cu->header.version == 2)
9603         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9604       else
9605         DW_ADDR (attr) = read_offset (abfd, info_ptr,
9606                                       &cu->header, &bytes_read);
9607       info_ptr += bytes_read;
9608       break;
9609     case DW_FORM_addr:
9610       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9611       info_ptr += bytes_read;
9612       break;
9613     case DW_FORM_block2:
9614       blk = dwarf_alloc_block (cu);
9615       blk->size = read_2_bytes (abfd, info_ptr);
9616       info_ptr += 2;
9617       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9618       info_ptr += blk->size;
9619       DW_BLOCK (attr) = blk;
9620       break;
9621     case DW_FORM_block4:
9622       blk = dwarf_alloc_block (cu);
9623       blk->size = read_4_bytes (abfd, info_ptr);
9624       info_ptr += 4;
9625       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9626       info_ptr += blk->size;
9627       DW_BLOCK (attr) = blk;
9628       break;
9629     case DW_FORM_data2:
9630       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9631       info_ptr += 2;
9632       break;
9633     case DW_FORM_data4:
9634       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9635       info_ptr += 4;
9636       break;
9637     case DW_FORM_data8:
9638       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9639       info_ptr += 8;
9640       break;
9641     case DW_FORM_sec_offset:
9642       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9643       info_ptr += bytes_read;
9644       break;
9645     case DW_FORM_string:
9646       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9647       DW_STRING_IS_CANONICAL (attr) = 0;
9648       info_ptr += bytes_read;
9649       break;
9650     case DW_FORM_strp:
9651       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9652                                                &bytes_read);
9653       DW_STRING_IS_CANONICAL (attr) = 0;
9654       info_ptr += bytes_read;
9655       break;
9656     case DW_FORM_exprloc:
9657     case DW_FORM_block:
9658       blk = dwarf_alloc_block (cu);
9659       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9660       info_ptr += bytes_read;
9661       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9662       info_ptr += blk->size;
9663       DW_BLOCK (attr) = blk;
9664       break;
9665     case DW_FORM_block1:
9666       blk = dwarf_alloc_block (cu);
9667       blk->size = read_1_byte (abfd, info_ptr);
9668       info_ptr += 1;
9669       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9670       info_ptr += blk->size;
9671       DW_BLOCK (attr) = blk;
9672       break;
9673     case DW_FORM_data1:
9674       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9675       info_ptr += 1;
9676       break;
9677     case DW_FORM_flag:
9678       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9679       info_ptr += 1;
9680       break;
9681     case DW_FORM_flag_present:
9682       DW_UNSND (attr) = 1;
9683       break;
9684     case DW_FORM_sdata:
9685       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9686       info_ptr += bytes_read;
9687       break;
9688     case DW_FORM_udata:
9689       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9690       info_ptr += bytes_read;
9691       break;
9692     case DW_FORM_ref1:
9693       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9694       info_ptr += 1;
9695       break;
9696     case DW_FORM_ref2:
9697       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9698       info_ptr += 2;
9699       break;
9700     case DW_FORM_ref4:
9701       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9702       info_ptr += 4;
9703       break;
9704     case DW_FORM_ref8:
9705       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9706       info_ptr += 8;
9707       break;
9708     case DW_FORM_ref_sig8:
9709       /* Convert the signature to something we can record in DW_UNSND
9710          for later lookup.
9711          NOTE: This is NULL if the type wasn't found.  */
9712       DW_SIGNATURED_TYPE (attr) =
9713         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9714       info_ptr += 8;
9715       break;
9716     case DW_FORM_ref_udata:
9717       DW_ADDR (attr) = (cu->header.offset
9718                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9719       info_ptr += bytes_read;
9720       break;
9721     case DW_FORM_indirect:
9722       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9723       info_ptr += bytes_read;
9724       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9725       break;
9726     default:
9727       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9728              dwarf_form_name (form),
9729              bfd_get_filename (abfd));
9730     }
9731
9732   /* We have seen instances where the compiler tried to emit a byte
9733      size attribute of -1 which ended up being encoded as an unsigned
9734      0xffffffff.  Although 0xffffffff is technically a valid size value,
9735      an object of this size seems pretty unlikely so we can relatively
9736      safely treat these cases as if the size attribute was invalid and
9737      treat them as zero by default.  */
9738   if (attr->name == DW_AT_byte_size
9739       && form == DW_FORM_data4
9740       && DW_UNSND (attr) >= 0xffffffff)
9741     {
9742       complaint
9743         (&symfile_complaints,
9744          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9745          hex_string (DW_UNSND (attr)));
9746       DW_UNSND (attr) = 0;
9747     }
9748
9749   return info_ptr;
9750 }
9751
9752 /* Read an attribute described by an abbreviated attribute.  */
9753
9754 static gdb_byte *
9755 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9756                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9757 {
9758   attr->name = abbrev->name;
9759   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9760 }
9761
9762 /* Read dwarf information from a buffer.  */
9763
9764 static unsigned int
9765 read_1_byte (bfd *abfd, gdb_byte *buf)
9766 {
9767   return bfd_get_8 (abfd, buf);
9768 }
9769
9770 static int
9771 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9772 {
9773   return bfd_get_signed_8 (abfd, buf);
9774 }
9775
9776 static unsigned int
9777 read_2_bytes (bfd *abfd, gdb_byte *buf)
9778 {
9779   return bfd_get_16 (abfd, buf);
9780 }
9781
9782 static int
9783 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9784 {
9785   return bfd_get_signed_16 (abfd, buf);
9786 }
9787
9788 static unsigned int
9789 read_4_bytes (bfd *abfd, gdb_byte *buf)
9790 {
9791   return bfd_get_32 (abfd, buf);
9792 }
9793
9794 static int
9795 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9796 {
9797   return bfd_get_signed_32 (abfd, buf);
9798 }
9799
9800 static ULONGEST
9801 read_8_bytes (bfd *abfd, gdb_byte *buf)
9802 {
9803   return bfd_get_64 (abfd, buf);
9804 }
9805
9806 static CORE_ADDR
9807 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9808               unsigned int *bytes_read)
9809 {
9810   struct comp_unit_head *cu_header = &cu->header;
9811   CORE_ADDR retval = 0;
9812
9813   if (cu_header->signed_addr_p)
9814     {
9815       switch (cu_header->addr_size)
9816         {
9817         case 2:
9818           retval = bfd_get_signed_16 (abfd, buf);
9819           break;
9820         case 4:
9821           retval = bfd_get_signed_32 (abfd, buf);
9822           break;
9823         case 8:
9824           retval = bfd_get_signed_64 (abfd, buf);
9825           break;
9826         default:
9827           internal_error (__FILE__, __LINE__,
9828                           _("read_address: bad switch, signed [in module %s]"),
9829                           bfd_get_filename (abfd));
9830         }
9831     }
9832   else
9833     {
9834       switch (cu_header->addr_size)
9835         {
9836         case 2:
9837           retval = bfd_get_16 (abfd, buf);
9838           break;
9839         case 4:
9840           retval = bfd_get_32 (abfd, buf);
9841           break;
9842         case 8:
9843           retval = bfd_get_64 (abfd, buf);
9844           break;
9845         default:
9846           internal_error (__FILE__, __LINE__,
9847                           _("read_address: bad switch, "
9848                             "unsigned [in module %s]"),
9849                           bfd_get_filename (abfd));
9850         }
9851     }
9852
9853   *bytes_read = cu_header->addr_size;
9854   return retval;
9855 }
9856
9857 /* Read the initial length from a section.  The (draft) DWARF 3
9858    specification allows the initial length to take up either 4 bytes
9859    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9860    bytes describe the length and all offsets will be 8 bytes in length
9861    instead of 4.
9862
9863    An older, non-standard 64-bit format is also handled by this
9864    function.  The older format in question stores the initial length
9865    as an 8-byte quantity without an escape value.  Lengths greater
9866    than 2^32 aren't very common which means that the initial 4 bytes
9867    is almost always zero.  Since a length value of zero doesn't make
9868    sense for the 32-bit format, this initial zero can be considered to
9869    be an escape value which indicates the presence of the older 64-bit
9870    format.  As written, the code can't detect (old format) lengths
9871    greater than 4GB.  If it becomes necessary to handle lengths
9872    somewhat larger than 4GB, we could allow other small values (such
9873    as the non-sensical values of 1, 2, and 3) to also be used as
9874    escape values indicating the presence of the old format.
9875
9876    The value returned via bytes_read should be used to increment the
9877    relevant pointer after calling read_initial_length().
9878
9879    [ Note:  read_initial_length() and read_offset() are based on the
9880      document entitled "DWARF Debugging Information Format", revision
9881      3, draft 8, dated November 19, 2001.  This document was obtained
9882      from:
9883
9884         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9885
9886      This document is only a draft and is subject to change.  (So beware.)
9887
9888      Details regarding the older, non-standard 64-bit format were
9889      determined empirically by examining 64-bit ELF files produced by
9890      the SGI toolchain on an IRIX 6.5 machine.
9891
9892      - Kevin, July 16, 2002
9893    ] */
9894
9895 static LONGEST
9896 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9897 {
9898   LONGEST length = bfd_get_32 (abfd, buf);
9899
9900   if (length == 0xffffffff)
9901     {
9902       length = bfd_get_64 (abfd, buf + 4);
9903       *bytes_read = 12;
9904     }
9905   else if (length == 0)
9906     {
9907       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9908       length = bfd_get_64 (abfd, buf);
9909       *bytes_read = 8;
9910     }
9911   else
9912     {
9913       *bytes_read = 4;
9914     }
9915
9916   return length;
9917 }
9918
9919 /* Cover function for read_initial_length.
9920    Returns the length of the object at BUF, and stores the size of the
9921    initial length in *BYTES_READ and stores the size that offsets will be in
9922    *OFFSET_SIZE.
9923    If the initial length size is not equivalent to that specified in
9924    CU_HEADER then issue a complaint.
9925    This is useful when reading non-comp-unit headers.  */
9926
9927 static LONGEST
9928 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9929                                         const struct comp_unit_head *cu_header,
9930                                         unsigned int *bytes_read,
9931                                         unsigned int *offset_size)
9932 {
9933   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9934
9935   gdb_assert (cu_header->initial_length_size == 4
9936               || cu_header->initial_length_size == 8
9937               || cu_header->initial_length_size == 12);
9938
9939   if (cu_header->initial_length_size != *bytes_read)
9940     complaint (&symfile_complaints,
9941                _("intermixed 32-bit and 64-bit DWARF sections"));
9942
9943   *offset_size = (*bytes_read == 4) ? 4 : 8;
9944   return length;
9945 }
9946
9947 /* Read an offset from the data stream.  The size of the offset is
9948    given by cu_header->offset_size.  */
9949
9950 static LONGEST
9951 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9952              unsigned int *bytes_read)
9953 {
9954   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9955
9956   *bytes_read = cu_header->offset_size;
9957   return offset;
9958 }
9959
9960 /* Read an offset from the data stream.  */
9961
9962 static LONGEST
9963 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9964 {
9965   LONGEST retval = 0;
9966
9967   switch (offset_size)
9968     {
9969     case 4:
9970       retval = bfd_get_32 (abfd, buf);
9971       break;
9972     case 8:
9973       retval = bfd_get_64 (abfd, buf);
9974       break;
9975     default:
9976       internal_error (__FILE__, __LINE__,
9977                       _("read_offset_1: bad switch [in module %s]"),
9978                       bfd_get_filename (abfd));
9979     }
9980
9981   return retval;
9982 }
9983
9984 static gdb_byte *
9985 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9986 {
9987   /* If the size of a host char is 8 bits, we can return a pointer
9988      to the buffer, otherwise we have to copy the data to a buffer
9989      allocated on the temporary obstack.  */
9990   gdb_assert (HOST_CHAR_BIT == 8);
9991   return buf;
9992 }
9993
9994 static char *
9995 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9996 {
9997   /* If the size of a host char is 8 bits, we can return a pointer
9998      to the string, otherwise we have to copy the string to a buffer
9999      allocated on the temporary obstack.  */
10000   gdb_assert (HOST_CHAR_BIT == 8);
10001   if (*buf == '\0')
10002     {
10003       *bytes_read_ptr = 1;
10004       return NULL;
10005     }
10006   *bytes_read_ptr = strlen ((char *) buf) + 1;
10007   return (char *) buf;
10008 }
10009
10010 static char *
10011 read_indirect_string (bfd *abfd, gdb_byte *buf,
10012                       const struct comp_unit_head *cu_header,
10013                       unsigned int *bytes_read_ptr)
10014 {
10015   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10016
10017   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10018   if (dwarf2_per_objfile->str.buffer == NULL)
10019     {
10020       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10021                       bfd_get_filename (abfd));
10022       return NULL;
10023     }
10024   if (str_offset >= dwarf2_per_objfile->str.size)
10025     {
10026       error (_("DW_FORM_strp pointing outside of "
10027                ".debug_str section [in module %s]"),
10028              bfd_get_filename (abfd));
10029       return NULL;
10030     }
10031   gdb_assert (HOST_CHAR_BIT == 8);
10032   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10033     return NULL;
10034   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10035 }
10036
10037 static unsigned long
10038 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10039 {
10040   unsigned long result;
10041   unsigned int num_read;
10042   int i, shift;
10043   unsigned char byte;
10044
10045   result = 0;
10046   shift = 0;
10047   num_read = 0;
10048   i = 0;
10049   while (1)
10050     {
10051       byte = bfd_get_8 (abfd, buf);
10052       buf++;
10053       num_read++;
10054       result |= ((unsigned long)(byte & 127) << shift);
10055       if ((byte & 128) == 0)
10056         {
10057           break;
10058         }
10059       shift += 7;
10060     }
10061   *bytes_read_ptr = num_read;
10062   return result;
10063 }
10064
10065 static long
10066 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10067 {
10068   long result;
10069   int i, shift, num_read;
10070   unsigned char byte;
10071
10072   result = 0;
10073   shift = 0;
10074   num_read = 0;
10075   i = 0;
10076   while (1)
10077     {
10078       byte = bfd_get_8 (abfd, buf);
10079       buf++;
10080       num_read++;
10081       result |= ((long)(byte & 127) << shift);
10082       shift += 7;
10083       if ((byte & 128) == 0)
10084         {
10085           break;
10086         }
10087     }
10088   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10089     result |= -(((long)1) << shift);
10090   *bytes_read_ptr = num_read;
10091   return result;
10092 }
10093
10094 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10095
10096 static gdb_byte *
10097 skip_leb128 (bfd *abfd, gdb_byte *buf)
10098 {
10099   int byte;
10100
10101   while (1)
10102     {
10103       byte = bfd_get_8 (abfd, buf);
10104       buf++;
10105       if ((byte & 128) == 0)
10106         return buf;
10107     }
10108 }
10109
10110 static void
10111 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10112 {
10113   switch (lang)
10114     {
10115     case DW_LANG_C89:
10116     case DW_LANG_C99:
10117     case DW_LANG_C:
10118       cu->language = language_c;
10119       break;
10120     case DW_LANG_C_plus_plus:
10121       cu->language = language_cplus;
10122       break;
10123     case DW_LANG_D:
10124       cu->language = language_d;
10125       break;
10126     case DW_LANG_Fortran77:
10127     case DW_LANG_Fortran90:
10128     case DW_LANG_Fortran95:
10129       cu->language = language_fortran;
10130       break;
10131     case DW_LANG_Mips_Assembler:
10132       cu->language = language_asm;
10133       break;
10134     case DW_LANG_Java:
10135       cu->language = language_java;
10136       break;
10137     case DW_LANG_Ada83:
10138     case DW_LANG_Ada95:
10139       cu->language = language_ada;
10140       break;
10141     case DW_LANG_Modula2:
10142       cu->language = language_m2;
10143       break;
10144     case DW_LANG_Pascal83:
10145       cu->language = language_pascal;
10146       break;
10147     case DW_LANG_ObjC:
10148       cu->language = language_objc;
10149       break;
10150     case DW_LANG_Cobol74:
10151     case DW_LANG_Cobol85:
10152     default:
10153       cu->language = language_minimal;
10154       break;
10155     }
10156   cu->language_defn = language_def (cu->language);
10157 }
10158
10159 /* Return the named attribute or NULL if not there.  */
10160
10161 static struct attribute *
10162 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10163 {
10164   unsigned int i;
10165   struct attribute *spec = NULL;
10166
10167   for (i = 0; i < die->num_attrs; ++i)
10168     {
10169       if (die->attrs[i].name == name)
10170         return &die->attrs[i];
10171       if (die->attrs[i].name == DW_AT_specification
10172           || die->attrs[i].name == DW_AT_abstract_origin)
10173         spec = &die->attrs[i];
10174     }
10175
10176   if (spec)
10177     {
10178       die = follow_die_ref (die, spec, &cu);
10179       return dwarf2_attr (die, name, cu);
10180     }
10181
10182   return NULL;
10183 }
10184
10185 /* Return the named attribute or NULL if not there,
10186    but do not follow DW_AT_specification, etc.
10187    This is for use in contexts where we're reading .debug_types dies.
10188    Following DW_AT_specification, DW_AT_abstract_origin will take us
10189    back up the chain, and we want to go down.  */
10190
10191 static struct attribute *
10192 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10193                        struct dwarf2_cu *cu)
10194 {
10195   unsigned int i;
10196
10197   for (i = 0; i < die->num_attrs; ++i)
10198     if (die->attrs[i].name == name)
10199       return &die->attrs[i];
10200
10201   return NULL;
10202 }
10203
10204 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10205    and holds a non-zero value.  This function should only be used for
10206    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10207
10208 static int
10209 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10210 {
10211   struct attribute *attr = dwarf2_attr (die, name, cu);
10212
10213   return (attr && DW_UNSND (attr));
10214 }
10215
10216 static int
10217 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10218 {
10219   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10220      which value is non-zero.  However, we have to be careful with
10221      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10222      (via dwarf2_flag_true_p) follows this attribute.  So we may
10223      end up accidently finding a declaration attribute that belongs
10224      to a different DIE referenced by the specification attribute,
10225      even though the given DIE does not have a declaration attribute.  */
10226   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10227           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10228 }
10229
10230 /* Return the die giving the specification for DIE, if there is
10231    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10232    containing the return value on output.  If there is no
10233    specification, but there is an abstract origin, that is
10234    returned.  */
10235
10236 static struct die_info *
10237 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10238 {
10239   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10240                                              *spec_cu);
10241
10242   if (spec_attr == NULL)
10243     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10244
10245   if (spec_attr == NULL)
10246     return NULL;
10247   else
10248     return follow_die_ref (die, spec_attr, spec_cu);
10249 }
10250
10251 /* Free the line_header structure *LH, and any arrays and strings it
10252    refers to.
10253    NOTE: This is also used as a "cleanup" function.  */
10254
10255 static void
10256 free_line_header (struct line_header *lh)
10257 {
10258   if (lh->standard_opcode_lengths)
10259     xfree (lh->standard_opcode_lengths);
10260
10261   /* Remember that all the lh->file_names[i].name pointers are
10262      pointers into debug_line_buffer, and don't need to be freed.  */
10263   if (lh->file_names)
10264     xfree (lh->file_names);
10265
10266   /* Similarly for the include directory names.  */
10267   if (lh->include_dirs)
10268     xfree (lh->include_dirs);
10269
10270   xfree (lh);
10271 }
10272
10273 /* Add an entry to LH's include directory table.  */
10274
10275 static void
10276 add_include_dir (struct line_header *lh, char *include_dir)
10277 {
10278   /* Grow the array if necessary.  */
10279   if (lh->include_dirs_size == 0)
10280     {
10281       lh->include_dirs_size = 1; /* for testing */
10282       lh->include_dirs = xmalloc (lh->include_dirs_size
10283                                   * sizeof (*lh->include_dirs));
10284     }
10285   else if (lh->num_include_dirs >= lh->include_dirs_size)
10286     {
10287       lh->include_dirs_size *= 2;
10288       lh->include_dirs = xrealloc (lh->include_dirs,
10289                                    (lh->include_dirs_size
10290                                     * sizeof (*lh->include_dirs)));
10291     }
10292
10293   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10294 }
10295
10296 /* Add an entry to LH's file name table.  */
10297
10298 static void
10299 add_file_name (struct line_header *lh,
10300                char *name,
10301                unsigned int dir_index,
10302                unsigned int mod_time,
10303                unsigned int length)
10304 {
10305   struct file_entry *fe;
10306
10307   /* Grow the array if necessary.  */
10308   if (lh->file_names_size == 0)
10309     {
10310       lh->file_names_size = 1; /* for testing */
10311       lh->file_names = xmalloc (lh->file_names_size
10312                                 * sizeof (*lh->file_names));
10313     }
10314   else if (lh->num_file_names >= lh->file_names_size)
10315     {
10316       lh->file_names_size *= 2;
10317       lh->file_names = xrealloc (lh->file_names,
10318                                  (lh->file_names_size
10319                                   * sizeof (*lh->file_names)));
10320     }
10321
10322   fe = &lh->file_names[lh->num_file_names++];
10323   fe->name = name;
10324   fe->dir_index = dir_index;
10325   fe->mod_time = mod_time;
10326   fe->length = length;
10327   fe->included_p = 0;
10328   fe->symtab = NULL;
10329 }
10330
10331 /* Read the statement program header starting at OFFSET in
10332    .debug_line, according to the endianness of ABFD.  Return a pointer
10333    to a struct line_header, allocated using xmalloc.
10334
10335    NOTE: the strings in the include directory and file name tables of
10336    the returned object point into debug_line_buffer, and must not be
10337    freed.  */
10338
10339 static struct line_header *
10340 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10341                           struct dwarf2_cu *cu)
10342 {
10343   struct cleanup *back_to;
10344   struct line_header *lh;
10345   gdb_byte *line_ptr;
10346   unsigned int bytes_read, offset_size;
10347   int i;
10348   char *cur_dir, *cur_file;
10349
10350   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10351   if (dwarf2_per_objfile->line.buffer == NULL)
10352     {
10353       complaint (&symfile_complaints, _("missing .debug_line section"));
10354       return 0;
10355     }
10356
10357   /* Make sure that at least there's room for the total_length field.
10358      That could be 12 bytes long, but we're just going to fudge that.  */
10359   if (offset + 4 >= dwarf2_per_objfile->line.size)
10360     {
10361       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10362       return 0;
10363     }
10364
10365   lh = xmalloc (sizeof (*lh));
10366   memset (lh, 0, sizeof (*lh));
10367   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10368                           (void *) lh);
10369
10370   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10371
10372   /* Read in the header.  */
10373   lh->total_length =
10374     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10375                                             &bytes_read, &offset_size);
10376   line_ptr += bytes_read;
10377   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10378                                      + dwarf2_per_objfile->line.size))
10379     {
10380       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10381       return 0;
10382     }
10383   lh->statement_program_end = line_ptr + lh->total_length;
10384   lh->version = read_2_bytes (abfd, line_ptr);
10385   line_ptr += 2;
10386   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10387   line_ptr += offset_size;
10388   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10389   line_ptr += 1;
10390   if (lh->version >= 4)
10391     {
10392       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10393       line_ptr += 1;
10394     }
10395   else
10396     lh->maximum_ops_per_instruction = 1;
10397
10398   if (lh->maximum_ops_per_instruction == 0)
10399     {
10400       lh->maximum_ops_per_instruction = 1;
10401       complaint (&symfile_complaints,
10402                  _("invalid maximum_ops_per_instruction "
10403                    "in `.debug_line' section"));
10404     }
10405
10406   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10407   line_ptr += 1;
10408   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10409   line_ptr += 1;
10410   lh->line_range = read_1_byte (abfd, line_ptr);
10411   line_ptr += 1;
10412   lh->opcode_base = read_1_byte (abfd, line_ptr);
10413   line_ptr += 1;
10414   lh->standard_opcode_lengths
10415     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10416
10417   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10418   for (i = 1; i < lh->opcode_base; ++i)
10419     {
10420       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10421       line_ptr += 1;
10422     }
10423
10424   /* Read directory table.  */
10425   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10426     {
10427       line_ptr += bytes_read;
10428       add_include_dir (lh, cur_dir);
10429     }
10430   line_ptr += bytes_read;
10431
10432   /* Read file name table.  */
10433   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10434     {
10435       unsigned int dir_index, mod_time, length;
10436
10437       line_ptr += bytes_read;
10438       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10439       line_ptr += bytes_read;
10440       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10441       line_ptr += bytes_read;
10442       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10443       line_ptr += bytes_read;
10444
10445       add_file_name (lh, cur_file, dir_index, mod_time, length);
10446     }
10447   line_ptr += bytes_read;
10448   lh->statement_program_start = line_ptr;
10449
10450   if (line_ptr > (dwarf2_per_objfile->line.buffer
10451                   + dwarf2_per_objfile->line.size))
10452     complaint (&symfile_complaints,
10453                _("line number info header doesn't "
10454                  "fit in `.debug_line' section"));
10455
10456   discard_cleanups (back_to);
10457   return lh;
10458 }
10459
10460 /* This function exists to work around a bug in certain compilers
10461    (particularly GCC 2.95), in which the first line number marker of a
10462    function does not show up until after the prologue, right before
10463    the second line number marker.  This function shifts ADDRESS down
10464    to the beginning of the function if necessary, and is called on
10465    addresses passed to record_line.  */
10466
10467 static CORE_ADDR
10468 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10469 {
10470   struct function_range *fn;
10471
10472   /* Find the function_range containing address.  */
10473   if (!cu->first_fn)
10474     return address;
10475
10476   if (!cu->cached_fn)
10477     cu->cached_fn = cu->first_fn;
10478
10479   fn = cu->cached_fn;
10480   while (fn)
10481     if (fn->lowpc <= address && fn->highpc > address)
10482       goto found;
10483     else
10484       fn = fn->next;
10485
10486   fn = cu->first_fn;
10487   while (fn && fn != cu->cached_fn)
10488     if (fn->lowpc <= address && fn->highpc > address)
10489       goto found;
10490     else
10491       fn = fn->next;
10492
10493   return address;
10494
10495  found:
10496   if (fn->seen_line)
10497     return address;
10498   if (address != fn->lowpc)
10499     complaint (&symfile_complaints,
10500                _("misplaced first line number at 0x%lx for '%s'"),
10501                (unsigned long) address, fn->name);
10502   fn->seen_line = 1;
10503   return fn->lowpc;
10504 }
10505
10506 /* Subroutine of dwarf_decode_lines to simplify it.
10507    Return the file name of the psymtab for included file FILE_INDEX
10508    in line header LH of PST.
10509    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10510    If space for the result is malloc'd, it will be freed by a cleanup.
10511    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10512
10513 static char *
10514 psymtab_include_file_name (const struct line_header *lh, int file_index,
10515                            const struct partial_symtab *pst,
10516                            const char *comp_dir)
10517 {
10518   const struct file_entry fe = lh->file_names [file_index];
10519   char *include_name = fe.name;
10520   char *include_name_to_compare = include_name;
10521   char *dir_name = NULL;
10522   const char *pst_filename;
10523   char *copied_name = NULL;
10524   int file_is_pst;
10525
10526   if (fe.dir_index)
10527     dir_name = lh->include_dirs[fe.dir_index - 1];
10528
10529   if (!IS_ABSOLUTE_PATH (include_name)
10530       && (dir_name != NULL || comp_dir != NULL))
10531     {
10532       /* Avoid creating a duplicate psymtab for PST.
10533          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10534          Before we do the comparison, however, we need to account
10535          for DIR_NAME and COMP_DIR.
10536          First prepend dir_name (if non-NULL).  If we still don't
10537          have an absolute path prepend comp_dir (if non-NULL).
10538          However, the directory we record in the include-file's
10539          psymtab does not contain COMP_DIR (to match the
10540          corresponding symtab(s)).
10541
10542          Example:
10543
10544          bash$ cd /tmp
10545          bash$ gcc -g ./hello.c
10546          include_name = "hello.c"
10547          dir_name = "."
10548          DW_AT_comp_dir = comp_dir = "/tmp"
10549          DW_AT_name = "./hello.c"  */
10550
10551       if (dir_name != NULL)
10552         {
10553           include_name = concat (dir_name, SLASH_STRING,
10554                                  include_name, (char *)NULL);
10555           include_name_to_compare = include_name;
10556           make_cleanup (xfree, include_name);
10557         }
10558       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10559         {
10560           include_name_to_compare = concat (comp_dir, SLASH_STRING,
10561                                             include_name, (char *)NULL);
10562         }
10563     }
10564
10565   pst_filename = pst->filename;
10566   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10567     {
10568       copied_name = concat (pst->dirname, SLASH_STRING,
10569                             pst_filename, (char *)NULL);
10570       pst_filename = copied_name;
10571     }
10572
10573   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10574
10575   if (include_name_to_compare != include_name)
10576     xfree (include_name_to_compare);
10577   if (copied_name != NULL)
10578     xfree (copied_name);
10579
10580   if (file_is_pst)
10581     return NULL;
10582   return include_name;
10583 }
10584
10585 /* Ignore this record_line request.  */
10586
10587 static void
10588 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10589 {
10590   return;
10591 }
10592
10593 /* Decode the Line Number Program (LNP) for the given line_header
10594    structure and CU.  The actual information extracted and the type
10595    of structures created from the LNP depends on the value of PST.
10596
10597    1. If PST is NULL, then this procedure uses the data from the program
10598       to create all necessary symbol tables, and their linetables.
10599
10600    2. If PST is not NULL, this procedure reads the program to determine
10601       the list of files included by the unit represented by PST, and
10602       builds all the associated partial symbol tables.
10603
10604    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10605    It is used for relative paths in the line table.
10606    NOTE: When processing partial symtabs (pst != NULL),
10607    comp_dir == pst->dirname.
10608
10609    NOTE: It is important that psymtabs have the same file name (via strcmp)
10610    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
10611    symtab we don't use it in the name of the psymtabs we create.
10612    E.g. expand_line_sal requires this when finding psymtabs to expand.
10613    A good testcase for this is mb-inline.exp.  */
10614
10615 static void
10616 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10617                     struct dwarf2_cu *cu, struct partial_symtab *pst)
10618 {
10619   gdb_byte *line_ptr, *extended_end;
10620   gdb_byte *line_end;
10621   unsigned int bytes_read, extended_len;
10622   unsigned char op_code, extended_op, adj_opcode;
10623   CORE_ADDR baseaddr;
10624   struct objfile *objfile = cu->objfile;
10625   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10626   const int decode_for_pst_p = (pst != NULL);
10627   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10628   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10629     = record_line;
10630
10631   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10632
10633   line_ptr = lh->statement_program_start;
10634   line_end = lh->statement_program_end;
10635
10636   /* Read the statement sequences until there's nothing left.  */
10637   while (line_ptr < line_end)
10638     {
10639       /* state machine registers  */
10640       CORE_ADDR address = 0;
10641       unsigned int file = 1;
10642       unsigned int line = 1;
10643       unsigned int column = 0;
10644       int is_stmt = lh->default_is_stmt;
10645       int basic_block = 0;
10646       int end_sequence = 0;
10647       CORE_ADDR addr;
10648       unsigned char op_index = 0;
10649
10650       if (!decode_for_pst_p && lh->num_file_names >= file)
10651         {
10652           /* Start a subfile for the current file of the state machine.  */
10653           /* lh->include_dirs and lh->file_names are 0-based, but the
10654              directory and file name numbers in the statement program
10655              are 1-based.  */
10656           struct file_entry *fe = &lh->file_names[file - 1];
10657           char *dir = NULL;
10658
10659           if (fe->dir_index)
10660             dir = lh->include_dirs[fe->dir_index - 1];
10661
10662           dwarf2_start_subfile (fe->name, dir, comp_dir);
10663         }
10664
10665       /* Decode the table.  */
10666       while (!end_sequence)
10667         {
10668           op_code = read_1_byte (abfd, line_ptr);
10669           line_ptr += 1;
10670           if (line_ptr > line_end)
10671             {
10672               dwarf2_debug_line_missing_end_sequence_complaint ();
10673               break;
10674             }
10675
10676           if (op_code >= lh->opcode_base)
10677             {
10678               /* Special operand.  */
10679               adj_opcode = op_code - lh->opcode_base;
10680               address += (((op_index + (adj_opcode / lh->line_range))
10681                            / lh->maximum_ops_per_instruction)
10682                           * lh->minimum_instruction_length);
10683               op_index = ((op_index + (adj_opcode / lh->line_range))
10684                           % lh->maximum_ops_per_instruction);
10685               line += lh->line_base + (adj_opcode % lh->line_range);
10686               if (lh->num_file_names < file || file == 0)
10687                 dwarf2_debug_line_missing_file_complaint ();
10688               /* For now we ignore lines not starting on an
10689                  instruction boundary.  */
10690               else if (op_index == 0)
10691                 {
10692                   lh->file_names[file - 1].included_p = 1;
10693                   if (!decode_for_pst_p && is_stmt)
10694                     {
10695                       if (last_subfile != current_subfile)
10696                         {
10697                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10698                           if (last_subfile)
10699                             (*p_record_line) (last_subfile, 0, addr);
10700                           last_subfile = current_subfile;
10701                         }
10702                       /* Append row to matrix using current values.  */
10703                       addr = check_cu_functions (address, cu);
10704                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10705                       (*p_record_line) (current_subfile, line, addr);
10706                     }
10707                 }
10708               basic_block = 0;
10709             }
10710           else switch (op_code)
10711             {
10712             case DW_LNS_extended_op:
10713               extended_len = read_unsigned_leb128 (abfd, line_ptr,
10714                                                    &bytes_read);
10715               line_ptr += bytes_read;
10716               extended_end = line_ptr + extended_len;
10717               extended_op = read_1_byte (abfd, line_ptr);
10718               line_ptr += 1;
10719               switch (extended_op)
10720                 {
10721                 case DW_LNE_end_sequence:
10722                   p_record_line = record_line;
10723                   end_sequence = 1;
10724                   break;
10725                 case DW_LNE_set_address:
10726                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10727
10728                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10729                     {
10730                       /* This line table is for a function which has been
10731                          GCd by the linker.  Ignore it.  PR gdb/12528 */
10732
10733                       long line_offset
10734                         = line_ptr - dwarf2_per_objfile->line.buffer;
10735
10736                       complaint (&symfile_complaints,
10737                                  _(".debug_line address at offset 0x%lx is 0 "
10738                                    "[in module %s]"),
10739                                  line_offset, cu->objfile->name);
10740                       p_record_line = noop_record_line;
10741                     }
10742
10743                   op_index = 0;
10744                   line_ptr += bytes_read;
10745                   address += baseaddr;
10746                   break;
10747                 case DW_LNE_define_file:
10748                   {
10749                     char *cur_file;
10750                     unsigned int dir_index, mod_time, length;
10751
10752                     cur_file = read_direct_string (abfd, line_ptr,
10753                                                    &bytes_read);
10754                     line_ptr += bytes_read;
10755                     dir_index =
10756                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10757                     line_ptr += bytes_read;
10758                     mod_time =
10759                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10760                     line_ptr += bytes_read;
10761                     length =
10762                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10763                     line_ptr += bytes_read;
10764                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10765                   }
10766                   break;
10767                 case DW_LNE_set_discriminator:
10768                   /* The discriminator is not interesting to the debugger;
10769                      just ignore it.  */
10770                   line_ptr = extended_end;
10771                   break;
10772                 default:
10773                   complaint (&symfile_complaints,
10774                              _("mangled .debug_line section"));
10775                   return;
10776                 }
10777               /* Make sure that we parsed the extended op correctly.  If e.g.
10778                  we expected a different address size than the producer used,
10779                  we may have read the wrong number of bytes.  */
10780               if (line_ptr != extended_end)
10781                 {
10782                   complaint (&symfile_complaints,
10783                              _("mangled .debug_line section"));
10784                   return;
10785                 }
10786               break;
10787             case DW_LNS_copy:
10788               if (lh->num_file_names < file || file == 0)
10789                 dwarf2_debug_line_missing_file_complaint ();
10790               else
10791                 {
10792                   lh->file_names[file - 1].included_p = 1;
10793                   if (!decode_for_pst_p && is_stmt)
10794                     {
10795                       if (last_subfile != current_subfile)
10796                         {
10797                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10798                           if (last_subfile)
10799                             (*p_record_line) (last_subfile, 0, addr);
10800                           last_subfile = current_subfile;
10801                         }
10802                       addr = check_cu_functions (address, cu);
10803                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10804                       (*p_record_line) (current_subfile, line, addr);
10805                     }
10806                 }
10807               basic_block = 0;
10808               break;
10809             case DW_LNS_advance_pc:
10810               {
10811                 CORE_ADDR adjust
10812                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10813
10814                 address += (((op_index + adjust)
10815                              / lh->maximum_ops_per_instruction)
10816                             * lh->minimum_instruction_length);
10817                 op_index = ((op_index + adjust)
10818                             % lh->maximum_ops_per_instruction);
10819                 line_ptr += bytes_read;
10820               }
10821               break;
10822             case DW_LNS_advance_line:
10823               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10824               line_ptr += bytes_read;
10825               break;
10826             case DW_LNS_set_file:
10827               {
10828                 /* The arrays lh->include_dirs and lh->file_names are
10829                    0-based, but the directory and file name numbers in
10830                    the statement program are 1-based.  */
10831                 struct file_entry *fe;
10832                 char *dir = NULL;
10833
10834                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10835                 line_ptr += bytes_read;
10836                 if (lh->num_file_names < file || file == 0)
10837                   dwarf2_debug_line_missing_file_complaint ();
10838                 else
10839                   {
10840                     fe = &lh->file_names[file - 1];
10841                     if (fe->dir_index)
10842                       dir = lh->include_dirs[fe->dir_index - 1];
10843                     if (!decode_for_pst_p)
10844                       {
10845                         last_subfile = current_subfile;
10846                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10847                       }
10848                   }
10849               }
10850               break;
10851             case DW_LNS_set_column:
10852               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10853               line_ptr += bytes_read;
10854               break;
10855             case DW_LNS_negate_stmt:
10856               is_stmt = (!is_stmt);
10857               break;
10858             case DW_LNS_set_basic_block:
10859               basic_block = 1;
10860               break;
10861             /* Add to the address register of the state machine the
10862                address increment value corresponding to special opcode
10863                255.  I.e., this value is scaled by the minimum
10864                instruction length since special opcode 255 would have
10865                scaled the increment.  */
10866             case DW_LNS_const_add_pc:
10867               {
10868                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10869
10870                 address += (((op_index + adjust)
10871                              / lh->maximum_ops_per_instruction)
10872                             * lh->minimum_instruction_length);
10873                 op_index = ((op_index + adjust)
10874                             % lh->maximum_ops_per_instruction);
10875               }
10876               break;
10877             case DW_LNS_fixed_advance_pc:
10878               address += read_2_bytes (abfd, line_ptr);
10879               op_index = 0;
10880               line_ptr += 2;
10881               break;
10882             default:
10883               {
10884                 /* Unknown standard opcode, ignore it.  */
10885                 int i;
10886
10887                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10888                   {
10889                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10890                     line_ptr += bytes_read;
10891                   }
10892               }
10893             }
10894         }
10895       if (lh->num_file_names < file || file == 0)
10896         dwarf2_debug_line_missing_file_complaint ();
10897       else
10898         {
10899           lh->file_names[file - 1].included_p = 1;
10900           if (!decode_for_pst_p)
10901             {
10902               addr = gdbarch_addr_bits_remove (gdbarch, address);
10903               (*p_record_line) (current_subfile, 0, addr);
10904             }
10905         }
10906     }
10907
10908   if (decode_for_pst_p)
10909     {
10910       int file_index;
10911
10912       /* Now that we're done scanning the Line Header Program, we can
10913          create the psymtab of each included file.  */
10914       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10915         if (lh->file_names[file_index].included_p == 1)
10916           {
10917             char *include_name =
10918               psymtab_include_file_name (lh, file_index, pst, comp_dir);
10919             if (include_name != NULL)
10920               dwarf2_create_include_psymtab (include_name, pst, objfile);
10921           }
10922     }
10923   else
10924     {
10925       /* Make sure a symtab is created for every file, even files
10926          which contain only variables (i.e. no code with associated
10927          line numbers).  */
10928
10929       int i;
10930       struct file_entry *fe;
10931
10932       for (i = 0; i < lh->num_file_names; i++)
10933         {
10934           char *dir = NULL;
10935
10936           fe = &lh->file_names[i];
10937           if (fe->dir_index)
10938             dir = lh->include_dirs[fe->dir_index - 1];
10939           dwarf2_start_subfile (fe->name, dir, comp_dir);
10940
10941           /* Skip the main file; we don't need it, and it must be
10942              allocated last, so that it will show up before the
10943              non-primary symtabs in the objfile's symtab list.  */
10944           if (current_subfile == first_subfile)
10945             continue;
10946
10947           if (current_subfile->symtab == NULL)
10948             current_subfile->symtab = allocate_symtab (current_subfile->name,
10949                                                        cu->objfile);
10950           fe->symtab = current_subfile->symtab;
10951         }
10952     }
10953 }
10954
10955 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10956    DIRNAME the name of the source directory which contains FILENAME
10957    or NULL if not known.  COMP_DIR is the compilation directory for the
10958    linetable's compilation unit or NULL if not known.
10959    This routine tries to keep line numbers from identical absolute and
10960    relative file names in a common subfile.
10961
10962    Using the `list' example from the GDB testsuite, which resides in
10963    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10964    of /srcdir/list0.c yields the following debugging information for list0.c:
10965
10966    DW_AT_name:          /srcdir/list0.c
10967    DW_AT_comp_dir:              /compdir
10968    files.files[0].name: list0.h
10969    files.files[0].dir:  /srcdir
10970    files.files[1].name: list0.c
10971    files.files[1].dir:  /srcdir
10972
10973    The line number information for list0.c has to end up in a single
10974    subfile, so that `break /srcdir/list0.c:1' works as expected.
10975    start_subfile will ensure that this happens provided that we pass the
10976    concatenation of files.files[1].dir and files.files[1].name as the
10977    subfile's name.  */
10978
10979 static void
10980 dwarf2_start_subfile (char *filename, const char *dirname,
10981                       const char *comp_dir)
10982 {
10983   char *fullname;
10984
10985   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10986      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10987      second argument to start_subfile.  To be consistent, we do the
10988      same here.  In order not to lose the line information directory,
10989      we concatenate it to the filename when it makes sense.
10990      Note that the Dwarf3 standard says (speaking of filenames in line
10991      information): ``The directory index is ignored for file names
10992      that represent full path names''.  Thus ignoring dirname in the
10993      `else' branch below isn't an issue.  */
10994
10995   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10996     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10997   else
10998     fullname = filename;
10999
11000   start_subfile (fullname, comp_dir);
11001
11002   if (fullname != filename)
11003     xfree (fullname);
11004 }
11005
11006 static void
11007 var_decode_location (struct attribute *attr, struct symbol *sym,
11008                      struct dwarf2_cu *cu)
11009 {
11010   struct objfile *objfile = cu->objfile;
11011   struct comp_unit_head *cu_header = &cu->header;
11012
11013   /* NOTE drow/2003-01-30: There used to be a comment and some special
11014      code here to turn a symbol with DW_AT_external and a
11015      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11016      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11017      with some versions of binutils) where shared libraries could have
11018      relocations against symbols in their debug information - the
11019      minimal symbol would have the right address, but the debug info
11020      would not.  It's no longer necessary, because we will explicitly
11021      apply relocations when we read in the debug information now.  */
11022
11023   /* A DW_AT_location attribute with no contents indicates that a
11024      variable has been optimized away.  */
11025   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11026     {
11027       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11028       return;
11029     }
11030
11031   /* Handle one degenerate form of location expression specially, to
11032      preserve GDB's previous behavior when section offsets are
11033      specified.  If this is just a DW_OP_addr then mark this symbol
11034      as LOC_STATIC.  */
11035
11036   if (attr_form_is_block (attr)
11037       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11038       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11039     {
11040       unsigned int dummy;
11041
11042       SYMBOL_VALUE_ADDRESS (sym) =
11043         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11044       SYMBOL_CLASS (sym) = LOC_STATIC;
11045       fixup_symbol_section (sym, objfile);
11046       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11047                                               SYMBOL_SECTION (sym));
11048       return;
11049     }
11050
11051   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11052      expression evaluator, and use LOC_COMPUTED only when necessary
11053      (i.e. when the value of a register or memory location is
11054      referenced, or a thread-local block, etc.).  Then again, it might
11055      not be worthwhile.  I'm assuming that it isn't unless performance
11056      or memory numbers show me otherwise.  */
11057
11058   dwarf2_symbol_mark_computed (attr, sym, cu);
11059   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11060
11061   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11062     cu->has_loclist = 1;
11063 }
11064
11065 /* Given a pointer to a DWARF information entry, figure out if we need
11066    to make a symbol table entry for it, and if so, create a new entry
11067    and return a pointer to it.
11068    If TYPE is NULL, determine symbol type from the die, otherwise
11069    used the passed type.
11070    If SPACE is not NULL, use it to hold the new symbol.  If it is
11071    NULL, allocate a new symbol on the objfile's obstack.  */
11072
11073 static struct symbol *
11074 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11075                  struct symbol *space)
11076 {
11077   struct objfile *objfile = cu->objfile;
11078   struct symbol *sym = NULL;
11079   char *name;
11080   struct attribute *attr = NULL;
11081   struct attribute *attr2 = NULL;
11082   CORE_ADDR baseaddr;
11083   struct pending **list_to_add = NULL;
11084
11085   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11086
11087   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11088
11089   name = dwarf2_name (die, cu);
11090   if (name)
11091     {
11092       const char *linkagename;
11093       int suppress_add = 0;
11094
11095       if (space)
11096         sym = space;
11097       else
11098         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11099       OBJSTAT (objfile, n_syms++);
11100
11101       /* Cache this symbol's name and the name's demangled form (if any).  */
11102       SYMBOL_SET_LANGUAGE (sym, cu->language);
11103       linkagename = dwarf2_physname (name, die, cu);
11104       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11105
11106       /* Fortran does not have mangling standard and the mangling does differ
11107          between gfortran, iFort etc.  */
11108       if (cu->language == language_fortran
11109           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11110         symbol_set_demangled_name (&(sym->ginfo),
11111                                    (char *) dwarf2_full_name (name, die, cu),
11112                                    NULL);
11113
11114       /* Default assumptions.
11115          Use the passed type or decode it from the die.  */
11116       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11117       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11118       if (type != NULL)
11119         SYMBOL_TYPE (sym) = type;
11120       else
11121         SYMBOL_TYPE (sym) = die_type (die, cu);
11122       attr = dwarf2_attr (die,
11123                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11124                           cu);
11125       if (attr)
11126         {
11127           SYMBOL_LINE (sym) = DW_UNSND (attr);
11128         }
11129
11130       attr = dwarf2_attr (die,
11131                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11132                           cu);
11133       if (attr)
11134         {
11135           int file_index = DW_UNSND (attr);
11136
11137           if (cu->line_header == NULL
11138               || file_index > cu->line_header->num_file_names)
11139             complaint (&symfile_complaints,
11140                        _("file index out of range"));
11141           else if (file_index > 0)
11142             {
11143               struct file_entry *fe;
11144
11145               fe = &cu->line_header->file_names[file_index - 1];
11146               SYMBOL_SYMTAB (sym) = fe->symtab;
11147             }
11148         }
11149
11150       switch (die->tag)
11151         {
11152         case DW_TAG_label:
11153           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11154           if (attr)
11155             {
11156               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11157             }
11158           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11159           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11160           SYMBOL_CLASS (sym) = LOC_LABEL;
11161           add_symbol_to_list (sym, cu->list_in_scope);
11162           break;
11163         case DW_TAG_subprogram:
11164           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11165              finish_block.  */
11166           SYMBOL_CLASS (sym) = LOC_BLOCK;
11167           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11168           if ((attr2 && (DW_UNSND (attr2) != 0))
11169               || cu->language == language_ada)
11170             {
11171               /* Subprograms marked external are stored as a global symbol.
11172                  Ada subprograms, whether marked external or not, are always
11173                  stored as a global symbol, because we want to be able to
11174                  access them globally.  For instance, we want to be able
11175                  to break on a nested subprogram without having to
11176                  specify the context.  */
11177               list_to_add = &global_symbols;
11178             }
11179           else
11180             {
11181               list_to_add = cu->list_in_scope;
11182             }
11183           break;
11184         case DW_TAG_inlined_subroutine:
11185           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11186              finish_block.  */
11187           SYMBOL_CLASS (sym) = LOC_BLOCK;
11188           SYMBOL_INLINED (sym) = 1;
11189           /* Do not add the symbol to any lists.  It will be found via
11190              BLOCK_FUNCTION from the blockvector.  */
11191           break;
11192         case DW_TAG_template_value_param:
11193           suppress_add = 1;
11194           /* Fall through.  */
11195         case DW_TAG_constant:
11196         case DW_TAG_variable:
11197         case DW_TAG_member:
11198           /* Compilation with minimal debug info may result in
11199              variables with missing type entries.  Change the
11200              misleading `void' type to something sensible.  */
11201           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11202             SYMBOL_TYPE (sym)
11203               = objfile_type (objfile)->nodebug_data_symbol;
11204
11205           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11206           /* In the case of DW_TAG_member, we should only be called for
11207              static const members.  */
11208           if (die->tag == DW_TAG_member)
11209             {
11210               /* dwarf2_add_field uses die_is_declaration,
11211                  so we do the same.  */
11212               gdb_assert (die_is_declaration (die, cu));
11213               gdb_assert (attr);
11214             }
11215           if (attr)
11216             {
11217               dwarf2_const_value (attr, sym, cu);
11218               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11219               if (!suppress_add)
11220                 {
11221                   if (attr2 && (DW_UNSND (attr2) != 0))
11222                     list_to_add = &global_symbols;
11223                   else
11224                     list_to_add = cu->list_in_scope;
11225                 }
11226               break;
11227             }
11228           attr = dwarf2_attr (die, DW_AT_location, cu);
11229           if (attr)
11230             {
11231               var_decode_location (attr, sym, cu);
11232               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11233               if (SYMBOL_CLASS (sym) == LOC_STATIC
11234                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11235                   && !dwarf2_per_objfile->has_section_at_zero)
11236                 {
11237                   /* When a static variable is eliminated by the linker,
11238                      the corresponding debug information is not stripped
11239                      out, but the variable address is set to null;
11240                      do not add such variables into symbol table.  */
11241                 }
11242               else if (attr2 && (DW_UNSND (attr2) != 0))
11243                 {
11244                   /* Workaround gfortran PR debug/40040 - it uses
11245                      DW_AT_location for variables in -fPIC libraries which may
11246                      get overriden by other libraries/executable and get
11247                      a different address.  Resolve it by the minimal symbol
11248                      which may come from inferior's executable using copy
11249                      relocation.  Make this workaround only for gfortran as for
11250                      other compilers GDB cannot guess the minimal symbol
11251                      Fortran mangling kind.  */
11252                   if (cu->language == language_fortran && die->parent
11253                       && die->parent->tag == DW_TAG_module
11254                       && cu->producer
11255                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11256                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11257
11258                   /* A variable with DW_AT_external is never static,
11259                      but it may be block-scoped.  */
11260                   list_to_add = (cu->list_in_scope == &file_symbols
11261                                  ? &global_symbols : cu->list_in_scope);
11262                 }
11263               else
11264                 list_to_add = cu->list_in_scope;
11265             }
11266           else
11267             {
11268               /* We do not know the address of this symbol.
11269                  If it is an external symbol and we have type information
11270                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11271                  The address of the variable will then be determined from
11272                  the minimal symbol table whenever the variable is
11273                  referenced.  */
11274               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11275               if (attr2 && (DW_UNSND (attr2) != 0)
11276                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11277                 {
11278                   /* A variable with DW_AT_external is never static, but it
11279                      may be block-scoped.  */
11280                   list_to_add = (cu->list_in_scope == &file_symbols
11281                                  ? &global_symbols : cu->list_in_scope);
11282
11283                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11284                 }
11285               else if (!die_is_declaration (die, cu))
11286                 {
11287                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11288                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11289                   if (!suppress_add)
11290                     list_to_add = cu->list_in_scope;
11291                 }
11292             }
11293           break;
11294         case DW_TAG_formal_parameter:
11295           /* If we are inside a function, mark this as an argument.  If
11296              not, we might be looking at an argument to an inlined function
11297              when we do not have enough information to show inlined frames;
11298              pretend it's a local variable in that case so that the user can
11299              still see it.  */
11300           if (context_stack_depth > 0
11301               && context_stack[context_stack_depth - 1].name != NULL)
11302             SYMBOL_IS_ARGUMENT (sym) = 1;
11303           attr = dwarf2_attr (die, DW_AT_location, cu);
11304           if (attr)
11305             {
11306               var_decode_location (attr, sym, cu);
11307             }
11308           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11309           if (attr)
11310             {
11311               dwarf2_const_value (attr, sym, cu);
11312             }
11313           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11314           if (attr && DW_UNSND (attr))
11315             {
11316               struct type *ref_type;
11317
11318               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11319               SYMBOL_TYPE (sym) = ref_type;
11320             }
11321
11322           list_to_add = cu->list_in_scope;
11323           break;
11324         case DW_TAG_unspecified_parameters:
11325           /* From varargs functions; gdb doesn't seem to have any
11326              interest in this information, so just ignore it for now.
11327              (FIXME?) */
11328           break;
11329         case DW_TAG_template_type_param:
11330           suppress_add = 1;
11331           /* Fall through.  */
11332         case DW_TAG_class_type:
11333         case DW_TAG_interface_type:
11334         case DW_TAG_structure_type:
11335         case DW_TAG_union_type:
11336         case DW_TAG_set_type:
11337         case DW_TAG_enumeration_type:
11338           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11339           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11340
11341           {
11342             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11343                really ever be static objects: otherwise, if you try
11344                to, say, break of a class's method and you're in a file
11345                which doesn't mention that class, it won't work unless
11346                the check for all static symbols in lookup_symbol_aux
11347                saves you.  See the OtherFileClass tests in
11348                gdb.c++/namespace.exp.  */
11349
11350             if (!suppress_add)
11351               {
11352                 list_to_add = (cu->list_in_scope == &file_symbols
11353                                && (cu->language == language_cplus
11354                                    || cu->language == language_java)
11355                                ? &global_symbols : cu->list_in_scope);
11356
11357                 /* The semantics of C++ state that "struct foo {
11358                    ... }" also defines a typedef for "foo".  A Java
11359                    class declaration also defines a typedef for the
11360                    class.  */
11361                 if (cu->language == language_cplus
11362                     || cu->language == language_java
11363                     || cu->language == language_ada)
11364                   {
11365                     /* The symbol's name is already allocated along
11366                        with this objfile, so we don't need to
11367                        duplicate it for the type.  */
11368                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11369                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11370                   }
11371               }
11372           }
11373           break;
11374         case DW_TAG_typedef:
11375           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11376           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11377           list_to_add = cu->list_in_scope;
11378           break;
11379         case DW_TAG_base_type:
11380         case DW_TAG_subrange_type:
11381           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11382           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11383           list_to_add = cu->list_in_scope;
11384           break;
11385         case DW_TAG_enumerator:
11386           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11387           if (attr)
11388             {
11389               dwarf2_const_value (attr, sym, cu);
11390             }
11391           {
11392             /* NOTE: carlton/2003-11-10: See comment above in the
11393                DW_TAG_class_type, etc. block.  */
11394
11395             list_to_add = (cu->list_in_scope == &file_symbols
11396                            && (cu->language == language_cplus
11397                                || cu->language == language_java)
11398                            ? &global_symbols : cu->list_in_scope);
11399           }
11400           break;
11401         case DW_TAG_namespace:
11402           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11403           list_to_add = &global_symbols;
11404           break;
11405         default:
11406           /* Not a tag we recognize.  Hopefully we aren't processing
11407              trash data, but since we must specifically ignore things
11408              we don't recognize, there is nothing else we should do at
11409              this point.  */
11410           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11411                      dwarf_tag_name (die->tag));
11412           break;
11413         }
11414
11415       if (suppress_add)
11416         {
11417           sym->hash_next = objfile->template_symbols;
11418           objfile->template_symbols = sym;
11419           list_to_add = NULL;
11420         }
11421
11422       if (list_to_add != NULL)
11423         add_symbol_to_list (sym, list_to_add);
11424
11425       /* For the benefit of old versions of GCC, check for anonymous
11426          namespaces based on the demangled name.  */
11427       if (!processing_has_namespace_info
11428           && cu->language == language_cplus)
11429         cp_scan_for_anonymous_namespaces (sym);
11430     }
11431   return (sym);
11432 }
11433
11434 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11435
11436 static struct symbol *
11437 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11438 {
11439   return new_symbol_full (die, type, cu, NULL);
11440 }
11441
11442 /* Given an attr with a DW_FORM_dataN value in host byte order,
11443    zero-extend it as appropriate for the symbol's type.  The DWARF
11444    standard (v4) is not entirely clear about the meaning of using
11445    DW_FORM_dataN for a constant with a signed type, where the type is
11446    wider than the data.  The conclusion of a discussion on the DWARF
11447    list was that this is unspecified.  We choose to always zero-extend
11448    because that is the interpretation long in use by GCC.  */
11449
11450 static gdb_byte *
11451 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11452                          const char *name, struct obstack *obstack,
11453                          struct dwarf2_cu *cu, long *value, int bits)
11454 {
11455   struct objfile *objfile = cu->objfile;
11456   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11457                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11458   LONGEST l = DW_UNSND (attr);
11459
11460   if (bits < sizeof (*value) * 8)
11461     {
11462       l &= ((LONGEST) 1 << bits) - 1;
11463       *value = l;
11464     }
11465   else if (bits == sizeof (*value) * 8)
11466     *value = l;
11467   else
11468     {
11469       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11470       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11471       return bytes;
11472     }
11473
11474   return NULL;
11475 }
11476
11477 /* Read a constant value from an attribute.  Either set *VALUE, or if
11478    the value does not fit in *VALUE, set *BYTES - either already
11479    allocated on the objfile obstack, or newly allocated on OBSTACK,
11480    or, set *BATON, if we translated the constant to a location
11481    expression.  */
11482
11483 static void
11484 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11485                          const char *name, struct obstack *obstack,
11486                          struct dwarf2_cu *cu,
11487                          long *value, gdb_byte **bytes,
11488                          struct dwarf2_locexpr_baton **baton)
11489 {
11490   struct objfile *objfile = cu->objfile;
11491   struct comp_unit_head *cu_header = &cu->header;
11492   struct dwarf_block *blk;
11493   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11494                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11495
11496   *value = 0;
11497   *bytes = NULL;
11498   *baton = NULL;
11499
11500   switch (attr->form)
11501     {
11502     case DW_FORM_addr:
11503       {
11504         gdb_byte *data;
11505
11506         if (TYPE_LENGTH (type) != cu_header->addr_size)
11507           dwarf2_const_value_length_mismatch_complaint (name,
11508                                                         cu_header->addr_size,
11509                                                         TYPE_LENGTH (type));
11510         /* Symbols of this form are reasonably rare, so we just
11511            piggyback on the existing location code rather than writing
11512            a new implementation of symbol_computed_ops.  */
11513         *baton = obstack_alloc (&objfile->objfile_obstack,
11514                                 sizeof (struct dwarf2_locexpr_baton));
11515         (*baton)->per_cu = cu->per_cu;
11516         gdb_assert ((*baton)->per_cu);
11517
11518         (*baton)->size = 2 + cu_header->addr_size;
11519         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11520         (*baton)->data = data;
11521
11522         data[0] = DW_OP_addr;
11523         store_unsigned_integer (&data[1], cu_header->addr_size,
11524                                 byte_order, DW_ADDR (attr));
11525         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11526       }
11527       break;
11528     case DW_FORM_string:
11529     case DW_FORM_strp:
11530       /* DW_STRING is already allocated on the objfile obstack, point
11531          directly to it.  */
11532       *bytes = (gdb_byte *) DW_STRING (attr);
11533       break;
11534     case DW_FORM_block1:
11535     case DW_FORM_block2:
11536     case DW_FORM_block4:
11537     case DW_FORM_block:
11538     case DW_FORM_exprloc:
11539       blk = DW_BLOCK (attr);
11540       if (TYPE_LENGTH (type) != blk->size)
11541         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11542                                                       TYPE_LENGTH (type));
11543       *bytes = blk->data;
11544       break;
11545
11546       /* The DW_AT_const_value attributes are supposed to carry the
11547          symbol's value "represented as it would be on the target
11548          architecture."  By the time we get here, it's already been
11549          converted to host endianness, so we just need to sign- or
11550          zero-extend it as appropriate.  */
11551     case DW_FORM_data1:
11552       *bytes = dwarf2_const_value_data (attr, type, name,
11553                                         obstack, cu, value, 8);
11554       break;
11555     case DW_FORM_data2:
11556       *bytes = dwarf2_const_value_data (attr, type, name,
11557                                         obstack, cu, value, 16);
11558       break;
11559     case DW_FORM_data4:
11560       *bytes = dwarf2_const_value_data (attr, type, name,
11561                                         obstack, cu, value, 32);
11562       break;
11563     case DW_FORM_data8:
11564       *bytes = dwarf2_const_value_data (attr, type, name,
11565                                         obstack, cu, value, 64);
11566       break;
11567
11568     case DW_FORM_sdata:
11569       *value = DW_SND (attr);
11570       break;
11571
11572     case DW_FORM_udata:
11573       *value = DW_UNSND (attr);
11574       break;
11575
11576     default:
11577       complaint (&symfile_complaints,
11578                  _("unsupported const value attribute form: '%s'"),
11579                  dwarf_form_name (attr->form));
11580       *value = 0;
11581       break;
11582     }
11583 }
11584
11585
11586 /* Copy constant value from an attribute to a symbol.  */
11587
11588 static void
11589 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11590                     struct dwarf2_cu *cu)
11591 {
11592   struct objfile *objfile = cu->objfile;
11593   struct comp_unit_head *cu_header = &cu->header;
11594   long value;
11595   gdb_byte *bytes;
11596   struct dwarf2_locexpr_baton *baton;
11597
11598   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11599                            SYMBOL_PRINT_NAME (sym),
11600                            &objfile->objfile_obstack, cu,
11601                            &value, &bytes, &baton);
11602
11603   if (baton != NULL)
11604     {
11605       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11606       SYMBOL_LOCATION_BATON (sym) = baton;
11607       SYMBOL_CLASS (sym) = LOC_COMPUTED;
11608     }
11609   else if (bytes != NULL)
11610      {
11611       SYMBOL_VALUE_BYTES (sym) = bytes;
11612       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11613     }
11614   else
11615     {
11616       SYMBOL_VALUE (sym) = value;
11617       SYMBOL_CLASS (sym) = LOC_CONST;
11618     }
11619 }
11620
11621 /* Return the type of the die in question using its DW_AT_type attribute.  */
11622
11623 static struct type *
11624 die_type (struct die_info *die, struct dwarf2_cu *cu)
11625 {
11626   struct attribute *type_attr;
11627
11628   type_attr = dwarf2_attr (die, DW_AT_type, cu);
11629   if (!type_attr)
11630     {
11631       /* A missing DW_AT_type represents a void type.  */
11632       return objfile_type (cu->objfile)->builtin_void;
11633     }
11634
11635   return lookup_die_type (die, type_attr, cu);
11636 }
11637
11638 /* True iff CU's producer generates GNAT Ada auxiliary information
11639    that allows to find parallel types through that information instead
11640    of having to do expensive parallel lookups by type name.  */
11641
11642 static int
11643 need_gnat_info (struct dwarf2_cu *cu)
11644 {
11645   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11646      of GNAT produces this auxiliary information, without any indication
11647      that it is produced.  Part of enhancing the FSF version of GNAT
11648      to produce that information will be to put in place an indicator
11649      that we can use in order to determine whether the descriptive type
11650      info is available or not.  One suggestion that has been made is
11651      to use a new attribute, attached to the CU die.  For now, assume
11652      that the descriptive type info is not available.  */
11653   return 0;
11654 }
11655
11656 /* Return the auxiliary type of the die in question using its
11657    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
11658    attribute is not present.  */
11659
11660 static struct type *
11661 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11662 {
11663   struct attribute *type_attr;
11664
11665   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11666   if (!type_attr)
11667     return NULL;
11668
11669   return lookup_die_type (die, type_attr, cu);
11670 }
11671
11672 /* If DIE has a descriptive_type attribute, then set the TYPE's
11673    descriptive type accordingly.  */
11674
11675 static void
11676 set_descriptive_type (struct type *type, struct die_info *die,
11677                       struct dwarf2_cu *cu)
11678 {
11679   struct type *descriptive_type = die_descriptive_type (die, cu);
11680
11681   if (descriptive_type)
11682     {
11683       ALLOCATE_GNAT_AUX_TYPE (type);
11684       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11685     }
11686 }
11687
11688 /* Return the containing type of the die in question using its
11689    DW_AT_containing_type attribute.  */
11690
11691 static struct type *
11692 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11693 {
11694   struct attribute *type_attr;
11695
11696   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11697   if (!type_attr)
11698     error (_("Dwarf Error: Problem turning containing type into gdb type "
11699              "[in module %s]"), cu->objfile->name);
11700
11701   return lookup_die_type (die, type_attr, cu);
11702 }
11703
11704 /* Look up the type of DIE in CU using its type attribute ATTR.
11705    If there is no type substitute an error marker.  */
11706
11707 static struct type *
11708 lookup_die_type (struct die_info *die, struct attribute *attr,
11709                  struct dwarf2_cu *cu)
11710 {
11711   struct type *this_type;
11712
11713   /* First see if we have it cached.  */
11714
11715   if (is_ref_attr (attr))
11716     {
11717       unsigned int offset = dwarf2_get_ref_die_offset (attr);
11718
11719       this_type = get_die_type_at_offset (offset, cu->per_cu);
11720     }
11721   else if (attr->form == DW_FORM_ref_sig8)
11722     {
11723       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11724       struct dwarf2_cu *sig_cu;
11725       unsigned int offset;
11726
11727       /* sig_type will be NULL if the signatured type is missing from
11728          the debug info.  */
11729       if (sig_type == NULL)
11730         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11731                  "at 0x%x [in module %s]"),
11732                die->offset, cu->objfile->name);
11733
11734       gdb_assert (sig_type->per_cu.from_debug_types);
11735       offset = sig_type->per_cu.offset + sig_type->type_offset;
11736       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11737     }
11738   else
11739     {
11740       dump_die_for_error (die);
11741       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11742              dwarf_attr_name (attr->name), cu->objfile->name);
11743     }
11744
11745   /* If not cached we need to read it in.  */
11746
11747   if (this_type == NULL)
11748     {
11749       struct die_info *type_die;
11750       struct dwarf2_cu *type_cu = cu;
11751
11752       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11753       /* If the type is cached, we should have found it above.  */
11754       gdb_assert (get_die_type (type_die, type_cu) == NULL);
11755       this_type = read_type_die_1 (type_die, type_cu);
11756     }
11757
11758   /* If we still don't have a type use an error marker.  */
11759
11760   if (this_type == NULL)
11761     {
11762       char *message, *saved;
11763
11764       /* read_type_die already issued a complaint.  */
11765       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11766                             cu->objfile->name,
11767                             cu->header.offset,
11768                             die->offset);
11769       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11770                              message, strlen (message));
11771       xfree (message);
11772
11773       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11774     }
11775
11776   return this_type;
11777 }
11778
11779 /* Return the type in DIE, CU.
11780    Returns NULL for invalid types.
11781
11782    This first does a lookup in the appropriate type_hash table,
11783    and only reads the die in if necessary.
11784
11785    NOTE: This can be called when reading in partial or full symbols.  */
11786
11787 static struct type *
11788 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11789 {
11790   struct type *this_type;
11791
11792   this_type = get_die_type (die, cu);
11793   if (this_type)
11794     return this_type;
11795
11796   return read_type_die_1 (die, cu);
11797 }
11798
11799 /* Read the type in DIE, CU.
11800    Returns NULL for invalid types.  */
11801
11802 static struct type *
11803 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11804 {
11805   struct type *this_type = NULL;
11806
11807   switch (die->tag)
11808     {
11809     case DW_TAG_class_type:
11810     case DW_TAG_interface_type:
11811     case DW_TAG_structure_type:
11812     case DW_TAG_union_type:
11813       this_type = read_structure_type (die, cu);
11814       break;
11815     case DW_TAG_enumeration_type:
11816       this_type = read_enumeration_type (die, cu);
11817       break;
11818     case DW_TAG_subprogram:
11819     case DW_TAG_subroutine_type:
11820     case DW_TAG_inlined_subroutine:
11821       this_type = read_subroutine_type (die, cu);
11822       break;
11823     case DW_TAG_array_type:
11824       this_type = read_array_type (die, cu);
11825       break;
11826     case DW_TAG_set_type:
11827       this_type = read_set_type (die, cu);
11828       break;
11829     case DW_TAG_pointer_type:
11830       this_type = read_tag_pointer_type (die, cu);
11831       break;
11832     case DW_TAG_ptr_to_member_type:
11833       this_type = read_tag_ptr_to_member_type (die, cu);
11834       break;
11835     case DW_TAG_reference_type:
11836       this_type = read_tag_reference_type (die, cu);
11837       break;
11838     case DW_TAG_const_type:
11839       this_type = read_tag_const_type (die, cu);
11840       break;
11841     case DW_TAG_volatile_type:
11842       this_type = read_tag_volatile_type (die, cu);
11843       break;
11844     case DW_TAG_string_type:
11845       this_type = read_tag_string_type (die, cu);
11846       break;
11847     case DW_TAG_typedef:
11848       this_type = read_typedef (die, cu);
11849       break;
11850     case DW_TAG_subrange_type:
11851       this_type = read_subrange_type (die, cu);
11852       break;
11853     case DW_TAG_base_type:
11854       this_type = read_base_type (die, cu);
11855       break;
11856     case DW_TAG_unspecified_type:
11857       this_type = read_unspecified_type (die, cu);
11858       break;
11859     case DW_TAG_namespace:
11860       this_type = read_namespace_type (die, cu);
11861       break;
11862     case DW_TAG_module:
11863       this_type = read_module_type (die, cu);
11864       break;
11865     default:
11866       complaint (&symfile_complaints,
11867                  _("unexpected tag in read_type_die: '%s'"),
11868                  dwarf_tag_name (die->tag));
11869       break;
11870     }
11871
11872   return this_type;
11873 }
11874
11875 /* See if we can figure out if the class lives in a namespace.  We do
11876    this by looking for a member function; its demangled name will
11877    contain namespace info, if there is any.
11878    Return the computed name or NULL.
11879    Space for the result is allocated on the objfile's obstack.
11880    This is the full-die version of guess_partial_die_structure_name.
11881    In this case we know DIE has no useful parent.  */
11882
11883 static char *
11884 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11885 {
11886   struct die_info *spec_die;
11887   struct dwarf2_cu *spec_cu;
11888   struct die_info *child;
11889
11890   spec_cu = cu;
11891   spec_die = die_specification (die, &spec_cu);
11892   if (spec_die != NULL)
11893     {
11894       die = spec_die;
11895       cu = spec_cu;
11896     }
11897
11898   for (child = die->child;
11899        child != NULL;
11900        child = child->sibling)
11901     {
11902       if (child->tag == DW_TAG_subprogram)
11903         {
11904           struct attribute *attr;
11905
11906           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11907           if (attr == NULL)
11908             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11909           if (attr != NULL)
11910             {
11911               char *actual_name
11912                 = language_class_name_from_physname (cu->language_defn,
11913                                                      DW_STRING (attr));
11914               char *name = NULL;
11915
11916               if (actual_name != NULL)
11917                 {
11918                   char *die_name = dwarf2_name (die, cu);
11919
11920                   if (die_name != NULL
11921                       && strcmp (die_name, actual_name) != 0)
11922                     {
11923                       /* Strip off the class name from the full name.
11924                          We want the prefix.  */
11925                       int die_name_len = strlen (die_name);
11926                       int actual_name_len = strlen (actual_name);
11927
11928                       /* Test for '::' as a sanity check.  */
11929                       if (actual_name_len > die_name_len + 2
11930                           && actual_name[actual_name_len
11931                                          - die_name_len - 1] == ':')
11932                         name =
11933                           obsavestring (actual_name,
11934                                         actual_name_len - die_name_len - 2,
11935                                         &cu->objfile->objfile_obstack);
11936                     }
11937                 }
11938               xfree (actual_name);
11939               return name;
11940             }
11941         }
11942     }
11943
11944   return NULL;
11945 }
11946
11947 /* Return the name of the namespace/class that DIE is defined within,
11948    or "" if we can't tell.  The caller should not xfree the result.
11949
11950    For example, if we're within the method foo() in the following
11951    code:
11952
11953    namespace N {
11954      class C {
11955        void foo () {
11956        }
11957      };
11958    }
11959
11960    then determine_prefix on foo's die will return "N::C".  */
11961
11962 static char *
11963 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11964 {
11965   struct die_info *parent, *spec_die;
11966   struct dwarf2_cu *spec_cu;
11967   struct type *parent_type;
11968
11969   if (cu->language != language_cplus && cu->language != language_java
11970       && cu->language != language_fortran)
11971     return "";
11972
11973   /* We have to be careful in the presence of DW_AT_specification.
11974      For example, with GCC 3.4, given the code
11975
11976      namespace N {
11977        void foo() {
11978          // Definition of N::foo.
11979        }
11980      }
11981
11982      then we'll have a tree of DIEs like this:
11983
11984      1: DW_TAG_compile_unit
11985        2: DW_TAG_namespace        // N
11986          3: DW_TAG_subprogram     // declaration of N::foo
11987        4: DW_TAG_subprogram       // definition of N::foo
11988             DW_AT_specification   // refers to die #3
11989
11990      Thus, when processing die #4, we have to pretend that we're in
11991      the context of its DW_AT_specification, namely the contex of die
11992      #3.  */
11993   spec_cu = cu;
11994   spec_die = die_specification (die, &spec_cu);
11995   if (spec_die == NULL)
11996     parent = die->parent;
11997   else
11998     {
11999       parent = spec_die->parent;
12000       cu = spec_cu;
12001     }
12002
12003   if (parent == NULL)
12004     return "";
12005   else if (parent->building_fullname)
12006     {
12007       const char *name;
12008       const char *parent_name;
12009
12010       /* It has been seen on RealView 2.2 built binaries,
12011          DW_TAG_template_type_param types actually _defined_ as
12012          children of the parent class:
12013
12014          enum E {};
12015          template class <class Enum> Class{};
12016          Class<enum E> class_e;
12017
12018          1: DW_TAG_class_type (Class)
12019            2: DW_TAG_enumeration_type (E)
12020              3: DW_TAG_enumerator (enum1:0)
12021              3: DW_TAG_enumerator (enum2:1)
12022              ...
12023            2: DW_TAG_template_type_param
12024               DW_AT_type  DW_FORM_ref_udata (E)
12025
12026          Besides being broken debug info, it can put GDB into an
12027          infinite loop.  Consider:
12028
12029          When we're building the full name for Class<E>, we'll start
12030          at Class, and go look over its template type parameters,
12031          finding E.  We'll then try to build the full name of E, and
12032          reach here.  We're now trying to build the full name of E,
12033          and look over the parent DIE for containing scope.  In the
12034          broken case, if we followed the parent DIE of E, we'd again
12035          find Class, and once again go look at its template type
12036          arguments, etc., etc.  Simply don't consider such parent die
12037          as source-level parent of this die (it can't be, the language
12038          doesn't allow it), and break the loop here.  */
12039       name = dwarf2_name (die, cu);
12040       parent_name = dwarf2_name (parent, cu);
12041       complaint (&symfile_complaints,
12042                  _("template param type '%s' defined within parent '%s'"),
12043                  name ? name : "<unknown>",
12044                  parent_name ? parent_name : "<unknown>");
12045       return "";
12046     }
12047   else
12048     switch (parent->tag)
12049       {
12050       case DW_TAG_namespace:
12051         parent_type = read_type_die (parent, cu);
12052         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12053            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12054            Work around this problem here.  */
12055         if (cu->language == language_cplus
12056             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12057           return "";
12058         /* We give a name to even anonymous namespaces.  */
12059         return TYPE_TAG_NAME (parent_type);
12060       case DW_TAG_class_type:
12061       case DW_TAG_interface_type:
12062       case DW_TAG_structure_type:
12063       case DW_TAG_union_type:
12064       case DW_TAG_module:
12065         parent_type = read_type_die (parent, cu);
12066         if (TYPE_TAG_NAME (parent_type) != NULL)
12067           return TYPE_TAG_NAME (parent_type);
12068         else
12069           /* An anonymous structure is only allowed non-static data
12070              members; no typedefs, no member functions, et cetera.
12071              So it does not need a prefix.  */
12072           return "";
12073       case DW_TAG_compile_unit:
12074         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12075         if (cu->language == language_cplus
12076             && dwarf2_per_objfile->types.asection != NULL
12077             && die->child != NULL
12078             && (die->tag == DW_TAG_class_type
12079                 || die->tag == DW_TAG_structure_type
12080                 || die->tag == DW_TAG_union_type))
12081           {
12082             char *name = guess_full_die_structure_name (die, cu);
12083             if (name != NULL)
12084               return name;
12085           }
12086         return "";
12087       default:
12088         return determine_prefix (parent, cu);
12089       }
12090 }
12091
12092 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12093    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12094    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12095    an obconcat, otherwise allocate storage for the result.  The CU argument is
12096    used to determine the language and hence, the appropriate separator.  */
12097
12098 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12099
12100 static char *
12101 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12102                  int physname, struct dwarf2_cu *cu)
12103 {
12104   const char *lead = "";
12105   const char *sep;
12106
12107   if (suffix == NULL || suffix[0] == '\0'
12108       || prefix == NULL || prefix[0] == '\0')
12109     sep = "";
12110   else if (cu->language == language_java)
12111     sep = ".";
12112   else if (cu->language == language_fortran && physname)
12113     {
12114       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12115          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12116
12117       lead = "__";
12118       sep = "_MOD_";
12119     }
12120   else
12121     sep = "::";
12122
12123   if (prefix == NULL)
12124     prefix = "";
12125   if (suffix == NULL)
12126     suffix = "";
12127
12128   if (obs == NULL)
12129     {
12130       char *retval
12131         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12132
12133       strcpy (retval, lead);
12134       strcat (retval, prefix);
12135       strcat (retval, sep);
12136       strcat (retval, suffix);
12137       return retval;
12138     }
12139   else
12140     {
12141       /* We have an obstack.  */
12142       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12143     }
12144 }
12145
12146 /* Return sibling of die, NULL if no sibling.  */
12147
12148 static struct die_info *
12149 sibling_die (struct die_info *die)
12150 {
12151   return die->sibling;
12152 }
12153
12154 /* Get name of a die, return NULL if not found.  */
12155
12156 static char *
12157 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12158                           struct obstack *obstack)
12159 {
12160   if (name && cu->language == language_cplus)
12161     {
12162       char *canon_name = cp_canonicalize_string (name);
12163
12164       if (canon_name != NULL)
12165         {
12166           if (strcmp (canon_name, name) != 0)
12167             name = obsavestring (canon_name, strlen (canon_name),
12168                                  obstack);
12169           xfree (canon_name);
12170         }
12171     }
12172
12173   return name;
12174 }
12175
12176 /* Get name of a die, return NULL if not found.  */
12177
12178 static char *
12179 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12180 {
12181   struct attribute *attr;
12182
12183   attr = dwarf2_attr (die, DW_AT_name, cu);
12184   if ((!attr || !DW_STRING (attr))
12185       && die->tag != DW_TAG_class_type
12186       && die->tag != DW_TAG_interface_type
12187       && die->tag != DW_TAG_structure_type
12188       && die->tag != DW_TAG_union_type)
12189     return NULL;
12190
12191   switch (die->tag)
12192     {
12193     case DW_TAG_compile_unit:
12194       /* Compilation units have a DW_AT_name that is a filename, not
12195          a source language identifier.  */
12196     case DW_TAG_enumeration_type:
12197     case DW_TAG_enumerator:
12198       /* These tags always have simple identifiers already; no need
12199          to canonicalize them.  */
12200       return DW_STRING (attr);
12201
12202     case DW_TAG_subprogram:
12203       /* Java constructors will all be named "<init>", so return
12204          the class name when we see this special case.  */
12205       if (cu->language == language_java
12206           && DW_STRING (attr) != NULL
12207           && strcmp (DW_STRING (attr), "<init>") == 0)
12208         {
12209           struct dwarf2_cu *spec_cu = cu;
12210           struct die_info *spec_die;
12211
12212           /* GCJ will output '<init>' for Java constructor names.
12213              For this special case, return the name of the parent class.  */
12214
12215           /* GCJ may output suprogram DIEs with AT_specification set.
12216              If so, use the name of the specified DIE.  */
12217           spec_die = die_specification (die, &spec_cu);
12218           if (spec_die != NULL)
12219             return dwarf2_name (spec_die, spec_cu);
12220
12221           do
12222             {
12223               die = die->parent;
12224               if (die->tag == DW_TAG_class_type)
12225                 return dwarf2_name (die, cu);
12226             }
12227           while (die->tag != DW_TAG_compile_unit);
12228         }
12229       break;
12230
12231     case DW_TAG_class_type:
12232     case DW_TAG_interface_type:
12233     case DW_TAG_structure_type:
12234     case DW_TAG_union_type:
12235       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12236          structures or unions.  These were of the form "._%d" in GCC 4.1,
12237          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12238          and GCC 4.4.  We work around this problem by ignoring these.  */
12239       if (attr && DW_STRING (attr)
12240           && (strncmp (DW_STRING (attr), "._", 2) == 0
12241               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12242         return NULL;
12243
12244       /* GCC might emit a nameless typedef that has a linkage name.  See
12245          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12246       if (!attr || DW_STRING (attr) == NULL)
12247         {
12248           char *demangled = NULL;
12249
12250           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12251           if (attr == NULL)
12252             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12253
12254           if (attr == NULL || DW_STRING (attr) == NULL)
12255             return NULL;
12256
12257           /* Avoid demangling DW_STRING (attr) the second time on a second
12258              call for the same DIE.  */
12259           if (!DW_STRING_IS_CANONICAL (attr))
12260             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12261
12262           if (demangled)
12263             {
12264               /* FIXME: we already did this for the partial symbol... */
12265               DW_STRING (attr)
12266                 = obsavestring (demangled, strlen (demangled),
12267                                 &cu->objfile->objfile_obstack);
12268               DW_STRING_IS_CANONICAL (attr) = 1;
12269               xfree (demangled);
12270             }
12271         }
12272       break;
12273
12274     default:
12275       break;
12276     }
12277
12278   if (!DW_STRING_IS_CANONICAL (attr))
12279     {
12280       DW_STRING (attr)
12281         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12282                                     &cu->objfile->objfile_obstack);
12283       DW_STRING_IS_CANONICAL (attr) = 1;
12284     }
12285   return DW_STRING (attr);
12286 }
12287
12288 /* Return the die that this die in an extension of, or NULL if there
12289    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12290    containing the return value on output.  */
12291
12292 static struct die_info *
12293 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12294 {
12295   struct attribute *attr;
12296
12297   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12298   if (attr == NULL)
12299     return NULL;
12300
12301   return follow_die_ref (die, attr, ext_cu);
12302 }
12303
12304 /* Convert a DIE tag into its string name.  */
12305
12306 static char *
12307 dwarf_tag_name (unsigned tag)
12308 {
12309   switch (tag)
12310     {
12311     case DW_TAG_padding:
12312       return "DW_TAG_padding";
12313     case DW_TAG_array_type:
12314       return "DW_TAG_array_type";
12315     case DW_TAG_class_type:
12316       return "DW_TAG_class_type";
12317     case DW_TAG_entry_point:
12318       return "DW_TAG_entry_point";
12319     case DW_TAG_enumeration_type:
12320       return "DW_TAG_enumeration_type";
12321     case DW_TAG_formal_parameter:
12322       return "DW_TAG_formal_parameter";
12323     case DW_TAG_imported_declaration:
12324       return "DW_TAG_imported_declaration";
12325     case DW_TAG_label:
12326       return "DW_TAG_label";
12327     case DW_TAG_lexical_block:
12328       return "DW_TAG_lexical_block";
12329     case DW_TAG_member:
12330       return "DW_TAG_member";
12331     case DW_TAG_pointer_type:
12332       return "DW_TAG_pointer_type";
12333     case DW_TAG_reference_type:
12334       return "DW_TAG_reference_type";
12335     case DW_TAG_compile_unit:
12336       return "DW_TAG_compile_unit";
12337     case DW_TAG_string_type:
12338       return "DW_TAG_string_type";
12339     case DW_TAG_structure_type:
12340       return "DW_TAG_structure_type";
12341     case DW_TAG_subroutine_type:
12342       return "DW_TAG_subroutine_type";
12343     case DW_TAG_typedef:
12344       return "DW_TAG_typedef";
12345     case DW_TAG_union_type:
12346       return "DW_TAG_union_type";
12347     case DW_TAG_unspecified_parameters:
12348       return "DW_TAG_unspecified_parameters";
12349     case DW_TAG_variant:
12350       return "DW_TAG_variant";
12351     case DW_TAG_common_block:
12352       return "DW_TAG_common_block";
12353     case DW_TAG_common_inclusion:
12354       return "DW_TAG_common_inclusion";
12355     case DW_TAG_inheritance:
12356       return "DW_TAG_inheritance";
12357     case DW_TAG_inlined_subroutine:
12358       return "DW_TAG_inlined_subroutine";
12359     case DW_TAG_module:
12360       return "DW_TAG_module";
12361     case DW_TAG_ptr_to_member_type:
12362       return "DW_TAG_ptr_to_member_type";
12363     case DW_TAG_set_type:
12364       return "DW_TAG_set_type";
12365     case DW_TAG_subrange_type:
12366       return "DW_TAG_subrange_type";
12367     case DW_TAG_with_stmt:
12368       return "DW_TAG_with_stmt";
12369     case DW_TAG_access_declaration:
12370       return "DW_TAG_access_declaration";
12371     case DW_TAG_base_type:
12372       return "DW_TAG_base_type";
12373     case DW_TAG_catch_block:
12374       return "DW_TAG_catch_block";
12375     case DW_TAG_const_type:
12376       return "DW_TAG_const_type";
12377     case DW_TAG_constant:
12378       return "DW_TAG_constant";
12379     case DW_TAG_enumerator:
12380       return "DW_TAG_enumerator";
12381     case DW_TAG_file_type:
12382       return "DW_TAG_file_type";
12383     case DW_TAG_friend:
12384       return "DW_TAG_friend";
12385     case DW_TAG_namelist:
12386       return "DW_TAG_namelist";
12387     case DW_TAG_namelist_item:
12388       return "DW_TAG_namelist_item";
12389     case DW_TAG_packed_type:
12390       return "DW_TAG_packed_type";
12391     case DW_TAG_subprogram:
12392       return "DW_TAG_subprogram";
12393     case DW_TAG_template_type_param:
12394       return "DW_TAG_template_type_param";
12395     case DW_TAG_template_value_param:
12396       return "DW_TAG_template_value_param";
12397     case DW_TAG_thrown_type:
12398       return "DW_TAG_thrown_type";
12399     case DW_TAG_try_block:
12400       return "DW_TAG_try_block";
12401     case DW_TAG_variant_part:
12402       return "DW_TAG_variant_part";
12403     case DW_TAG_variable:
12404       return "DW_TAG_variable";
12405     case DW_TAG_volatile_type:
12406       return "DW_TAG_volatile_type";
12407     case DW_TAG_dwarf_procedure:
12408       return "DW_TAG_dwarf_procedure";
12409     case DW_TAG_restrict_type:
12410       return "DW_TAG_restrict_type";
12411     case DW_TAG_interface_type:
12412       return "DW_TAG_interface_type";
12413     case DW_TAG_namespace:
12414       return "DW_TAG_namespace";
12415     case DW_TAG_imported_module:
12416       return "DW_TAG_imported_module";
12417     case DW_TAG_unspecified_type:
12418       return "DW_TAG_unspecified_type";
12419     case DW_TAG_partial_unit:
12420       return "DW_TAG_partial_unit";
12421     case DW_TAG_imported_unit:
12422       return "DW_TAG_imported_unit";
12423     case DW_TAG_condition:
12424       return "DW_TAG_condition";
12425     case DW_TAG_shared_type:
12426       return "DW_TAG_shared_type";
12427     case DW_TAG_type_unit:
12428       return "DW_TAG_type_unit";
12429     case DW_TAG_MIPS_loop:
12430       return "DW_TAG_MIPS_loop";
12431     case DW_TAG_HP_array_descriptor:
12432       return "DW_TAG_HP_array_descriptor";
12433     case DW_TAG_format_label:
12434       return "DW_TAG_format_label";
12435     case DW_TAG_function_template:
12436       return "DW_TAG_function_template";
12437     case DW_TAG_class_template:
12438       return "DW_TAG_class_template";
12439     case DW_TAG_GNU_BINCL:
12440       return "DW_TAG_GNU_BINCL";
12441     case DW_TAG_GNU_EINCL:
12442       return "DW_TAG_GNU_EINCL";
12443     case DW_TAG_upc_shared_type:
12444       return "DW_TAG_upc_shared_type";
12445     case DW_TAG_upc_strict_type:
12446       return "DW_TAG_upc_strict_type";
12447     case DW_TAG_upc_relaxed_type:
12448       return "DW_TAG_upc_relaxed_type";
12449     case DW_TAG_PGI_kanji_type:
12450       return "DW_TAG_PGI_kanji_type";
12451     case DW_TAG_PGI_interface_block:
12452       return "DW_TAG_PGI_interface_block";
12453     default:
12454       return "DW_TAG_<unknown>";
12455     }
12456 }
12457
12458 /* Convert a DWARF attribute code into its string name.  */
12459
12460 static char *
12461 dwarf_attr_name (unsigned attr)
12462 {
12463   switch (attr)
12464     {
12465     case DW_AT_sibling:
12466       return "DW_AT_sibling";
12467     case DW_AT_location:
12468       return "DW_AT_location";
12469     case DW_AT_name:
12470       return "DW_AT_name";
12471     case DW_AT_ordering:
12472       return "DW_AT_ordering";
12473     case DW_AT_subscr_data:
12474       return "DW_AT_subscr_data";
12475     case DW_AT_byte_size:
12476       return "DW_AT_byte_size";
12477     case DW_AT_bit_offset:
12478       return "DW_AT_bit_offset";
12479     case DW_AT_bit_size:
12480       return "DW_AT_bit_size";
12481     case DW_AT_element_list:
12482       return "DW_AT_element_list";
12483     case DW_AT_stmt_list:
12484       return "DW_AT_stmt_list";
12485     case DW_AT_low_pc:
12486       return "DW_AT_low_pc";
12487     case DW_AT_high_pc:
12488       return "DW_AT_high_pc";
12489     case DW_AT_language:
12490       return "DW_AT_language";
12491     case DW_AT_member:
12492       return "DW_AT_member";
12493     case DW_AT_discr:
12494       return "DW_AT_discr";
12495     case DW_AT_discr_value:
12496       return "DW_AT_discr_value";
12497     case DW_AT_visibility:
12498       return "DW_AT_visibility";
12499     case DW_AT_import:
12500       return "DW_AT_import";
12501     case DW_AT_string_length:
12502       return "DW_AT_string_length";
12503     case DW_AT_common_reference:
12504       return "DW_AT_common_reference";
12505     case DW_AT_comp_dir:
12506       return "DW_AT_comp_dir";
12507     case DW_AT_const_value:
12508       return "DW_AT_const_value";
12509     case DW_AT_containing_type:
12510       return "DW_AT_containing_type";
12511     case DW_AT_default_value:
12512       return "DW_AT_default_value";
12513     case DW_AT_inline:
12514       return "DW_AT_inline";
12515     case DW_AT_is_optional:
12516       return "DW_AT_is_optional";
12517     case DW_AT_lower_bound:
12518       return "DW_AT_lower_bound";
12519     case DW_AT_producer:
12520       return "DW_AT_producer";
12521     case DW_AT_prototyped:
12522       return "DW_AT_prototyped";
12523     case DW_AT_return_addr:
12524       return "DW_AT_return_addr";
12525     case DW_AT_start_scope:
12526       return "DW_AT_start_scope";
12527     case DW_AT_bit_stride:
12528       return "DW_AT_bit_stride";
12529     case DW_AT_upper_bound:
12530       return "DW_AT_upper_bound";
12531     case DW_AT_abstract_origin:
12532       return "DW_AT_abstract_origin";
12533     case DW_AT_accessibility:
12534       return "DW_AT_accessibility";
12535     case DW_AT_address_class:
12536       return "DW_AT_address_class";
12537     case DW_AT_artificial:
12538       return "DW_AT_artificial";
12539     case DW_AT_base_types:
12540       return "DW_AT_base_types";
12541     case DW_AT_calling_convention:
12542       return "DW_AT_calling_convention";
12543     case DW_AT_count:
12544       return "DW_AT_count";
12545     case DW_AT_data_member_location:
12546       return "DW_AT_data_member_location";
12547     case DW_AT_decl_column:
12548       return "DW_AT_decl_column";
12549     case DW_AT_decl_file:
12550       return "DW_AT_decl_file";
12551     case DW_AT_decl_line:
12552       return "DW_AT_decl_line";
12553     case DW_AT_declaration:
12554       return "DW_AT_declaration";
12555     case DW_AT_discr_list:
12556       return "DW_AT_discr_list";
12557     case DW_AT_encoding:
12558       return "DW_AT_encoding";
12559     case DW_AT_external:
12560       return "DW_AT_external";
12561     case DW_AT_frame_base:
12562       return "DW_AT_frame_base";
12563     case DW_AT_friend:
12564       return "DW_AT_friend";
12565     case DW_AT_identifier_case:
12566       return "DW_AT_identifier_case";
12567     case DW_AT_macro_info:
12568       return "DW_AT_macro_info";
12569     case DW_AT_namelist_items:
12570       return "DW_AT_namelist_items";
12571     case DW_AT_priority:
12572       return "DW_AT_priority";
12573     case DW_AT_segment:
12574       return "DW_AT_segment";
12575     case DW_AT_specification:
12576       return "DW_AT_specification";
12577     case DW_AT_static_link:
12578       return "DW_AT_static_link";
12579     case DW_AT_type:
12580       return "DW_AT_type";
12581     case DW_AT_use_location:
12582       return "DW_AT_use_location";
12583     case DW_AT_variable_parameter:
12584       return "DW_AT_variable_parameter";
12585     case DW_AT_virtuality:
12586       return "DW_AT_virtuality";
12587     case DW_AT_vtable_elem_location:
12588       return "DW_AT_vtable_elem_location";
12589     /* DWARF 3 values.  */
12590     case DW_AT_allocated:
12591       return "DW_AT_allocated";
12592     case DW_AT_associated:
12593       return "DW_AT_associated";
12594     case DW_AT_data_location:
12595       return "DW_AT_data_location";
12596     case DW_AT_byte_stride:
12597       return "DW_AT_byte_stride";
12598     case DW_AT_entry_pc:
12599       return "DW_AT_entry_pc";
12600     case DW_AT_use_UTF8:
12601       return "DW_AT_use_UTF8";
12602     case DW_AT_extension:
12603       return "DW_AT_extension";
12604     case DW_AT_ranges:
12605       return "DW_AT_ranges";
12606     case DW_AT_trampoline:
12607       return "DW_AT_trampoline";
12608     case DW_AT_call_column:
12609       return "DW_AT_call_column";
12610     case DW_AT_call_file:
12611       return "DW_AT_call_file";
12612     case DW_AT_call_line:
12613       return "DW_AT_call_line";
12614     case DW_AT_description:
12615       return "DW_AT_description";
12616     case DW_AT_binary_scale:
12617       return "DW_AT_binary_scale";
12618     case DW_AT_decimal_scale:
12619       return "DW_AT_decimal_scale";
12620     case DW_AT_small:
12621       return "DW_AT_small";
12622     case DW_AT_decimal_sign:
12623       return "DW_AT_decimal_sign";
12624     case DW_AT_digit_count:
12625       return "DW_AT_digit_count";
12626     case DW_AT_picture_string:
12627       return "DW_AT_picture_string";
12628     case DW_AT_mutable:
12629       return "DW_AT_mutable";
12630     case DW_AT_threads_scaled:
12631       return "DW_AT_threads_scaled";
12632     case DW_AT_explicit:
12633       return "DW_AT_explicit";
12634     case DW_AT_object_pointer:
12635       return "DW_AT_object_pointer";
12636     case DW_AT_endianity:
12637       return "DW_AT_endianity";
12638     case DW_AT_elemental:
12639       return "DW_AT_elemental";
12640     case DW_AT_pure:
12641       return "DW_AT_pure";
12642     case DW_AT_recursive:
12643       return "DW_AT_recursive";
12644     /* DWARF 4 values.  */
12645     case DW_AT_signature:
12646       return "DW_AT_signature";
12647     case DW_AT_linkage_name:
12648       return "DW_AT_linkage_name";
12649     /* SGI/MIPS extensions.  */
12650 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12651     case DW_AT_MIPS_fde:
12652       return "DW_AT_MIPS_fde";
12653 #endif
12654     case DW_AT_MIPS_loop_begin:
12655       return "DW_AT_MIPS_loop_begin";
12656     case DW_AT_MIPS_tail_loop_begin:
12657       return "DW_AT_MIPS_tail_loop_begin";
12658     case DW_AT_MIPS_epilog_begin:
12659       return "DW_AT_MIPS_epilog_begin";
12660     case DW_AT_MIPS_loop_unroll_factor:
12661       return "DW_AT_MIPS_loop_unroll_factor";
12662     case DW_AT_MIPS_software_pipeline_depth:
12663       return "DW_AT_MIPS_software_pipeline_depth";
12664     case DW_AT_MIPS_linkage_name:
12665       return "DW_AT_MIPS_linkage_name";
12666     case DW_AT_MIPS_stride:
12667       return "DW_AT_MIPS_stride";
12668     case DW_AT_MIPS_abstract_name:
12669       return "DW_AT_MIPS_abstract_name";
12670     case DW_AT_MIPS_clone_origin:
12671       return "DW_AT_MIPS_clone_origin";
12672     case DW_AT_MIPS_has_inlines:
12673       return "DW_AT_MIPS_has_inlines";
12674     /* HP extensions.  */
12675 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12676     case DW_AT_HP_block_index:
12677       return "DW_AT_HP_block_index";
12678 #endif
12679     case DW_AT_HP_unmodifiable:
12680       return "DW_AT_HP_unmodifiable";
12681     case DW_AT_HP_actuals_stmt_list:
12682       return "DW_AT_HP_actuals_stmt_list";
12683     case DW_AT_HP_proc_per_section:
12684       return "DW_AT_HP_proc_per_section";
12685     case DW_AT_HP_raw_data_ptr:
12686       return "DW_AT_HP_raw_data_ptr";
12687     case DW_AT_HP_pass_by_reference:
12688       return "DW_AT_HP_pass_by_reference";
12689     case DW_AT_HP_opt_level:
12690       return "DW_AT_HP_opt_level";
12691     case DW_AT_HP_prof_version_id:
12692       return "DW_AT_HP_prof_version_id";
12693     case DW_AT_HP_opt_flags:
12694       return "DW_AT_HP_opt_flags";
12695     case DW_AT_HP_cold_region_low_pc:
12696       return "DW_AT_HP_cold_region_low_pc";
12697     case DW_AT_HP_cold_region_high_pc:
12698       return "DW_AT_HP_cold_region_high_pc";
12699     case DW_AT_HP_all_variables_modifiable:
12700       return "DW_AT_HP_all_variables_modifiable";
12701     case DW_AT_HP_linkage_name:
12702       return "DW_AT_HP_linkage_name";
12703     case DW_AT_HP_prof_flags:
12704       return "DW_AT_HP_prof_flags";
12705     /* GNU extensions.  */
12706     case DW_AT_sf_names:
12707       return "DW_AT_sf_names";
12708     case DW_AT_src_info:
12709       return "DW_AT_src_info";
12710     case DW_AT_mac_info:
12711       return "DW_AT_mac_info";
12712     case DW_AT_src_coords:
12713       return "DW_AT_src_coords";
12714     case DW_AT_body_begin:
12715       return "DW_AT_body_begin";
12716     case DW_AT_body_end:
12717       return "DW_AT_body_end";
12718     case DW_AT_GNU_vector:
12719       return "DW_AT_GNU_vector";
12720     case DW_AT_GNU_odr_signature:
12721       return "DW_AT_GNU_odr_signature";
12722     /* VMS extensions.  */
12723     case DW_AT_VMS_rtnbeg_pd_address:
12724       return "DW_AT_VMS_rtnbeg_pd_address";
12725     /* UPC extension.  */
12726     case DW_AT_upc_threads_scaled:
12727       return "DW_AT_upc_threads_scaled";
12728     /* PGI (STMicroelectronics) extensions.  */
12729     case DW_AT_PGI_lbase:
12730       return "DW_AT_PGI_lbase";
12731     case DW_AT_PGI_soffset:
12732       return "DW_AT_PGI_soffset";
12733     case DW_AT_PGI_lstride:
12734       return "DW_AT_PGI_lstride";
12735     default:
12736       return "DW_AT_<unknown>";
12737     }
12738 }
12739
12740 /* Convert a DWARF value form code into its string name.  */
12741
12742 static char *
12743 dwarf_form_name (unsigned form)
12744 {
12745   switch (form)
12746     {
12747     case DW_FORM_addr:
12748       return "DW_FORM_addr";
12749     case DW_FORM_block2:
12750       return "DW_FORM_block2";
12751     case DW_FORM_block4:
12752       return "DW_FORM_block4";
12753     case DW_FORM_data2:
12754       return "DW_FORM_data2";
12755     case DW_FORM_data4:
12756       return "DW_FORM_data4";
12757     case DW_FORM_data8:
12758       return "DW_FORM_data8";
12759     case DW_FORM_string:
12760       return "DW_FORM_string";
12761     case DW_FORM_block:
12762       return "DW_FORM_block";
12763     case DW_FORM_block1:
12764       return "DW_FORM_block1";
12765     case DW_FORM_data1:
12766       return "DW_FORM_data1";
12767     case DW_FORM_flag:
12768       return "DW_FORM_flag";
12769     case DW_FORM_sdata:
12770       return "DW_FORM_sdata";
12771     case DW_FORM_strp:
12772       return "DW_FORM_strp";
12773     case DW_FORM_udata:
12774       return "DW_FORM_udata";
12775     case DW_FORM_ref_addr:
12776       return "DW_FORM_ref_addr";
12777     case DW_FORM_ref1:
12778       return "DW_FORM_ref1";
12779     case DW_FORM_ref2:
12780       return "DW_FORM_ref2";
12781     case DW_FORM_ref4:
12782       return "DW_FORM_ref4";
12783     case DW_FORM_ref8:
12784       return "DW_FORM_ref8";
12785     case DW_FORM_ref_udata:
12786       return "DW_FORM_ref_udata";
12787     case DW_FORM_indirect:
12788       return "DW_FORM_indirect";
12789     case DW_FORM_sec_offset:
12790       return "DW_FORM_sec_offset";
12791     case DW_FORM_exprloc:
12792       return "DW_FORM_exprloc";
12793     case DW_FORM_flag_present:
12794       return "DW_FORM_flag_present";
12795     case DW_FORM_ref_sig8:
12796       return "DW_FORM_ref_sig8";
12797     default:
12798       return "DW_FORM_<unknown>";
12799     }
12800 }
12801
12802 /* Convert a DWARF stack opcode into its string name.  */
12803
12804 const char *
12805 dwarf_stack_op_name (unsigned op)
12806 {
12807   switch (op)
12808     {
12809     case DW_OP_addr:
12810       return "DW_OP_addr";
12811     case DW_OP_deref:
12812       return "DW_OP_deref";
12813     case DW_OP_const1u:
12814       return "DW_OP_const1u";
12815     case DW_OP_const1s:
12816       return "DW_OP_const1s";
12817     case DW_OP_const2u:
12818       return "DW_OP_const2u";
12819     case DW_OP_const2s:
12820       return "DW_OP_const2s";
12821     case DW_OP_const4u:
12822       return "DW_OP_const4u";
12823     case DW_OP_const4s:
12824       return "DW_OP_const4s";
12825     case DW_OP_const8u:
12826       return "DW_OP_const8u";
12827     case DW_OP_const8s:
12828       return "DW_OP_const8s";
12829     case DW_OP_constu:
12830       return "DW_OP_constu";
12831     case DW_OP_consts:
12832       return "DW_OP_consts";
12833     case DW_OP_dup:
12834       return "DW_OP_dup";
12835     case DW_OP_drop:
12836       return "DW_OP_drop";
12837     case DW_OP_over:
12838       return "DW_OP_over";
12839     case DW_OP_pick:
12840       return "DW_OP_pick";
12841     case DW_OP_swap:
12842       return "DW_OP_swap";
12843     case DW_OP_rot:
12844       return "DW_OP_rot";
12845     case DW_OP_xderef:
12846       return "DW_OP_xderef";
12847     case DW_OP_abs:
12848       return "DW_OP_abs";
12849     case DW_OP_and:
12850       return "DW_OP_and";
12851     case DW_OP_div:
12852       return "DW_OP_div";
12853     case DW_OP_minus:
12854       return "DW_OP_minus";
12855     case DW_OP_mod:
12856       return "DW_OP_mod";
12857     case DW_OP_mul:
12858       return "DW_OP_mul";
12859     case DW_OP_neg:
12860       return "DW_OP_neg";
12861     case DW_OP_not:
12862       return "DW_OP_not";
12863     case DW_OP_or:
12864       return "DW_OP_or";
12865     case DW_OP_plus:
12866       return "DW_OP_plus";
12867     case DW_OP_plus_uconst:
12868       return "DW_OP_plus_uconst";
12869     case DW_OP_shl:
12870       return "DW_OP_shl";
12871     case DW_OP_shr:
12872       return "DW_OP_shr";
12873     case DW_OP_shra:
12874       return "DW_OP_shra";
12875     case DW_OP_xor:
12876       return "DW_OP_xor";
12877     case DW_OP_bra:
12878       return "DW_OP_bra";
12879     case DW_OP_eq:
12880       return "DW_OP_eq";
12881     case DW_OP_ge:
12882       return "DW_OP_ge";
12883     case DW_OP_gt:
12884       return "DW_OP_gt";
12885     case DW_OP_le:
12886       return "DW_OP_le";
12887     case DW_OP_lt:
12888       return "DW_OP_lt";
12889     case DW_OP_ne:
12890       return "DW_OP_ne";
12891     case DW_OP_skip:
12892       return "DW_OP_skip";
12893     case DW_OP_lit0:
12894       return "DW_OP_lit0";
12895     case DW_OP_lit1:
12896       return "DW_OP_lit1";
12897     case DW_OP_lit2:
12898       return "DW_OP_lit2";
12899     case DW_OP_lit3:
12900       return "DW_OP_lit3";
12901     case DW_OP_lit4:
12902       return "DW_OP_lit4";
12903     case DW_OP_lit5:
12904       return "DW_OP_lit5";
12905     case DW_OP_lit6:
12906       return "DW_OP_lit6";
12907     case DW_OP_lit7:
12908       return "DW_OP_lit7";
12909     case DW_OP_lit8:
12910       return "DW_OP_lit8";
12911     case DW_OP_lit9:
12912       return "DW_OP_lit9";
12913     case DW_OP_lit10:
12914       return "DW_OP_lit10";
12915     case DW_OP_lit11:
12916       return "DW_OP_lit11";
12917     case DW_OP_lit12:
12918       return "DW_OP_lit12";
12919     case DW_OP_lit13:
12920       return "DW_OP_lit13";
12921     case DW_OP_lit14:
12922       return "DW_OP_lit14";
12923     case DW_OP_lit15:
12924       return "DW_OP_lit15";
12925     case DW_OP_lit16:
12926       return "DW_OP_lit16";
12927     case DW_OP_lit17:
12928       return "DW_OP_lit17";
12929     case DW_OP_lit18:
12930       return "DW_OP_lit18";
12931     case DW_OP_lit19:
12932       return "DW_OP_lit19";
12933     case DW_OP_lit20:
12934       return "DW_OP_lit20";
12935     case DW_OP_lit21:
12936       return "DW_OP_lit21";
12937     case DW_OP_lit22:
12938       return "DW_OP_lit22";
12939     case DW_OP_lit23:
12940       return "DW_OP_lit23";
12941     case DW_OP_lit24:
12942       return "DW_OP_lit24";
12943     case DW_OP_lit25:
12944       return "DW_OP_lit25";
12945     case DW_OP_lit26:
12946       return "DW_OP_lit26";
12947     case DW_OP_lit27:
12948       return "DW_OP_lit27";
12949     case DW_OP_lit28:
12950       return "DW_OP_lit28";
12951     case DW_OP_lit29:
12952       return "DW_OP_lit29";
12953     case DW_OP_lit30:
12954       return "DW_OP_lit30";
12955     case DW_OP_lit31:
12956       return "DW_OP_lit31";
12957     case DW_OP_reg0:
12958       return "DW_OP_reg0";
12959     case DW_OP_reg1:
12960       return "DW_OP_reg1";
12961     case DW_OP_reg2:
12962       return "DW_OP_reg2";
12963     case DW_OP_reg3:
12964       return "DW_OP_reg3";
12965     case DW_OP_reg4:
12966       return "DW_OP_reg4";
12967     case DW_OP_reg5:
12968       return "DW_OP_reg5";
12969     case DW_OP_reg6:
12970       return "DW_OP_reg6";
12971     case DW_OP_reg7:
12972       return "DW_OP_reg7";
12973     case DW_OP_reg8:
12974       return "DW_OP_reg8";
12975     case DW_OP_reg9:
12976       return "DW_OP_reg9";
12977     case DW_OP_reg10:
12978       return "DW_OP_reg10";
12979     case DW_OP_reg11:
12980       return "DW_OP_reg11";
12981     case DW_OP_reg12:
12982       return "DW_OP_reg12";
12983     case DW_OP_reg13:
12984       return "DW_OP_reg13";
12985     case DW_OP_reg14:
12986       return "DW_OP_reg14";
12987     case DW_OP_reg15:
12988       return "DW_OP_reg15";
12989     case DW_OP_reg16:
12990       return "DW_OP_reg16";
12991     case DW_OP_reg17:
12992       return "DW_OP_reg17";
12993     case DW_OP_reg18:
12994       return "DW_OP_reg18";
12995     case DW_OP_reg19:
12996       return "DW_OP_reg19";
12997     case DW_OP_reg20:
12998       return "DW_OP_reg20";
12999     case DW_OP_reg21:
13000       return "DW_OP_reg21";
13001     case DW_OP_reg22:
13002       return "DW_OP_reg22";
13003     case DW_OP_reg23:
13004       return "DW_OP_reg23";
13005     case DW_OP_reg24:
13006       return "DW_OP_reg24";
13007     case DW_OP_reg25:
13008       return "DW_OP_reg25";
13009     case DW_OP_reg26:
13010       return "DW_OP_reg26";
13011     case DW_OP_reg27:
13012       return "DW_OP_reg27";
13013     case DW_OP_reg28:
13014       return "DW_OP_reg28";
13015     case DW_OP_reg29:
13016       return "DW_OP_reg29";
13017     case DW_OP_reg30:
13018       return "DW_OP_reg30";
13019     case DW_OP_reg31:
13020       return "DW_OP_reg31";
13021     case DW_OP_breg0:
13022       return "DW_OP_breg0";
13023     case DW_OP_breg1:
13024       return "DW_OP_breg1";
13025     case DW_OP_breg2:
13026       return "DW_OP_breg2";
13027     case DW_OP_breg3:
13028       return "DW_OP_breg3";
13029     case DW_OP_breg4:
13030       return "DW_OP_breg4";
13031     case DW_OP_breg5:
13032       return "DW_OP_breg5";
13033     case DW_OP_breg6:
13034       return "DW_OP_breg6";
13035     case DW_OP_breg7:
13036       return "DW_OP_breg7";
13037     case DW_OP_breg8:
13038       return "DW_OP_breg8";
13039     case DW_OP_breg9:
13040       return "DW_OP_breg9";
13041     case DW_OP_breg10:
13042       return "DW_OP_breg10";
13043     case DW_OP_breg11:
13044       return "DW_OP_breg11";
13045     case DW_OP_breg12:
13046       return "DW_OP_breg12";
13047     case DW_OP_breg13:
13048       return "DW_OP_breg13";
13049     case DW_OP_breg14:
13050       return "DW_OP_breg14";
13051     case DW_OP_breg15:
13052       return "DW_OP_breg15";
13053     case DW_OP_breg16:
13054       return "DW_OP_breg16";
13055     case DW_OP_breg17:
13056       return "DW_OP_breg17";
13057     case DW_OP_breg18:
13058       return "DW_OP_breg18";
13059     case DW_OP_breg19:
13060       return "DW_OP_breg19";
13061     case DW_OP_breg20:
13062       return "DW_OP_breg20";
13063     case DW_OP_breg21:
13064       return "DW_OP_breg21";
13065     case DW_OP_breg22:
13066       return "DW_OP_breg22";
13067     case DW_OP_breg23:
13068       return "DW_OP_breg23";
13069     case DW_OP_breg24:
13070       return "DW_OP_breg24";
13071     case DW_OP_breg25:
13072       return "DW_OP_breg25";
13073     case DW_OP_breg26:
13074       return "DW_OP_breg26";
13075     case DW_OP_breg27:
13076       return "DW_OP_breg27";
13077     case DW_OP_breg28:
13078       return "DW_OP_breg28";
13079     case DW_OP_breg29:
13080       return "DW_OP_breg29";
13081     case DW_OP_breg30:
13082       return "DW_OP_breg30";
13083     case DW_OP_breg31:
13084       return "DW_OP_breg31";
13085     case DW_OP_regx:
13086       return "DW_OP_regx";
13087     case DW_OP_fbreg:
13088       return "DW_OP_fbreg";
13089     case DW_OP_bregx:
13090       return "DW_OP_bregx";
13091     case DW_OP_piece:
13092       return "DW_OP_piece";
13093     case DW_OP_deref_size:
13094       return "DW_OP_deref_size";
13095     case DW_OP_xderef_size:
13096       return "DW_OP_xderef_size";
13097     case DW_OP_nop:
13098       return "DW_OP_nop";
13099     /* DWARF 3 extensions.  */
13100     case DW_OP_push_object_address:
13101       return "DW_OP_push_object_address";
13102     case DW_OP_call2:
13103       return "DW_OP_call2";
13104     case DW_OP_call4:
13105       return "DW_OP_call4";
13106     case DW_OP_call_ref:
13107       return "DW_OP_call_ref";
13108     case DW_OP_form_tls_address:
13109       return "DW_OP_form_tls_address";
13110     case DW_OP_call_frame_cfa:
13111       return "DW_OP_call_frame_cfa";
13112     case DW_OP_bit_piece:
13113       return "DW_OP_bit_piece";
13114     /* DWARF 4 extensions.  */
13115     case DW_OP_implicit_value:
13116       return "DW_OP_implicit_value";
13117     case DW_OP_stack_value:
13118       return "DW_OP_stack_value";
13119     /* GNU extensions.  */
13120     case DW_OP_GNU_push_tls_address:
13121       return "DW_OP_GNU_push_tls_address";
13122     case DW_OP_GNU_uninit:
13123       return "DW_OP_GNU_uninit";
13124     case DW_OP_GNU_implicit_pointer:
13125       return "DW_OP_GNU_implicit_pointer";
13126     case DW_OP_GNU_entry_value:
13127       return "DW_OP_GNU_entry_value";
13128     case DW_OP_GNU_const_type:
13129       return "DW_OP_GNU_const_type";
13130     case DW_OP_GNU_regval_type:
13131       return "DW_OP_GNU_regval_type";
13132     case DW_OP_GNU_deref_type:
13133       return "DW_OP_GNU_deref_type";
13134     case DW_OP_GNU_convert:
13135       return "DW_OP_GNU_convert";
13136     case DW_OP_GNU_reinterpret:
13137       return "DW_OP_GNU_reinterpret";
13138     default:
13139       return NULL;
13140     }
13141 }
13142
13143 static char *
13144 dwarf_bool_name (unsigned mybool)
13145 {
13146   if (mybool)
13147     return "TRUE";
13148   else
13149     return "FALSE";
13150 }
13151
13152 /* Convert a DWARF type code into its string name.  */
13153
13154 static char *
13155 dwarf_type_encoding_name (unsigned enc)
13156 {
13157   switch (enc)
13158     {
13159     case DW_ATE_void:
13160       return "DW_ATE_void";
13161     case DW_ATE_address:
13162       return "DW_ATE_address";
13163     case DW_ATE_boolean:
13164       return "DW_ATE_boolean";
13165     case DW_ATE_complex_float:
13166       return "DW_ATE_complex_float";
13167     case DW_ATE_float:
13168       return "DW_ATE_float";
13169     case DW_ATE_signed:
13170       return "DW_ATE_signed";
13171     case DW_ATE_signed_char:
13172       return "DW_ATE_signed_char";
13173     case DW_ATE_unsigned:
13174       return "DW_ATE_unsigned";
13175     case DW_ATE_unsigned_char:
13176       return "DW_ATE_unsigned_char";
13177     /* DWARF 3.  */
13178     case DW_ATE_imaginary_float:
13179       return "DW_ATE_imaginary_float";
13180     case DW_ATE_packed_decimal:
13181       return "DW_ATE_packed_decimal";
13182     case DW_ATE_numeric_string:
13183       return "DW_ATE_numeric_string";
13184     case DW_ATE_edited:
13185       return "DW_ATE_edited";
13186     case DW_ATE_signed_fixed:
13187       return "DW_ATE_signed_fixed";
13188     case DW_ATE_unsigned_fixed:
13189       return "DW_ATE_unsigned_fixed";
13190     case DW_ATE_decimal_float:
13191       return "DW_ATE_decimal_float";
13192     /* DWARF 4.  */
13193     case DW_ATE_UTF:
13194       return "DW_ATE_UTF";
13195     /* HP extensions.  */
13196     case DW_ATE_HP_float80:
13197       return "DW_ATE_HP_float80";
13198     case DW_ATE_HP_complex_float80:
13199       return "DW_ATE_HP_complex_float80";
13200     case DW_ATE_HP_float128:
13201       return "DW_ATE_HP_float128";
13202     case DW_ATE_HP_complex_float128:
13203       return "DW_ATE_HP_complex_float128";
13204     case DW_ATE_HP_floathpintel:
13205       return "DW_ATE_HP_floathpintel";
13206     case DW_ATE_HP_imaginary_float80:
13207       return "DW_ATE_HP_imaginary_float80";
13208     case DW_ATE_HP_imaginary_float128:
13209       return "DW_ATE_HP_imaginary_float128";
13210     default:
13211       return "DW_ATE_<unknown>";
13212     }
13213 }
13214
13215 /* Convert a DWARF call frame info operation to its string name.  */
13216
13217 #if 0
13218 static char *
13219 dwarf_cfi_name (unsigned cfi_opc)
13220 {
13221   switch (cfi_opc)
13222     {
13223     case DW_CFA_advance_loc:
13224       return "DW_CFA_advance_loc";
13225     case DW_CFA_offset:
13226       return "DW_CFA_offset";
13227     case DW_CFA_restore:
13228       return "DW_CFA_restore";
13229     case DW_CFA_nop:
13230       return "DW_CFA_nop";
13231     case DW_CFA_set_loc:
13232       return "DW_CFA_set_loc";
13233     case DW_CFA_advance_loc1:
13234       return "DW_CFA_advance_loc1";
13235     case DW_CFA_advance_loc2:
13236       return "DW_CFA_advance_loc2";
13237     case DW_CFA_advance_loc4:
13238       return "DW_CFA_advance_loc4";
13239     case DW_CFA_offset_extended:
13240       return "DW_CFA_offset_extended";
13241     case DW_CFA_restore_extended:
13242       return "DW_CFA_restore_extended";
13243     case DW_CFA_undefined:
13244       return "DW_CFA_undefined";
13245     case DW_CFA_same_value:
13246       return "DW_CFA_same_value";
13247     case DW_CFA_register:
13248       return "DW_CFA_register";
13249     case DW_CFA_remember_state:
13250       return "DW_CFA_remember_state";
13251     case DW_CFA_restore_state:
13252       return "DW_CFA_restore_state";
13253     case DW_CFA_def_cfa:
13254       return "DW_CFA_def_cfa";
13255     case DW_CFA_def_cfa_register:
13256       return "DW_CFA_def_cfa_register";
13257     case DW_CFA_def_cfa_offset:
13258       return "DW_CFA_def_cfa_offset";
13259     /* DWARF 3.  */
13260     case DW_CFA_def_cfa_expression:
13261       return "DW_CFA_def_cfa_expression";
13262     case DW_CFA_expression:
13263       return "DW_CFA_expression";
13264     case DW_CFA_offset_extended_sf:
13265       return "DW_CFA_offset_extended_sf";
13266     case DW_CFA_def_cfa_sf:
13267       return "DW_CFA_def_cfa_sf";
13268     case DW_CFA_def_cfa_offset_sf:
13269       return "DW_CFA_def_cfa_offset_sf";
13270     case DW_CFA_val_offset:
13271       return "DW_CFA_val_offset";
13272     case DW_CFA_val_offset_sf:
13273       return "DW_CFA_val_offset_sf";
13274     case DW_CFA_val_expression:
13275       return "DW_CFA_val_expression";
13276     /* SGI/MIPS specific.  */
13277     case DW_CFA_MIPS_advance_loc8:
13278       return "DW_CFA_MIPS_advance_loc8";
13279     /* GNU extensions.  */
13280     case DW_CFA_GNU_window_save:
13281       return "DW_CFA_GNU_window_save";
13282     case DW_CFA_GNU_args_size:
13283       return "DW_CFA_GNU_args_size";
13284     case DW_CFA_GNU_negative_offset_extended:
13285       return "DW_CFA_GNU_negative_offset_extended";
13286     default:
13287       return "DW_CFA_<unknown>";
13288     }
13289 }
13290 #endif
13291
13292 static void
13293 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13294 {
13295   unsigned int i;
13296
13297   print_spaces (indent, f);
13298   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13299            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13300
13301   if (die->parent != NULL)
13302     {
13303       print_spaces (indent, f);
13304       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13305                           die->parent->offset);
13306     }
13307
13308   print_spaces (indent, f);
13309   fprintf_unfiltered (f, "  has children: %s\n",
13310            dwarf_bool_name (die->child != NULL));
13311
13312   print_spaces (indent, f);
13313   fprintf_unfiltered (f, "  attributes:\n");
13314
13315   for (i = 0; i < die->num_attrs; ++i)
13316     {
13317       print_spaces (indent, f);
13318       fprintf_unfiltered (f, "    %s (%s) ",
13319                dwarf_attr_name (die->attrs[i].name),
13320                dwarf_form_name (die->attrs[i].form));
13321
13322       switch (die->attrs[i].form)
13323         {
13324         case DW_FORM_ref_addr:
13325         case DW_FORM_addr:
13326           fprintf_unfiltered (f, "address: ");
13327           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13328           break;
13329         case DW_FORM_block2:
13330         case DW_FORM_block4:
13331         case DW_FORM_block:
13332         case DW_FORM_block1:
13333           fprintf_unfiltered (f, "block: size %d",
13334                               DW_BLOCK (&die->attrs[i])->size);
13335           break;
13336         case DW_FORM_exprloc:
13337           fprintf_unfiltered (f, "expression: size %u",
13338                               DW_BLOCK (&die->attrs[i])->size);
13339           break;
13340         case DW_FORM_ref1:
13341         case DW_FORM_ref2:
13342         case DW_FORM_ref4:
13343           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13344                               (long) (DW_ADDR (&die->attrs[i])));
13345           break;
13346         case DW_FORM_data1:
13347         case DW_FORM_data2:
13348         case DW_FORM_data4:
13349         case DW_FORM_data8:
13350         case DW_FORM_udata:
13351         case DW_FORM_sdata:
13352           fprintf_unfiltered (f, "constant: %s",
13353                               pulongest (DW_UNSND (&die->attrs[i])));
13354           break;
13355         case DW_FORM_sec_offset:
13356           fprintf_unfiltered (f, "section offset: %s",
13357                               pulongest (DW_UNSND (&die->attrs[i])));
13358           break;
13359         case DW_FORM_ref_sig8:
13360           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13361             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13362                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13363           else
13364             fprintf_unfiltered (f, "signatured type, offset: unknown");
13365           break;
13366         case DW_FORM_string:
13367         case DW_FORM_strp:
13368           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13369                    DW_STRING (&die->attrs[i])
13370                    ? DW_STRING (&die->attrs[i]) : "",
13371                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13372           break;
13373         case DW_FORM_flag:
13374           if (DW_UNSND (&die->attrs[i]))
13375             fprintf_unfiltered (f, "flag: TRUE");
13376           else
13377             fprintf_unfiltered (f, "flag: FALSE");
13378           break;
13379         case DW_FORM_flag_present:
13380           fprintf_unfiltered (f, "flag: TRUE");
13381           break;
13382         case DW_FORM_indirect:
13383           /* The reader will have reduced the indirect form to
13384              the "base form" so this form should not occur.  */
13385           fprintf_unfiltered (f, 
13386                               "unexpected attribute form: DW_FORM_indirect");
13387           break;
13388         default:
13389           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13390                    die->attrs[i].form);
13391           break;
13392         }
13393       fprintf_unfiltered (f, "\n");
13394     }
13395 }
13396
13397 static void
13398 dump_die_for_error (struct die_info *die)
13399 {
13400   dump_die_shallow (gdb_stderr, 0, die);
13401 }
13402
13403 static void
13404 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13405 {
13406   int indent = level * 4;
13407
13408   gdb_assert (die != NULL);
13409
13410   if (level >= max_level)
13411     return;
13412
13413   dump_die_shallow (f, indent, die);
13414
13415   if (die->child != NULL)
13416     {
13417       print_spaces (indent, f);
13418       fprintf_unfiltered (f, "  Children:");
13419       if (level + 1 < max_level)
13420         {
13421           fprintf_unfiltered (f, "\n");
13422           dump_die_1 (f, level + 1, max_level, die->child);
13423         }
13424       else
13425         {
13426           fprintf_unfiltered (f,
13427                               " [not printed, max nesting level reached]\n");
13428         }
13429     }
13430
13431   if (die->sibling != NULL && level > 0)
13432     {
13433       dump_die_1 (f, level, max_level, die->sibling);
13434     }
13435 }
13436
13437 /* This is called from the pdie macro in gdbinit.in.
13438    It's not static so gcc will keep a copy callable from gdb.  */
13439
13440 void
13441 dump_die (struct die_info *die, int max_level)
13442 {
13443   dump_die_1 (gdb_stdlog, 0, max_level, die);
13444 }
13445
13446 static void
13447 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13448 {
13449   void **slot;
13450
13451   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13452
13453   *slot = die;
13454 }
13455
13456 static int
13457 is_ref_attr (struct attribute *attr)
13458 {
13459   switch (attr->form)
13460     {
13461     case DW_FORM_ref_addr:
13462     case DW_FORM_ref1:
13463     case DW_FORM_ref2:
13464     case DW_FORM_ref4:
13465     case DW_FORM_ref8:
13466     case DW_FORM_ref_udata:
13467       return 1;
13468     default:
13469       return 0;
13470     }
13471 }
13472
13473 static unsigned int
13474 dwarf2_get_ref_die_offset (struct attribute *attr)
13475 {
13476   if (is_ref_attr (attr))
13477     return DW_ADDR (attr);
13478
13479   complaint (&symfile_complaints,
13480              _("unsupported die ref attribute form: '%s'"),
13481              dwarf_form_name (attr->form));
13482   return 0;
13483 }
13484
13485 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13486  * the value held by the attribute is not constant.  */
13487
13488 static LONGEST
13489 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13490 {
13491   if (attr->form == DW_FORM_sdata)
13492     return DW_SND (attr);
13493   else if (attr->form == DW_FORM_udata
13494            || attr->form == DW_FORM_data1
13495            || attr->form == DW_FORM_data2
13496            || attr->form == DW_FORM_data4
13497            || attr->form == DW_FORM_data8)
13498     return DW_UNSND (attr);
13499   else
13500     {
13501       complaint (&symfile_complaints,
13502                  _("Attribute value is not a constant (%s)"),
13503                  dwarf_form_name (attr->form));
13504       return default_value;
13505     }
13506 }
13507
13508 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
13509    unit and add it to our queue.
13510    The result is non-zero if PER_CU was queued, otherwise the result is zero
13511    meaning either PER_CU is already queued or it is already loaded.  */
13512
13513 static int
13514 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13515                        struct dwarf2_per_cu_data *per_cu)
13516 {
13517   /* We may arrive here during partial symbol reading, if we need full
13518      DIEs to process an unusual case (e.g. template arguments).  Do
13519      not queue PER_CU, just tell our caller to load its DIEs.  */
13520   if (dwarf2_per_objfile->reading_partial_symbols)
13521     {
13522       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13523         return 1;
13524       return 0;
13525     }
13526
13527   /* Mark the dependence relation so that we don't flush PER_CU
13528      too early.  */
13529   dwarf2_add_dependence (this_cu, per_cu);
13530
13531   /* If it's already on the queue, we have nothing to do.  */
13532   if (per_cu->queued)
13533     return 0;
13534
13535   /* If the compilation unit is already loaded, just mark it as
13536      used.  */
13537   if (per_cu->cu != NULL)
13538     {
13539       per_cu->cu->last_used = 0;
13540       return 0;
13541     }
13542
13543   /* Add it to the queue.  */
13544   queue_comp_unit (per_cu, this_cu->objfile);
13545
13546   return 1;
13547 }
13548
13549 /* Follow reference or signature attribute ATTR of SRC_DIE.
13550    On entry *REF_CU is the CU of SRC_DIE.
13551    On exit *REF_CU is the CU of the result.  */
13552
13553 static struct die_info *
13554 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13555                        struct dwarf2_cu **ref_cu)
13556 {
13557   struct die_info *die;
13558
13559   if (is_ref_attr (attr))
13560     die = follow_die_ref (src_die, attr, ref_cu);
13561   else if (attr->form == DW_FORM_ref_sig8)
13562     die = follow_die_sig (src_die, attr, ref_cu);
13563   else
13564     {
13565       dump_die_for_error (src_die);
13566       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13567              (*ref_cu)->objfile->name);
13568     }
13569
13570   return die;
13571 }
13572
13573 /* Follow reference OFFSET.
13574    On entry *REF_CU is the CU of the source die referencing OFFSET.
13575    On exit *REF_CU is the CU of the result.
13576    Returns NULL if OFFSET is invalid.  */
13577
13578 static struct die_info *
13579 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13580 {
13581   struct die_info temp_die;
13582   struct dwarf2_cu *target_cu, *cu = *ref_cu;
13583
13584   gdb_assert (cu->per_cu != NULL);
13585
13586   target_cu = cu;
13587
13588   if (cu->per_cu->from_debug_types)
13589     {
13590       /* .debug_types CUs cannot reference anything outside their CU.
13591          If they need to, they have to reference a signatured type via
13592          DW_FORM_ref_sig8.  */
13593       if (! offset_in_cu_p (&cu->header, offset))
13594         return NULL;
13595     }
13596   else if (! offset_in_cu_p (&cu->header, offset))
13597     {
13598       struct dwarf2_per_cu_data *per_cu;
13599
13600       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13601
13602       /* If necessary, add it to the queue and load its DIEs.  */
13603       if (maybe_queue_comp_unit (cu, per_cu))
13604         load_full_comp_unit (per_cu, cu->objfile);
13605
13606       target_cu = per_cu->cu;
13607     }
13608   else if (cu->dies == NULL)
13609     {
13610       /* We're loading full DIEs during partial symbol reading.  */
13611       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13612       load_full_comp_unit (cu->per_cu, cu->objfile);
13613     }
13614
13615   *ref_cu = target_cu;
13616   temp_die.offset = offset;
13617   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13618 }
13619
13620 /* Follow reference attribute ATTR of SRC_DIE.
13621    On entry *REF_CU is the CU of SRC_DIE.
13622    On exit *REF_CU is the CU of the result.  */
13623
13624 static struct die_info *
13625 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13626                 struct dwarf2_cu **ref_cu)
13627 {
13628   unsigned int offset = dwarf2_get_ref_die_offset (attr);
13629   struct dwarf2_cu *cu = *ref_cu;
13630   struct die_info *die;
13631
13632   die = follow_die_offset (offset, ref_cu);
13633   if (!die)
13634     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13635            "at 0x%x [in module %s]"),
13636            offset, src_die->offset, cu->objfile->name);
13637
13638   return die;
13639 }
13640
13641 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
13642    value is intended for DW_OP_call*.  */
13643
13644 struct dwarf2_locexpr_baton
13645 dwarf2_fetch_die_location_block (unsigned int offset,
13646                                  struct dwarf2_per_cu_data *per_cu,
13647                                  CORE_ADDR (*get_frame_pc) (void *baton),
13648                                  void *baton)
13649 {
13650   struct dwarf2_cu *cu = per_cu->cu;
13651   struct die_info *die;
13652   struct attribute *attr;
13653   struct dwarf2_locexpr_baton retval;
13654
13655   dw2_setup (per_cu->objfile);
13656
13657   die = follow_die_offset (offset, &cu);
13658   if (!die)
13659     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13660            offset, per_cu->cu->objfile->name);
13661
13662   attr = dwarf2_attr (die, DW_AT_location, cu);
13663   if (!attr)
13664     {
13665       /* DWARF: "If there is no such attribute, then there is no effect.".  */
13666
13667       retval.data = NULL;
13668       retval.size = 0;
13669     }
13670   else if (attr_form_is_section_offset (attr))
13671     {
13672       struct dwarf2_loclist_baton loclist_baton;
13673       CORE_ADDR pc = (*get_frame_pc) (baton);
13674       size_t size;
13675
13676       fill_in_loclist_baton (cu, &loclist_baton, attr);
13677
13678       retval.data = dwarf2_find_location_expression (&loclist_baton,
13679                                                      &size, pc);
13680       retval.size = size;
13681     }
13682   else
13683     {
13684       if (!attr_form_is_block (attr))
13685         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13686                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13687                offset, per_cu->cu->objfile->name);
13688
13689       retval.data = DW_BLOCK (attr)->data;
13690       retval.size = DW_BLOCK (attr)->size;
13691     }
13692   retval.per_cu = cu->per_cu;
13693   return retval;
13694 }
13695
13696 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13697    PER_CU.  */
13698
13699 struct type *
13700 dwarf2_get_die_type (unsigned int die_offset,
13701                      struct dwarf2_per_cu_data *per_cu)
13702 {
13703   dw2_setup (per_cu->objfile);
13704   return get_die_type_at_offset (die_offset, per_cu);
13705 }
13706
13707 /* Follow the signature attribute ATTR in SRC_DIE.
13708    On entry *REF_CU is the CU of SRC_DIE.
13709    On exit *REF_CU is the CU of the result.  */
13710
13711 static struct die_info *
13712 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13713                 struct dwarf2_cu **ref_cu)
13714 {
13715   struct objfile *objfile = (*ref_cu)->objfile;
13716   struct die_info temp_die;
13717   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13718   struct dwarf2_cu *sig_cu;
13719   struct die_info *die;
13720
13721   /* sig_type will be NULL if the signatured type is missing from
13722      the debug info.  */
13723   if (sig_type == NULL)
13724     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13725              "at 0x%x [in module %s]"),
13726            src_die->offset, objfile->name);
13727
13728   /* If necessary, add it to the queue and load its DIEs.  */
13729
13730   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13731     read_signatured_type (objfile, sig_type);
13732
13733   gdb_assert (sig_type->per_cu.cu != NULL);
13734
13735   sig_cu = sig_type->per_cu.cu;
13736   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13737   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13738   if (die)
13739     {
13740       *ref_cu = sig_cu;
13741       return die;
13742     }
13743
13744   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13745          "from DIE at 0x%x [in module %s]"),
13746          sig_type->type_offset, src_die->offset, objfile->name);
13747 }
13748
13749 /* Given an offset of a signatured type, return its signatured_type.  */
13750
13751 static struct signatured_type *
13752 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13753 {
13754   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13755   unsigned int length, initial_length_size;
13756   unsigned int sig_offset;
13757   struct signatured_type find_entry, *type_sig;
13758
13759   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13760   sig_offset = (initial_length_size
13761                 + 2 /*version*/
13762                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13763                 + 1 /*address_size*/);
13764   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13765   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13766
13767   /* This is only used to lookup previously recorded types.
13768      If we didn't find it, it's our bug.  */
13769   gdb_assert (type_sig != NULL);
13770   gdb_assert (offset == type_sig->per_cu.offset);
13771
13772   return type_sig;
13773 }
13774
13775 /* Read in signatured type at OFFSET and build its CU and die(s).  */
13776
13777 static void
13778 read_signatured_type_at_offset (struct objfile *objfile,
13779                                 unsigned int offset)
13780 {
13781   struct signatured_type *type_sig;
13782
13783   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13784
13785   /* We have the section offset, but we need the signature to do the
13786      hash table lookup.  */
13787   type_sig = lookup_signatured_type_at_offset (objfile, offset);
13788
13789   gdb_assert (type_sig->per_cu.cu == NULL);
13790
13791   read_signatured_type (objfile, type_sig);
13792
13793   gdb_assert (type_sig->per_cu.cu != NULL);
13794 }
13795
13796 /* Read in a signatured type and build its CU and DIEs.  */
13797
13798 static void
13799 read_signatured_type (struct objfile *objfile,
13800                       struct signatured_type *type_sig)
13801 {
13802   gdb_byte *types_ptr;
13803   struct die_reader_specs reader_specs;
13804   struct dwarf2_cu *cu;
13805   ULONGEST signature;
13806   struct cleanup *back_to, *free_cu_cleanup;
13807
13808   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13809   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
13810
13811   gdb_assert (type_sig->per_cu.cu == NULL);
13812
13813   cu = xmalloc (sizeof (*cu));
13814   init_one_comp_unit (cu, objfile);
13815
13816   type_sig->per_cu.cu = cu;
13817   cu->per_cu = &type_sig->per_cu;
13818
13819   /* If an error occurs while loading, release our storage.  */
13820   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13821
13822   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13823                                         types_ptr, objfile->obfd);
13824   gdb_assert (signature == type_sig->signature);
13825
13826   cu->die_hash
13827     = htab_create_alloc_ex (cu->header.length / 12,
13828                             die_hash,
13829                             die_eq,
13830                             NULL,
13831                             &cu->comp_unit_obstack,
13832                             hashtab_obstack_allocate,
13833                             dummy_obstack_deallocate);
13834
13835   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13836   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13837
13838   init_cu_die_reader (&reader_specs, cu);
13839
13840   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13841                                     NULL /*parent*/);
13842
13843   /* We try not to read any attributes in this function, because not
13844      all objfiles needed for references have been loaded yet, and symbol
13845      table processing isn't initialized.  But we have to set the CU language,
13846      or we won't be able to build types correctly.  */
13847   prepare_one_comp_unit (cu, cu->dies);
13848
13849   do_cleanups (back_to);
13850
13851   /* We've successfully allocated this compilation unit.  Let our caller
13852      clean it up when finished with it.  */
13853   discard_cleanups (free_cu_cleanup);
13854
13855   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13856   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13857 }
13858
13859 /* Decode simple location descriptions.
13860    Given a pointer to a dwarf block that defines a location, compute
13861    the location and return the value.
13862
13863    NOTE drow/2003-11-18: This function is called in two situations
13864    now: for the address of static or global variables (partial symbols
13865    only) and for offsets into structures which are expected to be
13866    (more or less) constant.  The partial symbol case should go away,
13867    and only the constant case should remain.  That will let this
13868    function complain more accurately.  A few special modes are allowed
13869    without complaint for global variables (for instance, global
13870    register values and thread-local values).
13871
13872    A location description containing no operations indicates that the
13873    object is optimized out.  The return value is 0 for that case.
13874    FIXME drow/2003-11-16: No callers check for this case any more; soon all
13875    callers will only want a very basic result and this can become a
13876    complaint.
13877
13878    Note that stack[0] is unused except as a default error return.  */
13879
13880 static CORE_ADDR
13881 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13882 {
13883   struct objfile *objfile = cu->objfile;
13884   int i;
13885   int size = blk->size;
13886   gdb_byte *data = blk->data;
13887   CORE_ADDR stack[64];
13888   int stacki;
13889   unsigned int bytes_read, unsnd;
13890   gdb_byte op;
13891
13892   i = 0;
13893   stacki = 0;
13894   stack[stacki] = 0;
13895   stack[++stacki] = 0;
13896
13897   while (i < size)
13898     {
13899       op = data[i++];
13900       switch (op)
13901         {
13902         case DW_OP_lit0:
13903         case DW_OP_lit1:
13904         case DW_OP_lit2:
13905         case DW_OP_lit3:
13906         case DW_OP_lit4:
13907         case DW_OP_lit5:
13908         case DW_OP_lit6:
13909         case DW_OP_lit7:
13910         case DW_OP_lit8:
13911         case DW_OP_lit9:
13912         case DW_OP_lit10:
13913         case DW_OP_lit11:
13914         case DW_OP_lit12:
13915         case DW_OP_lit13:
13916         case DW_OP_lit14:
13917         case DW_OP_lit15:
13918         case DW_OP_lit16:
13919         case DW_OP_lit17:
13920         case DW_OP_lit18:
13921         case DW_OP_lit19:
13922         case DW_OP_lit20:
13923         case DW_OP_lit21:
13924         case DW_OP_lit22:
13925         case DW_OP_lit23:
13926         case DW_OP_lit24:
13927         case DW_OP_lit25:
13928         case DW_OP_lit26:
13929         case DW_OP_lit27:
13930         case DW_OP_lit28:
13931         case DW_OP_lit29:
13932         case DW_OP_lit30:
13933         case DW_OP_lit31:
13934           stack[++stacki] = op - DW_OP_lit0;
13935           break;
13936
13937         case DW_OP_reg0:
13938         case DW_OP_reg1:
13939         case DW_OP_reg2:
13940         case DW_OP_reg3:
13941         case DW_OP_reg4:
13942         case DW_OP_reg5:
13943         case DW_OP_reg6:
13944         case DW_OP_reg7:
13945         case DW_OP_reg8:
13946         case DW_OP_reg9:
13947         case DW_OP_reg10:
13948         case DW_OP_reg11:
13949         case DW_OP_reg12:
13950         case DW_OP_reg13:
13951         case DW_OP_reg14:
13952         case DW_OP_reg15:
13953         case DW_OP_reg16:
13954         case DW_OP_reg17:
13955         case DW_OP_reg18:
13956         case DW_OP_reg19:
13957         case DW_OP_reg20:
13958         case DW_OP_reg21:
13959         case DW_OP_reg22:
13960         case DW_OP_reg23:
13961         case DW_OP_reg24:
13962         case DW_OP_reg25:
13963         case DW_OP_reg26:
13964         case DW_OP_reg27:
13965         case DW_OP_reg28:
13966         case DW_OP_reg29:
13967         case DW_OP_reg30:
13968         case DW_OP_reg31:
13969           stack[++stacki] = op - DW_OP_reg0;
13970           if (i < size)
13971             dwarf2_complex_location_expr_complaint ();
13972           break;
13973
13974         case DW_OP_regx:
13975           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13976           i += bytes_read;
13977           stack[++stacki] = unsnd;
13978           if (i < size)
13979             dwarf2_complex_location_expr_complaint ();
13980           break;
13981
13982         case DW_OP_addr:
13983           stack[++stacki] = read_address (objfile->obfd, &data[i],
13984                                           cu, &bytes_read);
13985           i += bytes_read;
13986           break;
13987
13988         case DW_OP_const1u:
13989           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13990           i += 1;
13991           break;
13992
13993         case DW_OP_const1s:
13994           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13995           i += 1;
13996           break;
13997
13998         case DW_OP_const2u:
13999           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14000           i += 2;
14001           break;
14002
14003         case DW_OP_const2s:
14004           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14005           i += 2;
14006           break;
14007
14008         case DW_OP_const4u:
14009           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14010           i += 4;
14011           break;
14012
14013         case DW_OP_const4s:
14014           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14015           i += 4;
14016           break;
14017
14018         case DW_OP_constu:
14019           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14020                                                   &bytes_read);
14021           i += bytes_read;
14022           break;
14023
14024         case DW_OP_consts:
14025           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14026           i += bytes_read;
14027           break;
14028
14029         case DW_OP_dup:
14030           stack[stacki + 1] = stack[stacki];
14031           stacki++;
14032           break;
14033
14034         case DW_OP_plus:
14035           stack[stacki - 1] += stack[stacki];
14036           stacki--;
14037           break;
14038
14039         case DW_OP_plus_uconst:
14040           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14041                                                  &bytes_read);
14042           i += bytes_read;
14043           break;
14044
14045         case DW_OP_minus:
14046           stack[stacki - 1] -= stack[stacki];
14047           stacki--;
14048           break;
14049
14050         case DW_OP_deref:
14051           /* If we're not the last op, then we definitely can't encode
14052              this using GDB's address_class enum.  This is valid for partial
14053              global symbols, although the variable's address will be bogus
14054              in the psymtab.  */
14055           if (i < size)
14056             dwarf2_complex_location_expr_complaint ();
14057           break;
14058
14059         case DW_OP_GNU_push_tls_address:
14060           /* The top of the stack has the offset from the beginning
14061              of the thread control block at which the variable is located.  */
14062           /* Nothing should follow this operator, so the top of stack would
14063              be returned.  */
14064           /* This is valid for partial global symbols, but the variable's
14065              address will be bogus in the psymtab.  */
14066           if (i < size)
14067             dwarf2_complex_location_expr_complaint ();
14068           break;
14069
14070         case DW_OP_GNU_uninit:
14071           break;
14072
14073         default:
14074           {
14075             const char *name = dwarf_stack_op_name (op);
14076
14077             if (name)
14078               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14079                          name);
14080             else
14081               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14082                          op);
14083           }
14084
14085           return (stack[stacki]);
14086         }
14087
14088       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14089          outside of the allocated space.  Also enforce minimum>0.  */
14090       if (stacki >= ARRAY_SIZE (stack) - 1)
14091         {
14092           complaint (&symfile_complaints,
14093                      _("location description stack overflow"));
14094           return 0;
14095         }
14096
14097       if (stacki <= 0)
14098         {
14099           complaint (&symfile_complaints,
14100                      _("location description stack underflow"));
14101           return 0;
14102         }
14103     }
14104   return (stack[stacki]);
14105 }
14106
14107 /* memory allocation interface */
14108
14109 static struct dwarf_block *
14110 dwarf_alloc_block (struct dwarf2_cu *cu)
14111 {
14112   struct dwarf_block *blk;
14113
14114   blk = (struct dwarf_block *)
14115     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14116   return (blk);
14117 }
14118
14119 static struct abbrev_info *
14120 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14121 {
14122   struct abbrev_info *abbrev;
14123
14124   abbrev = (struct abbrev_info *)
14125     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14126   memset (abbrev, 0, sizeof (struct abbrev_info));
14127   return (abbrev);
14128 }
14129
14130 static struct die_info *
14131 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14132 {
14133   struct die_info *die;
14134   size_t size = sizeof (struct die_info);
14135
14136   if (num_attrs > 1)
14137     size += (num_attrs - 1) * sizeof (struct attribute);
14138
14139   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14140   memset (die, 0, sizeof (struct die_info));
14141   return (die);
14142 }
14143
14144 \f
14145 /* Macro support.  */
14146
14147 /* Return the full name of file number I in *LH's file name table.
14148    Use COMP_DIR as the name of the current directory of the
14149    compilation.  The result is allocated using xmalloc; the caller is
14150    responsible for freeing it.  */
14151 static char *
14152 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14153 {
14154   /* Is the file number a valid index into the line header's file name
14155      table?  Remember that file numbers start with one, not zero.  */
14156   if (1 <= file && file <= lh->num_file_names)
14157     {
14158       struct file_entry *fe = &lh->file_names[file - 1];
14159
14160       if (IS_ABSOLUTE_PATH (fe->name))
14161         return xstrdup (fe->name);
14162       else
14163         {
14164           const char *dir;
14165           int dir_len;
14166           char *full_name;
14167
14168           if (fe->dir_index)
14169             dir = lh->include_dirs[fe->dir_index - 1];
14170           else
14171             dir = comp_dir;
14172
14173           if (dir)
14174             {
14175               dir_len = strlen (dir);
14176               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14177               strcpy (full_name, dir);
14178               full_name[dir_len] = '/';
14179               strcpy (full_name + dir_len + 1, fe->name);
14180               return full_name;
14181             }
14182           else
14183             return xstrdup (fe->name);
14184         }
14185     }
14186   else
14187     {
14188       /* The compiler produced a bogus file number.  We can at least
14189          record the macro definitions made in the file, even if we
14190          won't be able to find the file by name.  */
14191       char fake_name[80];
14192
14193       sprintf (fake_name, "<bad macro file number %d>", file);
14194
14195       complaint (&symfile_complaints,
14196                  _("bad file number in macro information (%d)"),
14197                  file);
14198
14199       return xstrdup (fake_name);
14200     }
14201 }
14202
14203
14204 static struct macro_source_file *
14205 macro_start_file (int file, int line,
14206                   struct macro_source_file *current_file,
14207                   const char *comp_dir,
14208                   struct line_header *lh, struct objfile *objfile)
14209 {
14210   /* The full name of this source file.  */
14211   char *full_name = file_full_name (file, lh, comp_dir);
14212
14213   /* We don't create a macro table for this compilation unit
14214      at all until we actually get a filename.  */
14215   if (! pending_macros)
14216     pending_macros = new_macro_table (&objfile->objfile_obstack,
14217                                       objfile->macro_cache);
14218
14219   if (! current_file)
14220     /* If we have no current file, then this must be the start_file
14221        directive for the compilation unit's main source file.  */
14222     current_file = macro_set_main (pending_macros, full_name);
14223   else
14224     current_file = macro_include (current_file, line, full_name);
14225
14226   xfree (full_name);
14227
14228   return current_file;
14229 }
14230
14231
14232 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14233    followed by a null byte.  */
14234 static char *
14235 copy_string (const char *buf, int len)
14236 {
14237   char *s = xmalloc (len + 1);
14238
14239   memcpy (s, buf, len);
14240   s[len] = '\0';
14241   return s;
14242 }
14243
14244
14245 static const char *
14246 consume_improper_spaces (const char *p, const char *body)
14247 {
14248   if (*p == ' ')
14249     {
14250       complaint (&symfile_complaints,
14251                  _("macro definition contains spaces "
14252                    "in formal argument list:\n`%s'"),
14253                  body);
14254
14255       while (*p == ' ')
14256         p++;
14257     }
14258
14259   return p;
14260 }
14261
14262
14263 static void
14264 parse_macro_definition (struct macro_source_file *file, int line,
14265                         const char *body)
14266 {
14267   const char *p;
14268
14269   /* The body string takes one of two forms.  For object-like macro
14270      definitions, it should be:
14271
14272         <macro name> " " <definition>
14273
14274      For function-like macro definitions, it should be:
14275
14276         <macro name> "() " <definition>
14277      or
14278         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14279
14280      Spaces may appear only where explicitly indicated, and in the
14281      <definition>.
14282
14283      The Dwarf 2 spec says that an object-like macro's name is always
14284      followed by a space, but versions of GCC around March 2002 omit
14285      the space when the macro's definition is the empty string.
14286
14287      The Dwarf 2 spec says that there should be no spaces between the
14288      formal arguments in a function-like macro's formal argument list,
14289      but versions of GCC around March 2002 include spaces after the
14290      commas.  */
14291
14292
14293   /* Find the extent of the macro name.  The macro name is terminated
14294      by either a space or null character (for an object-like macro) or
14295      an opening paren (for a function-like macro).  */
14296   for (p = body; *p; p++)
14297     if (*p == ' ' || *p == '(')
14298       break;
14299
14300   if (*p == ' ' || *p == '\0')
14301     {
14302       /* It's an object-like macro.  */
14303       int name_len = p - body;
14304       char *name = copy_string (body, name_len);
14305       const char *replacement;
14306
14307       if (*p == ' ')
14308         replacement = body + name_len + 1;
14309       else
14310         {
14311           dwarf2_macro_malformed_definition_complaint (body);
14312           replacement = body + name_len;
14313         }
14314
14315       macro_define_object (file, line, name, replacement);
14316
14317       xfree (name);
14318     }
14319   else if (*p == '(')
14320     {
14321       /* It's a function-like macro.  */
14322       char *name = copy_string (body, p - body);
14323       int argc = 0;
14324       int argv_size = 1;
14325       char **argv = xmalloc (argv_size * sizeof (*argv));
14326
14327       p++;
14328
14329       p = consume_improper_spaces (p, body);
14330
14331       /* Parse the formal argument list.  */
14332       while (*p && *p != ')')
14333         {
14334           /* Find the extent of the current argument name.  */
14335           const char *arg_start = p;
14336
14337           while (*p && *p != ',' && *p != ')' && *p != ' ')
14338             p++;
14339
14340           if (! *p || p == arg_start)
14341             dwarf2_macro_malformed_definition_complaint (body);
14342           else
14343             {
14344               /* Make sure argv has room for the new argument.  */
14345               if (argc >= argv_size)
14346                 {
14347                   argv_size *= 2;
14348                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14349                 }
14350
14351               argv[argc++] = copy_string (arg_start, p - arg_start);
14352             }
14353
14354           p = consume_improper_spaces (p, body);
14355
14356           /* Consume the comma, if present.  */
14357           if (*p == ',')
14358             {
14359               p++;
14360
14361               p = consume_improper_spaces (p, body);
14362             }
14363         }
14364
14365       if (*p == ')')
14366         {
14367           p++;
14368
14369           if (*p == ' ')
14370             /* Perfectly formed definition, no complaints.  */
14371             macro_define_function (file, line, name,
14372                                    argc, (const char **) argv,
14373                                    p + 1);
14374           else if (*p == '\0')
14375             {
14376               /* Complain, but do define it.  */
14377               dwarf2_macro_malformed_definition_complaint (body);
14378               macro_define_function (file, line, name,
14379                                      argc, (const char **) argv,
14380                                      p);
14381             }
14382           else
14383             /* Just complain.  */
14384             dwarf2_macro_malformed_definition_complaint (body);
14385         }
14386       else
14387         /* Just complain.  */
14388         dwarf2_macro_malformed_definition_complaint (body);
14389
14390       xfree (name);
14391       {
14392         int i;
14393
14394         for (i = 0; i < argc; i++)
14395           xfree (argv[i]);
14396       }
14397       xfree (argv);
14398     }
14399   else
14400     dwarf2_macro_malformed_definition_complaint (body);
14401 }
14402
14403
14404 static void
14405 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14406                      char *comp_dir, bfd *abfd,
14407                      struct dwarf2_cu *cu)
14408 {
14409   gdb_byte *mac_ptr, *mac_end;
14410   struct macro_source_file *current_file = 0;
14411   enum dwarf_macinfo_record_type macinfo_type;
14412   int at_commandline;
14413
14414   dwarf2_read_section (dwarf2_per_objfile->objfile,
14415                        &dwarf2_per_objfile->macinfo);
14416   if (dwarf2_per_objfile->macinfo.buffer == NULL)
14417     {
14418       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14419       return;
14420     }
14421
14422   /* First pass: Find the name of the base filename.
14423      This filename is needed in order to process all macros whose definition
14424      (or undefinition) comes from the command line.  These macros are defined
14425      before the first DW_MACINFO_start_file entry, and yet still need to be
14426      associated to the base file.
14427
14428      To determine the base file name, we scan the macro definitions until we
14429      reach the first DW_MACINFO_start_file entry.  We then initialize
14430      CURRENT_FILE accordingly so that any macro definition found before the
14431      first DW_MACINFO_start_file can still be associated to the base file.  */
14432
14433   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14434   mac_end = dwarf2_per_objfile->macinfo.buffer
14435     + dwarf2_per_objfile->macinfo.size;
14436
14437   do
14438     {
14439       /* Do we at least have room for a macinfo type byte?  */
14440       if (mac_ptr >= mac_end)
14441         {
14442           /* Complaint is printed during the second pass as GDB will probably
14443              stop the first pass earlier upon finding
14444              DW_MACINFO_start_file.  */
14445           break;
14446         }
14447
14448       macinfo_type = read_1_byte (abfd, mac_ptr);
14449       mac_ptr++;
14450
14451       switch (macinfo_type)
14452         {
14453           /* A zero macinfo type indicates the end of the macro
14454              information.  */
14455         case 0:
14456           break;
14457
14458         case DW_MACINFO_define:
14459         case DW_MACINFO_undef:
14460           /* Only skip the data by MAC_PTR.  */
14461           {
14462             unsigned int bytes_read;
14463
14464             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14465             mac_ptr += bytes_read;
14466             read_direct_string (abfd, mac_ptr, &bytes_read);
14467             mac_ptr += bytes_read;
14468           }
14469           break;
14470
14471         case DW_MACINFO_start_file:
14472           {
14473             unsigned int bytes_read;
14474             int line, file;
14475
14476             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14477             mac_ptr += bytes_read;
14478             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14479             mac_ptr += bytes_read;
14480
14481             current_file = macro_start_file (file, line, current_file,
14482                                              comp_dir, lh, cu->objfile);
14483           }
14484           break;
14485
14486         case DW_MACINFO_end_file:
14487           /* No data to skip by MAC_PTR.  */
14488           break;
14489
14490         case DW_MACINFO_vendor_ext:
14491           /* Only skip the data by MAC_PTR.  */
14492           {
14493             unsigned int bytes_read;
14494
14495             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14496             mac_ptr += bytes_read;
14497             read_direct_string (abfd, mac_ptr, &bytes_read);
14498             mac_ptr += bytes_read;
14499           }
14500           break;
14501
14502         default:
14503           break;
14504         }
14505     } while (macinfo_type != 0 && current_file == NULL);
14506
14507   /* Second pass: Process all entries.
14508
14509      Use the AT_COMMAND_LINE flag to determine whether we are still processing
14510      command-line macro definitions/undefinitions.  This flag is unset when we
14511      reach the first DW_MACINFO_start_file entry.  */
14512
14513   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14514
14515   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
14516      GDB is still reading the definitions from command line.  First
14517      DW_MACINFO_start_file will need to be ignored as it was already executed
14518      to create CURRENT_FILE for the main source holding also the command line
14519      definitions.  On first met DW_MACINFO_start_file this flag is reset to
14520      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
14521
14522   at_commandline = 1;
14523
14524   do
14525     {
14526       /* Do we at least have room for a macinfo type byte?  */
14527       if (mac_ptr >= mac_end)
14528         {
14529           dwarf2_macros_too_long_complaint ();
14530           break;
14531         }
14532
14533       macinfo_type = read_1_byte (abfd, mac_ptr);
14534       mac_ptr++;
14535
14536       switch (macinfo_type)
14537         {
14538           /* A zero macinfo type indicates the end of the macro
14539              information.  */
14540         case 0:
14541           break;
14542
14543         case DW_MACINFO_define:
14544         case DW_MACINFO_undef:
14545           {
14546             unsigned int bytes_read;
14547             int line;
14548             char *body;
14549
14550             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14551             mac_ptr += bytes_read;
14552             body = read_direct_string (abfd, mac_ptr, &bytes_read);
14553             mac_ptr += bytes_read;
14554
14555             if (! current_file)
14556               {
14557                 /* DWARF violation as no main source is present.  */
14558                 complaint (&symfile_complaints,
14559                            _("debug info with no main source gives macro %s "
14560                              "on line %d: %s"),
14561                            macinfo_type == DW_MACINFO_define ?
14562                              _("definition") :
14563                                macinfo_type == DW_MACINFO_undef ?
14564                                  _("undefinition") :
14565                                  _("something-or-other"), line, body);
14566                 break;
14567               }
14568             if ((line == 0 && !at_commandline)
14569                 || (line != 0 && at_commandline))
14570               complaint (&symfile_complaints,
14571                          _("debug info gives %s macro %s with %s line %d: %s"),
14572                          at_commandline ? _("command-line") : _("in-file"),
14573                          macinfo_type == DW_MACINFO_define ?
14574                            _("definition") :
14575                              macinfo_type == DW_MACINFO_undef ?
14576                                _("undefinition") :
14577                                _("something-or-other"),
14578                          line == 0 ? _("zero") : _("non-zero"), line, body);
14579
14580             if (macinfo_type == DW_MACINFO_define)
14581               parse_macro_definition (current_file, line, body);
14582             else if (macinfo_type == DW_MACINFO_undef)
14583               macro_undef (current_file, line, body);
14584           }
14585           break;
14586
14587         case DW_MACINFO_start_file:
14588           {
14589             unsigned int bytes_read;
14590             int line, file;
14591
14592             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14593             mac_ptr += bytes_read;
14594             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14595             mac_ptr += bytes_read;
14596
14597             if ((line == 0 && !at_commandline)
14598                 || (line != 0 && at_commandline))
14599               complaint (&symfile_complaints,
14600                          _("debug info gives source %d included "
14601                            "from %s at %s line %d"),
14602                          file, at_commandline ? _("command-line") : _("file"),
14603                          line == 0 ? _("zero") : _("non-zero"), line);
14604
14605             if (at_commandline)
14606               {
14607                 /* This DW_MACINFO_start_file was executed in the pass one.  */
14608                 at_commandline = 0;
14609               }
14610             else
14611               current_file = macro_start_file (file, line,
14612                                                current_file, comp_dir,
14613                                                lh, cu->objfile);
14614           }
14615           break;
14616
14617         case DW_MACINFO_end_file:
14618           if (! current_file)
14619             complaint (&symfile_complaints,
14620                        _("macro debug info has an unmatched "
14621                          "`close_file' directive"));
14622           else
14623             {
14624               current_file = current_file->included_by;
14625               if (! current_file)
14626                 {
14627                   enum dwarf_macinfo_record_type next_type;
14628
14629                   /* GCC circa March 2002 doesn't produce the zero
14630                      type byte marking the end of the compilation
14631                      unit.  Complain if it's not there, but exit no
14632                      matter what.  */
14633
14634                   /* Do we at least have room for a macinfo type byte?  */
14635                   if (mac_ptr >= mac_end)
14636                     {
14637                       dwarf2_macros_too_long_complaint ();
14638                       return;
14639                     }
14640
14641                   /* We don't increment mac_ptr here, so this is just
14642                      a look-ahead.  */
14643                   next_type = read_1_byte (abfd, mac_ptr);
14644                   if (next_type != 0)
14645                     complaint (&symfile_complaints,
14646                                _("no terminating 0-type entry for "
14647                                  "macros in `.debug_macinfo' section"));
14648
14649                   return;
14650                 }
14651             }
14652           break;
14653
14654         case DW_MACINFO_vendor_ext:
14655           {
14656             unsigned int bytes_read;
14657             int constant;
14658
14659             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14660             mac_ptr += bytes_read;
14661             read_direct_string (abfd, mac_ptr, &bytes_read);
14662             mac_ptr += bytes_read;
14663
14664             /* We don't recognize any vendor extensions.  */
14665           }
14666           break;
14667         }
14668     } while (macinfo_type != 0);
14669 }
14670
14671 /* Check if the attribute's form is a DW_FORM_block*
14672    if so return true else false.  */
14673 static int
14674 attr_form_is_block (struct attribute *attr)
14675 {
14676   return (attr == NULL ? 0 :
14677       attr->form == DW_FORM_block1
14678       || attr->form == DW_FORM_block2
14679       || attr->form == DW_FORM_block4
14680       || attr->form == DW_FORM_block
14681       || attr->form == DW_FORM_exprloc);
14682 }
14683
14684 /* Return non-zero if ATTR's value is a section offset --- classes
14685    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14686    You may use DW_UNSND (attr) to retrieve such offsets.
14687
14688    Section 7.5.4, "Attribute Encodings", explains that no attribute
14689    may have a value that belongs to more than one of these classes; it
14690    would be ambiguous if we did, because we use the same forms for all
14691    of them.  */
14692 static int
14693 attr_form_is_section_offset (struct attribute *attr)
14694 {
14695   return (attr->form == DW_FORM_data4
14696           || attr->form == DW_FORM_data8
14697           || attr->form == DW_FORM_sec_offset);
14698 }
14699
14700
14701 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14702    zero otherwise.  When this function returns true, you can apply
14703    dwarf2_get_attr_constant_value to it.
14704
14705    However, note that for some attributes you must check
14706    attr_form_is_section_offset before using this test.  DW_FORM_data4
14707    and DW_FORM_data8 are members of both the constant class, and of
14708    the classes that contain offsets into other debug sections
14709    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
14710    that, if an attribute's can be either a constant or one of the
14711    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14712    taken as section offsets, not constants.  */
14713 static int
14714 attr_form_is_constant (struct attribute *attr)
14715 {
14716   switch (attr->form)
14717     {
14718     case DW_FORM_sdata:
14719     case DW_FORM_udata:
14720     case DW_FORM_data1:
14721     case DW_FORM_data2:
14722     case DW_FORM_data4:
14723     case DW_FORM_data8:
14724       return 1;
14725     default:
14726       return 0;
14727     }
14728 }
14729
14730 /* A helper function that fills in a dwarf2_loclist_baton.  */
14731
14732 static void
14733 fill_in_loclist_baton (struct dwarf2_cu *cu,
14734                        struct dwarf2_loclist_baton *baton,
14735                        struct attribute *attr)
14736 {
14737   dwarf2_read_section (dwarf2_per_objfile->objfile,
14738                        &dwarf2_per_objfile->loc);
14739
14740   baton->per_cu = cu->per_cu;
14741   gdb_assert (baton->per_cu);
14742   /* We don't know how long the location list is, but make sure we
14743      don't run off the edge of the section.  */
14744   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14745   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14746   baton->base_address = cu->base_address;
14747 }
14748
14749 static void
14750 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14751                              struct dwarf2_cu *cu)
14752 {
14753   if (attr_form_is_section_offset (attr)
14754       /* ".debug_loc" may not exist at all, or the offset may be outside
14755          the section.  If so, fall through to the complaint in the
14756          other branch.  */
14757       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14758                                                 &dwarf2_per_objfile->loc))
14759     {
14760       struct dwarf2_loclist_baton *baton;
14761
14762       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14763                              sizeof (struct dwarf2_loclist_baton));
14764
14765       fill_in_loclist_baton (cu, baton, attr);
14766
14767       if (cu->base_known == 0)
14768         complaint (&symfile_complaints,
14769                    _("Location list used without "
14770                      "specifying the CU base address."));
14771
14772       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14773       SYMBOL_LOCATION_BATON (sym) = baton;
14774     }
14775   else
14776     {
14777       struct dwarf2_locexpr_baton *baton;
14778
14779       baton = obstack_alloc (&cu->objfile->objfile_obstack,
14780                              sizeof (struct dwarf2_locexpr_baton));
14781       baton->per_cu = cu->per_cu;
14782       gdb_assert (baton->per_cu);
14783
14784       if (attr_form_is_block (attr))
14785         {
14786           /* Note that we're just copying the block's data pointer
14787              here, not the actual data.  We're still pointing into the
14788              info_buffer for SYM's objfile; right now we never release
14789              that buffer, but when we do clean up properly this may
14790              need to change.  */
14791           baton->size = DW_BLOCK (attr)->size;
14792           baton->data = DW_BLOCK (attr)->data;
14793         }
14794       else
14795         {
14796           dwarf2_invalid_attrib_class_complaint ("location description",
14797                                                  SYMBOL_NATURAL_NAME (sym));
14798           baton->size = 0;
14799           baton->data = NULL;
14800         }
14801
14802       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14803       SYMBOL_LOCATION_BATON (sym) = baton;
14804     }
14805 }
14806
14807 /* Return the OBJFILE associated with the compilation unit CU.  If CU
14808    came from a separate debuginfo file, then the master objfile is
14809    returned.  */
14810
14811 struct objfile *
14812 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14813 {
14814   struct objfile *objfile = per_cu->objfile;
14815
14816   /* Return the master objfile, so that we can report and look up the
14817      correct file containing this variable.  */
14818   if (objfile->separate_debug_objfile_backlink)
14819     objfile = objfile->separate_debug_objfile_backlink;
14820
14821   return objfile;
14822 }
14823
14824 /* Return the address size given in the compilation unit header for CU.  */
14825
14826 CORE_ADDR
14827 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14828 {
14829   if (per_cu->cu)
14830     return per_cu->cu->header.addr_size;
14831   else
14832     {
14833       /* If the CU is not currently read in, we re-read its header.  */
14834       struct objfile *objfile = per_cu->objfile;
14835       struct dwarf2_per_objfile *per_objfile
14836         = objfile_data (objfile, dwarf2_objfile_data_key);
14837       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14838       struct comp_unit_head cu_header;
14839
14840       memset (&cu_header, 0, sizeof cu_header);
14841       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14842       return cu_header.addr_size;
14843     }
14844 }
14845
14846 /* Return the offset size given in the compilation unit header for CU.  */
14847
14848 int
14849 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14850 {
14851   if (per_cu->cu)
14852     return per_cu->cu->header.offset_size;
14853   else
14854     {
14855       /* If the CU is not currently read in, we re-read its header.  */
14856       struct objfile *objfile = per_cu->objfile;
14857       struct dwarf2_per_objfile *per_objfile
14858         = objfile_data (objfile, dwarf2_objfile_data_key);
14859       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14860       struct comp_unit_head cu_header;
14861
14862       memset (&cu_header, 0, sizeof cu_header);
14863       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14864       return cu_header.offset_size;
14865     }
14866 }
14867
14868 /* Return the text offset of the CU.  The returned offset comes from
14869    this CU's objfile.  If this objfile came from a separate debuginfo
14870    file, then the offset may be different from the corresponding
14871    offset in the parent objfile.  */
14872
14873 CORE_ADDR
14874 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14875 {
14876   struct objfile *objfile = per_cu->objfile;
14877
14878   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14879 }
14880
14881 /* Locate the .debug_info compilation unit from CU's objfile which contains
14882    the DIE at OFFSET.  Raises an error on failure.  */
14883
14884 static struct dwarf2_per_cu_data *
14885 dwarf2_find_containing_comp_unit (unsigned int offset,
14886                                   struct objfile *objfile)
14887 {
14888   struct dwarf2_per_cu_data *this_cu;
14889   int low, high;
14890
14891   low = 0;
14892   high = dwarf2_per_objfile->n_comp_units - 1;
14893   while (high > low)
14894     {
14895       int mid = low + (high - low) / 2;
14896
14897       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14898         high = mid;
14899       else
14900         low = mid + 1;
14901     }
14902   gdb_assert (low == high);
14903   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14904     {
14905       if (low == 0)
14906         error (_("Dwarf Error: could not find partial DIE containing "
14907                "offset 0x%lx [in module %s]"),
14908                (long) offset, bfd_get_filename (objfile->obfd));
14909
14910       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14911       return dwarf2_per_objfile->all_comp_units[low-1];
14912     }
14913   else
14914     {
14915       this_cu = dwarf2_per_objfile->all_comp_units[low];
14916       if (low == dwarf2_per_objfile->n_comp_units - 1
14917           && offset >= this_cu->offset + this_cu->length)
14918         error (_("invalid dwarf2 offset %u"), offset);
14919       gdb_assert (offset < this_cu->offset + this_cu->length);
14920       return this_cu;
14921     }
14922 }
14923
14924 /* Locate the compilation unit from OBJFILE which is located at exactly
14925    OFFSET.  Raises an error on failure.  */
14926
14927 static struct dwarf2_per_cu_data *
14928 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14929 {
14930   struct dwarf2_per_cu_data *this_cu;
14931
14932   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14933   if (this_cu->offset != offset)
14934     error (_("no compilation unit with offset %u."), offset);
14935   return this_cu;
14936 }
14937
14938 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
14939
14940 static void
14941 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14942 {
14943   memset (cu, 0, sizeof (*cu));
14944   cu->objfile = objfile;
14945   obstack_init (&cu->comp_unit_obstack);
14946 }
14947
14948 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
14949
14950 static void
14951 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14952 {
14953   struct attribute *attr;
14954
14955   /* Set the language we're debugging.  */
14956   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14957   if (attr)
14958     set_cu_language (DW_UNSND (attr), cu);
14959   else
14960     {
14961       cu->language = language_minimal;
14962       cu->language_defn = language_def (cu->language);
14963     }
14964 }
14965
14966 /* Release one cached compilation unit, CU.  We unlink it from the tree
14967    of compilation units, but we don't remove it from the read_in_chain;
14968    the caller is responsible for that.
14969    NOTE: DATA is a void * because this function is also used as a
14970    cleanup routine.  */
14971
14972 static void
14973 free_one_comp_unit (void *data)
14974 {
14975   struct dwarf2_cu *cu = data;
14976
14977   if (cu->per_cu != NULL)
14978     cu->per_cu->cu = NULL;
14979   cu->per_cu = NULL;
14980
14981   obstack_free (&cu->comp_unit_obstack, NULL);
14982
14983   xfree (cu);
14984 }
14985
14986 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14987    when we're finished with it.  We can't free the pointer itself, but be
14988    sure to unlink it from the cache.  Also release any associated storage
14989    and perform cache maintenance.
14990
14991    Only used during partial symbol parsing.  */
14992
14993 static void
14994 free_stack_comp_unit (void *data)
14995 {
14996   struct dwarf2_cu *cu = data;
14997
14998   obstack_free (&cu->comp_unit_obstack, NULL);
14999   cu->partial_dies = NULL;
15000
15001   if (cu->per_cu != NULL)
15002     {
15003       /* This compilation unit is on the stack in our caller, so we
15004          should not xfree it.  Just unlink it.  */
15005       cu->per_cu->cu = NULL;
15006       cu->per_cu = NULL;
15007
15008       /* If we had a per-cu pointer, then we may have other compilation
15009          units loaded, so age them now.  */
15010       age_cached_comp_units ();
15011     }
15012 }
15013
15014 /* Free all cached compilation units.  */
15015
15016 static void
15017 free_cached_comp_units (void *data)
15018 {
15019   struct dwarf2_per_cu_data *per_cu, **last_chain;
15020
15021   per_cu = dwarf2_per_objfile->read_in_chain;
15022   last_chain = &dwarf2_per_objfile->read_in_chain;
15023   while (per_cu != NULL)
15024     {
15025       struct dwarf2_per_cu_data *next_cu;
15026
15027       next_cu = per_cu->cu->read_in_chain;
15028
15029       free_one_comp_unit (per_cu->cu);
15030       *last_chain = next_cu;
15031
15032       per_cu = next_cu;
15033     }
15034 }
15035
15036 /* Increase the age counter on each cached compilation unit, and free
15037    any that are too old.  */
15038
15039 static void
15040 age_cached_comp_units (void)
15041 {
15042   struct dwarf2_per_cu_data *per_cu, **last_chain;
15043
15044   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15045   per_cu = dwarf2_per_objfile->read_in_chain;
15046   while (per_cu != NULL)
15047     {
15048       per_cu->cu->last_used ++;
15049       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15050         dwarf2_mark (per_cu->cu);
15051       per_cu = per_cu->cu->read_in_chain;
15052     }
15053
15054   per_cu = dwarf2_per_objfile->read_in_chain;
15055   last_chain = &dwarf2_per_objfile->read_in_chain;
15056   while (per_cu != NULL)
15057     {
15058       struct dwarf2_per_cu_data *next_cu;
15059
15060       next_cu = per_cu->cu->read_in_chain;
15061
15062       if (!per_cu->cu->mark)
15063         {
15064           free_one_comp_unit (per_cu->cu);
15065           *last_chain = next_cu;
15066         }
15067       else
15068         last_chain = &per_cu->cu->read_in_chain;
15069
15070       per_cu = next_cu;
15071     }
15072 }
15073
15074 /* Remove a single compilation unit from the cache.  */
15075
15076 static void
15077 free_one_cached_comp_unit (void *target_cu)
15078 {
15079   struct dwarf2_per_cu_data *per_cu, **last_chain;
15080
15081   per_cu = dwarf2_per_objfile->read_in_chain;
15082   last_chain = &dwarf2_per_objfile->read_in_chain;
15083   while (per_cu != NULL)
15084     {
15085       struct dwarf2_per_cu_data *next_cu;
15086
15087       next_cu = per_cu->cu->read_in_chain;
15088
15089       if (per_cu->cu == target_cu)
15090         {
15091           free_one_comp_unit (per_cu->cu);
15092           *last_chain = next_cu;
15093           break;
15094         }
15095       else
15096         last_chain = &per_cu->cu->read_in_chain;
15097
15098       per_cu = next_cu;
15099     }
15100 }
15101
15102 /* Release all extra memory associated with OBJFILE.  */
15103
15104 void
15105 dwarf2_free_objfile (struct objfile *objfile)
15106 {
15107   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15108
15109   if (dwarf2_per_objfile == NULL)
15110     return;
15111
15112   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
15113   free_cached_comp_units (NULL);
15114
15115   if (dwarf2_per_objfile->quick_file_names_table)
15116     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15117
15118   /* Everything else should be on the objfile obstack.  */
15119 }
15120
15121 /* A pair of DIE offset and GDB type pointer.  We store these
15122    in a hash table separate from the DIEs, and preserve them
15123    when the DIEs are flushed out of cache.  */
15124
15125 struct dwarf2_offset_and_type
15126 {
15127   unsigned int offset;
15128   struct type *type;
15129 };
15130
15131 /* Hash function for a dwarf2_offset_and_type.  */
15132
15133 static hashval_t
15134 offset_and_type_hash (const void *item)
15135 {
15136   const struct dwarf2_offset_and_type *ofs = item;
15137
15138   return ofs->offset;
15139 }
15140
15141 /* Equality function for a dwarf2_offset_and_type.  */
15142
15143 static int
15144 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15145 {
15146   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15147   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15148
15149   return ofs_lhs->offset == ofs_rhs->offset;
15150 }
15151
15152 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15153    table if necessary.  For convenience, return TYPE.
15154
15155    The DIEs reading must have careful ordering to:
15156     * Not cause infite loops trying to read in DIEs as a prerequisite for
15157       reading current DIE.
15158     * Not trying to dereference contents of still incompletely read in types
15159       while reading in other DIEs.
15160     * Enable referencing still incompletely read in types just by a pointer to
15161       the type without accessing its fields.
15162
15163    Therefore caller should follow these rules:
15164      * Try to fetch any prerequisite types we may need to build this DIE type
15165        before building the type and calling set_die_type.
15166      * After building type call set_die_type for current DIE as soon as
15167        possible before fetching more types to complete the current type.
15168      * Make the type as complete as possible before fetching more types.  */
15169
15170 static struct type *
15171 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15172 {
15173   struct dwarf2_offset_and_type **slot, ofs;
15174   struct objfile *objfile = cu->objfile;
15175   htab_t *type_hash_ptr;
15176
15177   /* For Ada types, make sure that the gnat-specific data is always
15178      initialized (if not already set).  There are a few types where
15179      we should not be doing so, because the type-specific area is
15180      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15181      where the type-specific area is used to store the floatformat).
15182      But this is not a problem, because the gnat-specific information
15183      is actually not needed for these types.  */
15184   if (need_gnat_info (cu)
15185       && TYPE_CODE (type) != TYPE_CODE_FUNC
15186       && TYPE_CODE (type) != TYPE_CODE_FLT
15187       && !HAVE_GNAT_AUX_INFO (type))
15188     INIT_GNAT_SPECIFIC (type);
15189
15190   if (cu->per_cu->from_debug_types)
15191     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15192   else
15193     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15194
15195   if (*type_hash_ptr == NULL)
15196     {
15197       *type_hash_ptr
15198         = htab_create_alloc_ex (127,
15199                                 offset_and_type_hash,
15200                                 offset_and_type_eq,
15201                                 NULL,
15202                                 &objfile->objfile_obstack,
15203                                 hashtab_obstack_allocate,
15204                                 dummy_obstack_deallocate);
15205     }
15206
15207   ofs.offset = die->offset;
15208   ofs.type = type;
15209   slot = (struct dwarf2_offset_and_type **)
15210     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15211   if (*slot)
15212     complaint (&symfile_complaints,
15213                _("A problem internal to GDB: DIE 0x%x has type already set"),
15214                die->offset);
15215   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15216   **slot = ofs;
15217   return type;
15218 }
15219
15220 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15221    table, or return NULL if the die does not have a saved type.  */
15222
15223 static struct type *
15224 get_die_type_at_offset (unsigned int offset,
15225                         struct dwarf2_per_cu_data *per_cu)
15226 {
15227   struct dwarf2_offset_and_type *slot, ofs;
15228   htab_t type_hash;
15229
15230   if (per_cu->from_debug_types)
15231     type_hash = dwarf2_per_objfile->debug_types_type_hash;
15232   else
15233     type_hash = dwarf2_per_objfile->debug_info_type_hash;
15234   if (type_hash == NULL)
15235     return NULL;
15236
15237   ofs.offset = offset;
15238   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15239   if (slot)
15240     return slot->type;
15241   else
15242     return NULL;
15243 }
15244
15245 /* Look up the type for DIE in the appropriate type_hash table,
15246    or return NULL if DIE does not have a saved type.  */
15247
15248 static struct type *
15249 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15250 {
15251   return get_die_type_at_offset (die->offset, cu->per_cu);
15252 }
15253
15254 /* Add a dependence relationship from CU to REF_PER_CU.  */
15255
15256 static void
15257 dwarf2_add_dependence (struct dwarf2_cu *cu,
15258                        struct dwarf2_per_cu_data *ref_per_cu)
15259 {
15260   void **slot;
15261
15262   if (cu->dependencies == NULL)
15263     cu->dependencies
15264       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15265                               NULL, &cu->comp_unit_obstack,
15266                               hashtab_obstack_allocate,
15267                               dummy_obstack_deallocate);
15268
15269   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15270   if (*slot == NULL)
15271     *slot = ref_per_cu;
15272 }
15273
15274 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15275    Set the mark field in every compilation unit in the
15276    cache that we must keep because we are keeping CU.  */
15277
15278 static int
15279 dwarf2_mark_helper (void **slot, void *data)
15280 {
15281   struct dwarf2_per_cu_data *per_cu;
15282
15283   per_cu = (struct dwarf2_per_cu_data *) *slot;
15284   if (per_cu->cu->mark)
15285     return 1;
15286   per_cu->cu->mark = 1;
15287
15288   if (per_cu->cu->dependencies != NULL)
15289     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15290
15291   return 1;
15292 }
15293
15294 /* Set the mark field in CU and in every other compilation unit in the
15295    cache that we must keep because we are keeping CU.  */
15296
15297 static void
15298 dwarf2_mark (struct dwarf2_cu *cu)
15299 {
15300   if (cu->mark)
15301     return;
15302   cu->mark = 1;
15303   if (cu->dependencies != NULL)
15304     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15305 }
15306
15307 static void
15308 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15309 {
15310   while (per_cu)
15311     {
15312       per_cu->cu->mark = 0;
15313       per_cu = per_cu->cu->read_in_chain;
15314     }
15315 }
15316
15317 /* Trivial hash function for partial_die_info: the hash value of a DIE
15318    is its offset in .debug_info for this objfile.  */
15319
15320 static hashval_t
15321 partial_die_hash (const void *item)
15322 {
15323   const struct partial_die_info *part_die = item;
15324
15325   return part_die->offset;
15326 }
15327
15328 /* Trivial comparison function for partial_die_info structures: two DIEs
15329    are equal if they have the same offset.  */
15330
15331 static int
15332 partial_die_eq (const void *item_lhs, const void *item_rhs)
15333 {
15334   const struct partial_die_info *part_die_lhs = item_lhs;
15335   const struct partial_die_info *part_die_rhs = item_rhs;
15336
15337   return part_die_lhs->offset == part_die_rhs->offset;
15338 }
15339
15340 static struct cmd_list_element *set_dwarf2_cmdlist;
15341 static struct cmd_list_element *show_dwarf2_cmdlist;
15342
15343 static void
15344 set_dwarf2_cmd (char *args, int from_tty)
15345 {
15346   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15347 }
15348
15349 static void
15350 show_dwarf2_cmd (char *args, int from_tty)
15351 {
15352   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15353 }
15354
15355 /* If section described by INFO was mmapped, munmap it now.  */
15356
15357 static void
15358 munmap_section_buffer (struct dwarf2_section_info *info)
15359 {
15360   if (info->was_mmapped)
15361     {
15362 #ifdef HAVE_MMAP
15363       intptr_t begin = (intptr_t) info->buffer;
15364       intptr_t map_begin = begin & ~(pagesize - 1);
15365       size_t map_length = info->size + begin - map_begin;
15366
15367       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15368 #else
15369       /* Without HAVE_MMAP, we should never be here to begin with.  */
15370       gdb_assert_not_reached ("no mmap support");
15371 #endif
15372     }
15373 }
15374
15375 /* munmap debug sections for OBJFILE, if necessary.  */
15376
15377 static void
15378 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15379 {
15380   struct dwarf2_per_objfile *data = d;
15381
15382   /* This is sorted according to the order they're defined in to make it easier
15383      to keep in sync.  */
15384   munmap_section_buffer (&data->info);
15385   munmap_section_buffer (&data->abbrev);
15386   munmap_section_buffer (&data->line);
15387   munmap_section_buffer (&data->loc);
15388   munmap_section_buffer (&data->macinfo);
15389   munmap_section_buffer (&data->str);
15390   munmap_section_buffer (&data->ranges);
15391   munmap_section_buffer (&data->types);
15392   munmap_section_buffer (&data->frame);
15393   munmap_section_buffer (&data->eh_frame);
15394   munmap_section_buffer (&data->gdb_index);
15395 }
15396
15397 \f
15398 /* The "save gdb-index" command.  */
15399
15400 /* The contents of the hash table we create when building the string
15401    table.  */
15402 struct strtab_entry
15403 {
15404   offset_type offset;
15405   const char *str;
15406 };
15407
15408 /* Hash function for a strtab_entry.
15409
15410    Function is used only during write_hash_table so no index format backward
15411    compatibility is needed.  */
15412
15413 static hashval_t
15414 hash_strtab_entry (const void *e)
15415 {
15416   const struct strtab_entry *entry = e;
15417   return mapped_index_string_hash (INT_MAX, entry->str);
15418 }
15419
15420 /* Equality function for a strtab_entry.  */
15421
15422 static int
15423 eq_strtab_entry (const void *a, const void *b)
15424 {
15425   const struct strtab_entry *ea = a;
15426   const struct strtab_entry *eb = b;
15427   return !strcmp (ea->str, eb->str);
15428 }
15429
15430 /* Create a strtab_entry hash table.  */
15431
15432 static htab_t
15433 create_strtab (void)
15434 {
15435   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15436                             xfree, xcalloc, xfree);
15437 }
15438
15439 /* Add a string to the constant pool.  Return the string's offset in
15440    host order.  */
15441
15442 static offset_type
15443 add_string (htab_t table, struct obstack *cpool, const char *str)
15444 {
15445   void **slot;
15446   struct strtab_entry entry;
15447   struct strtab_entry *result;
15448
15449   entry.str = str;
15450   slot = htab_find_slot (table, &entry, INSERT);
15451   if (*slot)
15452     result = *slot;
15453   else
15454     {
15455       result = XNEW (struct strtab_entry);
15456       result->offset = obstack_object_size (cpool);
15457       result->str = str;
15458       obstack_grow_str0 (cpool, str);
15459       *slot = result;
15460     }
15461   return result->offset;
15462 }
15463
15464 /* An entry in the symbol table.  */
15465 struct symtab_index_entry
15466 {
15467   /* The name of the symbol.  */
15468   const char *name;
15469   /* The offset of the name in the constant pool.  */
15470   offset_type index_offset;
15471   /* A sorted vector of the indices of all the CUs that hold an object
15472      of this name.  */
15473   VEC (offset_type) *cu_indices;
15474 };
15475
15476 /* The symbol table.  This is a power-of-2-sized hash table.  */
15477 struct mapped_symtab
15478 {
15479   offset_type n_elements;
15480   offset_type size;
15481   struct symtab_index_entry **data;
15482 };
15483
15484 /* Hash function for a symtab_index_entry.  */
15485
15486 static hashval_t
15487 hash_symtab_entry (const void *e)
15488 {
15489   const struct symtab_index_entry *entry = e;
15490   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15491                          sizeof (offset_type) * VEC_length (offset_type,
15492                                                             entry->cu_indices),
15493                          0);
15494 }
15495
15496 /* Equality function for a symtab_index_entry.  */
15497
15498 static int
15499 eq_symtab_entry (const void *a, const void *b)
15500 {
15501   const struct symtab_index_entry *ea = a;
15502   const struct symtab_index_entry *eb = b;
15503   int len = VEC_length (offset_type, ea->cu_indices);
15504   if (len != VEC_length (offset_type, eb->cu_indices))
15505     return 0;
15506   return !memcmp (VEC_address (offset_type, ea->cu_indices),
15507                   VEC_address (offset_type, eb->cu_indices),
15508                   sizeof (offset_type) * len);
15509 }
15510
15511 /* Destroy a symtab_index_entry.  */
15512
15513 static void
15514 delete_symtab_entry (void *p)
15515 {
15516   struct symtab_index_entry *entry = p;
15517   VEC_free (offset_type, entry->cu_indices);
15518   xfree (entry);
15519 }
15520
15521 /* Create a hash table holding symtab_index_entry objects.  */
15522
15523 static htab_t
15524 create_symbol_hash_table (void)
15525 {
15526   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15527                             delete_symtab_entry, xcalloc, xfree);
15528 }
15529
15530 /* Create a new mapped symtab object.  */
15531
15532 static struct mapped_symtab *
15533 create_mapped_symtab (void)
15534 {
15535   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15536   symtab->n_elements = 0;
15537   symtab->size = 1024;
15538   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15539   return symtab;
15540 }
15541
15542 /* Destroy a mapped_symtab.  */
15543
15544 static void
15545 cleanup_mapped_symtab (void *p)
15546 {
15547   struct mapped_symtab *symtab = p;
15548   /* The contents of the array are freed when the other hash table is
15549      destroyed.  */
15550   xfree (symtab->data);
15551   xfree (symtab);
15552 }
15553
15554 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
15555    the slot.
15556    
15557    Function is used only during write_hash_table so no index format backward
15558    compatibility is needed.  */
15559
15560 static struct symtab_index_entry **
15561 find_slot (struct mapped_symtab *symtab, const char *name)
15562 {
15563   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15564
15565   index = hash & (symtab->size - 1);
15566   step = ((hash * 17) & (symtab->size - 1)) | 1;
15567
15568   for (;;)
15569     {
15570       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15571         return &symtab->data[index];
15572       index = (index + step) & (symtab->size - 1);
15573     }
15574 }
15575
15576 /* Expand SYMTAB's hash table.  */
15577
15578 static void
15579 hash_expand (struct mapped_symtab *symtab)
15580 {
15581   offset_type old_size = symtab->size;
15582   offset_type i;
15583   struct symtab_index_entry **old_entries = symtab->data;
15584
15585   symtab->size *= 2;
15586   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15587
15588   for (i = 0; i < old_size; ++i)
15589     {
15590       if (old_entries[i])
15591         {
15592           struct symtab_index_entry **slot = find_slot (symtab,
15593                                                         old_entries[i]->name);
15594           *slot = old_entries[i];
15595         }
15596     }
15597
15598   xfree (old_entries);
15599 }
15600
15601 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
15602    is the index of the CU in which the symbol appears.  */
15603
15604 static void
15605 add_index_entry (struct mapped_symtab *symtab, const char *name,
15606                  offset_type cu_index)
15607 {
15608   struct symtab_index_entry **slot;
15609
15610   ++symtab->n_elements;
15611   if (4 * symtab->n_elements / 3 >= symtab->size)
15612     hash_expand (symtab);
15613
15614   slot = find_slot (symtab, name);
15615   if (!*slot)
15616     {
15617       *slot = XNEW (struct symtab_index_entry);
15618       (*slot)->name = name;
15619       (*slot)->cu_indices = NULL;
15620     }
15621   /* Don't push an index twice.  Due to how we add entries we only
15622      have to check the last one.  */ 
15623   if (VEC_empty (offset_type, (*slot)->cu_indices)
15624       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15625     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15626 }
15627
15628 /* Add a vector of indices to the constant pool.  */
15629
15630 static offset_type
15631 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15632                       struct symtab_index_entry *entry)
15633 {
15634   void **slot;
15635
15636   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15637   if (!*slot)
15638     {
15639       offset_type len = VEC_length (offset_type, entry->cu_indices);
15640       offset_type val = MAYBE_SWAP (len);
15641       offset_type iter;
15642       int i;
15643
15644       *slot = entry;
15645       entry->index_offset = obstack_object_size (cpool);
15646
15647       obstack_grow (cpool, &val, sizeof (val));
15648       for (i = 0;
15649            VEC_iterate (offset_type, entry->cu_indices, i, iter);
15650            ++i)
15651         {
15652           val = MAYBE_SWAP (iter);
15653           obstack_grow (cpool, &val, sizeof (val));
15654         }
15655     }
15656   else
15657     {
15658       struct symtab_index_entry *old_entry = *slot;
15659       entry->index_offset = old_entry->index_offset;
15660       entry = old_entry;
15661     }
15662   return entry->index_offset;
15663 }
15664
15665 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15666    constant pool entries going into the obstack CPOOL.  */
15667
15668 static void
15669 write_hash_table (struct mapped_symtab *symtab,
15670                   struct obstack *output, struct obstack *cpool)
15671 {
15672   offset_type i;
15673   htab_t symbol_hash_table;
15674   htab_t str_table;
15675
15676   symbol_hash_table = create_symbol_hash_table ();
15677   str_table = create_strtab ();
15678
15679   /* We add all the index vectors to the constant pool first, to
15680      ensure alignment is ok.  */
15681   for (i = 0; i < symtab->size; ++i)
15682     {
15683       if (symtab->data[i])
15684         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15685     }
15686
15687   /* Now write out the hash table.  */
15688   for (i = 0; i < symtab->size; ++i)
15689     {
15690       offset_type str_off, vec_off;
15691
15692       if (symtab->data[i])
15693         {
15694           str_off = add_string (str_table, cpool, symtab->data[i]->name);
15695           vec_off = symtab->data[i]->index_offset;
15696         }
15697       else
15698         {
15699           /* While 0 is a valid constant pool index, it is not valid
15700              to have 0 for both offsets.  */
15701           str_off = 0;
15702           vec_off = 0;
15703         }
15704
15705       str_off = MAYBE_SWAP (str_off);
15706       vec_off = MAYBE_SWAP (vec_off);
15707
15708       obstack_grow (output, &str_off, sizeof (str_off));
15709       obstack_grow (output, &vec_off, sizeof (vec_off));
15710     }
15711
15712   htab_delete (str_table);
15713   htab_delete (symbol_hash_table);
15714 }
15715
15716 /* Struct to map psymtab to CU index in the index file.  */
15717 struct psymtab_cu_index_map
15718 {
15719   struct partial_symtab *psymtab;
15720   unsigned int cu_index;
15721 };
15722
15723 static hashval_t
15724 hash_psymtab_cu_index (const void *item)
15725 {
15726   const struct psymtab_cu_index_map *map = item;
15727
15728   return htab_hash_pointer (map->psymtab);
15729 }
15730
15731 static int
15732 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15733 {
15734   const struct psymtab_cu_index_map *lhs = item_lhs;
15735   const struct psymtab_cu_index_map *rhs = item_rhs;
15736
15737   return lhs->psymtab == rhs->psymtab;
15738 }
15739
15740 /* Helper struct for building the address table.  */
15741 struct addrmap_index_data
15742 {
15743   struct objfile *objfile;
15744   struct obstack *addr_obstack;
15745   htab_t cu_index_htab;
15746
15747   /* Non-zero if the previous_* fields are valid.
15748      We can't write an entry until we see the next entry (since it is only then
15749      that we know the end of the entry).  */
15750   int previous_valid;
15751   /* Index of the CU in the table of all CUs in the index file.  */
15752   unsigned int previous_cu_index;
15753   /* Start address of the CU.  */
15754   CORE_ADDR previous_cu_start;
15755 };
15756
15757 /* Write an address entry to OBSTACK.  */
15758
15759 static void
15760 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15761                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15762 {
15763   offset_type cu_index_to_write;
15764   char addr[8];
15765   CORE_ADDR baseaddr;
15766
15767   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15768
15769   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15770   obstack_grow (obstack, addr, 8);
15771   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15772   obstack_grow (obstack, addr, 8);
15773   cu_index_to_write = MAYBE_SWAP (cu_index);
15774   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15775 }
15776
15777 /* Worker function for traversing an addrmap to build the address table.  */
15778
15779 static int
15780 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15781 {
15782   struct addrmap_index_data *data = datap;
15783   struct partial_symtab *pst = obj;
15784   offset_type cu_index;
15785   void **slot;
15786
15787   if (data->previous_valid)
15788     add_address_entry (data->objfile, data->addr_obstack,
15789                        data->previous_cu_start, start_addr,
15790                        data->previous_cu_index);
15791
15792   data->previous_cu_start = start_addr;
15793   if (pst != NULL)
15794     {
15795       struct psymtab_cu_index_map find_map, *map;
15796       find_map.psymtab = pst;
15797       map = htab_find (data->cu_index_htab, &find_map);
15798       gdb_assert (map != NULL);
15799       data->previous_cu_index = map->cu_index;
15800       data->previous_valid = 1;
15801     }
15802   else
15803       data->previous_valid = 0;
15804
15805   return 0;
15806 }
15807
15808 /* Write OBJFILE's address map to OBSTACK.
15809    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15810    in the index file.  */
15811
15812 static void
15813 write_address_map (struct objfile *objfile, struct obstack *obstack,
15814                    htab_t cu_index_htab)
15815 {
15816   struct addrmap_index_data addrmap_index_data;
15817
15818   /* When writing the address table, we have to cope with the fact that
15819      the addrmap iterator only provides the start of a region; we have to
15820      wait until the next invocation to get the start of the next region.  */
15821
15822   addrmap_index_data.objfile = objfile;
15823   addrmap_index_data.addr_obstack = obstack;
15824   addrmap_index_data.cu_index_htab = cu_index_htab;
15825   addrmap_index_data.previous_valid = 0;
15826
15827   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15828                    &addrmap_index_data);
15829
15830   /* It's highly unlikely the last entry (end address = 0xff...ff)
15831      is valid, but we should still handle it.
15832      The end address is recorded as the start of the next region, but that
15833      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
15834      anyway.  */
15835   if (addrmap_index_data.previous_valid)
15836     add_address_entry (objfile, obstack,
15837                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15838                        addrmap_index_data.previous_cu_index);
15839 }
15840
15841 /* Add a list of partial symbols to SYMTAB.  */
15842
15843 static void
15844 write_psymbols (struct mapped_symtab *symtab,
15845                 htab_t psyms_seen,
15846                 struct partial_symbol **psymp,
15847                 int count,
15848                 offset_type cu_index,
15849                 int is_static)
15850 {
15851   for (; count-- > 0; ++psymp)
15852     {
15853       void **slot, *lookup;
15854
15855       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15856         error (_("Ada is not currently supported by the index"));
15857
15858       /* We only want to add a given psymbol once.  However, we also
15859          want to account for whether it is global or static.  So, we
15860          may add it twice, using slightly different values.  */
15861       if (is_static)
15862         {
15863           uintptr_t val = 1 | (uintptr_t) *psymp;
15864
15865           lookup = (void *) val;
15866         }
15867       else
15868         lookup = *psymp;
15869
15870       /* Only add a given psymbol once.  */
15871       slot = htab_find_slot (psyms_seen, lookup, INSERT);
15872       if (!*slot)
15873         {
15874           *slot = lookup;
15875           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15876         }
15877     }
15878 }
15879
15880 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
15881    exception if there is an error.  */
15882
15883 static void
15884 write_obstack (FILE *file, struct obstack *obstack)
15885 {
15886   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15887               file)
15888       != obstack_object_size (obstack))
15889     error (_("couldn't data write to file"));
15890 }
15891
15892 /* Unlink a file if the argument is not NULL.  */
15893
15894 static void
15895 unlink_if_set (void *p)
15896 {
15897   char **filename = p;
15898   if (*filename)
15899     unlink (*filename);
15900 }
15901
15902 /* A helper struct used when iterating over debug_types.  */
15903 struct signatured_type_index_data
15904 {
15905   struct objfile *objfile;
15906   struct mapped_symtab *symtab;
15907   struct obstack *types_list;
15908   htab_t psyms_seen;
15909   int cu_index;
15910 };
15911
15912 /* A helper function that writes a single signatured_type to an
15913    obstack.  */
15914
15915 static int
15916 write_one_signatured_type (void **slot, void *d)
15917 {
15918   struct signatured_type_index_data *info = d;
15919   struct signatured_type *entry = (struct signatured_type *) *slot;
15920   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15921   struct partial_symtab *psymtab = per_cu->v.psymtab;
15922   gdb_byte val[8];
15923
15924   write_psymbols (info->symtab,
15925                   info->psyms_seen,
15926                   info->objfile->global_psymbols.list
15927                   + psymtab->globals_offset,
15928                   psymtab->n_global_syms, info->cu_index,
15929                   0);
15930   write_psymbols (info->symtab,
15931                   info->psyms_seen,
15932                   info->objfile->static_psymbols.list
15933                   + psymtab->statics_offset,
15934                   psymtab->n_static_syms, info->cu_index,
15935                   1);
15936
15937   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
15938   obstack_grow (info->types_list, val, 8);
15939   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15940   obstack_grow (info->types_list, val, 8);
15941   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15942   obstack_grow (info->types_list, val, 8);
15943
15944   ++info->cu_index;
15945
15946   return 1;
15947 }
15948
15949 /* A cleanup function for an htab_t.  */
15950
15951 static void
15952 cleanup_htab (void *arg)
15953 {
15954   htab_delete (arg);
15955 }
15956
15957 /* Create an index file for OBJFILE in the directory DIR.  */
15958
15959 static void
15960 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15961 {
15962   struct cleanup *cleanup;
15963   char *filename, *cleanup_filename;
15964   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15965   struct obstack cu_list, types_cu_list;
15966   int i;
15967   FILE *out_file;
15968   struct mapped_symtab *symtab;
15969   offset_type val, size_of_contents, total_len;
15970   struct stat st;
15971   char buf[8];
15972   htab_t psyms_seen;
15973   htab_t cu_index_htab;
15974   struct psymtab_cu_index_map *psymtab_cu_index_map;
15975
15976   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15977     return;
15978
15979   if (dwarf2_per_objfile->using_index)
15980     error (_("Cannot use an index to create the index"));
15981
15982   if (stat (objfile->name, &st) < 0)
15983     perror_with_name (objfile->name);
15984
15985   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15986                      INDEX_SUFFIX, (char *) NULL);
15987   cleanup = make_cleanup (xfree, filename);
15988
15989   out_file = fopen (filename, "wb");
15990   if (!out_file)
15991     error (_("Can't open `%s' for writing"), filename);
15992
15993   cleanup_filename = filename;
15994   make_cleanup (unlink_if_set, &cleanup_filename);
15995
15996   symtab = create_mapped_symtab ();
15997   make_cleanup (cleanup_mapped_symtab, symtab);
15998
15999   obstack_init (&addr_obstack);
16000   make_cleanup_obstack_free (&addr_obstack);
16001
16002   obstack_init (&cu_list);
16003   make_cleanup_obstack_free (&cu_list);
16004
16005   obstack_init (&types_cu_list);
16006   make_cleanup_obstack_free (&types_cu_list);
16007
16008   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16009                                   NULL, xcalloc, xfree);
16010   make_cleanup (cleanup_htab, psyms_seen);
16011
16012   /* While we're scanning CU's create a table that maps a psymtab pointer
16013      (which is what addrmap records) to its index (which is what is recorded
16014      in the index file).  This will later be needed to write the address
16015      table.  */
16016   cu_index_htab = htab_create_alloc (100,
16017                                      hash_psymtab_cu_index,
16018                                      eq_psymtab_cu_index,
16019                                      NULL, xcalloc, xfree);
16020   make_cleanup (cleanup_htab, cu_index_htab);
16021   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16022     xmalloc (sizeof (struct psymtab_cu_index_map)
16023              * dwarf2_per_objfile->n_comp_units);
16024   make_cleanup (xfree, psymtab_cu_index_map);
16025
16026   /* The CU list is already sorted, so we don't need to do additional
16027      work here.  Also, the debug_types entries do not appear in
16028      all_comp_units, but only in their own hash table.  */
16029   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16030     {
16031       struct dwarf2_per_cu_data *per_cu
16032         = dwarf2_per_objfile->all_comp_units[i];
16033       struct partial_symtab *psymtab = per_cu->v.psymtab;
16034       gdb_byte val[8];
16035       struct psymtab_cu_index_map *map;
16036       void **slot;
16037
16038       write_psymbols (symtab,
16039                       psyms_seen,
16040                       objfile->global_psymbols.list + psymtab->globals_offset,
16041                       psymtab->n_global_syms, i,
16042                       0);
16043       write_psymbols (symtab,
16044                       psyms_seen,
16045                       objfile->static_psymbols.list + psymtab->statics_offset,
16046                       psymtab->n_static_syms, i,
16047                       1);
16048
16049       map = &psymtab_cu_index_map[i];
16050       map->psymtab = psymtab;
16051       map->cu_index = i;
16052       slot = htab_find_slot (cu_index_htab, map, INSERT);
16053       gdb_assert (slot != NULL);
16054       gdb_assert (*slot == NULL);
16055       *slot = map;
16056
16057       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16058       obstack_grow (&cu_list, val, 8);
16059       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16060       obstack_grow (&cu_list, val, 8);
16061     }
16062
16063   /* Dump the address map.  */
16064   write_address_map (objfile, &addr_obstack, cu_index_htab);
16065
16066   /* Write out the .debug_type entries, if any.  */
16067   if (dwarf2_per_objfile->signatured_types)
16068     {
16069       struct signatured_type_index_data sig_data;
16070
16071       sig_data.objfile = objfile;
16072       sig_data.symtab = symtab;
16073       sig_data.types_list = &types_cu_list;
16074       sig_data.psyms_seen = psyms_seen;
16075       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16076       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16077                               write_one_signatured_type, &sig_data);
16078     }
16079
16080   obstack_init (&constant_pool);
16081   make_cleanup_obstack_free (&constant_pool);
16082   obstack_init (&symtab_obstack);
16083   make_cleanup_obstack_free (&symtab_obstack);
16084   write_hash_table (symtab, &symtab_obstack, &constant_pool);
16085
16086   obstack_init (&contents);
16087   make_cleanup_obstack_free (&contents);
16088   size_of_contents = 6 * sizeof (offset_type);
16089   total_len = size_of_contents;
16090
16091   /* The version number.  */
16092   val = MAYBE_SWAP (5);
16093   obstack_grow (&contents, &val, sizeof (val));
16094
16095   /* The offset of the CU list from the start of the file.  */
16096   val = MAYBE_SWAP (total_len);
16097   obstack_grow (&contents, &val, sizeof (val));
16098   total_len += obstack_object_size (&cu_list);
16099
16100   /* The offset of the types CU list from the start of the file.  */
16101   val = MAYBE_SWAP (total_len);
16102   obstack_grow (&contents, &val, sizeof (val));
16103   total_len += obstack_object_size (&types_cu_list);
16104
16105   /* The offset of the address table from the start of the file.  */
16106   val = MAYBE_SWAP (total_len);
16107   obstack_grow (&contents, &val, sizeof (val));
16108   total_len += obstack_object_size (&addr_obstack);
16109
16110   /* The offset of the symbol table from the start of the file.  */
16111   val = MAYBE_SWAP (total_len);
16112   obstack_grow (&contents, &val, sizeof (val));
16113   total_len += obstack_object_size (&symtab_obstack);
16114
16115   /* The offset of the constant pool from the start of the file.  */
16116   val = MAYBE_SWAP (total_len);
16117   obstack_grow (&contents, &val, sizeof (val));
16118   total_len += obstack_object_size (&constant_pool);
16119
16120   gdb_assert (obstack_object_size (&contents) == size_of_contents);
16121
16122   write_obstack (out_file, &contents);
16123   write_obstack (out_file, &cu_list);
16124   write_obstack (out_file, &types_cu_list);
16125   write_obstack (out_file, &addr_obstack);
16126   write_obstack (out_file, &symtab_obstack);
16127   write_obstack (out_file, &constant_pool);
16128
16129   fclose (out_file);
16130
16131   /* We want to keep the file, so we set cleanup_filename to NULL
16132      here.  See unlink_if_set.  */
16133   cleanup_filename = NULL;
16134
16135   do_cleanups (cleanup);
16136 }
16137
16138 /* Implementation of the `save gdb-index' command.
16139    
16140    Note that the file format used by this command is documented in the
16141    GDB manual.  Any changes here must be documented there.  */
16142
16143 static void
16144 save_gdb_index_command (char *arg, int from_tty)
16145 {
16146   struct objfile *objfile;
16147
16148   if (!arg || !*arg)
16149     error (_("usage: save gdb-index DIRECTORY"));
16150
16151   ALL_OBJFILES (objfile)
16152   {
16153     struct stat st;
16154
16155     /* If the objfile does not correspond to an actual file, skip it.  */
16156     if (stat (objfile->name, &st) < 0)
16157       continue;
16158
16159     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16160     if (dwarf2_per_objfile)
16161       {
16162         volatile struct gdb_exception except;
16163
16164         TRY_CATCH (except, RETURN_MASK_ERROR)
16165           {
16166             write_psymtabs_to_index (objfile, arg);
16167           }
16168         if (except.reason < 0)
16169           exception_fprintf (gdb_stderr, except,
16170                              _("Error while writing index for `%s': "),
16171                              objfile->name);
16172       }
16173   }
16174 }
16175
16176 \f
16177
16178 int dwarf2_always_disassemble;
16179
16180 static void
16181 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16182                                 struct cmd_list_element *c, const char *value)
16183 {
16184   fprintf_filtered (file,
16185                     _("Whether to always disassemble "
16186                       "DWARF expressions is %s.\n"),
16187                     value);
16188 }
16189
16190 void _initialize_dwarf2_read (void);
16191
16192 void
16193 _initialize_dwarf2_read (void)
16194 {
16195   struct cmd_list_element *c;
16196
16197   dwarf2_objfile_data_key
16198     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16199
16200   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16201 Set DWARF 2 specific variables.\n\
16202 Configure DWARF 2 variables such as the cache size"),
16203                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16204                   0/*allow-unknown*/, &maintenance_set_cmdlist);
16205
16206   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16207 Show DWARF 2 specific variables\n\
16208 Show DWARF 2 variables such as the cache size"),
16209                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16210                   0/*allow-unknown*/, &maintenance_show_cmdlist);
16211
16212   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16213                             &dwarf2_max_cache_age, _("\
16214 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16215 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16216 A higher limit means that cached compilation units will be stored\n\
16217 in memory longer, and more total memory will be used.  Zero disables\n\
16218 caching, which can slow down startup."),
16219                             NULL,
16220                             show_dwarf2_max_cache_age,
16221                             &set_dwarf2_cmdlist,
16222                             &show_dwarf2_cmdlist);
16223
16224   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16225                            &dwarf2_always_disassemble, _("\
16226 Set whether `info address' always disassembles DWARF expressions."), _("\
16227 Show whether `info address' always disassembles DWARF expressions."), _("\
16228 When enabled, DWARF expressions are always printed in an assembly-like\n\
16229 syntax.  When disabled, expressions will be printed in a more\n\
16230 conversational style, when possible."),
16231                            NULL,
16232                            show_dwarf2_always_disassemble,
16233                            &set_dwarf2_cmdlist,
16234                            &show_dwarf2_cmdlist);
16235
16236   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16237 Set debugging of the dwarf2 DIE reader."), _("\
16238 Show debugging of the dwarf2 DIE reader."), _("\
16239 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16240 The value is the maximum depth to print."),
16241                             NULL,
16242                             NULL,
16243                             &setdebuglist, &showdebuglist);
16244
16245   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16246                _("\
16247 Save a gdb-index file.\n\
16248 Usage: save gdb-index DIRECTORY"),
16249                &save_cmdlist);
16250   set_cmd_completer (c, filename_completer);
16251 }