eec52fb75e83d150129c0e3266c30b2c39c1068f
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6    Inc.  with support from Florida State University (under contract
7    with the Ada Joint Program Office), and Silicon Graphics, Inc.
8    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10    support.
11
12    This file is part of GDB.
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 3 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28    reading off the end of the section.
29    E.g., load_partial_dies, read_partial_die.  */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h"  /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "valprint.h"
63 #include <ctype.h>
64
65 #include <fcntl.h>
66 #include "gdb_string.h"
67 #include "gdb_assert.h"
68 #include <sys/types.h>
69 #ifdef HAVE_ZLIB_H
70 #include <zlib.h>
71 #endif
72 #ifdef HAVE_MMAP
73 #include <sys/mman.h>
74 #ifndef MAP_FAILED
75 #define MAP_FAILED ((void *) -1)
76 #endif
77 #endif
78
79 typedef struct symbol *symbolp;
80 DEF_VEC_P (symbolp);
81
82 /* When non-zero, dump DIEs after they are read in.  */
83 static int dwarf2_die_debug = 0;
84
85 /* When non-zero, cross-check physname against demangler.  */
86 static int check_physname = 0;
87
88 /* When non-zero, do not reject deprecated .gdb_index sections.  */
89 int use_deprecated_index_sections = 0;
90
91 static int pagesize;
92
93 /* When set, the file that we're processing is known to have debugging
94    info for C++ namespaces.  GCC 3.3.x did not produce this information,
95    but later versions do.  */
96
97 static int processing_has_namespace_info;
98
99 static const struct objfile_data *dwarf2_objfile_data_key;
100
101 struct dwarf2_section_info
102 {
103   asection *asection;
104   gdb_byte *buffer;
105   bfd_size_type size;
106   /* Not NULL if the section was actually mmapped.  */
107   void *map_addr;
108   /* Page aligned size of mmapped area.  */
109   bfd_size_type map_len;
110   /* True if we have tried to read this section.  */
111   int readin;
112 };
113
114 typedef struct dwarf2_section_info dwarf2_section_info_def;
115 DEF_VEC_O (dwarf2_section_info_def);
116
117 /* All offsets in the index are of this type.  It must be
118    architecture-independent.  */
119 typedef uint32_t offset_type;
120
121 DEF_VEC_I (offset_type);
122
123 /* A description of the mapped index.  The file format is described in
124    a comment by the code that writes the index.  */
125 struct mapped_index
126 {
127   /* Index data format version.  */
128   int version;
129
130   /* The total length of the buffer.  */
131   off_t total_size;
132
133   /* A pointer to the address table data.  */
134   const gdb_byte *address_table;
135
136   /* Size of the address table data in bytes.  */
137   offset_type address_table_size;
138
139   /* The symbol table, implemented as a hash table.  */
140   const offset_type *symbol_table;
141
142   /* Size in slots, each slot is 2 offset_types.  */
143   offset_type symbol_table_slots;
144
145   /* A pointer to the constant pool.  */
146   const char *constant_pool;
147 };
148
149 /* Collection of data recorded per objfile.
150    This hangs off of dwarf2_objfile_data_key.  */
151
152 struct dwarf2_per_objfile
153 {
154   struct dwarf2_section_info info;
155   struct dwarf2_section_info abbrev;
156   struct dwarf2_section_info line;
157   struct dwarf2_section_info loc;
158   struct dwarf2_section_info macinfo;
159   struct dwarf2_section_info macro;
160   struct dwarf2_section_info str;
161   struct dwarf2_section_info ranges;
162   struct dwarf2_section_info frame;
163   struct dwarf2_section_info eh_frame;
164   struct dwarf2_section_info gdb_index;
165
166   VEC (dwarf2_section_info_def) *types;
167
168   /* Back link.  */
169   struct objfile *objfile;
170
171   /* Table of all the compilation units.  This is used to locate
172      the target compilation unit of a particular reference.  */
173   struct dwarf2_per_cu_data **all_comp_units;
174
175   /* The number of compilation units in ALL_COMP_UNITS.  */
176   int n_comp_units;
177
178   /* The number of .debug_types-related CUs.  */
179   int n_type_units;
180
181   /* The .debug_types-related CUs (TUs).  */
182   struct dwarf2_per_cu_data **all_type_units;
183
184   /* A chain of compilation units that are currently read in, so that
185      they can be freed later.  */
186   struct dwarf2_per_cu_data *read_in_chain;
187
188   /* A table mapping .debug_types signatures to its signatured_type entry.
189      This is NULL if the .debug_types section hasn't been read in yet.  */
190   htab_t signatured_types;
191
192   /* A flag indicating wether this objfile has a section loaded at a
193      VMA of 0.  */
194   int has_section_at_zero;
195
196   /* True if we are using the mapped index,
197      or we are faking it for OBJF_READNOW's sake.  */
198   unsigned char using_index;
199
200   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
201   struct mapped_index *index_table;
202
203   /* When using index_table, this keeps track of all quick_file_names entries.
204      TUs can share line table entries with CUs or other TUs, and there can be
205      a lot more TUs than unique line tables, so we maintain a separate table
206      of all line table entries to support the sharing.  */
207   htab_t quick_file_names_table;
208
209   /* Set during partial symbol reading, to prevent queueing of full
210      symbols.  */
211   int reading_partial_symbols;
212
213   /* Table mapping type .debug_info DIE offsets to types.
214      This is NULL if not allocated yet.
215      It (currently) makes sense to allocate debug_types_type_hash lazily.
216      To keep things simple we allocate both lazily.  */
217   htab_t debug_info_type_hash;
218
219   /* Table mapping type .debug_types DIE sect_offset to types.
220      This is NULL if not allocated yet.  */
221   htab_t debug_types_type_hash;
222 };
223
224 static struct dwarf2_per_objfile *dwarf2_per_objfile;
225
226 /* Default names of the debugging sections.  */
227
228 /* Note that if the debugging section has been compressed, it might
229    have a name like .zdebug_info.  */
230
231 static const struct dwarf2_debug_sections dwarf2_elf_names =
232 {
233   { ".debug_info", ".zdebug_info" },
234   { ".debug_abbrev", ".zdebug_abbrev" },
235   { ".debug_line", ".zdebug_line" },
236   { ".debug_loc", ".zdebug_loc" },
237   { ".debug_macinfo", ".zdebug_macinfo" },
238   { ".debug_macro", ".zdebug_macro" },
239   { ".debug_str", ".zdebug_str" },
240   { ".debug_ranges", ".zdebug_ranges" },
241   { ".debug_types", ".zdebug_types" },
242   { ".debug_frame", ".zdebug_frame" },
243   { ".eh_frame", NULL },
244   { ".gdb_index", ".zgdb_index" },
245   23
246 };
247
248 /* local data types */
249
250 /* We hold several abbreviation tables in memory at the same time.  */
251 #ifndef ABBREV_HASH_SIZE
252 #define ABBREV_HASH_SIZE 121
253 #endif
254
255 /* The data in a compilation unit header, after target2host
256    translation, looks like this.  */
257 struct comp_unit_head
258 {
259   unsigned int length;
260   short version;
261   unsigned char addr_size;
262   unsigned char signed_addr_p;
263   sect_offset abbrev_offset;
264
265   /* Size of file offsets; either 4 or 8.  */
266   unsigned int offset_size;
267
268   /* Size of the length field; either 4 or 12.  */
269   unsigned int initial_length_size;
270
271   /* Offset to the first byte of this compilation unit header in the
272      .debug_info section, for resolving relative reference dies.  */
273   sect_offset offset;
274
275   /* Offset to first die in this cu from the start of the cu.
276      This will be the first byte following the compilation unit header.  */
277   cu_offset first_die_offset;
278 };
279
280 /* Type used for delaying computation of method physnames.
281    See comments for compute_delayed_physnames.  */
282 struct delayed_method_info
283 {
284   /* The type to which the method is attached, i.e., its parent class.  */
285   struct type *type;
286
287   /* The index of the method in the type's function fieldlists.  */
288   int fnfield_index;
289
290   /* The index of the method in the fieldlist.  */
291   int index;
292
293   /* The name of the DIE.  */
294   const char *name;
295
296   /*  The DIE associated with this method.  */
297   struct die_info *die;
298 };
299
300 typedef struct delayed_method_info delayed_method_info;
301 DEF_VEC_O (delayed_method_info);
302
303 /* Internal state when decoding a particular compilation unit.  */
304 struct dwarf2_cu
305 {
306   /* The objfile containing this compilation unit.  */
307   struct objfile *objfile;
308
309   /* The header of the compilation unit.  */
310   struct comp_unit_head header;
311
312   /* Base address of this compilation unit.  */
313   CORE_ADDR base_address;
314
315   /* Non-zero if base_address has been set.  */
316   int base_known;
317
318   /* The language we are debugging.  */
319   enum language language;
320   const struct language_defn *language_defn;
321
322   const char *producer;
323
324   /* The generic symbol table building routines have separate lists for
325      file scope symbols and all all other scopes (local scopes).  So
326      we need to select the right one to pass to add_symbol_to_list().
327      We do it by keeping a pointer to the correct list in list_in_scope.
328
329      FIXME: The original dwarf code just treated the file scope as the
330      first local scope, and all other local scopes as nested local
331      scopes, and worked fine.  Check to see if we really need to
332      distinguish these in buildsym.c.  */
333   struct pending **list_in_scope;
334
335   /* DWARF abbreviation table associated with this compilation unit.  */
336   struct abbrev_info **dwarf2_abbrevs;
337
338   /* Storage for the abbrev table.  */
339   struct obstack abbrev_obstack;
340
341   /* Hash table holding all the loaded partial DIEs
342      with partial_die->offset.SECT_OFF as hash.  */
343   htab_t partial_dies;
344
345   /* Storage for things with the same lifetime as this read-in compilation
346      unit, including partial DIEs.  */
347   struct obstack comp_unit_obstack;
348
349   /* When multiple dwarf2_cu structures are living in memory, this field
350      chains them all together, so that they can be released efficiently.
351      We will probably also want a generation counter so that most-recently-used
352      compilation units are cached...  */
353   struct dwarf2_per_cu_data *read_in_chain;
354
355   /* Backchain to our per_cu entry if the tree has been built.  */
356   struct dwarf2_per_cu_data *per_cu;
357
358   /* How many compilation units ago was this CU last referenced?  */
359   int last_used;
360
361   /* A hash table of DIE cu_offset for following references with
362      die_info->offset.sect_off as hash.  */
363   htab_t die_hash;
364
365   /* Full DIEs if read in.  */
366   struct die_info *dies;
367
368   /* A set of pointers to dwarf2_per_cu_data objects for compilation
369      units referenced by this one.  Only set during full symbol processing;
370      partial symbol tables do not have dependencies.  */
371   htab_t dependencies;
372
373   /* Header data from the line table, during full symbol processing.  */
374   struct line_header *line_header;
375
376   /* A list of methods which need to have physnames computed
377      after all type information has been read.  */
378   VEC (delayed_method_info) *method_list;
379
380   /* To be copied to symtab->call_site_htab.  */
381   htab_t call_site_htab;
382
383   /* Mark used when releasing cached dies.  */
384   unsigned int mark : 1;
385
386   /* This CU references .debug_loc.  See the symtab->locations_valid field.
387      This test is imperfect as there may exist optimized debug code not using
388      any location list and still facing inlining issues if handled as
389      unoptimized code.  For a future better test see GCC PR other/32998.  */
390   unsigned int has_loclist : 1;
391
392   /* These cache the results of producer_is_gxx_lt_4_6.
393      CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid.  This
394      information is cached because profiling CU expansion showed
395      excessive time spent in producer_is_gxx_lt_4_6.  */
396   unsigned int checked_producer : 1;
397   unsigned int producer_is_gxx_lt_4_6 : 1;
398 };
399
400 /* Persistent data held for a compilation unit, even when not
401    processing it.  We put a pointer to this structure in the
402    read_symtab_private field of the psymtab.  */
403
404 struct dwarf2_per_cu_data
405 {
406   /* The start offset and length of this compilation unit.  2**29-1
407      bytes should suffice to store the length of any compilation unit
408      - if it doesn't, GDB will fall over anyway.
409      NOTE: Unlike comp_unit_head.length, this length includes
410      initial_length_size.  */
411   sect_offset offset;
412   unsigned int length : 29;
413
414   /* Flag indicating this compilation unit will be read in before
415      any of the current compilation units are processed.  */
416   unsigned int queued : 1;
417
418   /* This flag will be set when reading partial DIEs if we need to load
419      absolutely all DIEs for this compilation unit, instead of just the ones
420      we think are interesting.  It gets set if we look for a DIE in the
421      hash table and don't find it.  */
422   unsigned int load_all_dies : 1;
423
424   /* Non-null if this CU is from .debug_types; in which case it points
425      to the section.  Otherwise it's from .debug_info.  */
426   struct dwarf2_section_info *debug_types_section;
427
428   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
429      of the CU cache it gets reset to NULL again.  */
430   struct dwarf2_cu *cu;
431
432   /* The corresponding objfile.
433      Normally we can get the objfile from dwarf2_per_objfile.
434      However we can enter this file with just a "per_cu" handle.  */
435   struct objfile *objfile;
436
437   /* When using partial symbol tables, the 'psymtab' field is active.
438      Otherwise the 'quick' field is active.  */
439   union
440   {
441     /* The partial symbol table associated with this compilation unit,
442        or NULL for partial units (which do not have an associated
443        symtab).  */
444     struct partial_symtab *psymtab;
445
446     /* Data needed by the "quick" functions.  */
447     struct dwarf2_per_cu_quick_data *quick;
448   } v;
449 };
450
451 /* Entry in the signatured_types hash table.  */
452
453 struct signatured_type
454 {
455   ULONGEST signature;
456
457   /* Offset in this TU of the type defined by this TU.  */
458   cu_offset type_offset;
459
460   /* The CU(/TU) of this type.  */
461   struct dwarf2_per_cu_data per_cu;
462 };
463
464 /* Struct used to pass misc. parameters to read_die_and_children, et
465    al.  which are used for both .debug_info and .debug_types dies.
466    All parameters here are unchanging for the life of the call.  This
467    struct exists to abstract away the constant parameters of die
468    reading.  */
469
470 struct die_reader_specs
471 {
472   /* The bfd of this objfile.  */
473   bfd* abfd;
474
475   /* The CU of the DIE we are parsing.  */
476   struct dwarf2_cu *cu;
477
478   /* Pointer to start of section buffer.
479      This is either the start of .debug_info or .debug_types.  */
480   const gdb_byte *buffer;
481 };
482
483 /* The line number information for a compilation unit (found in the
484    .debug_line section) begins with a "statement program header",
485    which contains the following information.  */
486 struct line_header
487 {
488   unsigned int total_length;
489   unsigned short version;
490   unsigned int header_length;
491   unsigned char minimum_instruction_length;
492   unsigned char maximum_ops_per_instruction;
493   unsigned char default_is_stmt;
494   int line_base;
495   unsigned char line_range;
496   unsigned char opcode_base;
497
498   /* standard_opcode_lengths[i] is the number of operands for the
499      standard opcode whose value is i.  This means that
500      standard_opcode_lengths[0] is unused, and the last meaningful
501      element is standard_opcode_lengths[opcode_base - 1].  */
502   unsigned char *standard_opcode_lengths;
503
504   /* The include_directories table.  NOTE!  These strings are not
505      allocated with xmalloc; instead, they are pointers into
506      debug_line_buffer.  If you try to free them, `free' will get
507      indigestion.  */
508   unsigned int num_include_dirs, include_dirs_size;
509   char **include_dirs;
510
511   /* The file_names table.  NOTE!  These strings are not allocated
512      with xmalloc; instead, they are pointers into debug_line_buffer.
513      Don't try to free them directly.  */
514   unsigned int num_file_names, file_names_size;
515   struct file_entry
516   {
517     char *name;
518     unsigned int dir_index;
519     unsigned int mod_time;
520     unsigned int length;
521     int included_p; /* Non-zero if referenced by the Line Number Program.  */
522     struct symtab *symtab; /* The associated symbol table, if any.  */
523   } *file_names;
524
525   /* The start and end of the statement program following this
526      header.  These point into dwarf2_per_objfile->line_buffer.  */
527   gdb_byte *statement_program_start, *statement_program_end;
528 };
529
530 /* When we construct a partial symbol table entry we only
531    need this much information.  */
532 struct partial_die_info
533   {
534     /* Offset of this DIE.  */
535     sect_offset offset;
536
537     /* DWARF-2 tag for this DIE.  */
538     ENUM_BITFIELD(dwarf_tag) tag : 16;
539
540     /* Assorted flags describing the data found in this DIE.  */
541     unsigned int has_children : 1;
542     unsigned int is_external : 1;
543     unsigned int is_declaration : 1;
544     unsigned int has_type : 1;
545     unsigned int has_specification : 1;
546     unsigned int has_pc_info : 1;
547     unsigned int may_be_inlined : 1;
548
549     /* Flag set if the SCOPE field of this structure has been
550        computed.  */
551     unsigned int scope_set : 1;
552
553     /* Flag set if the DIE has a byte_size attribute.  */
554     unsigned int has_byte_size : 1;
555
556     /* Flag set if any of the DIE's children are template arguments.  */
557     unsigned int has_template_arguments : 1;
558
559     /* Flag set if fixup_partial_die has been called on this die.  */
560     unsigned int fixup_called : 1;
561
562     /* The name of this DIE.  Normally the value of DW_AT_name, but
563        sometimes a default name for unnamed DIEs.  */
564     char *name;
565
566     /* The linkage name, if present.  */
567     const char *linkage_name;
568
569     /* The scope to prepend to our children.  This is generally
570        allocated on the comp_unit_obstack, so will disappear
571        when this compilation unit leaves the cache.  */
572     char *scope;
573
574     /* The location description associated with this DIE, if any.  */
575     struct dwarf_block *locdesc;
576
577     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
578     CORE_ADDR lowpc;
579     CORE_ADDR highpc;
580
581     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
582        DW_AT_sibling, if any.  */
583     /* NOTE: This member isn't strictly necessary, read_partial_die could
584        return DW_AT_sibling values to its caller load_partial_dies.  */
585     gdb_byte *sibling;
586
587     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
588        DW_AT_specification (or DW_AT_abstract_origin or
589        DW_AT_extension).  */
590     sect_offset spec_offset;
591
592     /* Pointers to this DIE's parent, first child, and next sibling,
593        if any.  */
594     struct partial_die_info *die_parent, *die_child, *die_sibling;
595   };
596
597 /* This data structure holds the information of an abbrev.  */
598 struct abbrev_info
599   {
600     unsigned int number;        /* number identifying abbrev */
601     enum dwarf_tag tag;         /* dwarf tag */
602     unsigned short has_children;                /* boolean */
603     unsigned short num_attrs;   /* number of attributes */
604     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
605     struct abbrev_info *next;   /* next in chain */
606   };
607
608 struct attr_abbrev
609   {
610     ENUM_BITFIELD(dwarf_attribute) name : 16;
611     ENUM_BITFIELD(dwarf_form) form : 16;
612   };
613
614 /* Attributes have a name and a value.  */
615 struct attribute
616   {
617     ENUM_BITFIELD(dwarf_attribute) name : 16;
618     ENUM_BITFIELD(dwarf_form) form : 15;
619
620     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
621        field should be in u.str (existing only for DW_STRING) but it is kept
622        here for better struct attribute alignment.  */
623     unsigned int string_is_canonical : 1;
624
625     union
626       {
627         char *str;
628         struct dwarf_block *blk;
629         ULONGEST unsnd;
630         LONGEST snd;
631         CORE_ADDR addr;
632         struct signatured_type *signatured_type;
633       }
634     u;
635   };
636
637 /* This data structure holds a complete die structure.  */
638 struct die_info
639   {
640     /* DWARF-2 tag for this DIE.  */
641     ENUM_BITFIELD(dwarf_tag) tag : 16;
642
643     /* Number of attributes */
644     unsigned char num_attrs;
645
646     /* True if we're presently building the full type name for the
647        type derived from this DIE.  */
648     unsigned char building_fullname : 1;
649
650     /* Abbrev number */
651     unsigned int abbrev;
652
653     /* Offset in .debug_info or .debug_types section.  */
654     sect_offset offset;
655
656     /* The dies in a compilation unit form an n-ary tree.  PARENT
657        points to this die's parent; CHILD points to the first child of
658        this node; and all the children of a given node are chained
659        together via their SIBLING fields.  */
660     struct die_info *child;     /* Its first child, if any.  */
661     struct die_info *sibling;   /* Its next sibling, if any.  */
662     struct die_info *parent;    /* Its parent, if any.  */
663
664     /* An array of attributes, with NUM_ATTRS elements.  There may be
665        zero, but it's not common and zero-sized arrays are not
666        sufficiently portable C.  */
667     struct attribute attrs[1];
668   };
669
670 /* Get at parts of an attribute structure.  */
671
672 #define DW_STRING(attr)    ((attr)->u.str)
673 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
674 #define DW_UNSND(attr)     ((attr)->u.unsnd)
675 #define DW_BLOCK(attr)     ((attr)->u.blk)
676 #define DW_SND(attr)       ((attr)->u.snd)
677 #define DW_ADDR(attr)      ((attr)->u.addr)
678 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
679
680 /* Blocks are a bunch of untyped bytes.  */
681 struct dwarf_block
682   {
683     unsigned int size;
684
685     /* Valid only if SIZE is not zero.  */
686     gdb_byte *data;
687   };
688
689 #ifndef ATTR_ALLOC_CHUNK
690 #define ATTR_ALLOC_CHUNK 4
691 #endif
692
693 /* Allocate fields for structs, unions and enums in this size.  */
694 #ifndef DW_FIELD_ALLOC_CHUNK
695 #define DW_FIELD_ALLOC_CHUNK 4
696 #endif
697
698 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
699    but this would require a corresponding change in unpack_field_as_long
700    and friends.  */
701 static int bits_per_byte = 8;
702
703 /* The routines that read and process dies for a C struct or C++ class
704    pass lists of data member fields and lists of member function fields
705    in an instance of a field_info structure, as defined below.  */
706 struct field_info
707   {
708     /* List of data member and baseclasses fields.  */
709     struct nextfield
710       {
711         struct nextfield *next;
712         int accessibility;
713         int virtuality;
714         struct field field;
715       }
716      *fields, *baseclasses;
717
718     /* Number of fields (including baseclasses).  */
719     int nfields;
720
721     /* Number of baseclasses.  */
722     int nbaseclasses;
723
724     /* Set if the accesibility of one of the fields is not public.  */
725     int non_public_fields;
726
727     /* Member function fields array, entries are allocated in the order they
728        are encountered in the object file.  */
729     struct nextfnfield
730       {
731         struct nextfnfield *next;
732         struct fn_field fnfield;
733       }
734      *fnfields;
735
736     /* Member function fieldlist array, contains name of possibly overloaded
737        member function, number of overloaded member functions and a pointer
738        to the head of the member function field chain.  */
739     struct fnfieldlist
740       {
741         char *name;
742         int length;
743         struct nextfnfield *head;
744       }
745      *fnfieldlists;
746
747     /* Number of entries in the fnfieldlists array.  */
748     int nfnfields;
749
750     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
751        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
752     struct typedef_field_list
753       {
754         struct typedef_field field;
755         struct typedef_field_list *next;
756       }
757     *typedef_field_list;
758     unsigned typedef_field_list_count;
759   };
760
761 /* One item on the queue of compilation units to read in full symbols
762    for.  */
763 struct dwarf2_queue_item
764 {
765   struct dwarf2_per_cu_data *per_cu;
766   struct dwarf2_queue_item *next;
767 };
768
769 /* The current queue.  */
770 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
771
772 /* Loaded secondary compilation units are kept in memory until they
773    have not been referenced for the processing of this many
774    compilation units.  Set this to zero to disable caching.  Cache
775    sizes of up to at least twenty will improve startup time for
776    typical inter-CU-reference binaries, at an obvious memory cost.  */
777 static int dwarf2_max_cache_age = 5;
778 static void
779 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
780                            struct cmd_list_element *c, const char *value)
781 {
782   fprintf_filtered (file, _("The upper bound on the age of cached "
783                             "dwarf2 compilation units is %s.\n"),
784                     value);
785 }
786
787
788 /* Various complaints about symbol reading that don't abort the process.  */
789
790 static void
791 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
792 {
793   complaint (&symfile_complaints,
794              _("statement list doesn't fit in .debug_line section"));
795 }
796
797 static void
798 dwarf2_debug_line_missing_file_complaint (void)
799 {
800   complaint (&symfile_complaints,
801              _(".debug_line section has line data without a file"));
802 }
803
804 static void
805 dwarf2_debug_line_missing_end_sequence_complaint (void)
806 {
807   complaint (&symfile_complaints,
808              _(".debug_line section has line "
809                "program sequence without an end"));
810 }
811
812 static void
813 dwarf2_complex_location_expr_complaint (void)
814 {
815   complaint (&symfile_complaints, _("location expression too complex"));
816 }
817
818 static void
819 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
820                                               int arg3)
821 {
822   complaint (&symfile_complaints,
823              _("const value length mismatch for '%s', got %d, expected %d"),
824              arg1, arg2, arg3);
825 }
826
827 static void
828 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
829 {
830   complaint (&symfile_complaints,
831              _("macro info runs off end of `%s' section"),
832              section->asection->name);
833 }
834
835 static void
836 dwarf2_macro_malformed_definition_complaint (const char *arg1)
837 {
838   complaint (&symfile_complaints,
839              _("macro debug info contains a "
840                "malformed macro definition:\n`%s'"),
841              arg1);
842 }
843
844 static void
845 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
846 {
847   complaint (&symfile_complaints,
848              _("invalid attribute class or form for '%s' in '%s'"),
849              arg1, arg2);
850 }
851
852 /* local function prototypes */
853
854 static void dwarf2_locate_sections (bfd *, asection *, void *);
855
856 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
857                                            struct objfile *);
858
859 static void dwarf2_find_base_address (struct die_info *die,
860                                       struct dwarf2_cu *cu);
861
862 static void dwarf2_build_psymtabs_hard (struct objfile *);
863
864 static void scan_partial_symbols (struct partial_die_info *,
865                                   CORE_ADDR *, CORE_ADDR *,
866                                   int, struct dwarf2_cu *);
867
868 static void add_partial_symbol (struct partial_die_info *,
869                                 struct dwarf2_cu *);
870
871 static void add_partial_namespace (struct partial_die_info *pdi,
872                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
873                                    int need_pc, struct dwarf2_cu *cu);
874
875 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
876                                 CORE_ADDR *highpc, int need_pc,
877                                 struct dwarf2_cu *cu);
878
879 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
880                                      struct dwarf2_cu *cu);
881
882 static void add_partial_subprogram (struct partial_die_info *pdi,
883                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
884                                     int need_pc, struct dwarf2_cu *cu);
885
886 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
887                                      gdb_byte *buffer, gdb_byte *info_ptr,
888                                      bfd *abfd, struct dwarf2_cu *cu);
889
890 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
891
892 static void psymtab_to_symtab_1 (struct partial_symtab *);
893
894 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
895
896 static void dwarf2_free_abbrev_table (void *);
897
898 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
899
900 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
901                                             struct dwarf2_cu *);
902
903 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
904                                                  struct dwarf2_cu *);
905
906 static struct partial_die_info *load_partial_dies (bfd *,
907                                                    gdb_byte *, gdb_byte *,
908                                                    int, struct dwarf2_cu *);
909
910 static gdb_byte *read_partial_die (struct partial_die_info *,
911                                    struct abbrev_info *abbrev,
912                                    unsigned int, bfd *,
913                                    gdb_byte *, gdb_byte *,
914                                    struct dwarf2_cu *);
915
916 static struct partial_die_info *find_partial_die (sect_offset,
917                                                   struct dwarf2_cu *);
918
919 static void fixup_partial_die (struct partial_die_info *,
920                                struct dwarf2_cu *);
921
922 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
923                                  bfd *, gdb_byte *, struct dwarf2_cu *);
924
925 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
926                                        bfd *, gdb_byte *, struct dwarf2_cu *);
927
928 static unsigned int read_1_byte (bfd *, gdb_byte *);
929
930 static int read_1_signed_byte (bfd *, gdb_byte *);
931
932 static unsigned int read_2_bytes (bfd *, gdb_byte *);
933
934 static unsigned int read_4_bytes (bfd *, gdb_byte *);
935
936 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
937
938 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
939                                unsigned int *);
940
941 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
942
943 static LONGEST read_checked_initial_length_and_offset
944   (bfd *, gdb_byte *, const struct comp_unit_head *,
945    unsigned int *, unsigned int *);
946
947 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
948                             unsigned int *);
949
950 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
951
952 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
953
954 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
955
956 static char *read_indirect_string (bfd *, gdb_byte *,
957                                    const struct comp_unit_head *,
958                                    unsigned int *);
959
960 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
961
962 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
963
964 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
965
966 static void set_cu_language (unsigned int, struct dwarf2_cu *);
967
968 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
969                                       struct dwarf2_cu *);
970
971 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
972                                                 unsigned int,
973                                                 struct dwarf2_cu *);
974
975 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
976                                struct dwarf2_cu *cu);
977
978 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
979
980 static struct die_info *die_specification (struct die_info *die,
981                                            struct dwarf2_cu **);
982
983 static void free_line_header (struct line_header *lh);
984
985 static void add_file_name (struct line_header *, char *, unsigned int,
986                            unsigned int, unsigned int);
987
988 static struct line_header *(dwarf_decode_line_header
989                             (unsigned int offset,
990                              bfd *abfd, struct dwarf2_cu *cu));
991
992 static void dwarf_decode_lines (struct line_header *, const char *,
993                                 struct dwarf2_cu *, struct partial_symtab *,
994                                 int);
995
996 static void dwarf2_start_subfile (char *, const char *, const char *);
997
998 static struct symbol *new_symbol (struct die_info *, struct type *,
999                                   struct dwarf2_cu *);
1000
1001 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1002                                        struct dwarf2_cu *, struct symbol *);
1003
1004 static void dwarf2_const_value (struct attribute *, struct symbol *,
1005                                 struct dwarf2_cu *);
1006
1007 static void dwarf2_const_value_attr (struct attribute *attr,
1008                                      struct type *type,
1009                                      const char *name,
1010                                      struct obstack *obstack,
1011                                      struct dwarf2_cu *cu, long *value,
1012                                      gdb_byte **bytes,
1013                                      struct dwarf2_locexpr_baton **baton);
1014
1015 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1016
1017 static int need_gnat_info (struct dwarf2_cu *);
1018
1019 static struct type *die_descriptive_type (struct die_info *,
1020                                           struct dwarf2_cu *);
1021
1022 static void set_descriptive_type (struct type *, struct die_info *,
1023                                   struct dwarf2_cu *);
1024
1025 static struct type *die_containing_type (struct die_info *,
1026                                          struct dwarf2_cu *);
1027
1028 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1029                                      struct dwarf2_cu *);
1030
1031 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1032
1033 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1034
1035 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1036
1037 static char *typename_concat (struct obstack *obs, const char *prefix,
1038                               const char *suffix, int physname,
1039                               struct dwarf2_cu *cu);
1040
1041 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1046
1047 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1048
1049 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1050
1051 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1052                                struct dwarf2_cu *, struct partial_symtab *);
1053
1054 static int dwarf2_get_pc_bounds (struct die_info *,
1055                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1056                                  struct partial_symtab *);
1057
1058 static void get_scope_pc_bounds (struct die_info *,
1059                                  CORE_ADDR *, CORE_ADDR *,
1060                                  struct dwarf2_cu *);
1061
1062 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1063                                         CORE_ADDR, struct dwarf2_cu *);
1064
1065 static void dwarf2_add_field (struct field_info *, struct die_info *,
1066                               struct dwarf2_cu *);
1067
1068 static void dwarf2_attach_fields_to_type (struct field_info *,
1069                                           struct type *, struct dwarf2_cu *);
1070
1071 static void dwarf2_add_member_fn (struct field_info *,
1072                                   struct die_info *, struct type *,
1073                                   struct dwarf2_cu *);
1074
1075 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1076                                              struct type *,
1077                                              struct dwarf2_cu *);
1078
1079 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1082
1083 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1084
1085 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1086
1087 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1088
1089 static struct type *read_module_type (struct die_info *die,
1090                                       struct dwarf2_cu *cu);
1091
1092 static const char *namespace_name (struct die_info *die,
1093                                    int *is_anonymous, struct dwarf2_cu *);
1094
1095 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1096
1097 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1098
1099 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1100                                                        struct dwarf2_cu *);
1101
1102 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1103
1104 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1105                                                  gdb_byte *info_ptr,
1106                                                  gdb_byte **new_info_ptr,
1107                                                  struct die_info *parent);
1108
1109 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1110                                                gdb_byte *info_ptr,
1111                                                gdb_byte **new_info_ptr,
1112                                                struct die_info *parent);
1113
1114 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1115                                                gdb_byte *info_ptr,
1116                                                gdb_byte **new_info_ptr,
1117                                                struct die_info *parent);
1118
1119 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1120                                 struct die_info **, gdb_byte *,
1121                                 int *);
1122
1123 static void process_die (struct die_info *, struct dwarf2_cu *);
1124
1125 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1126                                        struct obstack *);
1127
1128 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1129
1130 static const char *dwarf2_full_name (char *name,
1131                                      struct die_info *die,
1132                                      struct dwarf2_cu *cu);
1133
1134 static struct die_info *dwarf2_extension (struct die_info *die,
1135                                           struct dwarf2_cu **);
1136
1137 static char *dwarf_tag_name (unsigned int);
1138
1139 static char *dwarf_attr_name (unsigned int);
1140
1141 static char *dwarf_form_name (unsigned int);
1142
1143 static char *dwarf_bool_name (unsigned int);
1144
1145 static char *dwarf_type_encoding_name (unsigned int);
1146
1147 #if 0
1148 static char *dwarf_cfi_name (unsigned int);
1149 #endif
1150
1151 static struct die_info *sibling_die (struct die_info *);
1152
1153 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1154
1155 static void dump_die_for_error (struct die_info *);
1156
1157 static void dump_die_1 (struct ui_file *, int level, int max_level,
1158                         struct die_info *);
1159
1160 /*static*/ void dump_die (struct die_info *, int max_level);
1161
1162 static void store_in_ref_table (struct die_info *,
1163                                 struct dwarf2_cu *);
1164
1165 static int is_ref_attr (struct attribute *);
1166
1167 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1168
1169 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1170
1171 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1172                                                struct attribute *,
1173                                                struct dwarf2_cu **);
1174
1175 static struct die_info *follow_die_ref (struct die_info *,
1176                                         struct attribute *,
1177                                         struct dwarf2_cu **);
1178
1179 static struct die_info *follow_die_sig (struct die_info *,
1180                                         struct attribute *,
1181                                         struct dwarf2_cu **);
1182
1183 static struct signatured_type *lookup_signatured_type_at_offset
1184     (struct objfile *objfile,
1185      struct dwarf2_section_info *section, sect_offset offset);
1186
1187 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1188
1189 static void read_signatured_type (struct signatured_type *);
1190
1191 /* memory allocation interface */
1192
1193 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1194
1195 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1196
1197 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1198
1199 static void dwarf_decode_macros (struct line_header *, unsigned int,
1200                                  char *, bfd *, struct dwarf2_cu *,
1201                                  struct dwarf2_section_info *,
1202                                  int);
1203
1204 static int attr_form_is_block (struct attribute *);
1205
1206 static int attr_form_is_section_offset (struct attribute *);
1207
1208 static int attr_form_is_constant (struct attribute *);
1209
1210 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211                                    struct dwarf2_loclist_baton *baton,
1212                                    struct attribute *attr);
1213
1214 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215                                          struct symbol *sym,
1216                                          struct dwarf2_cu *cu);
1217
1218 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219                                struct abbrev_info *abbrev,
1220                                struct dwarf2_cu *cu);
1221
1222 static void free_stack_comp_unit (void *);
1223
1224 static hashval_t partial_die_hash (const void *item);
1225
1226 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
1228 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1229   (sect_offset offset, struct objfile *objfile);
1230
1231 static void init_one_comp_unit (struct dwarf2_cu *cu,
1232                                 struct dwarf2_per_cu_data *per_cu);
1233
1234 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1235                                    struct die_info *comp_unit_die);
1236
1237 static void free_heap_comp_unit (void *);
1238
1239 static void free_cached_comp_units (void *);
1240
1241 static void age_cached_comp_units (void);
1242
1243 static void free_one_cached_comp_unit (void *);
1244
1245 static struct type *set_die_type (struct die_info *, struct type *,
1246                                   struct dwarf2_cu *);
1247
1248 static void create_all_comp_units (struct objfile *);
1249
1250 static int create_all_type_units (struct objfile *);
1251
1252 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1253
1254 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1255
1256 static void dwarf2_add_dependence (struct dwarf2_cu *,
1257                                    struct dwarf2_per_cu_data *);
1258
1259 static void dwarf2_mark (struct dwarf2_cu *);
1260
1261 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1262
1263 static struct type *get_die_type_at_offset (sect_offset,
1264                                             struct dwarf2_per_cu_data *per_cu);
1265
1266 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1267
1268 static void dwarf2_release_queue (void *dummy);
1269
1270 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1271
1272 static void process_queue (void);
1273
1274 static void find_file_and_directory (struct die_info *die,
1275                                      struct dwarf2_cu *cu,
1276                                      char **name, char **comp_dir);
1277
1278 static char *file_full_name (int file, struct line_header *lh,
1279                              const char *comp_dir);
1280
1281 static gdb_byte *read_and_check_comp_unit_head
1282   (struct comp_unit_head *header,
1283    struct dwarf2_section_info *section, gdb_byte *info_ptr,
1284    int is_debug_types_section);
1285
1286 static void init_cu_die_reader (struct die_reader_specs *reader,
1287                                 struct dwarf2_cu *cu);
1288
1289 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1290
1291 #if WORDS_BIGENDIAN
1292
1293 /* Convert VALUE between big- and little-endian.  */
1294 static offset_type
1295 byte_swap (offset_type value)
1296 {
1297   offset_type result;
1298
1299   result = (value & 0xff) << 24;
1300   result |= (value & 0xff00) << 8;
1301   result |= (value & 0xff0000) >> 8;
1302   result |= (value & 0xff000000) >> 24;
1303   return result;
1304 }
1305
1306 #define MAYBE_SWAP(V)  byte_swap (V)
1307
1308 #else
1309 #define MAYBE_SWAP(V) (V)
1310 #endif /* WORDS_BIGENDIAN */
1311
1312 /* The suffix for an index file.  */
1313 #define INDEX_SUFFIX ".gdb-index"
1314
1315 static const char *dwarf2_physname (char *name, struct die_info *die,
1316                                     struct dwarf2_cu *cu);
1317
1318 /* Try to locate the sections we need for DWARF 2 debugging
1319    information and return true if we have enough to do something.
1320    NAMES points to the dwarf2 section names, or is NULL if the standard
1321    ELF names are used.  */
1322
1323 int
1324 dwarf2_has_info (struct objfile *objfile,
1325                  const struct dwarf2_debug_sections *names)
1326 {
1327   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1328   if (!dwarf2_per_objfile)
1329     {
1330       /* Initialize per-objfile state.  */
1331       struct dwarf2_per_objfile *data
1332         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1333
1334       memset (data, 0, sizeof (*data));
1335       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1336       dwarf2_per_objfile = data;
1337
1338       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1339                              (void *) names);
1340       dwarf2_per_objfile->objfile = objfile;
1341     }
1342   return (dwarf2_per_objfile->info.asection != NULL
1343           && dwarf2_per_objfile->abbrev.asection != NULL);
1344 }
1345
1346 /* When loading sections, we look either for uncompressed section or for
1347    compressed section names.  */
1348
1349 static int
1350 section_is_p (const char *section_name,
1351               const struct dwarf2_section_names *names)
1352 {
1353   if (names->normal != NULL
1354       && strcmp (section_name, names->normal) == 0)
1355     return 1;
1356   if (names->compressed != NULL
1357       && strcmp (section_name, names->compressed) == 0)
1358     return 1;
1359   return 0;
1360 }
1361
1362 /* This function is mapped across the sections and remembers the
1363    offset and size of each of the debugging sections we are interested
1364    in.  */
1365
1366 static void
1367 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1368 {
1369   const struct dwarf2_debug_sections *names;
1370
1371   if (vnames == NULL)
1372     names = &dwarf2_elf_names;
1373   else
1374     names = (const struct dwarf2_debug_sections *) vnames;
1375
1376   if (section_is_p (sectp->name, &names->info))
1377     {
1378       dwarf2_per_objfile->info.asection = sectp;
1379       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1380     }
1381   else if (section_is_p (sectp->name, &names->abbrev))
1382     {
1383       dwarf2_per_objfile->abbrev.asection = sectp;
1384       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1385     }
1386   else if (section_is_p (sectp->name, &names->line))
1387     {
1388       dwarf2_per_objfile->line.asection = sectp;
1389       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1390     }
1391   else if (section_is_p (sectp->name, &names->loc))
1392     {
1393       dwarf2_per_objfile->loc.asection = sectp;
1394       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1395     }
1396   else if (section_is_p (sectp->name, &names->macinfo))
1397     {
1398       dwarf2_per_objfile->macinfo.asection = sectp;
1399       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1400     }
1401   else if (section_is_p (sectp->name, &names->macro))
1402     {
1403       dwarf2_per_objfile->macro.asection = sectp;
1404       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1405     }
1406   else if (section_is_p (sectp->name, &names->str))
1407     {
1408       dwarf2_per_objfile->str.asection = sectp;
1409       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1410     }
1411   else if (section_is_p (sectp->name, &names->frame))
1412     {
1413       dwarf2_per_objfile->frame.asection = sectp;
1414       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1415     }
1416   else if (section_is_p (sectp->name, &names->eh_frame))
1417     {
1418       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1419
1420       if (aflag & SEC_HAS_CONTENTS)
1421         {
1422           dwarf2_per_objfile->eh_frame.asection = sectp;
1423           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1424         }
1425     }
1426   else if (section_is_p (sectp->name, &names->ranges))
1427     {
1428       dwarf2_per_objfile->ranges.asection = sectp;
1429       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1430     }
1431   else if (section_is_p (sectp->name, &names->types))
1432     {
1433       struct dwarf2_section_info type_section;
1434
1435       memset (&type_section, 0, sizeof (type_section));
1436       type_section.asection = sectp;
1437       type_section.size = bfd_get_section_size (sectp);
1438
1439       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1440                      &type_section);
1441     }
1442   else if (section_is_p (sectp->name, &names->gdb_index))
1443     {
1444       dwarf2_per_objfile->gdb_index.asection = sectp;
1445       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1446     }
1447
1448   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1449       && bfd_section_vma (abfd, sectp) == 0)
1450     dwarf2_per_objfile->has_section_at_zero = 1;
1451 }
1452
1453 /* Decompress a section that was compressed using zlib.  Store the
1454    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1455
1456 static void
1457 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1458                          gdb_byte **outbuf, bfd_size_type *outsize)
1459 {
1460   bfd *abfd = objfile->obfd;
1461 #ifndef HAVE_ZLIB_H
1462   error (_("Support for zlib-compressed DWARF data (from '%s') "
1463            "is disabled in this copy of GDB"),
1464          bfd_get_filename (abfd));
1465 #else
1466   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1467   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1468   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1469   bfd_size_type uncompressed_size;
1470   gdb_byte *uncompressed_buffer;
1471   z_stream strm;
1472   int rc;
1473   int header_size = 12;
1474
1475   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1476       || bfd_bread (compressed_buffer,
1477                     compressed_size, abfd) != compressed_size)
1478     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1479            bfd_get_filename (abfd));
1480
1481   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1482      by the uncompressed section size, 8 bytes in big-endian order.  */
1483   if (compressed_size < header_size
1484       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1485     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1486            bfd_get_filename (abfd));
1487   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1488   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1489   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1490   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1491   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1492   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1493   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1494   uncompressed_size += compressed_buffer[11];
1495
1496   /* It is possible the section consists of several compressed
1497      buffers concatenated together, so we uncompress in a loop.  */
1498   strm.zalloc = NULL;
1499   strm.zfree = NULL;
1500   strm.opaque = NULL;
1501   strm.avail_in = compressed_size - header_size;
1502   strm.next_in = (Bytef*) compressed_buffer + header_size;
1503   strm.avail_out = uncompressed_size;
1504   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1505                                        uncompressed_size);
1506   rc = inflateInit (&strm);
1507   while (strm.avail_in > 0)
1508     {
1509       if (rc != Z_OK)
1510         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1511                bfd_get_filename (abfd), rc);
1512       strm.next_out = ((Bytef*) uncompressed_buffer
1513                        + (uncompressed_size - strm.avail_out));
1514       rc = inflate (&strm, Z_FINISH);
1515       if (rc != Z_STREAM_END)
1516         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1517                bfd_get_filename (abfd), rc);
1518       rc = inflateReset (&strm);
1519     }
1520   rc = inflateEnd (&strm);
1521   if (rc != Z_OK
1522       || strm.avail_out != 0)
1523     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1524            bfd_get_filename (abfd), rc);
1525
1526   do_cleanups (cleanup);
1527   *outbuf = uncompressed_buffer;
1528   *outsize = uncompressed_size;
1529 #endif
1530 }
1531
1532 /* A helper function that decides whether a section is empty.  */
1533
1534 static int
1535 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1536 {
1537   return info->asection == NULL || info->size == 0;
1538 }
1539
1540 /* Read the contents of the section INFO from object file specified by
1541    OBJFILE, store info about the section into INFO.
1542    If the section is compressed, uncompress it before returning.  */
1543
1544 static void
1545 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1546 {
1547   bfd *abfd = objfile->obfd;
1548   asection *sectp = info->asection;
1549   gdb_byte *buf, *retbuf;
1550   unsigned char header[4];
1551
1552   if (info->readin)
1553     return;
1554   info->buffer = NULL;
1555   info->map_addr = NULL;
1556   info->readin = 1;
1557
1558   if (dwarf2_section_empty_p (info))
1559     return;
1560
1561   /* Check if the file has a 4-byte header indicating compression.  */
1562   if (info->size > sizeof (header)
1563       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1564       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1565     {
1566       /* Upon decompression, update the buffer and its size.  */
1567       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1568         {
1569           zlib_decompress_section (objfile, sectp, &info->buffer,
1570                                    &info->size);
1571           return;
1572         }
1573     }
1574
1575 #ifdef HAVE_MMAP
1576   if (pagesize == 0)
1577     pagesize = getpagesize ();
1578
1579   /* Only try to mmap sections which are large enough: we don't want to
1580      waste space due to fragmentation.  Also, only try mmap for sections
1581      without relocations.  */
1582
1583   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1584     {
1585       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1586                          MAP_PRIVATE, sectp->filepos,
1587                          &info->map_addr, &info->map_len);
1588
1589       if ((caddr_t)info->buffer != MAP_FAILED)
1590         {
1591 #if HAVE_POSIX_MADVISE
1592           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1593 #endif
1594           return;
1595         }
1596     }
1597 #endif
1598
1599   /* If we get here, we are a normal, not-compressed section.  */
1600   info->buffer = buf
1601     = obstack_alloc (&objfile->objfile_obstack, info->size);
1602
1603   /* When debugging .o files, we may need to apply relocations; see
1604      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1605      We never compress sections in .o files, so we only need to
1606      try this when the section is not compressed.  */
1607   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1608   if (retbuf != NULL)
1609     {
1610       info->buffer = retbuf;
1611       return;
1612     }
1613
1614   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1615       || bfd_bread (buf, info->size, abfd) != info->size)
1616     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1617            bfd_get_filename (abfd));
1618 }
1619
1620 /* A helper function that returns the size of a section in a safe way.
1621    If you are positive that the section has been read before using the
1622    size, then it is safe to refer to the dwarf2_section_info object's
1623    "size" field directly.  In other cases, you must call this
1624    function, because for compressed sections the size field is not set
1625    correctly until the section has been read.  */
1626
1627 static bfd_size_type
1628 dwarf2_section_size (struct objfile *objfile,
1629                      struct dwarf2_section_info *info)
1630 {
1631   if (!info->readin)
1632     dwarf2_read_section (objfile, info);
1633   return info->size;
1634 }
1635
1636 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1637    SECTION_NAME.  */
1638
1639 void
1640 dwarf2_get_section_info (struct objfile *objfile,
1641                          enum dwarf2_section_enum sect,
1642                          asection **sectp, gdb_byte **bufp,
1643                          bfd_size_type *sizep)
1644 {
1645   struct dwarf2_per_objfile *data
1646     = objfile_data (objfile, dwarf2_objfile_data_key);
1647   struct dwarf2_section_info *info;
1648
1649   /* We may see an objfile without any DWARF, in which case we just
1650      return nothing.  */
1651   if (data == NULL)
1652     {
1653       *sectp = NULL;
1654       *bufp = NULL;
1655       *sizep = 0;
1656       return;
1657     }
1658   switch (sect)
1659     {
1660     case DWARF2_DEBUG_FRAME:
1661       info = &data->frame;
1662       break;
1663     case DWARF2_EH_FRAME:
1664       info = &data->eh_frame;
1665       break;
1666     default:
1667       gdb_assert_not_reached ("unexpected section");
1668     }
1669
1670   dwarf2_read_section (objfile, info);
1671
1672   *sectp = info->asection;
1673   *bufp = info->buffer;
1674   *sizep = info->size;
1675 }
1676
1677 \f
1678 /* DWARF quick_symbols_functions support.  */
1679
1680 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1681    unique line tables, so we maintain a separate table of all .debug_line
1682    derived entries to support the sharing.
1683    All the quick functions need is the list of file names.  We discard the
1684    line_header when we're done and don't need to record it here.  */
1685 struct quick_file_names
1686 {
1687   /* The offset in .debug_line of the line table.  We hash on this.  */
1688   unsigned int offset;
1689
1690   /* The number of entries in file_names, real_names.  */
1691   unsigned int num_file_names;
1692
1693   /* The file names from the line table, after being run through
1694      file_full_name.  */
1695   const char **file_names;
1696
1697   /* The file names from the line table after being run through
1698      gdb_realpath.  These are computed lazily.  */
1699   const char **real_names;
1700 };
1701
1702 /* When using the index (and thus not using psymtabs), each CU has an
1703    object of this type.  This is used to hold information needed by
1704    the various "quick" methods.  */
1705 struct dwarf2_per_cu_quick_data
1706 {
1707   /* The file table.  This can be NULL if there was no file table
1708      or it's currently not read in.
1709      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1710   struct quick_file_names *file_names;
1711
1712   /* The corresponding symbol table.  This is NULL if symbols for this
1713      CU have not yet been read.  */
1714   struct symtab *symtab;
1715
1716   /* A temporary mark bit used when iterating over all CUs in
1717      expand_symtabs_matching.  */
1718   unsigned int mark : 1;
1719
1720   /* True if we've tried to read the file table and found there isn't one.
1721      There will be no point in trying to read it again next time.  */
1722   unsigned int no_file_data : 1;
1723 };
1724
1725 /* Hash function for a quick_file_names.  */
1726
1727 static hashval_t
1728 hash_file_name_entry (const void *e)
1729 {
1730   const struct quick_file_names *file_data = e;
1731
1732   return file_data->offset;
1733 }
1734
1735 /* Equality function for a quick_file_names.  */
1736
1737 static int
1738 eq_file_name_entry (const void *a, const void *b)
1739 {
1740   const struct quick_file_names *ea = a;
1741   const struct quick_file_names *eb = b;
1742
1743   return ea->offset == eb->offset;
1744 }
1745
1746 /* Delete function for a quick_file_names.  */
1747
1748 static void
1749 delete_file_name_entry (void *e)
1750 {
1751   struct quick_file_names *file_data = e;
1752   int i;
1753
1754   for (i = 0; i < file_data->num_file_names; ++i)
1755     {
1756       xfree ((void*) file_data->file_names[i]);
1757       if (file_data->real_names)
1758         xfree ((void*) file_data->real_names[i]);
1759     }
1760
1761   /* The space for the struct itself lives on objfile_obstack,
1762      so we don't free it here.  */
1763 }
1764
1765 /* Create a quick_file_names hash table.  */
1766
1767 static htab_t
1768 create_quick_file_names_table (unsigned int nr_initial_entries)
1769 {
1770   return htab_create_alloc (nr_initial_entries,
1771                             hash_file_name_entry, eq_file_name_entry,
1772                             delete_file_name_entry, xcalloc, xfree);
1773 }
1774
1775 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1776    have to be created afterwards.  You should call age_cached_comp_units after
1777    processing PER_CU->CU.  dw2_setup must have been already called.  */
1778
1779 static void
1780 load_cu (struct dwarf2_per_cu_data *per_cu)
1781 {
1782   if (per_cu->debug_types_section)
1783     load_full_type_unit (per_cu);
1784   else
1785     load_full_comp_unit (per_cu);
1786
1787   gdb_assert (per_cu->cu != NULL);
1788
1789   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1790 }
1791
1792 /* Read in the symbols for PER_CU.  */
1793
1794 static void
1795 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1796 {
1797   struct cleanup *back_to;
1798
1799   back_to = make_cleanup (dwarf2_release_queue, NULL);
1800
1801   queue_comp_unit (per_cu);
1802
1803   load_cu (per_cu);
1804
1805   process_queue ();
1806
1807   /* Age the cache, releasing compilation units that have not
1808      been used recently.  */
1809   age_cached_comp_units ();
1810
1811   do_cleanups (back_to);
1812 }
1813
1814 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1815    the objfile from which this CU came.  Returns the resulting symbol
1816    table.  */
1817
1818 static struct symtab *
1819 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1820 {
1821   if (!per_cu->v.quick->symtab)
1822     {
1823       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1824       increment_reading_symtab ();
1825       dw2_do_instantiate_symtab (per_cu);
1826       do_cleanups (back_to);
1827     }
1828   return per_cu->v.quick->symtab;
1829 }
1830
1831 /* Return the CU given its index.  */
1832
1833 static struct dwarf2_per_cu_data *
1834 dw2_get_cu (int index)
1835 {
1836   if (index >= dwarf2_per_objfile->n_comp_units)
1837     {
1838       index -= dwarf2_per_objfile->n_comp_units;
1839       return dwarf2_per_objfile->all_type_units[index];
1840     }
1841   return dwarf2_per_objfile->all_comp_units[index];
1842 }
1843
1844 /* A helper function that knows how to read a 64-bit value in a way
1845    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1846    otherwise.  */
1847
1848 static int
1849 extract_cu_value (const char *bytes, ULONGEST *result)
1850 {
1851   if (sizeof (ULONGEST) < 8)
1852     {
1853       int i;
1854
1855       /* Ignore the upper 4 bytes if they are all zero.  */
1856       for (i = 0; i < 4; ++i)
1857         if (bytes[i + 4] != 0)
1858           return 0;
1859
1860       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1861     }
1862   else
1863     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1864   return 1;
1865 }
1866
1867 /* Read the CU list from the mapped index, and use it to create all
1868    the CU objects for this objfile.  Return 0 if something went wrong,
1869    1 if everything went ok.  */
1870
1871 static int
1872 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1873                        offset_type cu_list_elements)
1874 {
1875   offset_type i;
1876
1877   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1878   dwarf2_per_objfile->all_comp_units
1879     = obstack_alloc (&objfile->objfile_obstack,
1880                      dwarf2_per_objfile->n_comp_units
1881                      * sizeof (struct dwarf2_per_cu_data *));
1882
1883   for (i = 0; i < cu_list_elements; i += 2)
1884     {
1885       struct dwarf2_per_cu_data *the_cu;
1886       ULONGEST offset, length;
1887
1888       if (!extract_cu_value (cu_list, &offset)
1889           || !extract_cu_value (cu_list + 8, &length))
1890         return 0;
1891       cu_list += 2 * 8;
1892
1893       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1894                                struct dwarf2_per_cu_data);
1895       the_cu->offset.sect_off = offset;
1896       the_cu->length = length;
1897       the_cu->objfile = objfile;
1898       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1899                                         struct dwarf2_per_cu_quick_data);
1900       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1901     }
1902
1903   return 1;
1904 }
1905
1906 /* Create the signatured type hash table from the index.  */
1907
1908 static int
1909 create_signatured_type_table_from_index (struct objfile *objfile,
1910                                          struct dwarf2_section_info *section,
1911                                          const gdb_byte *bytes,
1912                                          offset_type elements)
1913 {
1914   offset_type i;
1915   htab_t sig_types_hash;
1916
1917   dwarf2_per_objfile->n_type_units = elements / 3;
1918   dwarf2_per_objfile->all_type_units
1919     = obstack_alloc (&objfile->objfile_obstack,
1920                      dwarf2_per_objfile->n_type_units
1921                      * sizeof (struct dwarf2_per_cu_data *));
1922
1923   sig_types_hash = allocate_signatured_type_table (objfile);
1924
1925   for (i = 0; i < elements; i += 3)
1926     {
1927       struct signatured_type *sig_type;
1928       ULONGEST offset, type_offset_in_tu, signature;
1929       void **slot;
1930
1931       if (!extract_cu_value (bytes, &offset)
1932           || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1933         return 0;
1934       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1935       bytes += 3 * 8;
1936
1937       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1938                                  struct signatured_type);
1939       sig_type->signature = signature;
1940       sig_type->type_offset.cu_off = type_offset_in_tu;
1941       sig_type->per_cu.debug_types_section = section;
1942       sig_type->per_cu.offset.sect_off = offset;
1943       sig_type->per_cu.objfile = objfile;
1944       sig_type->per_cu.v.quick
1945         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1946                           struct dwarf2_per_cu_quick_data);
1947
1948       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
1949       *slot = sig_type;
1950
1951       dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1952     }
1953
1954   dwarf2_per_objfile->signatured_types = sig_types_hash;
1955
1956   return 1;
1957 }
1958
1959 /* Read the address map data from the mapped index, and use it to
1960    populate the objfile's psymtabs_addrmap.  */
1961
1962 static void
1963 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1964 {
1965   const gdb_byte *iter, *end;
1966   struct obstack temp_obstack;
1967   struct addrmap *mutable_map;
1968   struct cleanup *cleanup;
1969   CORE_ADDR baseaddr;
1970
1971   obstack_init (&temp_obstack);
1972   cleanup = make_cleanup_obstack_free (&temp_obstack);
1973   mutable_map = addrmap_create_mutable (&temp_obstack);
1974
1975   iter = index->address_table;
1976   end = iter + index->address_table_size;
1977
1978   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1979
1980   while (iter < end)
1981     {
1982       ULONGEST hi, lo, cu_index;
1983       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984       iter += 8;
1985       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1986       iter += 8;
1987       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1988       iter += 4;
1989       
1990       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1991                          dw2_get_cu (cu_index));
1992     }
1993
1994   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1995                                                     &objfile->objfile_obstack);
1996   do_cleanups (cleanup);
1997 }
1998
1999 /* The hash function for strings in the mapped index.  This is the same as
2000    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2001    implementation.  This is necessary because the hash function is tied to the
2002    format of the mapped index file.  The hash values do not have to match with
2003    SYMBOL_HASH_NEXT.
2004    
2005    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2006
2007 static hashval_t
2008 mapped_index_string_hash (int index_version, const void *p)
2009 {
2010   const unsigned char *str = (const unsigned char *) p;
2011   hashval_t r = 0;
2012   unsigned char c;
2013
2014   while ((c = *str++) != 0)
2015     {
2016       if (index_version >= 5)
2017         c = tolower (c);
2018       r = r * 67 + c - 113;
2019     }
2020
2021   return r;
2022 }
2023
2024 /* Find a slot in the mapped index INDEX for the object named NAME.
2025    If NAME is found, set *VEC_OUT to point to the CU vector in the
2026    constant pool and return 1.  If NAME cannot be found, return 0.  */
2027
2028 static int
2029 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2030                           offset_type **vec_out)
2031 {
2032   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2033   offset_type hash;
2034   offset_type slot, step;
2035   int (*cmp) (const char *, const char *);
2036
2037   if (current_language->la_language == language_cplus
2038       || current_language->la_language == language_java
2039       || current_language->la_language == language_fortran)
2040     {
2041       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2042          not contain any.  */
2043       const char *paren = strchr (name, '(');
2044
2045       if (paren)
2046         {
2047           char *dup;
2048
2049           dup = xmalloc (paren - name + 1);
2050           memcpy (dup, name, paren - name);
2051           dup[paren - name] = 0;
2052
2053           make_cleanup (xfree, dup);
2054           name = dup;
2055         }
2056     }
2057
2058   /* Index version 4 did not support case insensitive searches.  But the
2059      indices for case insensitive languages are built in lowercase, therefore
2060      simulate our NAME being searched is also lowercased.  */
2061   hash = mapped_index_string_hash ((index->version == 4
2062                                     && case_sensitivity == case_sensitive_off
2063                                     ? 5 : index->version),
2064                                    name);
2065
2066   slot = hash & (index->symbol_table_slots - 1);
2067   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2068   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2069
2070   for (;;)
2071     {
2072       /* Convert a slot number to an offset into the table.  */
2073       offset_type i = 2 * slot;
2074       const char *str;
2075       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2076         {
2077           do_cleanups (back_to);
2078           return 0;
2079         }
2080
2081       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2082       if (!cmp (name, str))
2083         {
2084           *vec_out = (offset_type *) (index->constant_pool
2085                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2086           do_cleanups (back_to);
2087           return 1;
2088         }
2089
2090       slot = (slot + step) & (index->symbol_table_slots - 1);
2091     }
2092 }
2093
2094 /* Read the index file.  If everything went ok, initialize the "quick"
2095    elements of all the CUs and return 1.  Otherwise, return 0.  */
2096
2097 static int
2098 dwarf2_read_index (struct objfile *objfile)
2099 {
2100   char *addr;
2101   struct mapped_index *map;
2102   offset_type *metadata;
2103   const gdb_byte *cu_list;
2104   const gdb_byte *types_list = NULL;
2105   offset_type version, cu_list_elements;
2106   offset_type types_list_elements = 0;
2107   int i;
2108
2109   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2110     return 0;
2111
2112   /* Older elfutils strip versions could keep the section in the main
2113      executable while splitting it for the separate debug info file.  */
2114   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2115        & SEC_HAS_CONTENTS) == 0)
2116     return 0;
2117
2118   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2119
2120   addr = dwarf2_per_objfile->gdb_index.buffer;
2121   /* Version check.  */
2122   version = MAYBE_SWAP (*(offset_type *) addr);
2123   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2124      causes the index to behave very poorly for certain requests.  Version 3
2125      contained incomplete addrmap.  So, it seems better to just ignore such
2126      indices.  */
2127   if (version < 4)
2128     {
2129       static int warning_printed = 0;
2130       if (!warning_printed)
2131         {
2132           warning (_("Skipping obsolete .gdb_index section in %s."),
2133                    objfile->name);
2134           warning_printed = 1;
2135         }
2136       return 0;
2137     }
2138   /* Index version 4 uses a different hash function than index version
2139      5 and later.
2140
2141      Versions earlier than 6 did not emit psymbols for inlined
2142      functions.  Using these files will cause GDB not to be able to
2143      set breakpoints on inlined functions by name, so we ignore these
2144      indices unless the --use-deprecated-index-sections command line
2145      option was supplied.  */
2146   if (version < 6 && !use_deprecated_index_sections)
2147     {
2148       static int warning_printed = 0;
2149       if (!warning_printed)
2150         {
2151           warning (_("Skipping deprecated .gdb_index section in %s, pass "
2152                      "--use-deprecated-index-sections to use them anyway"),
2153                    objfile->name);
2154           warning_printed = 1;
2155         }
2156       return 0;
2157     }
2158   /* Indexes with higher version than the one supported by GDB may be no
2159      longer backward compatible.  */
2160   if (version > 6)
2161     return 0;
2162
2163   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2164   map->version = version;
2165   map->total_size = dwarf2_per_objfile->gdb_index.size;
2166
2167   metadata = (offset_type *) (addr + sizeof (offset_type));
2168
2169   i = 0;
2170   cu_list = addr + MAYBE_SWAP (metadata[i]);
2171   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2172                       / 8);
2173   ++i;
2174
2175   types_list = addr + MAYBE_SWAP (metadata[i]);
2176   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2177                           - MAYBE_SWAP (metadata[i]))
2178                          / 8);
2179   ++i;
2180
2181   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2182   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2183                              - MAYBE_SWAP (metadata[i]));
2184   ++i;
2185
2186   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2187   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2188                               - MAYBE_SWAP (metadata[i]))
2189                              / (2 * sizeof (offset_type)));
2190   ++i;
2191
2192   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2193
2194   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2195     return 0;
2196
2197   if (types_list_elements)
2198     {
2199       struct dwarf2_section_info *section;
2200
2201       /* We can only handle a single .debug_types when we have an
2202          index.  */
2203       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2204         return 0;
2205
2206       section = VEC_index (dwarf2_section_info_def,
2207                            dwarf2_per_objfile->types, 0);
2208
2209       if (!create_signatured_type_table_from_index (objfile, section,
2210                                                     types_list,
2211                                                     types_list_elements))
2212         return 0;
2213     }
2214
2215   create_addrmap_from_index (objfile, map);
2216
2217   dwarf2_per_objfile->index_table = map;
2218   dwarf2_per_objfile->using_index = 1;
2219   dwarf2_per_objfile->quick_file_names_table =
2220     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2221
2222   return 1;
2223 }
2224
2225 /* A helper for the "quick" functions which sets the global
2226    dwarf2_per_objfile according to OBJFILE.  */
2227
2228 static void
2229 dw2_setup (struct objfile *objfile)
2230 {
2231   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2232   gdb_assert (dwarf2_per_objfile);
2233 }
2234
2235 /* A helper for the "quick" functions which attempts to read the line
2236    table for THIS_CU.  */
2237
2238 static struct quick_file_names *
2239 dw2_get_file_names (struct objfile *objfile,
2240                     struct dwarf2_per_cu_data *this_cu)
2241 {
2242   bfd *abfd = objfile->obfd;
2243   struct line_header *lh;
2244   struct attribute *attr;
2245   struct cleanup *cleanups;
2246   struct die_info *comp_unit_die;
2247   struct dwarf2_section_info* sec;
2248   gdb_byte *info_ptr;
2249   int has_children, i;
2250   struct dwarf2_cu cu;
2251   unsigned int bytes_read;
2252   struct die_reader_specs reader_specs;
2253   char *name, *comp_dir;
2254   void **slot;
2255   struct quick_file_names *qfn;
2256   unsigned int line_offset;
2257
2258   if (this_cu->v.quick->file_names != NULL)
2259     return this_cu->v.quick->file_names;
2260   /* If we know there is no line data, no point in looking again.  */
2261   if (this_cu->v.quick->no_file_data)
2262     return NULL;
2263
2264   init_one_comp_unit (&cu, this_cu);
2265   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2266
2267   if (this_cu->debug_types_section)
2268     sec = this_cu->debug_types_section;
2269   else
2270     sec = &dwarf2_per_objfile->info;
2271   dwarf2_read_section (objfile, sec);
2272   info_ptr = sec->buffer + this_cu->offset.sect_off;
2273
2274   info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2275                                             this_cu->debug_types_section != NULL);
2276
2277   /* Skip dummy compilation units.  */
2278   if (info_ptr >= (sec->buffer + sec->size)
2279       || peek_abbrev_code (abfd, info_ptr) == 0)
2280     {
2281       do_cleanups (cleanups);
2282       return NULL;
2283     }
2284
2285   dwarf2_read_abbrevs (&cu);
2286   make_cleanup (dwarf2_free_abbrev_table, &cu);
2287
2288   init_cu_die_reader (&reader_specs, &cu);
2289   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2290                  &has_children);
2291
2292   lh = NULL;
2293   slot = NULL;
2294   line_offset = 0;
2295   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2296   if (attr)
2297     {
2298       struct quick_file_names find_entry;
2299
2300       line_offset = DW_UNSND (attr);
2301
2302       /* We may have already read in this line header (TU line header sharing).
2303          If we have we're done.  */
2304       find_entry.offset = line_offset;
2305       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2306                              &find_entry, INSERT);
2307       if (*slot != NULL)
2308         {
2309           do_cleanups (cleanups);
2310           this_cu->v.quick->file_names = *slot;
2311           return *slot;
2312         }
2313
2314       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2315     }
2316   if (lh == NULL)
2317     {
2318       do_cleanups (cleanups);
2319       this_cu->v.quick->no_file_data = 1;
2320       return NULL;
2321     }
2322
2323   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2324   qfn->offset = line_offset;
2325   gdb_assert (slot != NULL);
2326   *slot = qfn;
2327
2328   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2329
2330   qfn->num_file_names = lh->num_file_names;
2331   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2332                                    lh->num_file_names * sizeof (char *));
2333   for (i = 0; i < lh->num_file_names; ++i)
2334     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2335   qfn->real_names = NULL;
2336
2337   free_line_header (lh);
2338   do_cleanups (cleanups);
2339
2340   this_cu->v.quick->file_names = qfn;
2341   return qfn;
2342 }
2343
2344 /* A helper for the "quick" functions which computes and caches the
2345    real path for a given file name from the line table.  */
2346
2347 static const char *
2348 dw2_get_real_path (struct objfile *objfile,
2349                    struct quick_file_names *qfn, int index)
2350 {
2351   if (qfn->real_names == NULL)
2352     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2353                                       qfn->num_file_names, sizeof (char *));
2354
2355   if (qfn->real_names[index] == NULL)
2356     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2357
2358   return qfn->real_names[index];
2359 }
2360
2361 static struct symtab *
2362 dw2_find_last_source_symtab (struct objfile *objfile)
2363 {
2364   int index;
2365
2366   dw2_setup (objfile);
2367   index = dwarf2_per_objfile->n_comp_units - 1;
2368   return dw2_instantiate_symtab (dw2_get_cu (index));
2369 }
2370
2371 /* Traversal function for dw2_forget_cached_source_info.  */
2372
2373 static int
2374 dw2_free_cached_file_names (void **slot, void *info)
2375 {
2376   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2377
2378   if (file_data->real_names)
2379     {
2380       int i;
2381
2382       for (i = 0; i < file_data->num_file_names; ++i)
2383         {
2384           xfree ((void*) file_data->real_names[i]);
2385           file_data->real_names[i] = NULL;
2386         }
2387     }
2388
2389   return 1;
2390 }
2391
2392 static void
2393 dw2_forget_cached_source_info (struct objfile *objfile)
2394 {
2395   dw2_setup (objfile);
2396
2397   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2398                           dw2_free_cached_file_names, NULL);
2399 }
2400
2401 /* Helper function for dw2_map_symtabs_matching_filename that expands
2402    the symtabs and calls the iterator.  */
2403
2404 static int
2405 dw2_map_expand_apply (struct objfile *objfile,
2406                       struct dwarf2_per_cu_data *per_cu,
2407                       const char *name,
2408                       const char *full_path, const char *real_path,
2409                       int (*callback) (struct symtab *, void *),
2410                       void *data)
2411 {
2412   struct symtab *last_made = objfile->symtabs;
2413
2414   /* Don't visit already-expanded CUs.  */
2415   if (per_cu->v.quick->symtab)
2416     return 0;
2417
2418   /* This may expand more than one symtab, and we want to iterate over
2419      all of them.  */
2420   dw2_instantiate_symtab (per_cu);
2421
2422   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2423                                     objfile->symtabs, last_made);
2424 }
2425
2426 /* Implementation of the map_symtabs_matching_filename method.  */
2427
2428 static int
2429 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2430                                    const char *full_path, const char *real_path,
2431                                    int (*callback) (struct symtab *, void *),
2432                                    void *data)
2433 {
2434   int i;
2435   const char *name_basename = lbasename (name);
2436   int name_len = strlen (name);
2437   int is_abs = IS_ABSOLUTE_PATH (name);
2438
2439   dw2_setup (objfile);
2440
2441   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2442                    + dwarf2_per_objfile->n_type_units); ++i)
2443     {
2444       int j;
2445       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2446       struct quick_file_names *file_data;
2447
2448       /* We only need to look at symtabs not already expanded.  */
2449       if (per_cu->v.quick->symtab)
2450         continue;
2451
2452       file_data = dw2_get_file_names (objfile, per_cu);
2453       if (file_data == NULL)
2454         continue;
2455
2456       for (j = 0; j < file_data->num_file_names; ++j)
2457         {
2458           const char *this_name = file_data->file_names[j];
2459
2460           if (FILENAME_CMP (name, this_name) == 0
2461               || (!is_abs && compare_filenames_for_search (this_name,
2462                                                            name, name_len)))
2463             {
2464               if (dw2_map_expand_apply (objfile, per_cu,
2465                                         name, full_path, real_path,
2466                                         callback, data))
2467                 return 1;
2468             }
2469
2470           /* Before we invoke realpath, which can get expensive when many
2471              files are involved, do a quick comparison of the basenames.  */
2472           if (! basenames_may_differ
2473               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2474             continue;
2475
2476           if (full_path != NULL)
2477             {
2478               const char *this_real_name = dw2_get_real_path (objfile,
2479                                                               file_data, j);
2480
2481               if (this_real_name != NULL
2482                   && (FILENAME_CMP (full_path, this_real_name) == 0
2483                       || (!is_abs
2484                           && compare_filenames_for_search (this_real_name,
2485                                                            name, name_len))))
2486                 {
2487                   if (dw2_map_expand_apply (objfile, per_cu,
2488                                             name, full_path, real_path,
2489                                             callback, data))
2490                     return 1;
2491                 }
2492             }
2493
2494           if (real_path != NULL)
2495             {
2496               const char *this_real_name = dw2_get_real_path (objfile,
2497                                                               file_data, j);
2498
2499               if (this_real_name != NULL
2500                   && (FILENAME_CMP (real_path, this_real_name) == 0
2501                       || (!is_abs
2502                           && compare_filenames_for_search (this_real_name,
2503                                                            name, name_len))))
2504                 {
2505                   if (dw2_map_expand_apply (objfile, per_cu,
2506                                             name, full_path, real_path,
2507                                             callback, data))
2508                     return 1;
2509                 }
2510             }
2511         }
2512     }
2513
2514   return 0;
2515 }
2516
2517 static struct symtab *
2518 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2519                    const char *name, domain_enum domain)
2520 {
2521   /* We do all the work in the pre_expand_symtabs_matching hook
2522      instead.  */
2523   return NULL;
2524 }
2525
2526 /* A helper function that expands all symtabs that hold an object
2527    named NAME.  */
2528
2529 static void
2530 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2531 {
2532   dw2_setup (objfile);
2533
2534   /* index_table is NULL if OBJF_READNOW.  */
2535   if (dwarf2_per_objfile->index_table)
2536     {
2537       offset_type *vec;
2538
2539       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2540                                     name, &vec))
2541         {
2542           offset_type i, len = MAYBE_SWAP (*vec);
2543           for (i = 0; i < len; ++i)
2544             {
2545               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2546               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2547
2548               dw2_instantiate_symtab (per_cu);
2549             }
2550         }
2551     }
2552 }
2553
2554 static void
2555 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2556                                  enum block_enum block_kind, const char *name,
2557                                  domain_enum domain)
2558 {
2559   dw2_do_expand_symtabs_matching (objfile, name);
2560 }
2561
2562 static void
2563 dw2_print_stats (struct objfile *objfile)
2564 {
2565   int i, count;
2566
2567   dw2_setup (objfile);
2568   count = 0;
2569   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2570                    + dwarf2_per_objfile->n_type_units); ++i)
2571     {
2572       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2573
2574       if (!per_cu->v.quick->symtab)
2575         ++count;
2576     }
2577   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2578 }
2579
2580 static void
2581 dw2_dump (struct objfile *objfile)
2582 {
2583   /* Nothing worth printing.  */
2584 }
2585
2586 static void
2587 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2588               struct section_offsets *delta)
2589 {
2590   /* There's nothing to relocate here.  */
2591 }
2592
2593 static void
2594 dw2_expand_symtabs_for_function (struct objfile *objfile,
2595                                  const char *func_name)
2596 {
2597   dw2_do_expand_symtabs_matching (objfile, func_name);
2598 }
2599
2600 static void
2601 dw2_expand_all_symtabs (struct objfile *objfile)
2602 {
2603   int i;
2604
2605   dw2_setup (objfile);
2606
2607   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2608                    + dwarf2_per_objfile->n_type_units); ++i)
2609     {
2610       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2611
2612       dw2_instantiate_symtab (per_cu);
2613     }
2614 }
2615
2616 static void
2617 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2618                                   const char *filename)
2619 {
2620   int i;
2621
2622   dw2_setup (objfile);
2623
2624   /* We don't need to consider type units here.
2625      This is only called for examining code, e.g. expand_line_sal.
2626      There can be an order of magnitude (or more) more type units
2627      than comp units, and we avoid them if we can.  */
2628
2629   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2630     {
2631       int j;
2632       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2633       struct quick_file_names *file_data;
2634
2635       /* We only need to look at symtabs not already expanded.  */
2636       if (per_cu->v.quick->symtab)
2637         continue;
2638
2639       file_data = dw2_get_file_names (objfile, per_cu);
2640       if (file_data == NULL)
2641         continue;
2642
2643       for (j = 0; j < file_data->num_file_names; ++j)
2644         {
2645           const char *this_name = file_data->file_names[j];
2646           if (FILENAME_CMP (this_name, filename) == 0)
2647             {
2648               dw2_instantiate_symtab (per_cu);
2649               break;
2650             }
2651         }
2652     }
2653 }
2654
2655 static const char *
2656 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2657 {
2658   struct dwarf2_per_cu_data *per_cu;
2659   offset_type *vec;
2660   struct quick_file_names *file_data;
2661
2662   dw2_setup (objfile);
2663
2664   /* index_table is NULL if OBJF_READNOW.  */
2665   if (!dwarf2_per_objfile->index_table)
2666     {
2667       struct symtab *s;
2668
2669       ALL_OBJFILE_SYMTABS (objfile, s)
2670         if (s->primary)
2671           {
2672             struct blockvector *bv = BLOCKVECTOR (s);
2673             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2674             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2675
2676             if (sym)
2677               return sym->symtab->filename;
2678           }
2679       return NULL;
2680     }
2681
2682   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2683                                  name, &vec))
2684     return NULL;
2685
2686   /* Note that this just looks at the very first one named NAME -- but
2687      actually we are looking for a function.  find_main_filename
2688      should be rewritten so that it doesn't require a custom hook.  It
2689      could just use the ordinary symbol tables.  */
2690   /* vec[0] is the length, which must always be >0.  */
2691   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2692
2693   file_data = dw2_get_file_names (objfile, per_cu);
2694   if (file_data == NULL)
2695     return NULL;
2696
2697   return file_data->file_names[file_data->num_file_names - 1];
2698 }
2699
2700 static void
2701 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2702                           struct objfile *objfile, int global,
2703                           int (*callback) (struct block *,
2704                                            struct symbol *, void *),
2705                           void *data, symbol_compare_ftype *match,
2706                           symbol_compare_ftype *ordered_compare)
2707 {
2708   /* Currently unimplemented; used for Ada.  The function can be called if the
2709      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2710      does not look for non-Ada symbols this function should just return.  */
2711 }
2712
2713 static void
2714 dw2_expand_symtabs_matching
2715   (struct objfile *objfile,
2716    int (*file_matcher) (const char *, void *),
2717    int (*name_matcher) (const char *, void *),
2718    enum search_domain kind,
2719    void *data)
2720 {
2721   int i;
2722   offset_type iter;
2723   struct mapped_index *index;
2724
2725   dw2_setup (objfile);
2726
2727   /* index_table is NULL if OBJF_READNOW.  */
2728   if (!dwarf2_per_objfile->index_table)
2729     return;
2730   index = dwarf2_per_objfile->index_table;
2731
2732   if (file_matcher != NULL)
2733     {
2734       struct cleanup *cleanup;
2735       htab_t visited_found, visited_not_found;
2736
2737       visited_found = htab_create_alloc (10,
2738                                          htab_hash_pointer, htab_eq_pointer,
2739                                          NULL, xcalloc, xfree);
2740       cleanup = make_cleanup_htab_delete (visited_found);
2741       visited_not_found = htab_create_alloc (10,
2742                                              htab_hash_pointer, htab_eq_pointer,
2743                                              NULL, xcalloc, xfree);
2744       make_cleanup_htab_delete (visited_not_found);
2745
2746       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2747                        + dwarf2_per_objfile->n_type_units); ++i)
2748         {
2749           int j;
2750           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2751           struct quick_file_names *file_data;
2752           void **slot;
2753
2754           per_cu->v.quick->mark = 0;
2755
2756           /* We only need to look at symtabs not already expanded.  */
2757           if (per_cu->v.quick->symtab)
2758             continue;
2759
2760           file_data = dw2_get_file_names (objfile, per_cu);
2761           if (file_data == NULL)
2762             continue;
2763
2764           if (htab_find (visited_not_found, file_data) != NULL)
2765             continue;
2766           else if (htab_find (visited_found, file_data) != NULL)
2767             {
2768               per_cu->v.quick->mark = 1;
2769               continue;
2770             }
2771
2772           for (j = 0; j < file_data->num_file_names; ++j)
2773             {
2774               if (file_matcher (file_data->file_names[j], data))
2775                 {
2776                   per_cu->v.quick->mark = 1;
2777                   break;
2778                 }
2779             }
2780
2781           slot = htab_find_slot (per_cu->v.quick->mark
2782                                  ? visited_found
2783                                  : visited_not_found,
2784                                  file_data, INSERT);
2785           *slot = file_data;
2786         }
2787
2788       do_cleanups (cleanup);
2789     }
2790
2791   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2792     {
2793       offset_type idx = 2 * iter;
2794       const char *name;
2795       offset_type *vec, vec_len, vec_idx;
2796
2797       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2798         continue;
2799
2800       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2801
2802       if (! (*name_matcher) (name, data))
2803         continue;
2804
2805       /* The name was matched, now expand corresponding CUs that were
2806          marked.  */
2807       vec = (offset_type *) (index->constant_pool
2808                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2809       vec_len = MAYBE_SWAP (vec[0]);
2810       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2811         {
2812           struct dwarf2_per_cu_data *per_cu;
2813
2814           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2815           if (file_matcher == NULL || per_cu->v.quick->mark)
2816             dw2_instantiate_symtab (per_cu);
2817         }
2818     }
2819 }
2820
2821 static struct symtab *
2822 dw2_find_pc_sect_symtab (struct objfile *objfile,
2823                          struct minimal_symbol *msymbol,
2824                          CORE_ADDR pc,
2825                          struct obj_section *section,
2826                          int warn_if_readin)
2827 {
2828   struct dwarf2_per_cu_data *data;
2829
2830   dw2_setup (objfile);
2831
2832   if (!objfile->psymtabs_addrmap)
2833     return NULL;
2834
2835   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2836   if (!data)
2837     return NULL;
2838
2839   if (warn_if_readin && data->v.quick->symtab)
2840     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2841              paddress (get_objfile_arch (objfile), pc));
2842
2843   return dw2_instantiate_symtab (data);
2844 }
2845
2846 static void
2847 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2848                           void *data, int need_fullname)
2849 {
2850   int i;
2851   struct cleanup *cleanup;
2852   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2853                                       NULL, xcalloc, xfree);
2854
2855   cleanup = make_cleanup_htab_delete (visited);
2856   dw2_setup (objfile);
2857
2858   /* We can ignore file names coming from already-expanded CUs.  */
2859   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2860                    + dwarf2_per_objfile->n_type_units); ++i)
2861     {
2862       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2863
2864       if (per_cu->v.quick->symtab)
2865         {
2866           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2867                                         INSERT);
2868
2869           *slot = per_cu->v.quick->file_names;
2870         }
2871     }
2872
2873   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2874                    + dwarf2_per_objfile->n_type_units); ++i)
2875     {
2876       int j;
2877       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2878       struct quick_file_names *file_data;
2879       void **slot;
2880
2881       /* We only need to look at symtabs not already expanded.  */
2882       if (per_cu->v.quick->symtab)
2883         continue;
2884
2885       file_data = dw2_get_file_names (objfile, per_cu);
2886       if (file_data == NULL)
2887         continue;
2888
2889       slot = htab_find_slot (visited, file_data, INSERT);
2890       if (*slot)
2891         {
2892           /* Already visited.  */
2893           continue;
2894         }
2895       *slot = file_data;
2896
2897       for (j = 0; j < file_data->num_file_names; ++j)
2898         {
2899           const char *this_real_name;
2900
2901           if (need_fullname)
2902             this_real_name = dw2_get_real_path (objfile, file_data, j);
2903           else
2904             this_real_name = NULL;
2905           (*fun) (file_data->file_names[j], this_real_name, data);
2906         }
2907     }
2908
2909   do_cleanups (cleanup);
2910 }
2911
2912 static int
2913 dw2_has_symbols (struct objfile *objfile)
2914 {
2915   return 1;
2916 }
2917
2918 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2919 {
2920   dw2_has_symbols,
2921   dw2_find_last_source_symtab,
2922   dw2_forget_cached_source_info,
2923   dw2_map_symtabs_matching_filename,
2924   dw2_lookup_symbol,
2925   dw2_pre_expand_symtabs_matching,
2926   dw2_print_stats,
2927   dw2_dump,
2928   dw2_relocate,
2929   dw2_expand_symtabs_for_function,
2930   dw2_expand_all_symtabs,
2931   dw2_expand_symtabs_with_filename,
2932   dw2_find_symbol_file,
2933   dw2_map_matching_symbols,
2934   dw2_expand_symtabs_matching,
2935   dw2_find_pc_sect_symtab,
2936   dw2_map_symbol_filenames
2937 };
2938
2939 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2940    file will use psymtabs, or 1 if using the GNU index.  */
2941
2942 int
2943 dwarf2_initialize_objfile (struct objfile *objfile)
2944 {
2945   /* If we're about to read full symbols, don't bother with the
2946      indices.  In this case we also don't care if some other debug
2947      format is making psymtabs, because they are all about to be
2948      expanded anyway.  */
2949   if ((objfile->flags & OBJF_READNOW))
2950     {
2951       int i;
2952
2953       dwarf2_per_objfile->using_index = 1;
2954       create_all_comp_units (objfile);
2955       create_all_type_units (objfile);
2956       dwarf2_per_objfile->quick_file_names_table =
2957         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2958
2959       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2960                        + dwarf2_per_objfile->n_type_units); ++i)
2961         {
2962           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2963
2964           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965                                             struct dwarf2_per_cu_quick_data);
2966         }
2967
2968       /* Return 1 so that gdb sees the "quick" functions.  However,
2969          these functions will be no-ops because we will have expanded
2970          all symtabs.  */
2971       return 1;
2972     }
2973
2974   if (dwarf2_read_index (objfile))
2975     return 1;
2976
2977   return 0;
2978 }
2979
2980 \f
2981
2982 /* Build a partial symbol table.  */
2983
2984 void
2985 dwarf2_build_psymtabs (struct objfile *objfile)
2986 {
2987   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2988     {
2989       init_psymbol_list (objfile, 1024);
2990     }
2991
2992   dwarf2_build_psymtabs_hard (objfile);
2993 }
2994
2995 /* Return TRUE if OFFSET is within CU_HEADER.  */
2996
2997 static inline int
2998 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
2999 {
3000   sect_offset bottom = { cu_header->offset.sect_off };
3001   sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3002                        + cu_header->initial_length_size) };
3003
3004   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3005 }
3006
3007 /* Read in the comp unit header information from the debug_info at info_ptr.
3008    NOTE: This leaves members offset, first_die_offset to be filled in
3009    by the caller.  */
3010
3011 static gdb_byte *
3012 read_comp_unit_head (struct comp_unit_head *cu_header,
3013                      gdb_byte *info_ptr, bfd *abfd)
3014 {
3015   int signed_addr;
3016   unsigned int bytes_read;
3017
3018   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3019   cu_header->initial_length_size = bytes_read;
3020   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3021   info_ptr += bytes_read;
3022   cu_header->version = read_2_bytes (abfd, info_ptr);
3023   info_ptr += 2;
3024   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3025                                              &bytes_read);
3026   info_ptr += bytes_read;
3027   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3028   info_ptr += 1;
3029   signed_addr = bfd_get_sign_extend_vma (abfd);
3030   if (signed_addr < 0)
3031     internal_error (__FILE__, __LINE__,
3032                     _("read_comp_unit_head: dwarf from non elf file"));
3033   cu_header->signed_addr_p = signed_addr;
3034
3035   return info_ptr;
3036 }
3037
3038 /* Subroutine of read_and_check_comp_unit_head and
3039    read_and_check_type_unit_head to simplify them.
3040    Perform various error checking on the header.  */
3041
3042 static void
3043 error_check_comp_unit_head (struct comp_unit_head *header,
3044                             struct dwarf2_section_info *section)
3045 {
3046   bfd *abfd = section->asection->owner;
3047   const char *filename = bfd_get_filename (abfd);
3048
3049   if (header->version != 2 && header->version != 3 && header->version != 4)
3050     error (_("Dwarf Error: wrong version in compilation unit header "
3051            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3052            filename);
3053
3054   if (header->abbrev_offset.sect_off
3055       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3056                               &dwarf2_per_objfile->abbrev))
3057     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3058            "(offset 0x%lx + 6) [in module %s]"),
3059            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3060            filename);
3061
3062   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3063      avoid potential 32-bit overflow.  */
3064   if (((unsigned long) header->offset.sect_off
3065        + header->length + header->initial_length_size)
3066       > section->size)
3067     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3068            "(offset 0x%lx + 0) [in module %s]"),
3069            (long) header->length, (long) header->offset.sect_off,
3070            filename);
3071 }
3072
3073 /* Read in a CU/TU header and perform some basic error checking.
3074    The contents of the header are stored in HEADER.
3075    The result is a pointer to the start of the first DIE.  */
3076
3077 static gdb_byte *
3078 read_and_check_comp_unit_head (struct comp_unit_head *header,
3079                                struct dwarf2_section_info *section,
3080                                gdb_byte *info_ptr,
3081                                int is_debug_types_section)
3082 {
3083   gdb_byte *beg_of_comp_unit = info_ptr;
3084   bfd *abfd = section->asection->owner;
3085
3086   header->offset.sect_off = beg_of_comp_unit - section->buffer;
3087
3088   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3089
3090   /* If we're reading a type unit, skip over the signature and
3091      type_offset fields.  */
3092   if (is_debug_types_section)
3093     info_ptr += 8 /*signature*/ + header->offset_size;
3094
3095   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3096
3097   error_check_comp_unit_head (header, section);
3098
3099   return info_ptr;
3100 }
3101
3102 /* Read in the types comp unit header information from .debug_types entry at
3103    types_ptr.  The result is a pointer to one past the end of the header.  */
3104
3105 static gdb_byte *
3106 read_and_check_type_unit_head (struct comp_unit_head *header,
3107                                struct dwarf2_section_info *section,
3108                                gdb_byte *info_ptr,
3109                                ULONGEST *signature, cu_offset *type_offset)
3110 {
3111   gdb_byte *beg_of_comp_unit = info_ptr;
3112   bfd *abfd = section->asection->owner;
3113
3114   header->offset.sect_off = beg_of_comp_unit - section->buffer;
3115
3116   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3117
3118   /* If we're reading a type unit, skip over the signature and
3119      type_offset fields.  */
3120   if (signature != NULL)
3121     *signature = read_8_bytes (abfd, info_ptr);
3122   info_ptr += 8;
3123   if (type_offset != NULL)
3124     type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
3125   info_ptr += header->offset_size;
3126
3127   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3128
3129   error_check_comp_unit_head (header, section);
3130
3131   return info_ptr;
3132 }
3133
3134 /* Allocate a new partial symtab for file named NAME and mark this new
3135    partial symtab as being an include of PST.  */
3136
3137 static void
3138 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3139                                struct objfile *objfile)
3140 {
3141   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3142
3143   subpst->section_offsets = pst->section_offsets;
3144   subpst->textlow = 0;
3145   subpst->texthigh = 0;
3146
3147   subpst->dependencies = (struct partial_symtab **)
3148     obstack_alloc (&objfile->objfile_obstack,
3149                    sizeof (struct partial_symtab *));
3150   subpst->dependencies[0] = pst;
3151   subpst->number_of_dependencies = 1;
3152
3153   subpst->globals_offset = 0;
3154   subpst->n_global_syms = 0;
3155   subpst->statics_offset = 0;
3156   subpst->n_static_syms = 0;
3157   subpst->symtab = NULL;
3158   subpst->read_symtab = pst->read_symtab;
3159   subpst->readin = 0;
3160
3161   /* No private part is necessary for include psymtabs.  This property
3162      can be used to differentiate between such include psymtabs and
3163      the regular ones.  */
3164   subpst->read_symtab_private = NULL;
3165 }
3166
3167 /* Read the Line Number Program data and extract the list of files
3168    included by the source file represented by PST.  Build an include
3169    partial symtab for each of these included files.  */
3170
3171 static void
3172 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3173                                struct die_info *die,
3174                                struct partial_symtab *pst)
3175 {
3176   struct objfile *objfile = cu->objfile;
3177   bfd *abfd = objfile->obfd;
3178   struct line_header *lh = NULL;
3179   struct attribute *attr;
3180
3181   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3182   if (attr)
3183     {
3184       unsigned int line_offset = DW_UNSND (attr);
3185
3186       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3187     }
3188   if (lh == NULL)
3189     return;  /* No linetable, so no includes.  */
3190
3191   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3192   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3193
3194   free_line_header (lh);
3195 }
3196
3197 static hashval_t
3198 hash_signatured_type (const void *item)
3199 {
3200   const struct signatured_type *sig_type = item;
3201
3202   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3203   return sig_type->signature;
3204 }
3205
3206 static int
3207 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3208 {
3209   const struct signatured_type *lhs = item_lhs;
3210   const struct signatured_type *rhs = item_rhs;
3211
3212   return lhs->signature == rhs->signature;
3213 }
3214
3215 /* Allocate a hash table for signatured types.  */
3216
3217 static htab_t
3218 allocate_signatured_type_table (struct objfile *objfile)
3219 {
3220   return htab_create_alloc_ex (41,
3221                                hash_signatured_type,
3222                                eq_signatured_type,
3223                                NULL,
3224                                &objfile->objfile_obstack,
3225                                hashtab_obstack_allocate,
3226                                dummy_obstack_deallocate);
3227 }
3228
3229 /* A helper function to add a signatured type CU to a table.  */
3230
3231 static int
3232 add_signatured_type_cu_to_table (void **slot, void *datum)
3233 {
3234   struct signatured_type *sigt = *slot;
3235   struct dwarf2_per_cu_data ***datap = datum;
3236
3237   **datap = &sigt->per_cu;
3238   ++*datap;
3239
3240   return 1;
3241 }
3242
3243 /* Create the hash table of all entries in the .debug_types section(s).
3244    The result is zero if there are no .debug_types sections,
3245    otherwise non-zero.  */
3246
3247 static int
3248 create_all_type_units (struct objfile *objfile)
3249 {
3250   htab_t types_htab = NULL;
3251   struct dwarf2_per_cu_data **iter;
3252   int ix;
3253   struct dwarf2_section_info *section;
3254
3255   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3256     {
3257       dwarf2_per_objfile->signatured_types = NULL;
3258       return 0;
3259     }
3260
3261   for (ix = 0;
3262        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3263                     ix, section);
3264        ++ix)
3265     {
3266       gdb_byte *info_ptr, *end_ptr;
3267
3268       dwarf2_read_section (objfile, section);
3269       info_ptr = section->buffer;
3270
3271       if (info_ptr == NULL)
3272         continue;
3273
3274       if (types_htab == NULL)
3275         types_htab = allocate_signatured_type_table (objfile);
3276
3277       if (dwarf2_die_debug)
3278         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3279
3280       end_ptr = info_ptr + section->size;
3281       while (info_ptr < end_ptr)
3282         {
3283           sect_offset offset;
3284           cu_offset type_offset;
3285           ULONGEST signature;
3286           struct signatured_type *sig_type;
3287           void **slot;
3288           gdb_byte *ptr = info_ptr;
3289           struct comp_unit_head header;
3290
3291           offset.sect_off = ptr - section->buffer;
3292
3293           /* We need to read the type's signature in order to build the hash
3294              table, but we don't need anything else just yet.  */
3295
3296           ptr = read_and_check_type_unit_head (&header, section, ptr,
3297                                                &signature, &type_offset);
3298
3299           /* Skip dummy type units.  */
3300           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3301             {
3302               info_ptr = info_ptr + header.initial_length_size + header.length;
3303               continue;
3304             }
3305
3306           sig_type = obstack_alloc (&objfile->objfile_obstack, sizeof (*sig_type));
3307           memset (sig_type, 0, sizeof (*sig_type));
3308           sig_type->signature = signature;
3309           sig_type->type_offset = type_offset;
3310           sig_type->per_cu.objfile = objfile;
3311           sig_type->per_cu.debug_types_section = section;
3312           sig_type->per_cu.offset = offset;
3313
3314           slot = htab_find_slot (types_htab, sig_type, INSERT);
3315           gdb_assert (slot != NULL);
3316           if (*slot != NULL)
3317             {
3318               const struct signatured_type *dup_sig = *slot;
3319
3320               complaint (&symfile_complaints,
3321                          _("debug type entry at offset 0x%x is duplicate to the "
3322                            "entry at offset 0x%x, signature 0x%s"),
3323                          offset.sect_off, dup_sig->per_cu.offset.sect_off,
3324                          phex (signature, sizeof (signature)));
3325               gdb_assert (signature == dup_sig->signature);
3326             }
3327           *slot = sig_type;
3328
3329           if (dwarf2_die_debug)
3330             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3331                                 offset.sect_off,
3332                                 phex (signature, sizeof (signature)));
3333
3334           info_ptr = info_ptr + header.initial_length_size + header.length;
3335         }
3336     }
3337
3338   dwarf2_per_objfile->signatured_types = types_htab;
3339
3340   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3341   dwarf2_per_objfile->all_type_units
3342     = obstack_alloc (&objfile->objfile_obstack,
3343                      dwarf2_per_objfile->n_type_units
3344                      * sizeof (struct dwarf2_per_cu_data *));
3345   iter = &dwarf2_per_objfile->all_type_units[0];
3346   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3347   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3348               == dwarf2_per_objfile->n_type_units);
3349
3350   return 1;
3351 }
3352
3353 /* Lookup a signature based type.
3354    Returns NULL if signature SIG is not present in the table.  */
3355
3356 static struct signatured_type *
3357 lookup_signatured_type (ULONGEST sig)
3358 {
3359   struct signatured_type find_entry, *entry;
3360
3361   if (dwarf2_per_objfile->signatured_types == NULL)
3362     {
3363       complaint (&symfile_complaints,
3364                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3365       return 0;
3366     }
3367
3368   find_entry.signature = sig;
3369   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3370   return entry;
3371 }
3372
3373 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3374
3375 static void
3376 init_cu_die_reader (struct die_reader_specs *reader,
3377                     struct dwarf2_cu *cu)
3378 {
3379   reader->abfd = cu->objfile->obfd;
3380   reader->cu = cu;
3381   if (cu->per_cu->debug_types_section)
3382     {
3383       gdb_assert (cu->per_cu->debug_types_section->readin);
3384       reader->buffer = cu->per_cu->debug_types_section->buffer;
3385     }
3386   else
3387     {
3388       gdb_assert (dwarf2_per_objfile->info.readin);
3389       reader->buffer = dwarf2_per_objfile->info.buffer;
3390     }
3391 }
3392
3393 /* Find the base address of the compilation unit for range lists and
3394    location lists.  It will normally be specified by DW_AT_low_pc.
3395    In DWARF-3 draft 4, the base address could be overridden by
3396    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3397    compilation units with discontinuous ranges.  */
3398
3399 static void
3400 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3401 {
3402   struct attribute *attr;
3403
3404   cu->base_known = 0;
3405   cu->base_address = 0;
3406
3407   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3408   if (attr)
3409     {
3410       cu->base_address = DW_ADDR (attr);
3411       cu->base_known = 1;
3412     }
3413   else
3414     {
3415       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3416       if (attr)
3417         {
3418           cu->base_address = DW_ADDR (attr);
3419           cu->base_known = 1;
3420         }
3421     }
3422 }
3423
3424 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3425    to combine the common parts.
3426    Process compilation unit THIS_CU for a psymtab.
3427    SECTION is the section the CU/TU comes from,
3428    either .debug_info or .debug_types.  */
3429
3430 static void
3431 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3432                            struct dwarf2_section_info *section,
3433                            int is_debug_types_section)
3434 {
3435   struct objfile *objfile = this_cu->objfile;
3436   bfd *abfd = objfile->obfd;
3437   gdb_byte *buffer = section->buffer;
3438   gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
3439   unsigned int buffer_size = section->size;
3440   gdb_byte *beg_of_comp_unit = info_ptr;
3441   struct die_info *comp_unit_die;
3442   struct partial_symtab *pst;
3443   CORE_ADDR baseaddr;
3444   struct cleanup *back_to_inner;
3445   struct dwarf2_cu cu;
3446   int has_children, has_pc_info;
3447   struct attribute *attr;
3448   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3449   struct die_reader_specs reader_specs;
3450   const char *filename;
3451
3452   /* If this compilation unit was already read in, free the
3453      cached copy in order to read it in again.  This is
3454      necessary because we skipped some symbols when we first
3455      read in the compilation unit (see load_partial_dies).
3456      This problem could be avoided, but the benefit is
3457      unclear.  */
3458   if (this_cu->cu != NULL)
3459     free_one_cached_comp_unit (this_cu->cu);
3460
3461   /* Note that this is a pointer to our stack frame, being
3462      added to a global data structure.  It will be cleaned up
3463      in free_stack_comp_unit when we finish with this
3464      compilation unit.  */
3465   init_one_comp_unit (&cu, this_cu);
3466   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3467
3468   info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3469                                             is_debug_types_section);
3470
3471   /* Skip dummy compilation units.  */
3472   if (info_ptr >= buffer + buffer_size
3473       || peek_abbrev_code (abfd, info_ptr) == 0)
3474     {
3475       do_cleanups (back_to_inner);
3476       return;
3477     }
3478
3479   cu.list_in_scope = &file_symbols;
3480
3481   /* Read the abbrevs for this compilation unit into a table.  */
3482   dwarf2_read_abbrevs (&cu);
3483   make_cleanup (dwarf2_free_abbrev_table, &cu);
3484
3485   /* Read the compilation unit die.  */
3486   init_cu_die_reader (&reader_specs, &cu);
3487   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3488                             &has_children);
3489
3490   if (is_debug_types_section)
3491     {
3492       /* LENGTH has not been set yet for type units.  */
3493       gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
3494       this_cu->length = cu.header.length + cu.header.initial_length_size;
3495     }
3496   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3497     {
3498       do_cleanups (back_to_inner);
3499       return;
3500     }
3501
3502   prepare_one_comp_unit (&cu, comp_unit_die);
3503
3504   /* Allocate a new partial symbol table structure.  */
3505   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3506   if (attr == NULL || !DW_STRING (attr))
3507     filename = "";
3508   else
3509     filename = DW_STRING (attr);
3510   pst = start_psymtab_common (objfile, objfile->section_offsets,
3511                               filename,
3512                               /* TEXTLOW and TEXTHIGH are set below.  */
3513                               0,
3514                               objfile->global_psymbols.next,
3515                               objfile->static_psymbols.next);
3516   pst->psymtabs_addrmap_supported = 1;
3517
3518   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3519   if (attr != NULL)
3520     pst->dirname = DW_STRING (attr);
3521
3522   pst->read_symtab_private = this_cu;
3523
3524   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3525
3526   /* Store the function that reads in the rest of the symbol table.  */
3527   pst->read_symtab = dwarf2_psymtab_to_symtab;
3528
3529   this_cu->v.psymtab = pst;
3530
3531   dwarf2_find_base_address (comp_unit_die, &cu);
3532
3533   /* Possibly set the default values of LOWPC and HIGHPC from
3534      `DW_AT_ranges'.  */
3535   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3536                                       &best_highpc, &cu, pst);
3537   if (has_pc_info == 1 && best_lowpc < best_highpc)
3538     /* Store the contiguous range if it is not empty; it can be empty for
3539        CUs with no code.  */
3540     addrmap_set_empty (objfile->psymtabs_addrmap,
3541                        best_lowpc + baseaddr,
3542                        best_highpc + baseaddr - 1, pst);
3543
3544   /* Check if comp unit has_children.
3545      If so, read the rest of the partial symbols from this comp unit.
3546      If not, there's no more debug_info for this comp unit.  */
3547   if (has_children)
3548     {
3549       struct partial_die_info *first_die;
3550       CORE_ADDR lowpc, highpc;
3551
3552       lowpc = ((CORE_ADDR) -1);
3553       highpc = ((CORE_ADDR) 0);
3554
3555       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3556
3557       scan_partial_symbols (first_die, &lowpc, &highpc,
3558                             ! has_pc_info, &cu);
3559
3560       /* If we didn't find a lowpc, set it to highpc to avoid
3561          complaints from `maint check'.  */
3562       if (lowpc == ((CORE_ADDR) -1))
3563         lowpc = highpc;
3564
3565       /* If the compilation unit didn't have an explicit address range,
3566          then use the information extracted from its child dies.  */
3567       if (! has_pc_info)
3568         {
3569           best_lowpc = lowpc;
3570           best_highpc = highpc;
3571         }
3572     }
3573   pst->textlow = best_lowpc + baseaddr;
3574   pst->texthigh = best_highpc + baseaddr;
3575
3576   pst->n_global_syms = objfile->global_psymbols.next -
3577     (objfile->global_psymbols.list + pst->globals_offset);
3578   pst->n_static_syms = objfile->static_psymbols.next -
3579     (objfile->static_psymbols.list + pst->statics_offset);
3580   sort_pst_symbols (pst);
3581
3582   if (is_debug_types_section)
3583     {
3584       /* It's not clear we want to do anything with stmt lists here.
3585          Waiting to see what gcc ultimately does.  */
3586     }
3587   else
3588     {
3589       /* Get the list of files included in the current compilation unit,
3590          and build a psymtab for each of them.  */
3591       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3592     }
3593
3594   do_cleanups (back_to_inner);
3595 }
3596
3597 /* Traversal function for htab_traverse_noresize.
3598    Process one .debug_types comp-unit.  */
3599
3600 static int
3601 process_type_comp_unit (void **slot, void *info)
3602 {
3603   struct signatured_type *entry = (struct signatured_type *) *slot;
3604   struct dwarf2_per_cu_data *this_cu;
3605
3606   gdb_assert (info == NULL);
3607   this_cu = &entry->per_cu;
3608
3609   gdb_assert (this_cu->debug_types_section->readin);
3610   process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3611
3612   return 1;
3613 }
3614
3615 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3616    Build partial symbol tables for the .debug_types comp-units.  */
3617
3618 static void
3619 build_type_psymtabs (struct objfile *objfile)
3620 {
3621   if (! create_all_type_units (objfile))
3622     return;
3623
3624   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3625                           process_type_comp_unit, NULL);
3626 }
3627
3628 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3629
3630 static void
3631 psymtabs_addrmap_cleanup (void *o)
3632 {
3633   struct objfile *objfile = o;
3634
3635   objfile->psymtabs_addrmap = NULL;
3636 }
3637
3638 /* Build the partial symbol table by doing a quick pass through the
3639    .debug_info and .debug_abbrev sections.  */
3640
3641 static void
3642 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3643 {
3644   struct cleanup *back_to, *addrmap_cleanup;
3645   struct obstack temp_obstack;
3646   int i;
3647
3648   dwarf2_per_objfile->reading_partial_symbols = 1;
3649
3650   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3651
3652   /* Any cached compilation units will be linked by the per-objfile
3653      read_in_chain.  Make sure to free them when we're done.  */
3654   back_to = make_cleanup (free_cached_comp_units, NULL);
3655
3656   build_type_psymtabs (objfile);
3657
3658   create_all_comp_units (objfile);
3659
3660   /* Create a temporary address map on a temporary obstack.  We later
3661      copy this to the final obstack.  */
3662   obstack_init (&temp_obstack);
3663   make_cleanup_obstack_free (&temp_obstack);
3664   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3665   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3666
3667   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3668     {
3669       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3670
3671       process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3672     }
3673
3674   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3675                                                     &objfile->objfile_obstack);
3676   discard_cleanups (addrmap_cleanup);
3677
3678   do_cleanups (back_to);
3679 }
3680
3681 /* Load the partial DIEs for a secondary CU into memory.  */
3682
3683 static void
3684 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3685 {
3686   struct objfile *objfile = this_cu->objfile;
3687   bfd *abfd = objfile->obfd;
3688   gdb_byte *info_ptr;
3689   struct die_info *comp_unit_die;
3690   struct dwarf2_cu *cu;
3691   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3692   int has_children;
3693   struct die_reader_specs reader_specs;
3694   int read_cu = 0;
3695   struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3696
3697   gdb_assert (! this_cu->debug_types_section);
3698
3699   gdb_assert (section->readin);
3700   info_ptr = section->buffer + this_cu->offset.sect_off;
3701
3702   if (this_cu->cu == NULL)
3703     {
3704       cu = xmalloc (sizeof (*cu));
3705       init_one_comp_unit (cu, this_cu);
3706
3707       read_cu = 1;
3708
3709       /* If an error occurs while loading, release our storage.  */
3710       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3711
3712       info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3713                                                 0);
3714
3715       /* Skip dummy compilation units.  */
3716       if (info_ptr >= (section->buffer + section->size)
3717           || peek_abbrev_code (abfd, info_ptr) == 0)
3718         {
3719           do_cleanups (free_cu_cleanup);
3720           return;
3721         }
3722     }
3723   else
3724     {
3725       cu = this_cu->cu;
3726       info_ptr += cu->header.first_die_offset.cu_off;
3727     }
3728
3729   /* Read the abbrevs for this compilation unit into a table.  */
3730   gdb_assert (cu->dwarf2_abbrevs == NULL);
3731   dwarf2_read_abbrevs (cu);
3732   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3733
3734   /* Read the compilation unit die.  */
3735   init_cu_die_reader (&reader_specs, cu);
3736   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3737                             &has_children);
3738
3739   prepare_one_comp_unit (cu, comp_unit_die);
3740
3741   /* Check if comp unit has_children.
3742      If so, read the rest of the partial symbols from this comp unit.
3743      If not, there's no more debug_info for this comp unit.  */
3744   if (has_children)
3745     load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3746
3747   do_cleanups (free_abbrevs_cleanup);
3748
3749   if (read_cu)
3750     {
3751       /* We've successfully allocated this compilation unit.  Let our
3752          caller clean it up when finished with it.  */
3753       discard_cleanups (free_cu_cleanup);
3754
3755       /* Link this CU into read_in_chain.  */
3756       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3757       dwarf2_per_objfile->read_in_chain = this_cu;
3758     }
3759 }
3760
3761 /* Create a list of all compilation units in OBJFILE.
3762    This is only done for -readnow and building partial symtabs.  */
3763
3764 static void
3765 create_all_comp_units (struct objfile *objfile)
3766 {
3767   int n_allocated;
3768   int n_comp_units;
3769   struct dwarf2_per_cu_data **all_comp_units;
3770   gdb_byte *info_ptr;
3771
3772   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3773   info_ptr = dwarf2_per_objfile->info.buffer;
3774
3775   n_comp_units = 0;
3776   n_allocated = 10;
3777   all_comp_units = xmalloc (n_allocated
3778                             * sizeof (struct dwarf2_per_cu_data *));
3779
3780   while (info_ptr < dwarf2_per_objfile->info.buffer
3781          + dwarf2_per_objfile->info.size)
3782     {
3783       unsigned int length, initial_length_size;
3784       struct dwarf2_per_cu_data *this_cu;
3785       sect_offset offset;
3786
3787       offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
3788
3789       /* Read just enough information to find out where the next
3790          compilation unit is.  */
3791       length = read_initial_length (objfile->obfd, info_ptr,
3792                                     &initial_length_size);
3793
3794       /* Save the compilation unit for later lookup.  */
3795       this_cu = obstack_alloc (&objfile->objfile_obstack,
3796                                sizeof (struct dwarf2_per_cu_data));
3797       memset (this_cu, 0, sizeof (*this_cu));
3798       this_cu->offset = offset;
3799       this_cu->length = length + initial_length_size;
3800       this_cu->objfile = objfile;
3801
3802       if (n_comp_units == n_allocated)
3803         {
3804           n_allocated *= 2;
3805           all_comp_units = xrealloc (all_comp_units,
3806                                      n_allocated
3807                                      * sizeof (struct dwarf2_per_cu_data *));
3808         }
3809       all_comp_units[n_comp_units++] = this_cu;
3810
3811       info_ptr = info_ptr + this_cu->length;
3812     }
3813
3814   dwarf2_per_objfile->all_comp_units
3815     = obstack_alloc (&objfile->objfile_obstack,
3816                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3817   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3818           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3819   xfree (all_comp_units);
3820   dwarf2_per_objfile->n_comp_units = n_comp_units;
3821 }
3822
3823 /* Process all loaded DIEs for compilation unit CU, starting at
3824    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3825    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3826    DW_AT_ranges).  If NEED_PC is set, then this function will set
3827    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3828    and record the covered ranges in the addrmap.  */
3829
3830 static void
3831 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3832                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3833 {
3834   struct partial_die_info *pdi;
3835
3836   /* Now, march along the PDI's, descending into ones which have
3837      interesting children but skipping the children of the other ones,
3838      until we reach the end of the compilation unit.  */
3839
3840   pdi = first_die;
3841
3842   while (pdi != NULL)
3843     {
3844       fixup_partial_die (pdi, cu);
3845
3846       /* Anonymous namespaces or modules have no name but have interesting
3847          children, so we need to look at them.  Ditto for anonymous
3848          enums.  */
3849
3850       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3851           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3852         {
3853           switch (pdi->tag)
3854             {
3855             case DW_TAG_subprogram:
3856               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3857               break;
3858             case DW_TAG_constant:
3859             case DW_TAG_variable:
3860             case DW_TAG_typedef:
3861             case DW_TAG_union_type:
3862               if (!pdi->is_declaration)
3863                 {
3864                   add_partial_symbol (pdi, cu);
3865                 }
3866               break;
3867             case DW_TAG_class_type:
3868             case DW_TAG_interface_type:
3869             case DW_TAG_structure_type:
3870               if (!pdi->is_declaration)
3871                 {
3872                   add_partial_symbol (pdi, cu);
3873                 }
3874               break;
3875             case DW_TAG_enumeration_type:
3876               if (!pdi->is_declaration)
3877                 add_partial_enumeration (pdi, cu);
3878               break;
3879             case DW_TAG_base_type:
3880             case DW_TAG_subrange_type:
3881               /* File scope base type definitions are added to the partial
3882                  symbol table.  */
3883               add_partial_symbol (pdi, cu);
3884               break;
3885             case DW_TAG_namespace:
3886               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3887               break;
3888             case DW_TAG_module:
3889               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3890               break;
3891             default:
3892               break;
3893             }
3894         }
3895
3896       /* If the die has a sibling, skip to the sibling.  */
3897
3898       pdi = pdi->die_sibling;
3899     }
3900 }
3901
3902 /* Functions used to compute the fully scoped name of a partial DIE.
3903
3904    Normally, this is simple.  For C++, the parent DIE's fully scoped
3905    name is concatenated with "::" and the partial DIE's name.  For
3906    Java, the same thing occurs except that "." is used instead of "::".
3907    Enumerators are an exception; they use the scope of their parent
3908    enumeration type, i.e. the name of the enumeration type is not
3909    prepended to the enumerator.
3910
3911    There are two complexities.  One is DW_AT_specification; in this
3912    case "parent" means the parent of the target of the specification,
3913    instead of the direct parent of the DIE.  The other is compilers
3914    which do not emit DW_TAG_namespace; in this case we try to guess
3915    the fully qualified name of structure types from their members'
3916    linkage names.  This must be done using the DIE's children rather
3917    than the children of any DW_AT_specification target.  We only need
3918    to do this for structures at the top level, i.e. if the target of
3919    any DW_AT_specification (if any; otherwise the DIE itself) does not
3920    have a parent.  */
3921
3922 /* Compute the scope prefix associated with PDI's parent, in
3923    compilation unit CU.  The result will be allocated on CU's
3924    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3925    field.  NULL is returned if no prefix is necessary.  */
3926 static char *
3927 partial_die_parent_scope (struct partial_die_info *pdi,
3928                           struct dwarf2_cu *cu)
3929 {
3930   char *grandparent_scope;
3931   struct partial_die_info *parent, *real_pdi;
3932
3933   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3934      then this means the parent of the specification DIE.  */
3935
3936   real_pdi = pdi;
3937   while (real_pdi->has_specification)
3938     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3939
3940   parent = real_pdi->die_parent;
3941   if (parent == NULL)
3942     return NULL;
3943
3944   if (parent->scope_set)
3945     return parent->scope;
3946
3947   fixup_partial_die (parent, cu);
3948
3949   grandparent_scope = partial_die_parent_scope (parent, cu);
3950
3951   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3952      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3953      Work around this problem here.  */
3954   if (cu->language == language_cplus
3955       && parent->tag == DW_TAG_namespace
3956       && strcmp (parent->name, "::") == 0
3957       && grandparent_scope == NULL)
3958     {
3959       parent->scope = NULL;
3960       parent->scope_set = 1;
3961       return NULL;
3962     }
3963
3964   if (pdi->tag == DW_TAG_enumerator)
3965     /* Enumerators should not get the name of the enumeration as a prefix.  */
3966     parent->scope = grandparent_scope;
3967   else if (parent->tag == DW_TAG_namespace
3968       || parent->tag == DW_TAG_module
3969       || parent->tag == DW_TAG_structure_type
3970       || parent->tag == DW_TAG_class_type
3971       || parent->tag == DW_TAG_interface_type
3972       || parent->tag == DW_TAG_union_type
3973       || parent->tag == DW_TAG_enumeration_type)
3974     {
3975       if (grandparent_scope == NULL)
3976         parent->scope = parent->name;
3977       else
3978         parent->scope = typename_concat (&cu->comp_unit_obstack,
3979                                          grandparent_scope,
3980                                          parent->name, 0, cu);
3981     }
3982   else
3983     {
3984       /* FIXME drow/2004-04-01: What should we be doing with
3985          function-local names?  For partial symbols, we should probably be
3986          ignoring them.  */
3987       complaint (&symfile_complaints,
3988                  _("unhandled containing DIE tag %d for DIE at %d"),
3989                  parent->tag, pdi->offset.sect_off);
3990       parent->scope = grandparent_scope;
3991     }
3992
3993   parent->scope_set = 1;
3994   return parent->scope;
3995 }
3996
3997 /* Return the fully scoped name associated with PDI, from compilation unit
3998    CU.  The result will be allocated with malloc.  */
3999 static char *
4000 partial_die_full_name (struct partial_die_info *pdi,
4001                        struct dwarf2_cu *cu)
4002 {
4003   char *parent_scope;
4004
4005   /* If this is a template instantiation, we can not work out the
4006      template arguments from partial DIEs.  So, unfortunately, we have
4007      to go through the full DIEs.  At least any work we do building
4008      types here will be reused if full symbols are loaded later.  */
4009   if (pdi->has_template_arguments)
4010     {
4011       fixup_partial_die (pdi, cu);
4012
4013       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4014         {
4015           struct die_info *die;
4016           struct attribute attr;
4017           struct dwarf2_cu *ref_cu = cu;
4018
4019           /* DW_FORM_ref_addr is using section offset.  */
4020           attr.name = 0;
4021           attr.form = DW_FORM_ref_addr;
4022           attr.u.addr = pdi->offset.sect_off;
4023           die = follow_die_ref (NULL, &attr, &ref_cu);
4024
4025           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4026         }
4027     }
4028
4029   parent_scope = partial_die_parent_scope (pdi, cu);
4030   if (parent_scope == NULL)
4031     return NULL;
4032   else
4033     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4034 }
4035
4036 static void
4037 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4038 {
4039   struct objfile *objfile = cu->objfile;
4040   CORE_ADDR addr = 0;
4041   char *actual_name = NULL;
4042   CORE_ADDR baseaddr;
4043   int built_actual_name = 0;
4044
4045   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4046
4047   actual_name = partial_die_full_name (pdi, cu);
4048   if (actual_name)
4049     built_actual_name = 1;
4050
4051   if (actual_name == NULL)
4052     actual_name = pdi->name;
4053
4054   switch (pdi->tag)
4055     {
4056     case DW_TAG_subprogram:
4057       if (pdi->is_external || cu->language == language_ada)
4058         {
4059           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4060              of the global scope.  But in Ada, we want to be able to access
4061              nested procedures globally.  So all Ada subprograms are stored
4062              in the global scope.  */
4063           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4064              mst_text, objfile); */
4065           add_psymbol_to_list (actual_name, strlen (actual_name),
4066                                built_actual_name,
4067                                VAR_DOMAIN, LOC_BLOCK,
4068                                &objfile->global_psymbols,
4069                                0, pdi->lowpc + baseaddr,
4070                                cu->language, objfile);
4071         }
4072       else
4073         {
4074           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4075              mst_file_text, objfile); */
4076           add_psymbol_to_list (actual_name, strlen (actual_name),
4077                                built_actual_name,
4078                                VAR_DOMAIN, LOC_BLOCK,
4079                                &objfile->static_psymbols,
4080                                0, pdi->lowpc + baseaddr,
4081                                cu->language, objfile);
4082         }
4083       break;
4084     case DW_TAG_constant:
4085       {
4086         struct psymbol_allocation_list *list;
4087
4088         if (pdi->is_external)
4089           list = &objfile->global_psymbols;
4090         else
4091           list = &objfile->static_psymbols;
4092         add_psymbol_to_list (actual_name, strlen (actual_name),
4093                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4094                              list, 0, 0, cu->language, objfile);
4095       }
4096       break;
4097     case DW_TAG_variable:
4098       if (pdi->locdesc)
4099         addr = decode_locdesc (pdi->locdesc, cu);
4100
4101       if (pdi->locdesc
4102           && addr == 0
4103           && !dwarf2_per_objfile->has_section_at_zero)
4104         {
4105           /* A global or static variable may also have been stripped
4106              out by the linker if unused, in which case its address
4107              will be nullified; do not add such variables into partial
4108              symbol table then.  */
4109         }
4110       else if (pdi->is_external)
4111         {
4112           /* Global Variable.
4113              Don't enter into the minimal symbol tables as there is
4114              a minimal symbol table entry from the ELF symbols already.
4115              Enter into partial symbol table if it has a location
4116              descriptor or a type.
4117              If the location descriptor is missing, new_symbol will create
4118              a LOC_UNRESOLVED symbol, the address of the variable will then
4119              be determined from the minimal symbol table whenever the variable
4120              is referenced.
4121              The address for the partial symbol table entry is not
4122              used by GDB, but it comes in handy for debugging partial symbol
4123              table building.  */
4124
4125           if (pdi->locdesc || pdi->has_type)
4126             add_psymbol_to_list (actual_name, strlen (actual_name),
4127                                  built_actual_name,
4128                                  VAR_DOMAIN, LOC_STATIC,
4129                                  &objfile->global_psymbols,
4130                                  0, addr + baseaddr,
4131                                  cu->language, objfile);
4132         }
4133       else
4134         {
4135           /* Static Variable.  Skip symbols without location descriptors.  */
4136           if (pdi->locdesc == NULL)
4137             {
4138               if (built_actual_name)
4139                 xfree (actual_name);
4140               return;
4141             }
4142           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4143              mst_file_data, objfile); */
4144           add_psymbol_to_list (actual_name, strlen (actual_name),
4145                                built_actual_name,
4146                                VAR_DOMAIN, LOC_STATIC,
4147                                &objfile->static_psymbols,
4148                                0, addr + baseaddr,
4149                                cu->language, objfile);
4150         }
4151       break;
4152     case DW_TAG_typedef:
4153     case DW_TAG_base_type:
4154     case DW_TAG_subrange_type:
4155       add_psymbol_to_list (actual_name, strlen (actual_name),
4156                            built_actual_name,
4157                            VAR_DOMAIN, LOC_TYPEDEF,
4158                            &objfile->static_psymbols,
4159                            0, (CORE_ADDR) 0, cu->language, objfile);
4160       break;
4161     case DW_TAG_namespace:
4162       add_psymbol_to_list (actual_name, strlen (actual_name),
4163                            built_actual_name,
4164                            VAR_DOMAIN, LOC_TYPEDEF,
4165                            &objfile->global_psymbols,
4166                            0, (CORE_ADDR) 0, cu->language, objfile);
4167       break;
4168     case DW_TAG_class_type:
4169     case DW_TAG_interface_type:
4170     case DW_TAG_structure_type:
4171     case DW_TAG_union_type:
4172     case DW_TAG_enumeration_type:
4173       /* Skip external references.  The DWARF standard says in the section
4174          about "Structure, Union, and Class Type Entries": "An incomplete
4175          structure, union or class type is represented by a structure,
4176          union or class entry that does not have a byte size attribute
4177          and that has a DW_AT_declaration attribute."  */
4178       if (!pdi->has_byte_size && pdi->is_declaration)
4179         {
4180           if (built_actual_name)
4181             xfree (actual_name);
4182           return;
4183         }
4184
4185       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4186          static vs. global.  */
4187       add_psymbol_to_list (actual_name, strlen (actual_name),
4188                            built_actual_name,
4189                            STRUCT_DOMAIN, LOC_TYPEDEF,
4190                            (cu->language == language_cplus
4191                             || cu->language == language_java)
4192                            ? &objfile->global_psymbols
4193                            : &objfile->static_psymbols,
4194                            0, (CORE_ADDR) 0, cu->language, objfile);
4195
4196       break;
4197     case DW_TAG_enumerator:
4198       add_psymbol_to_list (actual_name, strlen (actual_name),
4199                            built_actual_name,
4200                            VAR_DOMAIN, LOC_CONST,
4201                            (cu->language == language_cplus
4202                             || cu->language == language_java)
4203                            ? &objfile->global_psymbols
4204                            : &objfile->static_psymbols,
4205                            0, (CORE_ADDR) 0, cu->language, objfile);
4206       break;
4207     default:
4208       break;
4209     }
4210
4211   if (built_actual_name)
4212     xfree (actual_name);
4213 }
4214
4215 /* Read a partial die corresponding to a namespace; also, add a symbol
4216    corresponding to that namespace to the symbol table.  NAMESPACE is
4217    the name of the enclosing namespace.  */
4218
4219 static void
4220 add_partial_namespace (struct partial_die_info *pdi,
4221                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4222                        int need_pc, struct dwarf2_cu *cu)
4223 {
4224   /* Add a symbol for the namespace.  */
4225
4226   add_partial_symbol (pdi, cu);
4227
4228   /* Now scan partial symbols in that namespace.  */
4229
4230   if (pdi->has_children)
4231     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4232 }
4233
4234 /* Read a partial die corresponding to a Fortran module.  */
4235
4236 static void
4237 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4238                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4239 {
4240   /* Now scan partial symbols in that module.  */
4241
4242   if (pdi->has_children)
4243     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4244 }
4245
4246 /* Read a partial die corresponding to a subprogram and create a partial
4247    symbol for that subprogram.  When the CU language allows it, this
4248    routine also defines a partial symbol for each nested subprogram
4249    that this subprogram contains.
4250
4251    DIE my also be a lexical block, in which case we simply search
4252    recursively for suprograms defined inside that lexical block.
4253    Again, this is only performed when the CU language allows this
4254    type of definitions.  */
4255
4256 static void
4257 add_partial_subprogram (struct partial_die_info *pdi,
4258                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4259                         int need_pc, struct dwarf2_cu *cu)
4260 {
4261   if (pdi->tag == DW_TAG_subprogram)
4262     {
4263       if (pdi->has_pc_info)
4264         {
4265           if (pdi->lowpc < *lowpc)
4266             *lowpc = pdi->lowpc;
4267           if (pdi->highpc > *highpc)
4268             *highpc = pdi->highpc;
4269           if (need_pc)
4270             {
4271               CORE_ADDR baseaddr;
4272               struct objfile *objfile = cu->objfile;
4273
4274               baseaddr = ANOFFSET (objfile->section_offsets,
4275                                    SECT_OFF_TEXT (objfile));
4276               addrmap_set_empty (objfile->psymtabs_addrmap,
4277                                  pdi->lowpc + baseaddr,
4278                                  pdi->highpc - 1 + baseaddr,
4279                                  cu->per_cu->v.psymtab);
4280             }
4281         }
4282
4283       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4284         {
4285           if (!pdi->is_declaration)
4286             /* Ignore subprogram DIEs that do not have a name, they are
4287                illegal.  Do not emit a complaint at this point, we will
4288                do so when we convert this psymtab into a symtab.  */
4289             if (pdi->name)
4290               add_partial_symbol (pdi, cu);
4291         }
4292     }
4293
4294   if (! pdi->has_children)
4295     return;
4296
4297   if (cu->language == language_ada)
4298     {
4299       pdi = pdi->die_child;
4300       while (pdi != NULL)
4301         {
4302           fixup_partial_die (pdi, cu);
4303           if (pdi->tag == DW_TAG_subprogram
4304               || pdi->tag == DW_TAG_lexical_block)
4305             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4306           pdi = pdi->die_sibling;
4307         }
4308     }
4309 }
4310
4311 /* Read a partial die corresponding to an enumeration type.  */
4312
4313 static void
4314 add_partial_enumeration (struct partial_die_info *enum_pdi,
4315                          struct dwarf2_cu *cu)
4316 {
4317   struct partial_die_info *pdi;
4318
4319   if (enum_pdi->name != NULL)
4320     add_partial_symbol (enum_pdi, cu);
4321
4322   pdi = enum_pdi->die_child;
4323   while (pdi)
4324     {
4325       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4326         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4327       else
4328         add_partial_symbol (pdi, cu);
4329       pdi = pdi->die_sibling;
4330     }
4331 }
4332
4333 /* Return the initial uleb128 in the die at INFO_PTR.  */
4334
4335 static unsigned int
4336 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4337 {
4338   unsigned int bytes_read;
4339
4340   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4341 }
4342
4343 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4344    Return the corresponding abbrev, or NULL if the number is zero (indicating
4345    an empty DIE).  In either case *BYTES_READ will be set to the length of
4346    the initial number.  */
4347
4348 static struct abbrev_info *
4349 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4350                  struct dwarf2_cu *cu)
4351 {
4352   bfd *abfd = cu->objfile->obfd;
4353   unsigned int abbrev_number;
4354   struct abbrev_info *abbrev;
4355
4356   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4357
4358   if (abbrev_number == 0)
4359     return NULL;
4360
4361   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4362   if (!abbrev)
4363     {
4364       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4365              abbrev_number, bfd_get_filename (abfd));
4366     }
4367
4368   return abbrev;
4369 }
4370
4371 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4372    Returns a pointer to the end of a series of DIEs, terminated by an empty
4373    DIE.  Any children of the skipped DIEs will also be skipped.  */
4374
4375 static gdb_byte *
4376 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4377 {
4378   struct abbrev_info *abbrev;
4379   unsigned int bytes_read;
4380
4381   while (1)
4382     {
4383       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4384       if (abbrev == NULL)
4385         return info_ptr + bytes_read;
4386       else
4387         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4388     }
4389 }
4390
4391 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4392    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4393    abbrev corresponding to that skipped uleb128 should be passed in
4394    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4395    children.  */
4396
4397 static gdb_byte *
4398 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4399               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4400 {
4401   unsigned int bytes_read;
4402   struct attribute attr;
4403   bfd *abfd = cu->objfile->obfd;
4404   unsigned int form, i;
4405
4406   for (i = 0; i < abbrev->num_attrs; i++)
4407     {
4408       /* The only abbrev we care about is DW_AT_sibling.  */
4409       if (abbrev->attrs[i].name == DW_AT_sibling)
4410         {
4411           read_attribute (&attr, &abbrev->attrs[i],
4412                           abfd, info_ptr, cu);
4413           if (attr.form == DW_FORM_ref_addr)
4414             complaint (&symfile_complaints,
4415                        _("ignoring absolute DW_AT_sibling"));
4416           else
4417             return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4418         }
4419
4420       /* If it isn't DW_AT_sibling, skip this attribute.  */
4421       form = abbrev->attrs[i].form;
4422     skip_attribute:
4423       switch (form)
4424         {
4425         case DW_FORM_ref_addr:
4426           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4427              and later it is offset sized.  */
4428           if (cu->header.version == 2)
4429             info_ptr += cu->header.addr_size;
4430           else
4431             info_ptr += cu->header.offset_size;
4432           break;
4433         case DW_FORM_addr:
4434           info_ptr += cu->header.addr_size;
4435           break;
4436         case DW_FORM_data1:
4437         case DW_FORM_ref1:
4438         case DW_FORM_flag:
4439           info_ptr += 1;
4440           break;
4441         case DW_FORM_flag_present:
4442           break;
4443         case DW_FORM_data2:
4444         case DW_FORM_ref2:
4445           info_ptr += 2;
4446           break;
4447         case DW_FORM_data4:
4448         case DW_FORM_ref4:
4449           info_ptr += 4;
4450           break;
4451         case DW_FORM_data8:
4452         case DW_FORM_ref8:
4453         case DW_FORM_ref_sig8:
4454           info_ptr += 8;
4455           break;
4456         case DW_FORM_string:
4457           read_direct_string (abfd, info_ptr, &bytes_read);
4458           info_ptr += bytes_read;
4459           break;
4460         case DW_FORM_sec_offset:
4461         case DW_FORM_strp:
4462           info_ptr += cu->header.offset_size;
4463           break;
4464         case DW_FORM_exprloc:
4465         case DW_FORM_block:
4466           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4467           info_ptr += bytes_read;
4468           break;
4469         case DW_FORM_block1:
4470           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4471           break;
4472         case DW_FORM_block2:
4473           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4474           break;
4475         case DW_FORM_block4:
4476           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4477           break;
4478         case DW_FORM_sdata:
4479         case DW_FORM_udata:
4480         case DW_FORM_ref_udata:
4481           info_ptr = skip_leb128 (abfd, info_ptr);
4482           break;
4483         case DW_FORM_indirect:
4484           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4485           info_ptr += bytes_read;
4486           /* We need to continue parsing from here, so just go back to
4487              the top.  */
4488           goto skip_attribute;
4489
4490         default:
4491           error (_("Dwarf Error: Cannot handle %s "
4492                    "in DWARF reader [in module %s]"),
4493                  dwarf_form_name (form),
4494                  bfd_get_filename (abfd));
4495         }
4496     }
4497
4498   if (abbrev->has_children)
4499     return skip_children (buffer, info_ptr, cu);
4500   else
4501     return info_ptr;
4502 }
4503
4504 /* Locate ORIG_PDI's sibling.
4505    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4506    in BUFFER.  */
4507
4508 static gdb_byte *
4509 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4510                     gdb_byte *buffer, gdb_byte *info_ptr,
4511                     bfd *abfd, struct dwarf2_cu *cu)
4512 {
4513   /* Do we know the sibling already?  */
4514
4515   if (orig_pdi->sibling)
4516     return orig_pdi->sibling;
4517
4518   /* Are there any children to deal with?  */
4519
4520   if (!orig_pdi->has_children)
4521     return info_ptr;
4522
4523   /* Skip the children the long way.  */
4524
4525   return skip_children (buffer, info_ptr, cu);
4526 }
4527
4528 /* Expand this partial symbol table into a full symbol table.  */
4529
4530 static void
4531 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4532 {
4533   if (pst != NULL)
4534     {
4535       if (pst->readin)
4536         {
4537           warning (_("bug: psymtab for %s is already read in."),
4538                    pst->filename);
4539         }
4540       else
4541         {
4542           if (info_verbose)
4543             {
4544               printf_filtered (_("Reading in symbols for %s..."),
4545                                pst->filename);
4546               gdb_flush (gdb_stdout);
4547             }
4548
4549           /* Restore our global data.  */
4550           dwarf2_per_objfile = objfile_data (pst->objfile,
4551                                              dwarf2_objfile_data_key);
4552
4553           /* If this psymtab is constructed from a debug-only objfile, the
4554              has_section_at_zero flag will not necessarily be correct.  We
4555              can get the correct value for this flag by looking at the data
4556              associated with the (presumably stripped) associated objfile.  */
4557           if (pst->objfile->separate_debug_objfile_backlink)
4558             {
4559               struct dwarf2_per_objfile *dpo_backlink
4560                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4561                                 dwarf2_objfile_data_key);
4562
4563               dwarf2_per_objfile->has_section_at_zero
4564                 = dpo_backlink->has_section_at_zero;
4565             }
4566
4567           dwarf2_per_objfile->reading_partial_symbols = 0;
4568
4569           psymtab_to_symtab_1 (pst);
4570
4571           /* Finish up the debug error message.  */
4572           if (info_verbose)
4573             printf_filtered (_("done.\n"));
4574         }
4575     }
4576 }
4577 \f
4578 /* Reading in full CUs.  */
4579
4580 /* Add PER_CU to the queue.  */
4581
4582 static void
4583 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4584 {
4585   struct dwarf2_queue_item *item;
4586
4587   per_cu->queued = 1;
4588   item = xmalloc (sizeof (*item));
4589   item->per_cu = per_cu;
4590   item->next = NULL;
4591
4592   if (dwarf2_queue == NULL)
4593     dwarf2_queue = item;
4594   else
4595     dwarf2_queue_tail->next = item;
4596
4597   dwarf2_queue_tail = item;
4598 }
4599
4600 /* Process the queue.  */
4601
4602 static void
4603 process_queue (void)
4604 {
4605   struct dwarf2_queue_item *item, *next_item;
4606
4607   /* The queue starts out with one item, but following a DIE reference
4608      may load a new CU, adding it to the end of the queue.  */
4609   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4610     {
4611       if (dwarf2_per_objfile->using_index
4612           ? !item->per_cu->v.quick->symtab
4613           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4614         process_full_comp_unit (item->per_cu);
4615
4616       item->per_cu->queued = 0;
4617       next_item = item->next;
4618       xfree (item);
4619     }
4620
4621   dwarf2_queue_tail = NULL;
4622 }
4623
4624 /* Free all allocated queue entries.  This function only releases anything if
4625    an error was thrown; if the queue was processed then it would have been
4626    freed as we went along.  */
4627
4628 static void
4629 dwarf2_release_queue (void *dummy)
4630 {
4631   struct dwarf2_queue_item *item, *last;
4632
4633   item = dwarf2_queue;
4634   while (item)
4635     {
4636       /* Anything still marked queued is likely to be in an
4637          inconsistent state, so discard it.  */
4638       if (item->per_cu->queued)
4639         {
4640           if (item->per_cu->cu != NULL)
4641             free_one_cached_comp_unit (item->per_cu->cu);
4642           item->per_cu->queued = 0;
4643         }
4644
4645       last = item;
4646       item = item->next;
4647       xfree (last);
4648     }
4649
4650   dwarf2_queue = dwarf2_queue_tail = NULL;
4651 }
4652
4653 /* Read in full symbols for PST, and anything it depends on.  */
4654
4655 static void
4656 psymtab_to_symtab_1 (struct partial_symtab *pst)
4657 {
4658   struct dwarf2_per_cu_data *per_cu;
4659   struct cleanup *back_to;
4660   int i;
4661
4662   for (i = 0; i < pst->number_of_dependencies; i++)
4663     if (!pst->dependencies[i]->readin)
4664       {
4665         /* Inform about additional files that need to be read in.  */
4666         if (info_verbose)
4667           {
4668             /* FIXME: i18n: Need to make this a single string.  */
4669             fputs_filtered (" ", gdb_stdout);
4670             wrap_here ("");
4671             fputs_filtered ("and ", gdb_stdout);
4672             wrap_here ("");
4673             printf_filtered ("%s...", pst->dependencies[i]->filename);
4674             wrap_here ("");     /* Flush output.  */
4675             gdb_flush (gdb_stdout);
4676           }
4677         psymtab_to_symtab_1 (pst->dependencies[i]);
4678       }
4679
4680   per_cu = pst->read_symtab_private;
4681
4682   if (per_cu == NULL)
4683     {
4684       /* It's an include file, no symbols to read for it.
4685          Everything is in the parent symtab.  */
4686       pst->readin = 1;
4687       return;
4688     }
4689
4690   dw2_do_instantiate_symtab (per_cu);
4691 }
4692
4693 /* Load the DIEs associated with PER_CU into memory.  */
4694
4695 static void
4696 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4697 {
4698   struct objfile *objfile = per_cu->objfile;
4699   bfd *abfd = objfile->obfd;
4700   struct dwarf2_cu *cu;
4701   sect_offset offset;
4702   gdb_byte *info_ptr, *beg_of_comp_unit;
4703   struct cleanup *free_cu_cleanup = NULL;
4704   struct attribute *attr;
4705   int read_cu = 0;
4706
4707   gdb_assert (! per_cu->debug_types_section);
4708
4709   /* Set local variables from the partial symbol table info.  */
4710   offset = per_cu->offset;
4711
4712   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4713   info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
4714   beg_of_comp_unit = info_ptr;
4715
4716   if (per_cu->cu == NULL)
4717     {
4718       cu = xmalloc (sizeof (*cu));
4719       init_one_comp_unit (cu, per_cu);
4720
4721       read_cu = 1;
4722
4723       /* If an error occurs while loading, release our storage.  */
4724       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4725
4726       /* Read in the comp_unit header.  */
4727       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4728
4729       /* Skip dummy compilation units.  */
4730       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4731                        + dwarf2_per_objfile->info.size)
4732           || peek_abbrev_code (abfd, info_ptr) == 0)
4733         {
4734           do_cleanups (free_cu_cleanup);
4735           return;
4736         }
4737
4738       /* Complete the cu_header.  */
4739       cu->header.offset = offset;
4740       cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4741     }
4742   else
4743     {
4744       cu = per_cu->cu;
4745       info_ptr += cu->header.first_die_offset.cu_off;
4746     }
4747
4748   cu->dies = read_comp_unit (info_ptr, cu);
4749
4750   /* We try not to read any attributes in this function, because not
4751      all CUs needed for references have been loaded yet, and symbol
4752      table processing isn't initialized.  But we have to set the CU language,
4753      or we won't be able to build types correctly.  */
4754   prepare_one_comp_unit (cu, cu->dies);
4755
4756   /* Similarly, if we do not read the producer, we can not apply
4757      producer-specific interpretation.  */
4758   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4759   if (attr)
4760     cu->producer = DW_STRING (attr);
4761
4762   if (read_cu)
4763     {
4764       /* We've successfully allocated this compilation unit.  Let our
4765          caller clean it up when finished with it.  */
4766       discard_cleanups (free_cu_cleanup);
4767
4768       /* Link this CU into read_in_chain.  */
4769       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4770       dwarf2_per_objfile->read_in_chain = per_cu;
4771     }
4772 }
4773
4774 /* Add a DIE to the delayed physname list.  */
4775
4776 static void
4777 add_to_method_list (struct type *type, int fnfield_index, int index,
4778                     const char *name, struct die_info *die,
4779                     struct dwarf2_cu *cu)
4780 {
4781   struct delayed_method_info mi;
4782   mi.type = type;
4783   mi.fnfield_index = fnfield_index;
4784   mi.index = index;
4785   mi.name = name;
4786   mi.die = die;
4787   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4788 }
4789
4790 /* A cleanup for freeing the delayed method list.  */
4791
4792 static void
4793 free_delayed_list (void *ptr)
4794 {
4795   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4796   if (cu->method_list != NULL)
4797     {
4798       VEC_free (delayed_method_info, cu->method_list);
4799       cu->method_list = NULL;
4800     }
4801 }
4802
4803 /* Compute the physnames of any methods on the CU's method list.
4804
4805    The computation of method physnames is delayed in order to avoid the
4806    (bad) condition that one of the method's formal parameters is of an as yet
4807    incomplete type.  */
4808
4809 static void
4810 compute_delayed_physnames (struct dwarf2_cu *cu)
4811 {
4812   int i;
4813   struct delayed_method_info *mi;
4814   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4815     {
4816       const char *physname;
4817       struct fn_fieldlist *fn_flp
4818         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4819       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4820       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4821     }
4822 }
4823
4824 /* Generate full symbol information for PER_CU, whose DIEs have
4825    already been loaded into memory.  */
4826
4827 static void
4828 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4829 {
4830   struct dwarf2_cu *cu = per_cu->cu;
4831   struct objfile *objfile = per_cu->objfile;
4832   CORE_ADDR lowpc, highpc;
4833   struct symtab *symtab;
4834   struct cleanup *back_to, *delayed_list_cleanup;
4835   CORE_ADDR baseaddr;
4836
4837   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4838
4839   buildsym_init ();
4840   back_to = make_cleanup (really_free_pendings, NULL);
4841   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4842
4843   cu->list_in_scope = &file_symbols;
4844
4845   /* Do line number decoding in read_file_scope () */
4846   process_die (cu->dies, cu);
4847
4848   /* Now that we have processed all the DIEs in the CU, all the types 
4849      should be complete, and it should now be safe to compute all of the
4850      physnames.  */
4851   compute_delayed_physnames (cu);
4852   do_cleanups (delayed_list_cleanup);
4853
4854   /* Some compilers don't define a DW_AT_high_pc attribute for the
4855      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4856      it, by scanning the DIE's below the compilation unit.  */
4857   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4858
4859   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4860
4861   if (symtab != NULL)
4862     {
4863       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4864
4865       /* Set symtab language to language from DW_AT_language.  If the
4866          compilation is from a C file generated by language preprocessors, do
4867          not set the language if it was already deduced by start_subfile.  */
4868       if (!(cu->language == language_c && symtab->language != language_c))
4869         symtab->language = cu->language;
4870
4871       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4872          produce DW_AT_location with location lists but it can be possibly
4873          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
4874          there were bugs in prologue debug info, fixed later in GCC-4.5
4875          by "unwind info for epilogues" patch (which is not directly related).
4876
4877          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4878          needed, it would be wrong due to missing DW_AT_producer there.
4879
4880          Still one can confuse GDB by using non-standard GCC compilation
4881          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4882          */ 
4883       if (cu->has_loclist && gcc_4_minor >= 5)
4884         symtab->locations_valid = 1;
4885
4886       if (gcc_4_minor >= 5)
4887         symtab->epilogue_unwind_valid = 1;
4888
4889       symtab->call_site_htab = cu->call_site_htab;
4890     }
4891
4892   if (dwarf2_per_objfile->using_index)
4893     per_cu->v.quick->symtab = symtab;
4894   else
4895     {
4896       struct partial_symtab *pst = per_cu->v.psymtab;
4897       pst->symtab = symtab;
4898       pst->readin = 1;
4899     }
4900
4901   do_cleanups (back_to);
4902 }
4903
4904 /* Process a die and its children.  */
4905
4906 static void
4907 process_die (struct die_info *die, struct dwarf2_cu *cu)
4908 {
4909   switch (die->tag)
4910     {
4911     case DW_TAG_padding:
4912       break;
4913     case DW_TAG_compile_unit:
4914       read_file_scope (die, cu);
4915       break;
4916     case DW_TAG_type_unit:
4917       read_type_unit_scope (die, cu);
4918       break;
4919     case DW_TAG_subprogram:
4920     case DW_TAG_inlined_subroutine:
4921       read_func_scope (die, cu);
4922       break;
4923     case DW_TAG_lexical_block:
4924     case DW_TAG_try_block:
4925     case DW_TAG_catch_block:
4926       read_lexical_block_scope (die, cu);
4927       break;
4928     case DW_TAG_GNU_call_site:
4929       read_call_site_scope (die, cu);
4930       break;
4931     case DW_TAG_class_type:
4932     case DW_TAG_interface_type:
4933     case DW_TAG_structure_type:
4934     case DW_TAG_union_type:
4935       process_structure_scope (die, cu);
4936       break;
4937     case DW_TAG_enumeration_type:
4938       process_enumeration_scope (die, cu);
4939       break;
4940
4941     /* These dies have a type, but processing them does not create
4942        a symbol or recurse to process the children.  Therefore we can
4943        read them on-demand through read_type_die.  */
4944     case DW_TAG_subroutine_type:
4945     case DW_TAG_set_type:
4946     case DW_TAG_array_type:
4947     case DW_TAG_pointer_type:
4948     case DW_TAG_ptr_to_member_type:
4949     case DW_TAG_reference_type:
4950     case DW_TAG_string_type:
4951       break;
4952
4953     case DW_TAG_base_type:
4954     case DW_TAG_subrange_type:
4955     case DW_TAG_typedef:
4956       /* Add a typedef symbol for the type definition, if it has a
4957          DW_AT_name.  */
4958       new_symbol (die, read_type_die (die, cu), cu);
4959       break;
4960     case DW_TAG_common_block:
4961       read_common_block (die, cu);
4962       break;
4963     case DW_TAG_common_inclusion:
4964       break;
4965     case DW_TAG_namespace:
4966       processing_has_namespace_info = 1;
4967       read_namespace (die, cu);
4968       break;
4969     case DW_TAG_module:
4970       processing_has_namespace_info = 1;
4971       read_module (die, cu);
4972       break;
4973     case DW_TAG_imported_declaration:
4974     case DW_TAG_imported_module:
4975       processing_has_namespace_info = 1;
4976       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4977                                  || cu->language != language_fortran))
4978         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4979                    dwarf_tag_name (die->tag));
4980       read_import_statement (die, cu);
4981       break;
4982     default:
4983       new_symbol (die, NULL, cu);
4984       break;
4985     }
4986 }
4987
4988 /* A helper function for dwarf2_compute_name which determines whether DIE
4989    needs to have the name of the scope prepended to the name listed in the
4990    die.  */
4991
4992 static int
4993 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4994 {
4995   struct attribute *attr;
4996
4997   switch (die->tag)
4998     {
4999     case DW_TAG_namespace:
5000     case DW_TAG_typedef:
5001     case DW_TAG_class_type:
5002     case DW_TAG_interface_type:
5003     case DW_TAG_structure_type:
5004     case DW_TAG_union_type:
5005     case DW_TAG_enumeration_type:
5006     case DW_TAG_enumerator:
5007     case DW_TAG_subprogram:
5008     case DW_TAG_member:
5009       return 1;
5010
5011     case DW_TAG_variable:
5012     case DW_TAG_constant:
5013       /* We only need to prefix "globally" visible variables.  These include
5014          any variable marked with DW_AT_external or any variable that
5015          lives in a namespace.  [Variables in anonymous namespaces
5016          require prefixing, but they are not DW_AT_external.]  */
5017
5018       if (dwarf2_attr (die, DW_AT_specification, cu))
5019         {
5020           struct dwarf2_cu *spec_cu = cu;
5021
5022           return die_needs_namespace (die_specification (die, &spec_cu),
5023                                       spec_cu);
5024         }
5025
5026       attr = dwarf2_attr (die, DW_AT_external, cu);
5027       if (attr == NULL && die->parent->tag != DW_TAG_namespace
5028           && die->parent->tag != DW_TAG_module)
5029         return 0;
5030       /* A variable in a lexical block of some kind does not need a
5031          namespace, even though in C++ such variables may be external
5032          and have a mangled name.  */
5033       if (die->parent->tag ==  DW_TAG_lexical_block
5034           || die->parent->tag ==  DW_TAG_try_block
5035           || die->parent->tag ==  DW_TAG_catch_block
5036           || die->parent->tag == DW_TAG_subprogram)
5037         return 0;
5038       return 1;
5039
5040     default:
5041       return 0;
5042     }
5043 }
5044
5045 /* Retrieve the last character from a mem_file.  */
5046
5047 static void
5048 do_ui_file_peek_last (void *object, const char *buffer, long length)
5049 {
5050   char *last_char_p = (char *) object;
5051
5052   if (length > 0)
5053     *last_char_p = buffer[length - 1];
5054 }
5055
5056 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
5057    compute the physname for the object, which include a method's
5058    formal parameters (C++/Java) and return type (Java).
5059
5060    For Ada, return the DIE's linkage name rather than the fully qualified
5061    name.  PHYSNAME is ignored..
5062
5063    The result is allocated on the objfile_obstack and canonicalized.  */
5064
5065 static const char *
5066 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5067                      int physname)
5068 {
5069   struct objfile *objfile = cu->objfile;
5070
5071   if (name == NULL)
5072     name = dwarf2_name (die, cu);
5073
5074   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5075      compute it by typename_concat inside GDB.  */
5076   if (cu->language == language_ada
5077       || (cu->language == language_fortran && physname))
5078     {
5079       /* For Ada unit, we prefer the linkage name over the name, as
5080          the former contains the exported name, which the user expects
5081          to be able to reference.  Ideally, we want the user to be able
5082          to reference this entity using either natural or linkage name,
5083          but we haven't started looking at this enhancement yet.  */
5084       struct attribute *attr;
5085
5086       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5087       if (attr == NULL)
5088         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5089       if (attr && DW_STRING (attr))
5090         return DW_STRING (attr);
5091     }
5092
5093   /* These are the only languages we know how to qualify names in.  */
5094   if (name != NULL
5095       && (cu->language == language_cplus || cu->language == language_java
5096           || cu->language == language_fortran))
5097     {
5098       if (die_needs_namespace (die, cu))
5099         {
5100           long length;
5101           const char *prefix;
5102           struct ui_file *buf;
5103
5104           prefix = determine_prefix (die, cu);
5105           buf = mem_fileopen ();
5106           if (*prefix != '\0')
5107             {
5108               char *prefixed_name = typename_concat (NULL, prefix, name,
5109                                                      physname, cu);
5110
5111               fputs_unfiltered (prefixed_name, buf);
5112               xfree (prefixed_name);
5113             }
5114           else
5115             fputs_unfiltered (name, buf);
5116
5117           /* Template parameters may be specified in the DIE's DW_AT_name, or
5118              as children with DW_TAG_template_type_param or
5119              DW_TAG_value_type_param.  If the latter, add them to the name
5120              here.  If the name already has template parameters, then
5121              skip this step; some versions of GCC emit both, and
5122              it is more efficient to use the pre-computed name.
5123
5124              Something to keep in mind about this process: it is very
5125              unlikely, or in some cases downright impossible, to produce
5126              something that will match the mangled name of a function.
5127              If the definition of the function has the same debug info,
5128              we should be able to match up with it anyway.  But fallbacks
5129              using the minimal symbol, for instance to find a method
5130              implemented in a stripped copy of libstdc++, will not work.
5131              If we do not have debug info for the definition, we will have to
5132              match them up some other way.
5133
5134              When we do name matching there is a related problem with function
5135              templates; two instantiated function templates are allowed to
5136              differ only by their return types, which we do not add here.  */
5137
5138           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5139             {
5140               struct attribute *attr;
5141               struct die_info *child;
5142               int first = 1;
5143
5144               die->building_fullname = 1;
5145
5146               for (child = die->child; child != NULL; child = child->sibling)
5147                 {
5148                   struct type *type;
5149                   long value;
5150                   gdb_byte *bytes;
5151                   struct dwarf2_locexpr_baton *baton;
5152                   struct value *v;
5153
5154                   if (child->tag != DW_TAG_template_type_param
5155                       && child->tag != DW_TAG_template_value_param)
5156                     continue;
5157
5158                   if (first)
5159                     {
5160                       fputs_unfiltered ("<", buf);
5161                       first = 0;
5162                     }
5163                   else
5164                     fputs_unfiltered (", ", buf);
5165
5166                   attr = dwarf2_attr (child, DW_AT_type, cu);
5167                   if (attr == NULL)
5168                     {
5169                       complaint (&symfile_complaints,
5170                                  _("template parameter missing DW_AT_type"));
5171                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5172                       continue;
5173                     }
5174                   type = die_type (child, cu);
5175
5176                   if (child->tag == DW_TAG_template_type_param)
5177                     {
5178                       c_print_type (type, "", buf, -1, 0);
5179                       continue;
5180                     }
5181
5182                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5183                   if (attr == NULL)
5184                     {
5185                       complaint (&symfile_complaints,
5186                                  _("template parameter missing "
5187                                    "DW_AT_const_value"));
5188                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5189                       continue;
5190                     }
5191
5192                   dwarf2_const_value_attr (attr, type, name,
5193                                            &cu->comp_unit_obstack, cu,
5194                                            &value, &bytes, &baton);
5195
5196                   if (TYPE_NOSIGN (type))
5197                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5198                        changed, this can use value_print instead.  */
5199                     c_printchar (value, type, buf);
5200                   else
5201                     {
5202                       struct value_print_options opts;
5203
5204                       if (baton != NULL)
5205                         v = dwarf2_evaluate_loc_desc (type, NULL,
5206                                                       baton->data,
5207                                                       baton->size,
5208                                                       baton->per_cu);
5209                       else if (bytes != NULL)
5210                         {
5211                           v = allocate_value (type);
5212                           memcpy (value_contents_writeable (v), bytes,
5213                                   TYPE_LENGTH (type));
5214                         }
5215                       else
5216                         v = value_from_longest (type, value);
5217
5218                       /* Specify decimal so that we do not depend on
5219                          the radix.  */
5220                       get_formatted_print_options (&opts, 'd');
5221                       opts.raw = 1;
5222                       value_print (v, buf, &opts);
5223                       release_value (v);
5224                       value_free (v);
5225                     }
5226                 }
5227
5228               die->building_fullname = 0;
5229
5230               if (!first)
5231                 {
5232                   /* Close the argument list, with a space if necessary
5233                      (nested templates).  */
5234                   char last_char = '\0';
5235                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5236                   if (last_char == '>')
5237                     fputs_unfiltered (" >", buf);
5238                   else
5239                     fputs_unfiltered (">", buf);
5240                 }
5241             }
5242
5243           /* For Java and C++ methods, append formal parameter type
5244              information, if PHYSNAME.  */
5245
5246           if (physname && die->tag == DW_TAG_subprogram
5247               && (cu->language == language_cplus
5248                   || cu->language == language_java))
5249             {
5250               struct type *type = read_type_die (die, cu);
5251
5252               c_type_print_args (type, buf, 1, cu->language);
5253
5254               if (cu->language == language_java)
5255                 {
5256                   /* For java, we must append the return type to method
5257                      names.  */
5258                   if (die->tag == DW_TAG_subprogram)
5259                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5260                                      0, 0);
5261                 }
5262               else if (cu->language == language_cplus)
5263                 {
5264                   /* Assume that an artificial first parameter is
5265                      "this", but do not crash if it is not.  RealView
5266                      marks unnamed (and thus unused) parameters as
5267                      artificial; there is no way to differentiate
5268                      the two cases.  */
5269                   if (TYPE_NFIELDS (type) > 0
5270                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5271                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5272                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5273                                                                         0))))
5274                     fputs_unfiltered (" const", buf);
5275                 }
5276             }
5277
5278           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5279                                        &length);
5280           ui_file_delete (buf);
5281
5282           if (cu->language == language_cplus)
5283             {
5284               char *cname
5285                 = dwarf2_canonicalize_name (name, cu,
5286                                             &objfile->objfile_obstack);
5287
5288               if (cname != NULL)
5289                 name = cname;
5290             }
5291         }
5292     }
5293
5294   return name;
5295 }
5296
5297 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5298    If scope qualifiers are appropriate they will be added.  The result
5299    will be allocated on the objfile_obstack, or NULL if the DIE does
5300    not have a name.  NAME may either be from a previous call to
5301    dwarf2_name or NULL.
5302
5303    The output string will be canonicalized (if C++/Java).  */
5304
5305 static const char *
5306 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5307 {
5308   return dwarf2_compute_name (name, die, cu, 0);
5309 }
5310
5311 /* Construct a physname for the given DIE in CU.  NAME may either be
5312    from a previous call to dwarf2_name or NULL.  The result will be
5313    allocated on the objfile_objstack or NULL if the DIE does not have a
5314    name.
5315
5316    The output string will be canonicalized (if C++/Java).  */
5317
5318 static const char *
5319 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5320 {
5321   struct objfile *objfile = cu->objfile;
5322   struct attribute *attr;
5323   const char *retval, *mangled = NULL, *canon = NULL;
5324   struct cleanup *back_to;
5325   int need_copy = 1;
5326
5327   /* In this case dwarf2_compute_name is just a shortcut not building anything
5328      on its own.  */
5329   if (!die_needs_namespace (die, cu))
5330     return dwarf2_compute_name (name, die, cu, 1);
5331
5332   back_to = make_cleanup (null_cleanup, NULL);
5333
5334   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5335   if (!attr)
5336     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5337
5338   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5339      has computed.  */
5340   if (attr && DW_STRING (attr))
5341     {
5342       char *demangled;
5343
5344       mangled = DW_STRING (attr);
5345
5346       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5347          type.  It is easier for GDB users to search for such functions as
5348          `name(params)' than `long name(params)'.  In such case the minimal
5349          symbol names do not match the full symbol names but for template
5350          functions there is never a need to look up their definition from their
5351          declaration so the only disadvantage remains the minimal symbol
5352          variant `long name(params)' does not have the proper inferior type.
5353          */
5354
5355       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5356                                             | (cu->language == language_java
5357                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5358                                                : DMGL_RET_DROP)));
5359       if (demangled)
5360         {
5361           make_cleanup (xfree, demangled);
5362           canon = demangled;
5363         }
5364       else
5365         {
5366           canon = mangled;
5367           need_copy = 0;
5368         }
5369     }
5370
5371   if (canon == NULL || check_physname)
5372     {
5373       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5374
5375       if (canon != NULL && strcmp (physname, canon) != 0)
5376         {
5377           /* It may not mean a bug in GDB.  The compiler could also
5378              compute DW_AT_linkage_name incorrectly.  But in such case
5379              GDB would need to be bug-to-bug compatible.  */
5380
5381           complaint (&symfile_complaints,
5382                      _("Computed physname <%s> does not match demangled <%s> "
5383                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5384                      physname, canon, mangled, die->offset.sect_off, objfile->name);
5385
5386           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5387              is available here - over computed PHYSNAME.  It is safer
5388              against both buggy GDB and buggy compilers.  */
5389
5390           retval = canon;
5391         }
5392       else
5393         {
5394           retval = physname;
5395           need_copy = 0;
5396         }
5397     }
5398   else
5399     retval = canon;
5400
5401   if (need_copy)
5402     retval = obsavestring (retval, strlen (retval),
5403                            &objfile->objfile_obstack);
5404
5405   do_cleanups (back_to);
5406   return retval;
5407 }
5408
5409 /* Read the import statement specified by the given die and record it.  */
5410
5411 static void
5412 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5413 {
5414   struct objfile *objfile = cu->objfile;
5415   struct attribute *import_attr;
5416   struct die_info *imported_die, *child_die;
5417   struct dwarf2_cu *imported_cu;
5418   const char *imported_name;
5419   const char *imported_name_prefix;
5420   const char *canonical_name;
5421   const char *import_alias;
5422   const char *imported_declaration = NULL;
5423   const char *import_prefix;
5424   VEC (const_char_ptr) *excludes = NULL;
5425   struct cleanup *cleanups;
5426
5427   char *temp;
5428
5429   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5430   if (import_attr == NULL)
5431     {
5432       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5433                  dwarf_tag_name (die->tag));
5434       return;
5435     }
5436
5437   imported_cu = cu;
5438   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5439   imported_name = dwarf2_name (imported_die, imported_cu);
5440   if (imported_name == NULL)
5441     {
5442       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5443
5444         The import in the following code:
5445         namespace A
5446           {
5447             typedef int B;
5448           }
5449
5450         int main ()
5451           {
5452             using A::B;
5453             B b;
5454             return b;
5455           }
5456
5457         ...
5458          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5459             <52>   DW_AT_decl_file   : 1
5460             <53>   DW_AT_decl_line   : 6
5461             <54>   DW_AT_import      : <0x75>
5462          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5463             <59>   DW_AT_name        : B
5464             <5b>   DW_AT_decl_file   : 1
5465             <5c>   DW_AT_decl_line   : 2
5466             <5d>   DW_AT_type        : <0x6e>
5467         ...
5468          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5469             <76>   DW_AT_byte_size   : 4
5470             <77>   DW_AT_encoding    : 5        (signed)
5471
5472         imports the wrong die ( 0x75 instead of 0x58 ).
5473         This case will be ignored until the gcc bug is fixed.  */
5474       return;
5475     }
5476
5477   /* Figure out the local name after import.  */
5478   import_alias = dwarf2_name (die, cu);
5479
5480   /* Figure out where the statement is being imported to.  */
5481   import_prefix = determine_prefix (die, cu);
5482
5483   /* Figure out what the scope of the imported die is and prepend it
5484      to the name of the imported die.  */
5485   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5486
5487   if (imported_die->tag != DW_TAG_namespace
5488       && imported_die->tag != DW_TAG_module)
5489     {
5490       imported_declaration = imported_name;
5491       canonical_name = imported_name_prefix;
5492     }
5493   else if (strlen (imported_name_prefix) > 0)
5494     {
5495       temp = alloca (strlen (imported_name_prefix)
5496                      + 2 + strlen (imported_name) + 1);
5497       strcpy (temp, imported_name_prefix);
5498       strcat (temp, "::");
5499       strcat (temp, imported_name);
5500       canonical_name = temp;
5501     }
5502   else
5503     canonical_name = imported_name;
5504
5505   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5506
5507   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5508     for (child_die = die->child; child_die && child_die->tag;
5509          child_die = sibling_die (child_die))
5510       {
5511         /* DWARF-4: A Fortran use statement with a “rename list” may be
5512            represented by an imported module entry with an import attribute
5513            referring to the module and owned entries corresponding to those
5514            entities that are renamed as part of being imported.  */
5515
5516         if (child_die->tag != DW_TAG_imported_declaration)
5517           {
5518             complaint (&symfile_complaints,
5519                        _("child DW_TAG_imported_declaration expected "
5520                          "- DIE at 0x%x [in module %s]"),
5521                        child_die->offset.sect_off, objfile->name);
5522             continue;
5523           }
5524
5525         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5526         if (import_attr == NULL)
5527           {
5528             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5529                        dwarf_tag_name (child_die->tag));
5530             continue;
5531           }
5532
5533         imported_cu = cu;
5534         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5535                                               &imported_cu);
5536         imported_name = dwarf2_name (imported_die, imported_cu);
5537         if (imported_name == NULL)
5538           {
5539             complaint (&symfile_complaints,
5540                        _("child DW_TAG_imported_declaration has unknown "
5541                          "imported name - DIE at 0x%x [in module %s]"),
5542                        child_die->offset.sect_off, objfile->name);
5543             continue;
5544           }
5545
5546         VEC_safe_push (const_char_ptr, excludes, imported_name);
5547
5548         process_die (child_die, cu);
5549       }
5550
5551   cp_add_using_directive (import_prefix,
5552                           canonical_name,
5553                           import_alias,
5554                           imported_declaration,
5555                           excludes,
5556                           &objfile->objfile_obstack);
5557
5558   do_cleanups (cleanups);
5559 }
5560
5561 /* Cleanup function for read_file_scope.  */
5562
5563 static void
5564 free_cu_line_header (void *arg)
5565 {
5566   struct dwarf2_cu *cu = arg;
5567
5568   free_line_header (cu->line_header);
5569   cu->line_header = NULL;
5570 }
5571
5572 static void
5573 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5574                          char **name, char **comp_dir)
5575 {
5576   struct attribute *attr;
5577
5578   *name = NULL;
5579   *comp_dir = NULL;
5580
5581   /* Find the filename.  Do not use dwarf2_name here, since the filename
5582      is not a source language identifier.  */
5583   attr = dwarf2_attr (die, DW_AT_name, cu);
5584   if (attr)
5585     {
5586       *name = DW_STRING (attr);
5587     }
5588
5589   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5590   if (attr)
5591     *comp_dir = DW_STRING (attr);
5592   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5593     {
5594       *comp_dir = ldirname (*name);
5595       if (*comp_dir != NULL)
5596         make_cleanup (xfree, *comp_dir);
5597     }
5598   if (*comp_dir != NULL)
5599     {
5600       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5601          directory, get rid of it.  */
5602       char *cp = strchr (*comp_dir, ':');
5603
5604       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5605         *comp_dir = cp + 1;
5606     }
5607
5608   if (*name == NULL)
5609     *name = "<unknown>";
5610 }
5611
5612 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5613    DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5614    COMP_DIR is the compilation directory.
5615    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
5616
5617 static void
5618 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5619                         const char *comp_dir, int want_line_info)
5620 {
5621   struct attribute *attr;
5622   struct objfile *objfile = cu->objfile;
5623   bfd *abfd = objfile->obfd;
5624
5625   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5626   if (attr)
5627     {
5628       unsigned int line_offset = DW_UNSND (attr);
5629       struct line_header *line_header
5630         = dwarf_decode_line_header (line_offset, abfd, cu);
5631
5632       if (line_header)
5633         {
5634           cu->line_header = line_header;
5635           make_cleanup (free_cu_line_header, cu);
5636           dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5637         }
5638     }
5639 }
5640
5641 /* Process DW_TAG_compile_unit.  */
5642
5643 static void
5644 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5645 {
5646   struct objfile *objfile = cu->objfile;
5647   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5648   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5649   CORE_ADDR highpc = ((CORE_ADDR) 0);
5650   struct attribute *attr;
5651   char *name = NULL;
5652   char *comp_dir = NULL;
5653   struct die_info *child_die;
5654   bfd *abfd = objfile->obfd;
5655   CORE_ADDR baseaddr;
5656
5657   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5658
5659   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5660
5661   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5662      from finish_block.  */
5663   if (lowpc == ((CORE_ADDR) -1))
5664     lowpc = highpc;
5665   lowpc += baseaddr;
5666   highpc += baseaddr;
5667
5668   find_file_and_directory (die, cu, &name, &comp_dir);
5669
5670   attr = dwarf2_attr (die, DW_AT_language, cu);
5671   if (attr)
5672     {
5673       set_cu_language (DW_UNSND (attr), cu);
5674     }
5675
5676   attr = dwarf2_attr (die, DW_AT_producer, cu);
5677   if (attr)
5678     cu->producer = DW_STRING (attr);
5679
5680   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5681      standardised yet.  As a workaround for the language detection we fall
5682      back to the DW_AT_producer string.  */
5683   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5684     cu->language = language_opencl;
5685
5686   /* We assume that we're processing GCC output.  */
5687   processing_gcc_compilation = 2;
5688
5689   processing_has_namespace_info = 0;
5690
5691   start_symtab (name, comp_dir, lowpc);
5692   record_debugformat ("DWARF 2");
5693   record_producer (cu->producer);
5694
5695   /* Decode line number information if present.  We do this before
5696      processing child DIEs, so that the line header table is available
5697      for DW_AT_decl_file.  */
5698   handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5699
5700   /* Process all dies in compilation unit.  */
5701   if (die->child != NULL)
5702     {
5703       child_die = die->child;
5704       while (child_die && child_die->tag)
5705         {
5706           process_die (child_die, cu);
5707           child_die = sibling_die (child_die);
5708         }
5709     }
5710
5711   /* Decode macro information, if present.  Dwarf 2 macro information
5712      refers to information in the line number info statement program
5713      header, so we can only read it if we've read the header
5714      successfully.  */
5715   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5716   if (attr && cu->line_header)
5717     {
5718       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5719         complaint (&symfile_complaints,
5720                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5721
5722       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5723                            comp_dir, abfd, cu,
5724                            &dwarf2_per_objfile->macro, 1);
5725     }
5726   else
5727     {
5728       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5729       if (attr && cu->line_header)
5730         {
5731           unsigned int macro_offset = DW_UNSND (attr);
5732
5733           dwarf_decode_macros (cu->line_header, macro_offset,
5734                                comp_dir, abfd, cu,
5735                                &dwarf2_per_objfile->macinfo, 0);
5736         }
5737     }
5738
5739   do_cleanups (back_to);
5740 }
5741
5742 /* Process DW_TAG_type_unit.
5743    For TUs we want to skip the first top level sibling if it's not the
5744    actual type being defined by this TU.  In this case the first top
5745    level sibling is there to provide context only.  */
5746
5747 static void
5748 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5749 {
5750   struct objfile *objfile = cu->objfile;
5751   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5752   CORE_ADDR lowpc;
5753   struct attribute *attr;
5754   char *name = NULL;
5755   char *comp_dir = NULL;
5756   struct die_info *child_die;
5757   bfd *abfd = objfile->obfd;
5758
5759   /* start_symtab needs a low pc, but we don't really have one.
5760      Do what read_file_scope would do in the absence of such info.  */
5761   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5762
5763   /* Find the filename.  Do not use dwarf2_name here, since the filename
5764      is not a source language identifier.  */
5765   attr = dwarf2_attr (die, DW_AT_name, cu);
5766   if (attr)
5767     name = DW_STRING (attr);
5768
5769   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5770   if (attr)
5771     comp_dir = DW_STRING (attr);
5772   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5773     {
5774       comp_dir = ldirname (name);
5775       if (comp_dir != NULL)
5776         make_cleanup (xfree, comp_dir);
5777     }
5778
5779   if (name == NULL)
5780     name = "<unknown>";
5781
5782   attr = dwarf2_attr (die, DW_AT_language, cu);
5783   if (attr)
5784     set_cu_language (DW_UNSND (attr), cu);
5785
5786   /* This isn't technically needed today.  It is done for symmetry
5787      with read_file_scope.  */
5788   attr = dwarf2_attr (die, DW_AT_producer, cu);
5789   if (attr)
5790     cu->producer = DW_STRING (attr);
5791
5792   /* We assume that we're processing GCC output.  */
5793   processing_gcc_compilation = 2;
5794
5795   processing_has_namespace_info = 0;
5796
5797   start_symtab (name, comp_dir, lowpc);
5798   record_debugformat ("DWARF 2");
5799   record_producer (cu->producer);
5800
5801   /* Decode line number information if present.  We do this before
5802      processing child DIEs, so that the line header table is available
5803      for DW_AT_decl_file.
5804      We don't need the pc/line-number mapping for type units.  */
5805   handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5806
5807   /* Process the dies in the type unit.  */
5808   if (die->child == NULL)
5809     {
5810       dump_die_for_error (die);
5811       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5812              bfd_get_filename (abfd));
5813     }
5814
5815   child_die = die->child;
5816
5817   while (child_die && child_die->tag)
5818     {
5819       process_die (child_die, cu);
5820
5821       child_die = sibling_die (child_die);
5822     }
5823
5824   do_cleanups (back_to);
5825 }
5826
5827 /* qsort helper for inherit_abstract_dies.  */
5828
5829 static int
5830 unsigned_int_compar (const void *ap, const void *bp)
5831 {
5832   unsigned int a = *(unsigned int *) ap;
5833   unsigned int b = *(unsigned int *) bp;
5834
5835   return (a > b) - (b > a);
5836 }
5837
5838 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5839    Inherit only the children of the DW_AT_abstract_origin DIE not being
5840    already referenced by DW_AT_abstract_origin from the children of the
5841    current DIE.  */
5842
5843 static void
5844 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5845 {
5846   struct die_info *child_die;
5847   unsigned die_children_count;
5848   /* CU offsets which were referenced by children of the current DIE.  */
5849   sect_offset *offsets;
5850   sect_offset *offsets_end, *offsetp;
5851   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5852   struct die_info *origin_die;
5853   /* Iterator of the ORIGIN_DIE children.  */
5854   struct die_info *origin_child_die;
5855   struct cleanup *cleanups;
5856   struct attribute *attr;
5857   struct dwarf2_cu *origin_cu;
5858   struct pending **origin_previous_list_in_scope;
5859
5860   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5861   if (!attr)
5862     return;
5863
5864   /* Note that following die references may follow to a die in a
5865      different cu.  */
5866
5867   origin_cu = cu;
5868   origin_die = follow_die_ref (die, attr, &origin_cu);
5869
5870   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5871      symbols in.  */
5872   origin_previous_list_in_scope = origin_cu->list_in_scope;
5873   origin_cu->list_in_scope = cu->list_in_scope;
5874
5875   if (die->tag != origin_die->tag
5876       && !(die->tag == DW_TAG_inlined_subroutine
5877            && origin_die->tag == DW_TAG_subprogram))
5878     complaint (&symfile_complaints,
5879                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5880                die->offset.sect_off, origin_die->offset.sect_off);
5881
5882   child_die = die->child;
5883   die_children_count = 0;
5884   while (child_die && child_die->tag)
5885     {
5886       child_die = sibling_die (child_die);
5887       die_children_count++;
5888     }
5889   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5890   cleanups = make_cleanup (xfree, offsets);
5891
5892   offsets_end = offsets;
5893   child_die = die->child;
5894   while (child_die && child_die->tag)
5895     {
5896       /* For each CHILD_DIE, find the corresponding child of
5897          ORIGIN_DIE.  If there is more than one layer of
5898          DW_AT_abstract_origin, follow them all; there shouldn't be,
5899          but GCC versions at least through 4.4 generate this (GCC PR
5900          40573).  */
5901       struct die_info *child_origin_die = child_die;
5902       struct dwarf2_cu *child_origin_cu = cu;
5903
5904       while (1)
5905         {
5906           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5907                               child_origin_cu);
5908           if (attr == NULL)
5909             break;
5910           child_origin_die = follow_die_ref (child_origin_die, attr,
5911                                              &child_origin_cu);
5912         }
5913
5914       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5915          counterpart may exist.  */
5916       if (child_origin_die != child_die)
5917         {
5918           if (child_die->tag != child_origin_die->tag
5919               && !(child_die->tag == DW_TAG_inlined_subroutine
5920                    && child_origin_die->tag == DW_TAG_subprogram))
5921             complaint (&symfile_complaints,
5922                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5923                          "different tags"), child_die->offset.sect_off,
5924                        child_origin_die->offset.sect_off);
5925           if (child_origin_die->parent != origin_die)
5926             complaint (&symfile_complaints,
5927                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5928                          "different parents"), child_die->offset.sect_off,
5929                        child_origin_die->offset.sect_off);
5930           else
5931             *offsets_end++ = child_origin_die->offset;
5932         }
5933       child_die = sibling_die (child_die);
5934     }
5935   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5936          unsigned_int_compar);
5937   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5938     if (offsetp[-1].sect_off == offsetp->sect_off)
5939       complaint (&symfile_complaints,
5940                  _("Multiple children of DIE 0x%x refer "
5941                    "to DIE 0x%x as their abstract origin"),
5942                  die->offset.sect_off, offsetp->sect_off);
5943
5944   offsetp = offsets;
5945   origin_child_die = origin_die->child;
5946   while (origin_child_die && origin_child_die->tag)
5947     {
5948       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5949       while (offsetp < offsets_end
5950              && offsetp->sect_off < origin_child_die->offset.sect_off)
5951         offsetp++;
5952       if (offsetp >= offsets_end
5953           || offsetp->sect_off > origin_child_die->offset.sect_off)
5954         {
5955           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5956           process_die (origin_child_die, origin_cu);
5957         }
5958       origin_child_die = sibling_die (origin_child_die);
5959     }
5960   origin_cu->list_in_scope = origin_previous_list_in_scope;
5961
5962   do_cleanups (cleanups);
5963 }
5964
5965 static void
5966 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5967 {
5968   struct objfile *objfile = cu->objfile;
5969   struct context_stack *new;
5970   CORE_ADDR lowpc;
5971   CORE_ADDR highpc;
5972   struct die_info *child_die;
5973   struct attribute *attr, *call_line, *call_file;
5974   char *name;
5975   CORE_ADDR baseaddr;
5976   struct block *block;
5977   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5978   VEC (symbolp) *template_args = NULL;
5979   struct template_symbol *templ_func = NULL;
5980
5981   if (inlined_func)
5982     {
5983       /* If we do not have call site information, we can't show the
5984          caller of this inlined function.  That's too confusing, so
5985          only use the scope for local variables.  */
5986       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5987       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5988       if (call_line == NULL || call_file == NULL)
5989         {
5990           read_lexical_block_scope (die, cu);
5991           return;
5992         }
5993     }
5994
5995   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5996
5997   name = dwarf2_name (die, cu);
5998
5999   /* Ignore functions with missing or empty names.  These are actually
6000      illegal according to the DWARF standard.  */
6001   if (name == NULL)
6002     {
6003       complaint (&symfile_complaints,
6004                  _("missing name for subprogram DIE at %d"),
6005                  die->offset.sect_off);
6006       return;
6007     }
6008
6009   /* Ignore functions with missing or invalid low and high pc attributes.  */
6010   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6011     {
6012       attr = dwarf2_attr (die, DW_AT_external, cu);
6013       if (!attr || !DW_UNSND (attr))
6014         complaint (&symfile_complaints,
6015                    _("cannot get low and high bounds "
6016                      "for subprogram DIE at %d"),
6017                    die->offset.sect_off);
6018       return;
6019     }
6020
6021   lowpc += baseaddr;
6022   highpc += baseaddr;
6023
6024   /* If we have any template arguments, then we must allocate a
6025      different sort of symbol.  */
6026   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6027     {
6028       if (child_die->tag == DW_TAG_template_type_param
6029           || child_die->tag == DW_TAG_template_value_param)
6030         {
6031           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6032                                        struct template_symbol);
6033           templ_func->base.is_cplus_template_function = 1;
6034           break;
6035         }
6036     }
6037
6038   new = push_context (0, lowpc);
6039   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6040                                (struct symbol *) templ_func);
6041
6042   /* If there is a location expression for DW_AT_frame_base, record
6043      it.  */
6044   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6045   if (attr)
6046     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6047        expression is being recorded directly in the function's symbol
6048        and not in a separate frame-base object.  I guess this hack is
6049        to avoid adding some sort of frame-base adjunct/annex to the
6050        function's symbol :-(.  The problem with doing this is that it
6051        results in a function symbol with a location expression that
6052        has nothing to do with the location of the function, ouch!  The
6053        relationship should be: a function's symbol has-a frame base; a
6054        frame-base has-a location expression.  */
6055     dwarf2_symbol_mark_computed (attr, new->name, cu);
6056
6057   cu->list_in_scope = &local_symbols;
6058
6059   if (die->child != NULL)
6060     {
6061       child_die = die->child;
6062       while (child_die && child_die->tag)
6063         {
6064           if (child_die->tag == DW_TAG_template_type_param
6065               || child_die->tag == DW_TAG_template_value_param)
6066             {
6067               struct symbol *arg = new_symbol (child_die, NULL, cu);
6068
6069               if (arg != NULL)
6070                 VEC_safe_push (symbolp, template_args, arg);
6071             }
6072           else
6073             process_die (child_die, cu);
6074           child_die = sibling_die (child_die);
6075         }
6076     }
6077
6078   inherit_abstract_dies (die, cu);
6079
6080   /* If we have a DW_AT_specification, we might need to import using
6081      directives from the context of the specification DIE.  See the
6082      comment in determine_prefix.  */
6083   if (cu->language == language_cplus
6084       && dwarf2_attr (die, DW_AT_specification, cu))
6085     {
6086       struct dwarf2_cu *spec_cu = cu;
6087       struct die_info *spec_die = die_specification (die, &spec_cu);
6088
6089       while (spec_die)
6090         {
6091           child_die = spec_die->child;
6092           while (child_die && child_die->tag)
6093             {
6094               if (child_die->tag == DW_TAG_imported_module)
6095                 process_die (child_die, spec_cu);
6096               child_die = sibling_die (child_die);
6097             }
6098
6099           /* In some cases, GCC generates specification DIEs that
6100              themselves contain DW_AT_specification attributes.  */
6101           spec_die = die_specification (spec_die, &spec_cu);
6102         }
6103     }
6104
6105   new = pop_context ();
6106   /* Make a block for the local symbols within.  */
6107   block = finish_block (new->name, &local_symbols, new->old_blocks,
6108                         lowpc, highpc, objfile);
6109
6110   /* For C++, set the block's scope.  */
6111   if (cu->language == language_cplus || cu->language == language_fortran)
6112     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6113                         determine_prefix (die, cu),
6114                         processing_has_namespace_info);
6115
6116   /* If we have address ranges, record them.  */
6117   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6118
6119   /* Attach template arguments to function.  */
6120   if (! VEC_empty (symbolp, template_args))
6121     {
6122       gdb_assert (templ_func != NULL);
6123
6124       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6125       templ_func->template_arguments
6126         = obstack_alloc (&objfile->objfile_obstack,
6127                          (templ_func->n_template_arguments
6128                           * sizeof (struct symbol *)));
6129       memcpy (templ_func->template_arguments,
6130               VEC_address (symbolp, template_args),
6131               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6132       VEC_free (symbolp, template_args);
6133     }
6134
6135   /* In C++, we can have functions nested inside functions (e.g., when
6136      a function declares a class that has methods).  This means that
6137      when we finish processing a function scope, we may need to go
6138      back to building a containing block's symbol lists.  */
6139   local_symbols = new->locals;
6140   param_symbols = new->params;
6141   using_directives = new->using_directives;
6142
6143   /* If we've finished processing a top-level function, subsequent
6144      symbols go in the file symbol list.  */
6145   if (outermost_context_p ())
6146     cu->list_in_scope = &file_symbols;
6147 }
6148
6149 /* Process all the DIES contained within a lexical block scope.  Start
6150    a new scope, process the dies, and then close the scope.  */
6151
6152 static void
6153 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6154 {
6155   struct objfile *objfile = cu->objfile;
6156   struct context_stack *new;
6157   CORE_ADDR lowpc, highpc;
6158   struct die_info *child_die;
6159   CORE_ADDR baseaddr;
6160
6161   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6162
6163   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6164   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6165      as multiple lexical blocks?  Handling children in a sane way would
6166      be nasty.  Might be easier to properly extend generic blocks to
6167      describe ranges.  */
6168   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6169     return;
6170   lowpc += baseaddr;
6171   highpc += baseaddr;
6172
6173   push_context (0, lowpc);
6174   if (die->child != NULL)
6175     {
6176       child_die = die->child;
6177       while (child_die && child_die->tag)
6178         {
6179           process_die (child_die, cu);
6180           child_die = sibling_die (child_die);
6181         }
6182     }
6183   new = pop_context ();
6184
6185   if (local_symbols != NULL || using_directives != NULL)
6186     {
6187       struct block *block
6188         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6189                         highpc, objfile);
6190
6191       /* Note that recording ranges after traversing children, as we
6192          do here, means that recording a parent's ranges entails
6193          walking across all its children's ranges as they appear in
6194          the address map, which is quadratic behavior.
6195
6196          It would be nicer to record the parent's ranges before
6197          traversing its children, simply overriding whatever you find
6198          there.  But since we don't even decide whether to create a
6199          block until after we've traversed its children, that's hard
6200          to do.  */
6201       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6202     }
6203   local_symbols = new->locals;
6204   using_directives = new->using_directives;
6205 }
6206
6207 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6208
6209 static void
6210 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6211 {
6212   struct objfile *objfile = cu->objfile;
6213   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6214   CORE_ADDR pc, baseaddr;
6215   struct attribute *attr;
6216   struct call_site *call_site, call_site_local;
6217   void **slot;
6218   int nparams;
6219   struct die_info *child_die;
6220
6221   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6222
6223   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6224   if (!attr)
6225     {
6226       complaint (&symfile_complaints,
6227                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6228                    "DIE 0x%x [in module %s]"),
6229                  die->offset.sect_off, objfile->name);
6230       return;
6231     }
6232   pc = DW_ADDR (attr) + baseaddr;
6233
6234   if (cu->call_site_htab == NULL)
6235     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6236                                                NULL, &objfile->objfile_obstack,
6237                                                hashtab_obstack_allocate, NULL);
6238   call_site_local.pc = pc;
6239   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6240   if (*slot != NULL)
6241     {
6242       complaint (&symfile_complaints,
6243                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6244                    "DIE 0x%x [in module %s]"),
6245                  paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
6246       return;
6247     }
6248
6249   /* Count parameters at the caller.  */
6250
6251   nparams = 0;
6252   for (child_die = die->child; child_die && child_die->tag;
6253        child_die = sibling_die (child_die))
6254     {
6255       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6256         {
6257           complaint (&symfile_complaints,
6258                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6259                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6260                      child_die->tag, child_die->offset.sect_off, objfile->name);
6261           continue;
6262         }
6263
6264       nparams++;
6265     }
6266
6267   call_site = obstack_alloc (&objfile->objfile_obstack,
6268                              (sizeof (*call_site)
6269                               + (sizeof (*call_site->parameter)
6270                                  * (nparams - 1))));
6271   *slot = call_site;
6272   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6273   call_site->pc = pc;
6274
6275   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6276     {
6277       struct die_info *func_die;
6278
6279       /* Skip also over DW_TAG_inlined_subroutine.  */
6280       for (func_die = die->parent;
6281            func_die && func_die->tag != DW_TAG_subprogram
6282            && func_die->tag != DW_TAG_subroutine_type;
6283            func_die = func_die->parent);
6284
6285       /* DW_AT_GNU_all_call_sites is a superset
6286          of DW_AT_GNU_all_tail_call_sites.  */
6287       if (func_die
6288           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6289           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6290         {
6291           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6292              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6293              both the initial caller containing the real return address PC and
6294              the final callee containing the current PC of a chain of tail
6295              calls do not need to have the tail call list complete.  But any
6296              function candidate for a virtual tail call frame searched via
6297              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6298              determined unambiguously.  */
6299         }
6300       else
6301         {
6302           struct type *func_type = NULL;
6303
6304           if (func_die)
6305             func_type = get_die_type (func_die, cu);
6306           if (func_type != NULL)
6307             {
6308               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6309
6310               /* Enlist this call site to the function.  */
6311               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6312               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6313             }
6314           else
6315             complaint (&symfile_complaints,
6316                        _("Cannot find function owning DW_TAG_GNU_call_site "
6317                          "DIE 0x%x [in module %s]"),
6318                        die->offset.sect_off, objfile->name);
6319         }
6320     }
6321
6322   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6323   if (attr == NULL)
6324     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6325   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6326   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6327     /* Keep NULL DWARF_BLOCK.  */;
6328   else if (attr_form_is_block (attr))
6329     {
6330       struct dwarf2_locexpr_baton *dlbaton;
6331
6332       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6333       dlbaton->data = DW_BLOCK (attr)->data;
6334       dlbaton->size = DW_BLOCK (attr)->size;
6335       dlbaton->per_cu = cu->per_cu;
6336
6337       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6338     }
6339   else if (is_ref_attr (attr))
6340     {
6341       struct dwarf2_cu *target_cu = cu;
6342       struct die_info *target_die;
6343
6344       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6345       gdb_assert (target_cu->objfile == objfile);
6346       if (die_is_declaration (target_die, target_cu))
6347         {
6348           const char *target_physname;
6349
6350           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6351           if (target_physname == NULL)
6352             complaint (&symfile_complaints,
6353                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6354                          "physname, for referencing DIE 0x%x [in module %s]"),
6355                        die->offset.sect_off, objfile->name);
6356           else
6357             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6358         }
6359       else
6360         {
6361           CORE_ADDR lowpc;
6362
6363           /* DW_AT_entry_pc should be preferred.  */
6364           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6365             complaint (&symfile_complaints,
6366                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6367                          "low pc, for referencing DIE 0x%x [in module %s]"),
6368                        die->offset.sect_off, objfile->name);
6369           else
6370             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6371         }
6372     }
6373   else
6374     complaint (&symfile_complaints,
6375                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6376                  "block nor reference, for DIE 0x%x [in module %s]"),
6377                die->offset.sect_off, objfile->name);
6378
6379   call_site->per_cu = cu->per_cu;
6380
6381   for (child_die = die->child;
6382        child_die && child_die->tag;
6383        child_die = sibling_die (child_die))
6384     {
6385       struct dwarf2_locexpr_baton *dlbaton;
6386       struct call_site_parameter *parameter;
6387
6388       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6389         {
6390           /* Already printed the complaint above.  */
6391           continue;
6392         }
6393
6394       gdb_assert (call_site->parameter_count < nparams);
6395       parameter = &call_site->parameter[call_site->parameter_count];
6396
6397       /* DW_AT_location specifies the register number.  Value of the data
6398          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6399
6400       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6401       if (!attr || !attr_form_is_block (attr))
6402         {
6403           complaint (&symfile_complaints,
6404                      _("No DW_FORM_block* DW_AT_location for "
6405                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6406                      child_die->offset.sect_off, objfile->name);
6407           continue;
6408         }
6409       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6410                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6411       if (parameter->dwarf_reg == -1
6412           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6413                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6414                                         &parameter->fb_offset))
6415         {
6416           complaint (&symfile_complaints,
6417                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6418                        "for DW_FORM_block* DW_AT_location for "
6419                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6420                      child_die->offset.sect_off, objfile->name);
6421           continue;
6422         }
6423
6424       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6425       if (!attr_form_is_block (attr))
6426         {
6427           complaint (&symfile_complaints,
6428                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6429                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6430                      child_die->offset.sect_off, objfile->name);
6431           continue;
6432         }
6433       parameter->value = DW_BLOCK (attr)->data;
6434       parameter->value_size = DW_BLOCK (attr)->size;
6435
6436       /* Parameters are not pre-cleared by memset above.  */
6437       parameter->data_value = NULL;
6438       parameter->data_value_size = 0;
6439       call_site->parameter_count++;
6440
6441       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6442       if (attr)
6443         {
6444           if (!attr_form_is_block (attr))
6445             complaint (&symfile_complaints,
6446                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6447                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6448                        child_die->offset.sect_off, objfile->name);
6449           else
6450             {
6451               parameter->data_value = DW_BLOCK (attr)->data;
6452               parameter->data_value_size = DW_BLOCK (attr)->size;
6453             }
6454         }
6455     }
6456 }
6457
6458 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6459    Return 1 if the attributes are present and valid, otherwise, return 0.
6460    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6461
6462 static int
6463 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6464                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6465                     struct partial_symtab *ranges_pst)
6466 {
6467   struct objfile *objfile = cu->objfile;
6468   struct comp_unit_head *cu_header = &cu->header;
6469   bfd *obfd = objfile->obfd;
6470   unsigned int addr_size = cu_header->addr_size;
6471   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6472   /* Base address selection entry.  */
6473   CORE_ADDR base;
6474   int found_base;
6475   unsigned int dummy;
6476   gdb_byte *buffer;
6477   CORE_ADDR marker;
6478   int low_set;
6479   CORE_ADDR low = 0;
6480   CORE_ADDR high = 0;
6481   CORE_ADDR baseaddr;
6482
6483   found_base = cu->base_known;
6484   base = cu->base_address;
6485
6486   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6487   if (offset >= dwarf2_per_objfile->ranges.size)
6488     {
6489       complaint (&symfile_complaints,
6490                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6491                  offset);
6492       return 0;
6493     }
6494   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6495
6496   /* Read in the largest possible address.  */
6497   marker = read_address (obfd, buffer, cu, &dummy);
6498   if ((marker & mask) == mask)
6499     {
6500       /* If we found the largest possible address, then
6501          read the base address.  */
6502       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6503       buffer += 2 * addr_size;
6504       offset += 2 * addr_size;
6505       found_base = 1;
6506     }
6507
6508   low_set = 0;
6509
6510   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6511
6512   while (1)
6513     {
6514       CORE_ADDR range_beginning, range_end;
6515
6516       range_beginning = read_address (obfd, buffer, cu, &dummy);
6517       buffer += addr_size;
6518       range_end = read_address (obfd, buffer, cu, &dummy);
6519       buffer += addr_size;
6520       offset += 2 * addr_size;
6521
6522       /* An end of list marker is a pair of zero addresses.  */
6523       if (range_beginning == 0 && range_end == 0)
6524         /* Found the end of list entry.  */
6525         break;
6526
6527       /* Each base address selection entry is a pair of 2 values.
6528          The first is the largest possible address, the second is
6529          the base address.  Check for a base address here.  */
6530       if ((range_beginning & mask) == mask)
6531         {
6532           /* If we found the largest possible address, then
6533              read the base address.  */
6534           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6535           found_base = 1;
6536           continue;
6537         }
6538
6539       if (!found_base)
6540         {
6541           /* We have no valid base address for the ranges
6542              data.  */
6543           complaint (&symfile_complaints,
6544                      _("Invalid .debug_ranges data (no base address)"));
6545           return 0;
6546         }
6547
6548       if (range_beginning > range_end)
6549         {
6550           /* Inverted range entries are invalid.  */
6551           complaint (&symfile_complaints,
6552                      _("Invalid .debug_ranges data (inverted range)"));
6553           return 0;
6554         }
6555
6556       /* Empty range entries have no effect.  */
6557       if (range_beginning == range_end)
6558         continue;
6559
6560       range_beginning += base;
6561       range_end += base;
6562
6563       if (ranges_pst != NULL)
6564         addrmap_set_empty (objfile->psymtabs_addrmap,
6565                            range_beginning + baseaddr,
6566                            range_end - 1 + baseaddr,
6567                            ranges_pst);
6568
6569       /* FIXME: This is recording everything as a low-high
6570          segment of consecutive addresses.  We should have a
6571          data structure for discontiguous block ranges
6572          instead.  */
6573       if (! low_set)
6574         {
6575           low = range_beginning;
6576           high = range_end;
6577           low_set = 1;
6578         }
6579       else
6580         {
6581           if (range_beginning < low)
6582             low = range_beginning;
6583           if (range_end > high)
6584             high = range_end;
6585         }
6586     }
6587
6588   if (! low_set)
6589     /* If the first entry is an end-of-list marker, the range
6590        describes an empty scope, i.e. no instructions.  */
6591     return 0;
6592
6593   if (low_return)
6594     *low_return = low;
6595   if (high_return)
6596     *high_return = high;
6597   return 1;
6598 }
6599
6600 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6601    are present and valid, otherwise, return 0.  Return -1 if the range is
6602    discontinuous, i.e. derived from DW_AT_ranges information.  */
6603 static int
6604 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6605                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6606                       struct partial_symtab *pst)
6607 {
6608   struct attribute *attr;
6609   CORE_ADDR low = 0;
6610   CORE_ADDR high = 0;
6611   int ret = 0;
6612
6613   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6614   if (attr)
6615     {
6616       high = DW_ADDR (attr);
6617       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6618       if (attr)
6619         low = DW_ADDR (attr);
6620       else
6621         /* Found high w/o low attribute.  */
6622         return 0;
6623
6624       /* Found consecutive range of addresses.  */
6625       ret = 1;
6626     }
6627   else
6628     {
6629       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6630       if (attr != NULL)
6631         {
6632           /* Value of the DW_AT_ranges attribute is the offset in the
6633              .debug_ranges section.  */
6634           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6635             return 0;
6636           /* Found discontinuous range of addresses.  */
6637           ret = -1;
6638         }
6639     }
6640
6641   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6642   if (high <= low)
6643     return 0;
6644
6645   /* When using the GNU linker, .gnu.linkonce. sections are used to
6646      eliminate duplicate copies of functions and vtables and such.
6647      The linker will arbitrarily choose one and discard the others.
6648      The AT_*_pc values for such functions refer to local labels in
6649      these sections.  If the section from that file was discarded, the
6650      labels are not in the output, so the relocs get a value of 0.
6651      If this is a discarded function, mark the pc bounds as invalid,
6652      so that GDB will ignore it.  */
6653   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6654     return 0;
6655
6656   *lowpc = low;
6657   if (highpc)
6658     *highpc = high;
6659   return ret;
6660 }
6661
6662 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6663    its low and high PC addresses.  Do nothing if these addresses could not
6664    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6665    and HIGHPC to the high address if greater than HIGHPC.  */
6666
6667 static void
6668 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6669                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6670                                  struct dwarf2_cu *cu)
6671 {
6672   CORE_ADDR low, high;
6673   struct die_info *child = die->child;
6674
6675   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6676     {
6677       *lowpc = min (*lowpc, low);
6678       *highpc = max (*highpc, high);
6679     }
6680
6681   /* If the language does not allow nested subprograms (either inside
6682      subprograms or lexical blocks), we're done.  */
6683   if (cu->language != language_ada)
6684     return;
6685
6686   /* Check all the children of the given DIE.  If it contains nested
6687      subprograms, then check their pc bounds.  Likewise, we need to
6688      check lexical blocks as well, as they may also contain subprogram
6689      definitions.  */
6690   while (child && child->tag)
6691     {
6692       if (child->tag == DW_TAG_subprogram
6693           || child->tag == DW_TAG_lexical_block)
6694         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6695       child = sibling_die (child);
6696     }
6697 }
6698
6699 /* Get the low and high pc's represented by the scope DIE, and store
6700    them in *LOWPC and *HIGHPC.  If the correct values can't be
6701    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6702
6703 static void
6704 get_scope_pc_bounds (struct die_info *die,
6705                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6706                      struct dwarf2_cu *cu)
6707 {
6708   CORE_ADDR best_low = (CORE_ADDR) -1;
6709   CORE_ADDR best_high = (CORE_ADDR) 0;
6710   CORE_ADDR current_low, current_high;
6711
6712   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6713     {
6714       best_low = current_low;
6715       best_high = current_high;
6716     }
6717   else
6718     {
6719       struct die_info *child = die->child;
6720
6721       while (child && child->tag)
6722         {
6723           switch (child->tag) {
6724           case DW_TAG_subprogram:
6725             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6726             break;
6727           case DW_TAG_namespace:
6728           case DW_TAG_module:
6729             /* FIXME: carlton/2004-01-16: Should we do this for
6730                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6731                that current GCC's always emit the DIEs corresponding
6732                to definitions of methods of classes as children of a
6733                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6734                the DIEs giving the declarations, which could be
6735                anywhere).  But I don't see any reason why the
6736                standards says that they have to be there.  */
6737             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6738
6739             if (current_low != ((CORE_ADDR) -1))
6740               {
6741                 best_low = min (best_low, current_low);
6742                 best_high = max (best_high, current_high);
6743               }
6744             break;
6745           default:
6746             /* Ignore.  */
6747             break;
6748           }
6749
6750           child = sibling_die (child);
6751         }
6752     }
6753
6754   *lowpc = best_low;
6755   *highpc = best_high;
6756 }
6757
6758 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6759    in DIE.  */
6760 static void
6761 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6762                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6763 {
6764   struct objfile *objfile = cu->objfile;
6765   struct attribute *attr;
6766
6767   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6768   if (attr)
6769     {
6770       CORE_ADDR high = DW_ADDR (attr);
6771
6772       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6773       if (attr)
6774         {
6775           CORE_ADDR low = DW_ADDR (attr);
6776
6777           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6778         }
6779     }
6780
6781   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6782   if (attr)
6783     {
6784       bfd *obfd = objfile->obfd;
6785
6786       /* The value of the DW_AT_ranges attribute is the offset of the
6787          address range list in the .debug_ranges section.  */
6788       unsigned long offset = DW_UNSND (attr);
6789       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6790
6791       /* For some target architectures, but not others, the
6792          read_address function sign-extends the addresses it returns.
6793          To recognize base address selection entries, we need a
6794          mask.  */
6795       unsigned int addr_size = cu->header.addr_size;
6796       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6797
6798       /* The base address, to which the next pair is relative.  Note
6799          that this 'base' is a DWARF concept: most entries in a range
6800          list are relative, to reduce the number of relocs against the
6801          debugging information.  This is separate from this function's
6802          'baseaddr' argument, which GDB uses to relocate debugging
6803          information from a shared library based on the address at
6804          which the library was loaded.  */
6805       CORE_ADDR base = cu->base_address;
6806       int base_known = cu->base_known;
6807
6808       gdb_assert (dwarf2_per_objfile->ranges.readin);
6809       if (offset >= dwarf2_per_objfile->ranges.size)
6810         {
6811           complaint (&symfile_complaints,
6812                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6813                      offset);
6814           return;
6815         }
6816
6817       for (;;)
6818         {
6819           unsigned int bytes_read;
6820           CORE_ADDR start, end;
6821
6822           start = read_address (obfd, buffer, cu, &bytes_read);
6823           buffer += bytes_read;
6824           end = read_address (obfd, buffer, cu, &bytes_read);
6825           buffer += bytes_read;
6826
6827           /* Did we find the end of the range list?  */
6828           if (start == 0 && end == 0)
6829             break;
6830
6831           /* Did we find a base address selection entry?  */
6832           else if ((start & base_select_mask) == base_select_mask)
6833             {
6834               base = end;
6835               base_known = 1;
6836             }
6837
6838           /* We found an ordinary address range.  */
6839           else
6840             {
6841               if (!base_known)
6842                 {
6843                   complaint (&symfile_complaints,
6844                              _("Invalid .debug_ranges data "
6845                                "(no base address)"));
6846                   return;
6847                 }
6848
6849               if (start > end)
6850                 {
6851                   /* Inverted range entries are invalid.  */
6852                   complaint (&symfile_complaints,
6853                              _("Invalid .debug_ranges data "
6854                                "(inverted range)"));
6855                   return;
6856                 }
6857
6858               /* Empty range entries have no effect.  */
6859               if (start == end)
6860                 continue;
6861
6862               record_block_range (block,
6863                                   baseaddr + base + start,
6864                                   baseaddr + base + end - 1);
6865             }
6866         }
6867     }
6868 }
6869
6870 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6871    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6872    during 4.6.0 experimental.  */
6873
6874 static int
6875 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6876 {
6877   const char *cs;
6878   int major, minor, release;
6879   int result = 0;
6880
6881   if (cu->producer == NULL)
6882     {
6883       /* For unknown compilers expect their behavior is DWARF version
6884          compliant.
6885
6886          GCC started to support .debug_types sections by -gdwarf-4 since
6887          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6888          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6889          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6890          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6891
6892       return 0;
6893     }
6894
6895   if (cu->checked_producer)
6896     return cu->producer_is_gxx_lt_4_6;
6897
6898   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6899
6900   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6901     {
6902       /* For non-GCC compilers expect their behavior is DWARF version
6903          compliant.  */
6904     }
6905   else
6906     {
6907       cs = &cu->producer[strlen ("GNU ")];
6908       while (*cs && !isdigit (*cs))
6909         cs++;
6910       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6911         {
6912           /* Not recognized as GCC.  */
6913         }
6914       else
6915         result = major < 4 || (major == 4 && minor < 6);
6916     }
6917
6918   cu->checked_producer = 1;
6919   cu->producer_is_gxx_lt_4_6 = result;
6920
6921   return result;
6922 }
6923
6924 /* Return the default accessibility type if it is not overriden by
6925    DW_AT_accessibility.  */
6926
6927 static enum dwarf_access_attribute
6928 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6929 {
6930   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6931     {
6932       /* The default DWARF 2 accessibility for members is public, the default
6933          accessibility for inheritance is private.  */
6934
6935       if (die->tag != DW_TAG_inheritance)
6936         return DW_ACCESS_public;
6937       else
6938         return DW_ACCESS_private;
6939     }
6940   else
6941     {
6942       /* DWARF 3+ defines the default accessibility a different way.  The same
6943          rules apply now for DW_TAG_inheritance as for the members and it only
6944          depends on the container kind.  */
6945
6946       if (die->parent->tag == DW_TAG_class_type)
6947         return DW_ACCESS_private;
6948       else
6949         return DW_ACCESS_public;
6950     }
6951 }
6952
6953 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6954    offset.  If the attribute was not found return 0, otherwise return
6955    1.  If it was found but could not properly be handled, set *OFFSET
6956    to 0.  */
6957
6958 static int
6959 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6960                              LONGEST *offset)
6961 {
6962   struct attribute *attr;
6963
6964   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6965   if (attr != NULL)
6966     {
6967       *offset = 0;
6968
6969       /* Note that we do not check for a section offset first here.
6970          This is because DW_AT_data_member_location is new in DWARF 4,
6971          so if we see it, we can assume that a constant form is really
6972          a constant and not a section offset.  */
6973       if (attr_form_is_constant (attr))
6974         *offset = dwarf2_get_attr_constant_value (attr, 0);
6975       else if (attr_form_is_section_offset (attr))
6976         dwarf2_complex_location_expr_complaint ();
6977       else if (attr_form_is_block (attr))
6978         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6979       else
6980         dwarf2_complex_location_expr_complaint ();
6981
6982       return 1;
6983     }
6984
6985   return 0;
6986 }
6987
6988 /* Add an aggregate field to the field list.  */
6989
6990 static void
6991 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6992                   struct dwarf2_cu *cu)
6993 {
6994   struct objfile *objfile = cu->objfile;
6995   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6996   struct nextfield *new_field;
6997   struct attribute *attr;
6998   struct field *fp;
6999   char *fieldname = "";
7000
7001   /* Allocate a new field list entry and link it in.  */
7002   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
7003   make_cleanup (xfree, new_field);
7004   memset (new_field, 0, sizeof (struct nextfield));
7005
7006   if (die->tag == DW_TAG_inheritance)
7007     {
7008       new_field->next = fip->baseclasses;
7009       fip->baseclasses = new_field;
7010     }
7011   else
7012     {
7013       new_field->next = fip->fields;
7014       fip->fields = new_field;
7015     }
7016   fip->nfields++;
7017
7018   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7019   if (attr)
7020     new_field->accessibility = DW_UNSND (attr);
7021   else
7022     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7023   if (new_field->accessibility != DW_ACCESS_public)
7024     fip->non_public_fields = 1;
7025
7026   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7027   if (attr)
7028     new_field->virtuality = DW_UNSND (attr);
7029   else
7030     new_field->virtuality = DW_VIRTUALITY_none;
7031
7032   fp = &new_field->field;
7033
7034   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7035     {
7036       LONGEST offset;
7037
7038       /* Data member other than a C++ static data member.  */
7039
7040       /* Get type of field.  */
7041       fp->type = die_type (die, cu);
7042
7043       SET_FIELD_BITPOS (*fp, 0);
7044
7045       /* Get bit size of field (zero if none).  */
7046       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7047       if (attr)
7048         {
7049           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7050         }
7051       else
7052         {
7053           FIELD_BITSIZE (*fp) = 0;
7054         }
7055
7056       /* Get bit offset of field.  */
7057       if (handle_data_member_location (die, cu, &offset))
7058         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7059       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7060       if (attr)
7061         {
7062           if (gdbarch_bits_big_endian (gdbarch))
7063             {
7064               /* For big endian bits, the DW_AT_bit_offset gives the
7065                  additional bit offset from the MSB of the containing
7066                  anonymous object to the MSB of the field.  We don't
7067                  have to do anything special since we don't need to
7068                  know the size of the anonymous object.  */
7069               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
7070             }
7071           else
7072             {
7073               /* For little endian bits, compute the bit offset to the
7074                  MSB of the anonymous object, subtract off the number of
7075                  bits from the MSB of the field to the MSB of the
7076                  object, and then subtract off the number of bits of
7077                  the field itself.  The result is the bit offset of
7078                  the LSB of the field.  */
7079               int anonymous_size;
7080               int bit_offset = DW_UNSND (attr);
7081
7082               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7083               if (attr)
7084                 {
7085                   /* The size of the anonymous object containing
7086                      the bit field is explicit, so use the
7087                      indicated size (in bytes).  */
7088                   anonymous_size = DW_UNSND (attr);
7089                 }
7090               else
7091                 {
7092                   /* The size of the anonymous object containing
7093                      the bit field must be inferred from the type
7094                      attribute of the data member containing the
7095                      bit field.  */
7096                   anonymous_size = TYPE_LENGTH (fp->type);
7097                 }
7098               SET_FIELD_BITPOS (*fp,
7099                                 (FIELD_BITPOS (*fp)
7100                                  + anonymous_size * bits_per_byte
7101                                  - bit_offset - FIELD_BITSIZE (*fp)));
7102             }
7103         }
7104
7105       /* Get name of field.  */
7106       fieldname = dwarf2_name (die, cu);
7107       if (fieldname == NULL)
7108         fieldname = "";
7109
7110       /* The name is already allocated along with this objfile, so we don't
7111          need to duplicate it for the type.  */
7112       fp->name = fieldname;
7113
7114       /* Change accessibility for artificial fields (e.g. virtual table
7115          pointer or virtual base class pointer) to private.  */
7116       if (dwarf2_attr (die, DW_AT_artificial, cu))
7117         {
7118           FIELD_ARTIFICIAL (*fp) = 1;
7119           new_field->accessibility = DW_ACCESS_private;
7120           fip->non_public_fields = 1;
7121         }
7122     }
7123   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7124     {
7125       /* C++ static member.  */
7126
7127       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7128          is a declaration, but all versions of G++ as of this writing
7129          (so through at least 3.2.1) incorrectly generate
7130          DW_TAG_variable tags.  */
7131
7132       const char *physname;
7133
7134       /* Get name of field.  */
7135       fieldname = dwarf2_name (die, cu);
7136       if (fieldname == NULL)
7137         return;
7138
7139       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7140       if (attr
7141           /* Only create a symbol if this is an external value.
7142              new_symbol checks this and puts the value in the global symbol
7143              table, which we want.  If it is not external, new_symbol
7144              will try to put the value in cu->list_in_scope which is wrong.  */
7145           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7146         {
7147           /* A static const member, not much different than an enum as far as
7148              we're concerned, except that we can support more types.  */
7149           new_symbol (die, NULL, cu);
7150         }
7151
7152       /* Get physical name.  */
7153       physname = dwarf2_physname (fieldname, die, cu);
7154
7155       /* The name is already allocated along with this objfile, so we don't
7156          need to duplicate it for the type.  */
7157       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7158       FIELD_TYPE (*fp) = die_type (die, cu);
7159       FIELD_NAME (*fp) = fieldname;
7160     }
7161   else if (die->tag == DW_TAG_inheritance)
7162     {
7163       LONGEST offset;
7164
7165       /* C++ base class field.  */
7166       if (handle_data_member_location (die, cu, &offset))
7167         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7168       FIELD_BITSIZE (*fp) = 0;
7169       FIELD_TYPE (*fp) = die_type (die, cu);
7170       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7171       fip->nbaseclasses++;
7172     }
7173 }
7174
7175 /* Add a typedef defined in the scope of the FIP's class.  */
7176
7177 static void
7178 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7179                     struct dwarf2_cu *cu)
7180 {
7181   struct objfile *objfile = cu->objfile;
7182   struct typedef_field_list *new_field;
7183   struct attribute *attr;
7184   struct typedef_field *fp;
7185   char *fieldname = "";
7186
7187   /* Allocate a new field list entry and link it in.  */
7188   new_field = xzalloc (sizeof (*new_field));
7189   make_cleanup (xfree, new_field);
7190
7191   gdb_assert (die->tag == DW_TAG_typedef);
7192
7193   fp = &new_field->field;
7194
7195   /* Get name of field.  */
7196   fp->name = dwarf2_name (die, cu);
7197   if (fp->name == NULL)
7198     return;
7199
7200   fp->type = read_type_die (die, cu);
7201
7202   new_field->next = fip->typedef_field_list;
7203   fip->typedef_field_list = new_field;
7204   fip->typedef_field_list_count++;
7205 }
7206
7207 /* Create the vector of fields, and attach it to the type.  */
7208
7209 static void
7210 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7211                               struct dwarf2_cu *cu)
7212 {
7213   int nfields = fip->nfields;
7214
7215   /* Record the field count, allocate space for the array of fields,
7216      and create blank accessibility bitfields if necessary.  */
7217   TYPE_NFIELDS (type) = nfields;
7218   TYPE_FIELDS (type) = (struct field *)
7219     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7220   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7221
7222   if (fip->non_public_fields && cu->language != language_ada)
7223     {
7224       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7225
7226       TYPE_FIELD_PRIVATE_BITS (type) =
7227         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7228       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7229
7230       TYPE_FIELD_PROTECTED_BITS (type) =
7231         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7232       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7233
7234       TYPE_FIELD_IGNORE_BITS (type) =
7235         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7236       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7237     }
7238
7239   /* If the type has baseclasses, allocate and clear a bit vector for
7240      TYPE_FIELD_VIRTUAL_BITS.  */
7241   if (fip->nbaseclasses && cu->language != language_ada)
7242     {
7243       int num_bytes = B_BYTES (fip->nbaseclasses);
7244       unsigned char *pointer;
7245
7246       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7247       pointer = TYPE_ALLOC (type, num_bytes);
7248       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7249       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7250       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7251     }
7252
7253   /* Copy the saved-up fields into the field vector.  Start from the head of
7254      the list, adding to the tail of the field array, so that they end up in
7255      the same order in the array in which they were added to the list.  */
7256   while (nfields-- > 0)
7257     {
7258       struct nextfield *fieldp;
7259
7260       if (fip->fields)
7261         {
7262           fieldp = fip->fields;
7263           fip->fields = fieldp->next;
7264         }
7265       else
7266         {
7267           fieldp = fip->baseclasses;
7268           fip->baseclasses = fieldp->next;
7269         }
7270
7271       TYPE_FIELD (type, nfields) = fieldp->field;
7272       switch (fieldp->accessibility)
7273         {
7274         case DW_ACCESS_private:
7275           if (cu->language != language_ada)
7276             SET_TYPE_FIELD_PRIVATE (type, nfields);
7277           break;
7278
7279         case DW_ACCESS_protected:
7280           if (cu->language != language_ada)
7281             SET_TYPE_FIELD_PROTECTED (type, nfields);
7282           break;
7283
7284         case DW_ACCESS_public:
7285           break;
7286
7287         default:
7288           /* Unknown accessibility.  Complain and treat it as public.  */
7289           {
7290             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7291                        fieldp->accessibility);
7292           }
7293           break;
7294         }
7295       if (nfields < fip->nbaseclasses)
7296         {
7297           switch (fieldp->virtuality)
7298             {
7299             case DW_VIRTUALITY_virtual:
7300             case DW_VIRTUALITY_pure_virtual:
7301               if (cu->language == language_ada)
7302                 error (_("unexpected virtuality in component of Ada type"));
7303               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7304               break;
7305             }
7306         }
7307     }
7308 }
7309
7310 /* Add a member function to the proper fieldlist.  */
7311
7312 static void
7313 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7314                       struct type *type, struct dwarf2_cu *cu)
7315 {
7316   struct objfile *objfile = cu->objfile;
7317   struct attribute *attr;
7318   struct fnfieldlist *flp;
7319   int i;
7320   struct fn_field *fnp;
7321   char *fieldname;
7322   struct nextfnfield *new_fnfield;
7323   struct type *this_type;
7324   enum dwarf_access_attribute accessibility;
7325
7326   if (cu->language == language_ada)
7327     error (_("unexpected member function in Ada type"));
7328
7329   /* Get name of member function.  */
7330   fieldname = dwarf2_name (die, cu);
7331   if (fieldname == NULL)
7332     return;
7333
7334   /* Look up member function name in fieldlist.  */
7335   for (i = 0; i < fip->nfnfields; i++)
7336     {
7337       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7338         break;
7339     }
7340
7341   /* Create new list element if necessary.  */
7342   if (i < fip->nfnfields)
7343     flp = &fip->fnfieldlists[i];
7344   else
7345     {
7346       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7347         {
7348           fip->fnfieldlists = (struct fnfieldlist *)
7349             xrealloc (fip->fnfieldlists,
7350                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7351                       * sizeof (struct fnfieldlist));
7352           if (fip->nfnfields == 0)
7353             make_cleanup (free_current_contents, &fip->fnfieldlists);
7354         }
7355       flp = &fip->fnfieldlists[fip->nfnfields];
7356       flp->name = fieldname;
7357       flp->length = 0;
7358       flp->head = NULL;
7359       i = fip->nfnfields++;
7360     }
7361
7362   /* Create a new member function field and chain it to the field list
7363      entry.  */
7364   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7365   make_cleanup (xfree, new_fnfield);
7366   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7367   new_fnfield->next = flp->head;
7368   flp->head = new_fnfield;
7369   flp->length++;
7370
7371   /* Fill in the member function field info.  */
7372   fnp = &new_fnfield->fnfield;
7373
7374   /* Delay processing of the physname until later.  */
7375   if (cu->language == language_cplus || cu->language == language_java)
7376     {
7377       add_to_method_list (type, i, flp->length - 1, fieldname,
7378                           die, cu);
7379     }
7380   else
7381     {
7382       const char *physname = dwarf2_physname (fieldname, die, cu);
7383       fnp->physname = physname ? physname : "";
7384     }
7385
7386   fnp->type = alloc_type (objfile);
7387   this_type = read_type_die (die, cu);
7388   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7389     {
7390       int nparams = TYPE_NFIELDS (this_type);
7391
7392       /* TYPE is the domain of this method, and THIS_TYPE is the type
7393            of the method itself (TYPE_CODE_METHOD).  */
7394       smash_to_method_type (fnp->type, type,
7395                             TYPE_TARGET_TYPE (this_type),
7396                             TYPE_FIELDS (this_type),
7397                             TYPE_NFIELDS (this_type),
7398                             TYPE_VARARGS (this_type));
7399
7400       /* Handle static member functions.
7401          Dwarf2 has no clean way to discern C++ static and non-static
7402          member functions.  G++ helps GDB by marking the first
7403          parameter for non-static member functions (which is the this
7404          pointer) as artificial.  We obtain this information from
7405          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7406       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7407         fnp->voffset = VOFFSET_STATIC;
7408     }
7409   else
7410     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7411                dwarf2_full_name (fieldname, die, cu));
7412
7413   /* Get fcontext from DW_AT_containing_type if present.  */
7414   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7415     fnp->fcontext = die_containing_type (die, cu);
7416
7417   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7418      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7419
7420   /* Get accessibility.  */
7421   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7422   if (attr)
7423     accessibility = DW_UNSND (attr);
7424   else
7425     accessibility = dwarf2_default_access_attribute (die, cu);
7426   switch (accessibility)
7427     {
7428     case DW_ACCESS_private:
7429       fnp->is_private = 1;
7430       break;
7431     case DW_ACCESS_protected:
7432       fnp->is_protected = 1;
7433       break;
7434     }
7435
7436   /* Check for artificial methods.  */
7437   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7438   if (attr && DW_UNSND (attr) != 0)
7439     fnp->is_artificial = 1;
7440
7441   /* Get index in virtual function table if it is a virtual member
7442      function.  For older versions of GCC, this is an offset in the
7443      appropriate virtual table, as specified by DW_AT_containing_type.
7444      For everyone else, it is an expression to be evaluated relative
7445      to the object address.  */
7446
7447   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7448   if (attr)
7449     {
7450       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7451         {
7452           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7453             {
7454               /* Old-style GCC.  */
7455               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7456             }
7457           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7458                    || (DW_BLOCK (attr)->size > 1
7459                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7460                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7461             {
7462               struct dwarf_block blk;
7463               int offset;
7464
7465               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7466                         ? 1 : 2);
7467               blk.size = DW_BLOCK (attr)->size - offset;
7468               blk.data = DW_BLOCK (attr)->data + offset;
7469               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7470               if ((fnp->voffset % cu->header.addr_size) != 0)
7471                 dwarf2_complex_location_expr_complaint ();
7472               else
7473                 fnp->voffset /= cu->header.addr_size;
7474               fnp->voffset += 2;
7475             }
7476           else
7477             dwarf2_complex_location_expr_complaint ();
7478
7479           if (!fnp->fcontext)
7480             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7481         }
7482       else if (attr_form_is_section_offset (attr))
7483         {
7484           dwarf2_complex_location_expr_complaint ();
7485         }
7486       else
7487         {
7488           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7489                                                  fieldname);
7490         }
7491     }
7492   else
7493     {
7494       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7495       if (attr && DW_UNSND (attr))
7496         {
7497           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7498           complaint (&symfile_complaints,
7499                      _("Member function \"%s\" (offset %d) is virtual "
7500                        "but the vtable offset is not specified"),
7501                      fieldname, die->offset.sect_off);
7502           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7503           TYPE_CPLUS_DYNAMIC (type) = 1;
7504         }
7505     }
7506 }
7507
7508 /* Create the vector of member function fields, and attach it to the type.  */
7509
7510 static void
7511 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7512                                  struct dwarf2_cu *cu)
7513 {
7514   struct fnfieldlist *flp;
7515   int i;
7516
7517   if (cu->language == language_ada)
7518     error (_("unexpected member functions in Ada type"));
7519
7520   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7521   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7522     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7523
7524   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7525     {
7526       struct nextfnfield *nfp = flp->head;
7527       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7528       int k;
7529
7530       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7531       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7532       fn_flp->fn_fields = (struct fn_field *)
7533         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7534       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7535         fn_flp->fn_fields[k] = nfp->fnfield;
7536     }
7537
7538   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7539 }
7540
7541 /* Returns non-zero if NAME is the name of a vtable member in CU's
7542    language, zero otherwise.  */
7543 static int
7544 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7545 {
7546   static const char vptr[] = "_vptr";
7547   static const char vtable[] = "vtable";
7548
7549   /* Look for the C++ and Java forms of the vtable.  */
7550   if ((cu->language == language_java
7551        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7552        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7553        && is_cplus_marker (name[sizeof (vptr) - 1])))
7554     return 1;
7555
7556   return 0;
7557 }
7558
7559 /* GCC outputs unnamed structures that are really pointers to member
7560    functions, with the ABI-specified layout.  If TYPE describes
7561    such a structure, smash it into a member function type.
7562
7563    GCC shouldn't do this; it should just output pointer to member DIEs.
7564    This is GCC PR debug/28767.  */
7565
7566 static void
7567 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7568 {
7569   struct type *pfn_type, *domain_type, *new_type;
7570
7571   /* Check for a structure with no name and two children.  */
7572   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7573     return;
7574
7575   /* Check for __pfn and __delta members.  */
7576   if (TYPE_FIELD_NAME (type, 0) == NULL
7577       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7578       || TYPE_FIELD_NAME (type, 1) == NULL
7579       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7580     return;
7581
7582   /* Find the type of the method.  */
7583   pfn_type = TYPE_FIELD_TYPE (type, 0);
7584   if (pfn_type == NULL
7585       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7586       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7587     return;
7588
7589   /* Look for the "this" argument.  */
7590   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7591   if (TYPE_NFIELDS (pfn_type) == 0
7592       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7593       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7594     return;
7595
7596   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7597   new_type = alloc_type (objfile);
7598   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7599                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7600                         TYPE_VARARGS (pfn_type));
7601   smash_to_methodptr_type (type, new_type);
7602 }
7603
7604 /* Called when we find the DIE that starts a structure or union scope
7605    (definition) to create a type for the structure or union.  Fill in
7606    the type's name and general properties; the members will not be
7607    processed until process_structure_type.
7608
7609    NOTE: we need to call these functions regardless of whether or not the
7610    DIE has a DW_AT_name attribute, since it might be an anonymous
7611    structure or union.  This gets the type entered into our set of
7612    user defined types.
7613
7614    However, if the structure is incomplete (an opaque struct/union)
7615    then suppress creating a symbol table entry for it since gdb only
7616    wants to find the one with the complete definition.  Note that if
7617    it is complete, we just call new_symbol, which does it's own
7618    checking about whether the struct/union is anonymous or not (and
7619    suppresses creating a symbol table entry itself).  */
7620
7621 static struct type *
7622 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7623 {
7624   struct objfile *objfile = cu->objfile;
7625   struct type *type;
7626   struct attribute *attr;
7627   char *name;
7628
7629   /* If the definition of this type lives in .debug_types, read that type.
7630      Don't follow DW_AT_specification though, that will take us back up
7631      the chain and we want to go down.  */
7632   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7633   if (attr)
7634     {
7635       struct dwarf2_cu *type_cu = cu;
7636       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7637
7638       /* We could just recurse on read_structure_type, but we need to call
7639          get_die_type to ensure only one type for this DIE is created.
7640          This is important, for example, because for c++ classes we need
7641          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7642       type = read_type_die (type_die, type_cu);
7643
7644       /* TYPE_CU may not be the same as CU.
7645          Ensure TYPE is recorded in CU's type_hash table.  */
7646       return set_die_type (die, type, cu);
7647     }
7648
7649   type = alloc_type (objfile);
7650   INIT_CPLUS_SPECIFIC (type);
7651
7652   name = dwarf2_name (die, cu);
7653   if (name != NULL)
7654     {
7655       if (cu->language == language_cplus
7656           || cu->language == language_java)
7657         {
7658           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7659
7660           /* dwarf2_full_name might have already finished building the DIE's
7661              type.  If so, there is no need to continue.  */
7662           if (get_die_type (die, cu) != NULL)
7663             return get_die_type (die, cu);
7664
7665           TYPE_TAG_NAME (type) = full_name;
7666           if (die->tag == DW_TAG_structure_type
7667               || die->tag == DW_TAG_class_type)
7668             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7669         }
7670       else
7671         {
7672           /* The name is already allocated along with this objfile, so
7673              we don't need to duplicate it for the type.  */
7674           TYPE_TAG_NAME (type) = (char *) name;
7675           if (die->tag == DW_TAG_class_type)
7676             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7677         }
7678     }
7679
7680   if (die->tag == DW_TAG_structure_type)
7681     {
7682       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7683     }
7684   else if (die->tag == DW_TAG_union_type)
7685     {
7686       TYPE_CODE (type) = TYPE_CODE_UNION;
7687     }
7688   else
7689     {
7690       TYPE_CODE (type) = TYPE_CODE_CLASS;
7691     }
7692
7693   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7694     TYPE_DECLARED_CLASS (type) = 1;
7695
7696   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7697   if (attr)
7698     {
7699       TYPE_LENGTH (type) = DW_UNSND (attr);
7700     }
7701   else
7702     {
7703       TYPE_LENGTH (type) = 0;
7704     }
7705
7706   TYPE_STUB_SUPPORTED (type) = 1;
7707   if (die_is_declaration (die, cu))
7708     TYPE_STUB (type) = 1;
7709   else if (attr == NULL && die->child == NULL
7710            && producer_is_realview (cu->producer))
7711     /* RealView does not output the required DW_AT_declaration
7712        on incomplete types.  */
7713     TYPE_STUB (type) = 1;
7714
7715   /* We need to add the type field to the die immediately so we don't
7716      infinitely recurse when dealing with pointers to the structure
7717      type within the structure itself.  */
7718   set_die_type (die, type, cu);
7719
7720   /* set_die_type should be already done.  */
7721   set_descriptive_type (type, die, cu);
7722
7723   return type;
7724 }
7725
7726 /* Finish creating a structure or union type, including filling in
7727    its members and creating a symbol for it.  */
7728
7729 static void
7730 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7731 {
7732   struct objfile *objfile = cu->objfile;
7733   struct die_info *child_die = die->child;
7734   struct type *type;
7735
7736   type = get_die_type (die, cu);
7737   if (type == NULL)
7738     type = read_structure_type (die, cu);
7739
7740   if (die->child != NULL && ! die_is_declaration (die, cu))
7741     {
7742       struct field_info fi;
7743       struct die_info *child_die;
7744       VEC (symbolp) *template_args = NULL;
7745       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7746
7747       memset (&fi, 0, sizeof (struct field_info));
7748
7749       child_die = die->child;
7750
7751       while (child_die && child_die->tag)
7752         {
7753           if (child_die->tag == DW_TAG_member
7754               || child_die->tag == DW_TAG_variable)
7755             {
7756               /* NOTE: carlton/2002-11-05: A C++ static data member
7757                  should be a DW_TAG_member that is a declaration, but
7758                  all versions of G++ as of this writing (so through at
7759                  least 3.2.1) incorrectly generate DW_TAG_variable
7760                  tags for them instead.  */
7761               dwarf2_add_field (&fi, child_die, cu);
7762             }
7763           else if (child_die->tag == DW_TAG_subprogram)
7764             {
7765               /* C++ member function.  */
7766               dwarf2_add_member_fn (&fi, child_die, type, cu);
7767             }
7768           else if (child_die->tag == DW_TAG_inheritance)
7769             {
7770               /* C++ base class field.  */
7771               dwarf2_add_field (&fi, child_die, cu);
7772             }
7773           else if (child_die->tag == DW_TAG_typedef)
7774             dwarf2_add_typedef (&fi, child_die, cu);
7775           else if (child_die->tag == DW_TAG_template_type_param
7776                    || child_die->tag == DW_TAG_template_value_param)
7777             {
7778               struct symbol *arg = new_symbol (child_die, NULL, cu);
7779
7780               if (arg != NULL)
7781                 VEC_safe_push (symbolp, template_args, arg);
7782             }
7783
7784           child_die = sibling_die (child_die);
7785         }
7786
7787       /* Attach template arguments to type.  */
7788       if (! VEC_empty (symbolp, template_args))
7789         {
7790           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7791           TYPE_N_TEMPLATE_ARGUMENTS (type)
7792             = VEC_length (symbolp, template_args);
7793           TYPE_TEMPLATE_ARGUMENTS (type)
7794             = obstack_alloc (&objfile->objfile_obstack,
7795                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7796                               * sizeof (struct symbol *)));
7797           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7798                   VEC_address (symbolp, template_args),
7799                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7800                    * sizeof (struct symbol *)));
7801           VEC_free (symbolp, template_args);
7802         }
7803
7804       /* Attach fields and member functions to the type.  */
7805       if (fi.nfields)
7806         dwarf2_attach_fields_to_type (&fi, type, cu);
7807       if (fi.nfnfields)
7808         {
7809           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7810
7811           /* Get the type which refers to the base class (possibly this
7812              class itself) which contains the vtable pointer for the current
7813              class from the DW_AT_containing_type attribute.  This use of
7814              DW_AT_containing_type is a GNU extension.  */
7815
7816           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7817             {
7818               struct type *t = die_containing_type (die, cu);
7819
7820               TYPE_VPTR_BASETYPE (type) = t;
7821               if (type == t)
7822                 {
7823                   int i;
7824
7825                   /* Our own class provides vtbl ptr.  */
7826                   for (i = TYPE_NFIELDS (t) - 1;
7827                        i >= TYPE_N_BASECLASSES (t);
7828                        --i)
7829                     {
7830                       const char *fieldname = TYPE_FIELD_NAME (t, i);
7831
7832                       if (is_vtable_name (fieldname, cu))
7833                         {
7834                           TYPE_VPTR_FIELDNO (type) = i;
7835                           break;
7836                         }
7837                     }
7838
7839                   /* Complain if virtual function table field not found.  */
7840                   if (i < TYPE_N_BASECLASSES (t))
7841                     complaint (&symfile_complaints,
7842                                _("virtual function table pointer "
7843                                  "not found when defining class '%s'"),
7844                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7845                                "");
7846                 }
7847               else
7848                 {
7849                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7850                 }
7851             }
7852           else if (cu->producer
7853                    && strncmp (cu->producer,
7854                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7855             {
7856               /* The IBM XLC compiler does not provide direct indication
7857                  of the containing type, but the vtable pointer is
7858                  always named __vfp.  */
7859
7860               int i;
7861
7862               for (i = TYPE_NFIELDS (type) - 1;
7863                    i >= TYPE_N_BASECLASSES (type);
7864                    --i)
7865                 {
7866                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7867                     {
7868                       TYPE_VPTR_FIELDNO (type) = i;
7869                       TYPE_VPTR_BASETYPE (type) = type;
7870                       break;
7871                     }
7872                 }
7873             }
7874         }
7875
7876       /* Copy fi.typedef_field_list linked list elements content into the
7877          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7878       if (fi.typedef_field_list)
7879         {
7880           int i = fi.typedef_field_list_count;
7881
7882           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7883           TYPE_TYPEDEF_FIELD_ARRAY (type)
7884             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7885           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7886
7887           /* Reverse the list order to keep the debug info elements order.  */
7888           while (--i >= 0)
7889             {
7890               struct typedef_field *dest, *src;
7891
7892               dest = &TYPE_TYPEDEF_FIELD (type, i);
7893               src = &fi.typedef_field_list->field;
7894               fi.typedef_field_list = fi.typedef_field_list->next;
7895               *dest = *src;
7896             }
7897         }
7898
7899       do_cleanups (back_to);
7900
7901       if (HAVE_CPLUS_STRUCT (type))
7902         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7903     }
7904
7905   quirk_gcc_member_function_pointer (type, objfile);
7906
7907   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7908      snapshots) has been known to create a die giving a declaration
7909      for a class that has, as a child, a die giving a definition for a
7910      nested class.  So we have to process our children even if the
7911      current die is a declaration.  Normally, of course, a declaration
7912      won't have any children at all.  */
7913
7914   while (child_die != NULL && child_die->tag)
7915     {
7916       if (child_die->tag == DW_TAG_member
7917           || child_die->tag == DW_TAG_variable
7918           || child_die->tag == DW_TAG_inheritance
7919           || child_die->tag == DW_TAG_template_value_param
7920           || child_die->tag == DW_TAG_template_type_param)
7921         {
7922           /* Do nothing.  */
7923         }
7924       else
7925         process_die (child_die, cu);
7926
7927       child_die = sibling_die (child_die);
7928     }
7929
7930   /* Do not consider external references.  According to the DWARF standard,
7931      these DIEs are identified by the fact that they have no byte_size
7932      attribute, and a declaration attribute.  */
7933   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7934       || !die_is_declaration (die, cu))
7935     new_symbol (die, type, cu);
7936 }
7937
7938 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7939    complete the type's fields yet, or create any symbols.  */
7940
7941 static struct type *
7942 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7943 {
7944   struct objfile *objfile = cu->objfile;
7945   struct type *type;
7946   struct attribute *attr;
7947   const char *name;
7948
7949   /* If the definition of this type lives in .debug_types, read that type.
7950      Don't follow DW_AT_specification though, that will take us back up
7951      the chain and we want to go down.  */
7952   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7953   if (attr)
7954     {
7955       struct dwarf2_cu *type_cu = cu;
7956       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7957
7958       type = read_type_die (type_die, type_cu);
7959
7960       /* TYPE_CU may not be the same as CU.
7961          Ensure TYPE is recorded in CU's type_hash table.  */
7962       return set_die_type (die, type, cu);
7963     }
7964
7965   type = alloc_type (objfile);
7966
7967   TYPE_CODE (type) = TYPE_CODE_ENUM;
7968   name = dwarf2_full_name (NULL, die, cu);
7969   if (name != NULL)
7970     TYPE_TAG_NAME (type) = (char *) name;
7971
7972   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7973   if (attr)
7974     {
7975       TYPE_LENGTH (type) = DW_UNSND (attr);
7976     }
7977   else
7978     {
7979       TYPE_LENGTH (type) = 0;
7980     }
7981
7982   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7983      declared as private in the package spec, and then defined only
7984      inside the package body.  Such types are known as Taft Amendment
7985      Types.  When another package uses such a type, an incomplete DIE
7986      may be generated by the compiler.  */
7987   if (die_is_declaration (die, cu))
7988     TYPE_STUB (type) = 1;
7989
7990   return set_die_type (die, type, cu);
7991 }
7992
7993 /* Given a pointer to a die which begins an enumeration, process all
7994    the dies that define the members of the enumeration, and create the
7995    symbol for the enumeration type.
7996
7997    NOTE: We reverse the order of the element list.  */
7998
7999 static void
8000 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8001 {
8002   struct type *this_type;
8003
8004   this_type = get_die_type (die, cu);
8005   if (this_type == NULL)
8006     this_type = read_enumeration_type (die, cu);
8007
8008   if (die->child != NULL)
8009     {
8010       struct die_info *child_die;
8011       struct symbol *sym;
8012       struct field *fields = NULL;
8013       int num_fields = 0;
8014       int unsigned_enum = 1;
8015       char *name;
8016       int flag_enum = 1;
8017       ULONGEST mask = 0;
8018
8019       child_die = die->child;
8020       while (child_die && child_die->tag)
8021         {
8022           if (child_die->tag != DW_TAG_enumerator)
8023             {
8024               process_die (child_die, cu);
8025             }
8026           else
8027             {
8028               name = dwarf2_name (child_die, cu);
8029               if (name)
8030                 {
8031                   sym = new_symbol (child_die, this_type, cu);
8032                   if (SYMBOL_VALUE (sym) < 0)
8033                     {
8034                       unsigned_enum = 0;
8035                       flag_enum = 0;
8036                     }
8037                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
8038                     flag_enum = 0;
8039                   else
8040                     mask |= SYMBOL_VALUE (sym);
8041
8042                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8043                     {
8044                       fields = (struct field *)
8045                         xrealloc (fields,
8046                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
8047                                   * sizeof (struct field));
8048                     }
8049
8050                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8051                   FIELD_TYPE (fields[num_fields]) = NULL;
8052                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8053                   FIELD_BITSIZE (fields[num_fields]) = 0;
8054
8055                   num_fields++;
8056                 }
8057             }
8058
8059           child_die = sibling_die (child_die);
8060         }
8061
8062       if (num_fields)
8063         {
8064           TYPE_NFIELDS (this_type) = num_fields;
8065           TYPE_FIELDS (this_type) = (struct field *)
8066             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8067           memcpy (TYPE_FIELDS (this_type), fields,
8068                   sizeof (struct field) * num_fields);
8069           xfree (fields);
8070         }
8071       if (unsigned_enum)
8072         TYPE_UNSIGNED (this_type) = 1;
8073       if (flag_enum)
8074         TYPE_FLAG_ENUM (this_type) = 1;
8075     }
8076
8077   /* If we are reading an enum from a .debug_types unit, and the enum
8078      is a declaration, and the enum is not the signatured type in the
8079      unit, then we do not want to add a symbol for it.  Adding a
8080      symbol would in some cases obscure the true definition of the
8081      enum, giving users an incomplete type when the definition is
8082      actually available.  Note that we do not want to do this for all
8083      enums which are just declarations, because C++0x allows forward
8084      enum declarations.  */
8085   if (cu->per_cu->debug_types_section
8086       && die_is_declaration (die, cu))
8087     {
8088       struct signatured_type *sig_type;
8089
8090       sig_type
8091         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8092                                             cu->per_cu->debug_types_section,
8093                                             cu->per_cu->offset);
8094       if (sig_type->per_cu.offset.sect_off + sig_type->type_offset.cu_off
8095           != die->offset.sect_off)
8096         return;
8097     }
8098
8099   new_symbol (die, this_type, cu);
8100 }
8101
8102 /* Extract all information from a DW_TAG_array_type DIE and put it in
8103    the DIE's type field.  For now, this only handles one dimensional
8104    arrays.  */
8105
8106 static struct type *
8107 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8108 {
8109   struct objfile *objfile = cu->objfile;
8110   struct die_info *child_die;
8111   struct type *type;
8112   struct type *element_type, *range_type, *index_type;
8113   struct type **range_types = NULL;
8114   struct attribute *attr;
8115   int ndim = 0;
8116   struct cleanup *back_to;
8117   char *name;
8118
8119   element_type = die_type (die, cu);
8120
8121   /* The die_type call above may have already set the type for this DIE.  */
8122   type = get_die_type (die, cu);
8123   if (type)
8124     return type;
8125
8126   /* Irix 6.2 native cc creates array types without children for
8127      arrays with unspecified length.  */
8128   if (die->child == NULL)
8129     {
8130       index_type = objfile_type (objfile)->builtin_int;
8131       range_type = create_range_type (NULL, index_type, 0, -1);
8132       type = create_array_type (NULL, element_type, range_type);
8133       return set_die_type (die, type, cu);
8134     }
8135
8136   back_to = make_cleanup (null_cleanup, NULL);
8137   child_die = die->child;
8138   while (child_die && child_die->tag)
8139     {
8140       if (child_die->tag == DW_TAG_subrange_type)
8141         {
8142           struct type *child_type = read_type_die (child_die, cu);
8143
8144           if (child_type != NULL)
8145             {
8146               /* The range type was succesfully read.  Save it for the
8147                  array type creation.  */
8148               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8149                 {
8150                   range_types = (struct type **)
8151                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8152                               * sizeof (struct type *));
8153                   if (ndim == 0)
8154                     make_cleanup (free_current_contents, &range_types);
8155                 }
8156               range_types[ndim++] = child_type;
8157             }
8158         }
8159       child_die = sibling_die (child_die);
8160     }
8161
8162   /* Dwarf2 dimensions are output from left to right, create the
8163      necessary array types in backwards order.  */
8164
8165   type = element_type;
8166
8167   if (read_array_order (die, cu) == DW_ORD_col_major)
8168     {
8169       int i = 0;
8170
8171       while (i < ndim)
8172         type = create_array_type (NULL, type, range_types[i++]);
8173     }
8174   else
8175     {
8176       while (ndim-- > 0)
8177         type = create_array_type (NULL, type, range_types[ndim]);
8178     }
8179
8180   /* Understand Dwarf2 support for vector types (like they occur on
8181      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8182      array type.  This is not part of the Dwarf2/3 standard yet, but a
8183      custom vendor extension.  The main difference between a regular
8184      array and the vector variant is that vectors are passed by value
8185      to functions.  */
8186   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8187   if (attr)
8188     make_vector_type (type);
8189
8190   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8191      implementation may choose to implement triple vectors using this
8192      attribute.  */
8193   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8194   if (attr)
8195     {
8196       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8197         TYPE_LENGTH (type) = DW_UNSND (attr);
8198       else
8199         complaint (&symfile_complaints,
8200                    _("DW_AT_byte_size for array type smaller "
8201                      "than the total size of elements"));
8202     }
8203
8204   name = dwarf2_name (die, cu);
8205   if (name)
8206     TYPE_NAME (type) = name;
8207
8208   /* Install the type in the die.  */
8209   set_die_type (die, type, cu);
8210
8211   /* set_die_type should be already done.  */
8212   set_descriptive_type (type, die, cu);
8213
8214   do_cleanups (back_to);
8215
8216   return type;
8217 }
8218
8219 static enum dwarf_array_dim_ordering
8220 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8221 {
8222   struct attribute *attr;
8223
8224   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8225
8226   if (attr) return DW_SND (attr);
8227
8228   /* GNU F77 is a special case, as at 08/2004 array type info is the
8229      opposite order to the dwarf2 specification, but data is still
8230      laid out as per normal fortran.
8231
8232      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8233      version checking.  */
8234
8235   if (cu->language == language_fortran
8236       && cu->producer && strstr (cu->producer, "GNU F77"))
8237     {
8238       return DW_ORD_row_major;
8239     }
8240
8241   switch (cu->language_defn->la_array_ordering)
8242     {
8243     case array_column_major:
8244       return DW_ORD_col_major;
8245     case array_row_major:
8246     default:
8247       return DW_ORD_row_major;
8248     };
8249 }
8250
8251 /* Extract all information from a DW_TAG_set_type DIE and put it in
8252    the DIE's type field.  */
8253
8254 static struct type *
8255 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8256 {
8257   struct type *domain_type, *set_type;
8258   struct attribute *attr;
8259
8260   domain_type = die_type (die, cu);
8261
8262   /* The die_type call above may have already set the type for this DIE.  */
8263   set_type = get_die_type (die, cu);
8264   if (set_type)
8265     return set_type;
8266
8267   set_type = create_set_type (NULL, domain_type);
8268
8269   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8270   if (attr)
8271     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8272
8273   return set_die_type (die, set_type, cu);
8274 }
8275
8276 /* First cut: install each common block member as a global variable.  */
8277
8278 static void
8279 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8280 {
8281   struct die_info *child_die;
8282   struct attribute *attr;
8283   struct symbol *sym;
8284   CORE_ADDR base = (CORE_ADDR) 0;
8285
8286   attr = dwarf2_attr (die, DW_AT_location, cu);
8287   if (attr)
8288     {
8289       /* Support the .debug_loc offsets.  */
8290       if (attr_form_is_block (attr))
8291         {
8292           base = decode_locdesc (DW_BLOCK (attr), cu);
8293         }
8294       else if (attr_form_is_section_offset (attr))
8295         {
8296           dwarf2_complex_location_expr_complaint ();
8297         }
8298       else
8299         {
8300           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8301                                                  "common block member");
8302         }
8303     }
8304   if (die->child != NULL)
8305     {
8306       child_die = die->child;
8307       while (child_die && child_die->tag)
8308         {
8309           LONGEST offset;
8310
8311           sym = new_symbol (child_die, NULL, cu);
8312           if (sym != NULL
8313               && handle_data_member_location (child_die, cu, &offset))
8314             {
8315               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8316               add_symbol_to_list (sym, &global_symbols);
8317             }
8318           child_die = sibling_die (child_die);
8319         }
8320     }
8321 }
8322
8323 /* Create a type for a C++ namespace.  */
8324
8325 static struct type *
8326 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8327 {
8328   struct objfile *objfile = cu->objfile;
8329   const char *previous_prefix, *name;
8330   int is_anonymous;
8331   struct type *type;
8332
8333   /* For extensions, reuse the type of the original namespace.  */
8334   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8335     {
8336       struct die_info *ext_die;
8337       struct dwarf2_cu *ext_cu = cu;
8338
8339       ext_die = dwarf2_extension (die, &ext_cu);
8340       type = read_type_die (ext_die, ext_cu);
8341
8342       /* EXT_CU may not be the same as CU.
8343          Ensure TYPE is recorded in CU's type_hash table.  */
8344       return set_die_type (die, type, cu);
8345     }
8346
8347   name = namespace_name (die, &is_anonymous, cu);
8348
8349   /* Now build the name of the current namespace.  */
8350
8351   previous_prefix = determine_prefix (die, cu);
8352   if (previous_prefix[0] != '\0')
8353     name = typename_concat (&objfile->objfile_obstack,
8354                             previous_prefix, name, 0, cu);
8355
8356   /* Create the type.  */
8357   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8358                     objfile);
8359   TYPE_NAME (type) = (char *) name;
8360   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8361
8362   return set_die_type (die, type, cu);
8363 }
8364
8365 /* Read a C++ namespace.  */
8366
8367 static void
8368 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8369 {
8370   struct objfile *objfile = cu->objfile;
8371   int is_anonymous;
8372
8373   /* Add a symbol associated to this if we haven't seen the namespace
8374      before.  Also, add a using directive if it's an anonymous
8375      namespace.  */
8376
8377   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8378     {
8379       struct type *type;
8380
8381       type = read_type_die (die, cu);
8382       new_symbol (die, type, cu);
8383
8384       namespace_name (die, &is_anonymous, cu);
8385       if (is_anonymous)
8386         {
8387           const char *previous_prefix = determine_prefix (die, cu);
8388
8389           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8390                                   NULL, NULL, &objfile->objfile_obstack);
8391         }
8392     }
8393
8394   if (die->child != NULL)
8395     {
8396       struct die_info *child_die = die->child;
8397
8398       while (child_die && child_die->tag)
8399         {
8400           process_die (child_die, cu);
8401           child_die = sibling_die (child_die);
8402         }
8403     }
8404 }
8405
8406 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8407    imported module.  Still we need that type as local Fortran "use ... only"
8408    declaration imports depend on the created type in determine_prefix.  */
8409
8410 static struct type *
8411 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8412 {
8413   struct objfile *objfile = cu->objfile;
8414   char *module_name;
8415   struct type *type;
8416
8417   module_name = dwarf2_name (die, cu);
8418   if (!module_name)
8419     complaint (&symfile_complaints,
8420                _("DW_TAG_module has no name, offset 0x%x"),
8421                die->offset.sect_off);
8422   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8423
8424   /* determine_prefix uses TYPE_TAG_NAME.  */
8425   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8426
8427   return set_die_type (die, type, cu);
8428 }
8429
8430 /* Read a Fortran module.  */
8431
8432 static void
8433 read_module (struct die_info *die, struct dwarf2_cu *cu)
8434 {
8435   struct die_info *child_die = die->child;
8436
8437   while (child_die && child_die->tag)
8438     {
8439       process_die (child_die, cu);
8440       child_die = sibling_die (child_die);
8441     }
8442 }
8443
8444 /* Return the name of the namespace represented by DIE.  Set
8445    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8446    namespace.  */
8447
8448 static const char *
8449 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8450 {
8451   struct die_info *current_die;
8452   const char *name = NULL;
8453
8454   /* Loop through the extensions until we find a name.  */
8455
8456   for (current_die = die;
8457        current_die != NULL;
8458        current_die = dwarf2_extension (die, &cu))
8459     {
8460       name = dwarf2_name (current_die, cu);
8461       if (name != NULL)
8462         break;
8463     }
8464
8465   /* Is it an anonymous namespace?  */
8466
8467   *is_anonymous = (name == NULL);
8468   if (*is_anonymous)
8469     name = CP_ANONYMOUS_NAMESPACE_STR;
8470
8471   return name;
8472 }
8473
8474 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8475    the user defined type vector.  */
8476
8477 static struct type *
8478 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8479 {
8480   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8481   struct comp_unit_head *cu_header = &cu->header;
8482   struct type *type;
8483   struct attribute *attr_byte_size;
8484   struct attribute *attr_address_class;
8485   int byte_size, addr_class;
8486   struct type *target_type;
8487
8488   target_type = die_type (die, cu);
8489
8490   /* The die_type call above may have already set the type for this DIE.  */
8491   type = get_die_type (die, cu);
8492   if (type)
8493     return type;
8494
8495   type = lookup_pointer_type (target_type);
8496
8497   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8498   if (attr_byte_size)
8499     byte_size = DW_UNSND (attr_byte_size);
8500   else
8501     byte_size = cu_header->addr_size;
8502
8503   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8504   if (attr_address_class)
8505     addr_class = DW_UNSND (attr_address_class);
8506   else
8507     addr_class = DW_ADDR_none;
8508
8509   /* If the pointer size or address class is different than the
8510      default, create a type variant marked as such and set the
8511      length accordingly.  */
8512   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8513     {
8514       if (gdbarch_address_class_type_flags_p (gdbarch))
8515         {
8516           int type_flags;
8517
8518           type_flags = gdbarch_address_class_type_flags
8519                          (gdbarch, byte_size, addr_class);
8520           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8521                       == 0);
8522           type = make_type_with_address_space (type, type_flags);
8523         }
8524       else if (TYPE_LENGTH (type) != byte_size)
8525         {
8526           complaint (&symfile_complaints,
8527                      _("invalid pointer size %d"), byte_size);
8528         }
8529       else
8530         {
8531           /* Should we also complain about unhandled address classes?  */
8532         }
8533     }
8534
8535   TYPE_LENGTH (type) = byte_size;
8536   return set_die_type (die, type, cu);
8537 }
8538
8539 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8540    the user defined type vector.  */
8541
8542 static struct type *
8543 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8544 {
8545   struct type *type;
8546   struct type *to_type;
8547   struct type *domain;
8548
8549   to_type = die_type (die, cu);
8550   domain = die_containing_type (die, cu);
8551
8552   /* The calls above may have already set the type for this DIE.  */
8553   type = get_die_type (die, cu);
8554   if (type)
8555     return type;
8556
8557   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8558     type = lookup_methodptr_type (to_type);
8559   else
8560     type = lookup_memberptr_type (to_type, domain);
8561
8562   return set_die_type (die, type, cu);
8563 }
8564
8565 /* Extract all information from a DW_TAG_reference_type DIE and add to
8566    the user defined type vector.  */
8567
8568 static struct type *
8569 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8570 {
8571   struct comp_unit_head *cu_header = &cu->header;
8572   struct type *type, *target_type;
8573   struct attribute *attr;
8574
8575   target_type = die_type (die, cu);
8576
8577   /* The die_type call above may have already set the type for this DIE.  */
8578   type = get_die_type (die, cu);
8579   if (type)
8580     return type;
8581
8582   type = lookup_reference_type (target_type);
8583   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8584   if (attr)
8585     {
8586       TYPE_LENGTH (type) = DW_UNSND (attr);
8587     }
8588   else
8589     {
8590       TYPE_LENGTH (type) = cu_header->addr_size;
8591     }
8592   return set_die_type (die, type, cu);
8593 }
8594
8595 static struct type *
8596 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8597 {
8598   struct type *base_type, *cv_type;
8599
8600   base_type = die_type (die, cu);
8601
8602   /* The die_type call above may have already set the type for this DIE.  */
8603   cv_type = get_die_type (die, cu);
8604   if (cv_type)
8605     return cv_type;
8606
8607   /* In case the const qualifier is applied to an array type, the element type
8608      is so qualified, not the array type (section 6.7.3 of C99).  */
8609   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8610     {
8611       struct type *el_type, *inner_array;
8612
8613       base_type = copy_type (base_type);
8614       inner_array = base_type;
8615
8616       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8617         {
8618           TYPE_TARGET_TYPE (inner_array) =
8619             copy_type (TYPE_TARGET_TYPE (inner_array));
8620           inner_array = TYPE_TARGET_TYPE (inner_array);
8621         }
8622
8623       el_type = TYPE_TARGET_TYPE (inner_array);
8624       TYPE_TARGET_TYPE (inner_array) =
8625         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8626
8627       return set_die_type (die, base_type, cu);
8628     }
8629
8630   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8631   return set_die_type (die, cv_type, cu);
8632 }
8633
8634 static struct type *
8635 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8636 {
8637   struct type *base_type, *cv_type;
8638
8639   base_type = die_type (die, cu);
8640
8641   /* The die_type call above may have already set the type for this DIE.  */
8642   cv_type = get_die_type (die, cu);
8643   if (cv_type)
8644     return cv_type;
8645
8646   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8647   return set_die_type (die, cv_type, cu);
8648 }
8649
8650 /* Extract all information from a DW_TAG_string_type DIE and add to
8651    the user defined type vector.  It isn't really a user defined type,
8652    but it behaves like one, with other DIE's using an AT_user_def_type
8653    attribute to reference it.  */
8654
8655 static struct type *
8656 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8657 {
8658   struct objfile *objfile = cu->objfile;
8659   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8660   struct type *type, *range_type, *index_type, *char_type;
8661   struct attribute *attr;
8662   unsigned int length;
8663
8664   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8665   if (attr)
8666     {
8667       length = DW_UNSND (attr);
8668     }
8669   else
8670     {
8671       /* Check for the DW_AT_byte_size attribute.  */
8672       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8673       if (attr)
8674         {
8675           length = DW_UNSND (attr);
8676         }
8677       else
8678         {
8679           length = 1;
8680         }
8681     }
8682
8683   index_type = objfile_type (objfile)->builtin_int;
8684   range_type = create_range_type (NULL, index_type, 1, length);
8685   char_type = language_string_char_type (cu->language_defn, gdbarch);
8686   type = create_string_type (NULL, char_type, range_type);
8687
8688   return set_die_type (die, type, cu);
8689 }
8690
8691 /* Handle DIES due to C code like:
8692
8693    struct foo
8694    {
8695    int (*funcp)(int a, long l);
8696    int b;
8697    };
8698
8699    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8700
8701 static struct type *
8702 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8703 {
8704   struct objfile *objfile = cu->objfile;
8705   struct type *type;            /* Type that this function returns.  */
8706   struct type *ftype;           /* Function that returns above type.  */
8707   struct attribute *attr;
8708
8709   type = die_type (die, cu);
8710
8711   /* The die_type call above may have already set the type for this DIE.  */
8712   ftype = get_die_type (die, cu);
8713   if (ftype)
8714     return ftype;
8715
8716   ftype = lookup_function_type (type);
8717
8718   /* All functions in C++, Pascal and Java have prototypes.  */
8719   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8720   if ((attr && (DW_UNSND (attr) != 0))
8721       || cu->language == language_cplus
8722       || cu->language == language_java
8723       || cu->language == language_pascal)
8724     TYPE_PROTOTYPED (ftype) = 1;
8725   else if (producer_is_realview (cu->producer))
8726     /* RealView does not emit DW_AT_prototyped.  We can not
8727        distinguish prototyped and unprototyped functions; default to
8728        prototyped, since that is more common in modern code (and
8729        RealView warns about unprototyped functions).  */
8730     TYPE_PROTOTYPED (ftype) = 1;
8731
8732   /* Store the calling convention in the type if it's available in
8733      the subroutine die.  Otherwise set the calling convention to
8734      the default value DW_CC_normal.  */
8735   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8736   if (attr)
8737     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8738   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8739     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8740   else
8741     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8742
8743   /* We need to add the subroutine type to the die immediately so
8744      we don't infinitely recurse when dealing with parameters
8745      declared as the same subroutine type.  */
8746   set_die_type (die, ftype, cu);
8747
8748   if (die->child != NULL)
8749     {
8750       struct type *void_type = objfile_type (objfile)->builtin_void;
8751       struct die_info *child_die;
8752       int nparams, iparams;
8753
8754       /* Count the number of parameters.
8755          FIXME: GDB currently ignores vararg functions, but knows about
8756          vararg member functions.  */
8757       nparams = 0;
8758       child_die = die->child;
8759       while (child_die && child_die->tag)
8760         {
8761           if (child_die->tag == DW_TAG_formal_parameter)
8762             nparams++;
8763           else if (child_die->tag == DW_TAG_unspecified_parameters)
8764             TYPE_VARARGS (ftype) = 1;
8765           child_die = sibling_die (child_die);
8766         }
8767
8768       /* Allocate storage for parameters and fill them in.  */
8769       TYPE_NFIELDS (ftype) = nparams;
8770       TYPE_FIELDS (ftype) = (struct field *)
8771         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8772
8773       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8774          even if we error out during the parameters reading below.  */
8775       for (iparams = 0; iparams < nparams; iparams++)
8776         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8777
8778       iparams = 0;
8779       child_die = die->child;
8780       while (child_die && child_die->tag)
8781         {
8782           if (child_die->tag == DW_TAG_formal_parameter)
8783             {
8784               struct type *arg_type;
8785
8786               /* DWARF version 2 has no clean way to discern C++
8787                  static and non-static member functions.  G++ helps
8788                  GDB by marking the first parameter for non-static
8789                  member functions (which is the this pointer) as
8790                  artificial.  We pass this information to
8791                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8792
8793                  DWARF version 3 added DW_AT_object_pointer, which GCC
8794                  4.5 does not yet generate.  */
8795               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8796               if (attr)
8797                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8798               else
8799                 {
8800                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8801
8802                   /* GCC/43521: In java, the formal parameter
8803                      "this" is sometimes not marked with DW_AT_artificial.  */
8804                   if (cu->language == language_java)
8805                     {
8806                       const char *name = dwarf2_name (child_die, cu);
8807
8808                       if (name && !strcmp (name, "this"))
8809                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8810                     }
8811                 }
8812               arg_type = die_type (child_die, cu);
8813
8814               /* RealView does not mark THIS as const, which the testsuite
8815                  expects.  GCC marks THIS as const in method definitions,
8816                  but not in the class specifications (GCC PR 43053).  */
8817               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8818                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8819                 {
8820                   int is_this = 0;
8821                   struct dwarf2_cu *arg_cu = cu;
8822                   const char *name = dwarf2_name (child_die, cu);
8823
8824                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8825                   if (attr)
8826                     {
8827                       /* If the compiler emits this, use it.  */
8828                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8829                         is_this = 1;
8830                     }
8831                   else if (name && strcmp (name, "this") == 0)
8832                     /* Function definitions will have the argument names.  */
8833                     is_this = 1;
8834                   else if (name == NULL && iparams == 0)
8835                     /* Declarations may not have the names, so like
8836                        elsewhere in GDB, assume an artificial first
8837                        argument is "this".  */
8838                     is_this = 1;
8839
8840                   if (is_this)
8841                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8842                                              arg_type, 0);
8843                 }
8844
8845               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8846               iparams++;
8847             }
8848           child_die = sibling_die (child_die);
8849         }
8850     }
8851
8852   return ftype;
8853 }
8854
8855 static struct type *
8856 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8857 {
8858   struct objfile *objfile = cu->objfile;
8859   const char *name = NULL;
8860   struct type *this_type, *target_type;
8861
8862   name = dwarf2_full_name (NULL, die, cu);
8863   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8864                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8865   TYPE_NAME (this_type) = (char *) name;
8866   set_die_type (die, this_type, cu);
8867   target_type = die_type (die, cu);
8868   if (target_type != this_type)
8869     TYPE_TARGET_TYPE (this_type) = target_type;
8870   else
8871     {
8872       /* Self-referential typedefs are, it seems, not allowed by the DWARF
8873          spec and cause infinite loops in GDB.  */
8874       complaint (&symfile_complaints,
8875                  _("Self-referential DW_TAG_typedef "
8876                    "- DIE at 0x%x [in module %s]"),
8877                  die->offset.sect_off, objfile->name);
8878       TYPE_TARGET_TYPE (this_type) = NULL;
8879     }
8880   return this_type;
8881 }
8882
8883 /* Find a representation of a given base type and install
8884    it in the TYPE field of the die.  */
8885
8886 static struct type *
8887 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8888 {
8889   struct objfile *objfile = cu->objfile;
8890   struct type *type;
8891   struct attribute *attr;
8892   int encoding = 0, size = 0;
8893   char *name;
8894   enum type_code code = TYPE_CODE_INT;
8895   int type_flags = 0;
8896   struct type *target_type = NULL;
8897
8898   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8899   if (attr)
8900     {
8901       encoding = DW_UNSND (attr);
8902     }
8903   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8904   if (attr)
8905     {
8906       size = DW_UNSND (attr);
8907     }
8908   name = dwarf2_name (die, cu);
8909   if (!name)
8910     {
8911       complaint (&symfile_complaints,
8912                  _("DW_AT_name missing from DW_TAG_base_type"));
8913     }
8914
8915   switch (encoding)
8916     {
8917       case DW_ATE_address:
8918         /* Turn DW_ATE_address into a void * pointer.  */
8919         code = TYPE_CODE_PTR;
8920         type_flags |= TYPE_FLAG_UNSIGNED;
8921         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8922         break;
8923       case DW_ATE_boolean:
8924         code = TYPE_CODE_BOOL;
8925         type_flags |= TYPE_FLAG_UNSIGNED;
8926         break;
8927       case DW_ATE_complex_float:
8928         code = TYPE_CODE_COMPLEX;
8929         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8930         break;
8931       case DW_ATE_decimal_float:
8932         code = TYPE_CODE_DECFLOAT;
8933         break;
8934       case DW_ATE_float:
8935         code = TYPE_CODE_FLT;
8936         break;
8937       case DW_ATE_signed:
8938         break;
8939       case DW_ATE_unsigned:
8940         type_flags |= TYPE_FLAG_UNSIGNED;
8941         if (cu->language == language_fortran
8942             && name
8943             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8944           code = TYPE_CODE_CHAR;
8945         break;
8946       case DW_ATE_signed_char:
8947         if (cu->language == language_ada || cu->language == language_m2
8948             || cu->language == language_pascal
8949             || cu->language == language_fortran)
8950           code = TYPE_CODE_CHAR;
8951         break;
8952       case DW_ATE_unsigned_char:
8953         if (cu->language == language_ada || cu->language == language_m2
8954             || cu->language == language_pascal
8955             || cu->language == language_fortran)
8956           code = TYPE_CODE_CHAR;
8957         type_flags |= TYPE_FLAG_UNSIGNED;
8958         break;
8959       case DW_ATE_UTF:
8960         /* We just treat this as an integer and then recognize the
8961            type by name elsewhere.  */
8962         break;
8963
8964       default:
8965         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8966                    dwarf_type_encoding_name (encoding));
8967         break;
8968     }
8969
8970   type = init_type (code, size, type_flags, NULL, objfile);
8971   TYPE_NAME (type) = name;
8972   TYPE_TARGET_TYPE (type) = target_type;
8973
8974   if (name && strcmp (name, "char") == 0)
8975     TYPE_NOSIGN (type) = 1;
8976
8977   return set_die_type (die, type, cu);
8978 }
8979
8980 /* Read the given DW_AT_subrange DIE.  */
8981
8982 static struct type *
8983 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8984 {
8985   struct type *base_type;
8986   struct type *range_type;
8987   struct attribute *attr;
8988   LONGEST low = 0;
8989   LONGEST high = -1;
8990   char *name;
8991   LONGEST negative_mask;
8992
8993   base_type = die_type (die, cu);
8994   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8995   check_typedef (base_type);
8996
8997   /* The die_type call above may have already set the type for this DIE.  */
8998   range_type = get_die_type (die, cu);
8999   if (range_type)
9000     return range_type;
9001
9002   if (cu->language == language_fortran)
9003     {
9004       /* FORTRAN implies a lower bound of 1, if not given.  */
9005       low = 1;
9006     }
9007
9008   /* FIXME: For variable sized arrays either of these could be
9009      a variable rather than a constant value.  We'll allow it,
9010      but we don't know how to handle it.  */
9011   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
9012   if (attr)
9013     low = dwarf2_get_attr_constant_value (attr, 0);
9014
9015   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
9016   if (attr)
9017     {
9018       if (attr_form_is_block (attr) || is_ref_attr (attr))
9019         {
9020           /* GCC encodes arrays with unspecified or dynamic length
9021              with a DW_FORM_block1 attribute or a reference attribute.
9022              FIXME: GDB does not yet know how to handle dynamic
9023              arrays properly, treat them as arrays with unspecified
9024              length for now.
9025
9026              FIXME: jimb/2003-09-22: GDB does not really know
9027              how to handle arrays of unspecified length
9028              either; we just represent them as zero-length
9029              arrays.  Choose an appropriate upper bound given
9030              the lower bound we've computed above.  */
9031           high = low - 1;
9032         }
9033       else
9034         high = dwarf2_get_attr_constant_value (attr, 1);
9035     }
9036   else
9037     {
9038       attr = dwarf2_attr (die, DW_AT_count, cu);
9039       if (attr)
9040         {
9041           int count = dwarf2_get_attr_constant_value (attr, 1);
9042           high = low + count - 1;
9043         }
9044       else
9045         {
9046           /* Unspecified array length.  */
9047           high = low - 1;
9048         }
9049     }
9050
9051   /* Dwarf-2 specifications explicitly allows to create subrange types
9052      without specifying a base type.
9053      In that case, the base type must be set to the type of
9054      the lower bound, upper bound or count, in that order, if any of these
9055      three attributes references an object that has a type.
9056      If no base type is found, the Dwarf-2 specifications say that
9057      a signed integer type of size equal to the size of an address should
9058      be used.
9059      For the following C code: `extern char gdb_int [];'
9060      GCC produces an empty range DIE.
9061      FIXME: muller/2010-05-28: Possible references to object for low bound,
9062      high bound or count are not yet handled by this code.  */
9063   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9064     {
9065       struct objfile *objfile = cu->objfile;
9066       struct gdbarch *gdbarch = get_objfile_arch (objfile);
9067       int addr_size = gdbarch_addr_bit (gdbarch) /8;
9068       struct type *int_type = objfile_type (objfile)->builtin_int;
9069
9070       /* Test "int", "long int", and "long long int" objfile types,
9071          and select the first one having a size above or equal to the
9072          architecture address size.  */
9073       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9074         base_type = int_type;
9075       else
9076         {
9077           int_type = objfile_type (objfile)->builtin_long;
9078           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9079             base_type = int_type;
9080           else
9081             {
9082               int_type = objfile_type (objfile)->builtin_long_long;
9083               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9084                 base_type = int_type;
9085             }
9086         }
9087     }
9088
9089   negative_mask =
9090     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9091   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9092     low |= negative_mask;
9093   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9094     high |= negative_mask;
9095
9096   range_type = create_range_type (NULL, base_type, low, high);
9097
9098   /* Mark arrays with dynamic length at least as an array of unspecified
9099      length.  GDB could check the boundary but before it gets implemented at
9100      least allow accessing the array elements.  */
9101   if (attr && attr_form_is_block (attr))
9102     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9103
9104   /* Ada expects an empty array on no boundary attributes.  */
9105   if (attr == NULL && cu->language != language_ada)
9106     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9107
9108   name = dwarf2_name (die, cu);
9109   if (name)
9110     TYPE_NAME (range_type) = name;
9111
9112   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9113   if (attr)
9114     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9115
9116   set_die_type (die, range_type, cu);
9117
9118   /* set_die_type should be already done.  */
9119   set_descriptive_type (range_type, die, cu);
9120
9121   return range_type;
9122 }
9123
9124 static struct type *
9125 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9126 {
9127   struct type *type;
9128
9129   /* For now, we only support the C meaning of an unspecified type: void.  */
9130
9131   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9132   TYPE_NAME (type) = dwarf2_name (die, cu);
9133
9134   return set_die_type (die, type, cu);
9135 }
9136
9137 /* Trivial hash function for die_info: the hash value of a DIE
9138    is its offset in .debug_info for this objfile.  */
9139
9140 static hashval_t
9141 die_hash (const void *item)
9142 {
9143   const struct die_info *die = item;
9144
9145   return die->offset.sect_off;
9146 }
9147
9148 /* Trivial comparison function for die_info structures: two DIEs
9149    are equal if they have the same offset.  */
9150
9151 static int
9152 die_eq (const void *item_lhs, const void *item_rhs)
9153 {
9154   const struct die_info *die_lhs = item_lhs;
9155   const struct die_info *die_rhs = item_rhs;
9156
9157   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
9158 }
9159
9160 /* Read a whole compilation unit into a linked list of dies.  */
9161
9162 static struct die_info *
9163 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9164 {
9165   struct die_reader_specs reader_specs;
9166   int read_abbrevs = 0;
9167   struct cleanup *back_to = NULL;
9168   struct die_info *die;
9169
9170   if (cu->dwarf2_abbrevs == NULL)
9171     {
9172       dwarf2_read_abbrevs (cu);
9173       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9174       read_abbrevs = 1;
9175     }
9176
9177   gdb_assert (cu->die_hash == NULL);
9178   cu->die_hash
9179     = htab_create_alloc_ex (cu->header.length / 12,
9180                             die_hash,
9181                             die_eq,
9182                             NULL,
9183                             &cu->comp_unit_obstack,
9184                             hashtab_obstack_allocate,
9185                             dummy_obstack_deallocate);
9186
9187   init_cu_die_reader (&reader_specs, cu);
9188
9189   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9190
9191   if (read_abbrevs)
9192     do_cleanups (back_to);
9193
9194   return die;
9195 }
9196
9197 /* Main entry point for reading a DIE and all children.
9198    Read the DIE and dump it if requested.  */
9199
9200 static struct die_info *
9201 read_die_and_children (const struct die_reader_specs *reader,
9202                        gdb_byte *info_ptr,
9203                        gdb_byte **new_info_ptr,
9204                        struct die_info *parent)
9205 {
9206   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9207                                                      new_info_ptr, parent);
9208
9209   if (dwarf2_die_debug)
9210     {
9211       fprintf_unfiltered (gdb_stdlog,
9212                           "\nRead die from %s of %s:\n",
9213                           (reader->cu->per_cu->debug_types_section
9214                            ? ".debug_types"
9215                            : ".debug_info"),
9216                           reader->abfd->filename);
9217       dump_die (result, dwarf2_die_debug);
9218     }
9219
9220   return result;
9221 }
9222
9223 /* Read a single die and all its descendents.  Set the die's sibling
9224    field to NULL; set other fields in the die correctly, and set all
9225    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9226    location of the info_ptr after reading all of those dies.  PARENT
9227    is the parent of the die in question.  */
9228
9229 static struct die_info *
9230 read_die_and_children_1 (const struct die_reader_specs *reader,
9231                          gdb_byte *info_ptr,
9232                          gdb_byte **new_info_ptr,
9233                          struct die_info *parent)
9234 {
9235   struct die_info *die;
9236   gdb_byte *cur_ptr;
9237   int has_children;
9238
9239   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9240   if (die == NULL)
9241     {
9242       *new_info_ptr = cur_ptr;
9243       return NULL;
9244     }
9245   store_in_ref_table (die, reader->cu);
9246
9247   if (has_children)
9248     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9249   else
9250     {
9251       die->child = NULL;
9252       *new_info_ptr = cur_ptr;
9253     }
9254
9255   die->sibling = NULL;
9256   die->parent = parent;
9257   return die;
9258 }
9259
9260 /* Read a die, all of its descendents, and all of its siblings; set
9261    all of the fields of all of the dies correctly.  Arguments are as
9262    in read_die_and_children.  */
9263
9264 static struct die_info *
9265 read_die_and_siblings (const struct die_reader_specs *reader,
9266                        gdb_byte *info_ptr,
9267                        gdb_byte **new_info_ptr,
9268                        struct die_info *parent)
9269 {
9270   struct die_info *first_die, *last_sibling;
9271   gdb_byte *cur_ptr;
9272
9273   cur_ptr = info_ptr;
9274   first_die = last_sibling = NULL;
9275
9276   while (1)
9277     {
9278       struct die_info *die
9279         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9280
9281       if (die == NULL)
9282         {
9283           *new_info_ptr = cur_ptr;
9284           return first_die;
9285         }
9286
9287       if (!first_die)
9288         first_die = die;
9289       else
9290         last_sibling->sibling = die;
9291
9292       last_sibling = die;
9293     }
9294 }
9295
9296 /* Read the die from the .debug_info section buffer.  Set DIEP to
9297    point to a newly allocated die with its information, except for its
9298    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9299    whether the die has children or not.  */
9300
9301 static gdb_byte *
9302 read_full_die (const struct die_reader_specs *reader,
9303                struct die_info **diep, gdb_byte *info_ptr,
9304                int *has_children)
9305 {
9306   unsigned int abbrev_number, bytes_read, i;
9307   sect_offset offset;
9308   struct abbrev_info *abbrev;
9309   struct die_info *die;
9310   struct dwarf2_cu *cu = reader->cu;
9311   bfd *abfd = reader->abfd;
9312
9313   offset.sect_off = info_ptr - reader->buffer;
9314   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9315   info_ptr += bytes_read;
9316   if (!abbrev_number)
9317     {
9318       *diep = NULL;
9319       *has_children = 0;
9320       return info_ptr;
9321     }
9322
9323   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9324   if (!abbrev)
9325     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9326            abbrev_number,
9327            bfd_get_filename (abfd));
9328
9329   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9330   die->offset = offset;
9331   die->tag = abbrev->tag;
9332   die->abbrev = abbrev_number;
9333
9334   die->num_attrs = abbrev->num_attrs;
9335
9336   for (i = 0; i < abbrev->num_attrs; ++i)
9337     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9338                                abfd, info_ptr, cu);
9339
9340   *diep = die;
9341   *has_children = abbrev->has_children;
9342   return info_ptr;
9343 }
9344
9345 /* In DWARF version 2, the description of the debugging information is
9346    stored in a separate .debug_abbrev section.  Before we read any
9347    dies from a section we read in all abbreviations and install them
9348    in a hash table.  This function also sets flags in CU describing
9349    the data found in the abbrev table.  */
9350
9351 static void
9352 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9353 {
9354   bfd *abfd = cu->objfile->obfd;
9355   struct comp_unit_head *cu_header = &cu->header;
9356   gdb_byte *abbrev_ptr;
9357   struct abbrev_info *cur_abbrev;
9358   unsigned int abbrev_number, bytes_read, abbrev_name;
9359   unsigned int abbrev_form, hash_number;
9360   struct attr_abbrev *cur_attrs;
9361   unsigned int allocated_attrs;
9362
9363   /* Initialize dwarf2 abbrevs.  */
9364   obstack_init (&cu->abbrev_obstack);
9365   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9366                                       (ABBREV_HASH_SIZE
9367                                        * sizeof (struct abbrev_info *)));
9368   memset (cu->dwarf2_abbrevs, 0,
9369           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9370
9371   dwarf2_read_section (dwarf2_per_objfile->objfile,
9372                        &dwarf2_per_objfile->abbrev);
9373   abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9374                 + cu_header->abbrev_offset.sect_off);
9375   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9376   abbrev_ptr += bytes_read;
9377
9378   allocated_attrs = ATTR_ALLOC_CHUNK;
9379   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9380
9381   /* Loop until we reach an abbrev number of 0.  */
9382   while (abbrev_number)
9383     {
9384       cur_abbrev = dwarf_alloc_abbrev (cu);
9385
9386       /* read in abbrev header */
9387       cur_abbrev->number = abbrev_number;
9388       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9389       abbrev_ptr += bytes_read;
9390       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9391       abbrev_ptr += 1;
9392
9393       /* now read in declarations */
9394       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9395       abbrev_ptr += bytes_read;
9396       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9397       abbrev_ptr += bytes_read;
9398       while (abbrev_name)
9399         {
9400           if (cur_abbrev->num_attrs == allocated_attrs)
9401             {
9402               allocated_attrs += ATTR_ALLOC_CHUNK;
9403               cur_attrs
9404                 = xrealloc (cur_attrs, (allocated_attrs
9405                                         * sizeof (struct attr_abbrev)));
9406             }
9407
9408           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9409           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9410           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9411           abbrev_ptr += bytes_read;
9412           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9413           abbrev_ptr += bytes_read;
9414         }
9415
9416       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9417                                          (cur_abbrev->num_attrs
9418                                           * sizeof (struct attr_abbrev)));
9419       memcpy (cur_abbrev->attrs, cur_attrs,
9420               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9421
9422       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9423       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9424       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9425
9426       /* Get next abbreviation.
9427          Under Irix6 the abbreviations for a compilation unit are not
9428          always properly terminated with an abbrev number of 0.
9429          Exit loop if we encounter an abbreviation which we have
9430          already read (which means we are about to read the abbreviations
9431          for the next compile unit) or if the end of the abbreviation
9432          table is reached.  */
9433       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9434           >= dwarf2_per_objfile->abbrev.size)
9435         break;
9436       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9437       abbrev_ptr += bytes_read;
9438       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9439         break;
9440     }
9441
9442   xfree (cur_attrs);
9443 }
9444
9445 /* Release the memory used by the abbrev table for a compilation unit.  */
9446
9447 static void
9448 dwarf2_free_abbrev_table (void *ptr_to_cu)
9449 {
9450   struct dwarf2_cu *cu = ptr_to_cu;
9451
9452   obstack_free (&cu->abbrev_obstack, NULL);
9453   cu->dwarf2_abbrevs = NULL;
9454 }
9455
9456 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9457
9458 static struct abbrev_info *
9459 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9460 {
9461   unsigned int hash_number;
9462   struct abbrev_info *abbrev;
9463
9464   hash_number = number % ABBREV_HASH_SIZE;
9465   abbrev = cu->dwarf2_abbrevs[hash_number];
9466
9467   while (abbrev)
9468     {
9469       if (abbrev->number == number)
9470         return abbrev;
9471       else
9472         abbrev = abbrev->next;
9473     }
9474   return NULL;
9475 }
9476
9477 /* Returns nonzero if TAG represents a type that we might generate a partial
9478    symbol for.  */
9479
9480 static int
9481 is_type_tag_for_partial (int tag)
9482 {
9483   switch (tag)
9484     {
9485 #if 0
9486     /* Some types that would be reasonable to generate partial symbols for,
9487        that we don't at present.  */
9488     case DW_TAG_array_type:
9489     case DW_TAG_file_type:
9490     case DW_TAG_ptr_to_member_type:
9491     case DW_TAG_set_type:
9492     case DW_TAG_string_type:
9493     case DW_TAG_subroutine_type:
9494 #endif
9495     case DW_TAG_base_type:
9496     case DW_TAG_class_type:
9497     case DW_TAG_interface_type:
9498     case DW_TAG_enumeration_type:
9499     case DW_TAG_structure_type:
9500     case DW_TAG_subrange_type:
9501     case DW_TAG_typedef:
9502     case DW_TAG_union_type:
9503       return 1;
9504     default:
9505       return 0;
9506     }
9507 }
9508
9509 /* Load all DIEs that are interesting for partial symbols into memory.  */
9510
9511 static struct partial_die_info *
9512 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9513                    int building_psymtab, struct dwarf2_cu *cu)
9514 {
9515   struct objfile *objfile = cu->objfile;
9516   struct partial_die_info *part_die;
9517   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9518   struct abbrev_info *abbrev;
9519   unsigned int bytes_read;
9520   unsigned int load_all = 0;
9521   int nesting_level = 1;
9522
9523   parent_die = NULL;
9524   last_die = NULL;
9525
9526   gdb_assert (cu->per_cu != NULL);
9527   if (cu->per_cu->load_all_dies)
9528     load_all = 1;
9529
9530   cu->partial_dies
9531     = htab_create_alloc_ex (cu->header.length / 12,
9532                             partial_die_hash,
9533                             partial_die_eq,
9534                             NULL,
9535                             &cu->comp_unit_obstack,
9536                             hashtab_obstack_allocate,
9537                             dummy_obstack_deallocate);
9538
9539   part_die = obstack_alloc (&cu->comp_unit_obstack,
9540                             sizeof (struct partial_die_info));
9541
9542   while (1)
9543     {
9544       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9545
9546       /* A NULL abbrev means the end of a series of children.  */
9547       if (abbrev == NULL)
9548         {
9549           if (--nesting_level == 0)
9550             {
9551               /* PART_DIE was probably the last thing allocated on the
9552                  comp_unit_obstack, so we could call obstack_free
9553                  here.  We don't do that because the waste is small,
9554                  and will be cleaned up when we're done with this
9555                  compilation unit.  This way, we're also more robust
9556                  against other users of the comp_unit_obstack.  */
9557               return first_die;
9558             }
9559           info_ptr += bytes_read;
9560           last_die = parent_die;
9561           parent_die = parent_die->die_parent;
9562           continue;
9563         }
9564
9565       /* Check for template arguments.  We never save these; if
9566          they're seen, we just mark the parent, and go on our way.  */
9567       if (parent_die != NULL
9568           && cu->language == language_cplus
9569           && (abbrev->tag == DW_TAG_template_type_param
9570               || abbrev->tag == DW_TAG_template_value_param))
9571         {
9572           parent_die->has_template_arguments = 1;
9573
9574           if (!load_all)
9575             {
9576               /* We don't need a partial DIE for the template argument.  */
9577               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9578                                        cu);
9579               continue;
9580             }
9581         }
9582
9583       /* We only recurse into c++ subprograms looking for template arguments.
9584          Skip their other children.  */
9585       if (!load_all
9586           && cu->language == language_cplus
9587           && parent_die != NULL
9588           && parent_die->tag == DW_TAG_subprogram)
9589         {
9590           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9591           continue;
9592         }
9593
9594       /* Check whether this DIE is interesting enough to save.  Normally
9595          we would not be interested in members here, but there may be
9596          later variables referencing them via DW_AT_specification (for
9597          static members).  */
9598       if (!load_all
9599           && !is_type_tag_for_partial (abbrev->tag)
9600           && abbrev->tag != DW_TAG_constant
9601           && abbrev->tag != DW_TAG_enumerator
9602           && abbrev->tag != DW_TAG_subprogram
9603           && abbrev->tag != DW_TAG_lexical_block
9604           && abbrev->tag != DW_TAG_variable
9605           && abbrev->tag != DW_TAG_namespace
9606           && abbrev->tag != DW_TAG_module
9607           && abbrev->tag != DW_TAG_member)
9608         {
9609           /* Otherwise we skip to the next sibling, if any.  */
9610           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9611           continue;
9612         }
9613
9614       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9615                                    buffer, info_ptr, cu);
9616
9617       /* This two-pass algorithm for processing partial symbols has a
9618          high cost in cache pressure.  Thus, handle some simple cases
9619          here which cover the majority of C partial symbols.  DIEs
9620          which neither have specification tags in them, nor could have
9621          specification tags elsewhere pointing at them, can simply be
9622          processed and discarded.
9623
9624          This segment is also optional; scan_partial_symbols and
9625          add_partial_symbol will handle these DIEs if we chain
9626          them in normally.  When compilers which do not emit large
9627          quantities of duplicate debug information are more common,
9628          this code can probably be removed.  */
9629
9630       /* Any complete simple types at the top level (pretty much all
9631          of them, for a language without namespaces), can be processed
9632          directly.  */
9633       if (parent_die == NULL
9634           && part_die->has_specification == 0
9635           && part_die->is_declaration == 0
9636           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9637               || part_die->tag == DW_TAG_base_type
9638               || part_die->tag == DW_TAG_subrange_type))
9639         {
9640           if (building_psymtab && part_die->name != NULL)
9641             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9642                                  VAR_DOMAIN, LOC_TYPEDEF,
9643                                  &objfile->static_psymbols,
9644                                  0, (CORE_ADDR) 0, cu->language, objfile);
9645           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9646           continue;
9647         }
9648
9649       /* The exception for DW_TAG_typedef with has_children above is
9650          a workaround of GCC PR debug/47510.  In the case of this complaint
9651          type_name_no_tag_or_error will error on such types later.
9652
9653          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9654          it could not find the child DIEs referenced later, this is checked
9655          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9656
9657       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9658         complaint (&symfile_complaints,
9659                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9660                      "- DIE at 0x%x [in module %s]"),
9661                    part_die->offset.sect_off, objfile->name);
9662
9663       /* If we're at the second level, and we're an enumerator, and
9664          our parent has no specification (meaning possibly lives in a
9665          namespace elsewhere), then we can add the partial symbol now
9666          instead of queueing it.  */
9667       if (part_die->tag == DW_TAG_enumerator
9668           && parent_die != NULL
9669           && parent_die->die_parent == NULL
9670           && parent_die->tag == DW_TAG_enumeration_type
9671           && parent_die->has_specification == 0)
9672         {
9673           if (part_die->name == NULL)
9674             complaint (&symfile_complaints,
9675                        _("malformed enumerator DIE ignored"));
9676           else if (building_psymtab)
9677             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9678                                  VAR_DOMAIN, LOC_CONST,
9679                                  (cu->language == language_cplus
9680                                   || cu->language == language_java)
9681                                  ? &objfile->global_psymbols
9682                                  : &objfile->static_psymbols,
9683                                  0, (CORE_ADDR) 0, cu->language, objfile);
9684
9685           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9686           continue;
9687         }
9688
9689       /* We'll save this DIE so link it in.  */
9690       part_die->die_parent = parent_die;
9691       part_die->die_sibling = NULL;
9692       part_die->die_child = NULL;
9693
9694       if (last_die && last_die == parent_die)
9695         last_die->die_child = part_die;
9696       else if (last_die)
9697         last_die->die_sibling = part_die;
9698
9699       last_die = part_die;
9700
9701       if (first_die == NULL)
9702         first_die = part_die;
9703
9704       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9705          find interesting need to be in the hash table, because we
9706          also have the parent/sibling/child chains; only those that we
9707          might refer to by offset later during partial symbol reading.
9708
9709          For now this means things that might have be the target of a
9710          DW_AT_specification, DW_AT_abstract_origin, or
9711          DW_AT_extension.  DW_AT_extension will refer only to
9712          namespaces; DW_AT_abstract_origin refers to functions (and
9713          many things under the function DIE, but we do not recurse
9714          into function DIEs during partial symbol reading) and
9715          possibly variables as well; DW_AT_specification refers to
9716          declarations.  Declarations ought to have the DW_AT_declaration
9717          flag.  It happens that GCC forgets to put it in sometimes, but
9718          only for functions, not for types.
9719
9720          Adding more things than necessary to the hash table is harmless
9721          except for the performance cost.  Adding too few will result in
9722          wasted time in find_partial_die, when we reread the compilation
9723          unit with load_all_dies set.  */
9724
9725       if (load_all
9726           || abbrev->tag == DW_TAG_constant
9727           || abbrev->tag == DW_TAG_subprogram
9728           || abbrev->tag == DW_TAG_variable
9729           || abbrev->tag == DW_TAG_namespace
9730           || part_die->is_declaration)
9731         {
9732           void **slot;
9733
9734           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9735                                            part_die->offset.sect_off, INSERT);
9736           *slot = part_die;
9737         }
9738
9739       part_die = obstack_alloc (&cu->comp_unit_obstack,
9740                                 sizeof (struct partial_die_info));
9741
9742       /* For some DIEs we want to follow their children (if any).  For C
9743          we have no reason to follow the children of structures; for other
9744          languages we have to, so that we can get at method physnames
9745          to infer fully qualified class names, for DW_AT_specification,
9746          and for C++ template arguments.  For C++, we also look one level
9747          inside functions to find template arguments (if the name of the
9748          function does not already contain the template arguments).
9749
9750          For Ada, we need to scan the children of subprograms and lexical
9751          blocks as well because Ada allows the definition of nested
9752          entities that could be interesting for the debugger, such as
9753          nested subprograms for instance.  */
9754       if (last_die->has_children
9755           && (load_all
9756               || last_die->tag == DW_TAG_namespace
9757               || last_die->tag == DW_TAG_module
9758               || last_die->tag == DW_TAG_enumeration_type
9759               || (cu->language == language_cplus
9760                   && last_die->tag == DW_TAG_subprogram
9761                   && (last_die->name == NULL
9762                       || strchr (last_die->name, '<') == NULL))
9763               || (cu->language != language_c
9764                   && (last_die->tag == DW_TAG_class_type
9765                       || last_die->tag == DW_TAG_interface_type
9766                       || last_die->tag == DW_TAG_structure_type
9767                       || last_die->tag == DW_TAG_union_type))
9768               || (cu->language == language_ada
9769                   && (last_die->tag == DW_TAG_subprogram
9770                       || last_die->tag == DW_TAG_lexical_block))))
9771         {
9772           nesting_level++;
9773           parent_die = last_die;
9774           continue;
9775         }
9776
9777       /* Otherwise we skip to the next sibling, if any.  */
9778       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9779
9780       /* Back to the top, do it again.  */
9781     }
9782 }
9783
9784 /* Read a minimal amount of information into the minimal die structure.  */
9785
9786 static gdb_byte *
9787 read_partial_die (struct partial_die_info *part_die,
9788                   struct abbrev_info *abbrev,
9789                   unsigned int abbrev_len, bfd *abfd,
9790                   gdb_byte *buffer, gdb_byte *info_ptr,
9791                   struct dwarf2_cu *cu)
9792 {
9793   struct objfile *objfile = cu->objfile;
9794   unsigned int i;
9795   struct attribute attr;
9796   int has_low_pc_attr = 0;
9797   int has_high_pc_attr = 0;
9798
9799   memset (part_die, 0, sizeof (struct partial_die_info));
9800
9801   part_die->offset.sect_off = info_ptr - buffer;
9802
9803   info_ptr += abbrev_len;
9804
9805   if (abbrev == NULL)
9806     return info_ptr;
9807
9808   part_die->tag = abbrev->tag;
9809   part_die->has_children = abbrev->has_children;
9810
9811   for (i = 0; i < abbrev->num_attrs; ++i)
9812     {
9813       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9814
9815       /* Store the data if it is of an attribute we want to keep in a
9816          partial symbol table.  */
9817       switch (attr.name)
9818         {
9819         case DW_AT_name:
9820           switch (part_die->tag)
9821             {
9822             case DW_TAG_compile_unit:
9823             case DW_TAG_type_unit:
9824               /* Compilation units have a DW_AT_name that is a filename, not
9825                  a source language identifier.  */
9826             case DW_TAG_enumeration_type:
9827             case DW_TAG_enumerator:
9828               /* These tags always have simple identifiers already; no need
9829                  to canonicalize them.  */
9830               part_die->name = DW_STRING (&attr);
9831               break;
9832             default:
9833               part_die->name
9834                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9835                                             &objfile->objfile_obstack);
9836               break;
9837             }
9838           break;
9839         case DW_AT_linkage_name:
9840         case DW_AT_MIPS_linkage_name:
9841           /* Note that both forms of linkage name might appear.  We
9842              assume they will be the same, and we only store the last
9843              one we see.  */
9844           if (cu->language == language_ada)
9845             part_die->name = DW_STRING (&attr);
9846           part_die->linkage_name = DW_STRING (&attr);
9847           break;
9848         case DW_AT_low_pc:
9849           has_low_pc_attr = 1;
9850           part_die->lowpc = DW_ADDR (&attr);
9851           break;
9852         case DW_AT_high_pc:
9853           has_high_pc_attr = 1;
9854           part_die->highpc = DW_ADDR (&attr);
9855           break;
9856         case DW_AT_location:
9857           /* Support the .debug_loc offsets.  */
9858           if (attr_form_is_block (&attr))
9859             {
9860                part_die->locdesc = DW_BLOCK (&attr);
9861             }
9862           else if (attr_form_is_section_offset (&attr))
9863             {
9864               dwarf2_complex_location_expr_complaint ();
9865             }
9866           else
9867             {
9868               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9869                                                      "partial symbol information");
9870             }
9871           break;
9872         case DW_AT_external:
9873           part_die->is_external = DW_UNSND (&attr);
9874           break;
9875         case DW_AT_declaration:
9876           part_die->is_declaration = DW_UNSND (&attr);
9877           break;
9878         case DW_AT_type:
9879           part_die->has_type = 1;
9880           break;
9881         case DW_AT_abstract_origin:
9882         case DW_AT_specification:
9883         case DW_AT_extension:
9884           part_die->has_specification = 1;
9885           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9886           break;
9887         case DW_AT_sibling:
9888           /* Ignore absolute siblings, they might point outside of
9889              the current compile unit.  */
9890           if (attr.form == DW_FORM_ref_addr)
9891             complaint (&symfile_complaints,
9892                        _("ignoring absolute DW_AT_sibling"));
9893           else
9894             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
9895           break;
9896         case DW_AT_byte_size:
9897           part_die->has_byte_size = 1;
9898           break;
9899         case DW_AT_calling_convention:
9900           /* DWARF doesn't provide a way to identify a program's source-level
9901              entry point.  DW_AT_calling_convention attributes are only meant
9902              to describe functions' calling conventions.
9903
9904              However, because it's a necessary piece of information in
9905              Fortran, and because DW_CC_program is the only piece of debugging
9906              information whose definition refers to a 'main program' at all,
9907              several compilers have begun marking Fortran main programs with
9908              DW_CC_program --- even when those functions use the standard
9909              calling conventions.
9910
9911              So until DWARF specifies a way to provide this information and
9912              compilers pick up the new representation, we'll support this
9913              practice.  */
9914           if (DW_UNSND (&attr) == DW_CC_program
9915               && cu->language == language_fortran)
9916             {
9917               set_main_name (part_die->name);
9918
9919               /* As this DIE has a static linkage the name would be difficult
9920                  to look up later.  */
9921               language_of_main = language_fortran;
9922             }
9923           break;
9924         case DW_AT_inline:
9925           if (DW_UNSND (&attr) == DW_INL_inlined
9926               || DW_UNSND (&attr) == DW_INL_declared_inlined)
9927             part_die->may_be_inlined = 1;
9928           break;
9929         default:
9930           break;
9931         }
9932     }
9933
9934   if (has_low_pc_attr && has_high_pc_attr)
9935     {
9936       /* When using the GNU linker, .gnu.linkonce. sections are used to
9937          eliminate duplicate copies of functions and vtables and such.
9938          The linker will arbitrarily choose one and discard the others.
9939          The AT_*_pc values for such functions refer to local labels in
9940          these sections.  If the section from that file was discarded, the
9941          labels are not in the output, so the relocs get a value of 0.
9942          If this is a discarded function, mark the pc bounds as invalid,
9943          so that GDB will ignore it.  */
9944       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9945         {
9946           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9947
9948           complaint (&symfile_complaints,
9949                      _("DW_AT_low_pc %s is zero "
9950                        "for DIE at 0x%x [in module %s]"),
9951                      paddress (gdbarch, part_die->lowpc),
9952                      part_die->offset.sect_off, objfile->name);
9953         }
9954       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9955       else if (part_die->lowpc >= part_die->highpc)
9956         {
9957           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9958
9959           complaint (&symfile_complaints,
9960                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9961                        "for DIE at 0x%x [in module %s]"),
9962                      paddress (gdbarch, part_die->lowpc),
9963                      paddress (gdbarch, part_die->highpc),
9964                      part_die->offset.sect_off, objfile->name);
9965         }
9966       else
9967         part_die->has_pc_info = 1;
9968     }
9969
9970   return info_ptr;
9971 }
9972
9973 /* Find a cached partial DIE at OFFSET in CU.  */
9974
9975 static struct partial_die_info *
9976 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
9977 {
9978   struct partial_die_info *lookup_die = NULL;
9979   struct partial_die_info part_die;
9980
9981   part_die.offset = offset;
9982   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
9983                                     offset.sect_off);
9984
9985   return lookup_die;
9986 }
9987
9988 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9989    except in the case of .debug_types DIEs which do not reference
9990    outside their CU (they do however referencing other types via
9991    DW_FORM_ref_sig8).  */
9992
9993 static struct partial_die_info *
9994 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
9995 {
9996   struct objfile *objfile = cu->objfile;
9997   struct dwarf2_per_cu_data *per_cu = NULL;
9998   struct partial_die_info *pd = NULL;
9999
10000   if (offset_in_cu_p (&cu->header, offset))
10001     {
10002       pd = find_partial_die_in_comp_unit (offset, cu);
10003       if (pd != NULL)
10004         return pd;
10005       /* We missed recording what we needed.
10006          Load all dies and try again.  */
10007       per_cu = cu->per_cu;
10008     }
10009   else
10010     {
10011       /* TUs don't reference other CUs/TUs (except via type signatures).  */
10012       if (cu->per_cu->debug_types_section)
10013         {
10014           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
10015                    " external reference to offset 0x%lx [in module %s].\n"),
10016                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
10017                  bfd_get_filename (objfile->obfd));
10018         }
10019       per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10020
10021       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10022         load_partial_comp_unit (per_cu);
10023
10024       per_cu->cu->last_used = 0;
10025       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10026     }
10027
10028   if (pd == NULL && per_cu->load_all_dies == 0)
10029     {
10030       struct cleanup *back_to;
10031       struct partial_die_info comp_unit_die;
10032       struct abbrev_info *abbrev;
10033       unsigned int bytes_read;
10034       char *info_ptr;
10035       struct dwarf2_section_info *sec;
10036
10037       per_cu->load_all_dies = 1;
10038
10039       if (per_cu->debug_types_section)
10040         sec = per_cu->debug_types_section;
10041       else
10042         sec = &dwarf2_per_objfile->info;
10043
10044       /* Re-read the DIEs, this time reading all of them.
10045          NOTE: We don't discard the previous set of DIEs.
10046          This doesn't happen very often so it's (hopefully) not a problem.  */
10047       back_to = make_cleanup (null_cleanup, 0);
10048       if (per_cu->cu->dwarf2_abbrevs == NULL)
10049         {
10050           dwarf2_read_abbrevs (per_cu->cu);
10051           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10052         }
10053       info_ptr = (sec->buffer
10054                   + per_cu->cu->header.offset.sect_off
10055                   + per_cu->cu->header.first_die_offset.cu_off);
10056       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10057       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10058                                    objfile->obfd, sec->buffer, info_ptr,
10059                                    per_cu->cu);
10060       if (comp_unit_die.has_children)
10061         load_partial_dies (objfile->obfd, sec->buffer, info_ptr, 0,
10062                            per_cu->cu);
10063       do_cleanups (back_to);
10064
10065       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10066     }
10067
10068   if (pd == NULL)
10069     internal_error (__FILE__, __LINE__,
10070                     _("could not find partial DIE 0x%x "
10071                       "in cache [from module %s]\n"),
10072                     offset.sect_off, bfd_get_filename (objfile->obfd));
10073   return pd;
10074 }
10075
10076 /* See if we can figure out if the class lives in a namespace.  We do
10077    this by looking for a member function; its demangled name will
10078    contain namespace info, if there is any.  */
10079
10080 static void
10081 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10082                                   struct dwarf2_cu *cu)
10083 {
10084   /* NOTE: carlton/2003-10-07: Getting the info this way changes
10085      what template types look like, because the demangler
10086      frequently doesn't give the same name as the debug info.  We
10087      could fix this by only using the demangled name to get the
10088      prefix (but see comment in read_structure_type).  */
10089
10090   struct partial_die_info *real_pdi;
10091   struct partial_die_info *child_pdi;
10092
10093   /* If this DIE (this DIE's specification, if any) has a parent, then
10094      we should not do this.  We'll prepend the parent's fully qualified
10095      name when we create the partial symbol.  */
10096
10097   real_pdi = struct_pdi;
10098   while (real_pdi->has_specification)
10099     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10100
10101   if (real_pdi->die_parent != NULL)
10102     return;
10103
10104   for (child_pdi = struct_pdi->die_child;
10105        child_pdi != NULL;
10106        child_pdi = child_pdi->die_sibling)
10107     {
10108       if (child_pdi->tag == DW_TAG_subprogram
10109           && child_pdi->linkage_name != NULL)
10110         {
10111           char *actual_class_name
10112             = language_class_name_from_physname (cu->language_defn,
10113                                                  child_pdi->linkage_name);
10114           if (actual_class_name != NULL)
10115             {
10116               struct_pdi->name
10117                 = obsavestring (actual_class_name,
10118                                 strlen (actual_class_name),
10119                                 &cu->objfile->objfile_obstack);
10120               xfree (actual_class_name);
10121             }
10122           break;
10123         }
10124     }
10125 }
10126
10127 /* Adjust PART_DIE before generating a symbol for it.  This function
10128    may set the is_external flag or change the DIE's name.  */
10129
10130 static void
10131 fixup_partial_die (struct partial_die_info *part_die,
10132                    struct dwarf2_cu *cu)
10133 {
10134   /* Once we've fixed up a die, there's no point in doing so again.
10135      This also avoids a memory leak if we were to call
10136      guess_partial_die_structure_name multiple times.  */
10137   if (part_die->fixup_called)
10138     return;
10139
10140   /* If we found a reference attribute and the DIE has no name, try
10141      to find a name in the referred to DIE.  */
10142
10143   if (part_die->name == NULL && part_die->has_specification)
10144     {
10145       struct partial_die_info *spec_die;
10146
10147       spec_die = find_partial_die (part_die->spec_offset, cu);
10148
10149       fixup_partial_die (spec_die, cu);
10150
10151       if (spec_die->name)
10152         {
10153           part_die->name = spec_die->name;
10154
10155           /* Copy DW_AT_external attribute if it is set.  */
10156           if (spec_die->is_external)
10157             part_die->is_external = spec_die->is_external;
10158         }
10159     }
10160
10161   /* Set default names for some unnamed DIEs.  */
10162
10163   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10164     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10165
10166   /* If there is no parent die to provide a namespace, and there are
10167      children, see if we can determine the namespace from their linkage
10168      name.  */
10169   if (cu->language == language_cplus
10170       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10171       && part_die->die_parent == NULL
10172       && part_die->has_children
10173       && (part_die->tag == DW_TAG_class_type
10174           || part_die->tag == DW_TAG_structure_type
10175           || part_die->tag == DW_TAG_union_type))
10176     guess_partial_die_structure_name (part_die, cu);
10177
10178   /* GCC might emit a nameless struct or union that has a linkage
10179      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10180   if (part_die->name == NULL
10181       && (part_die->tag == DW_TAG_class_type
10182           || part_die->tag == DW_TAG_interface_type
10183           || part_die->tag == DW_TAG_structure_type
10184           || part_die->tag == DW_TAG_union_type)
10185       && part_die->linkage_name != NULL)
10186     {
10187       char *demangled;
10188
10189       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10190       if (demangled)
10191         {
10192           const char *base;
10193
10194           /* Strip any leading namespaces/classes, keep only the base name.
10195              DW_AT_name for named DIEs does not contain the prefixes.  */
10196           base = strrchr (demangled, ':');
10197           if (base && base > demangled && base[-1] == ':')
10198             base++;
10199           else
10200             base = demangled;
10201
10202           part_die->name = obsavestring (base, strlen (base),
10203                                          &cu->objfile->objfile_obstack);
10204           xfree (demangled);
10205         }
10206     }
10207
10208   part_die->fixup_called = 1;
10209 }
10210
10211 /* Read an attribute value described by an attribute form.  */
10212
10213 static gdb_byte *
10214 read_attribute_value (struct attribute *attr, unsigned form,
10215                       bfd *abfd, gdb_byte *info_ptr,
10216                       struct dwarf2_cu *cu)
10217 {
10218   struct comp_unit_head *cu_header = &cu->header;
10219   unsigned int bytes_read;
10220   struct dwarf_block *blk;
10221
10222   attr->form = form;
10223   switch (form)
10224     {
10225     case DW_FORM_ref_addr:
10226       if (cu->header.version == 2)
10227         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10228       else
10229         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10230                                       &cu->header, &bytes_read);
10231       info_ptr += bytes_read;
10232       break;
10233     case DW_FORM_addr:
10234       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10235       info_ptr += bytes_read;
10236       break;
10237     case DW_FORM_block2:
10238       blk = dwarf_alloc_block (cu);
10239       blk->size = read_2_bytes (abfd, info_ptr);
10240       info_ptr += 2;
10241       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10242       info_ptr += blk->size;
10243       DW_BLOCK (attr) = blk;
10244       break;
10245     case DW_FORM_block4:
10246       blk = dwarf_alloc_block (cu);
10247       blk->size = read_4_bytes (abfd, info_ptr);
10248       info_ptr += 4;
10249       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10250       info_ptr += blk->size;
10251       DW_BLOCK (attr) = blk;
10252       break;
10253     case DW_FORM_data2:
10254       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10255       info_ptr += 2;
10256       break;
10257     case DW_FORM_data4:
10258       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10259       info_ptr += 4;
10260       break;
10261     case DW_FORM_data8:
10262       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10263       info_ptr += 8;
10264       break;
10265     case DW_FORM_sec_offset:
10266       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10267       info_ptr += bytes_read;
10268       break;
10269     case DW_FORM_string:
10270       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10271       DW_STRING_IS_CANONICAL (attr) = 0;
10272       info_ptr += bytes_read;
10273       break;
10274     case DW_FORM_strp:
10275       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10276                                                &bytes_read);
10277       DW_STRING_IS_CANONICAL (attr) = 0;
10278       info_ptr += bytes_read;
10279       break;
10280     case DW_FORM_exprloc:
10281     case DW_FORM_block:
10282       blk = dwarf_alloc_block (cu);
10283       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10284       info_ptr += bytes_read;
10285       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10286       info_ptr += blk->size;
10287       DW_BLOCK (attr) = blk;
10288       break;
10289     case DW_FORM_block1:
10290       blk = dwarf_alloc_block (cu);
10291       blk->size = read_1_byte (abfd, info_ptr);
10292       info_ptr += 1;
10293       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10294       info_ptr += blk->size;
10295       DW_BLOCK (attr) = blk;
10296       break;
10297     case DW_FORM_data1:
10298       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10299       info_ptr += 1;
10300       break;
10301     case DW_FORM_flag:
10302       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10303       info_ptr += 1;
10304       break;
10305     case DW_FORM_flag_present:
10306       DW_UNSND (attr) = 1;
10307       break;
10308     case DW_FORM_sdata:
10309       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10310       info_ptr += bytes_read;
10311       break;
10312     case DW_FORM_udata:
10313       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10314       info_ptr += bytes_read;
10315       break;
10316     case DW_FORM_ref1:
10317       DW_ADDR (attr) = (cu->header.offset.sect_off
10318                         + read_1_byte (abfd, info_ptr));
10319       info_ptr += 1;
10320       break;
10321     case DW_FORM_ref2:
10322       DW_ADDR (attr) = (cu->header.offset.sect_off
10323                         + read_2_bytes (abfd, info_ptr));
10324       info_ptr += 2;
10325       break;
10326     case DW_FORM_ref4:
10327       DW_ADDR (attr) = (cu->header.offset.sect_off
10328                         + read_4_bytes (abfd, info_ptr));
10329       info_ptr += 4;
10330       break;
10331     case DW_FORM_ref8:
10332       DW_ADDR (attr) = (cu->header.offset.sect_off
10333                         + read_8_bytes (abfd, info_ptr));
10334       info_ptr += 8;
10335       break;
10336     case DW_FORM_ref_sig8:
10337       /* Convert the signature to something we can record in DW_UNSND
10338          for later lookup.
10339          NOTE: This is NULL if the type wasn't found.  */
10340       DW_SIGNATURED_TYPE (attr) =
10341         lookup_signatured_type (read_8_bytes (abfd, info_ptr));
10342       info_ptr += 8;
10343       break;
10344     case DW_FORM_ref_udata:
10345       DW_ADDR (attr) = (cu->header.offset.sect_off
10346                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10347       info_ptr += bytes_read;
10348       break;
10349     case DW_FORM_indirect:
10350       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10351       info_ptr += bytes_read;
10352       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10353       break;
10354     default:
10355       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10356              dwarf_form_name (form),
10357              bfd_get_filename (abfd));
10358     }
10359
10360   /* We have seen instances where the compiler tried to emit a byte
10361      size attribute of -1 which ended up being encoded as an unsigned
10362      0xffffffff.  Although 0xffffffff is technically a valid size value,
10363      an object of this size seems pretty unlikely so we can relatively
10364      safely treat these cases as if the size attribute was invalid and
10365      treat them as zero by default.  */
10366   if (attr->name == DW_AT_byte_size
10367       && form == DW_FORM_data4
10368       && DW_UNSND (attr) >= 0xffffffff)
10369     {
10370       complaint
10371         (&symfile_complaints,
10372          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10373          hex_string (DW_UNSND (attr)));
10374       DW_UNSND (attr) = 0;
10375     }
10376
10377   return info_ptr;
10378 }
10379
10380 /* Read an attribute described by an abbreviated attribute.  */
10381
10382 static gdb_byte *
10383 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10384                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10385 {
10386   attr->name = abbrev->name;
10387   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10388 }
10389
10390 /* Read dwarf information from a buffer.  */
10391
10392 static unsigned int
10393 read_1_byte (bfd *abfd, gdb_byte *buf)
10394 {
10395   return bfd_get_8 (abfd, buf);
10396 }
10397
10398 static int
10399 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10400 {
10401   return bfd_get_signed_8 (abfd, buf);
10402 }
10403
10404 static unsigned int
10405 read_2_bytes (bfd *abfd, gdb_byte *buf)
10406 {
10407   return bfd_get_16 (abfd, buf);
10408 }
10409
10410 static int
10411 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10412 {
10413   return bfd_get_signed_16 (abfd, buf);
10414 }
10415
10416 static unsigned int
10417 read_4_bytes (bfd *abfd, gdb_byte *buf)
10418 {
10419   return bfd_get_32 (abfd, buf);
10420 }
10421
10422 static int
10423 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10424 {
10425   return bfd_get_signed_32 (abfd, buf);
10426 }
10427
10428 static ULONGEST
10429 read_8_bytes (bfd *abfd, gdb_byte *buf)
10430 {
10431   return bfd_get_64 (abfd, buf);
10432 }
10433
10434 static CORE_ADDR
10435 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10436               unsigned int *bytes_read)
10437 {
10438   struct comp_unit_head *cu_header = &cu->header;
10439   CORE_ADDR retval = 0;
10440
10441   if (cu_header->signed_addr_p)
10442     {
10443       switch (cu_header->addr_size)
10444         {
10445         case 2:
10446           retval = bfd_get_signed_16 (abfd, buf);
10447           break;
10448         case 4:
10449           retval = bfd_get_signed_32 (abfd, buf);
10450           break;
10451         case 8:
10452           retval = bfd_get_signed_64 (abfd, buf);
10453           break;
10454         default:
10455           internal_error (__FILE__, __LINE__,
10456                           _("read_address: bad switch, signed [in module %s]"),
10457                           bfd_get_filename (abfd));
10458         }
10459     }
10460   else
10461     {
10462       switch (cu_header->addr_size)
10463         {
10464         case 2:
10465           retval = bfd_get_16 (abfd, buf);
10466           break;
10467         case 4:
10468           retval = bfd_get_32 (abfd, buf);
10469           break;
10470         case 8:
10471           retval = bfd_get_64 (abfd, buf);
10472           break;
10473         default:
10474           internal_error (__FILE__, __LINE__,
10475                           _("read_address: bad switch, "
10476                             "unsigned [in module %s]"),
10477                           bfd_get_filename (abfd));
10478         }
10479     }
10480
10481   *bytes_read = cu_header->addr_size;
10482   return retval;
10483 }
10484
10485 /* Read the initial length from a section.  The (draft) DWARF 3
10486    specification allows the initial length to take up either 4 bytes
10487    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10488    bytes describe the length and all offsets will be 8 bytes in length
10489    instead of 4.
10490
10491    An older, non-standard 64-bit format is also handled by this
10492    function.  The older format in question stores the initial length
10493    as an 8-byte quantity without an escape value.  Lengths greater
10494    than 2^32 aren't very common which means that the initial 4 bytes
10495    is almost always zero.  Since a length value of zero doesn't make
10496    sense for the 32-bit format, this initial zero can be considered to
10497    be an escape value which indicates the presence of the older 64-bit
10498    format.  As written, the code can't detect (old format) lengths
10499    greater than 4GB.  If it becomes necessary to handle lengths
10500    somewhat larger than 4GB, we could allow other small values (such
10501    as the non-sensical values of 1, 2, and 3) to also be used as
10502    escape values indicating the presence of the old format.
10503
10504    The value returned via bytes_read should be used to increment the
10505    relevant pointer after calling read_initial_length().
10506
10507    [ Note:  read_initial_length() and read_offset() are based on the
10508      document entitled "DWARF Debugging Information Format", revision
10509      3, draft 8, dated November 19, 2001.  This document was obtained
10510      from:
10511
10512         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10513
10514      This document is only a draft and is subject to change.  (So beware.)
10515
10516      Details regarding the older, non-standard 64-bit format were
10517      determined empirically by examining 64-bit ELF files produced by
10518      the SGI toolchain on an IRIX 6.5 machine.
10519
10520      - Kevin, July 16, 2002
10521    ] */
10522
10523 static LONGEST
10524 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10525 {
10526   LONGEST length = bfd_get_32 (abfd, buf);
10527
10528   if (length == 0xffffffff)
10529     {
10530       length = bfd_get_64 (abfd, buf + 4);
10531       *bytes_read = 12;
10532     }
10533   else if (length == 0)
10534     {
10535       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10536       length = bfd_get_64 (abfd, buf);
10537       *bytes_read = 8;
10538     }
10539   else
10540     {
10541       *bytes_read = 4;
10542     }
10543
10544   return length;
10545 }
10546
10547 /* Cover function for read_initial_length.
10548    Returns the length of the object at BUF, and stores the size of the
10549    initial length in *BYTES_READ and stores the size that offsets will be in
10550    *OFFSET_SIZE.
10551    If the initial length size is not equivalent to that specified in
10552    CU_HEADER then issue a complaint.
10553    This is useful when reading non-comp-unit headers.  */
10554
10555 static LONGEST
10556 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10557                                         const struct comp_unit_head *cu_header,
10558                                         unsigned int *bytes_read,
10559                                         unsigned int *offset_size)
10560 {
10561   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10562
10563   gdb_assert (cu_header->initial_length_size == 4
10564               || cu_header->initial_length_size == 8
10565               || cu_header->initial_length_size == 12);
10566
10567   if (cu_header->initial_length_size != *bytes_read)
10568     complaint (&symfile_complaints,
10569                _("intermixed 32-bit and 64-bit DWARF sections"));
10570
10571   *offset_size = (*bytes_read == 4) ? 4 : 8;
10572   return length;
10573 }
10574
10575 /* Read an offset from the data stream.  The size of the offset is
10576    given by cu_header->offset_size.  */
10577
10578 static LONGEST
10579 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10580              unsigned int *bytes_read)
10581 {
10582   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10583
10584   *bytes_read = cu_header->offset_size;
10585   return offset;
10586 }
10587
10588 /* Read an offset from the data stream.  */
10589
10590 static LONGEST
10591 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10592 {
10593   LONGEST retval = 0;
10594
10595   switch (offset_size)
10596     {
10597     case 4:
10598       retval = bfd_get_32 (abfd, buf);
10599       break;
10600     case 8:
10601       retval = bfd_get_64 (abfd, buf);
10602       break;
10603     default:
10604       internal_error (__FILE__, __LINE__,
10605                       _("read_offset_1: bad switch [in module %s]"),
10606                       bfd_get_filename (abfd));
10607     }
10608
10609   return retval;
10610 }
10611
10612 static gdb_byte *
10613 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10614 {
10615   /* If the size of a host char is 8 bits, we can return a pointer
10616      to the buffer, otherwise we have to copy the data to a buffer
10617      allocated on the temporary obstack.  */
10618   gdb_assert (HOST_CHAR_BIT == 8);
10619   return buf;
10620 }
10621
10622 static char *
10623 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10624 {
10625   /* If the size of a host char is 8 bits, we can return a pointer
10626      to the string, otherwise we have to copy the string to a buffer
10627      allocated on the temporary obstack.  */
10628   gdb_assert (HOST_CHAR_BIT == 8);
10629   if (*buf == '\0')
10630     {
10631       *bytes_read_ptr = 1;
10632       return NULL;
10633     }
10634   *bytes_read_ptr = strlen ((char *) buf) + 1;
10635   return (char *) buf;
10636 }
10637
10638 static char *
10639 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10640 {
10641   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10642   if (dwarf2_per_objfile->str.buffer == NULL)
10643     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10644            bfd_get_filename (abfd));
10645   if (str_offset >= dwarf2_per_objfile->str.size)
10646     error (_("DW_FORM_strp pointing outside of "
10647              ".debug_str section [in module %s]"),
10648            bfd_get_filename (abfd));
10649   gdb_assert (HOST_CHAR_BIT == 8);
10650   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10651     return NULL;
10652   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10653 }
10654
10655 static char *
10656 read_indirect_string (bfd *abfd, gdb_byte *buf,
10657                       const struct comp_unit_head *cu_header,
10658                       unsigned int *bytes_read_ptr)
10659 {
10660   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10661
10662   return read_indirect_string_at_offset (abfd, str_offset);
10663 }
10664
10665 static unsigned long
10666 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10667 {
10668   unsigned long result;
10669   unsigned int num_read;
10670   int i, shift;
10671   unsigned char byte;
10672
10673   result = 0;
10674   shift = 0;
10675   num_read = 0;
10676   i = 0;
10677   while (1)
10678     {
10679       byte = bfd_get_8 (abfd, buf);
10680       buf++;
10681       num_read++;
10682       result |= ((unsigned long)(byte & 127) << shift);
10683       if ((byte & 128) == 0)
10684         {
10685           break;
10686         }
10687       shift += 7;
10688     }
10689   *bytes_read_ptr = num_read;
10690   return result;
10691 }
10692
10693 static long
10694 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10695 {
10696   long result;
10697   int i, shift, num_read;
10698   unsigned char byte;
10699
10700   result = 0;
10701   shift = 0;
10702   num_read = 0;
10703   i = 0;
10704   while (1)
10705     {
10706       byte = bfd_get_8 (abfd, buf);
10707       buf++;
10708       num_read++;
10709       result |= ((long)(byte & 127) << shift);
10710       shift += 7;
10711       if ((byte & 128) == 0)
10712         {
10713           break;
10714         }
10715     }
10716   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10717     result |= -(((long)1) << shift);
10718   *bytes_read_ptr = num_read;
10719   return result;
10720 }
10721
10722 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10723
10724 static gdb_byte *
10725 skip_leb128 (bfd *abfd, gdb_byte *buf)
10726 {
10727   int byte;
10728
10729   while (1)
10730     {
10731       byte = bfd_get_8 (abfd, buf);
10732       buf++;
10733       if ((byte & 128) == 0)
10734         return buf;
10735     }
10736 }
10737
10738 static void
10739 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10740 {
10741   switch (lang)
10742     {
10743     case DW_LANG_C89:
10744     case DW_LANG_C99:
10745     case DW_LANG_C:
10746       cu->language = language_c;
10747       break;
10748     case DW_LANG_C_plus_plus:
10749       cu->language = language_cplus;
10750       break;
10751     case DW_LANG_D:
10752       cu->language = language_d;
10753       break;
10754     case DW_LANG_Fortran77:
10755     case DW_LANG_Fortran90:
10756     case DW_LANG_Fortran95:
10757       cu->language = language_fortran;
10758       break;
10759     case DW_LANG_Mips_Assembler:
10760       cu->language = language_asm;
10761       break;
10762     case DW_LANG_Java:
10763       cu->language = language_java;
10764       break;
10765     case DW_LANG_Ada83:
10766     case DW_LANG_Ada95:
10767       cu->language = language_ada;
10768       break;
10769     case DW_LANG_Modula2:
10770       cu->language = language_m2;
10771       break;
10772     case DW_LANG_Pascal83:
10773       cu->language = language_pascal;
10774       break;
10775     case DW_LANG_ObjC:
10776       cu->language = language_objc;
10777       break;
10778     case DW_LANG_Cobol74:
10779     case DW_LANG_Cobol85:
10780     default:
10781       cu->language = language_minimal;
10782       break;
10783     }
10784   cu->language_defn = language_def (cu->language);
10785 }
10786
10787 /* Return the named attribute or NULL if not there.  */
10788
10789 static struct attribute *
10790 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10791 {
10792   for (;;)
10793     {
10794       unsigned int i;
10795       struct attribute *spec = NULL;
10796
10797       for (i = 0; i < die->num_attrs; ++i)
10798         {
10799           if (die->attrs[i].name == name)
10800             return &die->attrs[i];
10801           if (die->attrs[i].name == DW_AT_specification
10802               || die->attrs[i].name == DW_AT_abstract_origin)
10803             spec = &die->attrs[i];
10804         }
10805
10806       if (!spec)
10807         break;
10808
10809       die = follow_die_ref (die, spec, &cu);
10810     }
10811
10812   return NULL;
10813 }
10814
10815 /* Return the named attribute or NULL if not there,
10816    but do not follow DW_AT_specification, etc.
10817    This is for use in contexts where we're reading .debug_types dies.
10818    Following DW_AT_specification, DW_AT_abstract_origin will take us
10819    back up the chain, and we want to go down.  */
10820
10821 static struct attribute *
10822 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10823                        struct dwarf2_cu *cu)
10824 {
10825   unsigned int i;
10826
10827   for (i = 0; i < die->num_attrs; ++i)
10828     if (die->attrs[i].name == name)
10829       return &die->attrs[i];
10830
10831   return NULL;
10832 }
10833
10834 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10835    and holds a non-zero value.  This function should only be used for
10836    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10837
10838 static int
10839 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10840 {
10841   struct attribute *attr = dwarf2_attr (die, name, cu);
10842
10843   return (attr && DW_UNSND (attr));
10844 }
10845
10846 static int
10847 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10848 {
10849   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10850      which value is non-zero.  However, we have to be careful with
10851      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10852      (via dwarf2_flag_true_p) follows this attribute.  So we may
10853      end up accidently finding a declaration attribute that belongs
10854      to a different DIE referenced by the specification attribute,
10855      even though the given DIE does not have a declaration attribute.  */
10856   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10857           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10858 }
10859
10860 /* Return the die giving the specification for DIE, if there is
10861    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10862    containing the return value on output.  If there is no
10863    specification, but there is an abstract origin, that is
10864    returned.  */
10865
10866 static struct die_info *
10867 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10868 {
10869   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10870                                              *spec_cu);
10871
10872   if (spec_attr == NULL)
10873     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10874
10875   if (spec_attr == NULL)
10876     return NULL;
10877   else
10878     return follow_die_ref (die, spec_attr, spec_cu);
10879 }
10880
10881 /* Free the line_header structure *LH, and any arrays and strings it
10882    refers to.
10883    NOTE: This is also used as a "cleanup" function.  */
10884
10885 static void
10886 free_line_header (struct line_header *lh)
10887 {
10888   if (lh->standard_opcode_lengths)
10889     xfree (lh->standard_opcode_lengths);
10890
10891   /* Remember that all the lh->file_names[i].name pointers are
10892      pointers into debug_line_buffer, and don't need to be freed.  */
10893   if (lh->file_names)
10894     xfree (lh->file_names);
10895
10896   /* Similarly for the include directory names.  */
10897   if (lh->include_dirs)
10898     xfree (lh->include_dirs);
10899
10900   xfree (lh);
10901 }
10902
10903 /* Add an entry to LH's include directory table.  */
10904
10905 static void
10906 add_include_dir (struct line_header *lh, char *include_dir)
10907 {
10908   /* Grow the array if necessary.  */
10909   if (lh->include_dirs_size == 0)
10910     {
10911       lh->include_dirs_size = 1; /* for testing */
10912       lh->include_dirs = xmalloc (lh->include_dirs_size
10913                                   * sizeof (*lh->include_dirs));
10914     }
10915   else if (lh->num_include_dirs >= lh->include_dirs_size)
10916     {
10917       lh->include_dirs_size *= 2;
10918       lh->include_dirs = xrealloc (lh->include_dirs,
10919                                    (lh->include_dirs_size
10920                                     * sizeof (*lh->include_dirs)));
10921     }
10922
10923   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10924 }
10925
10926 /* Add an entry to LH's file name table.  */
10927
10928 static void
10929 add_file_name (struct line_header *lh,
10930                char *name,
10931                unsigned int dir_index,
10932                unsigned int mod_time,
10933                unsigned int length)
10934 {
10935   struct file_entry *fe;
10936
10937   /* Grow the array if necessary.  */
10938   if (lh->file_names_size == 0)
10939     {
10940       lh->file_names_size = 1; /* for testing */
10941       lh->file_names = xmalloc (lh->file_names_size
10942                                 * sizeof (*lh->file_names));
10943     }
10944   else if (lh->num_file_names >= lh->file_names_size)
10945     {
10946       lh->file_names_size *= 2;
10947       lh->file_names = xrealloc (lh->file_names,
10948                                  (lh->file_names_size
10949                                   * sizeof (*lh->file_names)));
10950     }
10951
10952   fe = &lh->file_names[lh->num_file_names++];
10953   fe->name = name;
10954   fe->dir_index = dir_index;
10955   fe->mod_time = mod_time;
10956   fe->length = length;
10957   fe->included_p = 0;
10958   fe->symtab = NULL;
10959 }
10960
10961 /* Read the statement program header starting at OFFSET in
10962    .debug_line, according to the endianness of ABFD.  Return a pointer
10963    to a struct line_header, allocated using xmalloc.
10964
10965    NOTE: the strings in the include directory and file name tables of
10966    the returned object point into debug_line_buffer, and must not be
10967    freed.  */
10968
10969 static struct line_header *
10970 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10971                           struct dwarf2_cu *cu)
10972 {
10973   struct cleanup *back_to;
10974   struct line_header *lh;
10975   gdb_byte *line_ptr;
10976   unsigned int bytes_read, offset_size;
10977   int i;
10978   char *cur_dir, *cur_file;
10979
10980   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10981   if (dwarf2_per_objfile->line.buffer == NULL)
10982     {
10983       complaint (&symfile_complaints, _("missing .debug_line section"));
10984       return 0;
10985     }
10986
10987   /* Make sure that at least there's room for the total_length field.
10988      That could be 12 bytes long, but we're just going to fudge that.  */
10989   if (offset + 4 >= dwarf2_per_objfile->line.size)
10990     {
10991       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10992       return 0;
10993     }
10994
10995   lh = xmalloc (sizeof (*lh));
10996   memset (lh, 0, sizeof (*lh));
10997   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10998                           (void *) lh);
10999
11000   line_ptr = dwarf2_per_objfile->line.buffer + offset;
11001
11002   /* Read in the header.  */
11003   lh->total_length =
11004     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11005                                             &bytes_read, &offset_size);
11006   line_ptr += bytes_read;
11007   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11008                                      + dwarf2_per_objfile->line.size))
11009     {
11010       dwarf2_statement_list_fits_in_line_number_section_complaint ();
11011       return 0;
11012     }
11013   lh->statement_program_end = line_ptr + lh->total_length;
11014   lh->version = read_2_bytes (abfd, line_ptr);
11015   line_ptr += 2;
11016   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11017   line_ptr += offset_size;
11018   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11019   line_ptr += 1;
11020   if (lh->version >= 4)
11021     {
11022       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11023       line_ptr += 1;
11024     }
11025   else
11026     lh->maximum_ops_per_instruction = 1;
11027
11028   if (lh->maximum_ops_per_instruction == 0)
11029     {
11030       lh->maximum_ops_per_instruction = 1;
11031       complaint (&symfile_complaints,
11032                  _("invalid maximum_ops_per_instruction "
11033                    "in `.debug_line' section"));
11034     }
11035
11036   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11037   line_ptr += 1;
11038   lh->line_base = read_1_signed_byte (abfd, line_ptr);
11039   line_ptr += 1;
11040   lh->line_range = read_1_byte (abfd, line_ptr);
11041   line_ptr += 1;
11042   lh->opcode_base = read_1_byte (abfd, line_ptr);
11043   line_ptr += 1;
11044   lh->standard_opcode_lengths
11045     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11046
11047   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
11048   for (i = 1; i < lh->opcode_base; ++i)
11049     {
11050       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11051       line_ptr += 1;
11052     }
11053
11054   /* Read directory table.  */
11055   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11056     {
11057       line_ptr += bytes_read;
11058       add_include_dir (lh, cur_dir);
11059     }
11060   line_ptr += bytes_read;
11061
11062   /* Read file name table.  */
11063   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11064     {
11065       unsigned int dir_index, mod_time, length;
11066
11067       line_ptr += bytes_read;
11068       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11069       line_ptr += bytes_read;
11070       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11071       line_ptr += bytes_read;
11072       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11073       line_ptr += bytes_read;
11074
11075       add_file_name (lh, cur_file, dir_index, mod_time, length);
11076     }
11077   line_ptr += bytes_read;
11078   lh->statement_program_start = line_ptr;
11079
11080   if (line_ptr > (dwarf2_per_objfile->line.buffer
11081                   + dwarf2_per_objfile->line.size))
11082     complaint (&symfile_complaints,
11083                _("line number info header doesn't "
11084                  "fit in `.debug_line' section"));
11085
11086   discard_cleanups (back_to);
11087   return lh;
11088 }
11089
11090 /* Subroutine of dwarf_decode_lines to simplify it.
11091    Return the file name of the psymtab for included file FILE_INDEX
11092    in line header LH of PST.
11093    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11094    If space for the result is malloc'd, it will be freed by a cleanup.
11095    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
11096
11097 static char *
11098 psymtab_include_file_name (const struct line_header *lh, int file_index,
11099                            const struct partial_symtab *pst,
11100                            const char *comp_dir)
11101 {
11102   const struct file_entry fe = lh->file_names [file_index];
11103   char *include_name = fe.name;
11104   char *include_name_to_compare = include_name;
11105   char *dir_name = NULL;
11106   const char *pst_filename;
11107   char *copied_name = NULL;
11108   int file_is_pst;
11109
11110   if (fe.dir_index)
11111     dir_name = lh->include_dirs[fe.dir_index - 1];
11112
11113   if (!IS_ABSOLUTE_PATH (include_name)
11114       && (dir_name != NULL || comp_dir != NULL))
11115     {
11116       /* Avoid creating a duplicate psymtab for PST.
11117          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11118          Before we do the comparison, however, we need to account
11119          for DIR_NAME and COMP_DIR.
11120          First prepend dir_name (if non-NULL).  If we still don't
11121          have an absolute path prepend comp_dir (if non-NULL).
11122          However, the directory we record in the include-file's
11123          psymtab does not contain COMP_DIR (to match the
11124          corresponding symtab(s)).
11125
11126          Example:
11127
11128          bash$ cd /tmp
11129          bash$ gcc -g ./hello.c
11130          include_name = "hello.c"
11131          dir_name = "."
11132          DW_AT_comp_dir = comp_dir = "/tmp"
11133          DW_AT_name = "./hello.c"  */
11134
11135       if (dir_name != NULL)
11136         {
11137           include_name = concat (dir_name, SLASH_STRING,
11138                                  include_name, (char *)NULL);
11139           include_name_to_compare = include_name;
11140           make_cleanup (xfree, include_name);
11141         }
11142       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11143         {
11144           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11145                                             include_name, (char *)NULL);
11146         }
11147     }
11148
11149   pst_filename = pst->filename;
11150   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11151     {
11152       copied_name = concat (pst->dirname, SLASH_STRING,
11153                             pst_filename, (char *)NULL);
11154       pst_filename = copied_name;
11155     }
11156
11157   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11158
11159   if (include_name_to_compare != include_name)
11160     xfree (include_name_to_compare);
11161   if (copied_name != NULL)
11162     xfree (copied_name);
11163
11164   if (file_is_pst)
11165     return NULL;
11166   return include_name;
11167 }
11168
11169 /* Ignore this record_line request.  */
11170
11171 static void
11172 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11173 {
11174   return;
11175 }
11176
11177 /* Subroutine of dwarf_decode_lines to simplify it.
11178    Process the line number information in LH.  */
11179
11180 static void
11181 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11182                       struct dwarf2_cu *cu, struct partial_symtab *pst)
11183 {
11184   gdb_byte *line_ptr, *extended_end;
11185   gdb_byte *line_end;
11186   unsigned int bytes_read, extended_len;
11187   unsigned char op_code, extended_op, adj_opcode;
11188   CORE_ADDR baseaddr;
11189   struct objfile *objfile = cu->objfile;
11190   bfd *abfd = objfile->obfd;
11191   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11192   const int decode_for_pst_p = (pst != NULL);
11193   struct subfile *last_subfile = NULL;
11194   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11195     = record_line;
11196
11197   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11198
11199   line_ptr = lh->statement_program_start;
11200   line_end = lh->statement_program_end;
11201
11202   /* Read the statement sequences until there's nothing left.  */
11203   while (line_ptr < line_end)
11204     {
11205       /* state machine registers  */
11206       CORE_ADDR address = 0;
11207       unsigned int file = 1;
11208       unsigned int line = 1;
11209       unsigned int column = 0;
11210       int is_stmt = lh->default_is_stmt;
11211       int basic_block = 0;
11212       int end_sequence = 0;
11213       CORE_ADDR addr;
11214       unsigned char op_index = 0;
11215
11216       if (!decode_for_pst_p && lh->num_file_names >= file)
11217         {
11218           /* Start a subfile for the current file of the state machine.  */
11219           /* lh->include_dirs and lh->file_names are 0-based, but the
11220              directory and file name numbers in the statement program
11221              are 1-based.  */
11222           struct file_entry *fe = &lh->file_names[file - 1];
11223           char *dir = NULL;
11224
11225           if (fe->dir_index)
11226             dir = lh->include_dirs[fe->dir_index - 1];
11227
11228           dwarf2_start_subfile (fe->name, dir, comp_dir);
11229         }
11230
11231       /* Decode the table.  */
11232       while (!end_sequence)
11233         {
11234           op_code = read_1_byte (abfd, line_ptr);
11235           line_ptr += 1;
11236           if (line_ptr > line_end)
11237             {
11238               dwarf2_debug_line_missing_end_sequence_complaint ();
11239               break;
11240             }
11241
11242           if (op_code >= lh->opcode_base)
11243             {
11244               /* Special operand.  */
11245               adj_opcode = op_code - lh->opcode_base;
11246               address += (((op_index + (adj_opcode / lh->line_range))
11247                            / lh->maximum_ops_per_instruction)
11248                           * lh->minimum_instruction_length);
11249               op_index = ((op_index + (adj_opcode / lh->line_range))
11250                           % lh->maximum_ops_per_instruction);
11251               line += lh->line_base + (adj_opcode % lh->line_range);
11252               if (lh->num_file_names < file || file == 0)
11253                 dwarf2_debug_line_missing_file_complaint ();
11254               /* For now we ignore lines not starting on an
11255                  instruction boundary.  */
11256               else if (op_index == 0)
11257                 {
11258                   lh->file_names[file - 1].included_p = 1;
11259                   if (!decode_for_pst_p && is_stmt)
11260                     {
11261                       if (last_subfile != current_subfile)
11262                         {
11263                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11264                           if (last_subfile)
11265                             (*p_record_line) (last_subfile, 0, addr);
11266                           last_subfile = current_subfile;
11267                         }
11268                       /* Append row to matrix using current values.  */
11269                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11270                       (*p_record_line) (current_subfile, line, addr);
11271                     }
11272                 }
11273               basic_block = 0;
11274             }
11275           else switch (op_code)
11276             {
11277             case DW_LNS_extended_op:
11278               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11279                                                    &bytes_read);
11280               line_ptr += bytes_read;
11281               extended_end = line_ptr + extended_len;
11282               extended_op = read_1_byte (abfd, line_ptr);
11283               line_ptr += 1;
11284               switch (extended_op)
11285                 {
11286                 case DW_LNE_end_sequence:
11287                   p_record_line = record_line;
11288                   end_sequence = 1;
11289                   break;
11290                 case DW_LNE_set_address:
11291                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11292
11293                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11294                     {
11295                       /* This line table is for a function which has been
11296                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11297
11298                       long line_offset
11299                         = line_ptr - dwarf2_per_objfile->line.buffer;
11300
11301                       complaint (&symfile_complaints,
11302                                  _(".debug_line address at offset 0x%lx is 0 "
11303                                    "[in module %s]"),
11304                                  line_offset, objfile->name);
11305                       p_record_line = noop_record_line;
11306                     }
11307
11308                   op_index = 0;
11309                   line_ptr += bytes_read;
11310                   address += baseaddr;
11311                   break;
11312                 case DW_LNE_define_file:
11313                   {
11314                     char *cur_file;
11315                     unsigned int dir_index, mod_time, length;
11316
11317                     cur_file = read_direct_string (abfd, line_ptr,
11318                                                    &bytes_read);
11319                     line_ptr += bytes_read;
11320                     dir_index =
11321                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11322                     line_ptr += bytes_read;
11323                     mod_time =
11324                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11325                     line_ptr += bytes_read;
11326                     length =
11327                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11328                     line_ptr += bytes_read;
11329                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11330                   }
11331                   break;
11332                 case DW_LNE_set_discriminator:
11333                   /* The discriminator is not interesting to the debugger;
11334                      just ignore it.  */
11335                   line_ptr = extended_end;
11336                   break;
11337                 default:
11338                   complaint (&symfile_complaints,
11339                              _("mangled .debug_line section"));
11340                   return;
11341                 }
11342               /* Make sure that we parsed the extended op correctly.  If e.g.
11343                  we expected a different address size than the producer used,
11344                  we may have read the wrong number of bytes.  */
11345               if (line_ptr != extended_end)
11346                 {
11347                   complaint (&symfile_complaints,
11348                              _("mangled .debug_line section"));
11349                   return;
11350                 }
11351               break;
11352             case DW_LNS_copy:
11353               if (lh->num_file_names < file || file == 0)
11354                 dwarf2_debug_line_missing_file_complaint ();
11355               else
11356                 {
11357                   lh->file_names[file - 1].included_p = 1;
11358                   if (!decode_for_pst_p && is_stmt)
11359                     {
11360                       if (last_subfile != current_subfile)
11361                         {
11362                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11363                           if (last_subfile)
11364                             (*p_record_line) (last_subfile, 0, addr);
11365                           last_subfile = current_subfile;
11366                         }
11367                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11368                       (*p_record_line) (current_subfile, line, addr);
11369                     }
11370                 }
11371               basic_block = 0;
11372               break;
11373             case DW_LNS_advance_pc:
11374               {
11375                 CORE_ADDR adjust
11376                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11377
11378                 address += (((op_index + adjust)
11379                              / lh->maximum_ops_per_instruction)
11380                             * lh->minimum_instruction_length);
11381                 op_index = ((op_index + adjust)
11382                             % lh->maximum_ops_per_instruction);
11383                 line_ptr += bytes_read;
11384               }
11385               break;
11386             case DW_LNS_advance_line:
11387               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11388               line_ptr += bytes_read;
11389               break;
11390             case DW_LNS_set_file:
11391               {
11392                 /* The arrays lh->include_dirs and lh->file_names are
11393                    0-based, but the directory and file name numbers in
11394                    the statement program are 1-based.  */
11395                 struct file_entry *fe;
11396                 char *dir = NULL;
11397
11398                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11399                 line_ptr += bytes_read;
11400                 if (lh->num_file_names < file || file == 0)
11401                   dwarf2_debug_line_missing_file_complaint ();
11402                 else
11403                   {
11404                     fe = &lh->file_names[file - 1];
11405                     if (fe->dir_index)
11406                       dir = lh->include_dirs[fe->dir_index - 1];
11407                     if (!decode_for_pst_p)
11408                       {
11409                         last_subfile = current_subfile;
11410                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11411                       }
11412                   }
11413               }
11414               break;
11415             case DW_LNS_set_column:
11416               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11417               line_ptr += bytes_read;
11418               break;
11419             case DW_LNS_negate_stmt:
11420               is_stmt = (!is_stmt);
11421               break;
11422             case DW_LNS_set_basic_block:
11423               basic_block = 1;
11424               break;
11425             /* Add to the address register of the state machine the
11426                address increment value corresponding to special opcode
11427                255.  I.e., this value is scaled by the minimum
11428                instruction length since special opcode 255 would have
11429                scaled the increment.  */
11430             case DW_LNS_const_add_pc:
11431               {
11432                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11433
11434                 address += (((op_index + adjust)
11435                              / lh->maximum_ops_per_instruction)
11436                             * lh->minimum_instruction_length);
11437                 op_index = ((op_index + adjust)
11438                             % lh->maximum_ops_per_instruction);
11439               }
11440               break;
11441             case DW_LNS_fixed_advance_pc:
11442               address += read_2_bytes (abfd, line_ptr);
11443               op_index = 0;
11444               line_ptr += 2;
11445               break;
11446             default:
11447               {
11448                 /* Unknown standard opcode, ignore it.  */
11449                 int i;
11450
11451                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11452                   {
11453                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11454                     line_ptr += bytes_read;
11455                   }
11456               }
11457             }
11458         }
11459       if (lh->num_file_names < file || file == 0)
11460         dwarf2_debug_line_missing_file_complaint ();
11461       else
11462         {
11463           lh->file_names[file - 1].included_p = 1;
11464           if (!decode_for_pst_p)
11465             {
11466               addr = gdbarch_addr_bits_remove (gdbarch, address);
11467               (*p_record_line) (current_subfile, 0, addr);
11468             }
11469         }
11470     }
11471 }
11472
11473 /* Decode the Line Number Program (LNP) for the given line_header
11474    structure and CU.  The actual information extracted and the type
11475    of structures created from the LNP depends on the value of PST.
11476
11477    1. If PST is NULL, then this procedure uses the data from the program
11478       to create all necessary symbol tables, and their linetables.
11479
11480    2. If PST is not NULL, this procedure reads the program to determine
11481       the list of files included by the unit represented by PST, and
11482       builds all the associated partial symbol tables.
11483
11484    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11485    It is used for relative paths in the line table.
11486    NOTE: When processing partial symtabs (pst != NULL),
11487    comp_dir == pst->dirname.
11488
11489    NOTE: It is important that psymtabs have the same file name (via strcmp)
11490    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11491    symtab we don't use it in the name of the psymtabs we create.
11492    E.g. expand_line_sal requires this when finding psymtabs to expand.
11493    A good testcase for this is mb-inline.exp.  */
11494
11495 static void
11496 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11497                     struct dwarf2_cu *cu, struct partial_symtab *pst,
11498                     int want_line_info)
11499 {
11500   struct objfile *objfile = cu->objfile;
11501   const int decode_for_pst_p = (pst != NULL);
11502   struct subfile *first_subfile = current_subfile;
11503
11504   if (want_line_info)
11505     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11506
11507   if (decode_for_pst_p)
11508     {
11509       int file_index;
11510
11511       /* Now that we're done scanning the Line Header Program, we can
11512          create the psymtab of each included file.  */
11513       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11514         if (lh->file_names[file_index].included_p == 1)
11515           {
11516             char *include_name =
11517               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11518             if (include_name != NULL)
11519               dwarf2_create_include_psymtab (include_name, pst, objfile);
11520           }
11521     }
11522   else
11523     {
11524       /* Make sure a symtab is created for every file, even files
11525          which contain only variables (i.e. no code with associated
11526          line numbers).  */
11527       int i;
11528
11529       for (i = 0; i < lh->num_file_names; i++)
11530         {
11531           char *dir = NULL;
11532           struct file_entry *fe;
11533
11534           fe = &lh->file_names[i];
11535           if (fe->dir_index)
11536             dir = lh->include_dirs[fe->dir_index - 1];
11537           dwarf2_start_subfile (fe->name, dir, comp_dir);
11538
11539           /* Skip the main file; we don't need it, and it must be
11540              allocated last, so that it will show up before the
11541              non-primary symtabs in the objfile's symtab list.  */
11542           if (current_subfile == first_subfile)
11543             continue;
11544
11545           if (current_subfile->symtab == NULL)
11546             current_subfile->symtab = allocate_symtab (current_subfile->name,
11547                                                        objfile);
11548           fe->symtab = current_subfile->symtab;
11549         }
11550     }
11551 }
11552
11553 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11554    DIRNAME the name of the source directory which contains FILENAME
11555    or NULL if not known.  COMP_DIR is the compilation directory for the
11556    linetable's compilation unit or NULL if not known.
11557    This routine tries to keep line numbers from identical absolute and
11558    relative file names in a common subfile.
11559
11560    Using the `list' example from the GDB testsuite, which resides in
11561    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11562    of /srcdir/list0.c yields the following debugging information for list0.c:
11563
11564    DW_AT_name:          /srcdir/list0.c
11565    DW_AT_comp_dir:              /compdir
11566    files.files[0].name: list0.h
11567    files.files[0].dir:  /srcdir
11568    files.files[1].name: list0.c
11569    files.files[1].dir:  /srcdir
11570
11571    The line number information for list0.c has to end up in a single
11572    subfile, so that `break /srcdir/list0.c:1' works as expected.
11573    start_subfile will ensure that this happens provided that we pass the
11574    concatenation of files.files[1].dir and files.files[1].name as the
11575    subfile's name.  */
11576
11577 static void
11578 dwarf2_start_subfile (char *filename, const char *dirname,
11579                       const char *comp_dir)
11580 {
11581   char *fullname;
11582
11583   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11584      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11585      second argument to start_subfile.  To be consistent, we do the
11586      same here.  In order not to lose the line information directory,
11587      we concatenate it to the filename when it makes sense.
11588      Note that the Dwarf3 standard says (speaking of filenames in line
11589      information): ``The directory index is ignored for file names
11590      that represent full path names''.  Thus ignoring dirname in the
11591      `else' branch below isn't an issue.  */
11592
11593   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11594     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11595   else
11596     fullname = filename;
11597
11598   start_subfile (fullname, comp_dir);
11599
11600   if (fullname != filename)
11601     xfree (fullname);
11602 }
11603
11604 static void
11605 var_decode_location (struct attribute *attr, struct symbol *sym,
11606                      struct dwarf2_cu *cu)
11607 {
11608   struct objfile *objfile = cu->objfile;
11609   struct comp_unit_head *cu_header = &cu->header;
11610
11611   /* NOTE drow/2003-01-30: There used to be a comment and some special
11612      code here to turn a symbol with DW_AT_external and a
11613      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11614      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11615      with some versions of binutils) where shared libraries could have
11616      relocations against symbols in their debug information - the
11617      minimal symbol would have the right address, but the debug info
11618      would not.  It's no longer necessary, because we will explicitly
11619      apply relocations when we read in the debug information now.  */
11620
11621   /* A DW_AT_location attribute with no contents indicates that a
11622      variable has been optimized away.  */
11623   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11624     {
11625       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11626       return;
11627     }
11628
11629   /* Handle one degenerate form of location expression specially, to
11630      preserve GDB's previous behavior when section offsets are
11631      specified.  If this is just a DW_OP_addr then mark this symbol
11632      as LOC_STATIC.  */
11633
11634   if (attr_form_is_block (attr)
11635       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11636       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11637     {
11638       unsigned int dummy;
11639
11640       SYMBOL_VALUE_ADDRESS (sym) =
11641         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11642       SYMBOL_CLASS (sym) = LOC_STATIC;
11643       fixup_symbol_section (sym, objfile);
11644       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11645                                               SYMBOL_SECTION (sym));
11646       return;
11647     }
11648
11649   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11650      expression evaluator, and use LOC_COMPUTED only when necessary
11651      (i.e. when the value of a register or memory location is
11652      referenced, or a thread-local block, etc.).  Then again, it might
11653      not be worthwhile.  I'm assuming that it isn't unless performance
11654      or memory numbers show me otherwise.  */
11655
11656   dwarf2_symbol_mark_computed (attr, sym, cu);
11657   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11658
11659   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11660     cu->has_loclist = 1;
11661 }
11662
11663 /* Given a pointer to a DWARF information entry, figure out if we need
11664    to make a symbol table entry for it, and if so, create a new entry
11665    and return a pointer to it.
11666    If TYPE is NULL, determine symbol type from the die, otherwise
11667    used the passed type.
11668    If SPACE is not NULL, use it to hold the new symbol.  If it is
11669    NULL, allocate a new symbol on the objfile's obstack.  */
11670
11671 static struct symbol *
11672 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11673                  struct symbol *space)
11674 {
11675   struct objfile *objfile = cu->objfile;
11676   struct symbol *sym = NULL;
11677   char *name;
11678   struct attribute *attr = NULL;
11679   struct attribute *attr2 = NULL;
11680   CORE_ADDR baseaddr;
11681   struct pending **list_to_add = NULL;
11682
11683   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11684
11685   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11686
11687   name = dwarf2_name (die, cu);
11688   if (name)
11689     {
11690       const char *linkagename;
11691       int suppress_add = 0;
11692
11693       if (space)
11694         sym = space;
11695       else
11696         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11697       OBJSTAT (objfile, n_syms++);
11698
11699       /* Cache this symbol's name and the name's demangled form (if any).  */
11700       SYMBOL_SET_LANGUAGE (sym, cu->language);
11701       linkagename = dwarf2_physname (name, die, cu);
11702       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11703
11704       /* Fortran does not have mangling standard and the mangling does differ
11705          between gfortran, iFort etc.  */
11706       if (cu->language == language_fortran
11707           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11708         symbol_set_demangled_name (&(sym->ginfo),
11709                                    (char *) dwarf2_full_name (name, die, cu),
11710                                    NULL);
11711
11712       /* Default assumptions.
11713          Use the passed type or decode it from the die.  */
11714       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11715       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11716       if (type != NULL)
11717         SYMBOL_TYPE (sym) = type;
11718       else
11719         SYMBOL_TYPE (sym) = die_type (die, cu);
11720       attr = dwarf2_attr (die,
11721                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11722                           cu);
11723       if (attr)
11724         {
11725           SYMBOL_LINE (sym) = DW_UNSND (attr);
11726         }
11727
11728       attr = dwarf2_attr (die,
11729                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11730                           cu);
11731       if (attr)
11732         {
11733           int file_index = DW_UNSND (attr);
11734
11735           if (cu->line_header == NULL
11736               || file_index > cu->line_header->num_file_names)
11737             complaint (&symfile_complaints,
11738                        _("file index out of range"));
11739           else if (file_index > 0)
11740             {
11741               struct file_entry *fe;
11742
11743               fe = &cu->line_header->file_names[file_index - 1];
11744               SYMBOL_SYMTAB (sym) = fe->symtab;
11745             }
11746         }
11747
11748       switch (die->tag)
11749         {
11750         case DW_TAG_label:
11751           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11752           if (attr)
11753             {
11754               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11755             }
11756           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11757           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11758           SYMBOL_CLASS (sym) = LOC_LABEL;
11759           add_symbol_to_list (sym, cu->list_in_scope);
11760           break;
11761         case DW_TAG_subprogram:
11762           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11763              finish_block.  */
11764           SYMBOL_CLASS (sym) = LOC_BLOCK;
11765           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11766           if ((attr2 && (DW_UNSND (attr2) != 0))
11767               || cu->language == language_ada)
11768             {
11769               /* Subprograms marked external are stored as a global symbol.
11770                  Ada subprograms, whether marked external or not, are always
11771                  stored as a global symbol, because we want to be able to
11772                  access them globally.  For instance, we want to be able
11773                  to break on a nested subprogram without having to
11774                  specify the context.  */
11775               list_to_add = &global_symbols;
11776             }
11777           else
11778             {
11779               list_to_add = cu->list_in_scope;
11780             }
11781           break;
11782         case DW_TAG_inlined_subroutine:
11783           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11784              finish_block.  */
11785           SYMBOL_CLASS (sym) = LOC_BLOCK;
11786           SYMBOL_INLINED (sym) = 1;
11787           list_to_add = cu->list_in_scope;
11788           break;
11789         case DW_TAG_template_value_param:
11790           suppress_add = 1;
11791           /* Fall through.  */
11792         case DW_TAG_constant:
11793         case DW_TAG_variable:
11794         case DW_TAG_member:
11795           /* Compilation with minimal debug info may result in
11796              variables with missing type entries.  Change the
11797              misleading `void' type to something sensible.  */
11798           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11799             SYMBOL_TYPE (sym)
11800               = objfile_type (objfile)->nodebug_data_symbol;
11801
11802           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11803           /* In the case of DW_TAG_member, we should only be called for
11804              static const members.  */
11805           if (die->tag == DW_TAG_member)
11806             {
11807               /* dwarf2_add_field uses die_is_declaration,
11808                  so we do the same.  */
11809               gdb_assert (die_is_declaration (die, cu));
11810               gdb_assert (attr);
11811             }
11812           if (attr)
11813             {
11814               dwarf2_const_value (attr, sym, cu);
11815               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11816               if (!suppress_add)
11817                 {
11818                   if (attr2 && (DW_UNSND (attr2) != 0))
11819                     list_to_add = &global_symbols;
11820                   else
11821                     list_to_add = cu->list_in_scope;
11822                 }
11823               break;
11824             }
11825           attr = dwarf2_attr (die, DW_AT_location, cu);
11826           if (attr)
11827             {
11828               var_decode_location (attr, sym, cu);
11829               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11830               if (SYMBOL_CLASS (sym) == LOC_STATIC
11831                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11832                   && !dwarf2_per_objfile->has_section_at_zero)
11833                 {
11834                   /* When a static variable is eliminated by the linker,
11835                      the corresponding debug information is not stripped
11836                      out, but the variable address is set to null;
11837                      do not add such variables into symbol table.  */
11838                 }
11839               else if (attr2 && (DW_UNSND (attr2) != 0))
11840                 {
11841                   /* Workaround gfortran PR debug/40040 - it uses
11842                      DW_AT_location for variables in -fPIC libraries which may
11843                      get overriden by other libraries/executable and get
11844                      a different address.  Resolve it by the minimal symbol
11845                      which may come from inferior's executable using copy
11846                      relocation.  Make this workaround only for gfortran as for
11847                      other compilers GDB cannot guess the minimal symbol
11848                      Fortran mangling kind.  */
11849                   if (cu->language == language_fortran && die->parent
11850                       && die->parent->tag == DW_TAG_module
11851                       && cu->producer
11852                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11853                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11854
11855                   /* A variable with DW_AT_external is never static,
11856                      but it may be block-scoped.  */
11857                   list_to_add = (cu->list_in_scope == &file_symbols
11858                                  ? &global_symbols : cu->list_in_scope);
11859                 }
11860               else
11861                 list_to_add = cu->list_in_scope;
11862             }
11863           else
11864             {
11865               /* We do not know the address of this symbol.
11866                  If it is an external symbol and we have type information
11867                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11868                  The address of the variable will then be determined from
11869                  the minimal symbol table whenever the variable is
11870                  referenced.  */
11871               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11872               if (attr2 && (DW_UNSND (attr2) != 0)
11873                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11874                 {
11875                   /* A variable with DW_AT_external is never static, but it
11876                      may be block-scoped.  */
11877                   list_to_add = (cu->list_in_scope == &file_symbols
11878                                  ? &global_symbols : cu->list_in_scope);
11879
11880                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11881                 }
11882               else if (!die_is_declaration (die, cu))
11883                 {
11884                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11885                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11886                   if (!suppress_add)
11887                     list_to_add = cu->list_in_scope;
11888                 }
11889             }
11890           break;
11891         case DW_TAG_formal_parameter:
11892           /* If we are inside a function, mark this as an argument.  If
11893              not, we might be looking at an argument to an inlined function
11894              when we do not have enough information to show inlined frames;
11895              pretend it's a local variable in that case so that the user can
11896              still see it.  */
11897           if (context_stack_depth > 0
11898               && context_stack[context_stack_depth - 1].name != NULL)
11899             SYMBOL_IS_ARGUMENT (sym) = 1;
11900           attr = dwarf2_attr (die, DW_AT_location, cu);
11901           if (attr)
11902             {
11903               var_decode_location (attr, sym, cu);
11904             }
11905           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11906           if (attr)
11907             {
11908               dwarf2_const_value (attr, sym, cu);
11909             }
11910
11911           list_to_add = cu->list_in_scope;
11912           break;
11913         case DW_TAG_unspecified_parameters:
11914           /* From varargs functions; gdb doesn't seem to have any
11915              interest in this information, so just ignore it for now.
11916              (FIXME?) */
11917           break;
11918         case DW_TAG_template_type_param:
11919           suppress_add = 1;
11920           /* Fall through.  */
11921         case DW_TAG_class_type:
11922         case DW_TAG_interface_type:
11923         case DW_TAG_structure_type:
11924         case DW_TAG_union_type:
11925         case DW_TAG_set_type:
11926         case DW_TAG_enumeration_type:
11927           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11928           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11929
11930           {
11931             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11932                really ever be static objects: otherwise, if you try
11933                to, say, break of a class's method and you're in a file
11934                which doesn't mention that class, it won't work unless
11935                the check for all static symbols in lookup_symbol_aux
11936                saves you.  See the OtherFileClass tests in
11937                gdb.c++/namespace.exp.  */
11938
11939             if (!suppress_add)
11940               {
11941                 list_to_add = (cu->list_in_scope == &file_symbols
11942                                && (cu->language == language_cplus
11943                                    || cu->language == language_java)
11944                                ? &global_symbols : cu->list_in_scope);
11945
11946                 /* The semantics of C++ state that "struct foo {
11947                    ... }" also defines a typedef for "foo".  A Java
11948                    class declaration also defines a typedef for the
11949                    class.  */
11950                 if (cu->language == language_cplus
11951                     || cu->language == language_java
11952                     || cu->language == language_ada)
11953                   {
11954                     /* The symbol's name is already allocated along
11955                        with this objfile, so we don't need to
11956                        duplicate it for the type.  */
11957                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11958                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11959                   }
11960               }
11961           }
11962           break;
11963         case DW_TAG_typedef:
11964           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11965           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11966           list_to_add = cu->list_in_scope;
11967           break;
11968         case DW_TAG_base_type:
11969         case DW_TAG_subrange_type:
11970           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11971           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11972           list_to_add = cu->list_in_scope;
11973           break;
11974         case DW_TAG_enumerator:
11975           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11976           if (attr)
11977             {
11978               dwarf2_const_value (attr, sym, cu);
11979             }
11980           {
11981             /* NOTE: carlton/2003-11-10: See comment above in the
11982                DW_TAG_class_type, etc. block.  */
11983
11984             list_to_add = (cu->list_in_scope == &file_symbols
11985                            && (cu->language == language_cplus
11986                                || cu->language == language_java)
11987                            ? &global_symbols : cu->list_in_scope);
11988           }
11989           break;
11990         case DW_TAG_namespace:
11991           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11992           list_to_add = &global_symbols;
11993           break;
11994         default:
11995           /* Not a tag we recognize.  Hopefully we aren't processing
11996              trash data, but since we must specifically ignore things
11997              we don't recognize, there is nothing else we should do at
11998              this point.  */
11999           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
12000                      dwarf_tag_name (die->tag));
12001           break;
12002         }
12003
12004       if (suppress_add)
12005         {
12006           sym->hash_next = objfile->template_symbols;
12007           objfile->template_symbols = sym;
12008           list_to_add = NULL;
12009         }
12010
12011       if (list_to_add != NULL)
12012         add_symbol_to_list (sym, list_to_add);
12013
12014       /* For the benefit of old versions of GCC, check for anonymous
12015          namespaces based on the demangled name.  */
12016       if (!processing_has_namespace_info
12017           && cu->language == language_cplus)
12018         cp_scan_for_anonymous_namespaces (sym, objfile);
12019     }
12020   return (sym);
12021 }
12022
12023 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
12024
12025 static struct symbol *
12026 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12027 {
12028   return new_symbol_full (die, type, cu, NULL);
12029 }
12030
12031 /* Given an attr with a DW_FORM_dataN value in host byte order,
12032    zero-extend it as appropriate for the symbol's type.  The DWARF
12033    standard (v4) is not entirely clear about the meaning of using
12034    DW_FORM_dataN for a constant with a signed type, where the type is
12035    wider than the data.  The conclusion of a discussion on the DWARF
12036    list was that this is unspecified.  We choose to always zero-extend
12037    because that is the interpretation long in use by GCC.  */
12038
12039 static gdb_byte *
12040 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12041                          const char *name, struct obstack *obstack,
12042                          struct dwarf2_cu *cu, long *value, int bits)
12043 {
12044   struct objfile *objfile = cu->objfile;
12045   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12046                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12047   LONGEST l = DW_UNSND (attr);
12048
12049   if (bits < sizeof (*value) * 8)
12050     {
12051       l &= ((LONGEST) 1 << bits) - 1;
12052       *value = l;
12053     }
12054   else if (bits == sizeof (*value) * 8)
12055     *value = l;
12056   else
12057     {
12058       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12059       store_unsigned_integer (bytes, bits / 8, byte_order, l);
12060       return bytes;
12061     }
12062
12063   return NULL;
12064 }
12065
12066 /* Read a constant value from an attribute.  Either set *VALUE, or if
12067    the value does not fit in *VALUE, set *BYTES - either already
12068    allocated on the objfile obstack, or newly allocated on OBSTACK,
12069    or, set *BATON, if we translated the constant to a location
12070    expression.  */
12071
12072 static void
12073 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12074                          const char *name, struct obstack *obstack,
12075                          struct dwarf2_cu *cu,
12076                          long *value, gdb_byte **bytes,
12077                          struct dwarf2_locexpr_baton **baton)
12078 {
12079   struct objfile *objfile = cu->objfile;
12080   struct comp_unit_head *cu_header = &cu->header;
12081   struct dwarf_block *blk;
12082   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12083                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12084
12085   *value = 0;
12086   *bytes = NULL;
12087   *baton = NULL;
12088
12089   switch (attr->form)
12090     {
12091     case DW_FORM_addr:
12092       {
12093         gdb_byte *data;
12094
12095         if (TYPE_LENGTH (type) != cu_header->addr_size)
12096           dwarf2_const_value_length_mismatch_complaint (name,
12097                                                         cu_header->addr_size,
12098                                                         TYPE_LENGTH (type));
12099         /* Symbols of this form are reasonably rare, so we just
12100            piggyback on the existing location code rather than writing
12101            a new implementation of symbol_computed_ops.  */
12102         *baton = obstack_alloc (&objfile->objfile_obstack,
12103                                 sizeof (struct dwarf2_locexpr_baton));
12104         (*baton)->per_cu = cu->per_cu;
12105         gdb_assert ((*baton)->per_cu);
12106
12107         (*baton)->size = 2 + cu_header->addr_size;
12108         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12109         (*baton)->data = data;
12110
12111         data[0] = DW_OP_addr;
12112         store_unsigned_integer (&data[1], cu_header->addr_size,
12113                                 byte_order, DW_ADDR (attr));
12114         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12115       }
12116       break;
12117     case DW_FORM_string:
12118     case DW_FORM_strp:
12119       /* DW_STRING is already allocated on the objfile obstack, point
12120          directly to it.  */
12121       *bytes = (gdb_byte *) DW_STRING (attr);
12122       break;
12123     case DW_FORM_block1:
12124     case DW_FORM_block2:
12125     case DW_FORM_block4:
12126     case DW_FORM_block:
12127     case DW_FORM_exprloc:
12128       blk = DW_BLOCK (attr);
12129       if (TYPE_LENGTH (type) != blk->size)
12130         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12131                                                       TYPE_LENGTH (type));
12132       *bytes = blk->data;
12133       break;
12134
12135       /* The DW_AT_const_value attributes are supposed to carry the
12136          symbol's value "represented as it would be on the target
12137          architecture."  By the time we get here, it's already been
12138          converted to host endianness, so we just need to sign- or
12139          zero-extend it as appropriate.  */
12140     case DW_FORM_data1:
12141       *bytes = dwarf2_const_value_data (attr, type, name,
12142                                         obstack, cu, value, 8);
12143       break;
12144     case DW_FORM_data2:
12145       *bytes = dwarf2_const_value_data (attr, type, name,
12146                                         obstack, cu, value, 16);
12147       break;
12148     case DW_FORM_data4:
12149       *bytes = dwarf2_const_value_data (attr, type, name,
12150                                         obstack, cu, value, 32);
12151       break;
12152     case DW_FORM_data8:
12153       *bytes = dwarf2_const_value_data (attr, type, name,
12154                                         obstack, cu, value, 64);
12155       break;
12156
12157     case DW_FORM_sdata:
12158       *value = DW_SND (attr);
12159       break;
12160
12161     case DW_FORM_udata:
12162       *value = DW_UNSND (attr);
12163       break;
12164
12165     default:
12166       complaint (&symfile_complaints,
12167                  _("unsupported const value attribute form: '%s'"),
12168                  dwarf_form_name (attr->form));
12169       *value = 0;
12170       break;
12171     }
12172 }
12173
12174
12175 /* Copy constant value from an attribute to a symbol.  */
12176
12177 static void
12178 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12179                     struct dwarf2_cu *cu)
12180 {
12181   struct objfile *objfile = cu->objfile;
12182   struct comp_unit_head *cu_header = &cu->header;
12183   long value;
12184   gdb_byte *bytes;
12185   struct dwarf2_locexpr_baton *baton;
12186
12187   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12188                            SYMBOL_PRINT_NAME (sym),
12189                            &objfile->objfile_obstack, cu,
12190                            &value, &bytes, &baton);
12191
12192   if (baton != NULL)
12193     {
12194       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12195       SYMBOL_LOCATION_BATON (sym) = baton;
12196       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12197     }
12198   else if (bytes != NULL)
12199      {
12200       SYMBOL_VALUE_BYTES (sym) = bytes;
12201       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12202     }
12203   else
12204     {
12205       SYMBOL_VALUE (sym) = value;
12206       SYMBOL_CLASS (sym) = LOC_CONST;
12207     }
12208 }
12209
12210 /* Return the type of the die in question using its DW_AT_type attribute.  */
12211
12212 static struct type *
12213 die_type (struct die_info *die, struct dwarf2_cu *cu)
12214 {
12215   struct attribute *type_attr;
12216
12217   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12218   if (!type_attr)
12219     {
12220       /* A missing DW_AT_type represents a void type.  */
12221       return objfile_type (cu->objfile)->builtin_void;
12222     }
12223
12224   return lookup_die_type (die, type_attr, cu);
12225 }
12226
12227 /* True iff CU's producer generates GNAT Ada auxiliary information
12228    that allows to find parallel types through that information instead
12229    of having to do expensive parallel lookups by type name.  */
12230
12231 static int
12232 need_gnat_info (struct dwarf2_cu *cu)
12233 {
12234   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12235      of GNAT produces this auxiliary information, without any indication
12236      that it is produced.  Part of enhancing the FSF version of GNAT
12237      to produce that information will be to put in place an indicator
12238      that we can use in order to determine whether the descriptive type
12239      info is available or not.  One suggestion that has been made is
12240      to use a new attribute, attached to the CU die.  For now, assume
12241      that the descriptive type info is not available.  */
12242   return 0;
12243 }
12244
12245 /* Return the auxiliary type of the die in question using its
12246    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12247    attribute is not present.  */
12248
12249 static struct type *
12250 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12251 {
12252   struct attribute *type_attr;
12253
12254   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12255   if (!type_attr)
12256     return NULL;
12257
12258   return lookup_die_type (die, type_attr, cu);
12259 }
12260
12261 /* If DIE has a descriptive_type attribute, then set the TYPE's
12262    descriptive type accordingly.  */
12263
12264 static void
12265 set_descriptive_type (struct type *type, struct die_info *die,
12266                       struct dwarf2_cu *cu)
12267 {
12268   struct type *descriptive_type = die_descriptive_type (die, cu);
12269
12270   if (descriptive_type)
12271     {
12272       ALLOCATE_GNAT_AUX_TYPE (type);
12273       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12274     }
12275 }
12276
12277 /* Return the containing type of the die in question using its
12278    DW_AT_containing_type attribute.  */
12279
12280 static struct type *
12281 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12282 {
12283   struct attribute *type_attr;
12284
12285   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12286   if (!type_attr)
12287     error (_("Dwarf Error: Problem turning containing type into gdb type "
12288              "[in module %s]"), cu->objfile->name);
12289
12290   return lookup_die_type (die, type_attr, cu);
12291 }
12292
12293 /* Look up the type of DIE in CU using its type attribute ATTR.
12294    If there is no type substitute an error marker.  */
12295
12296 static struct type *
12297 lookup_die_type (struct die_info *die, struct attribute *attr,
12298                  struct dwarf2_cu *cu)
12299 {
12300   struct objfile *objfile = cu->objfile;
12301   struct type *this_type;
12302
12303   /* First see if we have it cached.  */
12304
12305   if (is_ref_attr (attr))
12306     {
12307       sect_offset offset = dwarf2_get_ref_die_offset (attr);
12308
12309       this_type = get_die_type_at_offset (offset, cu->per_cu);
12310     }
12311   else if (attr->form == DW_FORM_ref_sig8)
12312     {
12313       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12314       struct dwarf2_cu *sig_cu;
12315       sect_offset offset;
12316
12317       /* sig_type will be NULL if the signatured type is missing from
12318          the debug info.  */
12319       if (sig_type == NULL)
12320         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12321                  "at 0x%x [in module %s]"),
12322                die->offset.sect_off, objfile->name);
12323
12324       gdb_assert (sig_type->per_cu.debug_types_section);
12325       offset.sect_off = (sig_type->per_cu.offset.sect_off
12326                          + sig_type->type_offset.cu_off);
12327       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12328     }
12329   else
12330     {
12331       dump_die_for_error (die);
12332       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12333              dwarf_attr_name (attr->name), objfile->name);
12334     }
12335
12336   /* If not cached we need to read it in.  */
12337
12338   if (this_type == NULL)
12339     {
12340       struct die_info *type_die;
12341       struct dwarf2_cu *type_cu = cu;
12342
12343       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12344       /* If the type is cached, we should have found it above.  */
12345       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12346       this_type = read_type_die_1 (type_die, type_cu);
12347     }
12348
12349   /* If we still don't have a type use an error marker.  */
12350
12351   if (this_type == NULL)
12352     {
12353       char *message, *saved;
12354
12355       /* read_type_die already issued a complaint.  */
12356       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12357                             objfile->name,
12358                             cu->header.offset.sect_off,
12359                             die->offset.sect_off);
12360       saved = obstack_copy0 (&objfile->objfile_obstack,
12361                              message, strlen (message));
12362       xfree (message);
12363
12364       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12365     }
12366
12367   return this_type;
12368 }
12369
12370 /* Return the type in DIE, CU.
12371    Returns NULL for invalid types.
12372
12373    This first does a lookup in the appropriate type_hash table,
12374    and only reads the die in if necessary.
12375
12376    NOTE: This can be called when reading in partial or full symbols.  */
12377
12378 static struct type *
12379 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12380 {
12381   struct type *this_type;
12382
12383   this_type = get_die_type (die, cu);
12384   if (this_type)
12385     return this_type;
12386
12387   return read_type_die_1 (die, cu);
12388 }
12389
12390 /* Read the type in DIE, CU.
12391    Returns NULL for invalid types.  */
12392
12393 static struct type *
12394 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12395 {
12396   struct type *this_type = NULL;
12397
12398   switch (die->tag)
12399     {
12400     case DW_TAG_class_type:
12401     case DW_TAG_interface_type:
12402     case DW_TAG_structure_type:
12403     case DW_TAG_union_type:
12404       this_type = read_structure_type (die, cu);
12405       break;
12406     case DW_TAG_enumeration_type:
12407       this_type = read_enumeration_type (die, cu);
12408       break;
12409     case DW_TAG_subprogram:
12410     case DW_TAG_subroutine_type:
12411     case DW_TAG_inlined_subroutine:
12412       this_type = read_subroutine_type (die, cu);
12413       break;
12414     case DW_TAG_array_type:
12415       this_type = read_array_type (die, cu);
12416       break;
12417     case DW_TAG_set_type:
12418       this_type = read_set_type (die, cu);
12419       break;
12420     case DW_TAG_pointer_type:
12421       this_type = read_tag_pointer_type (die, cu);
12422       break;
12423     case DW_TAG_ptr_to_member_type:
12424       this_type = read_tag_ptr_to_member_type (die, cu);
12425       break;
12426     case DW_TAG_reference_type:
12427       this_type = read_tag_reference_type (die, cu);
12428       break;
12429     case DW_TAG_const_type:
12430       this_type = read_tag_const_type (die, cu);
12431       break;
12432     case DW_TAG_volatile_type:
12433       this_type = read_tag_volatile_type (die, cu);
12434       break;
12435     case DW_TAG_string_type:
12436       this_type = read_tag_string_type (die, cu);
12437       break;
12438     case DW_TAG_typedef:
12439       this_type = read_typedef (die, cu);
12440       break;
12441     case DW_TAG_subrange_type:
12442       this_type = read_subrange_type (die, cu);
12443       break;
12444     case DW_TAG_base_type:
12445       this_type = read_base_type (die, cu);
12446       break;
12447     case DW_TAG_unspecified_type:
12448       this_type = read_unspecified_type (die, cu);
12449       break;
12450     case DW_TAG_namespace:
12451       this_type = read_namespace_type (die, cu);
12452       break;
12453     case DW_TAG_module:
12454       this_type = read_module_type (die, cu);
12455       break;
12456     default:
12457       complaint (&symfile_complaints,
12458                  _("unexpected tag in read_type_die: '%s'"),
12459                  dwarf_tag_name (die->tag));
12460       break;
12461     }
12462
12463   return this_type;
12464 }
12465
12466 /* See if we can figure out if the class lives in a namespace.  We do
12467    this by looking for a member function; its demangled name will
12468    contain namespace info, if there is any.
12469    Return the computed name or NULL.
12470    Space for the result is allocated on the objfile's obstack.
12471    This is the full-die version of guess_partial_die_structure_name.
12472    In this case we know DIE has no useful parent.  */
12473
12474 static char *
12475 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12476 {
12477   struct die_info *spec_die;
12478   struct dwarf2_cu *spec_cu;
12479   struct die_info *child;
12480
12481   spec_cu = cu;
12482   spec_die = die_specification (die, &spec_cu);
12483   if (spec_die != NULL)
12484     {
12485       die = spec_die;
12486       cu = spec_cu;
12487     }
12488
12489   for (child = die->child;
12490        child != NULL;
12491        child = child->sibling)
12492     {
12493       if (child->tag == DW_TAG_subprogram)
12494         {
12495           struct attribute *attr;
12496
12497           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12498           if (attr == NULL)
12499             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12500           if (attr != NULL)
12501             {
12502               char *actual_name
12503                 = language_class_name_from_physname (cu->language_defn,
12504                                                      DW_STRING (attr));
12505               char *name = NULL;
12506
12507               if (actual_name != NULL)
12508                 {
12509                   char *die_name = dwarf2_name (die, cu);
12510
12511                   if (die_name != NULL
12512                       && strcmp (die_name, actual_name) != 0)
12513                     {
12514                       /* Strip off the class name from the full name.
12515                          We want the prefix.  */
12516                       int die_name_len = strlen (die_name);
12517                       int actual_name_len = strlen (actual_name);
12518
12519                       /* Test for '::' as a sanity check.  */
12520                       if (actual_name_len > die_name_len + 2
12521                           && actual_name[actual_name_len
12522                                          - die_name_len - 1] == ':')
12523                         name =
12524                           obsavestring (actual_name,
12525                                         actual_name_len - die_name_len - 2,
12526                                         &cu->objfile->objfile_obstack);
12527                     }
12528                 }
12529               xfree (actual_name);
12530               return name;
12531             }
12532         }
12533     }
12534
12535   return NULL;
12536 }
12537
12538 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12539    prefix part in such case.  See
12540    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12541
12542 static char *
12543 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12544 {
12545   struct attribute *attr;
12546   char *base;
12547
12548   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12549       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12550     return NULL;
12551
12552   attr = dwarf2_attr (die, DW_AT_name, cu);
12553   if (attr != NULL && DW_STRING (attr) != NULL)
12554     return NULL;
12555
12556   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12557   if (attr == NULL)
12558     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12559   if (attr == NULL || DW_STRING (attr) == NULL)
12560     return NULL;
12561
12562   /* dwarf2_name had to be already called.  */
12563   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12564
12565   /* Strip the base name, keep any leading namespaces/classes.  */
12566   base = strrchr (DW_STRING (attr), ':');
12567   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12568     return "";
12569
12570   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12571                        &cu->objfile->objfile_obstack);
12572 }
12573
12574 /* Return the name of the namespace/class that DIE is defined within,
12575    or "" if we can't tell.  The caller should not xfree the result.
12576
12577    For example, if we're within the method foo() in the following
12578    code:
12579
12580    namespace N {
12581      class C {
12582        void foo () {
12583        }
12584      };
12585    }
12586
12587    then determine_prefix on foo's die will return "N::C".  */
12588
12589 static const char *
12590 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12591 {
12592   struct die_info *parent, *spec_die;
12593   struct dwarf2_cu *spec_cu;
12594   struct type *parent_type;
12595   char *retval;
12596
12597   if (cu->language != language_cplus && cu->language != language_java
12598       && cu->language != language_fortran)
12599     return "";
12600
12601   retval = anonymous_struct_prefix (die, cu);
12602   if (retval)
12603     return retval;
12604
12605   /* We have to be careful in the presence of DW_AT_specification.
12606      For example, with GCC 3.4, given the code
12607
12608      namespace N {
12609        void foo() {
12610          // Definition of N::foo.
12611        }
12612      }
12613
12614      then we'll have a tree of DIEs like this:
12615
12616      1: DW_TAG_compile_unit
12617        2: DW_TAG_namespace        // N
12618          3: DW_TAG_subprogram     // declaration of N::foo
12619        4: DW_TAG_subprogram       // definition of N::foo
12620             DW_AT_specification   // refers to die #3
12621
12622      Thus, when processing die #4, we have to pretend that we're in
12623      the context of its DW_AT_specification, namely the contex of die
12624      #3.  */
12625   spec_cu = cu;
12626   spec_die = die_specification (die, &spec_cu);
12627   if (spec_die == NULL)
12628     parent = die->parent;
12629   else
12630     {
12631       parent = spec_die->parent;
12632       cu = spec_cu;
12633     }
12634
12635   if (parent == NULL)
12636     return "";
12637   else if (parent->building_fullname)
12638     {
12639       const char *name;
12640       const char *parent_name;
12641
12642       /* It has been seen on RealView 2.2 built binaries,
12643          DW_TAG_template_type_param types actually _defined_ as
12644          children of the parent class:
12645
12646          enum E {};
12647          template class <class Enum> Class{};
12648          Class<enum E> class_e;
12649
12650          1: DW_TAG_class_type (Class)
12651            2: DW_TAG_enumeration_type (E)
12652              3: DW_TAG_enumerator (enum1:0)
12653              3: DW_TAG_enumerator (enum2:1)
12654              ...
12655            2: DW_TAG_template_type_param
12656               DW_AT_type  DW_FORM_ref_udata (E)
12657
12658          Besides being broken debug info, it can put GDB into an
12659          infinite loop.  Consider:
12660
12661          When we're building the full name for Class<E>, we'll start
12662          at Class, and go look over its template type parameters,
12663          finding E.  We'll then try to build the full name of E, and
12664          reach here.  We're now trying to build the full name of E,
12665          and look over the parent DIE for containing scope.  In the
12666          broken case, if we followed the parent DIE of E, we'd again
12667          find Class, and once again go look at its template type
12668          arguments, etc., etc.  Simply don't consider such parent die
12669          as source-level parent of this die (it can't be, the language
12670          doesn't allow it), and break the loop here.  */
12671       name = dwarf2_name (die, cu);
12672       parent_name = dwarf2_name (parent, cu);
12673       complaint (&symfile_complaints,
12674                  _("template param type '%s' defined within parent '%s'"),
12675                  name ? name : "<unknown>",
12676                  parent_name ? parent_name : "<unknown>");
12677       return "";
12678     }
12679   else
12680     switch (parent->tag)
12681       {
12682       case DW_TAG_namespace:
12683         parent_type = read_type_die (parent, cu);
12684         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12685            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12686            Work around this problem here.  */
12687         if (cu->language == language_cplus
12688             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12689           return "";
12690         /* We give a name to even anonymous namespaces.  */
12691         return TYPE_TAG_NAME (parent_type);
12692       case DW_TAG_class_type:
12693       case DW_TAG_interface_type:
12694       case DW_TAG_structure_type:
12695       case DW_TAG_union_type:
12696       case DW_TAG_module:
12697         parent_type = read_type_die (parent, cu);
12698         if (TYPE_TAG_NAME (parent_type) != NULL)
12699           return TYPE_TAG_NAME (parent_type);
12700         else
12701           /* An anonymous structure is only allowed non-static data
12702              members; no typedefs, no member functions, et cetera.
12703              So it does not need a prefix.  */
12704           return "";
12705       case DW_TAG_compile_unit:
12706         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12707         if (cu->language == language_cplus
12708             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12709             && die->child != NULL
12710             && (die->tag == DW_TAG_class_type
12711                 || die->tag == DW_TAG_structure_type
12712                 || die->tag == DW_TAG_union_type))
12713           {
12714             char *name = guess_full_die_structure_name (die, cu);
12715             if (name != NULL)
12716               return name;
12717           }
12718         return "";
12719       default:
12720         return determine_prefix (parent, cu);
12721       }
12722 }
12723
12724 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12725    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12726    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12727    an obconcat, otherwise allocate storage for the result.  The CU argument is
12728    used to determine the language and hence, the appropriate separator.  */
12729
12730 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12731
12732 static char *
12733 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12734                  int physname, struct dwarf2_cu *cu)
12735 {
12736   const char *lead = "";
12737   const char *sep;
12738
12739   if (suffix == NULL || suffix[0] == '\0'
12740       || prefix == NULL || prefix[0] == '\0')
12741     sep = "";
12742   else if (cu->language == language_java)
12743     sep = ".";
12744   else if (cu->language == language_fortran && physname)
12745     {
12746       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12747          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12748
12749       lead = "__";
12750       sep = "_MOD_";
12751     }
12752   else
12753     sep = "::";
12754
12755   if (prefix == NULL)
12756     prefix = "";
12757   if (suffix == NULL)
12758     suffix = "";
12759
12760   if (obs == NULL)
12761     {
12762       char *retval
12763         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12764
12765       strcpy (retval, lead);
12766       strcat (retval, prefix);
12767       strcat (retval, sep);
12768       strcat (retval, suffix);
12769       return retval;
12770     }
12771   else
12772     {
12773       /* We have an obstack.  */
12774       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12775     }
12776 }
12777
12778 /* Return sibling of die, NULL if no sibling.  */
12779
12780 static struct die_info *
12781 sibling_die (struct die_info *die)
12782 {
12783   return die->sibling;
12784 }
12785
12786 /* Get name of a die, return NULL if not found.  */
12787
12788 static char *
12789 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12790                           struct obstack *obstack)
12791 {
12792   if (name && cu->language == language_cplus)
12793     {
12794       char *canon_name = cp_canonicalize_string (name);
12795
12796       if (canon_name != NULL)
12797         {
12798           if (strcmp (canon_name, name) != 0)
12799             name = obsavestring (canon_name, strlen (canon_name),
12800                                  obstack);
12801           xfree (canon_name);
12802         }
12803     }
12804
12805   return name;
12806 }
12807
12808 /* Get name of a die, return NULL if not found.  */
12809
12810 static char *
12811 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12812 {
12813   struct attribute *attr;
12814
12815   attr = dwarf2_attr (die, DW_AT_name, cu);
12816   if ((!attr || !DW_STRING (attr))
12817       && die->tag != DW_TAG_class_type
12818       && die->tag != DW_TAG_interface_type
12819       && die->tag != DW_TAG_structure_type
12820       && die->tag != DW_TAG_union_type)
12821     return NULL;
12822
12823   switch (die->tag)
12824     {
12825     case DW_TAG_compile_unit:
12826       /* Compilation units have a DW_AT_name that is a filename, not
12827          a source language identifier.  */
12828     case DW_TAG_enumeration_type:
12829     case DW_TAG_enumerator:
12830       /* These tags always have simple identifiers already; no need
12831          to canonicalize them.  */
12832       return DW_STRING (attr);
12833
12834     case DW_TAG_subprogram:
12835       /* Java constructors will all be named "<init>", so return
12836          the class name when we see this special case.  */
12837       if (cu->language == language_java
12838           && DW_STRING (attr) != NULL
12839           && strcmp (DW_STRING (attr), "<init>") == 0)
12840         {
12841           struct dwarf2_cu *spec_cu = cu;
12842           struct die_info *spec_die;
12843
12844           /* GCJ will output '<init>' for Java constructor names.
12845              For this special case, return the name of the parent class.  */
12846
12847           /* GCJ may output suprogram DIEs with AT_specification set.
12848              If so, use the name of the specified DIE.  */
12849           spec_die = die_specification (die, &spec_cu);
12850           if (spec_die != NULL)
12851             return dwarf2_name (spec_die, spec_cu);
12852
12853           do
12854             {
12855               die = die->parent;
12856               if (die->tag == DW_TAG_class_type)
12857                 return dwarf2_name (die, cu);
12858             }
12859           while (die->tag != DW_TAG_compile_unit);
12860         }
12861       break;
12862
12863     case DW_TAG_class_type:
12864     case DW_TAG_interface_type:
12865     case DW_TAG_structure_type:
12866     case DW_TAG_union_type:
12867       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12868          structures or unions.  These were of the form "._%d" in GCC 4.1,
12869          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12870          and GCC 4.4.  We work around this problem by ignoring these.  */
12871       if (attr && DW_STRING (attr)
12872           && (strncmp (DW_STRING (attr), "._", 2) == 0
12873               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12874         return NULL;
12875
12876       /* GCC might emit a nameless typedef that has a linkage name.  See
12877          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12878       if (!attr || DW_STRING (attr) == NULL)
12879         {
12880           char *demangled = NULL;
12881
12882           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12883           if (attr == NULL)
12884             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12885
12886           if (attr == NULL || DW_STRING (attr) == NULL)
12887             return NULL;
12888
12889           /* Avoid demangling DW_STRING (attr) the second time on a second
12890              call for the same DIE.  */
12891           if (!DW_STRING_IS_CANONICAL (attr))
12892             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12893
12894           if (demangled)
12895             {
12896               char *base;
12897
12898               /* FIXME: we already did this for the partial symbol... */
12899               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12900                                                &cu->objfile->objfile_obstack);
12901               DW_STRING_IS_CANONICAL (attr) = 1;
12902               xfree (demangled);
12903
12904               /* Strip any leading namespaces/classes, keep only the base name.
12905                  DW_AT_name for named DIEs does not contain the prefixes.  */
12906               base = strrchr (DW_STRING (attr), ':');
12907               if (base && base > DW_STRING (attr) && base[-1] == ':')
12908                 return &base[1];
12909               else
12910                 return DW_STRING (attr);
12911             }
12912         }
12913       break;
12914
12915     default:
12916       break;
12917     }
12918
12919   if (!DW_STRING_IS_CANONICAL (attr))
12920     {
12921       DW_STRING (attr)
12922         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12923                                     &cu->objfile->objfile_obstack);
12924       DW_STRING_IS_CANONICAL (attr) = 1;
12925     }
12926   return DW_STRING (attr);
12927 }
12928
12929 /* Return the die that this die in an extension of, or NULL if there
12930    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12931    containing the return value on output.  */
12932
12933 static struct die_info *
12934 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12935 {
12936   struct attribute *attr;
12937
12938   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12939   if (attr == NULL)
12940     return NULL;
12941
12942   return follow_die_ref (die, attr, ext_cu);
12943 }
12944
12945 /* Convert a DIE tag into its string name.  */
12946
12947 static char *
12948 dwarf_tag_name (unsigned tag)
12949 {
12950   switch (tag)
12951     {
12952     case DW_TAG_padding:
12953       return "DW_TAG_padding";
12954     case DW_TAG_array_type:
12955       return "DW_TAG_array_type";
12956     case DW_TAG_class_type:
12957       return "DW_TAG_class_type";
12958     case DW_TAG_entry_point:
12959       return "DW_TAG_entry_point";
12960     case DW_TAG_enumeration_type:
12961       return "DW_TAG_enumeration_type";
12962     case DW_TAG_formal_parameter:
12963       return "DW_TAG_formal_parameter";
12964     case DW_TAG_imported_declaration:
12965       return "DW_TAG_imported_declaration";
12966     case DW_TAG_label:
12967       return "DW_TAG_label";
12968     case DW_TAG_lexical_block:
12969       return "DW_TAG_lexical_block";
12970     case DW_TAG_member:
12971       return "DW_TAG_member";
12972     case DW_TAG_pointer_type:
12973       return "DW_TAG_pointer_type";
12974     case DW_TAG_reference_type:
12975       return "DW_TAG_reference_type";
12976     case DW_TAG_compile_unit:
12977       return "DW_TAG_compile_unit";
12978     case DW_TAG_string_type:
12979       return "DW_TAG_string_type";
12980     case DW_TAG_structure_type:
12981       return "DW_TAG_structure_type";
12982     case DW_TAG_subroutine_type:
12983       return "DW_TAG_subroutine_type";
12984     case DW_TAG_typedef:
12985       return "DW_TAG_typedef";
12986     case DW_TAG_union_type:
12987       return "DW_TAG_union_type";
12988     case DW_TAG_unspecified_parameters:
12989       return "DW_TAG_unspecified_parameters";
12990     case DW_TAG_variant:
12991       return "DW_TAG_variant";
12992     case DW_TAG_common_block:
12993       return "DW_TAG_common_block";
12994     case DW_TAG_common_inclusion:
12995       return "DW_TAG_common_inclusion";
12996     case DW_TAG_inheritance:
12997       return "DW_TAG_inheritance";
12998     case DW_TAG_inlined_subroutine:
12999       return "DW_TAG_inlined_subroutine";
13000     case DW_TAG_module:
13001       return "DW_TAG_module";
13002     case DW_TAG_ptr_to_member_type:
13003       return "DW_TAG_ptr_to_member_type";
13004     case DW_TAG_set_type:
13005       return "DW_TAG_set_type";
13006     case DW_TAG_subrange_type:
13007       return "DW_TAG_subrange_type";
13008     case DW_TAG_with_stmt:
13009       return "DW_TAG_with_stmt";
13010     case DW_TAG_access_declaration:
13011       return "DW_TAG_access_declaration";
13012     case DW_TAG_base_type:
13013       return "DW_TAG_base_type";
13014     case DW_TAG_catch_block:
13015       return "DW_TAG_catch_block";
13016     case DW_TAG_const_type:
13017       return "DW_TAG_const_type";
13018     case DW_TAG_constant:
13019       return "DW_TAG_constant";
13020     case DW_TAG_enumerator:
13021       return "DW_TAG_enumerator";
13022     case DW_TAG_file_type:
13023       return "DW_TAG_file_type";
13024     case DW_TAG_friend:
13025       return "DW_TAG_friend";
13026     case DW_TAG_namelist:
13027       return "DW_TAG_namelist";
13028     case DW_TAG_namelist_item:
13029       return "DW_TAG_namelist_item";
13030     case DW_TAG_packed_type:
13031       return "DW_TAG_packed_type";
13032     case DW_TAG_subprogram:
13033       return "DW_TAG_subprogram";
13034     case DW_TAG_template_type_param:
13035       return "DW_TAG_template_type_param";
13036     case DW_TAG_template_value_param:
13037       return "DW_TAG_template_value_param";
13038     case DW_TAG_thrown_type:
13039       return "DW_TAG_thrown_type";
13040     case DW_TAG_try_block:
13041       return "DW_TAG_try_block";
13042     case DW_TAG_variant_part:
13043       return "DW_TAG_variant_part";
13044     case DW_TAG_variable:
13045       return "DW_TAG_variable";
13046     case DW_TAG_volatile_type:
13047       return "DW_TAG_volatile_type";
13048     case DW_TAG_dwarf_procedure:
13049       return "DW_TAG_dwarf_procedure";
13050     case DW_TAG_restrict_type:
13051       return "DW_TAG_restrict_type";
13052     case DW_TAG_interface_type:
13053       return "DW_TAG_interface_type";
13054     case DW_TAG_namespace:
13055       return "DW_TAG_namespace";
13056     case DW_TAG_imported_module:
13057       return "DW_TAG_imported_module";
13058     case DW_TAG_unspecified_type:
13059       return "DW_TAG_unspecified_type";
13060     case DW_TAG_partial_unit:
13061       return "DW_TAG_partial_unit";
13062     case DW_TAG_imported_unit:
13063       return "DW_TAG_imported_unit";
13064     case DW_TAG_condition:
13065       return "DW_TAG_condition";
13066     case DW_TAG_shared_type:
13067       return "DW_TAG_shared_type";
13068     case DW_TAG_type_unit:
13069       return "DW_TAG_type_unit";
13070     case DW_TAG_MIPS_loop:
13071       return "DW_TAG_MIPS_loop";
13072     case DW_TAG_HP_array_descriptor:
13073       return "DW_TAG_HP_array_descriptor";
13074     case DW_TAG_format_label:
13075       return "DW_TAG_format_label";
13076     case DW_TAG_function_template:
13077       return "DW_TAG_function_template";
13078     case DW_TAG_class_template:
13079       return "DW_TAG_class_template";
13080     case DW_TAG_GNU_BINCL:
13081       return "DW_TAG_GNU_BINCL";
13082     case DW_TAG_GNU_EINCL:
13083       return "DW_TAG_GNU_EINCL";
13084     case DW_TAG_upc_shared_type:
13085       return "DW_TAG_upc_shared_type";
13086     case DW_TAG_upc_strict_type:
13087       return "DW_TAG_upc_strict_type";
13088     case DW_TAG_upc_relaxed_type:
13089       return "DW_TAG_upc_relaxed_type";
13090     case DW_TAG_PGI_kanji_type:
13091       return "DW_TAG_PGI_kanji_type";
13092     case DW_TAG_PGI_interface_block:
13093       return "DW_TAG_PGI_interface_block";
13094     case DW_TAG_GNU_call_site:
13095       return "DW_TAG_GNU_call_site";
13096     default:
13097       return "DW_TAG_<unknown>";
13098     }
13099 }
13100
13101 /* Convert a DWARF attribute code into its string name.  */
13102
13103 static char *
13104 dwarf_attr_name (unsigned attr)
13105 {
13106   switch (attr)
13107     {
13108     case DW_AT_sibling:
13109       return "DW_AT_sibling";
13110     case DW_AT_location:
13111       return "DW_AT_location";
13112     case DW_AT_name:
13113       return "DW_AT_name";
13114     case DW_AT_ordering:
13115       return "DW_AT_ordering";
13116     case DW_AT_subscr_data:
13117       return "DW_AT_subscr_data";
13118     case DW_AT_byte_size:
13119       return "DW_AT_byte_size";
13120     case DW_AT_bit_offset:
13121       return "DW_AT_bit_offset";
13122     case DW_AT_bit_size:
13123       return "DW_AT_bit_size";
13124     case DW_AT_element_list:
13125       return "DW_AT_element_list";
13126     case DW_AT_stmt_list:
13127       return "DW_AT_stmt_list";
13128     case DW_AT_low_pc:
13129       return "DW_AT_low_pc";
13130     case DW_AT_high_pc:
13131       return "DW_AT_high_pc";
13132     case DW_AT_language:
13133       return "DW_AT_language";
13134     case DW_AT_member:
13135       return "DW_AT_member";
13136     case DW_AT_discr:
13137       return "DW_AT_discr";
13138     case DW_AT_discr_value:
13139       return "DW_AT_discr_value";
13140     case DW_AT_visibility:
13141       return "DW_AT_visibility";
13142     case DW_AT_import:
13143       return "DW_AT_import";
13144     case DW_AT_string_length:
13145       return "DW_AT_string_length";
13146     case DW_AT_common_reference:
13147       return "DW_AT_common_reference";
13148     case DW_AT_comp_dir:
13149       return "DW_AT_comp_dir";
13150     case DW_AT_const_value:
13151       return "DW_AT_const_value";
13152     case DW_AT_containing_type:
13153       return "DW_AT_containing_type";
13154     case DW_AT_default_value:
13155       return "DW_AT_default_value";
13156     case DW_AT_inline:
13157       return "DW_AT_inline";
13158     case DW_AT_is_optional:
13159       return "DW_AT_is_optional";
13160     case DW_AT_lower_bound:
13161       return "DW_AT_lower_bound";
13162     case DW_AT_producer:
13163       return "DW_AT_producer";
13164     case DW_AT_prototyped:
13165       return "DW_AT_prototyped";
13166     case DW_AT_return_addr:
13167       return "DW_AT_return_addr";
13168     case DW_AT_start_scope:
13169       return "DW_AT_start_scope";
13170     case DW_AT_bit_stride:
13171       return "DW_AT_bit_stride";
13172     case DW_AT_upper_bound:
13173       return "DW_AT_upper_bound";
13174     case DW_AT_abstract_origin:
13175       return "DW_AT_abstract_origin";
13176     case DW_AT_accessibility:
13177       return "DW_AT_accessibility";
13178     case DW_AT_address_class:
13179       return "DW_AT_address_class";
13180     case DW_AT_artificial:
13181       return "DW_AT_artificial";
13182     case DW_AT_base_types:
13183       return "DW_AT_base_types";
13184     case DW_AT_calling_convention:
13185       return "DW_AT_calling_convention";
13186     case DW_AT_count:
13187       return "DW_AT_count";
13188     case DW_AT_data_member_location:
13189       return "DW_AT_data_member_location";
13190     case DW_AT_decl_column:
13191       return "DW_AT_decl_column";
13192     case DW_AT_decl_file:
13193       return "DW_AT_decl_file";
13194     case DW_AT_decl_line:
13195       return "DW_AT_decl_line";
13196     case DW_AT_declaration:
13197       return "DW_AT_declaration";
13198     case DW_AT_discr_list:
13199       return "DW_AT_discr_list";
13200     case DW_AT_encoding:
13201       return "DW_AT_encoding";
13202     case DW_AT_external:
13203       return "DW_AT_external";
13204     case DW_AT_frame_base:
13205       return "DW_AT_frame_base";
13206     case DW_AT_friend:
13207       return "DW_AT_friend";
13208     case DW_AT_identifier_case:
13209       return "DW_AT_identifier_case";
13210     case DW_AT_macro_info:
13211       return "DW_AT_macro_info";
13212     case DW_AT_namelist_items:
13213       return "DW_AT_namelist_items";
13214     case DW_AT_priority:
13215       return "DW_AT_priority";
13216     case DW_AT_segment:
13217       return "DW_AT_segment";
13218     case DW_AT_specification:
13219       return "DW_AT_specification";
13220     case DW_AT_static_link:
13221       return "DW_AT_static_link";
13222     case DW_AT_type:
13223       return "DW_AT_type";
13224     case DW_AT_use_location:
13225       return "DW_AT_use_location";
13226     case DW_AT_variable_parameter:
13227       return "DW_AT_variable_parameter";
13228     case DW_AT_virtuality:
13229       return "DW_AT_virtuality";
13230     case DW_AT_vtable_elem_location:
13231       return "DW_AT_vtable_elem_location";
13232     /* DWARF 3 values.  */
13233     case DW_AT_allocated:
13234       return "DW_AT_allocated";
13235     case DW_AT_associated:
13236       return "DW_AT_associated";
13237     case DW_AT_data_location:
13238       return "DW_AT_data_location";
13239     case DW_AT_byte_stride:
13240       return "DW_AT_byte_stride";
13241     case DW_AT_entry_pc:
13242       return "DW_AT_entry_pc";
13243     case DW_AT_use_UTF8:
13244       return "DW_AT_use_UTF8";
13245     case DW_AT_extension:
13246       return "DW_AT_extension";
13247     case DW_AT_ranges:
13248       return "DW_AT_ranges";
13249     case DW_AT_trampoline:
13250       return "DW_AT_trampoline";
13251     case DW_AT_call_column:
13252       return "DW_AT_call_column";
13253     case DW_AT_call_file:
13254       return "DW_AT_call_file";
13255     case DW_AT_call_line:
13256       return "DW_AT_call_line";
13257     case DW_AT_description:
13258       return "DW_AT_description";
13259     case DW_AT_binary_scale:
13260       return "DW_AT_binary_scale";
13261     case DW_AT_decimal_scale:
13262       return "DW_AT_decimal_scale";
13263     case DW_AT_small:
13264       return "DW_AT_small";
13265     case DW_AT_decimal_sign:
13266       return "DW_AT_decimal_sign";
13267     case DW_AT_digit_count:
13268       return "DW_AT_digit_count";
13269     case DW_AT_picture_string:
13270       return "DW_AT_picture_string";
13271     case DW_AT_mutable:
13272       return "DW_AT_mutable";
13273     case DW_AT_threads_scaled:
13274       return "DW_AT_threads_scaled";
13275     case DW_AT_explicit:
13276       return "DW_AT_explicit";
13277     case DW_AT_object_pointer:
13278       return "DW_AT_object_pointer";
13279     case DW_AT_endianity:
13280       return "DW_AT_endianity";
13281     case DW_AT_elemental:
13282       return "DW_AT_elemental";
13283     case DW_AT_pure:
13284       return "DW_AT_pure";
13285     case DW_AT_recursive:
13286       return "DW_AT_recursive";
13287     /* DWARF 4 values.  */
13288     case DW_AT_signature:
13289       return "DW_AT_signature";
13290     case DW_AT_linkage_name:
13291       return "DW_AT_linkage_name";
13292     /* SGI/MIPS extensions.  */
13293 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13294     case DW_AT_MIPS_fde:
13295       return "DW_AT_MIPS_fde";
13296 #endif
13297     case DW_AT_MIPS_loop_begin:
13298       return "DW_AT_MIPS_loop_begin";
13299     case DW_AT_MIPS_tail_loop_begin:
13300       return "DW_AT_MIPS_tail_loop_begin";
13301     case DW_AT_MIPS_epilog_begin:
13302       return "DW_AT_MIPS_epilog_begin";
13303     case DW_AT_MIPS_loop_unroll_factor:
13304       return "DW_AT_MIPS_loop_unroll_factor";
13305     case DW_AT_MIPS_software_pipeline_depth:
13306       return "DW_AT_MIPS_software_pipeline_depth";
13307     case DW_AT_MIPS_linkage_name:
13308       return "DW_AT_MIPS_linkage_name";
13309     case DW_AT_MIPS_stride:
13310       return "DW_AT_MIPS_stride";
13311     case DW_AT_MIPS_abstract_name:
13312       return "DW_AT_MIPS_abstract_name";
13313     case DW_AT_MIPS_clone_origin:
13314       return "DW_AT_MIPS_clone_origin";
13315     case DW_AT_MIPS_has_inlines:
13316       return "DW_AT_MIPS_has_inlines";
13317     /* HP extensions.  */
13318 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13319     case DW_AT_HP_block_index:
13320       return "DW_AT_HP_block_index";
13321 #endif
13322     case DW_AT_HP_unmodifiable:
13323       return "DW_AT_HP_unmodifiable";
13324     case DW_AT_HP_actuals_stmt_list:
13325       return "DW_AT_HP_actuals_stmt_list";
13326     case DW_AT_HP_proc_per_section:
13327       return "DW_AT_HP_proc_per_section";
13328     case DW_AT_HP_raw_data_ptr:
13329       return "DW_AT_HP_raw_data_ptr";
13330     case DW_AT_HP_pass_by_reference:
13331       return "DW_AT_HP_pass_by_reference";
13332     case DW_AT_HP_opt_level:
13333       return "DW_AT_HP_opt_level";
13334     case DW_AT_HP_prof_version_id:
13335       return "DW_AT_HP_prof_version_id";
13336     case DW_AT_HP_opt_flags:
13337       return "DW_AT_HP_opt_flags";
13338     case DW_AT_HP_cold_region_low_pc:
13339       return "DW_AT_HP_cold_region_low_pc";
13340     case DW_AT_HP_cold_region_high_pc:
13341       return "DW_AT_HP_cold_region_high_pc";
13342     case DW_AT_HP_all_variables_modifiable:
13343       return "DW_AT_HP_all_variables_modifiable";
13344     case DW_AT_HP_linkage_name:
13345       return "DW_AT_HP_linkage_name";
13346     case DW_AT_HP_prof_flags:
13347       return "DW_AT_HP_prof_flags";
13348     /* GNU extensions.  */
13349     case DW_AT_sf_names:
13350       return "DW_AT_sf_names";
13351     case DW_AT_src_info:
13352       return "DW_AT_src_info";
13353     case DW_AT_mac_info:
13354       return "DW_AT_mac_info";
13355     case DW_AT_src_coords:
13356       return "DW_AT_src_coords";
13357     case DW_AT_body_begin:
13358       return "DW_AT_body_begin";
13359     case DW_AT_body_end:
13360       return "DW_AT_body_end";
13361     case DW_AT_GNU_vector:
13362       return "DW_AT_GNU_vector";
13363     case DW_AT_GNU_odr_signature:
13364       return "DW_AT_GNU_odr_signature";
13365     /* VMS extensions.  */
13366     case DW_AT_VMS_rtnbeg_pd_address:
13367       return "DW_AT_VMS_rtnbeg_pd_address";
13368     /* UPC extension.  */
13369     case DW_AT_upc_threads_scaled:
13370       return "DW_AT_upc_threads_scaled";
13371     /* PGI (STMicroelectronics) extensions.  */
13372     case DW_AT_PGI_lbase:
13373       return "DW_AT_PGI_lbase";
13374     case DW_AT_PGI_soffset:
13375       return "DW_AT_PGI_soffset";
13376     case DW_AT_PGI_lstride:
13377       return "DW_AT_PGI_lstride";
13378     default:
13379       return "DW_AT_<unknown>";
13380     }
13381 }
13382
13383 /* Convert a DWARF value form code into its string name.  */
13384
13385 static char *
13386 dwarf_form_name (unsigned form)
13387 {
13388   switch (form)
13389     {
13390     case DW_FORM_addr:
13391       return "DW_FORM_addr";
13392     case DW_FORM_block2:
13393       return "DW_FORM_block2";
13394     case DW_FORM_block4:
13395       return "DW_FORM_block4";
13396     case DW_FORM_data2:
13397       return "DW_FORM_data2";
13398     case DW_FORM_data4:
13399       return "DW_FORM_data4";
13400     case DW_FORM_data8:
13401       return "DW_FORM_data8";
13402     case DW_FORM_string:
13403       return "DW_FORM_string";
13404     case DW_FORM_block:
13405       return "DW_FORM_block";
13406     case DW_FORM_block1:
13407       return "DW_FORM_block1";
13408     case DW_FORM_data1:
13409       return "DW_FORM_data1";
13410     case DW_FORM_flag:
13411       return "DW_FORM_flag";
13412     case DW_FORM_sdata:
13413       return "DW_FORM_sdata";
13414     case DW_FORM_strp:
13415       return "DW_FORM_strp";
13416     case DW_FORM_udata:
13417       return "DW_FORM_udata";
13418     case DW_FORM_ref_addr:
13419       return "DW_FORM_ref_addr";
13420     case DW_FORM_ref1:
13421       return "DW_FORM_ref1";
13422     case DW_FORM_ref2:
13423       return "DW_FORM_ref2";
13424     case DW_FORM_ref4:
13425       return "DW_FORM_ref4";
13426     case DW_FORM_ref8:
13427       return "DW_FORM_ref8";
13428     case DW_FORM_ref_udata:
13429       return "DW_FORM_ref_udata";
13430     case DW_FORM_indirect:
13431       return "DW_FORM_indirect";
13432     case DW_FORM_sec_offset:
13433       return "DW_FORM_sec_offset";
13434     case DW_FORM_exprloc:
13435       return "DW_FORM_exprloc";
13436     case DW_FORM_flag_present:
13437       return "DW_FORM_flag_present";
13438     case DW_FORM_ref_sig8:
13439       return "DW_FORM_ref_sig8";
13440     default:
13441       return "DW_FORM_<unknown>";
13442     }
13443 }
13444
13445 /* Convert a DWARF stack opcode into its string name.  */
13446
13447 const char *
13448 dwarf_stack_op_name (unsigned op)
13449 {
13450   switch (op)
13451     {
13452     case DW_OP_addr:
13453       return "DW_OP_addr";
13454     case DW_OP_deref:
13455       return "DW_OP_deref";
13456     case DW_OP_const1u:
13457       return "DW_OP_const1u";
13458     case DW_OP_const1s:
13459       return "DW_OP_const1s";
13460     case DW_OP_const2u:
13461       return "DW_OP_const2u";
13462     case DW_OP_const2s:
13463       return "DW_OP_const2s";
13464     case DW_OP_const4u:
13465       return "DW_OP_const4u";
13466     case DW_OP_const4s:
13467       return "DW_OP_const4s";
13468     case DW_OP_const8u:
13469       return "DW_OP_const8u";
13470     case DW_OP_const8s:
13471       return "DW_OP_const8s";
13472     case DW_OP_constu:
13473       return "DW_OP_constu";
13474     case DW_OP_consts:
13475       return "DW_OP_consts";
13476     case DW_OP_dup:
13477       return "DW_OP_dup";
13478     case DW_OP_drop:
13479       return "DW_OP_drop";
13480     case DW_OP_over:
13481       return "DW_OP_over";
13482     case DW_OP_pick:
13483       return "DW_OP_pick";
13484     case DW_OP_swap:
13485       return "DW_OP_swap";
13486     case DW_OP_rot:
13487       return "DW_OP_rot";
13488     case DW_OP_xderef:
13489       return "DW_OP_xderef";
13490     case DW_OP_abs:
13491       return "DW_OP_abs";
13492     case DW_OP_and:
13493       return "DW_OP_and";
13494     case DW_OP_div:
13495       return "DW_OP_div";
13496     case DW_OP_minus:
13497       return "DW_OP_minus";
13498     case DW_OP_mod:
13499       return "DW_OP_mod";
13500     case DW_OP_mul:
13501       return "DW_OP_mul";
13502     case DW_OP_neg:
13503       return "DW_OP_neg";
13504     case DW_OP_not:
13505       return "DW_OP_not";
13506     case DW_OP_or:
13507       return "DW_OP_or";
13508     case DW_OP_plus:
13509       return "DW_OP_plus";
13510     case DW_OP_plus_uconst:
13511       return "DW_OP_plus_uconst";
13512     case DW_OP_shl:
13513       return "DW_OP_shl";
13514     case DW_OP_shr:
13515       return "DW_OP_shr";
13516     case DW_OP_shra:
13517       return "DW_OP_shra";
13518     case DW_OP_xor:
13519       return "DW_OP_xor";
13520     case DW_OP_bra:
13521       return "DW_OP_bra";
13522     case DW_OP_eq:
13523       return "DW_OP_eq";
13524     case DW_OP_ge:
13525       return "DW_OP_ge";
13526     case DW_OP_gt:
13527       return "DW_OP_gt";
13528     case DW_OP_le:
13529       return "DW_OP_le";
13530     case DW_OP_lt:
13531       return "DW_OP_lt";
13532     case DW_OP_ne:
13533       return "DW_OP_ne";
13534     case DW_OP_skip:
13535       return "DW_OP_skip";
13536     case DW_OP_lit0:
13537       return "DW_OP_lit0";
13538     case DW_OP_lit1:
13539       return "DW_OP_lit1";
13540     case DW_OP_lit2:
13541       return "DW_OP_lit2";
13542     case DW_OP_lit3:
13543       return "DW_OP_lit3";
13544     case DW_OP_lit4:
13545       return "DW_OP_lit4";
13546     case DW_OP_lit5:
13547       return "DW_OP_lit5";
13548     case DW_OP_lit6:
13549       return "DW_OP_lit6";
13550     case DW_OP_lit7:
13551       return "DW_OP_lit7";
13552     case DW_OP_lit8:
13553       return "DW_OP_lit8";
13554     case DW_OP_lit9:
13555       return "DW_OP_lit9";
13556     case DW_OP_lit10:
13557       return "DW_OP_lit10";
13558     case DW_OP_lit11:
13559       return "DW_OP_lit11";
13560     case DW_OP_lit12:
13561       return "DW_OP_lit12";
13562     case DW_OP_lit13:
13563       return "DW_OP_lit13";
13564     case DW_OP_lit14:
13565       return "DW_OP_lit14";
13566     case DW_OP_lit15:
13567       return "DW_OP_lit15";
13568     case DW_OP_lit16:
13569       return "DW_OP_lit16";
13570     case DW_OP_lit17:
13571       return "DW_OP_lit17";
13572     case DW_OP_lit18:
13573       return "DW_OP_lit18";
13574     case DW_OP_lit19:
13575       return "DW_OP_lit19";
13576     case DW_OP_lit20:
13577       return "DW_OP_lit20";
13578     case DW_OP_lit21:
13579       return "DW_OP_lit21";
13580     case DW_OP_lit22:
13581       return "DW_OP_lit22";
13582     case DW_OP_lit23:
13583       return "DW_OP_lit23";
13584     case DW_OP_lit24:
13585       return "DW_OP_lit24";
13586     case DW_OP_lit25:
13587       return "DW_OP_lit25";
13588     case DW_OP_lit26:
13589       return "DW_OP_lit26";
13590     case DW_OP_lit27:
13591       return "DW_OP_lit27";
13592     case DW_OP_lit28:
13593       return "DW_OP_lit28";
13594     case DW_OP_lit29:
13595       return "DW_OP_lit29";
13596     case DW_OP_lit30:
13597       return "DW_OP_lit30";
13598     case DW_OP_lit31:
13599       return "DW_OP_lit31";
13600     case DW_OP_reg0:
13601       return "DW_OP_reg0";
13602     case DW_OP_reg1:
13603       return "DW_OP_reg1";
13604     case DW_OP_reg2:
13605       return "DW_OP_reg2";
13606     case DW_OP_reg3:
13607       return "DW_OP_reg3";
13608     case DW_OP_reg4:
13609       return "DW_OP_reg4";
13610     case DW_OP_reg5:
13611       return "DW_OP_reg5";
13612     case DW_OP_reg6:
13613       return "DW_OP_reg6";
13614     case DW_OP_reg7:
13615       return "DW_OP_reg7";
13616     case DW_OP_reg8:
13617       return "DW_OP_reg8";
13618     case DW_OP_reg9:
13619       return "DW_OP_reg9";
13620     case DW_OP_reg10:
13621       return "DW_OP_reg10";
13622     case DW_OP_reg11:
13623       return "DW_OP_reg11";
13624     case DW_OP_reg12:
13625       return "DW_OP_reg12";
13626     case DW_OP_reg13:
13627       return "DW_OP_reg13";
13628     case DW_OP_reg14:
13629       return "DW_OP_reg14";
13630     case DW_OP_reg15:
13631       return "DW_OP_reg15";
13632     case DW_OP_reg16:
13633       return "DW_OP_reg16";
13634     case DW_OP_reg17:
13635       return "DW_OP_reg17";
13636     case DW_OP_reg18:
13637       return "DW_OP_reg18";
13638     case DW_OP_reg19:
13639       return "DW_OP_reg19";
13640     case DW_OP_reg20:
13641       return "DW_OP_reg20";
13642     case DW_OP_reg21:
13643       return "DW_OP_reg21";
13644     case DW_OP_reg22:
13645       return "DW_OP_reg22";
13646     case DW_OP_reg23:
13647       return "DW_OP_reg23";
13648     case DW_OP_reg24:
13649       return "DW_OP_reg24";
13650     case DW_OP_reg25:
13651       return "DW_OP_reg25";
13652     case DW_OP_reg26:
13653       return "DW_OP_reg26";
13654     case DW_OP_reg27:
13655       return "DW_OP_reg27";
13656     case DW_OP_reg28:
13657       return "DW_OP_reg28";
13658     case DW_OP_reg29:
13659       return "DW_OP_reg29";
13660     case DW_OP_reg30:
13661       return "DW_OP_reg30";
13662     case DW_OP_reg31:
13663       return "DW_OP_reg31";
13664     case DW_OP_breg0:
13665       return "DW_OP_breg0";
13666     case DW_OP_breg1:
13667       return "DW_OP_breg1";
13668     case DW_OP_breg2:
13669       return "DW_OP_breg2";
13670     case DW_OP_breg3:
13671       return "DW_OP_breg3";
13672     case DW_OP_breg4:
13673       return "DW_OP_breg4";
13674     case DW_OP_breg5:
13675       return "DW_OP_breg5";
13676     case DW_OP_breg6:
13677       return "DW_OP_breg6";
13678     case DW_OP_breg7:
13679       return "DW_OP_breg7";
13680     case DW_OP_breg8:
13681       return "DW_OP_breg8";
13682     case DW_OP_breg9:
13683       return "DW_OP_breg9";
13684     case DW_OP_breg10:
13685       return "DW_OP_breg10";
13686     case DW_OP_breg11:
13687       return "DW_OP_breg11";
13688     case DW_OP_breg12:
13689       return "DW_OP_breg12";
13690     case DW_OP_breg13:
13691       return "DW_OP_breg13";
13692     case DW_OP_breg14:
13693       return "DW_OP_breg14";
13694     case DW_OP_breg15:
13695       return "DW_OP_breg15";
13696     case DW_OP_breg16:
13697       return "DW_OP_breg16";
13698     case DW_OP_breg17:
13699       return "DW_OP_breg17";
13700     case DW_OP_breg18:
13701       return "DW_OP_breg18";
13702     case DW_OP_breg19:
13703       return "DW_OP_breg19";
13704     case DW_OP_breg20:
13705       return "DW_OP_breg20";
13706     case DW_OP_breg21:
13707       return "DW_OP_breg21";
13708     case DW_OP_breg22:
13709       return "DW_OP_breg22";
13710     case DW_OP_breg23:
13711       return "DW_OP_breg23";
13712     case DW_OP_breg24:
13713       return "DW_OP_breg24";
13714     case DW_OP_breg25:
13715       return "DW_OP_breg25";
13716     case DW_OP_breg26:
13717       return "DW_OP_breg26";
13718     case DW_OP_breg27:
13719       return "DW_OP_breg27";
13720     case DW_OP_breg28:
13721       return "DW_OP_breg28";
13722     case DW_OP_breg29:
13723       return "DW_OP_breg29";
13724     case DW_OP_breg30:
13725       return "DW_OP_breg30";
13726     case DW_OP_breg31:
13727       return "DW_OP_breg31";
13728     case DW_OP_regx:
13729       return "DW_OP_regx";
13730     case DW_OP_fbreg:
13731       return "DW_OP_fbreg";
13732     case DW_OP_bregx:
13733       return "DW_OP_bregx";
13734     case DW_OP_piece:
13735       return "DW_OP_piece";
13736     case DW_OP_deref_size:
13737       return "DW_OP_deref_size";
13738     case DW_OP_xderef_size:
13739       return "DW_OP_xderef_size";
13740     case DW_OP_nop:
13741       return "DW_OP_nop";
13742     /* DWARF 3 extensions.  */
13743     case DW_OP_push_object_address:
13744       return "DW_OP_push_object_address";
13745     case DW_OP_call2:
13746       return "DW_OP_call2";
13747     case DW_OP_call4:
13748       return "DW_OP_call4";
13749     case DW_OP_call_ref:
13750       return "DW_OP_call_ref";
13751     case DW_OP_form_tls_address:
13752       return "DW_OP_form_tls_address";
13753     case DW_OP_call_frame_cfa:
13754       return "DW_OP_call_frame_cfa";
13755     case DW_OP_bit_piece:
13756       return "DW_OP_bit_piece";
13757     /* DWARF 4 extensions.  */
13758     case DW_OP_implicit_value:
13759       return "DW_OP_implicit_value";
13760     case DW_OP_stack_value:
13761       return "DW_OP_stack_value";
13762     /* GNU extensions.  */
13763     case DW_OP_GNU_push_tls_address:
13764       return "DW_OP_GNU_push_tls_address";
13765     case DW_OP_GNU_uninit:
13766       return "DW_OP_GNU_uninit";
13767     case DW_OP_GNU_encoded_addr:
13768       return "DW_OP_GNU_encoded_addr";
13769     case DW_OP_GNU_implicit_pointer:
13770       return "DW_OP_GNU_implicit_pointer";
13771     case DW_OP_GNU_entry_value:
13772       return "DW_OP_GNU_entry_value";
13773     case DW_OP_GNU_const_type:
13774       return "DW_OP_GNU_const_type";
13775     case DW_OP_GNU_regval_type:
13776       return "DW_OP_GNU_regval_type";
13777     case DW_OP_GNU_deref_type:
13778       return "DW_OP_GNU_deref_type";
13779     case DW_OP_GNU_convert:
13780       return "DW_OP_GNU_convert";
13781     case DW_OP_GNU_reinterpret:
13782       return "DW_OP_GNU_reinterpret";
13783     case DW_OP_GNU_parameter_ref:
13784       return "DW_OP_GNU_parameter_ref";
13785     default:
13786       return NULL;
13787     }
13788 }
13789
13790 static char *
13791 dwarf_bool_name (unsigned mybool)
13792 {
13793   if (mybool)
13794     return "TRUE";
13795   else
13796     return "FALSE";
13797 }
13798
13799 /* Convert a DWARF type code into its string name.  */
13800
13801 static char *
13802 dwarf_type_encoding_name (unsigned enc)
13803 {
13804   switch (enc)
13805     {
13806     case DW_ATE_void:
13807       return "DW_ATE_void";
13808     case DW_ATE_address:
13809       return "DW_ATE_address";
13810     case DW_ATE_boolean:
13811       return "DW_ATE_boolean";
13812     case DW_ATE_complex_float:
13813       return "DW_ATE_complex_float";
13814     case DW_ATE_float:
13815       return "DW_ATE_float";
13816     case DW_ATE_signed:
13817       return "DW_ATE_signed";
13818     case DW_ATE_signed_char:
13819       return "DW_ATE_signed_char";
13820     case DW_ATE_unsigned:
13821       return "DW_ATE_unsigned";
13822     case DW_ATE_unsigned_char:
13823       return "DW_ATE_unsigned_char";
13824     /* DWARF 3.  */
13825     case DW_ATE_imaginary_float:
13826       return "DW_ATE_imaginary_float";
13827     case DW_ATE_packed_decimal:
13828       return "DW_ATE_packed_decimal";
13829     case DW_ATE_numeric_string:
13830       return "DW_ATE_numeric_string";
13831     case DW_ATE_edited:
13832       return "DW_ATE_edited";
13833     case DW_ATE_signed_fixed:
13834       return "DW_ATE_signed_fixed";
13835     case DW_ATE_unsigned_fixed:
13836       return "DW_ATE_unsigned_fixed";
13837     case DW_ATE_decimal_float:
13838       return "DW_ATE_decimal_float";
13839     /* DWARF 4.  */
13840     case DW_ATE_UTF:
13841       return "DW_ATE_UTF";
13842     /* HP extensions.  */
13843     case DW_ATE_HP_float80:
13844       return "DW_ATE_HP_float80";
13845     case DW_ATE_HP_complex_float80:
13846       return "DW_ATE_HP_complex_float80";
13847     case DW_ATE_HP_float128:
13848       return "DW_ATE_HP_float128";
13849     case DW_ATE_HP_complex_float128:
13850       return "DW_ATE_HP_complex_float128";
13851     case DW_ATE_HP_floathpintel:
13852       return "DW_ATE_HP_floathpintel";
13853     case DW_ATE_HP_imaginary_float80:
13854       return "DW_ATE_HP_imaginary_float80";
13855     case DW_ATE_HP_imaginary_float128:
13856       return "DW_ATE_HP_imaginary_float128";
13857     default:
13858       return "DW_ATE_<unknown>";
13859     }
13860 }
13861
13862 /* Convert a DWARF call frame info operation to its string name.  */
13863
13864 #if 0
13865 static char *
13866 dwarf_cfi_name (unsigned cfi_opc)
13867 {
13868   switch (cfi_opc)
13869     {
13870     case DW_CFA_advance_loc:
13871       return "DW_CFA_advance_loc";
13872     case DW_CFA_offset:
13873       return "DW_CFA_offset";
13874     case DW_CFA_restore:
13875       return "DW_CFA_restore";
13876     case DW_CFA_nop:
13877       return "DW_CFA_nop";
13878     case DW_CFA_set_loc:
13879       return "DW_CFA_set_loc";
13880     case DW_CFA_advance_loc1:
13881       return "DW_CFA_advance_loc1";
13882     case DW_CFA_advance_loc2:
13883       return "DW_CFA_advance_loc2";
13884     case DW_CFA_advance_loc4:
13885       return "DW_CFA_advance_loc4";
13886     case DW_CFA_offset_extended:
13887       return "DW_CFA_offset_extended";
13888     case DW_CFA_restore_extended:
13889       return "DW_CFA_restore_extended";
13890     case DW_CFA_undefined:
13891       return "DW_CFA_undefined";
13892     case DW_CFA_same_value:
13893       return "DW_CFA_same_value";
13894     case DW_CFA_register:
13895       return "DW_CFA_register";
13896     case DW_CFA_remember_state:
13897       return "DW_CFA_remember_state";
13898     case DW_CFA_restore_state:
13899       return "DW_CFA_restore_state";
13900     case DW_CFA_def_cfa:
13901       return "DW_CFA_def_cfa";
13902     case DW_CFA_def_cfa_register:
13903       return "DW_CFA_def_cfa_register";
13904     case DW_CFA_def_cfa_offset:
13905       return "DW_CFA_def_cfa_offset";
13906     /* DWARF 3.  */
13907     case DW_CFA_def_cfa_expression:
13908       return "DW_CFA_def_cfa_expression";
13909     case DW_CFA_expression:
13910       return "DW_CFA_expression";
13911     case DW_CFA_offset_extended_sf:
13912       return "DW_CFA_offset_extended_sf";
13913     case DW_CFA_def_cfa_sf:
13914       return "DW_CFA_def_cfa_sf";
13915     case DW_CFA_def_cfa_offset_sf:
13916       return "DW_CFA_def_cfa_offset_sf";
13917     case DW_CFA_val_offset:
13918       return "DW_CFA_val_offset";
13919     case DW_CFA_val_offset_sf:
13920       return "DW_CFA_val_offset_sf";
13921     case DW_CFA_val_expression:
13922       return "DW_CFA_val_expression";
13923     /* SGI/MIPS specific.  */
13924     case DW_CFA_MIPS_advance_loc8:
13925       return "DW_CFA_MIPS_advance_loc8";
13926     /* GNU extensions.  */
13927     case DW_CFA_GNU_window_save:
13928       return "DW_CFA_GNU_window_save";
13929     case DW_CFA_GNU_args_size:
13930       return "DW_CFA_GNU_args_size";
13931     case DW_CFA_GNU_negative_offset_extended:
13932       return "DW_CFA_GNU_negative_offset_extended";
13933     default:
13934       return "DW_CFA_<unknown>";
13935     }
13936 }
13937 #endif
13938
13939 static void
13940 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13941 {
13942   unsigned int i;
13943
13944   print_spaces (indent, f);
13945   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13946            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
13947
13948   if (die->parent != NULL)
13949     {
13950       print_spaces (indent, f);
13951       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13952                           die->parent->offset.sect_off);
13953     }
13954
13955   print_spaces (indent, f);
13956   fprintf_unfiltered (f, "  has children: %s\n",
13957            dwarf_bool_name (die->child != NULL));
13958
13959   print_spaces (indent, f);
13960   fprintf_unfiltered (f, "  attributes:\n");
13961
13962   for (i = 0; i < die->num_attrs; ++i)
13963     {
13964       print_spaces (indent, f);
13965       fprintf_unfiltered (f, "    %s (%s) ",
13966                dwarf_attr_name (die->attrs[i].name),
13967                dwarf_form_name (die->attrs[i].form));
13968
13969       switch (die->attrs[i].form)
13970         {
13971         case DW_FORM_ref_addr:
13972         case DW_FORM_addr:
13973           fprintf_unfiltered (f, "address: ");
13974           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13975           break;
13976         case DW_FORM_block2:
13977         case DW_FORM_block4:
13978         case DW_FORM_block:
13979         case DW_FORM_block1:
13980           fprintf_unfiltered (f, "block: size %d",
13981                               DW_BLOCK (&die->attrs[i])->size);
13982           break;
13983         case DW_FORM_exprloc:
13984           fprintf_unfiltered (f, "expression: size %u",
13985                               DW_BLOCK (&die->attrs[i])->size);
13986           break;
13987         case DW_FORM_ref1:
13988         case DW_FORM_ref2:
13989         case DW_FORM_ref4:
13990           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13991                               (long) (DW_ADDR (&die->attrs[i])));
13992           break;
13993         case DW_FORM_data1:
13994         case DW_FORM_data2:
13995         case DW_FORM_data4:
13996         case DW_FORM_data8:
13997         case DW_FORM_udata:
13998         case DW_FORM_sdata:
13999           fprintf_unfiltered (f, "constant: %s",
14000                               pulongest (DW_UNSND (&die->attrs[i])));
14001           break;
14002         case DW_FORM_sec_offset:
14003           fprintf_unfiltered (f, "section offset: %s",
14004                               pulongest (DW_UNSND (&die->attrs[i])));
14005           break;
14006         case DW_FORM_ref_sig8:
14007           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14008             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14009                          DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14010           else
14011             fprintf_unfiltered (f, "signatured type, offset: unknown");
14012           break;
14013         case DW_FORM_string:
14014         case DW_FORM_strp:
14015           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14016                    DW_STRING (&die->attrs[i])
14017                    ? DW_STRING (&die->attrs[i]) : "",
14018                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14019           break;
14020         case DW_FORM_flag:
14021           if (DW_UNSND (&die->attrs[i]))
14022             fprintf_unfiltered (f, "flag: TRUE");
14023           else
14024             fprintf_unfiltered (f, "flag: FALSE");
14025           break;
14026         case DW_FORM_flag_present:
14027           fprintf_unfiltered (f, "flag: TRUE");
14028           break;
14029         case DW_FORM_indirect:
14030           /* The reader will have reduced the indirect form to
14031              the "base form" so this form should not occur.  */
14032           fprintf_unfiltered (f, 
14033                               "unexpected attribute form: DW_FORM_indirect");
14034           break;
14035         default:
14036           fprintf_unfiltered (f, "unsupported attribute form: %d.",
14037                    die->attrs[i].form);
14038           break;
14039         }
14040       fprintf_unfiltered (f, "\n");
14041     }
14042 }
14043
14044 static void
14045 dump_die_for_error (struct die_info *die)
14046 {
14047   dump_die_shallow (gdb_stderr, 0, die);
14048 }
14049
14050 static void
14051 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14052 {
14053   int indent = level * 4;
14054
14055   gdb_assert (die != NULL);
14056
14057   if (level >= max_level)
14058     return;
14059
14060   dump_die_shallow (f, indent, die);
14061
14062   if (die->child != NULL)
14063     {
14064       print_spaces (indent, f);
14065       fprintf_unfiltered (f, "  Children:");
14066       if (level + 1 < max_level)
14067         {
14068           fprintf_unfiltered (f, "\n");
14069           dump_die_1 (f, level + 1, max_level, die->child);
14070         }
14071       else
14072         {
14073           fprintf_unfiltered (f,
14074                               " [not printed, max nesting level reached]\n");
14075         }
14076     }
14077
14078   if (die->sibling != NULL && level > 0)
14079     {
14080       dump_die_1 (f, level, max_level, die->sibling);
14081     }
14082 }
14083
14084 /* This is called from the pdie macro in gdbinit.in.
14085    It's not static so gcc will keep a copy callable from gdb.  */
14086
14087 void
14088 dump_die (struct die_info *die, int max_level)
14089 {
14090   dump_die_1 (gdb_stdlog, 0, max_level, die);
14091 }
14092
14093 static void
14094 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14095 {
14096   void **slot;
14097
14098   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14099                                    INSERT);
14100
14101   *slot = die;
14102 }
14103
14104 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14105    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
14106
14107 static int
14108 is_ref_attr (struct attribute *attr)
14109 {
14110   switch (attr->form)
14111     {
14112     case DW_FORM_ref_addr:
14113     case DW_FORM_ref1:
14114     case DW_FORM_ref2:
14115     case DW_FORM_ref4:
14116     case DW_FORM_ref8:
14117     case DW_FORM_ref_udata:
14118       return 1;
14119     default:
14120       return 0;
14121     }
14122 }
14123
14124 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
14125    required kind.  */
14126
14127 static sect_offset
14128 dwarf2_get_ref_die_offset (struct attribute *attr)
14129 {
14130   sect_offset retval = { DW_ADDR (attr) };
14131
14132   if (is_ref_attr (attr))
14133     return retval;
14134
14135   retval.sect_off = 0;
14136   complaint (&symfile_complaints,
14137              _("unsupported die ref attribute form: '%s'"),
14138              dwarf_form_name (attr->form));
14139   return retval;
14140 }
14141
14142 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14143  * the value held by the attribute is not constant.  */
14144
14145 static LONGEST
14146 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14147 {
14148   if (attr->form == DW_FORM_sdata)
14149     return DW_SND (attr);
14150   else if (attr->form == DW_FORM_udata
14151            || attr->form == DW_FORM_data1
14152            || attr->form == DW_FORM_data2
14153            || attr->form == DW_FORM_data4
14154            || attr->form == DW_FORM_data8)
14155     return DW_UNSND (attr);
14156   else
14157     {
14158       complaint (&symfile_complaints,
14159                  _("Attribute value is not a constant (%s)"),
14160                  dwarf_form_name (attr->form));
14161       return default_value;
14162     }
14163 }
14164
14165 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14166    unit and add it to our queue.
14167    The result is non-zero if PER_CU was queued, otherwise the result is zero
14168    meaning either PER_CU is already queued or it is already loaded.  */
14169
14170 static int
14171 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14172                        struct dwarf2_per_cu_data *per_cu)
14173 {
14174   /* We may arrive here during partial symbol reading, if we need full
14175      DIEs to process an unusual case (e.g. template arguments).  Do
14176      not queue PER_CU, just tell our caller to load its DIEs.  */
14177   if (dwarf2_per_objfile->reading_partial_symbols)
14178     {
14179       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14180         return 1;
14181       return 0;
14182     }
14183
14184   /* Mark the dependence relation so that we don't flush PER_CU
14185      too early.  */
14186   dwarf2_add_dependence (this_cu, per_cu);
14187
14188   /* If it's already on the queue, we have nothing to do.  */
14189   if (per_cu->queued)
14190     return 0;
14191
14192   /* If the compilation unit is already loaded, just mark it as
14193      used.  */
14194   if (per_cu->cu != NULL)
14195     {
14196       per_cu->cu->last_used = 0;
14197       return 0;
14198     }
14199
14200   /* Add it to the queue.  */
14201   queue_comp_unit (per_cu);
14202
14203   return 1;
14204 }
14205
14206 /* Follow reference or signature attribute ATTR of SRC_DIE.
14207    On entry *REF_CU is the CU of SRC_DIE.
14208    On exit *REF_CU is the CU of the result.  */
14209
14210 static struct die_info *
14211 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14212                        struct dwarf2_cu **ref_cu)
14213 {
14214   struct die_info *die;
14215
14216   if (is_ref_attr (attr))
14217     die = follow_die_ref (src_die, attr, ref_cu);
14218   else if (attr->form == DW_FORM_ref_sig8)
14219     die = follow_die_sig (src_die, attr, ref_cu);
14220   else
14221     {
14222       dump_die_for_error (src_die);
14223       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14224              (*ref_cu)->objfile->name);
14225     }
14226
14227   return die;
14228 }
14229
14230 /* Follow reference OFFSET.
14231    On entry *REF_CU is the CU of the source die referencing OFFSET.
14232    On exit *REF_CU is the CU of the result.
14233    Returns NULL if OFFSET is invalid.  */
14234
14235 static struct die_info *
14236 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14237 {
14238   struct die_info temp_die;
14239   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14240
14241   gdb_assert (cu->per_cu != NULL);
14242
14243   target_cu = cu;
14244
14245   if (cu->per_cu->debug_types_section)
14246     {
14247       /* .debug_types CUs cannot reference anything outside their CU.
14248          If they need to, they have to reference a signatured type via
14249          DW_FORM_ref_sig8.  */
14250       if (! offset_in_cu_p (&cu->header, offset))
14251         return NULL;
14252     }
14253   else if (! offset_in_cu_p (&cu->header, offset))
14254     {
14255       struct dwarf2_per_cu_data *per_cu;
14256
14257       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14258
14259       /* If necessary, add it to the queue and load its DIEs.  */
14260       if (maybe_queue_comp_unit (cu, per_cu))
14261         load_full_comp_unit (per_cu);
14262
14263       target_cu = per_cu->cu;
14264     }
14265   else if (cu->dies == NULL)
14266     {
14267       /* We're loading full DIEs during partial symbol reading.  */
14268       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14269       load_full_comp_unit (cu->per_cu);
14270     }
14271
14272   *ref_cu = target_cu;
14273   temp_die.offset = offset;
14274   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14275 }
14276
14277 /* Follow reference attribute ATTR of SRC_DIE.
14278    On entry *REF_CU is the CU of SRC_DIE.
14279    On exit *REF_CU is the CU of the result.  */
14280
14281 static struct die_info *
14282 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14283                 struct dwarf2_cu **ref_cu)
14284 {
14285   sect_offset offset = dwarf2_get_ref_die_offset (attr);
14286   struct dwarf2_cu *cu = *ref_cu;
14287   struct die_info *die;
14288
14289   die = follow_die_offset (offset, ref_cu);
14290   if (!die)
14291     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14292            "at 0x%x [in module %s]"),
14293            offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14294
14295   return die;
14296 }
14297
14298 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14299    Returned value is intended for DW_OP_call*.  Returned
14300    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14301
14302 struct dwarf2_locexpr_baton
14303 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14304                                  struct dwarf2_per_cu_data *per_cu,
14305                                  CORE_ADDR (*get_frame_pc) (void *baton),
14306                                  void *baton)
14307 {
14308   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14309   struct dwarf2_cu *cu;
14310   struct die_info *die;
14311   struct attribute *attr;
14312   struct dwarf2_locexpr_baton retval;
14313
14314   dw2_setup (per_cu->objfile);
14315
14316   if (per_cu->cu == NULL)
14317     load_cu (per_cu);
14318   cu = per_cu->cu;
14319
14320   die = follow_die_offset (offset, &cu);
14321   if (!die)
14322     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14323            offset.sect_off, per_cu->objfile->name);
14324
14325   attr = dwarf2_attr (die, DW_AT_location, cu);
14326   if (!attr)
14327     {
14328       /* DWARF: "If there is no such attribute, then there is no effect.".
14329          DATA is ignored if SIZE is 0.  */
14330
14331       retval.data = NULL;
14332       retval.size = 0;
14333     }
14334   else if (attr_form_is_section_offset (attr))
14335     {
14336       struct dwarf2_loclist_baton loclist_baton;
14337       CORE_ADDR pc = (*get_frame_pc) (baton);
14338       size_t size;
14339
14340       fill_in_loclist_baton (cu, &loclist_baton, attr);
14341
14342       retval.data = dwarf2_find_location_expression (&loclist_baton,
14343                                                      &size, pc);
14344       retval.size = size;
14345     }
14346   else
14347     {
14348       if (!attr_form_is_block (attr))
14349         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14350                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14351                offset.sect_off, per_cu->objfile->name);
14352
14353       retval.data = DW_BLOCK (attr)->data;
14354       retval.size = DW_BLOCK (attr)->size;
14355     }
14356   retval.per_cu = cu->per_cu;
14357
14358   age_cached_comp_units ();
14359
14360   return retval;
14361 }
14362
14363 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14364    PER_CU.  */
14365
14366 struct type *
14367 dwarf2_get_die_type (cu_offset die_offset,
14368                      struct dwarf2_per_cu_data *per_cu)
14369 {
14370   sect_offset die_offset_sect;
14371
14372   dw2_setup (per_cu->objfile);
14373
14374   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14375   return get_die_type_at_offset (die_offset_sect, per_cu);
14376 }
14377
14378 /* Follow the signature attribute ATTR in SRC_DIE.
14379    On entry *REF_CU is the CU of SRC_DIE.
14380    On exit *REF_CU is the CU of the result.  */
14381
14382 static struct die_info *
14383 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14384                 struct dwarf2_cu **ref_cu)
14385 {
14386   struct objfile *objfile = (*ref_cu)->objfile;
14387   struct die_info temp_die;
14388   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14389   struct dwarf2_cu *sig_cu;
14390   struct die_info *die;
14391
14392   /* sig_type will be NULL if the signatured type is missing from
14393      the debug info.  */
14394   if (sig_type == NULL)
14395     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14396              "at 0x%x [in module %s]"),
14397            src_die->offset.sect_off, objfile->name);
14398
14399   /* If necessary, add it to the queue and load its DIEs.  */
14400
14401   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14402     read_signatured_type (sig_type);
14403
14404   gdb_assert (sig_type->per_cu.cu != NULL);
14405
14406   sig_cu = sig_type->per_cu.cu;
14407   temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14408                               + sig_type->type_offset.cu_off);
14409   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14410                              temp_die.offset.sect_off);
14411   if (die)
14412     {
14413       *ref_cu = sig_cu;
14414       return die;
14415     }
14416
14417   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14418          "from DIE at 0x%x [in module %s]"),
14419          temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
14420 }
14421
14422 /* Given an offset of a signatured type, return its signatured_type.  */
14423
14424 static struct signatured_type *
14425 lookup_signatured_type_at_offset (struct objfile *objfile,
14426                                   struct dwarf2_section_info *section,
14427                                   sect_offset offset)
14428 {
14429   gdb_byte *info_ptr = section->buffer + offset.sect_off;
14430   unsigned int length, initial_length_size;
14431   unsigned int sig_offset;
14432   struct signatured_type find_entry, *sig_type;
14433
14434   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14435   sig_offset = (initial_length_size
14436                 + 2 /*version*/
14437                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14438                 + 1 /*address_size*/);
14439   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14440   sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14441
14442   /* This is only used to lookup previously recorded types.
14443      If we didn't find it, it's our bug.  */
14444   gdb_assert (sig_type != NULL);
14445   gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
14446
14447   return sig_type;
14448 }
14449
14450 /* Load the DIEs associated with type unit PER_CU into memory.  */
14451
14452 static void
14453 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14454 {
14455   struct objfile *objfile = per_cu->objfile;
14456   struct dwarf2_section_info *sect = per_cu->debug_types_section;
14457   sect_offset offset = per_cu->offset;
14458   struct signatured_type *sig_type;
14459
14460   dwarf2_read_section (objfile, sect);
14461
14462   /* We have the section offset, but we need the signature to do the
14463      hash table lookup.  */
14464   /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14465      the signature to assert we found the right one.
14466      Ok, but it's a lot of work.  We should simplify things so any needed
14467      assert doesn't require all this clumsiness.  */
14468   sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
14469
14470   gdb_assert (sig_type->per_cu.cu == NULL);
14471
14472   read_signatured_type (sig_type);
14473
14474   gdb_assert (sig_type->per_cu.cu != NULL);
14475 }
14476
14477 /* Read in a signatured type and build its CU and DIEs.  */
14478
14479 static void
14480 read_signatured_type (struct signatured_type *sig_type)
14481 {
14482   struct objfile *objfile = sig_type->per_cu.objfile;
14483   gdb_byte *types_ptr;
14484   struct die_reader_specs reader_specs;
14485   struct dwarf2_cu *cu;
14486   ULONGEST signature;
14487   struct cleanup *back_to, *free_cu_cleanup;
14488   struct dwarf2_section_info *section = sig_type->per_cu.debug_types_section;
14489
14490   dwarf2_read_section (objfile, section);
14491   types_ptr = section->buffer + sig_type->per_cu.offset.sect_off;
14492
14493   gdb_assert (sig_type->per_cu.cu == NULL);
14494
14495   cu = xmalloc (sizeof (*cu));
14496   init_one_comp_unit (cu, &sig_type->per_cu);
14497
14498   /* If an error occurs while loading, release our storage.  */
14499   free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14500
14501   types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14502                                              &signature, NULL);
14503   gdb_assert (signature == sig_type->signature);
14504
14505   cu->die_hash
14506     = htab_create_alloc_ex (cu->header.length / 12,
14507                             die_hash,
14508                             die_eq,
14509                             NULL,
14510                             &cu->comp_unit_obstack,
14511                             hashtab_obstack_allocate,
14512                             dummy_obstack_deallocate);
14513
14514   dwarf2_read_abbrevs (cu);
14515   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14516
14517   init_cu_die_reader (&reader_specs, cu);
14518
14519   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14520                                     NULL /*parent*/);
14521
14522   /* We try not to read any attributes in this function, because not
14523      all CUs needed for references have been loaded yet, and symbol
14524      table processing isn't initialized.  But we have to set the CU language,
14525      or we won't be able to build types correctly.  */
14526   prepare_one_comp_unit (cu, cu->dies);
14527
14528   do_cleanups (back_to);
14529
14530   /* We've successfully allocated this compilation unit.  Let our caller
14531      clean it up when finished with it.  */
14532   discard_cleanups (free_cu_cleanup);
14533
14534   /* Link this TU into read_in_chain.  */
14535   sig_type->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14536   dwarf2_per_objfile->read_in_chain = &sig_type->per_cu;
14537 }
14538
14539 /* Decode simple location descriptions.
14540    Given a pointer to a dwarf block that defines a location, compute
14541    the location and return the value.
14542
14543    NOTE drow/2003-11-18: This function is called in two situations
14544    now: for the address of static or global variables (partial symbols
14545    only) and for offsets into structures which are expected to be
14546    (more or less) constant.  The partial symbol case should go away,
14547    and only the constant case should remain.  That will let this
14548    function complain more accurately.  A few special modes are allowed
14549    without complaint for global variables (for instance, global
14550    register values and thread-local values).
14551
14552    A location description containing no operations indicates that the
14553    object is optimized out.  The return value is 0 for that case.
14554    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14555    callers will only want a very basic result and this can become a
14556    complaint.
14557
14558    Note that stack[0] is unused except as a default error return.  */
14559
14560 static CORE_ADDR
14561 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14562 {
14563   struct objfile *objfile = cu->objfile;
14564   int i;
14565   int size = blk->size;
14566   gdb_byte *data = blk->data;
14567   CORE_ADDR stack[64];
14568   int stacki;
14569   unsigned int bytes_read, unsnd;
14570   gdb_byte op;
14571
14572   i = 0;
14573   stacki = 0;
14574   stack[stacki] = 0;
14575   stack[++stacki] = 0;
14576
14577   while (i < size)
14578     {
14579       op = data[i++];
14580       switch (op)
14581         {
14582         case DW_OP_lit0:
14583         case DW_OP_lit1:
14584         case DW_OP_lit2:
14585         case DW_OP_lit3:
14586         case DW_OP_lit4:
14587         case DW_OP_lit5:
14588         case DW_OP_lit6:
14589         case DW_OP_lit7:
14590         case DW_OP_lit8:
14591         case DW_OP_lit9:
14592         case DW_OP_lit10:
14593         case DW_OP_lit11:
14594         case DW_OP_lit12:
14595         case DW_OP_lit13:
14596         case DW_OP_lit14:
14597         case DW_OP_lit15:
14598         case DW_OP_lit16:
14599         case DW_OP_lit17:
14600         case DW_OP_lit18:
14601         case DW_OP_lit19:
14602         case DW_OP_lit20:
14603         case DW_OP_lit21:
14604         case DW_OP_lit22:
14605         case DW_OP_lit23:
14606         case DW_OP_lit24:
14607         case DW_OP_lit25:
14608         case DW_OP_lit26:
14609         case DW_OP_lit27:
14610         case DW_OP_lit28:
14611         case DW_OP_lit29:
14612         case DW_OP_lit30:
14613         case DW_OP_lit31:
14614           stack[++stacki] = op - DW_OP_lit0;
14615           break;
14616
14617         case DW_OP_reg0:
14618         case DW_OP_reg1:
14619         case DW_OP_reg2:
14620         case DW_OP_reg3:
14621         case DW_OP_reg4:
14622         case DW_OP_reg5:
14623         case DW_OP_reg6:
14624         case DW_OP_reg7:
14625         case DW_OP_reg8:
14626         case DW_OP_reg9:
14627         case DW_OP_reg10:
14628         case DW_OP_reg11:
14629         case DW_OP_reg12:
14630         case DW_OP_reg13:
14631         case DW_OP_reg14:
14632         case DW_OP_reg15:
14633         case DW_OP_reg16:
14634         case DW_OP_reg17:
14635         case DW_OP_reg18:
14636         case DW_OP_reg19:
14637         case DW_OP_reg20:
14638         case DW_OP_reg21:
14639         case DW_OP_reg22:
14640         case DW_OP_reg23:
14641         case DW_OP_reg24:
14642         case DW_OP_reg25:
14643         case DW_OP_reg26:
14644         case DW_OP_reg27:
14645         case DW_OP_reg28:
14646         case DW_OP_reg29:
14647         case DW_OP_reg30:
14648         case DW_OP_reg31:
14649           stack[++stacki] = op - DW_OP_reg0;
14650           if (i < size)
14651             dwarf2_complex_location_expr_complaint ();
14652           break;
14653
14654         case DW_OP_regx:
14655           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14656           i += bytes_read;
14657           stack[++stacki] = unsnd;
14658           if (i < size)
14659             dwarf2_complex_location_expr_complaint ();
14660           break;
14661
14662         case DW_OP_addr:
14663           stack[++stacki] = read_address (objfile->obfd, &data[i],
14664                                           cu, &bytes_read);
14665           i += bytes_read;
14666           break;
14667
14668         case DW_OP_const1u:
14669           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14670           i += 1;
14671           break;
14672
14673         case DW_OP_const1s:
14674           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14675           i += 1;
14676           break;
14677
14678         case DW_OP_const2u:
14679           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14680           i += 2;
14681           break;
14682
14683         case DW_OP_const2s:
14684           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14685           i += 2;
14686           break;
14687
14688         case DW_OP_const4u:
14689           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14690           i += 4;
14691           break;
14692
14693         case DW_OP_const4s:
14694           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14695           i += 4;
14696           break;
14697
14698         case DW_OP_const8u:
14699           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14700           i += 8;
14701           break;
14702
14703         case DW_OP_constu:
14704           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14705                                                   &bytes_read);
14706           i += bytes_read;
14707           break;
14708
14709         case DW_OP_consts:
14710           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14711           i += bytes_read;
14712           break;
14713
14714         case DW_OP_dup:
14715           stack[stacki + 1] = stack[stacki];
14716           stacki++;
14717           break;
14718
14719         case DW_OP_plus:
14720           stack[stacki - 1] += stack[stacki];
14721           stacki--;
14722           break;
14723
14724         case DW_OP_plus_uconst:
14725           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14726                                                  &bytes_read);
14727           i += bytes_read;
14728           break;
14729
14730         case DW_OP_minus:
14731           stack[stacki - 1] -= stack[stacki];
14732           stacki--;
14733           break;
14734
14735         case DW_OP_deref:
14736           /* If we're not the last op, then we definitely can't encode
14737              this using GDB's address_class enum.  This is valid for partial
14738              global symbols, although the variable's address will be bogus
14739              in the psymtab.  */
14740           if (i < size)
14741             dwarf2_complex_location_expr_complaint ();
14742           break;
14743
14744         case DW_OP_GNU_push_tls_address:
14745           /* The top of the stack has the offset from the beginning
14746              of the thread control block at which the variable is located.  */
14747           /* Nothing should follow this operator, so the top of stack would
14748              be returned.  */
14749           /* This is valid for partial global symbols, but the variable's
14750              address will be bogus in the psymtab.  Make it always at least
14751              non-zero to not look as a variable garbage collected by linker
14752              which have DW_OP_addr 0.  */
14753           if (i < size)
14754             dwarf2_complex_location_expr_complaint ();
14755           stack[stacki]++;
14756           break;
14757
14758         case DW_OP_GNU_uninit:
14759           break;
14760
14761         default:
14762           {
14763             const char *name = dwarf_stack_op_name (op);
14764
14765             if (name)
14766               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14767                          name);
14768             else
14769               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14770                          op);
14771           }
14772
14773           return (stack[stacki]);
14774         }
14775
14776       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14777          outside of the allocated space.  Also enforce minimum>0.  */
14778       if (stacki >= ARRAY_SIZE (stack) - 1)
14779         {
14780           complaint (&symfile_complaints,
14781                      _("location description stack overflow"));
14782           return 0;
14783         }
14784
14785       if (stacki <= 0)
14786         {
14787           complaint (&symfile_complaints,
14788                      _("location description stack underflow"));
14789           return 0;
14790         }
14791     }
14792   return (stack[stacki]);
14793 }
14794
14795 /* memory allocation interface */
14796
14797 static struct dwarf_block *
14798 dwarf_alloc_block (struct dwarf2_cu *cu)
14799 {
14800   struct dwarf_block *blk;
14801
14802   blk = (struct dwarf_block *)
14803     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14804   return (blk);
14805 }
14806
14807 static struct abbrev_info *
14808 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14809 {
14810   struct abbrev_info *abbrev;
14811
14812   abbrev = (struct abbrev_info *)
14813     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14814   memset (abbrev, 0, sizeof (struct abbrev_info));
14815   return (abbrev);
14816 }
14817
14818 static struct die_info *
14819 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14820 {
14821   struct die_info *die;
14822   size_t size = sizeof (struct die_info);
14823
14824   if (num_attrs > 1)
14825     size += (num_attrs - 1) * sizeof (struct attribute);
14826
14827   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14828   memset (die, 0, sizeof (struct die_info));
14829   return (die);
14830 }
14831
14832 \f
14833 /* Macro support.  */
14834
14835 /* Return the full name of file number I in *LH's file name table.
14836    Use COMP_DIR as the name of the current directory of the
14837    compilation.  The result is allocated using xmalloc; the caller is
14838    responsible for freeing it.  */
14839 static char *
14840 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14841 {
14842   /* Is the file number a valid index into the line header's file name
14843      table?  Remember that file numbers start with one, not zero.  */
14844   if (1 <= file && file <= lh->num_file_names)
14845     {
14846       struct file_entry *fe = &lh->file_names[file - 1];
14847
14848       if (IS_ABSOLUTE_PATH (fe->name))
14849         return xstrdup (fe->name);
14850       else
14851         {
14852           const char *dir;
14853           int dir_len;
14854           char *full_name;
14855
14856           if (fe->dir_index)
14857             dir = lh->include_dirs[fe->dir_index - 1];
14858           else
14859             dir = comp_dir;
14860
14861           if (dir)
14862             {
14863               dir_len = strlen (dir);
14864               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14865               strcpy (full_name, dir);
14866               full_name[dir_len] = '/';
14867               strcpy (full_name + dir_len + 1, fe->name);
14868               return full_name;
14869             }
14870           else
14871             return xstrdup (fe->name);
14872         }
14873     }
14874   else
14875     {
14876       /* The compiler produced a bogus file number.  We can at least
14877          record the macro definitions made in the file, even if we
14878          won't be able to find the file by name.  */
14879       char fake_name[80];
14880
14881       sprintf (fake_name, "<bad macro file number %d>", file);
14882
14883       complaint (&symfile_complaints,
14884                  _("bad file number in macro information (%d)"),
14885                  file);
14886
14887       return xstrdup (fake_name);
14888     }
14889 }
14890
14891
14892 static struct macro_source_file *
14893 macro_start_file (int file, int line,
14894                   struct macro_source_file *current_file,
14895                   const char *comp_dir,
14896                   struct line_header *lh, struct objfile *objfile)
14897 {
14898   /* The full name of this source file.  */
14899   char *full_name = file_full_name (file, lh, comp_dir);
14900
14901   /* We don't create a macro table for this compilation unit
14902      at all until we actually get a filename.  */
14903   if (! pending_macros)
14904     pending_macros = new_macro_table (&objfile->objfile_obstack,
14905                                       objfile->macro_cache);
14906
14907   if (! current_file)
14908     /* If we have no current file, then this must be the start_file
14909        directive for the compilation unit's main source file.  */
14910     current_file = macro_set_main (pending_macros, full_name);
14911   else
14912     current_file = macro_include (current_file, line, full_name);
14913
14914   xfree (full_name);
14915
14916   return current_file;
14917 }
14918
14919
14920 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14921    followed by a null byte.  */
14922 static char *
14923 copy_string (const char *buf, int len)
14924 {
14925   char *s = xmalloc (len + 1);
14926
14927   memcpy (s, buf, len);
14928   s[len] = '\0';
14929   return s;
14930 }
14931
14932
14933 static const char *
14934 consume_improper_spaces (const char *p, const char *body)
14935 {
14936   if (*p == ' ')
14937     {
14938       complaint (&symfile_complaints,
14939                  _("macro definition contains spaces "
14940                    "in formal argument list:\n`%s'"),
14941                  body);
14942
14943       while (*p == ' ')
14944         p++;
14945     }
14946
14947   return p;
14948 }
14949
14950
14951 static void
14952 parse_macro_definition (struct macro_source_file *file, int line,
14953                         const char *body)
14954 {
14955   const char *p;
14956
14957   /* The body string takes one of two forms.  For object-like macro
14958      definitions, it should be:
14959
14960         <macro name> " " <definition>
14961
14962      For function-like macro definitions, it should be:
14963
14964         <macro name> "() " <definition>
14965      or
14966         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14967
14968      Spaces may appear only where explicitly indicated, and in the
14969      <definition>.
14970
14971      The Dwarf 2 spec says that an object-like macro's name is always
14972      followed by a space, but versions of GCC around March 2002 omit
14973      the space when the macro's definition is the empty string.
14974
14975      The Dwarf 2 spec says that there should be no spaces between the
14976      formal arguments in a function-like macro's formal argument list,
14977      but versions of GCC around March 2002 include spaces after the
14978      commas.  */
14979
14980
14981   /* Find the extent of the macro name.  The macro name is terminated
14982      by either a space or null character (for an object-like macro) or
14983      an opening paren (for a function-like macro).  */
14984   for (p = body; *p; p++)
14985     if (*p == ' ' || *p == '(')
14986       break;
14987
14988   if (*p == ' ' || *p == '\0')
14989     {
14990       /* It's an object-like macro.  */
14991       int name_len = p - body;
14992       char *name = copy_string (body, name_len);
14993       const char *replacement;
14994
14995       if (*p == ' ')
14996         replacement = body + name_len + 1;
14997       else
14998         {
14999           dwarf2_macro_malformed_definition_complaint (body);
15000           replacement = body + name_len;
15001         }
15002
15003       macro_define_object (file, line, name, replacement);
15004
15005       xfree (name);
15006     }
15007   else if (*p == '(')
15008     {
15009       /* It's a function-like macro.  */
15010       char *name = copy_string (body, p - body);
15011       int argc = 0;
15012       int argv_size = 1;
15013       char **argv = xmalloc (argv_size * sizeof (*argv));
15014
15015       p++;
15016
15017       p = consume_improper_spaces (p, body);
15018
15019       /* Parse the formal argument list.  */
15020       while (*p && *p != ')')
15021         {
15022           /* Find the extent of the current argument name.  */
15023           const char *arg_start = p;
15024
15025           while (*p && *p != ',' && *p != ')' && *p != ' ')
15026             p++;
15027
15028           if (! *p || p == arg_start)
15029             dwarf2_macro_malformed_definition_complaint (body);
15030           else
15031             {
15032               /* Make sure argv has room for the new argument.  */
15033               if (argc >= argv_size)
15034                 {
15035                   argv_size *= 2;
15036                   argv = xrealloc (argv, argv_size * sizeof (*argv));
15037                 }
15038
15039               argv[argc++] = copy_string (arg_start, p - arg_start);
15040             }
15041
15042           p = consume_improper_spaces (p, body);
15043
15044           /* Consume the comma, if present.  */
15045           if (*p == ',')
15046             {
15047               p++;
15048
15049               p = consume_improper_spaces (p, body);
15050             }
15051         }
15052
15053       if (*p == ')')
15054         {
15055           p++;
15056
15057           if (*p == ' ')
15058             /* Perfectly formed definition, no complaints.  */
15059             macro_define_function (file, line, name,
15060                                    argc, (const char **) argv,
15061                                    p + 1);
15062           else if (*p == '\0')
15063             {
15064               /* Complain, but do define it.  */
15065               dwarf2_macro_malformed_definition_complaint (body);
15066               macro_define_function (file, line, name,
15067                                      argc, (const char **) argv,
15068                                      p);
15069             }
15070           else
15071             /* Just complain.  */
15072             dwarf2_macro_malformed_definition_complaint (body);
15073         }
15074       else
15075         /* Just complain.  */
15076         dwarf2_macro_malformed_definition_complaint (body);
15077
15078       xfree (name);
15079       {
15080         int i;
15081
15082         for (i = 0; i < argc; i++)
15083           xfree (argv[i]);
15084       }
15085       xfree (argv);
15086     }
15087   else
15088     dwarf2_macro_malformed_definition_complaint (body);
15089 }
15090
15091 /* Skip some bytes from BYTES according to the form given in FORM.
15092    Returns the new pointer.  */
15093
15094 static gdb_byte *
15095 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15096                  enum dwarf_form form,
15097                  unsigned int offset_size,
15098                  struct dwarf2_section_info *section)
15099 {
15100   unsigned int bytes_read;
15101
15102   switch (form)
15103     {
15104     case DW_FORM_data1:
15105     case DW_FORM_flag:
15106       ++bytes;
15107       break;
15108
15109     case DW_FORM_data2:
15110       bytes += 2;
15111       break;
15112
15113     case DW_FORM_data4:
15114       bytes += 4;
15115       break;
15116
15117     case DW_FORM_data8:
15118       bytes += 8;
15119       break;
15120
15121     case DW_FORM_string:
15122       read_direct_string (abfd, bytes, &bytes_read);
15123       bytes += bytes_read;
15124       break;
15125
15126     case DW_FORM_sec_offset:
15127     case DW_FORM_strp:
15128       bytes += offset_size;
15129       break;
15130
15131     case DW_FORM_block:
15132       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15133       bytes += bytes_read;
15134       break;
15135
15136     case DW_FORM_block1:
15137       bytes += 1 + read_1_byte (abfd, bytes);
15138       break;
15139     case DW_FORM_block2:
15140       bytes += 2 + read_2_bytes (abfd, bytes);
15141       break;
15142     case DW_FORM_block4:
15143       bytes += 4 + read_4_bytes (abfd, bytes);
15144       break;
15145
15146     case DW_FORM_sdata:
15147     case DW_FORM_udata:
15148       bytes = skip_leb128 (abfd, bytes);
15149       break;
15150
15151     default:
15152       {
15153       complain:
15154         complaint (&symfile_complaints,
15155                    _("invalid form 0x%x in `%s'"),
15156                    form,
15157                    section->asection->name);
15158         return NULL;
15159       }
15160     }
15161
15162   return bytes;
15163 }
15164
15165 /* A helper for dwarf_decode_macros that handles skipping an unknown
15166    opcode.  Returns an updated pointer to the macro data buffer; or,
15167    on error, issues a complaint and returns NULL.  */
15168
15169 static gdb_byte *
15170 skip_unknown_opcode (unsigned int opcode,
15171                      gdb_byte **opcode_definitions,
15172                      gdb_byte *mac_ptr,
15173                      bfd *abfd,
15174                      unsigned int offset_size,
15175                      struct dwarf2_section_info *section)
15176 {
15177   unsigned int bytes_read, i;
15178   unsigned long arg;
15179   gdb_byte *defn;
15180
15181   if (opcode_definitions[opcode] == NULL)
15182     {
15183       complaint (&symfile_complaints,
15184                  _("unrecognized DW_MACFINO opcode 0x%x"),
15185                  opcode);
15186       return NULL;
15187     }
15188
15189   defn = opcode_definitions[opcode];
15190   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15191   defn += bytes_read;
15192
15193   for (i = 0; i < arg; ++i)
15194     {
15195       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15196       if (mac_ptr == NULL)
15197         {
15198           /* skip_form_bytes already issued the complaint.  */
15199           return NULL;
15200         }
15201     }
15202
15203   return mac_ptr;
15204 }
15205
15206 /* A helper function which parses the header of a macro section.
15207    If the macro section is the extended (for now called "GNU") type,
15208    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15209    the header, or issues a complaint and returns NULL on error.  */
15210
15211 static gdb_byte *
15212 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15213                           bfd *abfd,
15214                           gdb_byte *mac_ptr,
15215                           unsigned int *offset_size,
15216                           int section_is_gnu)
15217 {
15218   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15219
15220   if (section_is_gnu)
15221     {
15222       unsigned int version, flags;
15223
15224       version = read_2_bytes (abfd, mac_ptr);
15225       if (version != 4)
15226         {
15227           complaint (&symfile_complaints,
15228                      _("unrecognized version `%d' in .debug_macro section"),
15229                      version);
15230           return NULL;
15231         }
15232       mac_ptr += 2;
15233
15234       flags = read_1_byte (abfd, mac_ptr);
15235       ++mac_ptr;
15236       *offset_size = (flags & 1) ? 8 : 4;
15237
15238       if ((flags & 2) != 0)
15239         /* We don't need the line table offset.  */
15240         mac_ptr += *offset_size;
15241
15242       /* Vendor opcode descriptions.  */
15243       if ((flags & 4) != 0)
15244         {
15245           unsigned int i, count;
15246
15247           count = read_1_byte (abfd, mac_ptr);
15248           ++mac_ptr;
15249           for (i = 0; i < count; ++i)
15250             {
15251               unsigned int opcode, bytes_read;
15252               unsigned long arg;
15253
15254               opcode = read_1_byte (abfd, mac_ptr);
15255               ++mac_ptr;
15256               opcode_definitions[opcode] = mac_ptr;
15257               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15258               mac_ptr += bytes_read;
15259               mac_ptr += arg;
15260             }
15261         }
15262     }
15263
15264   return mac_ptr;
15265 }
15266
15267 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15268    including DW_MACRO_GNU_transparent_include.  */
15269
15270 static void
15271 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15272                           struct macro_source_file *current_file,
15273                           struct line_header *lh, char *comp_dir,
15274                           struct dwarf2_section_info *section,
15275                           int section_is_gnu,
15276                           unsigned int offset_size,
15277                           struct objfile *objfile,
15278                           htab_t include_hash)
15279 {
15280   enum dwarf_macro_record_type macinfo_type;
15281   int at_commandline;
15282   gdb_byte *opcode_definitions[256];
15283
15284   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15285                                       &offset_size, section_is_gnu);
15286   if (mac_ptr == NULL)
15287     {
15288       /* We already issued a complaint.  */
15289       return;
15290     }
15291
15292   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15293      GDB is still reading the definitions from command line.  First
15294      DW_MACINFO_start_file will need to be ignored as it was already executed
15295      to create CURRENT_FILE for the main source holding also the command line
15296      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15297      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15298
15299   at_commandline = 1;
15300
15301   do
15302     {
15303       /* Do we at least have room for a macinfo type byte?  */
15304       if (mac_ptr >= mac_end)
15305         {
15306           dwarf2_macros_too_long_complaint (section);
15307           break;
15308         }
15309
15310       macinfo_type = read_1_byte (abfd, mac_ptr);
15311       mac_ptr++;
15312
15313       /* Note that we rely on the fact that the corresponding GNU and
15314          DWARF constants are the same.  */
15315       switch (macinfo_type)
15316         {
15317           /* A zero macinfo type indicates the end of the macro
15318              information.  */
15319         case 0:
15320           break;
15321
15322         case DW_MACRO_GNU_define:
15323         case DW_MACRO_GNU_undef:
15324         case DW_MACRO_GNU_define_indirect:
15325         case DW_MACRO_GNU_undef_indirect:
15326           {
15327             unsigned int bytes_read;
15328             int line;
15329             char *body;
15330             int is_define;
15331
15332             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15333             mac_ptr += bytes_read;
15334
15335             if (macinfo_type == DW_MACRO_GNU_define
15336                 || macinfo_type == DW_MACRO_GNU_undef)
15337               {
15338                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15339                 mac_ptr += bytes_read;
15340               }
15341             else
15342               {
15343                 LONGEST str_offset;
15344
15345                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15346                 mac_ptr += offset_size;
15347
15348                 body = read_indirect_string_at_offset (abfd, str_offset);
15349               }
15350
15351             is_define = (macinfo_type == DW_MACRO_GNU_define
15352                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15353             if (! current_file)
15354               {
15355                 /* DWARF violation as no main source is present.  */
15356                 complaint (&symfile_complaints,
15357                            _("debug info with no main source gives macro %s "
15358                              "on line %d: %s"),
15359                            is_define ? _("definition") : _("undefinition"),
15360                            line, body);
15361                 break;
15362               }
15363             if ((line == 0 && !at_commandline)
15364                 || (line != 0 && at_commandline))
15365               complaint (&symfile_complaints,
15366                          _("debug info gives %s macro %s with %s line %d: %s"),
15367                          at_commandline ? _("command-line") : _("in-file"),
15368                          is_define ? _("definition") : _("undefinition"),
15369                          line == 0 ? _("zero") : _("non-zero"), line, body);
15370
15371             if (is_define)
15372               parse_macro_definition (current_file, line, body);
15373             else
15374               {
15375                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15376                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15377                 macro_undef (current_file, line, body);
15378               }
15379           }
15380           break;
15381
15382         case DW_MACRO_GNU_start_file:
15383           {
15384             unsigned int bytes_read;
15385             int line, file;
15386
15387             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15388             mac_ptr += bytes_read;
15389             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15390             mac_ptr += bytes_read;
15391
15392             if ((line == 0 && !at_commandline)
15393                 || (line != 0 && at_commandline))
15394               complaint (&symfile_complaints,
15395                          _("debug info gives source %d included "
15396                            "from %s at %s line %d"),
15397                          file, at_commandline ? _("command-line") : _("file"),
15398                          line == 0 ? _("zero") : _("non-zero"), line);
15399
15400             if (at_commandline)
15401               {
15402                 /* This DW_MACRO_GNU_start_file was executed in the
15403                    pass one.  */
15404                 at_commandline = 0;
15405               }
15406             else
15407               current_file = macro_start_file (file, line,
15408                                                current_file, comp_dir,
15409                                                lh, objfile);
15410           }
15411           break;
15412
15413         case DW_MACRO_GNU_end_file:
15414           if (! current_file)
15415             complaint (&symfile_complaints,
15416                        _("macro debug info has an unmatched "
15417                          "`close_file' directive"));
15418           else
15419             {
15420               current_file = current_file->included_by;
15421               if (! current_file)
15422                 {
15423                   enum dwarf_macro_record_type next_type;
15424
15425                   /* GCC circa March 2002 doesn't produce the zero
15426                      type byte marking the end of the compilation
15427                      unit.  Complain if it's not there, but exit no
15428                      matter what.  */
15429
15430                   /* Do we at least have room for a macinfo type byte?  */
15431                   if (mac_ptr >= mac_end)
15432                     {
15433                       dwarf2_macros_too_long_complaint (section);
15434                       return;
15435                     }
15436
15437                   /* We don't increment mac_ptr here, so this is just
15438                      a look-ahead.  */
15439                   next_type = read_1_byte (abfd, mac_ptr);
15440                   if (next_type != 0)
15441                     complaint (&symfile_complaints,
15442                                _("no terminating 0-type entry for "
15443                                  "macros in `.debug_macinfo' section"));
15444
15445                   return;
15446                 }
15447             }
15448           break;
15449
15450         case DW_MACRO_GNU_transparent_include:
15451           {
15452             LONGEST offset;
15453             void **slot;
15454
15455             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15456             mac_ptr += offset_size;
15457
15458             slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15459             if (*slot != NULL)
15460               {
15461                 /* This has actually happened; see
15462                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
15463                 complaint (&symfile_complaints,
15464                            _("recursive DW_MACRO_GNU_transparent_include in "
15465                              ".debug_macro section"));
15466               }
15467             else
15468               {
15469                 *slot = mac_ptr;
15470
15471                 dwarf_decode_macro_bytes (abfd,
15472                                           section->buffer + offset,
15473                                           mac_end, current_file,
15474                                           lh, comp_dir,
15475                                           section, section_is_gnu,
15476                                           offset_size, objfile, include_hash);
15477
15478                 htab_remove_elt (include_hash, mac_ptr);
15479               }
15480           }
15481           break;
15482
15483         case DW_MACINFO_vendor_ext:
15484           if (!section_is_gnu)
15485             {
15486               unsigned int bytes_read;
15487               int constant;
15488
15489               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15490               mac_ptr += bytes_read;
15491               read_direct_string (abfd, mac_ptr, &bytes_read);
15492               mac_ptr += bytes_read;
15493
15494               /* We don't recognize any vendor extensions.  */
15495               break;
15496             }
15497           /* FALLTHROUGH */
15498
15499         default:
15500           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15501                                          mac_ptr, abfd, offset_size,
15502                                          section);
15503           if (mac_ptr == NULL)
15504             return;
15505           break;
15506         }
15507     } while (macinfo_type != 0);
15508 }
15509
15510 static void
15511 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15512                      char *comp_dir, bfd *abfd,
15513                      struct dwarf2_cu *cu,
15514                      struct dwarf2_section_info *section,
15515                      int section_is_gnu)
15516 {
15517   struct objfile *objfile = dwarf2_per_objfile->objfile;
15518   gdb_byte *mac_ptr, *mac_end;
15519   struct macro_source_file *current_file = 0;
15520   enum dwarf_macro_record_type macinfo_type;
15521   unsigned int offset_size = cu->header.offset_size;
15522   gdb_byte *opcode_definitions[256];
15523   struct cleanup *cleanup;
15524   htab_t include_hash;
15525   void **slot;
15526
15527   dwarf2_read_section (objfile, section);
15528   if (section->buffer == NULL)
15529     {
15530       complaint (&symfile_complaints, _("missing %s section"),
15531                  section->asection->name);
15532       return;
15533     }
15534
15535   /* First pass: Find the name of the base filename.
15536      This filename is needed in order to process all macros whose definition
15537      (or undefinition) comes from the command line.  These macros are defined
15538      before the first DW_MACINFO_start_file entry, and yet still need to be
15539      associated to the base file.
15540
15541      To determine the base file name, we scan the macro definitions until we
15542      reach the first DW_MACINFO_start_file entry.  We then initialize
15543      CURRENT_FILE accordingly so that any macro definition found before the
15544      first DW_MACINFO_start_file can still be associated to the base file.  */
15545
15546   mac_ptr = section->buffer + offset;
15547   mac_end = section->buffer + section->size;
15548
15549   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15550                                       &offset_size, section_is_gnu);
15551   if (mac_ptr == NULL)
15552     {
15553       /* We already issued a complaint.  */
15554       return;
15555     }
15556
15557   do
15558     {
15559       /* Do we at least have room for a macinfo type byte?  */
15560       if (mac_ptr >= mac_end)
15561         {
15562           /* Complaint is printed during the second pass as GDB will probably
15563              stop the first pass earlier upon finding
15564              DW_MACINFO_start_file.  */
15565           break;
15566         }
15567
15568       macinfo_type = read_1_byte (abfd, mac_ptr);
15569       mac_ptr++;
15570
15571       /* Note that we rely on the fact that the corresponding GNU and
15572          DWARF constants are the same.  */
15573       switch (macinfo_type)
15574         {
15575           /* A zero macinfo type indicates the end of the macro
15576              information.  */
15577         case 0:
15578           break;
15579
15580         case DW_MACRO_GNU_define:
15581         case DW_MACRO_GNU_undef:
15582           /* Only skip the data by MAC_PTR.  */
15583           {
15584             unsigned int bytes_read;
15585
15586             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15587             mac_ptr += bytes_read;
15588             read_direct_string (abfd, mac_ptr, &bytes_read);
15589             mac_ptr += bytes_read;
15590           }
15591           break;
15592
15593         case DW_MACRO_GNU_start_file:
15594           {
15595             unsigned int bytes_read;
15596             int line, file;
15597
15598             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15599             mac_ptr += bytes_read;
15600             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15601             mac_ptr += bytes_read;
15602
15603             current_file = macro_start_file (file, line, current_file,
15604                                              comp_dir, lh, objfile);
15605           }
15606           break;
15607
15608         case DW_MACRO_GNU_end_file:
15609           /* No data to skip by MAC_PTR.  */
15610           break;
15611
15612         case DW_MACRO_GNU_define_indirect:
15613         case DW_MACRO_GNU_undef_indirect:
15614           {
15615             unsigned int bytes_read;
15616
15617             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15618             mac_ptr += bytes_read;
15619             mac_ptr += offset_size;
15620           }
15621           break;
15622
15623         case DW_MACRO_GNU_transparent_include:
15624           /* Note that, according to the spec, a transparent include
15625              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15626              skip this opcode.  */
15627           mac_ptr += offset_size;
15628           break;
15629
15630         case DW_MACINFO_vendor_ext:
15631           /* Only skip the data by MAC_PTR.  */
15632           if (!section_is_gnu)
15633             {
15634               unsigned int bytes_read;
15635
15636               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15637               mac_ptr += bytes_read;
15638               read_direct_string (abfd, mac_ptr, &bytes_read);
15639               mac_ptr += bytes_read;
15640             }
15641           /* FALLTHROUGH */
15642
15643         default:
15644           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15645                                          mac_ptr, abfd, offset_size,
15646                                          section);
15647           if (mac_ptr == NULL)
15648             return;
15649           break;
15650         }
15651     } while (macinfo_type != 0 && current_file == NULL);
15652
15653   /* Second pass: Process all entries.
15654
15655      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15656      command-line macro definitions/undefinitions.  This flag is unset when we
15657      reach the first DW_MACINFO_start_file entry.  */
15658
15659   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15660                                     NULL, xcalloc, xfree);
15661   cleanup = make_cleanup_htab_delete (include_hash);
15662   mac_ptr = section->buffer + offset;
15663   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15664   *slot = mac_ptr;
15665   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15666                             current_file, lh, comp_dir, section, section_is_gnu,
15667                             offset_size, objfile, include_hash);
15668   do_cleanups (cleanup);
15669 }
15670
15671 /* Check if the attribute's form is a DW_FORM_block*
15672    if so return true else false.  */
15673 static int
15674 attr_form_is_block (struct attribute *attr)
15675 {
15676   return (attr == NULL ? 0 :
15677       attr->form == DW_FORM_block1
15678       || attr->form == DW_FORM_block2
15679       || attr->form == DW_FORM_block4
15680       || attr->form == DW_FORM_block
15681       || attr->form == DW_FORM_exprloc);
15682 }
15683
15684 /* Return non-zero if ATTR's value is a section offset --- classes
15685    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15686    You may use DW_UNSND (attr) to retrieve such offsets.
15687
15688    Section 7.5.4, "Attribute Encodings", explains that no attribute
15689    may have a value that belongs to more than one of these classes; it
15690    would be ambiguous if we did, because we use the same forms for all
15691    of them.  */
15692 static int
15693 attr_form_is_section_offset (struct attribute *attr)
15694 {
15695   return (attr->form == DW_FORM_data4
15696           || attr->form == DW_FORM_data8
15697           || attr->form == DW_FORM_sec_offset);
15698 }
15699
15700
15701 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15702    zero otherwise.  When this function returns true, you can apply
15703    dwarf2_get_attr_constant_value to it.
15704
15705    However, note that for some attributes you must check
15706    attr_form_is_section_offset before using this test.  DW_FORM_data4
15707    and DW_FORM_data8 are members of both the constant class, and of
15708    the classes that contain offsets into other debug sections
15709    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15710    that, if an attribute's can be either a constant or one of the
15711    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15712    taken as section offsets, not constants.  */
15713 static int
15714 attr_form_is_constant (struct attribute *attr)
15715 {
15716   switch (attr->form)
15717     {
15718     case DW_FORM_sdata:
15719     case DW_FORM_udata:
15720     case DW_FORM_data1:
15721     case DW_FORM_data2:
15722     case DW_FORM_data4:
15723     case DW_FORM_data8:
15724       return 1;
15725     default:
15726       return 0;
15727     }
15728 }
15729
15730 /* A helper function that fills in a dwarf2_loclist_baton.  */
15731
15732 static void
15733 fill_in_loclist_baton (struct dwarf2_cu *cu,
15734                        struct dwarf2_loclist_baton *baton,
15735                        struct attribute *attr)
15736 {
15737   dwarf2_read_section (dwarf2_per_objfile->objfile,
15738                        &dwarf2_per_objfile->loc);
15739
15740   baton->per_cu = cu->per_cu;
15741   gdb_assert (baton->per_cu);
15742   /* We don't know how long the location list is, but make sure we
15743      don't run off the edge of the section.  */
15744   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15745   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15746   baton->base_address = cu->base_address;
15747 }
15748
15749 static void
15750 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15751                              struct dwarf2_cu *cu)
15752 {
15753   struct objfile *objfile = dwarf2_per_objfile->objfile;
15754
15755   if (attr_form_is_section_offset (attr)
15756       /* ".debug_loc" may not exist at all, or the offset may be outside
15757          the section.  If so, fall through to the complaint in the
15758          other branch.  */
15759       && DW_UNSND (attr) < dwarf2_section_size (objfile,
15760                                                 &dwarf2_per_objfile->loc))
15761     {
15762       struct dwarf2_loclist_baton *baton;
15763
15764       baton = obstack_alloc (&objfile->objfile_obstack,
15765                              sizeof (struct dwarf2_loclist_baton));
15766
15767       fill_in_loclist_baton (cu, baton, attr);
15768
15769       if (cu->base_known == 0)
15770         complaint (&symfile_complaints,
15771                    _("Location list used without "
15772                      "specifying the CU base address."));
15773
15774       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15775       SYMBOL_LOCATION_BATON (sym) = baton;
15776     }
15777   else
15778     {
15779       struct dwarf2_locexpr_baton *baton;
15780
15781       baton = obstack_alloc (&objfile->objfile_obstack,
15782                              sizeof (struct dwarf2_locexpr_baton));
15783       baton->per_cu = cu->per_cu;
15784       gdb_assert (baton->per_cu);
15785
15786       if (attr_form_is_block (attr))
15787         {
15788           /* Note that we're just copying the block's data pointer
15789              here, not the actual data.  We're still pointing into the
15790              info_buffer for SYM's objfile; right now we never release
15791              that buffer, but when we do clean up properly this may
15792              need to change.  */
15793           baton->size = DW_BLOCK (attr)->size;
15794           baton->data = DW_BLOCK (attr)->data;
15795         }
15796       else
15797         {
15798           dwarf2_invalid_attrib_class_complaint ("location description",
15799                                                  SYMBOL_NATURAL_NAME (sym));
15800           baton->size = 0;
15801         }
15802
15803       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15804       SYMBOL_LOCATION_BATON (sym) = baton;
15805     }
15806 }
15807
15808 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15809    came from a separate debuginfo file, then the master objfile is
15810    returned.  */
15811
15812 struct objfile *
15813 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15814 {
15815   struct objfile *objfile = per_cu->objfile;
15816
15817   /* Return the master objfile, so that we can report and look up the
15818      correct file containing this variable.  */
15819   if (objfile->separate_debug_objfile_backlink)
15820     objfile = objfile->separate_debug_objfile_backlink;
15821
15822   return objfile;
15823 }
15824
15825 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15826    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15827    CU_HEADERP first.  */
15828
15829 static const struct comp_unit_head *
15830 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15831                        struct dwarf2_per_cu_data *per_cu)
15832 {
15833   struct objfile *objfile;
15834   struct dwarf2_per_objfile *per_objfile;
15835   gdb_byte *info_ptr;
15836
15837   if (per_cu->cu)
15838     return &per_cu->cu->header;
15839
15840   objfile = per_cu->objfile;
15841   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15842   info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
15843
15844   memset (cu_headerp, 0, sizeof (*cu_headerp));
15845   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15846
15847   return cu_headerp;
15848 }
15849
15850 /* Return the address size given in the compilation unit header for CU.  */
15851
15852 int
15853 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15854 {
15855   struct comp_unit_head cu_header_local;
15856   const struct comp_unit_head *cu_headerp;
15857
15858   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15859
15860   return cu_headerp->addr_size;
15861 }
15862
15863 /* Return the offset size given in the compilation unit header for CU.  */
15864
15865 int
15866 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15867 {
15868   struct comp_unit_head cu_header_local;
15869   const struct comp_unit_head *cu_headerp;
15870
15871   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15872
15873   return cu_headerp->offset_size;
15874 }
15875
15876 /* See its dwarf2loc.h declaration.  */
15877
15878 int
15879 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15880 {
15881   struct comp_unit_head cu_header_local;
15882   const struct comp_unit_head *cu_headerp;
15883
15884   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15885
15886   if (cu_headerp->version == 2)
15887     return cu_headerp->addr_size;
15888   else
15889     return cu_headerp->offset_size;
15890 }
15891
15892 /* Return the text offset of the CU.  The returned offset comes from
15893    this CU's objfile.  If this objfile came from a separate debuginfo
15894    file, then the offset may be different from the corresponding
15895    offset in the parent objfile.  */
15896
15897 CORE_ADDR
15898 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15899 {
15900   struct objfile *objfile = per_cu->objfile;
15901
15902   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15903 }
15904
15905 /* Locate the .debug_info compilation unit from CU's objfile which contains
15906    the DIE at OFFSET.  Raises an error on failure.  */
15907
15908 static struct dwarf2_per_cu_data *
15909 dwarf2_find_containing_comp_unit (sect_offset offset,
15910                                   struct objfile *objfile)
15911 {
15912   struct dwarf2_per_cu_data *this_cu;
15913   int low, high;
15914
15915   low = 0;
15916   high = dwarf2_per_objfile->n_comp_units - 1;
15917   while (high > low)
15918     {
15919       int mid = low + (high - low) / 2;
15920
15921       if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
15922           >= offset.sect_off)
15923         high = mid;
15924       else
15925         low = mid + 1;
15926     }
15927   gdb_assert (low == high);
15928   if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
15929       > offset.sect_off)
15930     {
15931       if (low == 0)
15932         error (_("Dwarf Error: could not find partial DIE containing "
15933                "offset 0x%lx [in module %s]"),
15934                (long) offset.sect_off, bfd_get_filename (objfile->obfd));
15935
15936       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
15937                   <= offset.sect_off);
15938       return dwarf2_per_objfile->all_comp_units[low-1];
15939     }
15940   else
15941     {
15942       this_cu = dwarf2_per_objfile->all_comp_units[low];
15943       if (low == dwarf2_per_objfile->n_comp_units - 1
15944           && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
15945         error (_("invalid dwarf2 offset %u"), offset.sect_off);
15946       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
15947       return this_cu;
15948     }
15949 }
15950
15951 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
15952
15953 static void
15954 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15955 {
15956   memset (cu, 0, sizeof (*cu));
15957   per_cu->cu = cu;
15958   cu->per_cu = per_cu;
15959   cu->objfile = per_cu->objfile;
15960   obstack_init (&cu->comp_unit_obstack);
15961 }
15962
15963 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15964
15965 static void
15966 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15967 {
15968   struct attribute *attr;
15969
15970   /* Set the language we're debugging.  */
15971   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15972   if (attr)
15973     set_cu_language (DW_UNSND (attr), cu);
15974   else
15975     {
15976       cu->language = language_minimal;
15977       cu->language_defn = language_def (cu->language);
15978     }
15979 }
15980
15981 /* Release one cached compilation unit, CU.  We unlink it from the tree
15982    of compilation units, but we don't remove it from the read_in_chain;
15983    the caller is responsible for that.
15984    NOTE: DATA is a void * because this function is also used as a
15985    cleanup routine.  */
15986
15987 static void
15988 free_heap_comp_unit (void *data)
15989 {
15990   struct dwarf2_cu *cu = data;
15991
15992   gdb_assert (cu->per_cu != NULL);
15993   cu->per_cu->cu = NULL;
15994   cu->per_cu = NULL;
15995
15996   obstack_free (&cu->comp_unit_obstack, NULL);
15997
15998   xfree (cu);
15999 }
16000
16001 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16002    when we're finished with it.  We can't free the pointer itself, but be
16003    sure to unlink it from the cache.  Also release any associated storage
16004    and perform cache maintenance.
16005
16006    Only used during partial symbol parsing.  */
16007
16008 static void
16009 free_stack_comp_unit (void *data)
16010 {
16011   struct dwarf2_cu *cu = data;
16012
16013   gdb_assert (cu->per_cu != NULL);
16014   cu->per_cu->cu = NULL;
16015   cu->per_cu = NULL;
16016
16017   obstack_free (&cu->comp_unit_obstack, NULL);
16018   cu->partial_dies = NULL;
16019
16020   /* The previous code only did this if per_cu != NULL.
16021      But that would always succeed, so now we just unconditionally do
16022      the aging.  This seems like the wrong place to do such aging,
16023      but cleaning that up is left for later.  */
16024   age_cached_comp_units ();
16025 }
16026
16027 /* Free all cached compilation units.  */
16028
16029 static void
16030 free_cached_comp_units (void *data)
16031 {
16032   struct dwarf2_per_cu_data *per_cu, **last_chain;
16033
16034   per_cu = dwarf2_per_objfile->read_in_chain;
16035   last_chain = &dwarf2_per_objfile->read_in_chain;
16036   while (per_cu != NULL)
16037     {
16038       struct dwarf2_per_cu_data *next_cu;
16039
16040       next_cu = per_cu->cu->read_in_chain;
16041
16042       free_heap_comp_unit (per_cu->cu);
16043       *last_chain = next_cu;
16044
16045       per_cu = next_cu;
16046     }
16047 }
16048
16049 /* Increase the age counter on each cached compilation unit, and free
16050    any that are too old.  */
16051
16052 static void
16053 age_cached_comp_units (void)
16054 {
16055   struct dwarf2_per_cu_data *per_cu, **last_chain;
16056
16057   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16058   per_cu = dwarf2_per_objfile->read_in_chain;
16059   while (per_cu != NULL)
16060     {
16061       per_cu->cu->last_used ++;
16062       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16063         dwarf2_mark (per_cu->cu);
16064       per_cu = per_cu->cu->read_in_chain;
16065     }
16066
16067   per_cu = dwarf2_per_objfile->read_in_chain;
16068   last_chain = &dwarf2_per_objfile->read_in_chain;
16069   while (per_cu != NULL)
16070     {
16071       struct dwarf2_per_cu_data *next_cu;
16072
16073       next_cu = per_cu->cu->read_in_chain;
16074
16075       if (!per_cu->cu->mark)
16076         {
16077           free_heap_comp_unit (per_cu->cu);
16078           *last_chain = next_cu;
16079         }
16080       else
16081         last_chain = &per_cu->cu->read_in_chain;
16082
16083       per_cu = next_cu;
16084     }
16085 }
16086
16087 /* Remove a single compilation unit from the cache.  */
16088
16089 static void
16090 free_one_cached_comp_unit (void *target_cu)
16091 {
16092   struct dwarf2_per_cu_data *per_cu, **last_chain;
16093
16094   per_cu = dwarf2_per_objfile->read_in_chain;
16095   last_chain = &dwarf2_per_objfile->read_in_chain;
16096   while (per_cu != NULL)
16097     {
16098       struct dwarf2_per_cu_data *next_cu;
16099
16100       next_cu = per_cu->cu->read_in_chain;
16101
16102       if (per_cu->cu == target_cu)
16103         {
16104           free_heap_comp_unit (per_cu->cu);
16105           *last_chain = next_cu;
16106           break;
16107         }
16108       else
16109         last_chain = &per_cu->cu->read_in_chain;
16110
16111       per_cu = next_cu;
16112     }
16113 }
16114
16115 /* Release all extra memory associated with OBJFILE.  */
16116
16117 void
16118 dwarf2_free_objfile (struct objfile *objfile)
16119 {
16120   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16121
16122   if (dwarf2_per_objfile == NULL)
16123     return;
16124
16125   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
16126   free_cached_comp_units (NULL);
16127
16128   if (dwarf2_per_objfile->quick_file_names_table)
16129     htab_delete (dwarf2_per_objfile->quick_file_names_table);
16130
16131   /* Everything else should be on the objfile obstack.  */
16132 }
16133
16134 /* A pair of DIE offset and GDB type pointer.  We store these
16135    in a hash table separate from the DIEs, and preserve them
16136    when the DIEs are flushed out of cache.  */
16137
16138 struct dwarf2_offset_and_type
16139 {
16140   sect_offset offset;
16141   struct type *type;
16142 };
16143
16144 /* Hash function for a dwarf2_offset_and_type.  */
16145
16146 static hashval_t
16147 offset_and_type_hash (const void *item)
16148 {
16149   const struct dwarf2_offset_and_type *ofs = item;
16150
16151   return ofs->offset.sect_off;
16152 }
16153
16154 /* Equality function for a dwarf2_offset_and_type.  */
16155
16156 static int
16157 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16158 {
16159   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16160   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16161
16162   return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
16163 }
16164
16165 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16166    table if necessary.  For convenience, return TYPE.
16167
16168    The DIEs reading must have careful ordering to:
16169     * Not cause infite loops trying to read in DIEs as a prerequisite for
16170       reading current DIE.
16171     * Not trying to dereference contents of still incompletely read in types
16172       while reading in other DIEs.
16173     * Enable referencing still incompletely read in types just by a pointer to
16174       the type without accessing its fields.
16175
16176    Therefore caller should follow these rules:
16177      * Try to fetch any prerequisite types we may need to build this DIE type
16178        before building the type and calling set_die_type.
16179      * After building type call set_die_type for current DIE as soon as
16180        possible before fetching more types to complete the current type.
16181      * Make the type as complete as possible before fetching more types.  */
16182
16183 static struct type *
16184 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16185 {
16186   struct dwarf2_offset_and_type **slot, ofs;
16187   struct objfile *objfile = cu->objfile;
16188   htab_t *type_hash_ptr;
16189
16190   /* For Ada types, make sure that the gnat-specific data is always
16191      initialized (if not already set).  There are a few types where
16192      we should not be doing so, because the type-specific area is
16193      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16194      where the type-specific area is used to store the floatformat).
16195      But this is not a problem, because the gnat-specific information
16196      is actually not needed for these types.  */
16197   if (need_gnat_info (cu)
16198       && TYPE_CODE (type) != TYPE_CODE_FUNC
16199       && TYPE_CODE (type) != TYPE_CODE_FLT
16200       && !HAVE_GNAT_AUX_INFO (type))
16201     INIT_GNAT_SPECIFIC (type);
16202
16203   if (cu->per_cu->debug_types_section)
16204     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16205   else
16206     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16207
16208   if (*type_hash_ptr == NULL)
16209     {
16210       *type_hash_ptr
16211         = htab_create_alloc_ex (127,
16212                                 offset_and_type_hash,
16213                                 offset_and_type_eq,
16214                                 NULL,
16215                                 &objfile->objfile_obstack,
16216                                 hashtab_obstack_allocate,
16217                                 dummy_obstack_deallocate);
16218     }
16219
16220   ofs.offset = die->offset;
16221   ofs.type = type;
16222   slot = (struct dwarf2_offset_and_type **)
16223     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16224                               INSERT);
16225   if (*slot)
16226     complaint (&symfile_complaints,
16227                _("A problem internal to GDB: DIE 0x%x has type already set"),
16228                die->offset.sect_off);
16229   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16230   **slot = ofs;
16231   return type;
16232 }
16233
16234 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16235    table, or return NULL if the die does not have a saved type.  */
16236
16237 static struct type *
16238 get_die_type_at_offset (sect_offset offset,
16239                         struct dwarf2_per_cu_data *per_cu)
16240 {
16241   struct dwarf2_offset_and_type *slot, ofs;
16242   htab_t type_hash;
16243
16244   if (per_cu->debug_types_section)
16245     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16246   else
16247     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16248   if (type_hash == NULL)
16249     return NULL;
16250
16251   ofs.offset = offset;
16252   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
16253   if (slot)
16254     return slot->type;
16255   else
16256     return NULL;
16257 }
16258
16259 /* Look up the type for DIE in the appropriate type_hash table,
16260    or return NULL if DIE does not have a saved type.  */
16261
16262 static struct type *
16263 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16264 {
16265   return get_die_type_at_offset (die->offset, cu->per_cu);
16266 }
16267
16268 /* Add a dependence relationship from CU to REF_PER_CU.  */
16269
16270 static void
16271 dwarf2_add_dependence (struct dwarf2_cu *cu,
16272                        struct dwarf2_per_cu_data *ref_per_cu)
16273 {
16274   void **slot;
16275
16276   if (cu->dependencies == NULL)
16277     cu->dependencies
16278       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16279                               NULL, &cu->comp_unit_obstack,
16280                               hashtab_obstack_allocate,
16281                               dummy_obstack_deallocate);
16282
16283   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16284   if (*slot == NULL)
16285     *slot = ref_per_cu;
16286 }
16287
16288 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16289    Set the mark field in every compilation unit in the
16290    cache that we must keep because we are keeping CU.  */
16291
16292 static int
16293 dwarf2_mark_helper (void **slot, void *data)
16294 {
16295   struct dwarf2_per_cu_data *per_cu;
16296
16297   per_cu = (struct dwarf2_per_cu_data *) *slot;
16298
16299   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16300      reading of the chain.  As such dependencies remain valid it is not much
16301      useful to track and undo them during QUIT cleanups.  */
16302   if (per_cu->cu == NULL)
16303     return 1;
16304
16305   if (per_cu->cu->mark)
16306     return 1;
16307   per_cu->cu->mark = 1;
16308
16309   if (per_cu->cu->dependencies != NULL)
16310     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16311
16312   return 1;
16313 }
16314
16315 /* Set the mark field in CU and in every other compilation unit in the
16316    cache that we must keep because we are keeping CU.  */
16317
16318 static void
16319 dwarf2_mark (struct dwarf2_cu *cu)
16320 {
16321   if (cu->mark)
16322     return;
16323   cu->mark = 1;
16324   if (cu->dependencies != NULL)
16325     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16326 }
16327
16328 static void
16329 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16330 {
16331   while (per_cu)
16332     {
16333       per_cu->cu->mark = 0;
16334       per_cu = per_cu->cu->read_in_chain;
16335     }
16336 }
16337
16338 /* Trivial hash function for partial_die_info: the hash value of a DIE
16339    is its offset in .debug_info for this objfile.  */
16340
16341 static hashval_t
16342 partial_die_hash (const void *item)
16343 {
16344   const struct partial_die_info *part_die = item;
16345
16346   return part_die->offset.sect_off;
16347 }
16348
16349 /* Trivial comparison function for partial_die_info structures: two DIEs
16350    are equal if they have the same offset.  */
16351
16352 static int
16353 partial_die_eq (const void *item_lhs, const void *item_rhs)
16354 {
16355   const struct partial_die_info *part_die_lhs = item_lhs;
16356   const struct partial_die_info *part_die_rhs = item_rhs;
16357
16358   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
16359 }
16360
16361 static struct cmd_list_element *set_dwarf2_cmdlist;
16362 static struct cmd_list_element *show_dwarf2_cmdlist;
16363
16364 static void
16365 set_dwarf2_cmd (char *args, int from_tty)
16366 {
16367   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16368 }
16369
16370 static void
16371 show_dwarf2_cmd (char *args, int from_tty)
16372 {
16373   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16374 }
16375
16376 /* If section described by INFO was mmapped, munmap it now.  */
16377
16378 static void
16379 munmap_section_buffer (struct dwarf2_section_info *info)
16380 {
16381   if (info->map_addr != NULL)
16382     {
16383 #ifdef HAVE_MMAP
16384       int res;
16385
16386       res = munmap (info->map_addr, info->map_len);
16387       gdb_assert (res == 0);
16388 #else
16389       /* Without HAVE_MMAP, we should never be here to begin with.  */
16390       gdb_assert_not_reached ("no mmap support");
16391 #endif
16392     }
16393 }
16394
16395 /* munmap debug sections for OBJFILE, if necessary.  */
16396
16397 static void
16398 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16399 {
16400   struct dwarf2_per_objfile *data = d;
16401   int ix;
16402   struct dwarf2_section_info *section;
16403
16404   /* This is sorted according to the order they're defined in to make it easier
16405      to keep in sync.  */
16406   munmap_section_buffer (&data->info);
16407   munmap_section_buffer (&data->abbrev);
16408   munmap_section_buffer (&data->line);
16409   munmap_section_buffer (&data->loc);
16410   munmap_section_buffer (&data->macinfo);
16411   munmap_section_buffer (&data->macro);
16412   munmap_section_buffer (&data->str);
16413   munmap_section_buffer (&data->ranges);
16414   munmap_section_buffer (&data->frame);
16415   munmap_section_buffer (&data->eh_frame);
16416   munmap_section_buffer (&data->gdb_index);
16417
16418   for (ix = 0;
16419        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16420        ++ix)
16421     munmap_section_buffer (section);
16422
16423   VEC_free (dwarf2_section_info_def, data->types);
16424 }
16425
16426 \f
16427 /* The "save gdb-index" command.  */
16428
16429 /* The contents of the hash table we create when building the string
16430    table.  */
16431 struct strtab_entry
16432 {
16433   offset_type offset;
16434   const char *str;
16435 };
16436
16437 /* Hash function for a strtab_entry.
16438
16439    Function is used only during write_hash_table so no index format backward
16440    compatibility is needed.  */
16441
16442 static hashval_t
16443 hash_strtab_entry (const void *e)
16444 {
16445   const struct strtab_entry *entry = e;
16446   return mapped_index_string_hash (INT_MAX, entry->str);
16447 }
16448
16449 /* Equality function for a strtab_entry.  */
16450
16451 static int
16452 eq_strtab_entry (const void *a, const void *b)
16453 {
16454   const struct strtab_entry *ea = a;
16455   const struct strtab_entry *eb = b;
16456   return !strcmp (ea->str, eb->str);
16457 }
16458
16459 /* Create a strtab_entry hash table.  */
16460
16461 static htab_t
16462 create_strtab (void)
16463 {
16464   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16465                             xfree, xcalloc, xfree);
16466 }
16467
16468 /* Add a string to the constant pool.  Return the string's offset in
16469    host order.  */
16470
16471 static offset_type
16472 add_string (htab_t table, struct obstack *cpool, const char *str)
16473 {
16474   void **slot;
16475   struct strtab_entry entry;
16476   struct strtab_entry *result;
16477
16478   entry.str = str;
16479   slot = htab_find_slot (table, &entry, INSERT);
16480   if (*slot)
16481     result = *slot;
16482   else
16483     {
16484       result = XNEW (struct strtab_entry);
16485       result->offset = obstack_object_size (cpool);
16486       result->str = str;
16487       obstack_grow_str0 (cpool, str);
16488       *slot = result;
16489     }
16490   return result->offset;
16491 }
16492
16493 /* An entry in the symbol table.  */
16494 struct symtab_index_entry
16495 {
16496   /* The name of the symbol.  */
16497   const char *name;
16498   /* The offset of the name in the constant pool.  */
16499   offset_type index_offset;
16500   /* A sorted vector of the indices of all the CUs that hold an object
16501      of this name.  */
16502   VEC (offset_type) *cu_indices;
16503 };
16504
16505 /* The symbol table.  This is a power-of-2-sized hash table.  */
16506 struct mapped_symtab
16507 {
16508   offset_type n_elements;
16509   offset_type size;
16510   struct symtab_index_entry **data;
16511 };
16512
16513 /* Hash function for a symtab_index_entry.  */
16514
16515 static hashval_t
16516 hash_symtab_entry (const void *e)
16517 {
16518   const struct symtab_index_entry *entry = e;
16519   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16520                          sizeof (offset_type) * VEC_length (offset_type,
16521                                                             entry->cu_indices),
16522                          0);
16523 }
16524
16525 /* Equality function for a symtab_index_entry.  */
16526
16527 static int
16528 eq_symtab_entry (const void *a, const void *b)
16529 {
16530   const struct symtab_index_entry *ea = a;
16531   const struct symtab_index_entry *eb = b;
16532   int len = VEC_length (offset_type, ea->cu_indices);
16533   if (len != VEC_length (offset_type, eb->cu_indices))
16534     return 0;
16535   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16536                   VEC_address (offset_type, eb->cu_indices),
16537                   sizeof (offset_type) * len);
16538 }
16539
16540 /* Destroy a symtab_index_entry.  */
16541
16542 static void
16543 delete_symtab_entry (void *p)
16544 {
16545   struct symtab_index_entry *entry = p;
16546   VEC_free (offset_type, entry->cu_indices);
16547   xfree (entry);
16548 }
16549
16550 /* Create a hash table holding symtab_index_entry objects.  */
16551
16552 static htab_t
16553 create_symbol_hash_table (void)
16554 {
16555   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16556                             delete_symtab_entry, xcalloc, xfree);
16557 }
16558
16559 /* Create a new mapped symtab object.  */
16560
16561 static struct mapped_symtab *
16562 create_mapped_symtab (void)
16563 {
16564   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16565   symtab->n_elements = 0;
16566   symtab->size = 1024;
16567   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16568   return symtab;
16569 }
16570
16571 /* Destroy a mapped_symtab.  */
16572
16573 static void
16574 cleanup_mapped_symtab (void *p)
16575 {
16576   struct mapped_symtab *symtab = p;
16577   /* The contents of the array are freed when the other hash table is
16578      destroyed.  */
16579   xfree (symtab->data);
16580   xfree (symtab);
16581 }
16582
16583 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16584    the slot.
16585    
16586    Function is used only during write_hash_table so no index format backward
16587    compatibility is needed.  */
16588
16589 static struct symtab_index_entry **
16590 find_slot (struct mapped_symtab *symtab, const char *name)
16591 {
16592   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16593
16594   index = hash & (symtab->size - 1);
16595   step = ((hash * 17) & (symtab->size - 1)) | 1;
16596
16597   for (;;)
16598     {
16599       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16600         return &symtab->data[index];
16601       index = (index + step) & (symtab->size - 1);
16602     }
16603 }
16604
16605 /* Expand SYMTAB's hash table.  */
16606
16607 static void
16608 hash_expand (struct mapped_symtab *symtab)
16609 {
16610   offset_type old_size = symtab->size;
16611   offset_type i;
16612   struct symtab_index_entry **old_entries = symtab->data;
16613
16614   symtab->size *= 2;
16615   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16616
16617   for (i = 0; i < old_size; ++i)
16618     {
16619       if (old_entries[i])
16620         {
16621           struct symtab_index_entry **slot = find_slot (symtab,
16622                                                         old_entries[i]->name);
16623           *slot = old_entries[i];
16624         }
16625     }
16626
16627   xfree (old_entries);
16628 }
16629
16630 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16631    is the index of the CU in which the symbol appears.  */
16632
16633 static void
16634 add_index_entry (struct mapped_symtab *symtab, const char *name,
16635                  offset_type cu_index)
16636 {
16637   struct symtab_index_entry **slot;
16638
16639   ++symtab->n_elements;
16640   if (4 * symtab->n_elements / 3 >= symtab->size)
16641     hash_expand (symtab);
16642
16643   slot = find_slot (symtab, name);
16644   if (!*slot)
16645     {
16646       *slot = XNEW (struct symtab_index_entry);
16647       (*slot)->name = name;
16648       (*slot)->cu_indices = NULL;
16649     }
16650   /* Don't push an index twice.  Due to how we add entries we only
16651      have to check the last one.  */ 
16652   if (VEC_empty (offset_type, (*slot)->cu_indices)
16653       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16654     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16655 }
16656
16657 /* Add a vector of indices to the constant pool.  */
16658
16659 static offset_type
16660 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16661                       struct symtab_index_entry *entry)
16662 {
16663   void **slot;
16664
16665   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16666   if (!*slot)
16667     {
16668       offset_type len = VEC_length (offset_type, entry->cu_indices);
16669       offset_type val = MAYBE_SWAP (len);
16670       offset_type iter;
16671       int i;
16672
16673       *slot = entry;
16674       entry->index_offset = obstack_object_size (cpool);
16675
16676       obstack_grow (cpool, &val, sizeof (val));
16677       for (i = 0;
16678            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16679            ++i)
16680         {
16681           val = MAYBE_SWAP (iter);
16682           obstack_grow (cpool, &val, sizeof (val));
16683         }
16684     }
16685   else
16686     {
16687       struct symtab_index_entry *old_entry = *slot;
16688       entry->index_offset = old_entry->index_offset;
16689       entry = old_entry;
16690     }
16691   return entry->index_offset;
16692 }
16693
16694 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16695    constant pool entries going into the obstack CPOOL.  */
16696
16697 static void
16698 write_hash_table (struct mapped_symtab *symtab,
16699                   struct obstack *output, struct obstack *cpool)
16700 {
16701   offset_type i;
16702   htab_t symbol_hash_table;
16703   htab_t str_table;
16704
16705   symbol_hash_table = create_symbol_hash_table ();
16706   str_table = create_strtab ();
16707
16708   /* We add all the index vectors to the constant pool first, to
16709      ensure alignment is ok.  */
16710   for (i = 0; i < symtab->size; ++i)
16711     {
16712       if (symtab->data[i])
16713         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16714     }
16715
16716   /* Now write out the hash table.  */
16717   for (i = 0; i < symtab->size; ++i)
16718     {
16719       offset_type str_off, vec_off;
16720
16721       if (symtab->data[i])
16722         {
16723           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16724           vec_off = symtab->data[i]->index_offset;
16725         }
16726       else
16727         {
16728           /* While 0 is a valid constant pool index, it is not valid
16729              to have 0 for both offsets.  */
16730           str_off = 0;
16731           vec_off = 0;
16732         }
16733
16734       str_off = MAYBE_SWAP (str_off);
16735       vec_off = MAYBE_SWAP (vec_off);
16736
16737       obstack_grow (output, &str_off, sizeof (str_off));
16738       obstack_grow (output, &vec_off, sizeof (vec_off));
16739     }
16740
16741   htab_delete (str_table);
16742   htab_delete (symbol_hash_table);
16743 }
16744
16745 /* Struct to map psymtab to CU index in the index file.  */
16746 struct psymtab_cu_index_map
16747 {
16748   struct partial_symtab *psymtab;
16749   unsigned int cu_index;
16750 };
16751
16752 static hashval_t
16753 hash_psymtab_cu_index (const void *item)
16754 {
16755   const struct psymtab_cu_index_map *map = item;
16756
16757   return htab_hash_pointer (map->psymtab);
16758 }
16759
16760 static int
16761 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16762 {
16763   const struct psymtab_cu_index_map *lhs = item_lhs;
16764   const struct psymtab_cu_index_map *rhs = item_rhs;
16765
16766   return lhs->psymtab == rhs->psymtab;
16767 }
16768
16769 /* Helper struct for building the address table.  */
16770 struct addrmap_index_data
16771 {
16772   struct objfile *objfile;
16773   struct obstack *addr_obstack;
16774   htab_t cu_index_htab;
16775
16776   /* Non-zero if the previous_* fields are valid.
16777      We can't write an entry until we see the next entry (since it is only then
16778      that we know the end of the entry).  */
16779   int previous_valid;
16780   /* Index of the CU in the table of all CUs in the index file.  */
16781   unsigned int previous_cu_index;
16782   /* Start address of the CU.  */
16783   CORE_ADDR previous_cu_start;
16784 };
16785
16786 /* Write an address entry to OBSTACK.  */
16787
16788 static void
16789 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16790                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16791 {
16792   offset_type cu_index_to_write;
16793   char addr[8];
16794   CORE_ADDR baseaddr;
16795
16796   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16797
16798   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16799   obstack_grow (obstack, addr, 8);
16800   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16801   obstack_grow (obstack, addr, 8);
16802   cu_index_to_write = MAYBE_SWAP (cu_index);
16803   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16804 }
16805
16806 /* Worker function for traversing an addrmap to build the address table.  */
16807
16808 static int
16809 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16810 {
16811   struct addrmap_index_data *data = datap;
16812   struct partial_symtab *pst = obj;
16813   offset_type cu_index;
16814   void **slot;
16815
16816   if (data->previous_valid)
16817     add_address_entry (data->objfile, data->addr_obstack,
16818                        data->previous_cu_start, start_addr,
16819                        data->previous_cu_index);
16820
16821   data->previous_cu_start = start_addr;
16822   if (pst != NULL)
16823     {
16824       struct psymtab_cu_index_map find_map, *map;
16825       find_map.psymtab = pst;
16826       map = htab_find (data->cu_index_htab, &find_map);
16827       gdb_assert (map != NULL);
16828       data->previous_cu_index = map->cu_index;
16829       data->previous_valid = 1;
16830     }
16831   else
16832       data->previous_valid = 0;
16833
16834   return 0;
16835 }
16836
16837 /* Write OBJFILE's address map to OBSTACK.
16838    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16839    in the index file.  */
16840
16841 static void
16842 write_address_map (struct objfile *objfile, struct obstack *obstack,
16843                    htab_t cu_index_htab)
16844 {
16845   struct addrmap_index_data addrmap_index_data;
16846
16847   /* When writing the address table, we have to cope with the fact that
16848      the addrmap iterator only provides the start of a region; we have to
16849      wait until the next invocation to get the start of the next region.  */
16850
16851   addrmap_index_data.objfile = objfile;
16852   addrmap_index_data.addr_obstack = obstack;
16853   addrmap_index_data.cu_index_htab = cu_index_htab;
16854   addrmap_index_data.previous_valid = 0;
16855
16856   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16857                    &addrmap_index_data);
16858
16859   /* It's highly unlikely the last entry (end address = 0xff...ff)
16860      is valid, but we should still handle it.
16861      The end address is recorded as the start of the next region, but that
16862      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16863      anyway.  */
16864   if (addrmap_index_data.previous_valid)
16865     add_address_entry (objfile, obstack,
16866                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16867                        addrmap_index_data.previous_cu_index);
16868 }
16869
16870 /* Add a list of partial symbols to SYMTAB.  */
16871
16872 static void
16873 write_psymbols (struct mapped_symtab *symtab,
16874                 htab_t psyms_seen,
16875                 struct partial_symbol **psymp,
16876                 int count,
16877                 offset_type cu_index,
16878                 int is_static)
16879 {
16880   for (; count-- > 0; ++psymp)
16881     {
16882       void **slot, *lookup;
16883
16884       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16885         error (_("Ada is not currently supported by the index"));
16886
16887       /* We only want to add a given psymbol once.  However, we also
16888          want to account for whether it is global or static.  So, we
16889          may add it twice, using slightly different values.  */
16890       if (is_static)
16891         {
16892           uintptr_t val = 1 | (uintptr_t) *psymp;
16893
16894           lookup = (void *) val;
16895         }
16896       else
16897         lookup = *psymp;
16898
16899       /* Only add a given psymbol once.  */
16900       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16901       if (!*slot)
16902         {
16903           *slot = lookup;
16904           add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16905         }
16906     }
16907 }
16908
16909 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16910    exception if there is an error.  */
16911
16912 static void
16913 write_obstack (FILE *file, struct obstack *obstack)
16914 {
16915   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16916               file)
16917       != obstack_object_size (obstack))
16918     error (_("couldn't data write to file"));
16919 }
16920
16921 /* Unlink a file if the argument is not NULL.  */
16922
16923 static void
16924 unlink_if_set (void *p)
16925 {
16926   char **filename = p;
16927   if (*filename)
16928     unlink (*filename);
16929 }
16930
16931 /* A helper struct used when iterating over debug_types.  */
16932 struct signatured_type_index_data
16933 {
16934   struct objfile *objfile;
16935   struct mapped_symtab *symtab;
16936   struct obstack *types_list;
16937   htab_t psyms_seen;
16938   int cu_index;
16939 };
16940
16941 /* A helper function that writes a single signatured_type to an
16942    obstack.  */
16943
16944 static int
16945 write_one_signatured_type (void **slot, void *d)
16946 {
16947   struct signatured_type_index_data *info = d;
16948   struct signatured_type *entry = (struct signatured_type *) *slot;
16949   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16950   struct partial_symtab *psymtab = per_cu->v.psymtab;
16951   gdb_byte val[8];
16952
16953   write_psymbols (info->symtab,
16954                   info->psyms_seen,
16955                   info->objfile->global_psymbols.list
16956                   + psymtab->globals_offset,
16957                   psymtab->n_global_syms, info->cu_index,
16958                   0);
16959   write_psymbols (info->symtab,
16960                   info->psyms_seen,
16961                   info->objfile->static_psymbols.list
16962                   + psymtab->statics_offset,
16963                   psymtab->n_static_syms, info->cu_index,
16964                   1);
16965
16966   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
16967                           entry->per_cu.offset.sect_off);
16968   obstack_grow (info->types_list, val, 8);
16969   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
16970   obstack_grow (info->types_list, val, 8);
16971   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16972   obstack_grow (info->types_list, val, 8);
16973
16974   ++info->cu_index;
16975
16976   return 1;
16977 }
16978
16979 /* Create an index file for OBJFILE in the directory DIR.  */
16980
16981 static void
16982 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16983 {
16984   struct cleanup *cleanup;
16985   char *filename, *cleanup_filename;
16986   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16987   struct obstack cu_list, types_cu_list;
16988   int i;
16989   FILE *out_file;
16990   struct mapped_symtab *symtab;
16991   offset_type val, size_of_contents, total_len;
16992   struct stat st;
16993   char buf[8];
16994   htab_t psyms_seen;
16995   htab_t cu_index_htab;
16996   struct psymtab_cu_index_map *psymtab_cu_index_map;
16997
16998   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16999     return;
17000
17001   if (dwarf2_per_objfile->using_index)
17002     error (_("Cannot use an index to create the index"));
17003
17004   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17005     error (_("Cannot make an index when the file has multiple .debug_types sections"));
17006
17007   if (stat (objfile->name, &st) < 0)
17008     perror_with_name (objfile->name);
17009
17010   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17011                      INDEX_SUFFIX, (char *) NULL);
17012   cleanup = make_cleanup (xfree, filename);
17013
17014   out_file = fopen (filename, "wb");
17015   if (!out_file)
17016     error (_("Can't open `%s' for writing"), filename);
17017
17018   cleanup_filename = filename;
17019   make_cleanup (unlink_if_set, &cleanup_filename);
17020
17021   symtab = create_mapped_symtab ();
17022   make_cleanup (cleanup_mapped_symtab, symtab);
17023
17024   obstack_init (&addr_obstack);
17025   make_cleanup_obstack_free (&addr_obstack);
17026
17027   obstack_init (&cu_list);
17028   make_cleanup_obstack_free (&cu_list);
17029
17030   obstack_init (&types_cu_list);
17031   make_cleanup_obstack_free (&types_cu_list);
17032
17033   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17034                                   NULL, xcalloc, xfree);
17035   make_cleanup_htab_delete (psyms_seen);
17036
17037   /* While we're scanning CU's create a table that maps a psymtab pointer
17038      (which is what addrmap records) to its index (which is what is recorded
17039      in the index file).  This will later be needed to write the address
17040      table.  */
17041   cu_index_htab = htab_create_alloc (100,
17042                                      hash_psymtab_cu_index,
17043                                      eq_psymtab_cu_index,
17044                                      NULL, xcalloc, xfree);
17045   make_cleanup_htab_delete (cu_index_htab);
17046   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17047     xmalloc (sizeof (struct psymtab_cu_index_map)
17048              * dwarf2_per_objfile->n_comp_units);
17049   make_cleanup (xfree, psymtab_cu_index_map);
17050
17051   /* The CU list is already sorted, so we don't need to do additional
17052      work here.  Also, the debug_types entries do not appear in
17053      all_comp_units, but only in their own hash table.  */
17054   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17055     {
17056       struct dwarf2_per_cu_data *per_cu
17057         = dwarf2_per_objfile->all_comp_units[i];
17058       struct partial_symtab *psymtab = per_cu->v.psymtab;
17059       gdb_byte val[8];
17060       struct psymtab_cu_index_map *map;
17061       void **slot;
17062
17063       write_psymbols (symtab,
17064                       psyms_seen,
17065                       objfile->global_psymbols.list + psymtab->globals_offset,
17066                       psymtab->n_global_syms, i,
17067                       0);
17068       write_psymbols (symtab,
17069                       psyms_seen,
17070                       objfile->static_psymbols.list + psymtab->statics_offset,
17071                       psymtab->n_static_syms, i,
17072                       1);
17073
17074       map = &psymtab_cu_index_map[i];
17075       map->psymtab = psymtab;
17076       map->cu_index = i;
17077       slot = htab_find_slot (cu_index_htab, map, INSERT);
17078       gdb_assert (slot != NULL);
17079       gdb_assert (*slot == NULL);
17080       *slot = map;
17081
17082       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17083                               per_cu->offset.sect_off);
17084       obstack_grow (&cu_list, val, 8);
17085       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17086       obstack_grow (&cu_list, val, 8);
17087     }
17088
17089   /* Dump the address map.  */
17090   write_address_map (objfile, &addr_obstack, cu_index_htab);
17091
17092   /* Write out the .debug_type entries, if any.  */
17093   if (dwarf2_per_objfile->signatured_types)
17094     {
17095       struct signatured_type_index_data sig_data;
17096
17097       sig_data.objfile = objfile;
17098       sig_data.symtab = symtab;
17099       sig_data.types_list = &types_cu_list;
17100       sig_data.psyms_seen = psyms_seen;
17101       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17102       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17103                               write_one_signatured_type, &sig_data);
17104     }
17105
17106   obstack_init (&constant_pool);
17107   make_cleanup_obstack_free (&constant_pool);
17108   obstack_init (&symtab_obstack);
17109   make_cleanup_obstack_free (&symtab_obstack);
17110   write_hash_table (symtab, &symtab_obstack, &constant_pool);
17111
17112   obstack_init (&contents);
17113   make_cleanup_obstack_free (&contents);
17114   size_of_contents = 6 * sizeof (offset_type);
17115   total_len = size_of_contents;
17116
17117   /* The version number.  */
17118   val = MAYBE_SWAP (6);
17119   obstack_grow (&contents, &val, sizeof (val));
17120
17121   /* The offset of the CU list from the start of the file.  */
17122   val = MAYBE_SWAP (total_len);
17123   obstack_grow (&contents, &val, sizeof (val));
17124   total_len += obstack_object_size (&cu_list);
17125
17126   /* The offset of the types CU list from the start of the file.  */
17127   val = MAYBE_SWAP (total_len);
17128   obstack_grow (&contents, &val, sizeof (val));
17129   total_len += obstack_object_size (&types_cu_list);
17130
17131   /* The offset of the address table from the start of the file.  */
17132   val = MAYBE_SWAP (total_len);
17133   obstack_grow (&contents, &val, sizeof (val));
17134   total_len += obstack_object_size (&addr_obstack);
17135
17136   /* The offset of the symbol table from the start of the file.  */
17137   val = MAYBE_SWAP (total_len);
17138   obstack_grow (&contents, &val, sizeof (val));
17139   total_len += obstack_object_size (&symtab_obstack);
17140
17141   /* The offset of the constant pool from the start of the file.  */
17142   val = MAYBE_SWAP (total_len);
17143   obstack_grow (&contents, &val, sizeof (val));
17144   total_len += obstack_object_size (&constant_pool);
17145
17146   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17147
17148   write_obstack (out_file, &contents);
17149   write_obstack (out_file, &cu_list);
17150   write_obstack (out_file, &types_cu_list);
17151   write_obstack (out_file, &addr_obstack);
17152   write_obstack (out_file, &symtab_obstack);
17153   write_obstack (out_file, &constant_pool);
17154
17155   fclose (out_file);
17156
17157   /* We want to keep the file, so we set cleanup_filename to NULL
17158      here.  See unlink_if_set.  */
17159   cleanup_filename = NULL;
17160
17161   do_cleanups (cleanup);
17162 }
17163
17164 /* Implementation of the `save gdb-index' command.
17165    
17166    Note that the file format used by this command is documented in the
17167    GDB manual.  Any changes here must be documented there.  */
17168
17169 static void
17170 save_gdb_index_command (char *arg, int from_tty)
17171 {
17172   struct objfile *objfile;
17173
17174   if (!arg || !*arg)
17175     error (_("usage: save gdb-index DIRECTORY"));
17176
17177   ALL_OBJFILES (objfile)
17178   {
17179     struct stat st;
17180
17181     /* If the objfile does not correspond to an actual file, skip it.  */
17182     if (stat (objfile->name, &st) < 0)
17183       continue;
17184
17185     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17186     if (dwarf2_per_objfile)
17187       {
17188         volatile struct gdb_exception except;
17189
17190         TRY_CATCH (except, RETURN_MASK_ERROR)
17191           {
17192             write_psymtabs_to_index (objfile, arg);
17193           }
17194         if (except.reason < 0)
17195           exception_fprintf (gdb_stderr, except,
17196                              _("Error while writing index for `%s': "),
17197                              objfile->name);
17198       }
17199   }
17200 }
17201
17202 \f
17203
17204 int dwarf2_always_disassemble;
17205
17206 static void
17207 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17208                                 struct cmd_list_element *c, const char *value)
17209 {
17210   fprintf_filtered (file,
17211                     _("Whether to always disassemble "
17212                       "DWARF expressions is %s.\n"),
17213                     value);
17214 }
17215
17216 static void
17217 show_check_physname (struct ui_file *file, int from_tty,
17218                      struct cmd_list_element *c, const char *value)
17219 {
17220   fprintf_filtered (file,
17221                     _("Whether to check \"physname\" is %s.\n"),
17222                     value);
17223 }
17224
17225 void _initialize_dwarf2_read (void);
17226
17227 void
17228 _initialize_dwarf2_read (void)
17229 {
17230   struct cmd_list_element *c;
17231
17232   dwarf2_objfile_data_key
17233     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17234
17235   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17236 Set DWARF 2 specific variables.\n\
17237 Configure DWARF 2 variables such as the cache size"),
17238                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17239                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17240
17241   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17242 Show DWARF 2 specific variables\n\
17243 Show DWARF 2 variables such as the cache size"),
17244                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17245                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17246
17247   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17248                             &dwarf2_max_cache_age, _("\
17249 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17250 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17251 A higher limit means that cached compilation units will be stored\n\
17252 in memory longer, and more total memory will be used.  Zero disables\n\
17253 caching, which can slow down startup."),
17254                             NULL,
17255                             show_dwarf2_max_cache_age,
17256                             &set_dwarf2_cmdlist,
17257                             &show_dwarf2_cmdlist);
17258
17259   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17260                            &dwarf2_always_disassemble, _("\
17261 Set whether `info address' always disassembles DWARF expressions."), _("\
17262 Show whether `info address' always disassembles DWARF expressions."), _("\
17263 When enabled, DWARF expressions are always printed in an assembly-like\n\
17264 syntax.  When disabled, expressions will be printed in a more\n\
17265 conversational style, when possible."),
17266                            NULL,
17267                            show_dwarf2_always_disassemble,
17268                            &set_dwarf2_cmdlist,
17269                            &show_dwarf2_cmdlist);
17270
17271   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17272 Set debugging of the dwarf2 DIE reader."), _("\
17273 Show debugging of the dwarf2 DIE reader."), _("\
17274 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17275 The value is the maximum depth to print."),
17276                             NULL,
17277                             NULL,
17278                             &setdebuglist, &showdebuglist);
17279
17280   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17281 Set cross-checking of \"physname\" code against demangler."), _("\
17282 Show cross-checking of \"physname\" code against demangler."), _("\
17283 When enabled, GDB's internal \"physname\" code is checked against\n\
17284 the demangler."),
17285                            NULL, show_check_physname,
17286                            &setdebuglist, &showdebuglist);
17287
17288   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17289                _("\
17290 Save a gdb-index file.\n\
17291 Usage: save gdb-index DIRECTORY"),
17292                &save_cmdlist);
17293   set_cmd_completer (c, filename_completer);
17294 }