* dwarf2read.c (dw2_map_symtabs_matching_filename): Remove
[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 static int pagesize;
89
90 /* When set, the file that we're processing is known to have debugging
91    info for C++ namespaces.  GCC 3.3.x did not produce this information,
92    but later versions do.  */
93
94 static int processing_has_namespace_info;
95
96 static const struct objfile_data *dwarf2_objfile_data_key;
97
98 struct dwarf2_section_info
99 {
100   asection *asection;
101   gdb_byte *buffer;
102   bfd_size_type size;
103   /* Not NULL if the section was actually mmapped.  */
104   void *map_addr;
105   /* Page aligned size of mmapped area.  */
106   bfd_size_type map_len;
107   /* True if we have tried to read this section.  */
108   int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type.  It must be
115    architecture-independent.  */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* A description of the mapped index.  The file format is described in
121    a comment by the code that writes the index.  */
122 struct mapped_index
123 {
124   /* Index data format version.  */
125   int version;
126
127   /* The total length of the buffer.  */
128   off_t total_size;
129
130   /* A pointer to the address table data.  */
131   const gdb_byte *address_table;
132
133   /* Size of the address table data in bytes.  */
134   offset_type address_table_size;
135
136   /* The symbol table, implemented as a hash table.  */
137   const offset_type *symbol_table;
138
139   /* Size in slots, each slot is 2 offset_types.  */
140   offset_type symbol_table_slots;
141
142   /* A pointer to the constant pool.  */
143   const char *constant_pool;
144 };
145
146 /* Collection of data recorded per objfile.
147    This hangs off of dwarf2_objfile_data_key.  */
148
149 struct dwarf2_per_objfile
150 {
151   struct dwarf2_section_info info;
152   struct dwarf2_section_info abbrev;
153   struct dwarf2_section_info line;
154   struct dwarf2_section_info loc;
155   struct dwarf2_section_info macinfo;
156   struct dwarf2_section_info macro;
157   struct dwarf2_section_info str;
158   struct dwarf2_section_info ranges;
159   struct dwarf2_section_info frame;
160   struct dwarf2_section_info eh_frame;
161   struct dwarf2_section_info gdb_index;
162
163   VEC (dwarf2_section_info_def) *types;
164
165   /* Back link.  */
166   struct objfile *objfile;
167
168   /* Table of all the compilation units.  This is used to locate
169      the target compilation unit of a particular reference.  */
170   struct dwarf2_per_cu_data **all_comp_units;
171
172   /* The number of compilation units in ALL_COMP_UNITS.  */
173   int n_comp_units;
174
175   /* The number of .debug_types-related CUs.  */
176   int n_type_units;
177
178   /* The .debug_types-related CUs (TUs).  */
179   struct dwarf2_per_cu_data **all_type_units;
180
181   /* A chain of compilation units that are currently read in, so that
182      they can be freed later.  */
183   struct dwarf2_per_cu_data *read_in_chain;
184
185   /* A table mapping .debug_types signatures to its signatured_type entry.
186      This is NULL if the .debug_types section hasn't been read in yet.  */
187   htab_t signatured_types;
188
189   /* A flag indicating wether this objfile has a section loaded at a
190      VMA of 0.  */
191   int has_section_at_zero;
192
193   /* True if we are using the mapped index,
194      or we are faking it for OBJF_READNOW's sake.  */
195   unsigned char using_index;
196
197   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
198   struct mapped_index *index_table;
199
200   /* When using index_table, this keeps track of all quick_file_names entries.
201      TUs can share line table entries with CUs or other TUs, and there can be
202      a lot more TUs than unique line tables, so we maintain a separate table
203      of all line table entries to support the sharing.  */
204   htab_t quick_file_names_table;
205
206   /* Set during partial symbol reading, to prevent queueing of full
207      symbols.  */
208   int reading_partial_symbols;
209
210   /* Table mapping type .debug_info DIE offsets to types.
211      This is NULL if not allocated yet.
212      It (currently) makes sense to allocate debug_types_type_hash lazily.
213      To keep things simple we allocate both lazily.  */
214   htab_t debug_info_type_hash;
215
216   /* Table mapping type .debug_types DIE offsets to types.
217      This is NULL if not allocated yet.  */
218   htab_t debug_types_type_hash;
219 };
220
221 static struct dwarf2_per_objfile *dwarf2_per_objfile;
222
223 /* Default names of the debugging sections.  */
224
225 /* Note that if the debugging section has been compressed, it might
226    have a name like .zdebug_info.  */
227
228 static const struct dwarf2_debug_sections dwarf2_elf_names =
229 {
230   { ".debug_info", ".zdebug_info" },
231   { ".debug_abbrev", ".zdebug_abbrev" },
232   { ".debug_line", ".zdebug_line" },
233   { ".debug_loc", ".zdebug_loc" },
234   { ".debug_macinfo", ".zdebug_macinfo" },
235   { ".debug_macro", ".zdebug_macro" },
236   { ".debug_str", ".zdebug_str" },
237   { ".debug_ranges", ".zdebug_ranges" },
238   { ".debug_types", ".zdebug_types" },
239   { ".debug_frame", ".zdebug_frame" },
240   { ".eh_frame", NULL },
241   { ".gdb_index", ".zgdb_index" },
242   23
243 };
244
245 /* local data types */
246
247 /* We hold several abbreviation tables in memory at the same time.  */
248 #ifndef ABBREV_HASH_SIZE
249 #define ABBREV_HASH_SIZE 121
250 #endif
251
252 /* The data in a compilation unit header, after target2host
253    translation, looks like this.  */
254 struct comp_unit_head
255 {
256   unsigned int length;
257   short version;
258   unsigned char addr_size;
259   unsigned char signed_addr_p;
260   unsigned int abbrev_offset;
261
262   /* Size of file offsets; either 4 or 8.  */
263   unsigned int offset_size;
264
265   /* Size of the length field; either 4 or 12.  */
266   unsigned int initial_length_size;
267
268   /* Offset to the first byte of this compilation unit header in the
269      .debug_info section, for resolving relative reference dies.  */
270   unsigned int offset;
271
272   /* Offset to first die in this cu from the start of the cu.
273      This will be the first byte following the compilation unit header.  */
274   unsigned int first_die_offset;
275 };
276
277 /* Type used for delaying computation of method physnames.
278    See comments for compute_delayed_physnames.  */
279 struct delayed_method_info
280 {
281   /* The type to which the method is attached, i.e., its parent class.  */
282   struct type *type;
283
284   /* The index of the method in the type's function fieldlists.  */
285   int fnfield_index;
286
287   /* The index of the method in the fieldlist.  */
288   int index;
289
290   /* The name of the DIE.  */
291   const char *name;
292
293   /*  The DIE associated with this method.  */
294   struct die_info *die;
295 };
296
297 typedef struct delayed_method_info delayed_method_info;
298 DEF_VEC_O (delayed_method_info);
299
300 /* Internal state when decoding a particular compilation unit.  */
301 struct dwarf2_cu
302 {
303   /* The objfile containing this compilation unit.  */
304   struct objfile *objfile;
305
306   /* The header of the compilation unit.  */
307   struct comp_unit_head header;
308
309   /* Base address of this compilation unit.  */
310   CORE_ADDR base_address;
311
312   /* Non-zero if base_address has been set.  */
313   int base_known;
314
315   /* The language we are debugging.  */
316   enum language language;
317   const struct language_defn *language_defn;
318
319   const char *producer;
320
321   /* The generic symbol table building routines have separate lists for
322      file scope symbols and all all other scopes (local scopes).  So
323      we need to select the right one to pass to add_symbol_to_list().
324      We do it by keeping a pointer to the correct list in list_in_scope.
325
326      FIXME: The original dwarf code just treated the file scope as the
327      first local scope, and all other local scopes as nested local
328      scopes, and worked fine.  Check to see if we really need to
329      distinguish these in buildsym.c.  */
330   struct pending **list_in_scope;
331
332   /* DWARF abbreviation table associated with this compilation unit.  */
333   struct abbrev_info **dwarf2_abbrevs;
334
335   /* Storage for the abbrev table.  */
336   struct obstack abbrev_obstack;
337
338   /* Hash table holding all the loaded partial DIEs.  */
339   htab_t partial_dies;
340
341   /* Storage for things with the same lifetime as this read-in compilation
342      unit, including partial DIEs.  */
343   struct obstack comp_unit_obstack;
344
345   /* When multiple dwarf2_cu structures are living in memory, this field
346      chains them all together, so that they can be released efficiently.
347      We will probably also want a generation counter so that most-recently-used
348      compilation units are cached...  */
349   struct dwarf2_per_cu_data *read_in_chain;
350
351   /* Backchain to our per_cu entry if the tree has been built.  */
352   struct dwarf2_per_cu_data *per_cu;
353
354   /* How many compilation units ago was this CU last referenced?  */
355   int last_used;
356
357   /* A hash table of die offsets for following references.  */
358   htab_t die_hash;
359
360   /* Full DIEs if read in.  */
361   struct die_info *dies;
362
363   /* A set of pointers to dwarf2_per_cu_data objects for compilation
364      units referenced by this one.  Only set during full symbol processing;
365      partial symbol tables do not have dependencies.  */
366   htab_t dependencies;
367
368   /* Header data from the line table, during full symbol processing.  */
369   struct line_header *line_header;
370
371   /* A list of methods which need to have physnames computed
372      after all type information has been read.  */
373   VEC (delayed_method_info) *method_list;
374
375   /* To be copied to symtab->call_site_htab.  */
376   htab_t call_site_htab;
377
378   /* Mark used when releasing cached dies.  */
379   unsigned int mark : 1;
380
381   /* This flag will be set if this compilation unit might include
382      inter-compilation-unit references.  */
383   unsigned int has_form_ref_addr : 1;
384
385   /* This flag will be set if this compilation unit includes any
386      DW_TAG_namespace DIEs.  If we know that there are explicit
387      DIEs for namespaces, we don't need to try to infer them
388      from mangled names.  */
389   unsigned int has_namespace_info : 1;
390
391   /* This CU references .debug_loc.  See the symtab->locations_valid field.
392      This test is imperfect as there may exist optimized debug code not using
393      any location list and still facing inlining issues if handled as
394      unoptimized code.  For a future better test see GCC PR other/32998.  */
395   unsigned int has_loclist : 1;
396 };
397
398 /* Persistent data held for a compilation unit, even when not
399    processing it.  We put a pointer to this structure in the
400    read_symtab_private field of the psymtab.  */
401
402 struct dwarf2_per_cu_data
403 {
404   /* The start offset and length of this compilation unit.  2**29-1
405      bytes should suffice to store the length of any compilation unit
406      - if it doesn't, GDB will fall over anyway.
407      NOTE: Unlike comp_unit_head.length, this length includes
408      initial_length_size.  */
409   unsigned int offset;
410   unsigned int length : 29;
411
412   /* Flag indicating this compilation unit will be read in before
413      any of the current compilation units are processed.  */
414   unsigned int queued : 1;
415
416   /* This flag will be set if we need to load absolutely all DIEs
417      for this compilation unit, instead of just the ones we think
418      are interesting.  It gets set if we look for a DIE in the
419      hash table and don't find it.  */
420   unsigned int load_all_dies : 1;
421
422   /* Non-null if this CU is from .debug_types; in which case it points
423      to the section.  Otherwise it's from .debug_info.  */
424   struct dwarf2_section_info *debug_types_section;
425
426   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
427      of the CU cache it gets reset to NULL again.  */
428   struct dwarf2_cu *cu;
429
430   /* The corresponding objfile.
431      Normally we can get the objfile from dwarf2_per_objfile.
432      However we can enter this file with just a "per_cu" handle.  */
433   struct objfile *objfile;
434
435   /* When using partial symbol tables, the 'psymtab' field is active.
436      Otherwise the 'quick' field is active.  */
437   union
438   {
439     /* The partial symbol table associated with this compilation unit,
440        or NULL for partial units (which do not have an associated
441        symtab).  */
442     struct partial_symtab *psymtab;
443
444     /* Data needed by the "quick" functions.  */
445     struct dwarf2_per_cu_quick_data *quick;
446   } v;
447 };
448
449 /* Entry in the signatured_types hash table.  */
450
451 struct signatured_type
452 {
453   ULONGEST signature;
454
455   /* Offset in .debug_types of the type defined by this TU.  */
456   unsigned int type_offset;
457
458   /* The CU(/TU) of this type.  */
459   struct dwarf2_per_cu_data per_cu;
460 };
461
462 /* Struct used to pass misc. parameters to read_die_and_children, et
463    al.  which are used for both .debug_info and .debug_types dies.
464    All parameters here are unchanging for the life of the call.  This
465    struct exists to abstract away the constant parameters of die
466    reading.  */
467
468 struct die_reader_specs
469 {
470   /* The bfd of this objfile.  */
471   bfd* abfd;
472
473   /* The CU of the DIE we are parsing.  */
474   struct dwarf2_cu *cu;
475
476   /* Pointer to start of section buffer.
477      This is either the start of .debug_info or .debug_types.  */
478   const gdb_byte *buffer;
479 };
480
481 /* The line number information for a compilation unit (found in the
482    .debug_line section) begins with a "statement program header",
483    which contains the following information.  */
484 struct line_header
485 {
486   unsigned int total_length;
487   unsigned short version;
488   unsigned int header_length;
489   unsigned char minimum_instruction_length;
490   unsigned char maximum_ops_per_instruction;
491   unsigned char default_is_stmt;
492   int line_base;
493   unsigned char line_range;
494   unsigned char opcode_base;
495
496   /* standard_opcode_lengths[i] is the number of operands for the
497      standard opcode whose value is i.  This means that
498      standard_opcode_lengths[0] is unused, and the last meaningful
499      element is standard_opcode_lengths[opcode_base - 1].  */
500   unsigned char *standard_opcode_lengths;
501
502   /* The include_directories table.  NOTE!  These strings are not
503      allocated with xmalloc; instead, they are pointers into
504      debug_line_buffer.  If you try to free them, `free' will get
505      indigestion.  */
506   unsigned int num_include_dirs, include_dirs_size;
507   char **include_dirs;
508
509   /* The file_names table.  NOTE!  These strings are not allocated
510      with xmalloc; instead, they are pointers into debug_line_buffer.
511      Don't try to free them directly.  */
512   unsigned int num_file_names, file_names_size;
513   struct file_entry
514   {
515     char *name;
516     unsigned int dir_index;
517     unsigned int mod_time;
518     unsigned int length;
519     int included_p; /* Non-zero if referenced by the Line Number Program.  */
520     struct symtab *symtab; /* The associated symbol table, if any.  */
521   } *file_names;
522
523   /* The start and end of the statement program following this
524      header.  These point into dwarf2_per_objfile->line_buffer.  */
525   gdb_byte *statement_program_start, *statement_program_end;
526 };
527
528 /* When we construct a partial symbol table entry we only
529    need this much information.  */
530 struct partial_die_info
531   {
532     /* Offset of this DIE.  */
533     unsigned int offset;
534
535     /* DWARF-2 tag for this DIE.  */
536     ENUM_BITFIELD(dwarf_tag) tag : 16;
537
538     /* Assorted flags describing the data found in this DIE.  */
539     unsigned int has_children : 1;
540     unsigned int is_external : 1;
541     unsigned int is_declaration : 1;
542     unsigned int has_type : 1;
543     unsigned int has_specification : 1;
544     unsigned int has_pc_info : 1;
545
546     /* Flag set if the SCOPE field of this structure has been
547        computed.  */
548     unsigned int scope_set : 1;
549
550     /* Flag set if the DIE has a byte_size attribute.  */
551     unsigned int has_byte_size : 1;
552
553     /* Flag set if any of the DIE's children are template arguments.  */
554     unsigned int has_template_arguments : 1;
555
556     /* Flag set if fixup_partial_die has been called on this die.  */
557     unsigned int fixup_called : 1;
558
559     /* The name of this DIE.  Normally the value of DW_AT_name, but
560        sometimes a default name for unnamed DIEs.  */
561     char *name;
562
563     /* The linkage name, if present.  */
564     const char *linkage_name;
565
566     /* The scope to prepend to our children.  This is generally
567        allocated on the comp_unit_obstack, so will disappear
568        when this compilation unit leaves the cache.  */
569     char *scope;
570
571     /* The location description associated with this DIE, if any.  */
572     struct dwarf_block *locdesc;
573
574     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
575     CORE_ADDR lowpc;
576     CORE_ADDR highpc;
577
578     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
579        DW_AT_sibling, if any.  */
580     /* NOTE: This member isn't strictly necessary, read_partial_die could
581        return DW_AT_sibling values to its caller load_partial_dies.  */
582     gdb_byte *sibling;
583
584     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
585        DW_AT_specification (or DW_AT_abstract_origin or
586        DW_AT_extension).  */
587     unsigned int spec_offset;
588
589     /* Pointers to this DIE's parent, first child, and next sibling,
590        if any.  */
591     struct partial_die_info *die_parent, *die_child, *die_sibling;
592   };
593
594 /* This data structure holds the information of an abbrev.  */
595 struct abbrev_info
596   {
597     unsigned int number;        /* number identifying abbrev */
598     enum dwarf_tag tag;         /* dwarf tag */
599     unsigned short has_children;                /* boolean */
600     unsigned short num_attrs;   /* number of attributes */
601     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
602     struct abbrev_info *next;   /* next in chain */
603   };
604
605 struct attr_abbrev
606   {
607     ENUM_BITFIELD(dwarf_attribute) name : 16;
608     ENUM_BITFIELD(dwarf_form) form : 16;
609   };
610
611 /* Attributes have a name and a value.  */
612 struct attribute
613   {
614     ENUM_BITFIELD(dwarf_attribute) name : 16;
615     ENUM_BITFIELD(dwarf_form) form : 15;
616
617     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
618        field should be in u.str (existing only for DW_STRING) but it is kept
619        here for better struct attribute alignment.  */
620     unsigned int string_is_canonical : 1;
621
622     union
623       {
624         char *str;
625         struct dwarf_block *blk;
626         ULONGEST unsnd;
627         LONGEST snd;
628         CORE_ADDR addr;
629         struct signatured_type *signatured_type;
630       }
631     u;
632   };
633
634 /* This data structure holds a complete die structure.  */
635 struct die_info
636   {
637     /* DWARF-2 tag for this DIE.  */
638     ENUM_BITFIELD(dwarf_tag) tag : 16;
639
640     /* Number of attributes */
641     unsigned char num_attrs;
642
643     /* True if we're presently building the full type name for the
644        type derived from this DIE.  */
645     unsigned char building_fullname : 1;
646
647     /* Abbrev number */
648     unsigned int abbrev;
649
650     /* Offset in .debug_info or .debug_types section.  */
651     unsigned int offset;
652
653     /* The dies in a compilation unit form an n-ary tree.  PARENT
654        points to this die's parent; CHILD points to the first child of
655        this node; and all the children of a given node are chained
656        together via their SIBLING fields.  */
657     struct die_info *child;     /* Its first child, if any.  */
658     struct die_info *sibling;   /* Its next sibling, if any.  */
659     struct die_info *parent;    /* Its parent, if any.  */
660
661     /* An array of attributes, with NUM_ATTRS elements.  There may be
662        zero, but it's not common and zero-sized arrays are not
663        sufficiently portable C.  */
664     struct attribute attrs[1];
665   };
666
667 /* Get at parts of an attribute structure.  */
668
669 #define DW_STRING(attr)    ((attr)->u.str)
670 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
671 #define DW_UNSND(attr)     ((attr)->u.unsnd)
672 #define DW_BLOCK(attr)     ((attr)->u.blk)
673 #define DW_SND(attr)       ((attr)->u.snd)
674 #define DW_ADDR(attr)      ((attr)->u.addr)
675 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
676
677 /* Blocks are a bunch of untyped bytes.  */
678 struct dwarf_block
679   {
680     unsigned int size;
681
682     /* Valid only if SIZE is not zero.  */
683     gdb_byte *data;
684   };
685
686 #ifndef ATTR_ALLOC_CHUNK
687 #define ATTR_ALLOC_CHUNK 4
688 #endif
689
690 /* Allocate fields for structs, unions and enums in this size.  */
691 #ifndef DW_FIELD_ALLOC_CHUNK
692 #define DW_FIELD_ALLOC_CHUNK 4
693 #endif
694
695 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
696    but this would require a corresponding change in unpack_field_as_long
697    and friends.  */
698 static int bits_per_byte = 8;
699
700 /* The routines that read and process dies for a C struct or C++ class
701    pass lists of data member fields and lists of member function fields
702    in an instance of a field_info structure, as defined below.  */
703 struct field_info
704   {
705     /* List of data member and baseclasses fields.  */
706     struct nextfield
707       {
708         struct nextfield *next;
709         int accessibility;
710         int virtuality;
711         struct field field;
712       }
713      *fields, *baseclasses;
714
715     /* Number of fields (including baseclasses).  */
716     int nfields;
717
718     /* Number of baseclasses.  */
719     int nbaseclasses;
720
721     /* Set if the accesibility of one of the fields is not public.  */
722     int non_public_fields;
723
724     /* Member function fields array, entries are allocated in the order they
725        are encountered in the object file.  */
726     struct nextfnfield
727       {
728         struct nextfnfield *next;
729         struct fn_field fnfield;
730       }
731      *fnfields;
732
733     /* Member function fieldlist array, contains name of possibly overloaded
734        member function, number of overloaded member functions and a pointer
735        to the head of the member function field chain.  */
736     struct fnfieldlist
737       {
738         char *name;
739         int length;
740         struct nextfnfield *head;
741       }
742      *fnfieldlists;
743
744     /* Number of entries in the fnfieldlists array.  */
745     int nfnfields;
746
747     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
748        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
749     struct typedef_field_list
750       {
751         struct typedef_field field;
752         struct typedef_field_list *next;
753       }
754     *typedef_field_list;
755     unsigned typedef_field_list_count;
756   };
757
758 /* One item on the queue of compilation units to read in full symbols
759    for.  */
760 struct dwarf2_queue_item
761 {
762   struct dwarf2_per_cu_data *per_cu;
763   struct dwarf2_queue_item *next;
764 };
765
766 /* The current queue.  */
767 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
768
769 /* Loaded secondary compilation units are kept in memory until they
770    have not been referenced for the processing of this many
771    compilation units.  Set this to zero to disable caching.  Cache
772    sizes of up to at least twenty will improve startup time for
773    typical inter-CU-reference binaries, at an obvious memory cost.  */
774 static int dwarf2_max_cache_age = 5;
775 static void
776 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
777                            struct cmd_list_element *c, const char *value)
778 {
779   fprintf_filtered (file, _("The upper bound on the age of cached "
780                             "dwarf2 compilation units is %s.\n"),
781                     value);
782 }
783
784
785 /* Various complaints about symbol reading that don't abort the process.  */
786
787 static void
788 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
789 {
790   complaint (&symfile_complaints,
791              _("statement list doesn't fit in .debug_line section"));
792 }
793
794 static void
795 dwarf2_debug_line_missing_file_complaint (void)
796 {
797   complaint (&symfile_complaints,
798              _(".debug_line section has line data without a file"));
799 }
800
801 static void
802 dwarf2_debug_line_missing_end_sequence_complaint (void)
803 {
804   complaint (&symfile_complaints,
805              _(".debug_line section has line "
806                "program sequence without an end"));
807 }
808
809 static void
810 dwarf2_complex_location_expr_complaint (void)
811 {
812   complaint (&symfile_complaints, _("location expression too complex"));
813 }
814
815 static void
816 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
817                                               int arg3)
818 {
819   complaint (&symfile_complaints,
820              _("const value length mismatch for '%s', got %d, expected %d"),
821              arg1, arg2, arg3);
822 }
823
824 static void
825 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
826 {
827   complaint (&symfile_complaints,
828              _("macro info runs off end of `%s' section"),
829              section->asection->name);
830 }
831
832 static void
833 dwarf2_macro_malformed_definition_complaint (const char *arg1)
834 {
835   complaint (&symfile_complaints,
836              _("macro debug info contains a "
837                "malformed macro definition:\n`%s'"),
838              arg1);
839 }
840
841 static void
842 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
843 {
844   complaint (&symfile_complaints,
845              _("invalid attribute class or form for '%s' in '%s'"),
846              arg1, arg2);
847 }
848
849 /* local function prototypes */
850
851 static void dwarf2_locate_sections (bfd *, asection *, void *);
852
853 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
854                                            struct objfile *);
855
856 static void dwarf2_find_base_address (struct die_info *die,
857                                       struct dwarf2_cu *cu);
858
859 static void dwarf2_build_psymtabs_hard (struct objfile *);
860
861 static void scan_partial_symbols (struct partial_die_info *,
862                                   CORE_ADDR *, CORE_ADDR *,
863                                   int, struct dwarf2_cu *);
864
865 static void add_partial_symbol (struct partial_die_info *,
866                                 struct dwarf2_cu *);
867
868 static void add_partial_namespace (struct partial_die_info *pdi,
869                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
870                                    int need_pc, struct dwarf2_cu *cu);
871
872 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
873                                 CORE_ADDR *highpc, int need_pc,
874                                 struct dwarf2_cu *cu);
875
876 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
877                                      struct dwarf2_cu *cu);
878
879 static void add_partial_subprogram (struct partial_die_info *pdi,
880                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
881                                     int need_pc, struct dwarf2_cu *cu);
882
883 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
884                                      gdb_byte *buffer, gdb_byte *info_ptr,
885                                      bfd *abfd, struct dwarf2_cu *cu);
886
887 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
888
889 static void psymtab_to_symtab_1 (struct partial_symtab *);
890
891 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
892
893 static void dwarf2_free_abbrev_table (void *);
894
895 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
896
897 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
898                                             struct dwarf2_cu *);
899
900 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
901                                                  struct dwarf2_cu *);
902
903 static struct partial_die_info *load_partial_dies (bfd *,
904                                                    gdb_byte *, gdb_byte *,
905                                                    int, struct dwarf2_cu *);
906
907 static gdb_byte *read_partial_die (struct partial_die_info *,
908                                    struct abbrev_info *abbrev,
909                                    unsigned int, bfd *,
910                                    gdb_byte *, gdb_byte *,
911                                    struct dwarf2_cu *);
912
913 static struct partial_die_info *find_partial_die (unsigned int,
914                                                   struct dwarf2_cu *);
915
916 static void fixup_partial_die (struct partial_die_info *,
917                                struct dwarf2_cu *);
918
919 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
920                                  bfd *, gdb_byte *, struct dwarf2_cu *);
921
922 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
923                                        bfd *, gdb_byte *, struct dwarf2_cu *);
924
925 static unsigned int read_1_byte (bfd *, gdb_byte *);
926
927 static int read_1_signed_byte (bfd *, gdb_byte *);
928
929 static unsigned int read_2_bytes (bfd *, gdb_byte *);
930
931 static unsigned int read_4_bytes (bfd *, gdb_byte *);
932
933 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
934
935 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
936                                unsigned int *);
937
938 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
939
940 static LONGEST read_checked_initial_length_and_offset
941   (bfd *, gdb_byte *, const struct comp_unit_head *,
942    unsigned int *, unsigned int *);
943
944 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
945                             unsigned int *);
946
947 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
948
949 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
950
951 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
952
953 static char *read_indirect_string (bfd *, gdb_byte *,
954                                    const struct comp_unit_head *,
955                                    unsigned int *);
956
957 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
958
959 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
960
961 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
962
963 static void set_cu_language (unsigned int, struct dwarf2_cu *);
964
965 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
966                                       struct dwarf2_cu *);
967
968 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
969                                                 unsigned int,
970                                                 struct dwarf2_cu *);
971
972 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
973                                struct dwarf2_cu *cu);
974
975 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
976
977 static struct die_info *die_specification (struct die_info *die,
978                                            struct dwarf2_cu **);
979
980 static void free_line_header (struct line_header *lh);
981
982 static void add_file_name (struct line_header *, char *, unsigned int,
983                            unsigned int, unsigned int);
984
985 static struct line_header *(dwarf_decode_line_header
986                             (unsigned int offset,
987                              bfd *abfd, struct dwarf2_cu *cu));
988
989 static void dwarf_decode_lines (struct line_header *, const char *,
990                                 struct dwarf2_cu *, struct partial_symtab *,
991                                 int);
992
993 static void dwarf2_start_subfile (char *, const char *, const char *);
994
995 static struct symbol *new_symbol (struct die_info *, struct type *,
996                                   struct dwarf2_cu *);
997
998 static struct symbol *new_symbol_full (struct die_info *, struct type *,
999                                        struct dwarf2_cu *, struct symbol *);
1000
1001 static void dwarf2_const_value (struct attribute *, struct symbol *,
1002                                 struct dwarf2_cu *);
1003
1004 static void dwarf2_const_value_attr (struct attribute *attr,
1005                                      struct type *type,
1006                                      const char *name,
1007                                      struct obstack *obstack,
1008                                      struct dwarf2_cu *cu, long *value,
1009                                      gdb_byte **bytes,
1010                                      struct dwarf2_locexpr_baton **baton);
1011
1012 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1013
1014 static int need_gnat_info (struct dwarf2_cu *);
1015
1016 static struct type *die_descriptive_type (struct die_info *,
1017                                           struct dwarf2_cu *);
1018
1019 static void set_descriptive_type (struct type *, struct die_info *,
1020                                   struct dwarf2_cu *);
1021
1022 static struct type *die_containing_type (struct die_info *,
1023                                          struct dwarf2_cu *);
1024
1025 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1026                                      struct dwarf2_cu *);
1027
1028 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1029
1030 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1031
1032 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1033
1034 static char *typename_concat (struct obstack *obs, const char *prefix,
1035                               const char *suffix, int physname,
1036                               struct dwarf2_cu *cu);
1037
1038 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1039
1040 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1041
1042 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1043
1044 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1045
1046 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1047
1048 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1049                                struct dwarf2_cu *, struct partial_symtab *);
1050
1051 static int dwarf2_get_pc_bounds (struct die_info *,
1052                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1053                                  struct partial_symtab *);
1054
1055 static void get_scope_pc_bounds (struct die_info *,
1056                                  CORE_ADDR *, CORE_ADDR *,
1057                                  struct dwarf2_cu *);
1058
1059 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1060                                         CORE_ADDR, struct dwarf2_cu *);
1061
1062 static void dwarf2_add_field (struct field_info *, struct die_info *,
1063                               struct dwarf2_cu *);
1064
1065 static void dwarf2_attach_fields_to_type (struct field_info *,
1066                                           struct type *, struct dwarf2_cu *);
1067
1068 static void dwarf2_add_member_fn (struct field_info *,
1069                                   struct die_info *, struct type *,
1070                                   struct dwarf2_cu *);
1071
1072 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1073                                              struct type *,
1074                                              struct dwarf2_cu *);
1075
1076 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1077
1078 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1079
1080 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1081
1082 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1083
1084 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1085
1086 static struct type *read_module_type (struct die_info *die,
1087                                       struct dwarf2_cu *cu);
1088
1089 static const char *namespace_name (struct die_info *die,
1090                                    int *is_anonymous, struct dwarf2_cu *);
1091
1092 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1093
1094 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1095
1096 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1097                                                        struct dwarf2_cu *);
1098
1099 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1100
1101 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1102                                                  gdb_byte *info_ptr,
1103                                                  gdb_byte **new_info_ptr,
1104                                                  struct die_info *parent);
1105
1106 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1107                                                gdb_byte *info_ptr,
1108                                                gdb_byte **new_info_ptr,
1109                                                struct die_info *parent);
1110
1111 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1112                                                gdb_byte *info_ptr,
1113                                                gdb_byte **new_info_ptr,
1114                                                struct die_info *parent);
1115
1116 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1117                                 struct die_info **, gdb_byte *,
1118                                 int *);
1119
1120 static void process_die (struct die_info *, struct dwarf2_cu *);
1121
1122 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1123                                        struct obstack *);
1124
1125 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1126
1127 static const char *dwarf2_full_name (char *name,
1128                                      struct die_info *die,
1129                                      struct dwarf2_cu *cu);
1130
1131 static struct die_info *dwarf2_extension (struct die_info *die,
1132                                           struct dwarf2_cu **);
1133
1134 static char *dwarf_tag_name (unsigned int);
1135
1136 static char *dwarf_attr_name (unsigned int);
1137
1138 static char *dwarf_form_name (unsigned int);
1139
1140 static char *dwarf_bool_name (unsigned int);
1141
1142 static char *dwarf_type_encoding_name (unsigned int);
1143
1144 #if 0
1145 static char *dwarf_cfi_name (unsigned int);
1146 #endif
1147
1148 static struct die_info *sibling_die (struct die_info *);
1149
1150 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1151
1152 static void dump_die_for_error (struct die_info *);
1153
1154 static void dump_die_1 (struct ui_file *, int level, int max_level,
1155                         struct die_info *);
1156
1157 /*static*/ void dump_die (struct die_info *, int max_level);
1158
1159 static void store_in_ref_table (struct die_info *,
1160                                 struct dwarf2_cu *);
1161
1162 static int is_ref_attr (struct attribute *);
1163
1164 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1165
1166 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1167
1168 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1169                                                struct attribute *,
1170                                                struct dwarf2_cu **);
1171
1172 static struct die_info *follow_die_ref (struct die_info *,
1173                                         struct attribute *,
1174                                         struct dwarf2_cu **);
1175
1176 static struct die_info *follow_die_sig (struct die_info *,
1177                                         struct attribute *,
1178                                         struct dwarf2_cu **);
1179
1180 static struct signatured_type *lookup_signatured_type_at_offset
1181     (struct objfile *objfile,
1182      struct dwarf2_section_info *section,
1183      unsigned int offset);
1184
1185 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1186
1187 static void read_signatured_type (struct signatured_type *type_sig);
1188
1189 /* memory allocation interface */
1190
1191 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1192
1193 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1194
1195 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1196
1197 static void dwarf_decode_macros (struct line_header *, unsigned int,
1198                                  char *, bfd *, struct dwarf2_cu *,
1199                                  struct dwarf2_section_info *,
1200                                  int);
1201
1202 static int attr_form_is_block (struct attribute *);
1203
1204 static int attr_form_is_section_offset (struct attribute *);
1205
1206 static int attr_form_is_constant (struct attribute *);
1207
1208 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1209                                    struct dwarf2_loclist_baton *baton,
1210                                    struct attribute *attr);
1211
1212 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1213                                          struct symbol *sym,
1214                                          struct dwarf2_cu *cu);
1215
1216 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1217                                struct abbrev_info *abbrev,
1218                                struct dwarf2_cu *cu);
1219
1220 static void free_stack_comp_unit (void *);
1221
1222 static hashval_t partial_die_hash (const void *item);
1223
1224 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1225
1226 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1227   (unsigned int offset, struct objfile *objfile);
1228
1229 static void init_one_comp_unit (struct dwarf2_cu *cu,
1230                                 struct dwarf2_per_cu_data *per_cu);
1231
1232 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1233                                    struct die_info *comp_unit_die);
1234
1235 static void free_heap_comp_unit (void *);
1236
1237 static void free_cached_comp_units (void *);
1238
1239 static void age_cached_comp_units (void);
1240
1241 static void free_one_cached_comp_unit (void *);
1242
1243 static struct type *set_die_type (struct die_info *, struct type *,
1244                                   struct dwarf2_cu *);
1245
1246 static void create_all_comp_units (struct objfile *);
1247
1248 static int create_debug_types_hash_table (struct objfile *objfile);
1249
1250 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1251
1252 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1253
1254 static void dwarf2_add_dependence (struct dwarf2_cu *,
1255                                    struct dwarf2_per_cu_data *);
1256
1257 static void dwarf2_mark (struct dwarf2_cu *);
1258
1259 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1260
1261 static struct type *get_die_type_at_offset (unsigned int,
1262                                             struct dwarf2_per_cu_data *per_cu);
1263
1264 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1265
1266 static void dwarf2_release_queue (void *dummy);
1267
1268 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1269
1270 static void process_queue (void);
1271
1272 static void find_file_and_directory (struct die_info *die,
1273                                      struct dwarf2_cu *cu,
1274                                      char **name, char **comp_dir);
1275
1276 static char *file_full_name (int file, struct line_header *lh,
1277                              const char *comp_dir);
1278
1279 static gdb_byte *read_and_check_comp_unit_head
1280   (struct comp_unit_head *header,
1281    struct dwarf2_section_info *section, gdb_byte *info_ptr,
1282    int is_debug_types_section);
1283
1284 static void init_cu_die_reader (struct die_reader_specs *reader,
1285                                 struct dwarf2_cu *cu);
1286
1287 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1288
1289 #if WORDS_BIGENDIAN
1290
1291 /* Convert VALUE between big- and little-endian.  */
1292 static offset_type
1293 byte_swap (offset_type value)
1294 {
1295   offset_type result;
1296
1297   result = (value & 0xff) << 24;
1298   result |= (value & 0xff00) << 8;
1299   result |= (value & 0xff0000) >> 8;
1300   result |= (value & 0xff000000) >> 24;
1301   return result;
1302 }
1303
1304 #define MAYBE_SWAP(V)  byte_swap (V)
1305
1306 #else
1307 #define MAYBE_SWAP(V) (V)
1308 #endif /* WORDS_BIGENDIAN */
1309
1310 /* The suffix for an index file.  */
1311 #define INDEX_SUFFIX ".gdb-index"
1312
1313 static const char *dwarf2_physname (char *name, struct die_info *die,
1314                                     struct dwarf2_cu *cu);
1315
1316 /* Try to locate the sections we need for DWARF 2 debugging
1317    information and return true if we have enough to do something.
1318    NAMES points to the dwarf2 section names, or is NULL if the standard
1319    ELF names are used.  */
1320
1321 int
1322 dwarf2_has_info (struct objfile *objfile,
1323                  const struct dwarf2_debug_sections *names)
1324 {
1325   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1326   if (!dwarf2_per_objfile)
1327     {
1328       /* Initialize per-objfile state.  */
1329       struct dwarf2_per_objfile *data
1330         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1331
1332       memset (data, 0, sizeof (*data));
1333       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1334       dwarf2_per_objfile = data;
1335
1336       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1337                              (void *) names);
1338       dwarf2_per_objfile->objfile = objfile;
1339     }
1340   return (dwarf2_per_objfile->info.asection != NULL
1341           && dwarf2_per_objfile->abbrev.asection != NULL);
1342 }
1343
1344 /* When loading sections, we look either for uncompressed section or for
1345    compressed section names.  */
1346
1347 static int
1348 section_is_p (const char *section_name,
1349               const struct dwarf2_section_names *names)
1350 {
1351   if (names->normal != NULL
1352       && strcmp (section_name, names->normal) == 0)
1353     return 1;
1354   if (names->compressed != NULL
1355       && strcmp (section_name, names->compressed) == 0)
1356     return 1;
1357   return 0;
1358 }
1359
1360 /* This function is mapped across the sections and remembers the
1361    offset and size of each of the debugging sections we are interested
1362    in.  */
1363
1364 static void
1365 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1366 {
1367   const struct dwarf2_debug_sections *names;
1368
1369   if (vnames == NULL)
1370     names = &dwarf2_elf_names;
1371   else
1372     names = (const struct dwarf2_debug_sections *) vnames;
1373
1374   if (section_is_p (sectp->name, &names->info))
1375     {
1376       dwarf2_per_objfile->info.asection = sectp;
1377       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1378     }
1379   else if (section_is_p (sectp->name, &names->abbrev))
1380     {
1381       dwarf2_per_objfile->abbrev.asection = sectp;
1382       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1383     }
1384   else if (section_is_p (sectp->name, &names->line))
1385     {
1386       dwarf2_per_objfile->line.asection = sectp;
1387       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1388     }
1389   else if (section_is_p (sectp->name, &names->loc))
1390     {
1391       dwarf2_per_objfile->loc.asection = sectp;
1392       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1393     }
1394   else if (section_is_p (sectp->name, &names->macinfo))
1395     {
1396       dwarf2_per_objfile->macinfo.asection = sectp;
1397       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1398     }
1399   else if (section_is_p (sectp->name, &names->macro))
1400     {
1401       dwarf2_per_objfile->macro.asection = sectp;
1402       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1403     }
1404   else if (section_is_p (sectp->name, &names->str))
1405     {
1406       dwarf2_per_objfile->str.asection = sectp;
1407       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1408     }
1409   else if (section_is_p (sectp->name, &names->frame))
1410     {
1411       dwarf2_per_objfile->frame.asection = sectp;
1412       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1413     }
1414   else if (section_is_p (sectp->name, &names->eh_frame))
1415     {
1416       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1417
1418       if (aflag & SEC_HAS_CONTENTS)
1419         {
1420           dwarf2_per_objfile->eh_frame.asection = sectp;
1421           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1422         }
1423     }
1424   else if (section_is_p (sectp->name, &names->ranges))
1425     {
1426       dwarf2_per_objfile->ranges.asection = sectp;
1427       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1428     }
1429   else if (section_is_p (sectp->name, &names->types))
1430     {
1431       struct dwarf2_section_info type_section;
1432
1433       memset (&type_section, 0, sizeof (type_section));
1434       type_section.asection = sectp;
1435       type_section.size = bfd_get_section_size (sectp);
1436
1437       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1438                      &type_section);
1439     }
1440   else if (section_is_p (sectp->name, &names->gdb_index))
1441     {
1442       dwarf2_per_objfile->gdb_index.asection = sectp;
1443       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1444     }
1445
1446   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1447       && bfd_section_vma (abfd, sectp) == 0)
1448     dwarf2_per_objfile->has_section_at_zero = 1;
1449 }
1450
1451 /* Decompress a section that was compressed using zlib.  Store the
1452    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1453
1454 static void
1455 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1456                          gdb_byte **outbuf, bfd_size_type *outsize)
1457 {
1458   bfd *abfd = objfile->obfd;
1459 #ifndef HAVE_ZLIB_H
1460   error (_("Support for zlib-compressed DWARF data (from '%s') "
1461            "is disabled in this copy of GDB"),
1462          bfd_get_filename (abfd));
1463 #else
1464   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1465   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1466   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1467   bfd_size_type uncompressed_size;
1468   gdb_byte *uncompressed_buffer;
1469   z_stream strm;
1470   int rc;
1471   int header_size = 12;
1472
1473   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1474       || bfd_bread (compressed_buffer,
1475                     compressed_size, abfd) != compressed_size)
1476     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1477            bfd_get_filename (abfd));
1478
1479   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1480      by the uncompressed section size, 8 bytes in big-endian order.  */
1481   if (compressed_size < header_size
1482       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1483     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1484            bfd_get_filename (abfd));
1485   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1486   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1487   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1488   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1489   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1490   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1491   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1492   uncompressed_size += compressed_buffer[11];
1493
1494   /* It is possible the section consists of several compressed
1495      buffers concatenated together, so we uncompress in a loop.  */
1496   strm.zalloc = NULL;
1497   strm.zfree = NULL;
1498   strm.opaque = NULL;
1499   strm.avail_in = compressed_size - header_size;
1500   strm.next_in = (Bytef*) compressed_buffer + header_size;
1501   strm.avail_out = uncompressed_size;
1502   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1503                                        uncompressed_size);
1504   rc = inflateInit (&strm);
1505   while (strm.avail_in > 0)
1506     {
1507       if (rc != Z_OK)
1508         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1509                bfd_get_filename (abfd), rc);
1510       strm.next_out = ((Bytef*) uncompressed_buffer
1511                        + (uncompressed_size - strm.avail_out));
1512       rc = inflate (&strm, Z_FINISH);
1513       if (rc != Z_STREAM_END)
1514         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1515                bfd_get_filename (abfd), rc);
1516       rc = inflateReset (&strm);
1517     }
1518   rc = inflateEnd (&strm);
1519   if (rc != Z_OK
1520       || strm.avail_out != 0)
1521     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1522            bfd_get_filename (abfd), rc);
1523
1524   do_cleanups (cleanup);
1525   *outbuf = uncompressed_buffer;
1526   *outsize = uncompressed_size;
1527 #endif
1528 }
1529
1530 /* A helper function that decides whether a section is empty.  */
1531
1532 static int
1533 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1534 {
1535   return info->asection == NULL || info->size == 0;
1536 }
1537
1538 /* Read the contents of the section INFO from object file specified by
1539    OBJFILE, store info about the section into INFO.
1540    If the section is compressed, uncompress it before returning.  */
1541
1542 static void
1543 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1544 {
1545   bfd *abfd = objfile->obfd;
1546   asection *sectp = info->asection;
1547   gdb_byte *buf, *retbuf;
1548   unsigned char header[4];
1549
1550   if (info->readin)
1551     return;
1552   info->buffer = NULL;
1553   info->map_addr = NULL;
1554   info->readin = 1;
1555
1556   if (dwarf2_section_empty_p (info))
1557     return;
1558
1559   /* Check if the file has a 4-byte header indicating compression.  */
1560   if (info->size > sizeof (header)
1561       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1562       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1563     {
1564       /* Upon decompression, update the buffer and its size.  */
1565       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1566         {
1567           zlib_decompress_section (objfile, sectp, &info->buffer,
1568                                    &info->size);
1569           return;
1570         }
1571     }
1572
1573 #ifdef HAVE_MMAP
1574   if (pagesize == 0)
1575     pagesize = getpagesize ();
1576
1577   /* Only try to mmap sections which are large enough: we don't want to
1578      waste space due to fragmentation.  Also, only try mmap for sections
1579      without relocations.  */
1580
1581   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1582     {
1583       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1584                          MAP_PRIVATE, sectp->filepos,
1585                          &info->map_addr, &info->map_len);
1586
1587       if ((caddr_t)info->buffer != MAP_FAILED)
1588         {
1589 #if HAVE_POSIX_MADVISE
1590           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1591 #endif
1592           return;
1593         }
1594     }
1595 #endif
1596
1597   /* If we get here, we are a normal, not-compressed section.  */
1598   info->buffer = buf
1599     = obstack_alloc (&objfile->objfile_obstack, info->size);
1600
1601   /* When debugging .o files, we may need to apply relocations; see
1602      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1603      We never compress sections in .o files, so we only need to
1604      try this when the section is not compressed.  */
1605   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1606   if (retbuf != NULL)
1607     {
1608       info->buffer = retbuf;
1609       return;
1610     }
1611
1612   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1613       || bfd_bread (buf, info->size, abfd) != info->size)
1614     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1615            bfd_get_filename (abfd));
1616 }
1617
1618 /* A helper function that returns the size of a section in a safe way.
1619    If you are positive that the section has been read before using the
1620    size, then it is safe to refer to the dwarf2_section_info object's
1621    "size" field directly.  In other cases, you must call this
1622    function, because for compressed sections the size field is not set
1623    correctly until the section has been read.  */
1624
1625 static bfd_size_type
1626 dwarf2_section_size (struct objfile *objfile,
1627                      struct dwarf2_section_info *info)
1628 {
1629   if (!info->readin)
1630     dwarf2_read_section (objfile, info);
1631   return info->size;
1632 }
1633
1634 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1635    SECTION_NAME.  */
1636
1637 void
1638 dwarf2_get_section_info (struct objfile *objfile,
1639                          enum dwarf2_section_enum sect,
1640                          asection **sectp, gdb_byte **bufp,
1641                          bfd_size_type *sizep)
1642 {
1643   struct dwarf2_per_objfile *data
1644     = objfile_data (objfile, dwarf2_objfile_data_key);
1645   struct dwarf2_section_info *info;
1646
1647   /* We may see an objfile without any DWARF, in which case we just
1648      return nothing.  */
1649   if (data == NULL)
1650     {
1651       *sectp = NULL;
1652       *bufp = NULL;
1653       *sizep = 0;
1654       return;
1655     }
1656   switch (sect)
1657     {
1658     case DWARF2_DEBUG_FRAME:
1659       info = &data->frame;
1660       break;
1661     case DWARF2_EH_FRAME:
1662       info = &data->eh_frame;
1663       break;
1664     default:
1665       gdb_assert_not_reached ("unexpected section");
1666     }
1667
1668   dwarf2_read_section (objfile, info);
1669
1670   *sectp = info->asection;
1671   *bufp = info->buffer;
1672   *sizep = info->size;
1673 }
1674
1675 \f
1676 /* DWARF quick_symbols_functions support.  */
1677
1678 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1679    unique line tables, so we maintain a separate table of all .debug_line
1680    derived entries to support the sharing.
1681    All the quick functions need is the list of file names.  We discard the
1682    line_header when we're done and don't need to record it here.  */
1683 struct quick_file_names
1684 {
1685   /* The offset in .debug_line of the line table.  We hash on this.  */
1686   unsigned int offset;
1687
1688   /* The number of entries in file_names, real_names.  */
1689   unsigned int num_file_names;
1690
1691   /* The file names from the line table, after being run through
1692      file_full_name.  */
1693   const char **file_names;
1694
1695   /* The file names from the line table after being run through
1696      gdb_realpath.  These are computed lazily.  */
1697   const char **real_names;
1698 };
1699
1700 /* When using the index (and thus not using psymtabs), each CU has an
1701    object of this type.  This is used to hold information needed by
1702    the various "quick" methods.  */
1703 struct dwarf2_per_cu_quick_data
1704 {
1705   /* The file table.  This can be NULL if there was no file table
1706      or it's currently not read in.
1707      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1708   struct quick_file_names *file_names;
1709
1710   /* The corresponding symbol table.  This is NULL if symbols for this
1711      CU have not yet been read.  */
1712   struct symtab *symtab;
1713
1714   /* A temporary mark bit used when iterating over all CUs in
1715      expand_symtabs_matching.  */
1716   unsigned int mark : 1;
1717
1718   /* True if we've tried to read the file table and found there isn't one.
1719      There will be no point in trying to read it again next time.  */
1720   unsigned int no_file_data : 1;
1721 };
1722
1723 /* Hash function for a quick_file_names.  */
1724
1725 static hashval_t
1726 hash_file_name_entry (const void *e)
1727 {
1728   const struct quick_file_names *file_data = e;
1729
1730   return file_data->offset;
1731 }
1732
1733 /* Equality function for a quick_file_names.  */
1734
1735 static int
1736 eq_file_name_entry (const void *a, const void *b)
1737 {
1738   const struct quick_file_names *ea = a;
1739   const struct quick_file_names *eb = b;
1740
1741   return ea->offset == eb->offset;
1742 }
1743
1744 /* Delete function for a quick_file_names.  */
1745
1746 static void
1747 delete_file_name_entry (void *e)
1748 {
1749   struct quick_file_names *file_data = e;
1750   int i;
1751
1752   for (i = 0; i < file_data->num_file_names; ++i)
1753     {
1754       xfree ((void*) file_data->file_names[i]);
1755       if (file_data->real_names)
1756         xfree ((void*) file_data->real_names[i]);
1757     }
1758
1759   /* The space for the struct itself lives on objfile_obstack,
1760      so we don't free it here.  */
1761 }
1762
1763 /* Create a quick_file_names hash table.  */
1764
1765 static htab_t
1766 create_quick_file_names_table (unsigned int nr_initial_entries)
1767 {
1768   return htab_create_alloc (nr_initial_entries,
1769                             hash_file_name_entry, eq_file_name_entry,
1770                             delete_file_name_entry, xcalloc, xfree);
1771 }
1772
1773 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1774    have to be created afterwards.  You should call age_cached_comp_units after
1775    processing PER_CU->CU.  dw2_setup must have been already called.  */
1776
1777 static void
1778 load_cu (struct dwarf2_per_cu_data *per_cu)
1779 {
1780   if (per_cu->debug_types_section)
1781     load_full_type_unit (per_cu);
1782   else
1783     load_full_comp_unit (per_cu);
1784
1785   gdb_assert (per_cu->cu != NULL);
1786
1787   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1788 }
1789
1790 /* Read in the symbols for PER_CU.  */
1791
1792 static void
1793 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1794 {
1795   struct cleanup *back_to;
1796
1797   back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799   queue_comp_unit (per_cu);
1800
1801   load_cu (per_cu);
1802
1803   process_queue ();
1804
1805   /* Age the cache, releasing compilation units that have not
1806      been used recently.  */
1807   age_cached_comp_units ();
1808
1809   do_cleanups (back_to);
1810 }
1811
1812 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1813    the objfile from which this CU came.  Returns the resulting symbol
1814    table.  */
1815
1816 static struct symtab *
1817 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1818 {
1819   if (!per_cu->v.quick->symtab)
1820     {
1821       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1822       increment_reading_symtab ();
1823       dw2_do_instantiate_symtab (per_cu);
1824       do_cleanups (back_to);
1825     }
1826   return per_cu->v.quick->symtab;
1827 }
1828
1829 /* Return the CU given its index.  */
1830
1831 static struct dwarf2_per_cu_data *
1832 dw2_get_cu (int index)
1833 {
1834   if (index >= dwarf2_per_objfile->n_comp_units)
1835     {
1836       index -= dwarf2_per_objfile->n_comp_units;
1837       return dwarf2_per_objfile->all_type_units[index];
1838     }
1839   return dwarf2_per_objfile->all_comp_units[index];
1840 }
1841
1842 /* A helper function that knows how to read a 64-bit value in a way
1843    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1844    otherwise.  */
1845
1846 static int
1847 extract_cu_value (const char *bytes, ULONGEST *result)
1848 {
1849   if (sizeof (ULONGEST) < 8)
1850     {
1851       int i;
1852
1853       /* Ignore the upper 4 bytes if they are all zero.  */
1854       for (i = 0; i < 4; ++i)
1855         if (bytes[i + 4] != 0)
1856           return 0;
1857
1858       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1859     }
1860   else
1861     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1862   return 1;
1863 }
1864
1865 /* Read the CU list from the mapped index, and use it to create all
1866    the CU objects for this objfile.  Return 0 if something went wrong,
1867    1 if everything went ok.  */
1868
1869 static int
1870 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1871                        offset_type cu_list_elements)
1872 {
1873   offset_type i;
1874
1875   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1876   dwarf2_per_objfile->all_comp_units
1877     = obstack_alloc (&objfile->objfile_obstack,
1878                      dwarf2_per_objfile->n_comp_units
1879                      * sizeof (struct dwarf2_per_cu_data *));
1880
1881   for (i = 0; i < cu_list_elements; i += 2)
1882     {
1883       struct dwarf2_per_cu_data *the_cu;
1884       ULONGEST offset, length;
1885
1886       if (!extract_cu_value (cu_list, &offset)
1887           || !extract_cu_value (cu_list + 8, &length))
1888         return 0;
1889       cu_list += 2 * 8;
1890
1891       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1892                                struct dwarf2_per_cu_data);
1893       the_cu->offset = offset;
1894       the_cu->length = length;
1895       the_cu->objfile = objfile;
1896       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1897                                         struct dwarf2_per_cu_quick_data);
1898       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1899     }
1900
1901   return 1;
1902 }
1903
1904 /* Create the signatured type hash table from the index.  */
1905
1906 static int
1907 create_signatured_type_table_from_index (struct objfile *objfile,
1908                                          struct dwarf2_section_info *section,
1909                                          const gdb_byte *bytes,
1910                                          offset_type elements)
1911 {
1912   offset_type i;
1913   htab_t sig_types_hash;
1914
1915   dwarf2_per_objfile->n_type_units = elements / 3;
1916   dwarf2_per_objfile->all_type_units
1917     = obstack_alloc (&objfile->objfile_obstack,
1918                      dwarf2_per_objfile->n_type_units
1919                      * sizeof (struct dwarf2_per_cu_data *));
1920
1921   sig_types_hash = allocate_signatured_type_table (objfile);
1922
1923   for (i = 0; i < elements; i += 3)
1924     {
1925       struct signatured_type *type_sig;
1926       ULONGEST offset, type_offset, signature;
1927       void **slot;
1928
1929       if (!extract_cu_value (bytes, &offset)
1930           || !extract_cu_value (bytes + 8, &type_offset))
1931         return 0;
1932       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1933       bytes += 3 * 8;
1934
1935       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1936                                  struct signatured_type);
1937       type_sig->signature = signature;
1938       type_sig->type_offset = type_offset;
1939       type_sig->per_cu.debug_types_section = section;
1940       type_sig->per_cu.offset = offset;
1941       type_sig->per_cu.objfile = objfile;
1942       type_sig->per_cu.v.quick
1943         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1944                           struct dwarf2_per_cu_quick_data);
1945
1946       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1947       *slot = type_sig;
1948
1949       dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1950     }
1951
1952   dwarf2_per_objfile->signatured_types = sig_types_hash;
1953
1954   return 1;
1955 }
1956
1957 /* Read the address map data from the mapped index, and use it to
1958    populate the objfile's psymtabs_addrmap.  */
1959
1960 static void
1961 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1962 {
1963   const gdb_byte *iter, *end;
1964   struct obstack temp_obstack;
1965   struct addrmap *mutable_map;
1966   struct cleanup *cleanup;
1967   CORE_ADDR baseaddr;
1968
1969   obstack_init (&temp_obstack);
1970   cleanup = make_cleanup_obstack_free (&temp_obstack);
1971   mutable_map = addrmap_create_mutable (&temp_obstack);
1972
1973   iter = index->address_table;
1974   end = iter + index->address_table_size;
1975
1976   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1977
1978   while (iter < end)
1979     {
1980       ULONGEST hi, lo, cu_index;
1981       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1982       iter += 8;
1983       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984       iter += 8;
1985       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1986       iter += 4;
1987       
1988       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1989                          dw2_get_cu (cu_index));
1990     }
1991
1992   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1993                                                     &objfile->objfile_obstack);
1994   do_cleanups (cleanup);
1995 }
1996
1997 /* The hash function for strings in the mapped index.  This is the same as
1998    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1999    implementation.  This is necessary because the hash function is tied to the
2000    format of the mapped index file.  The hash values do not have to match with
2001    SYMBOL_HASH_NEXT.
2002    
2003    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2004
2005 static hashval_t
2006 mapped_index_string_hash (int index_version, const void *p)
2007 {
2008   const unsigned char *str = (const unsigned char *) p;
2009   hashval_t r = 0;
2010   unsigned char c;
2011
2012   while ((c = *str++) != 0)
2013     {
2014       if (index_version >= 5)
2015         c = tolower (c);
2016       r = r * 67 + c - 113;
2017     }
2018
2019   return r;
2020 }
2021
2022 /* Find a slot in the mapped index INDEX for the object named NAME.
2023    If NAME is found, set *VEC_OUT to point to the CU vector in the
2024    constant pool and return 1.  If NAME cannot be found, return 0.  */
2025
2026 static int
2027 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2028                           offset_type **vec_out)
2029 {
2030   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2031   offset_type hash;
2032   offset_type slot, step;
2033   int (*cmp) (const char *, const char *);
2034
2035   if (current_language->la_language == language_cplus
2036       || current_language->la_language == language_java
2037       || current_language->la_language == language_fortran)
2038     {
2039       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2040          not contain any.  */
2041       const char *paren = strchr (name, '(');
2042
2043       if (paren)
2044         {
2045           char *dup;
2046
2047           dup = xmalloc (paren - name + 1);
2048           memcpy (dup, name, paren - name);
2049           dup[paren - name] = 0;
2050
2051           make_cleanup (xfree, dup);
2052           name = dup;
2053         }
2054     }
2055
2056   /* Index version 4 did not support case insensitive searches.  But the
2057      indexes for case insensitive languages are built in lowercase, therefore
2058      simulate our NAME being searched is also lowercased.  */
2059   hash = mapped_index_string_hash ((index->version == 4
2060                                     && case_sensitivity == case_sensitive_off
2061                                     ? 5 : index->version),
2062                                    name);
2063
2064   slot = hash & (index->symbol_table_slots - 1);
2065   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2066   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2067
2068   for (;;)
2069     {
2070       /* Convert a slot number to an offset into the table.  */
2071       offset_type i = 2 * slot;
2072       const char *str;
2073       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2074         {
2075           do_cleanups (back_to);
2076           return 0;
2077         }
2078
2079       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2080       if (!cmp (name, str))
2081         {
2082           *vec_out = (offset_type *) (index->constant_pool
2083                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2084           do_cleanups (back_to);
2085           return 1;
2086         }
2087
2088       slot = (slot + step) & (index->symbol_table_slots - 1);
2089     }
2090 }
2091
2092 /* Read the index file.  If everything went ok, initialize the "quick"
2093    elements of all the CUs and return 1.  Otherwise, return 0.  */
2094
2095 static int
2096 dwarf2_read_index (struct objfile *objfile)
2097 {
2098   char *addr;
2099   struct mapped_index *map;
2100   offset_type *metadata;
2101   const gdb_byte *cu_list;
2102   const gdb_byte *types_list = NULL;
2103   offset_type version, cu_list_elements;
2104   offset_type types_list_elements = 0;
2105   int i;
2106
2107   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2108     return 0;
2109
2110   /* Older elfutils strip versions could keep the section in the main
2111      executable while splitting it for the separate debug info file.  */
2112   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2113        & SEC_HAS_CONTENTS) == 0)
2114     return 0;
2115
2116   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2117
2118   addr = dwarf2_per_objfile->gdb_index.buffer;
2119   /* Version check.  */
2120   version = MAYBE_SWAP (*(offset_type *) addr);
2121   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2122      causes the index to behave very poorly for certain requests.  Version 3
2123      contained incomplete addrmap.  So, it seems better to just ignore such
2124      indices.  Index version 4 uses a different hash function than index
2125      version 5 and later.  */
2126   if (version < 4)
2127     return 0;
2128   /* Indexes with higher version than the one supported by GDB may be no
2129      longer backward compatible.  */
2130   if (version > 5)
2131     return 0;
2132
2133   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2134   map->version = version;
2135   map->total_size = dwarf2_per_objfile->gdb_index.size;
2136
2137   metadata = (offset_type *) (addr + sizeof (offset_type));
2138
2139   i = 0;
2140   cu_list = addr + MAYBE_SWAP (metadata[i]);
2141   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2142                       / 8);
2143   ++i;
2144
2145   types_list = addr + MAYBE_SWAP (metadata[i]);
2146   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2147                           - MAYBE_SWAP (metadata[i]))
2148                          / 8);
2149   ++i;
2150
2151   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2152   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2153                              - MAYBE_SWAP (metadata[i]));
2154   ++i;
2155
2156   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2157   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2158                               - MAYBE_SWAP (metadata[i]))
2159                              / (2 * sizeof (offset_type)));
2160   ++i;
2161
2162   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2163
2164   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2165     return 0;
2166
2167   if (types_list_elements)
2168     {
2169       struct dwarf2_section_info *section;
2170
2171       /* We can only handle a single .debug_types when we have an
2172          index.  */
2173       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2174         return 0;
2175
2176       section = VEC_index (dwarf2_section_info_def,
2177                            dwarf2_per_objfile->types, 0);
2178
2179       if (!create_signatured_type_table_from_index (objfile, section,
2180                                                     types_list,
2181                                                     types_list_elements))
2182         return 0;
2183     }
2184
2185   create_addrmap_from_index (objfile, map);
2186
2187   dwarf2_per_objfile->index_table = map;
2188   dwarf2_per_objfile->using_index = 1;
2189   dwarf2_per_objfile->quick_file_names_table =
2190     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2191
2192   return 1;
2193 }
2194
2195 /* A helper for the "quick" functions which sets the global
2196    dwarf2_per_objfile according to OBJFILE.  */
2197
2198 static void
2199 dw2_setup (struct objfile *objfile)
2200 {
2201   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2202   gdb_assert (dwarf2_per_objfile);
2203 }
2204
2205 /* A helper for the "quick" functions which attempts to read the line
2206    table for THIS_CU.  */
2207
2208 static struct quick_file_names *
2209 dw2_get_file_names (struct objfile *objfile,
2210                     struct dwarf2_per_cu_data *this_cu)
2211 {
2212   bfd *abfd = objfile->obfd;
2213   struct line_header *lh;
2214   struct attribute *attr;
2215   struct cleanup *cleanups;
2216   struct die_info *comp_unit_die;
2217   struct dwarf2_section_info* sec;
2218   gdb_byte *info_ptr;
2219   int has_children, i;
2220   struct dwarf2_cu cu;
2221   unsigned int bytes_read;
2222   struct die_reader_specs reader_specs;
2223   char *name, *comp_dir;
2224   void **slot;
2225   struct quick_file_names *qfn;
2226   unsigned int line_offset;
2227
2228   if (this_cu->v.quick->file_names != NULL)
2229     return this_cu->v.quick->file_names;
2230   /* If we know there is no line data, no point in looking again.  */
2231   if (this_cu->v.quick->no_file_data)
2232     return NULL;
2233
2234   init_one_comp_unit (&cu, this_cu);
2235   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2236
2237   if (this_cu->debug_types_section)
2238     sec = this_cu->debug_types_section;
2239   else
2240     sec = &dwarf2_per_objfile->info;
2241   dwarf2_read_section (objfile, sec);
2242   info_ptr = sec->buffer + this_cu->offset;
2243
2244   info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2245                                             this_cu->debug_types_section != NULL);
2246
2247   /* Skip dummy compilation units.  */
2248   if (info_ptr >= (sec->buffer + sec->size)
2249       || peek_abbrev_code (abfd, info_ptr) == 0)
2250     {
2251       do_cleanups (cleanups);
2252       return NULL;
2253     }
2254
2255   dwarf2_read_abbrevs (&cu);
2256   make_cleanup (dwarf2_free_abbrev_table, &cu);
2257
2258   init_cu_die_reader (&reader_specs, &cu);
2259   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2260                  &has_children);
2261
2262   lh = NULL;
2263   slot = NULL;
2264   line_offset = 0;
2265   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2266   if (attr)
2267     {
2268       struct quick_file_names find_entry;
2269
2270       line_offset = DW_UNSND (attr);
2271
2272       /* We may have already read in this line header (TU line header sharing).
2273          If we have we're done.  */
2274       find_entry.offset = line_offset;
2275       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2276                              &find_entry, INSERT);
2277       if (*slot != NULL)
2278         {
2279           do_cleanups (cleanups);
2280           this_cu->v.quick->file_names = *slot;
2281           return *slot;
2282         }
2283
2284       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2285     }
2286   if (lh == NULL)
2287     {
2288       do_cleanups (cleanups);
2289       this_cu->v.quick->no_file_data = 1;
2290       return NULL;
2291     }
2292
2293   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2294   qfn->offset = line_offset;
2295   gdb_assert (slot != NULL);
2296   *slot = qfn;
2297
2298   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2299
2300   qfn->num_file_names = lh->num_file_names;
2301   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2302                                    lh->num_file_names * sizeof (char *));
2303   for (i = 0; i < lh->num_file_names; ++i)
2304     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2305   qfn->real_names = NULL;
2306
2307   free_line_header (lh);
2308   do_cleanups (cleanups);
2309
2310   this_cu->v.quick->file_names = qfn;
2311   return qfn;
2312 }
2313
2314 /* A helper for the "quick" functions which computes and caches the
2315    real path for a given file name from the line table.  */
2316
2317 static const char *
2318 dw2_get_real_path (struct objfile *objfile,
2319                    struct quick_file_names *qfn, int index)
2320 {
2321   if (qfn->real_names == NULL)
2322     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2323                                       qfn->num_file_names, sizeof (char *));
2324
2325   if (qfn->real_names[index] == NULL)
2326     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2327
2328   return qfn->real_names[index];
2329 }
2330
2331 static struct symtab *
2332 dw2_find_last_source_symtab (struct objfile *objfile)
2333 {
2334   int index;
2335
2336   dw2_setup (objfile);
2337   index = dwarf2_per_objfile->n_comp_units - 1;
2338   return dw2_instantiate_symtab (dw2_get_cu (index));
2339 }
2340
2341 /* Traversal function for dw2_forget_cached_source_info.  */
2342
2343 static int
2344 dw2_free_cached_file_names (void **slot, void *info)
2345 {
2346   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2347
2348   if (file_data->real_names)
2349     {
2350       int i;
2351
2352       for (i = 0; i < file_data->num_file_names; ++i)
2353         {
2354           xfree ((void*) file_data->real_names[i]);
2355           file_data->real_names[i] = NULL;
2356         }
2357     }
2358
2359   return 1;
2360 }
2361
2362 static void
2363 dw2_forget_cached_source_info (struct objfile *objfile)
2364 {
2365   dw2_setup (objfile);
2366
2367   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2368                           dw2_free_cached_file_names, NULL);
2369 }
2370
2371 /* Helper function for dw2_map_symtabs_matching_filename that expands
2372    the symtabs and calls the iterator.  */
2373
2374 static int
2375 dw2_map_expand_apply (struct objfile *objfile,
2376                       struct dwarf2_per_cu_data *per_cu,
2377                       const char *name,
2378                       const char *full_path, const char *real_path,
2379                       int (*callback) (struct symtab *, void *),
2380                       void *data)
2381 {
2382   struct symtab *last_made = objfile->symtabs;
2383
2384   /* Don't visit already-expanded CUs.  */
2385   if (per_cu->v.quick->symtab)
2386     return 0;
2387
2388   /* This may expand more than one symtab, and we want to iterate over
2389      all of them.  */
2390   dw2_instantiate_symtab (per_cu);
2391
2392   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2393                                     objfile->symtabs, last_made);
2394 }
2395
2396 /* Implementation of the map_symtabs_matching_filename method.  */
2397
2398 static int
2399 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2400                                    const char *full_path, const char *real_path,
2401                                    int (*callback) (struct symtab *, void *),
2402                                    void *data)
2403 {
2404   int i;
2405   const char *name_basename = lbasename (name);
2406   int name_len = strlen (name);
2407   int is_abs = IS_ABSOLUTE_PATH (name);
2408
2409   dw2_setup (objfile);
2410
2411   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2412                    + dwarf2_per_objfile->n_type_units); ++i)
2413     {
2414       int j;
2415       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2416       struct quick_file_names *file_data;
2417
2418       /* We only need to look at symtabs not already expanded.  */
2419       if (per_cu->v.quick->symtab)
2420         continue;
2421
2422       file_data = dw2_get_file_names (objfile, per_cu);
2423       if (file_data == NULL)
2424         continue;
2425
2426       for (j = 0; j < file_data->num_file_names; ++j)
2427         {
2428           const char *this_name = file_data->file_names[j];
2429
2430           if (FILENAME_CMP (name, this_name) == 0
2431               || (!is_abs && compare_filenames_for_search (this_name,
2432                                                            name, name_len)))
2433             {
2434               if (dw2_map_expand_apply (objfile, per_cu,
2435                                         name, full_path, real_path,
2436                                         callback, data))
2437                 return 1;
2438             }
2439
2440           /* Before we invoke realpath, which can get expensive when many
2441              files are involved, do a quick comparison of the basenames.  */
2442           if (! basenames_may_differ
2443               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2444             continue;
2445
2446           if (full_path != NULL)
2447             {
2448               const char *this_real_name = dw2_get_real_path (objfile,
2449                                                               file_data, j);
2450
2451               if (this_real_name != NULL
2452                   && (FILENAME_CMP (full_path, this_real_name) == 0
2453                       || (!is_abs
2454                           && compare_filenames_for_search (this_real_name,
2455                                                            name, name_len))))
2456                 {
2457                   if (dw2_map_expand_apply (objfile, per_cu,
2458                                             name, full_path, real_path,
2459                                             callback, data))
2460                     return 1;
2461                 }
2462             }
2463
2464           if (real_path != NULL)
2465             {
2466               const char *this_real_name = dw2_get_real_path (objfile,
2467                                                               file_data, j);
2468
2469               if (this_real_name != NULL
2470                   && (FILENAME_CMP (real_path, this_real_name) == 0
2471                       || (!is_abs
2472                           && compare_filenames_for_search (this_real_name,
2473                                                            name, name_len))))
2474                 {
2475                   if (dw2_map_expand_apply (objfile, per_cu,
2476                                             name, full_path, real_path,
2477                                             callback, data))
2478                     return 1;
2479                 }
2480             }
2481         }
2482     }
2483
2484   return 0;
2485 }
2486
2487 static struct symtab *
2488 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2489                    const char *name, domain_enum domain)
2490 {
2491   /* We do all the work in the pre_expand_symtabs_matching hook
2492      instead.  */
2493   return NULL;
2494 }
2495
2496 /* A helper function that expands all symtabs that hold an object
2497    named NAME.  */
2498
2499 static void
2500 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2501 {
2502   dw2_setup (objfile);
2503
2504   /* index_table is NULL if OBJF_READNOW.  */
2505   if (dwarf2_per_objfile->index_table)
2506     {
2507       offset_type *vec;
2508
2509       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2510                                     name, &vec))
2511         {
2512           offset_type i, len = MAYBE_SWAP (*vec);
2513           for (i = 0; i < len; ++i)
2514             {
2515               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2516               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2517
2518               dw2_instantiate_symtab (per_cu);
2519             }
2520         }
2521     }
2522 }
2523
2524 static void
2525 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2526                                  enum block_enum block_kind, const char *name,
2527                                  domain_enum domain)
2528 {
2529   dw2_do_expand_symtabs_matching (objfile, name);
2530 }
2531
2532 static void
2533 dw2_print_stats (struct objfile *objfile)
2534 {
2535   int i, count;
2536
2537   dw2_setup (objfile);
2538   count = 0;
2539   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2540                    + dwarf2_per_objfile->n_type_units); ++i)
2541     {
2542       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2543
2544       if (!per_cu->v.quick->symtab)
2545         ++count;
2546     }
2547   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2548 }
2549
2550 static void
2551 dw2_dump (struct objfile *objfile)
2552 {
2553   /* Nothing worth printing.  */
2554 }
2555
2556 static void
2557 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2558               struct section_offsets *delta)
2559 {
2560   /* There's nothing to relocate here.  */
2561 }
2562
2563 static void
2564 dw2_expand_symtabs_for_function (struct objfile *objfile,
2565                                  const char *func_name)
2566 {
2567   dw2_do_expand_symtabs_matching (objfile, func_name);
2568 }
2569
2570 static void
2571 dw2_expand_all_symtabs (struct objfile *objfile)
2572 {
2573   int i;
2574
2575   dw2_setup (objfile);
2576
2577   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2578                    + dwarf2_per_objfile->n_type_units); ++i)
2579     {
2580       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2581
2582       dw2_instantiate_symtab (per_cu);
2583     }
2584 }
2585
2586 static void
2587 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2588                                   const char *filename)
2589 {
2590   int i;
2591
2592   dw2_setup (objfile);
2593
2594   /* We don't need to consider type units here.
2595      This is only called for examining code, e.g. expand_line_sal.
2596      There can be an order of magnitude (or more) more type units
2597      than comp units, and we avoid them if we can.  */
2598
2599   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2600     {
2601       int j;
2602       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2603       struct quick_file_names *file_data;
2604
2605       /* We only need to look at symtabs not already expanded.  */
2606       if (per_cu->v.quick->symtab)
2607         continue;
2608
2609       file_data = dw2_get_file_names (objfile, per_cu);
2610       if (file_data == NULL)
2611         continue;
2612
2613       for (j = 0; j < file_data->num_file_names; ++j)
2614         {
2615           const char *this_name = file_data->file_names[j];
2616           if (FILENAME_CMP (this_name, filename) == 0)
2617             {
2618               dw2_instantiate_symtab (per_cu);
2619               break;
2620             }
2621         }
2622     }
2623 }
2624
2625 static const char *
2626 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2627 {
2628   struct dwarf2_per_cu_data *per_cu;
2629   offset_type *vec;
2630   struct quick_file_names *file_data;
2631
2632   dw2_setup (objfile);
2633
2634   /* index_table is NULL if OBJF_READNOW.  */
2635   if (!dwarf2_per_objfile->index_table)
2636     {
2637       struct symtab *s;
2638
2639       ALL_OBJFILE_SYMTABS (objfile, s)
2640         if (s->primary)
2641           {
2642             struct blockvector *bv = BLOCKVECTOR (s);
2643             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2644             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2645
2646             if (sym)
2647               return sym->symtab->filename;
2648           }
2649       return NULL;
2650     }
2651
2652   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2653                                  name, &vec))
2654     return NULL;
2655
2656   /* Note that this just looks at the very first one named NAME -- but
2657      actually we are looking for a function.  find_main_filename
2658      should be rewritten so that it doesn't require a custom hook.  It
2659      could just use the ordinary symbol tables.  */
2660   /* vec[0] is the length, which must always be >0.  */
2661   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2662
2663   file_data = dw2_get_file_names (objfile, per_cu);
2664   if (file_data == NULL)
2665     return NULL;
2666
2667   return file_data->file_names[file_data->num_file_names - 1];
2668 }
2669
2670 static void
2671 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2672                           struct objfile *objfile, int global,
2673                           int (*callback) (struct block *,
2674                                            struct symbol *, void *),
2675                           void *data, symbol_compare_ftype *match,
2676                           symbol_compare_ftype *ordered_compare)
2677 {
2678   /* Currently unimplemented; used for Ada.  The function can be called if the
2679      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2680      does not look for non-Ada symbols this function should just return.  */
2681 }
2682
2683 static void
2684 dw2_expand_symtabs_matching
2685   (struct objfile *objfile,
2686    int (*file_matcher) (const char *, void *),
2687    int (*name_matcher) (const char *, void *),
2688    enum search_domain kind,
2689    void *data)
2690 {
2691   int i;
2692   offset_type iter;
2693   struct mapped_index *index;
2694
2695   dw2_setup (objfile);
2696
2697   /* index_table is NULL if OBJF_READNOW.  */
2698   if (!dwarf2_per_objfile->index_table)
2699     return;
2700   index = dwarf2_per_objfile->index_table;
2701
2702   if (file_matcher != NULL)
2703     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2704                      + dwarf2_per_objfile->n_type_units); ++i)
2705       {
2706         int j;
2707         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2708         struct quick_file_names *file_data;
2709
2710         per_cu->v.quick->mark = 0;
2711
2712         /* We only need to look at symtabs not already expanded.  */
2713         if (per_cu->v.quick->symtab)
2714           continue;
2715
2716         file_data = dw2_get_file_names (objfile, per_cu);
2717         if (file_data == NULL)
2718           continue;
2719
2720         for (j = 0; j < file_data->num_file_names; ++j)
2721           {
2722             if (file_matcher (file_data->file_names[j], data))
2723               {
2724                 per_cu->v.quick->mark = 1;
2725                 break;
2726               }
2727           }
2728       }
2729
2730   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2731     {
2732       offset_type idx = 2 * iter;
2733       const char *name;
2734       offset_type *vec, vec_len, vec_idx;
2735
2736       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2737         continue;
2738
2739       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2740
2741       if (! (*name_matcher) (name, data))
2742         continue;
2743
2744       /* The name was matched, now expand corresponding CUs that were
2745          marked.  */
2746       vec = (offset_type *) (index->constant_pool
2747                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2748       vec_len = MAYBE_SWAP (vec[0]);
2749       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2750         {
2751           struct dwarf2_per_cu_data *per_cu;
2752
2753           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2754           if (file_matcher == NULL || per_cu->v.quick->mark)
2755             dw2_instantiate_symtab (per_cu);
2756         }
2757     }
2758 }
2759
2760 static struct symtab *
2761 dw2_find_pc_sect_symtab (struct objfile *objfile,
2762                          struct minimal_symbol *msymbol,
2763                          CORE_ADDR pc,
2764                          struct obj_section *section,
2765                          int warn_if_readin)
2766 {
2767   struct dwarf2_per_cu_data *data;
2768
2769   dw2_setup (objfile);
2770
2771   if (!objfile->psymtabs_addrmap)
2772     return NULL;
2773
2774   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2775   if (!data)
2776     return NULL;
2777
2778   if (warn_if_readin && data->v.quick->symtab)
2779     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2780              paddress (get_objfile_arch (objfile), pc));
2781
2782   return dw2_instantiate_symtab (data);
2783 }
2784
2785 static void
2786 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2787                           void *data, int need_fullname)
2788 {
2789   int i;
2790
2791   dw2_setup (objfile);
2792
2793   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2794                    + dwarf2_per_objfile->n_type_units); ++i)
2795     {
2796       int j;
2797       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2798       struct quick_file_names *file_data;
2799
2800       /* We only need to look at symtabs not already expanded.  */
2801       if (per_cu->v.quick->symtab)
2802         continue;
2803
2804       file_data = dw2_get_file_names (objfile, per_cu);
2805       if (file_data == NULL)
2806         continue;
2807
2808       for (j = 0; j < file_data->num_file_names; ++j)
2809         {
2810           const char *this_real_name;
2811
2812           if (need_fullname)
2813             this_real_name = dw2_get_real_path (objfile, file_data, j);
2814           else
2815             this_real_name = NULL;
2816           (*fun) (file_data->file_names[j], this_real_name, data);
2817         }
2818     }
2819 }
2820
2821 static int
2822 dw2_has_symbols (struct objfile *objfile)
2823 {
2824   return 1;
2825 }
2826
2827 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2828 {
2829   dw2_has_symbols,
2830   dw2_find_last_source_symtab,
2831   dw2_forget_cached_source_info,
2832   dw2_map_symtabs_matching_filename,
2833   dw2_lookup_symbol,
2834   dw2_pre_expand_symtabs_matching,
2835   dw2_print_stats,
2836   dw2_dump,
2837   dw2_relocate,
2838   dw2_expand_symtabs_for_function,
2839   dw2_expand_all_symtabs,
2840   dw2_expand_symtabs_with_filename,
2841   dw2_find_symbol_file,
2842   dw2_map_matching_symbols,
2843   dw2_expand_symtabs_matching,
2844   dw2_find_pc_sect_symtab,
2845   dw2_map_symbol_filenames
2846 };
2847
2848 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2849    file will use psymtabs, or 1 if using the GNU index.  */
2850
2851 int
2852 dwarf2_initialize_objfile (struct objfile *objfile)
2853 {
2854   /* If we're about to read full symbols, don't bother with the
2855      indices.  In this case we also don't care if some other debug
2856      format is making psymtabs, because they are all about to be
2857      expanded anyway.  */
2858   if ((objfile->flags & OBJF_READNOW))
2859     {
2860       int i;
2861
2862       dwarf2_per_objfile->using_index = 1;
2863       create_all_comp_units (objfile);
2864       create_debug_types_hash_table (objfile);
2865       dwarf2_per_objfile->quick_file_names_table =
2866         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2867
2868       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2869                        + dwarf2_per_objfile->n_type_units); ++i)
2870         {
2871           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2872
2873           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2874                                             struct dwarf2_per_cu_quick_data);
2875         }
2876
2877       /* Return 1 so that gdb sees the "quick" functions.  However,
2878          these functions will be no-ops because we will have expanded
2879          all symtabs.  */
2880       return 1;
2881     }
2882
2883   if (dwarf2_read_index (objfile))
2884     return 1;
2885
2886   return 0;
2887 }
2888
2889 \f
2890
2891 /* Build a partial symbol table.  */
2892
2893 void
2894 dwarf2_build_psymtabs (struct objfile *objfile)
2895 {
2896   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2897     {
2898       init_psymbol_list (objfile, 1024);
2899     }
2900
2901   dwarf2_build_psymtabs_hard (objfile);
2902 }
2903
2904 /* Return TRUE if OFFSET is within CU_HEADER.  */
2905
2906 static inline int
2907 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2908 {
2909   unsigned int bottom = cu_header->offset;
2910   unsigned int top = (cu_header->offset
2911                       + cu_header->length
2912                       + cu_header->initial_length_size);
2913
2914   return (offset >= bottom && offset < top);
2915 }
2916
2917 /* Read in the comp unit header information from the debug_info at info_ptr.
2918    NOTE: This leaves members offset, first_die_offset to be filled in
2919    by the caller.  */
2920
2921 static gdb_byte *
2922 read_comp_unit_head (struct comp_unit_head *cu_header,
2923                      gdb_byte *info_ptr, bfd *abfd)
2924 {
2925   int signed_addr;
2926   unsigned int bytes_read;
2927
2928   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2929   cu_header->initial_length_size = bytes_read;
2930   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2931   info_ptr += bytes_read;
2932   cu_header->version = read_2_bytes (abfd, info_ptr);
2933   info_ptr += 2;
2934   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2935                                           &bytes_read);
2936   info_ptr += bytes_read;
2937   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2938   info_ptr += 1;
2939   signed_addr = bfd_get_sign_extend_vma (abfd);
2940   if (signed_addr < 0)
2941     internal_error (__FILE__, __LINE__,
2942                     _("read_comp_unit_head: dwarf from non elf file"));
2943   cu_header->signed_addr_p = signed_addr;
2944
2945   return info_ptr;
2946 }
2947
2948 /* Subroutine of read_and_check_comp_unit_head and
2949    read_and_check_type_unit_head to simplify them.
2950    Perform various error checking on the header.  */
2951
2952 static void
2953 error_check_comp_unit_head (struct comp_unit_head *header,
2954                             struct dwarf2_section_info *section)
2955 {
2956   bfd *abfd = section->asection->owner;
2957   const char *filename = bfd_get_filename (abfd);
2958
2959   if (header->version != 2 && header->version != 3 && header->version != 4)
2960     error (_("Dwarf Error: wrong version in compilation unit header "
2961            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2962            filename);
2963
2964   if (header->abbrev_offset
2965       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2966                               &dwarf2_per_objfile->abbrev))
2967     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2968            "(offset 0x%lx + 6) [in module %s]"),
2969            (long) header->abbrev_offset, (long) header->offset,
2970            filename);
2971
2972   /* Cast to unsigned long to use 64-bit arithmetic when possible to
2973      avoid potential 32-bit overflow.  */
2974   if (((unsigned long) header->offset
2975        + header->length + header->initial_length_size)
2976       > section->size)
2977     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2978            "(offset 0x%lx + 0) [in module %s]"),
2979            (long) header->length, (long) header->offset,
2980            filename);
2981 }
2982
2983 /* Read in a CU/TU header and perform some basic error checking.
2984    The contents of the header are stored in HEADER.
2985    The result is a pointer to the start of the first DIE.  */
2986
2987 static gdb_byte *
2988 read_and_check_comp_unit_head (struct comp_unit_head *header,
2989                                struct dwarf2_section_info *section,
2990                                gdb_byte *info_ptr,
2991                                int is_debug_types_section)
2992 {
2993   gdb_byte *beg_of_comp_unit = info_ptr;
2994   bfd *abfd = section->asection->owner;
2995
2996   header->offset = beg_of_comp_unit - section->buffer;
2997
2998   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2999
3000   /* If we're reading a type unit, skip over the signature and
3001      type_offset fields.  */
3002   if (is_debug_types_section)
3003     info_ptr += 8 /*signature*/ + header->offset_size;
3004
3005   header->first_die_offset = info_ptr - beg_of_comp_unit;
3006
3007   error_check_comp_unit_head (header, section);
3008
3009   return info_ptr;
3010 }
3011
3012 /* Read in the types comp unit header information from .debug_types entry at
3013    types_ptr.  The result is a pointer to one past the end of the header.  */
3014
3015 static gdb_byte *
3016 read_and_check_type_unit_head (struct comp_unit_head *header,
3017                                struct dwarf2_section_info *section,
3018                                gdb_byte *info_ptr,
3019                                ULONGEST *signature, unsigned int *type_offset)
3020 {
3021   gdb_byte *beg_of_comp_unit = info_ptr;
3022   bfd *abfd = section->asection->owner;
3023
3024   header->offset = beg_of_comp_unit - section->buffer;
3025
3026   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3027
3028   /* If we're reading a type unit, skip over the signature and
3029      type_offset fields.  */
3030   if (signature != NULL)
3031     *signature = read_8_bytes (abfd, info_ptr);
3032   info_ptr += 8;
3033   if (type_offset != NULL)
3034     *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3035   info_ptr += header->offset_size;
3036
3037   header->first_die_offset = info_ptr - beg_of_comp_unit;
3038
3039   error_check_comp_unit_head (header, section);
3040
3041   return info_ptr;
3042 }
3043
3044 /* Allocate a new partial symtab for file named NAME and mark this new
3045    partial symtab as being an include of PST.  */
3046
3047 static void
3048 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3049                                struct objfile *objfile)
3050 {
3051   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3052
3053   subpst->section_offsets = pst->section_offsets;
3054   subpst->textlow = 0;
3055   subpst->texthigh = 0;
3056
3057   subpst->dependencies = (struct partial_symtab **)
3058     obstack_alloc (&objfile->objfile_obstack,
3059                    sizeof (struct partial_symtab *));
3060   subpst->dependencies[0] = pst;
3061   subpst->number_of_dependencies = 1;
3062
3063   subpst->globals_offset = 0;
3064   subpst->n_global_syms = 0;
3065   subpst->statics_offset = 0;
3066   subpst->n_static_syms = 0;
3067   subpst->symtab = NULL;
3068   subpst->read_symtab = pst->read_symtab;
3069   subpst->readin = 0;
3070
3071   /* No private part is necessary for include psymtabs.  This property
3072      can be used to differentiate between such include psymtabs and
3073      the regular ones.  */
3074   subpst->read_symtab_private = NULL;
3075 }
3076
3077 /* Read the Line Number Program data and extract the list of files
3078    included by the source file represented by PST.  Build an include
3079    partial symtab for each of these included files.  */
3080
3081 static void
3082 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3083                                struct die_info *die,
3084                                struct partial_symtab *pst)
3085 {
3086   struct objfile *objfile = cu->objfile;
3087   bfd *abfd = objfile->obfd;
3088   struct line_header *lh = NULL;
3089   struct attribute *attr;
3090
3091   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3092   if (attr)
3093     {
3094       unsigned int line_offset = DW_UNSND (attr);
3095
3096       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3097     }
3098   if (lh == NULL)
3099     return;  /* No linetable, so no includes.  */
3100
3101   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3102   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3103
3104   free_line_header (lh);
3105 }
3106
3107 static hashval_t
3108 hash_type_signature (const void *item)
3109 {
3110   const struct signatured_type *type_sig = item;
3111
3112   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3113   return type_sig->signature;
3114 }
3115
3116 static int
3117 eq_type_signature (const void *item_lhs, const void *item_rhs)
3118 {
3119   const struct signatured_type *lhs = item_lhs;
3120   const struct signatured_type *rhs = item_rhs;
3121
3122   return lhs->signature == rhs->signature;
3123 }
3124
3125 /* Allocate a hash table for signatured types.  */
3126
3127 static htab_t
3128 allocate_signatured_type_table (struct objfile *objfile)
3129 {
3130   return htab_create_alloc_ex (41,
3131                                hash_type_signature,
3132                                eq_type_signature,
3133                                NULL,
3134                                &objfile->objfile_obstack,
3135                                hashtab_obstack_allocate,
3136                                dummy_obstack_deallocate);
3137 }
3138
3139 /* A helper function to add a signatured type CU to a table.  */
3140
3141 static int
3142 add_signatured_type_cu_to_table (void **slot, void *datum)
3143 {
3144   struct signatured_type *sigt = *slot;
3145   struct dwarf2_per_cu_data ***datap = datum;
3146
3147   **datap = &sigt->per_cu;
3148   ++*datap;
3149
3150   return 1;
3151 }
3152
3153 /* Create the hash table of all entries in the .debug_types section(s).
3154    The result is zero if there is an error (e.g. missing .debug_types section),
3155    otherwise non-zero.  */
3156
3157 static int
3158 create_debug_types_hash_table (struct objfile *objfile)
3159 {
3160   htab_t types_htab = NULL;
3161   struct dwarf2_per_cu_data **iter;
3162   int ix;
3163   struct dwarf2_section_info *section;
3164
3165   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3166     {
3167       dwarf2_per_objfile->signatured_types = NULL;
3168       return 0;
3169     }
3170
3171   for (ix = 0;
3172        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3173                     ix, section);
3174        ++ix)
3175     {
3176       gdb_byte *info_ptr, *end_ptr;
3177
3178       dwarf2_read_section (objfile, section);
3179       info_ptr = section->buffer;
3180
3181       if (info_ptr == NULL)
3182         continue;
3183
3184       if (types_htab == NULL)
3185         types_htab = allocate_signatured_type_table (objfile);
3186
3187       if (dwarf2_die_debug)
3188         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3189
3190       end_ptr = info_ptr + section->size;
3191       while (info_ptr < end_ptr)
3192         {
3193           unsigned int offset;
3194           unsigned int type_offset;
3195           ULONGEST signature;
3196           struct signatured_type *type_sig;
3197           void **slot;
3198           gdb_byte *ptr = info_ptr;
3199           struct comp_unit_head header;
3200
3201           offset = ptr - section->buffer;
3202
3203           /* We need to read the type's signature in order to build the hash
3204              table, but we don't need anything else just yet.  */
3205
3206           ptr = read_and_check_type_unit_head (&header, section, ptr,
3207                                                &signature, &type_offset);
3208
3209           /* Skip dummy type units.  */
3210           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3211             {
3212               info_ptr = info_ptr + header.initial_length_size + header.length;
3213               continue;
3214             }
3215
3216           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3217           memset (type_sig, 0, sizeof (*type_sig));
3218           type_sig->signature = signature;
3219           type_sig->type_offset = type_offset;
3220           type_sig->per_cu.objfile = objfile;
3221           type_sig->per_cu.debug_types_section = section;
3222           type_sig->per_cu.offset = offset;
3223
3224           slot = htab_find_slot (types_htab, type_sig, INSERT);
3225           gdb_assert (slot != NULL);
3226           if (*slot != NULL)
3227             {
3228               const struct signatured_type *dup_sig = *slot;
3229
3230               complaint (&symfile_complaints,
3231                          _("debug type entry at offset 0x%x is duplicate to the "
3232                            "entry at offset 0x%x, signature 0x%s"),
3233                          offset, dup_sig->per_cu.offset,
3234                          phex (signature, sizeof (signature)));
3235               gdb_assert (signature == dup_sig->signature);
3236             }
3237           *slot = type_sig;
3238
3239           if (dwarf2_die_debug)
3240             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3241                                 offset, phex (signature, sizeof (signature)));
3242
3243           info_ptr = info_ptr + header.initial_length_size + header.length;
3244         }
3245     }
3246
3247   dwarf2_per_objfile->signatured_types = types_htab;
3248
3249   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3250   dwarf2_per_objfile->all_type_units
3251     = obstack_alloc (&objfile->objfile_obstack,
3252                      dwarf2_per_objfile->n_type_units
3253                      * sizeof (struct dwarf2_per_cu_data *));
3254   iter = &dwarf2_per_objfile->all_type_units[0];
3255   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3256   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3257               == dwarf2_per_objfile->n_type_units);
3258
3259   return 1;
3260 }
3261
3262 /* Lookup a signature based type.
3263    Returns NULL if SIG is not present in the table.  */
3264
3265 static struct signatured_type *
3266 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3267 {
3268   struct signatured_type find_entry, *entry;
3269
3270   if (dwarf2_per_objfile->signatured_types == NULL)
3271     {
3272       complaint (&symfile_complaints,
3273                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3274       return 0;
3275     }
3276
3277   find_entry.signature = sig;
3278   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3279   return entry;
3280 }
3281
3282 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3283
3284 static void
3285 init_cu_die_reader (struct die_reader_specs *reader,
3286                     struct dwarf2_cu *cu)
3287 {
3288   reader->abfd = cu->objfile->obfd;
3289   reader->cu = cu;
3290   if (cu->per_cu->debug_types_section)
3291     {
3292       gdb_assert (cu->per_cu->debug_types_section->readin);
3293       reader->buffer = cu->per_cu->debug_types_section->buffer;
3294     }
3295   else
3296     {
3297       gdb_assert (dwarf2_per_objfile->info.readin);
3298       reader->buffer = dwarf2_per_objfile->info.buffer;
3299     }
3300 }
3301
3302 /* Find the base address of the compilation unit for range lists and
3303    location lists.  It will normally be specified by DW_AT_low_pc.
3304    In DWARF-3 draft 4, the base address could be overridden by
3305    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3306    compilation units with discontinuous ranges.  */
3307
3308 static void
3309 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3310 {
3311   struct attribute *attr;
3312
3313   cu->base_known = 0;
3314   cu->base_address = 0;
3315
3316   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3317   if (attr)
3318     {
3319       cu->base_address = DW_ADDR (attr);
3320       cu->base_known = 1;
3321     }
3322   else
3323     {
3324       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3325       if (attr)
3326         {
3327           cu->base_address = DW_ADDR (attr);
3328           cu->base_known = 1;
3329         }
3330     }
3331 }
3332
3333 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3334    to combine the common parts.
3335    Process compilation unit THIS_CU for a psymtab.
3336    SECTION is the section the CU/TU comes from,
3337    either .debug_info or .debug_types.  */
3338
3339 void
3340 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3341                            struct dwarf2_section_info *section,
3342                            int is_debug_types_section)
3343 {
3344   struct objfile *objfile = this_cu->objfile;
3345   bfd *abfd = objfile->obfd;
3346   gdb_byte *buffer = section->buffer;
3347   gdb_byte *info_ptr = buffer + this_cu->offset;
3348   unsigned int buffer_size = section->size;
3349   gdb_byte *beg_of_comp_unit = info_ptr;
3350   struct die_info *comp_unit_die;
3351   struct partial_symtab *pst;
3352   CORE_ADDR baseaddr;
3353   struct cleanup *back_to_inner;
3354   struct dwarf2_cu cu;
3355   int has_children, has_pc_info;
3356   struct attribute *attr;
3357   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3358   struct die_reader_specs reader_specs;
3359   const char *filename;
3360
3361   /* If this compilation unit was already read in, free the
3362      cached copy in order to read it in again.  This is
3363      necessary because we skipped some symbols when we first
3364      read in the compilation unit (see load_partial_dies).
3365      This problem could be avoided, but the benefit is
3366      unclear.  */
3367   if (this_cu->cu != NULL)
3368     free_one_cached_comp_unit (this_cu->cu);
3369
3370   /* Note that this is a pointer to our stack frame, being
3371      added to a global data structure.  It will be cleaned up
3372      in free_stack_comp_unit when we finish with this
3373      compilation unit.  */
3374   init_one_comp_unit (&cu, this_cu);
3375   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3376
3377   info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3378                                             is_debug_types_section);
3379
3380   /* Skip dummy compilation units.  */
3381   if (info_ptr >= buffer + buffer_size
3382       || peek_abbrev_code (abfd, info_ptr) == 0)
3383     {
3384       do_cleanups (back_to_inner);
3385       return;
3386     }
3387
3388   cu.list_in_scope = &file_symbols;
3389
3390   /* Read the abbrevs for this compilation unit into a table.  */
3391   dwarf2_read_abbrevs (&cu);
3392   make_cleanup (dwarf2_free_abbrev_table, &cu);
3393
3394   /* Read the compilation unit die.  */
3395   init_cu_die_reader (&reader_specs, &cu);
3396   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3397                             &has_children);
3398
3399   if (is_debug_types_section)
3400     {
3401       /* LENGTH has not been set yet for type units.  */
3402       gdb_assert (this_cu->offset == cu.header.offset);
3403       this_cu->length = cu.header.length + cu.header.initial_length_size;
3404     }
3405   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3406     {
3407       do_cleanups (back_to_inner);
3408       return;
3409     }
3410
3411   prepare_one_comp_unit (&cu, comp_unit_die);
3412
3413   /* Allocate a new partial symbol table structure.  */
3414   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3415   if (attr == NULL || !DW_STRING (attr))
3416     filename = "";
3417   else
3418     filename = DW_STRING (attr);
3419   pst = start_psymtab_common (objfile, objfile->section_offsets,
3420                               filename,
3421                               /* TEXTLOW and TEXTHIGH are set below.  */
3422                               0,
3423                               objfile->global_psymbols.next,
3424                               objfile->static_psymbols.next);
3425   pst->psymtabs_addrmap_supported = 1;
3426
3427   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3428   if (attr != NULL)
3429     pst->dirname = DW_STRING (attr);
3430
3431   pst->read_symtab_private = this_cu;
3432
3433   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3434
3435   /* Store the function that reads in the rest of the symbol table.  */
3436   pst->read_symtab = dwarf2_psymtab_to_symtab;
3437
3438   this_cu->v.psymtab = pst;
3439
3440   dwarf2_find_base_address (comp_unit_die, &cu);
3441
3442   /* Possibly set the default values of LOWPC and HIGHPC from
3443      `DW_AT_ranges'.  */
3444   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3445                                       &best_highpc, &cu, pst);
3446   if (has_pc_info == 1 && best_lowpc < best_highpc)
3447     /* Store the contiguous range if it is not empty; it can be empty for
3448        CUs with no code.  */
3449     addrmap_set_empty (objfile->psymtabs_addrmap,
3450                        best_lowpc + baseaddr,
3451                        best_highpc + baseaddr - 1, pst);
3452
3453   /* Check if comp unit has_children.
3454      If so, read the rest of the partial symbols from this comp unit.
3455      If not, there's no more debug_info for this comp unit.  */
3456   if (has_children)
3457     {
3458       struct partial_die_info *first_die;
3459       CORE_ADDR lowpc, highpc;
3460
3461       lowpc = ((CORE_ADDR) -1);
3462       highpc = ((CORE_ADDR) 0);
3463
3464       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3465
3466       scan_partial_symbols (first_die, &lowpc, &highpc,
3467                             ! has_pc_info, &cu);
3468
3469       /* If we didn't find a lowpc, set it to highpc to avoid
3470          complaints from `maint check'.  */
3471       if (lowpc == ((CORE_ADDR) -1))
3472         lowpc = highpc;
3473
3474       /* If the compilation unit didn't have an explicit address range,
3475          then use the information extracted from its child dies.  */
3476       if (! has_pc_info)
3477         {
3478           best_lowpc = lowpc;
3479           best_highpc = highpc;
3480         }
3481     }
3482   pst->textlow = best_lowpc + baseaddr;
3483   pst->texthigh = best_highpc + baseaddr;
3484
3485   pst->n_global_syms = objfile->global_psymbols.next -
3486     (objfile->global_psymbols.list + pst->globals_offset);
3487   pst->n_static_syms = objfile->static_psymbols.next -
3488     (objfile->static_psymbols.list + pst->statics_offset);
3489   sort_pst_symbols (pst);
3490
3491   if (is_debug_types_section)
3492     {
3493       /* It's not clear we want to do anything with stmt lists here.
3494          Waiting to see what gcc ultimately does.  */
3495     }
3496   else
3497     {
3498       /* Get the list of files included in the current compilation unit,
3499          and build a psymtab for each of them.  */
3500       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3501     }
3502
3503   do_cleanups (back_to_inner);
3504 }
3505
3506 /* Traversal function for htab_traverse_noresize.
3507    Process one .debug_types comp-unit.  */
3508
3509 static int
3510 process_type_comp_unit (void **slot, void *info)
3511 {
3512   struct signatured_type *entry = (struct signatured_type *) *slot;
3513   struct dwarf2_per_cu_data *this_cu;
3514
3515   gdb_assert (info == NULL);
3516   this_cu = &entry->per_cu;
3517
3518   gdb_assert (this_cu->debug_types_section->readin);
3519   process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3520
3521   return 1;
3522 }
3523
3524 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3525    Build partial symbol tables for the .debug_types comp-units.  */
3526
3527 static void
3528 build_type_psymtabs (struct objfile *objfile)
3529 {
3530   if (! create_debug_types_hash_table (objfile))
3531     return;
3532
3533   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3534                           process_type_comp_unit, NULL);
3535 }
3536
3537 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3538
3539 static void
3540 psymtabs_addrmap_cleanup (void *o)
3541 {
3542   struct objfile *objfile = o;
3543
3544   objfile->psymtabs_addrmap = NULL;
3545 }
3546
3547 /* Build the partial symbol table by doing a quick pass through the
3548    .debug_info and .debug_abbrev sections.  */
3549
3550 static void
3551 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3552 {
3553   struct cleanup *back_to, *addrmap_cleanup;
3554   struct obstack temp_obstack;
3555   int i;
3556
3557   dwarf2_per_objfile->reading_partial_symbols = 1;
3558
3559   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3560
3561   /* Any cached compilation units will be linked by the per-objfile
3562      read_in_chain.  Make sure to free them when we're done.  */
3563   back_to = make_cleanup (free_cached_comp_units, NULL);
3564
3565   build_type_psymtabs (objfile);
3566
3567   create_all_comp_units (objfile);
3568
3569   /* Create a temporary address map on a temporary obstack.  We later
3570      copy this to the final obstack.  */
3571   obstack_init (&temp_obstack);
3572   make_cleanup_obstack_free (&temp_obstack);
3573   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3574   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3575
3576   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3577     {
3578       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3579
3580       process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3581     }
3582
3583   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3584                                                     &objfile->objfile_obstack);
3585   discard_cleanups (addrmap_cleanup);
3586
3587   do_cleanups (back_to);
3588 }
3589
3590 /* Load the partial DIEs for a secondary CU into memory.  */
3591
3592 static void
3593 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3594 {
3595   struct objfile *objfile = this_cu->objfile;
3596   bfd *abfd = objfile->obfd;
3597   gdb_byte *info_ptr;
3598   struct die_info *comp_unit_die;
3599   struct dwarf2_cu *cu;
3600   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3601   int has_children;
3602   struct die_reader_specs reader_specs;
3603   int read_cu = 0;
3604   struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3605
3606   gdb_assert (! this_cu->debug_types_section);
3607
3608   gdb_assert (section->readin);
3609   info_ptr = section->buffer + this_cu->offset;
3610
3611   if (this_cu->cu == NULL)
3612     {
3613       cu = xmalloc (sizeof (*cu));
3614       init_one_comp_unit (cu, this_cu);
3615
3616       read_cu = 1;
3617
3618       /* If an error occurs while loading, release our storage.  */
3619       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3620
3621       info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3622                                                 0);
3623
3624       /* Skip dummy compilation units.  */
3625       if (info_ptr >= (section->buffer + section->size)
3626           || peek_abbrev_code (abfd, info_ptr) == 0)
3627         {
3628           do_cleanups (free_cu_cleanup);
3629           return;
3630         }
3631
3632       /* Link this CU into read_in_chain.  */
3633       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3634       dwarf2_per_objfile->read_in_chain = this_cu;
3635     }
3636   else
3637     {
3638       cu = this_cu->cu;
3639       info_ptr += cu->header.first_die_offset;
3640     }
3641
3642   /* Read the abbrevs for this compilation unit into a table.  */
3643   gdb_assert (cu->dwarf2_abbrevs == NULL);
3644   dwarf2_read_abbrevs (cu);
3645   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3646
3647   /* Read the compilation unit die.  */
3648   init_cu_die_reader (&reader_specs, cu);
3649   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3650                             &has_children);
3651
3652   prepare_one_comp_unit (cu, comp_unit_die);
3653
3654   /* Check if comp unit has_children.
3655      If so, read the rest of the partial symbols from this comp unit.
3656      If not, there's no more debug_info for this comp unit.  */
3657   if (has_children)
3658     load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3659
3660   do_cleanups (free_abbrevs_cleanup);
3661
3662   if (read_cu)
3663     {
3664       /* We've successfully allocated this compilation unit.  Let our
3665          caller clean it up when finished with it.  */
3666       discard_cleanups (free_cu_cleanup);
3667     }
3668 }
3669
3670 /* Create a list of all compilation units in OBJFILE.
3671    This is only done for -readnow and building partial symtabs.  */
3672
3673 static void
3674 create_all_comp_units (struct objfile *objfile)
3675 {
3676   int n_allocated;
3677   int n_comp_units;
3678   struct dwarf2_per_cu_data **all_comp_units;
3679   gdb_byte *info_ptr;
3680
3681   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3682   info_ptr = dwarf2_per_objfile->info.buffer;
3683
3684   n_comp_units = 0;
3685   n_allocated = 10;
3686   all_comp_units = xmalloc (n_allocated
3687                             * sizeof (struct dwarf2_per_cu_data *));
3688
3689   while (info_ptr < dwarf2_per_objfile->info.buffer
3690          + dwarf2_per_objfile->info.size)
3691     {
3692       unsigned int length, initial_length_size;
3693       struct dwarf2_per_cu_data *this_cu;
3694       unsigned int offset;
3695
3696       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3697
3698       /* Read just enough information to find out where the next
3699          compilation unit is.  */
3700       length = read_initial_length (objfile->obfd, info_ptr,
3701                                     &initial_length_size);
3702
3703       /* Save the compilation unit for later lookup.  */
3704       this_cu = obstack_alloc (&objfile->objfile_obstack,
3705                                sizeof (struct dwarf2_per_cu_data));
3706       memset (this_cu, 0, sizeof (*this_cu));
3707       this_cu->offset = offset;
3708       this_cu->length = length + initial_length_size;
3709       this_cu->objfile = objfile;
3710
3711       if (n_comp_units == n_allocated)
3712         {
3713           n_allocated *= 2;
3714           all_comp_units = xrealloc (all_comp_units,
3715                                      n_allocated
3716                                      * sizeof (struct dwarf2_per_cu_data *));
3717         }
3718       all_comp_units[n_comp_units++] = this_cu;
3719
3720       info_ptr = info_ptr + this_cu->length;
3721     }
3722
3723   dwarf2_per_objfile->all_comp_units
3724     = obstack_alloc (&objfile->objfile_obstack,
3725                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3726   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3727           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3728   xfree (all_comp_units);
3729   dwarf2_per_objfile->n_comp_units = n_comp_units;
3730 }
3731
3732 /* Process all loaded DIEs for compilation unit CU, starting at
3733    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3734    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3735    DW_AT_ranges).  If NEED_PC is set, then this function will set
3736    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3737    and record the covered ranges in the addrmap.  */
3738
3739 static void
3740 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3741                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3742 {
3743   struct partial_die_info *pdi;
3744
3745   /* Now, march along the PDI's, descending into ones which have
3746      interesting children but skipping the children of the other ones,
3747      until we reach the end of the compilation unit.  */
3748
3749   pdi = first_die;
3750
3751   while (pdi != NULL)
3752     {
3753       fixup_partial_die (pdi, cu);
3754
3755       /* Anonymous namespaces or modules have no name but have interesting
3756          children, so we need to look at them.  Ditto for anonymous
3757          enums.  */
3758
3759       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3760           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3761         {
3762           switch (pdi->tag)
3763             {
3764             case DW_TAG_subprogram:
3765               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3766               break;
3767             case DW_TAG_constant:
3768             case DW_TAG_variable:
3769             case DW_TAG_typedef:
3770             case DW_TAG_union_type:
3771               if (!pdi->is_declaration)
3772                 {
3773                   add_partial_symbol (pdi, cu);
3774                 }
3775               break;
3776             case DW_TAG_class_type:
3777             case DW_TAG_interface_type:
3778             case DW_TAG_structure_type:
3779               if (!pdi->is_declaration)
3780                 {
3781                   add_partial_symbol (pdi, cu);
3782                 }
3783               break;
3784             case DW_TAG_enumeration_type:
3785               if (!pdi->is_declaration)
3786                 add_partial_enumeration (pdi, cu);
3787               break;
3788             case DW_TAG_base_type:
3789             case DW_TAG_subrange_type:
3790               /* File scope base type definitions are added to the partial
3791                  symbol table.  */
3792               add_partial_symbol (pdi, cu);
3793               break;
3794             case DW_TAG_namespace:
3795               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3796               break;
3797             case DW_TAG_module:
3798               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3799               break;
3800             default:
3801               break;
3802             }
3803         }
3804
3805       /* If the die has a sibling, skip to the sibling.  */
3806
3807       pdi = pdi->die_sibling;
3808     }
3809 }
3810
3811 /* Functions used to compute the fully scoped name of a partial DIE.
3812
3813    Normally, this is simple.  For C++, the parent DIE's fully scoped
3814    name is concatenated with "::" and the partial DIE's name.  For
3815    Java, the same thing occurs except that "." is used instead of "::".
3816    Enumerators are an exception; they use the scope of their parent
3817    enumeration type, i.e. the name of the enumeration type is not
3818    prepended to the enumerator.
3819
3820    There are two complexities.  One is DW_AT_specification; in this
3821    case "parent" means the parent of the target of the specification,
3822    instead of the direct parent of the DIE.  The other is compilers
3823    which do not emit DW_TAG_namespace; in this case we try to guess
3824    the fully qualified name of structure types from their members'
3825    linkage names.  This must be done using the DIE's children rather
3826    than the children of any DW_AT_specification target.  We only need
3827    to do this for structures at the top level, i.e. if the target of
3828    any DW_AT_specification (if any; otherwise the DIE itself) does not
3829    have a parent.  */
3830
3831 /* Compute the scope prefix associated with PDI's parent, in
3832    compilation unit CU.  The result will be allocated on CU's
3833    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3834    field.  NULL is returned if no prefix is necessary.  */
3835 static char *
3836 partial_die_parent_scope (struct partial_die_info *pdi,
3837                           struct dwarf2_cu *cu)
3838 {
3839   char *grandparent_scope;
3840   struct partial_die_info *parent, *real_pdi;
3841
3842   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3843      then this means the parent of the specification DIE.  */
3844
3845   real_pdi = pdi;
3846   while (real_pdi->has_specification)
3847     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3848
3849   parent = real_pdi->die_parent;
3850   if (parent == NULL)
3851     return NULL;
3852
3853   if (parent->scope_set)
3854     return parent->scope;
3855
3856   fixup_partial_die (parent, cu);
3857
3858   grandparent_scope = partial_die_parent_scope (parent, cu);
3859
3860   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3861      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3862      Work around this problem here.  */
3863   if (cu->language == language_cplus
3864       && parent->tag == DW_TAG_namespace
3865       && strcmp (parent->name, "::") == 0
3866       && grandparent_scope == NULL)
3867     {
3868       parent->scope = NULL;
3869       parent->scope_set = 1;
3870       return NULL;
3871     }
3872
3873   if (pdi->tag == DW_TAG_enumerator)
3874     /* Enumerators should not get the name of the enumeration as a prefix.  */
3875     parent->scope = grandparent_scope;
3876   else if (parent->tag == DW_TAG_namespace
3877       || parent->tag == DW_TAG_module
3878       || parent->tag == DW_TAG_structure_type
3879       || parent->tag == DW_TAG_class_type
3880       || parent->tag == DW_TAG_interface_type
3881       || parent->tag == DW_TAG_union_type
3882       || parent->tag == DW_TAG_enumeration_type)
3883     {
3884       if (grandparent_scope == NULL)
3885         parent->scope = parent->name;
3886       else
3887         parent->scope = typename_concat (&cu->comp_unit_obstack,
3888                                          grandparent_scope,
3889                                          parent->name, 0, cu);
3890     }
3891   else
3892     {
3893       /* FIXME drow/2004-04-01: What should we be doing with
3894          function-local names?  For partial symbols, we should probably be
3895          ignoring them.  */
3896       complaint (&symfile_complaints,
3897                  _("unhandled containing DIE tag %d for DIE at %d"),
3898                  parent->tag, pdi->offset);
3899       parent->scope = grandparent_scope;
3900     }
3901
3902   parent->scope_set = 1;
3903   return parent->scope;
3904 }
3905
3906 /* Return the fully scoped name associated with PDI, from compilation unit
3907    CU.  The result will be allocated with malloc.  */
3908 static char *
3909 partial_die_full_name (struct partial_die_info *pdi,
3910                        struct dwarf2_cu *cu)
3911 {
3912   char *parent_scope;
3913
3914   /* If this is a template instantiation, we can not work out the
3915      template arguments from partial DIEs.  So, unfortunately, we have
3916      to go through the full DIEs.  At least any work we do building
3917      types here will be reused if full symbols are loaded later.  */
3918   if (pdi->has_template_arguments)
3919     {
3920       fixup_partial_die (pdi, cu);
3921
3922       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3923         {
3924           struct die_info *die;
3925           struct attribute attr;
3926           struct dwarf2_cu *ref_cu = cu;
3927
3928           attr.name = 0;
3929           attr.form = DW_FORM_ref_addr;
3930           attr.u.addr = pdi->offset;
3931           die = follow_die_ref (NULL, &attr, &ref_cu);
3932
3933           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3934         }
3935     }
3936
3937   parent_scope = partial_die_parent_scope (pdi, cu);
3938   if (parent_scope == NULL)
3939     return NULL;
3940   else
3941     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3942 }
3943
3944 static void
3945 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3946 {
3947   struct objfile *objfile = cu->objfile;
3948   CORE_ADDR addr = 0;
3949   char *actual_name = NULL;
3950   CORE_ADDR baseaddr;
3951   int built_actual_name = 0;
3952
3953   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3954
3955   actual_name = partial_die_full_name (pdi, cu);
3956   if (actual_name)
3957     built_actual_name = 1;
3958
3959   if (actual_name == NULL)
3960     actual_name = pdi->name;
3961
3962   switch (pdi->tag)
3963     {
3964     case DW_TAG_subprogram:
3965       if (pdi->is_external || cu->language == language_ada)
3966         {
3967           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3968              of the global scope.  But in Ada, we want to be able to access
3969              nested procedures globally.  So all Ada subprograms are stored
3970              in the global scope.  */
3971           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3972              mst_text, objfile); */
3973           add_psymbol_to_list (actual_name, strlen (actual_name),
3974                                built_actual_name,
3975                                VAR_DOMAIN, LOC_BLOCK,
3976                                &objfile->global_psymbols,
3977                                0, pdi->lowpc + baseaddr,
3978                                cu->language, objfile);
3979         }
3980       else
3981         {
3982           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3983              mst_file_text, objfile); */
3984           add_psymbol_to_list (actual_name, strlen (actual_name),
3985                                built_actual_name,
3986                                VAR_DOMAIN, LOC_BLOCK,
3987                                &objfile->static_psymbols,
3988                                0, pdi->lowpc + baseaddr,
3989                                cu->language, objfile);
3990         }
3991       break;
3992     case DW_TAG_constant:
3993       {
3994         struct psymbol_allocation_list *list;
3995
3996         if (pdi->is_external)
3997           list = &objfile->global_psymbols;
3998         else
3999           list = &objfile->static_psymbols;
4000         add_psymbol_to_list (actual_name, strlen (actual_name),
4001                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4002                              list, 0, 0, cu->language, objfile);
4003       }
4004       break;
4005     case DW_TAG_variable:
4006       if (pdi->locdesc)
4007         addr = decode_locdesc (pdi->locdesc, cu);
4008
4009       if (pdi->locdesc
4010           && addr == 0
4011           && !dwarf2_per_objfile->has_section_at_zero)
4012         {
4013           /* A global or static variable may also have been stripped
4014              out by the linker if unused, in which case its address
4015              will be nullified; do not add such variables into partial
4016              symbol table then.  */
4017         }
4018       else if (pdi->is_external)
4019         {
4020           /* Global Variable.
4021              Don't enter into the minimal symbol tables as there is
4022              a minimal symbol table entry from the ELF symbols already.
4023              Enter into partial symbol table if it has a location
4024              descriptor or a type.
4025              If the location descriptor is missing, new_symbol will create
4026              a LOC_UNRESOLVED symbol, the address of the variable will then
4027              be determined from the minimal symbol table whenever the variable
4028              is referenced.
4029              The address for the partial symbol table entry is not
4030              used by GDB, but it comes in handy for debugging partial symbol
4031              table building.  */
4032
4033           if (pdi->locdesc || pdi->has_type)
4034             add_psymbol_to_list (actual_name, strlen (actual_name),
4035                                  built_actual_name,
4036                                  VAR_DOMAIN, LOC_STATIC,
4037                                  &objfile->global_psymbols,
4038                                  0, addr + baseaddr,
4039                                  cu->language, objfile);
4040         }
4041       else
4042         {
4043           /* Static Variable.  Skip symbols without location descriptors.  */
4044           if (pdi->locdesc == NULL)
4045             {
4046               if (built_actual_name)
4047                 xfree (actual_name);
4048               return;
4049             }
4050           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4051              mst_file_data, objfile); */
4052           add_psymbol_to_list (actual_name, strlen (actual_name),
4053                                built_actual_name,
4054                                VAR_DOMAIN, LOC_STATIC,
4055                                &objfile->static_psymbols,
4056                                0, addr + baseaddr,
4057                                cu->language, objfile);
4058         }
4059       break;
4060     case DW_TAG_typedef:
4061     case DW_TAG_base_type:
4062     case DW_TAG_subrange_type:
4063       add_psymbol_to_list (actual_name, strlen (actual_name),
4064                            built_actual_name,
4065                            VAR_DOMAIN, LOC_TYPEDEF,
4066                            &objfile->static_psymbols,
4067                            0, (CORE_ADDR) 0, cu->language, objfile);
4068       break;
4069     case DW_TAG_namespace:
4070       add_psymbol_to_list (actual_name, strlen (actual_name),
4071                            built_actual_name,
4072                            VAR_DOMAIN, LOC_TYPEDEF,
4073                            &objfile->global_psymbols,
4074                            0, (CORE_ADDR) 0, cu->language, objfile);
4075       break;
4076     case DW_TAG_class_type:
4077     case DW_TAG_interface_type:
4078     case DW_TAG_structure_type:
4079     case DW_TAG_union_type:
4080     case DW_TAG_enumeration_type:
4081       /* Skip external references.  The DWARF standard says in the section
4082          about "Structure, Union, and Class Type Entries": "An incomplete
4083          structure, union or class type is represented by a structure,
4084          union or class entry that does not have a byte size attribute
4085          and that has a DW_AT_declaration attribute."  */
4086       if (!pdi->has_byte_size && pdi->is_declaration)
4087         {
4088           if (built_actual_name)
4089             xfree (actual_name);
4090           return;
4091         }
4092
4093       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4094          static vs. global.  */
4095       add_psymbol_to_list (actual_name, strlen (actual_name),
4096                            built_actual_name,
4097                            STRUCT_DOMAIN, LOC_TYPEDEF,
4098                            (cu->language == language_cplus
4099                             || cu->language == language_java)
4100                            ? &objfile->global_psymbols
4101                            : &objfile->static_psymbols,
4102                            0, (CORE_ADDR) 0, cu->language, objfile);
4103
4104       break;
4105     case DW_TAG_enumerator:
4106       add_psymbol_to_list (actual_name, strlen (actual_name),
4107                            built_actual_name,
4108                            VAR_DOMAIN, LOC_CONST,
4109                            (cu->language == language_cplus
4110                             || cu->language == language_java)
4111                            ? &objfile->global_psymbols
4112                            : &objfile->static_psymbols,
4113                            0, (CORE_ADDR) 0, cu->language, objfile);
4114       break;
4115     default:
4116       break;
4117     }
4118
4119   if (built_actual_name)
4120     xfree (actual_name);
4121 }
4122
4123 /* Read a partial die corresponding to a namespace; also, add a symbol
4124    corresponding to that namespace to the symbol table.  NAMESPACE is
4125    the name of the enclosing namespace.  */
4126
4127 static void
4128 add_partial_namespace (struct partial_die_info *pdi,
4129                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4130                        int need_pc, struct dwarf2_cu *cu)
4131 {
4132   /* Add a symbol for the namespace.  */
4133
4134   add_partial_symbol (pdi, cu);
4135
4136   /* Now scan partial symbols in that namespace.  */
4137
4138   if (pdi->has_children)
4139     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4140 }
4141
4142 /* Read a partial die corresponding to a Fortran module.  */
4143
4144 static void
4145 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4146                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4147 {
4148   /* Now scan partial symbols in that module.  */
4149
4150   if (pdi->has_children)
4151     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4152 }
4153
4154 /* Read a partial die corresponding to a subprogram and create a partial
4155    symbol for that subprogram.  When the CU language allows it, this
4156    routine also defines a partial symbol for each nested subprogram
4157    that this subprogram contains.
4158
4159    DIE my also be a lexical block, in which case we simply search
4160    recursively for suprograms defined inside that lexical block.
4161    Again, this is only performed when the CU language allows this
4162    type of definitions.  */
4163
4164 static void
4165 add_partial_subprogram (struct partial_die_info *pdi,
4166                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4167                         int need_pc, struct dwarf2_cu *cu)
4168 {
4169   if (pdi->tag == DW_TAG_subprogram)
4170     {
4171       if (pdi->has_pc_info)
4172         {
4173           if (pdi->lowpc < *lowpc)
4174             *lowpc = pdi->lowpc;
4175           if (pdi->highpc > *highpc)
4176             *highpc = pdi->highpc;
4177           if (need_pc)
4178             {
4179               CORE_ADDR baseaddr;
4180               struct objfile *objfile = cu->objfile;
4181
4182               baseaddr = ANOFFSET (objfile->section_offsets,
4183                                    SECT_OFF_TEXT (objfile));
4184               addrmap_set_empty (objfile->psymtabs_addrmap,
4185                                  pdi->lowpc + baseaddr,
4186                                  pdi->highpc - 1 + baseaddr,
4187                                  cu->per_cu->v.psymtab);
4188             }
4189           if (!pdi->is_declaration)
4190             /* Ignore subprogram DIEs that do not have a name, they are
4191                illegal.  Do not emit a complaint at this point, we will
4192                do so when we convert this psymtab into a symtab.  */
4193             if (pdi->name)
4194               add_partial_symbol (pdi, cu);
4195         }
4196     }
4197
4198   if (! pdi->has_children)
4199     return;
4200
4201   if (cu->language == language_ada)
4202     {
4203       pdi = pdi->die_child;
4204       while (pdi != NULL)
4205         {
4206           fixup_partial_die (pdi, cu);
4207           if (pdi->tag == DW_TAG_subprogram
4208               || pdi->tag == DW_TAG_lexical_block)
4209             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4210           pdi = pdi->die_sibling;
4211         }
4212     }
4213 }
4214
4215 /* Read a partial die corresponding to an enumeration type.  */
4216
4217 static void
4218 add_partial_enumeration (struct partial_die_info *enum_pdi,
4219                          struct dwarf2_cu *cu)
4220 {
4221   struct partial_die_info *pdi;
4222
4223   if (enum_pdi->name != NULL)
4224     add_partial_symbol (enum_pdi, cu);
4225
4226   pdi = enum_pdi->die_child;
4227   while (pdi)
4228     {
4229       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4230         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4231       else
4232         add_partial_symbol (pdi, cu);
4233       pdi = pdi->die_sibling;
4234     }
4235 }
4236
4237 /* Return the initial uleb128 in the die at INFO_PTR.  */
4238
4239 static unsigned int
4240 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4241 {
4242   unsigned int bytes_read;
4243
4244   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4245 }
4246
4247 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4248    Return the corresponding abbrev, or NULL if the number is zero (indicating
4249    an empty DIE).  In either case *BYTES_READ will be set to the length of
4250    the initial number.  */
4251
4252 static struct abbrev_info *
4253 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4254                  struct dwarf2_cu *cu)
4255 {
4256   bfd *abfd = cu->objfile->obfd;
4257   unsigned int abbrev_number;
4258   struct abbrev_info *abbrev;
4259
4260   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4261
4262   if (abbrev_number == 0)
4263     return NULL;
4264
4265   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4266   if (!abbrev)
4267     {
4268       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4269              abbrev_number, bfd_get_filename (abfd));
4270     }
4271
4272   return abbrev;
4273 }
4274
4275 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4276    Returns a pointer to the end of a series of DIEs, terminated by an empty
4277    DIE.  Any children of the skipped DIEs will also be skipped.  */
4278
4279 static gdb_byte *
4280 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4281 {
4282   struct abbrev_info *abbrev;
4283   unsigned int bytes_read;
4284
4285   while (1)
4286     {
4287       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4288       if (abbrev == NULL)
4289         return info_ptr + bytes_read;
4290       else
4291         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4292     }
4293 }
4294
4295 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4296    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4297    abbrev corresponding to that skipped uleb128 should be passed in
4298    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4299    children.  */
4300
4301 static gdb_byte *
4302 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4303               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4304 {
4305   unsigned int bytes_read;
4306   struct attribute attr;
4307   bfd *abfd = cu->objfile->obfd;
4308   unsigned int form, i;
4309
4310   for (i = 0; i < abbrev->num_attrs; i++)
4311     {
4312       /* The only abbrev we care about is DW_AT_sibling.  */
4313       if (abbrev->attrs[i].name == DW_AT_sibling)
4314         {
4315           read_attribute (&attr, &abbrev->attrs[i],
4316                           abfd, info_ptr, cu);
4317           if (attr.form == DW_FORM_ref_addr)
4318             complaint (&symfile_complaints,
4319                        _("ignoring absolute DW_AT_sibling"));
4320           else
4321             return buffer + dwarf2_get_ref_die_offset (&attr);
4322         }
4323
4324       /* If it isn't DW_AT_sibling, skip this attribute.  */
4325       form = abbrev->attrs[i].form;
4326     skip_attribute:
4327       switch (form)
4328         {
4329         case DW_FORM_ref_addr:
4330           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4331              and later it is offset sized.  */
4332           if (cu->header.version == 2)
4333             info_ptr += cu->header.addr_size;
4334           else
4335             info_ptr += cu->header.offset_size;
4336           break;
4337         case DW_FORM_addr:
4338           info_ptr += cu->header.addr_size;
4339           break;
4340         case DW_FORM_data1:
4341         case DW_FORM_ref1:
4342         case DW_FORM_flag:
4343           info_ptr += 1;
4344           break;
4345         case DW_FORM_flag_present:
4346           break;
4347         case DW_FORM_data2:
4348         case DW_FORM_ref2:
4349           info_ptr += 2;
4350           break;
4351         case DW_FORM_data4:
4352         case DW_FORM_ref4:
4353           info_ptr += 4;
4354           break;
4355         case DW_FORM_data8:
4356         case DW_FORM_ref8:
4357         case DW_FORM_ref_sig8:
4358           info_ptr += 8;
4359           break;
4360         case DW_FORM_string:
4361           read_direct_string (abfd, info_ptr, &bytes_read);
4362           info_ptr += bytes_read;
4363           break;
4364         case DW_FORM_sec_offset:
4365         case DW_FORM_strp:
4366           info_ptr += cu->header.offset_size;
4367           break;
4368         case DW_FORM_exprloc:
4369         case DW_FORM_block:
4370           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4371           info_ptr += bytes_read;
4372           break;
4373         case DW_FORM_block1:
4374           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4375           break;
4376         case DW_FORM_block2:
4377           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4378           break;
4379         case DW_FORM_block4:
4380           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4381           break;
4382         case DW_FORM_sdata:
4383         case DW_FORM_udata:
4384         case DW_FORM_ref_udata:
4385           info_ptr = skip_leb128 (abfd, info_ptr);
4386           break;
4387         case DW_FORM_indirect:
4388           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4389           info_ptr += bytes_read;
4390           /* We need to continue parsing from here, so just go back to
4391              the top.  */
4392           goto skip_attribute;
4393
4394         default:
4395           error (_("Dwarf Error: Cannot handle %s "
4396                    "in DWARF reader [in module %s]"),
4397                  dwarf_form_name (form),
4398                  bfd_get_filename (abfd));
4399         }
4400     }
4401
4402   if (abbrev->has_children)
4403     return skip_children (buffer, info_ptr, cu);
4404   else
4405     return info_ptr;
4406 }
4407
4408 /* Locate ORIG_PDI's sibling.
4409    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4410    in BUFFER.  */
4411
4412 static gdb_byte *
4413 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4414                     gdb_byte *buffer, gdb_byte *info_ptr,
4415                     bfd *abfd, struct dwarf2_cu *cu)
4416 {
4417   /* Do we know the sibling already?  */
4418
4419   if (orig_pdi->sibling)
4420     return orig_pdi->sibling;
4421
4422   /* Are there any children to deal with?  */
4423
4424   if (!orig_pdi->has_children)
4425     return info_ptr;
4426
4427   /* Skip the children the long way.  */
4428
4429   return skip_children (buffer, info_ptr, cu);
4430 }
4431
4432 /* Expand this partial symbol table into a full symbol table.  */
4433
4434 static void
4435 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4436 {
4437   if (pst != NULL)
4438     {
4439       if (pst->readin)
4440         {
4441           warning (_("bug: psymtab for %s is already read in."),
4442                    pst->filename);
4443         }
4444       else
4445         {
4446           if (info_verbose)
4447             {
4448               printf_filtered (_("Reading in symbols for %s..."),
4449                                pst->filename);
4450               gdb_flush (gdb_stdout);
4451             }
4452
4453           /* Restore our global data.  */
4454           dwarf2_per_objfile = objfile_data (pst->objfile,
4455                                              dwarf2_objfile_data_key);
4456
4457           /* If this psymtab is constructed from a debug-only objfile, the
4458              has_section_at_zero flag will not necessarily be correct.  We
4459              can get the correct value for this flag by looking at the data
4460              associated with the (presumably stripped) associated objfile.  */
4461           if (pst->objfile->separate_debug_objfile_backlink)
4462             {
4463               struct dwarf2_per_objfile *dpo_backlink
4464                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4465                                 dwarf2_objfile_data_key);
4466
4467               dwarf2_per_objfile->has_section_at_zero
4468                 = dpo_backlink->has_section_at_zero;
4469             }
4470
4471           dwarf2_per_objfile->reading_partial_symbols = 0;
4472
4473           psymtab_to_symtab_1 (pst);
4474
4475           /* Finish up the debug error message.  */
4476           if (info_verbose)
4477             printf_filtered (_("done.\n"));
4478         }
4479     }
4480 }
4481 \f
4482 /* Reading in full CUs.  */
4483
4484 /* Add PER_CU to the queue.  */
4485
4486 static void
4487 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4488 {
4489   struct dwarf2_queue_item *item;
4490
4491   per_cu->queued = 1;
4492   item = xmalloc (sizeof (*item));
4493   item->per_cu = per_cu;
4494   item->next = NULL;
4495
4496   if (dwarf2_queue == NULL)
4497     dwarf2_queue = item;
4498   else
4499     dwarf2_queue_tail->next = item;
4500
4501   dwarf2_queue_tail = item;
4502 }
4503
4504 /* Process the queue.  */
4505
4506 static void
4507 process_queue (void)
4508 {
4509   struct dwarf2_queue_item *item, *next_item;
4510
4511   /* The queue starts out with one item, but following a DIE reference
4512      may load a new CU, adding it to the end of the queue.  */
4513   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4514     {
4515       if (dwarf2_per_objfile->using_index
4516           ? !item->per_cu->v.quick->symtab
4517           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4518         process_full_comp_unit (item->per_cu);
4519
4520       item->per_cu->queued = 0;
4521       next_item = item->next;
4522       xfree (item);
4523     }
4524
4525   dwarf2_queue_tail = NULL;
4526 }
4527
4528 /* Free all allocated queue entries.  This function only releases anything if
4529    an error was thrown; if the queue was processed then it would have been
4530    freed as we went along.  */
4531
4532 static void
4533 dwarf2_release_queue (void *dummy)
4534 {
4535   struct dwarf2_queue_item *item, *last;
4536
4537   item = dwarf2_queue;
4538   while (item)
4539     {
4540       /* Anything still marked queued is likely to be in an
4541          inconsistent state, so discard it.  */
4542       if (item->per_cu->queued)
4543         {
4544           if (item->per_cu->cu != NULL)
4545             free_one_cached_comp_unit (item->per_cu->cu);
4546           item->per_cu->queued = 0;
4547         }
4548
4549       last = item;
4550       item = item->next;
4551       xfree (last);
4552     }
4553
4554   dwarf2_queue = dwarf2_queue_tail = NULL;
4555 }
4556
4557 /* Read in full symbols for PST, and anything it depends on.  */
4558
4559 static void
4560 psymtab_to_symtab_1 (struct partial_symtab *pst)
4561 {
4562   struct dwarf2_per_cu_data *per_cu;
4563   struct cleanup *back_to;
4564   int i;
4565
4566   for (i = 0; i < pst->number_of_dependencies; i++)
4567     if (!pst->dependencies[i]->readin)
4568       {
4569         /* Inform about additional files that need to be read in.  */
4570         if (info_verbose)
4571           {
4572             /* FIXME: i18n: Need to make this a single string.  */
4573             fputs_filtered (" ", gdb_stdout);
4574             wrap_here ("");
4575             fputs_filtered ("and ", gdb_stdout);
4576             wrap_here ("");
4577             printf_filtered ("%s...", pst->dependencies[i]->filename);
4578             wrap_here ("");     /* Flush output.  */
4579             gdb_flush (gdb_stdout);
4580           }
4581         psymtab_to_symtab_1 (pst->dependencies[i]);
4582       }
4583
4584   per_cu = pst->read_symtab_private;
4585
4586   if (per_cu == NULL)
4587     {
4588       /* It's an include file, no symbols to read for it.
4589          Everything is in the parent symtab.  */
4590       pst->readin = 1;
4591       return;
4592     }
4593
4594   dw2_do_instantiate_symtab (per_cu);
4595 }
4596
4597 /* Load the DIEs associated with PER_CU into memory.  */
4598
4599 static void
4600 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4601 {
4602   struct objfile *objfile = per_cu->objfile;
4603   bfd *abfd = objfile->obfd;
4604   struct dwarf2_cu *cu;
4605   unsigned int offset;
4606   gdb_byte *info_ptr, *beg_of_comp_unit;
4607   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4608   struct attribute *attr;
4609   int read_cu = 0;
4610
4611   gdb_assert (! per_cu->debug_types_section);
4612
4613   /* Set local variables from the partial symbol table info.  */
4614   offset = per_cu->offset;
4615
4616   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4617   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4618   beg_of_comp_unit = info_ptr;
4619
4620   if (per_cu->cu == NULL)
4621     {
4622       cu = xmalloc (sizeof (*cu));
4623       init_one_comp_unit (cu, per_cu);
4624
4625       read_cu = 1;
4626
4627       /* If an error occurs while loading, release our storage.  */
4628       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4629
4630       /* Read in the comp_unit header.  */
4631       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4632
4633       /* Skip dummy compilation units.  */
4634       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4635                        + dwarf2_per_objfile->info.size)
4636           || peek_abbrev_code (abfd, info_ptr) == 0)
4637         {
4638           do_cleanups (free_cu_cleanup);
4639           return;
4640         }
4641
4642       /* Complete the cu_header.  */
4643       cu->header.offset = offset;
4644       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4645
4646       /* Read the abbrevs for this compilation unit.  */
4647       dwarf2_read_abbrevs (cu);
4648       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4649
4650       /* Link this CU into read_in_chain.  */
4651       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4652       dwarf2_per_objfile->read_in_chain = per_cu;
4653     }
4654   else
4655     {
4656       cu = per_cu->cu;
4657       info_ptr += cu->header.first_die_offset;
4658     }
4659
4660   cu->dies = read_comp_unit (info_ptr, cu);
4661
4662   /* We try not to read any attributes in this function, because not
4663      all CUs needed for references have been loaded yet, and symbol
4664      table processing isn't initialized.  But we have to set the CU language,
4665      or we won't be able to build types correctly.  */
4666   prepare_one_comp_unit (cu, cu->dies);
4667
4668   /* Similarly, if we do not read the producer, we can not apply
4669      producer-specific interpretation.  */
4670   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4671   if (attr)
4672     cu->producer = DW_STRING (attr);
4673
4674   if (read_cu)
4675     {
4676       do_cleanups (free_abbrevs_cleanup);
4677
4678       /* We've successfully allocated this compilation unit.  Let our
4679          caller clean it up when finished with it.  */
4680       discard_cleanups (free_cu_cleanup);
4681     }
4682 }
4683
4684 /* Add a DIE to the delayed physname list.  */
4685
4686 static void
4687 add_to_method_list (struct type *type, int fnfield_index, int index,
4688                     const char *name, struct die_info *die,
4689                     struct dwarf2_cu *cu)
4690 {
4691   struct delayed_method_info mi;
4692   mi.type = type;
4693   mi.fnfield_index = fnfield_index;
4694   mi.index = index;
4695   mi.name = name;
4696   mi.die = die;
4697   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4698 }
4699
4700 /* A cleanup for freeing the delayed method list.  */
4701
4702 static void
4703 free_delayed_list (void *ptr)
4704 {
4705   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4706   if (cu->method_list != NULL)
4707     {
4708       VEC_free (delayed_method_info, cu->method_list);
4709       cu->method_list = NULL;
4710     }
4711 }
4712
4713 /* Compute the physnames of any methods on the CU's method list.
4714
4715    The computation of method physnames is delayed in order to avoid the
4716    (bad) condition that one of the method's formal parameters is of an as yet
4717    incomplete type.  */
4718
4719 static void
4720 compute_delayed_physnames (struct dwarf2_cu *cu)
4721 {
4722   int i;
4723   struct delayed_method_info *mi;
4724   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4725     {
4726       const char *physname;
4727       struct fn_fieldlist *fn_flp
4728         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4729       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4730       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4731     }
4732 }
4733
4734 /* Generate full symbol information for PER_CU, whose DIEs have
4735    already been loaded into memory.  */
4736
4737 static void
4738 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4739 {
4740   struct dwarf2_cu *cu = per_cu->cu;
4741   struct objfile *objfile = per_cu->objfile;
4742   CORE_ADDR lowpc, highpc;
4743   struct symtab *symtab;
4744   struct cleanup *back_to, *delayed_list_cleanup;
4745   CORE_ADDR baseaddr;
4746
4747   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4748
4749   buildsym_init ();
4750   back_to = make_cleanup (really_free_pendings, NULL);
4751   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4752
4753   cu->list_in_scope = &file_symbols;
4754
4755   /* Do line number decoding in read_file_scope () */
4756   process_die (cu->dies, cu);
4757
4758   /* Now that we have processed all the DIEs in the CU, all the types 
4759      should be complete, and it should now be safe to compute all of the
4760      physnames.  */
4761   compute_delayed_physnames (cu);
4762   do_cleanups (delayed_list_cleanup);
4763
4764   /* Some compilers don't define a DW_AT_high_pc attribute for the
4765      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4766      it, by scanning the DIE's below the compilation unit.  */
4767   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4768
4769   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4770
4771   if (symtab != NULL)
4772     {
4773       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4774
4775       /* Set symtab language to language from DW_AT_language.  If the
4776          compilation is from a C file generated by language preprocessors, do
4777          not set the language if it was already deduced by start_subfile.  */
4778       if (!(cu->language == language_c && symtab->language != language_c))
4779         symtab->language = cu->language;
4780
4781       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4782          produce DW_AT_location with location lists but it can be possibly
4783          invalid without -fvar-tracking.
4784
4785          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4786          needed, it would be wrong due to missing DW_AT_producer there.
4787
4788          Still one can confuse GDB by using non-standard GCC compilation
4789          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4790          */ 
4791       if (cu->has_loclist && gcc_4_minor >= 0)
4792         symtab->locations_valid = 1;
4793
4794       if (gcc_4_minor >= 5)
4795         symtab->epilogue_unwind_valid = 1;
4796
4797       symtab->call_site_htab = cu->call_site_htab;
4798     }
4799
4800   if (dwarf2_per_objfile->using_index)
4801     per_cu->v.quick->symtab = symtab;
4802   else
4803     {
4804       struct partial_symtab *pst = per_cu->v.psymtab;
4805       pst->symtab = symtab;
4806       pst->readin = 1;
4807     }
4808
4809   do_cleanups (back_to);
4810 }
4811
4812 /* Process a die and its children.  */
4813
4814 static void
4815 process_die (struct die_info *die, struct dwarf2_cu *cu)
4816 {
4817   switch (die->tag)
4818     {
4819     case DW_TAG_padding:
4820       break;
4821     case DW_TAG_compile_unit:
4822       read_file_scope (die, cu);
4823       break;
4824     case DW_TAG_type_unit:
4825       read_type_unit_scope (die, cu);
4826       break;
4827     case DW_TAG_subprogram:
4828     case DW_TAG_inlined_subroutine:
4829       read_func_scope (die, cu);
4830       break;
4831     case DW_TAG_lexical_block:
4832     case DW_TAG_try_block:
4833     case DW_TAG_catch_block:
4834       read_lexical_block_scope (die, cu);
4835       break;
4836     case DW_TAG_GNU_call_site:
4837       read_call_site_scope (die, cu);
4838       break;
4839     case DW_TAG_class_type:
4840     case DW_TAG_interface_type:
4841     case DW_TAG_structure_type:
4842     case DW_TAG_union_type:
4843       process_structure_scope (die, cu);
4844       break;
4845     case DW_TAG_enumeration_type:
4846       process_enumeration_scope (die, cu);
4847       break;
4848
4849     /* These dies have a type, but processing them does not create
4850        a symbol or recurse to process the children.  Therefore we can
4851        read them on-demand through read_type_die.  */
4852     case DW_TAG_subroutine_type:
4853     case DW_TAG_set_type:
4854     case DW_TAG_array_type:
4855     case DW_TAG_pointer_type:
4856     case DW_TAG_ptr_to_member_type:
4857     case DW_TAG_reference_type:
4858     case DW_TAG_string_type:
4859       break;
4860
4861     case DW_TAG_base_type:
4862     case DW_TAG_subrange_type:
4863     case DW_TAG_typedef:
4864       /* Add a typedef symbol for the type definition, if it has a
4865          DW_AT_name.  */
4866       new_symbol (die, read_type_die (die, cu), cu);
4867       break;
4868     case DW_TAG_common_block:
4869       read_common_block (die, cu);
4870       break;
4871     case DW_TAG_common_inclusion:
4872       break;
4873     case DW_TAG_namespace:
4874       processing_has_namespace_info = 1;
4875       read_namespace (die, cu);
4876       break;
4877     case DW_TAG_module:
4878       processing_has_namespace_info = 1;
4879       read_module (die, cu);
4880       break;
4881     case DW_TAG_imported_declaration:
4882     case DW_TAG_imported_module:
4883       processing_has_namespace_info = 1;
4884       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4885                                  || cu->language != language_fortran))
4886         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4887                    dwarf_tag_name (die->tag));
4888       read_import_statement (die, cu);
4889       break;
4890     default:
4891       new_symbol (die, NULL, cu);
4892       break;
4893     }
4894 }
4895
4896 /* A helper function for dwarf2_compute_name which determines whether DIE
4897    needs to have the name of the scope prepended to the name listed in the
4898    die.  */
4899
4900 static int
4901 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4902 {
4903   struct attribute *attr;
4904
4905   switch (die->tag)
4906     {
4907     case DW_TAG_namespace:
4908     case DW_TAG_typedef:
4909     case DW_TAG_class_type:
4910     case DW_TAG_interface_type:
4911     case DW_TAG_structure_type:
4912     case DW_TAG_union_type:
4913     case DW_TAG_enumeration_type:
4914     case DW_TAG_enumerator:
4915     case DW_TAG_subprogram:
4916     case DW_TAG_member:
4917       return 1;
4918
4919     case DW_TAG_variable:
4920     case DW_TAG_constant:
4921       /* We only need to prefix "globally" visible variables.  These include
4922          any variable marked with DW_AT_external or any variable that
4923          lives in a namespace.  [Variables in anonymous namespaces
4924          require prefixing, but they are not DW_AT_external.]  */
4925
4926       if (dwarf2_attr (die, DW_AT_specification, cu))
4927         {
4928           struct dwarf2_cu *spec_cu = cu;
4929
4930           return die_needs_namespace (die_specification (die, &spec_cu),
4931                                       spec_cu);
4932         }
4933
4934       attr = dwarf2_attr (die, DW_AT_external, cu);
4935       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4936           && die->parent->tag != DW_TAG_module)
4937         return 0;
4938       /* A variable in a lexical block of some kind does not need a
4939          namespace, even though in C++ such variables may be external
4940          and have a mangled name.  */
4941       if (die->parent->tag ==  DW_TAG_lexical_block
4942           || die->parent->tag ==  DW_TAG_try_block
4943           || die->parent->tag ==  DW_TAG_catch_block
4944           || die->parent->tag == DW_TAG_subprogram)
4945         return 0;
4946       return 1;
4947
4948     default:
4949       return 0;
4950     }
4951 }
4952
4953 /* Retrieve the last character from a mem_file.  */
4954
4955 static void
4956 do_ui_file_peek_last (void *object, const char *buffer, long length)
4957 {
4958   char *last_char_p = (char *) object;
4959
4960   if (length > 0)
4961     *last_char_p = buffer[length - 1];
4962 }
4963
4964 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4965    compute the physname for the object, which include a method's
4966    formal parameters (C++/Java) and return type (Java).
4967
4968    For Ada, return the DIE's linkage name rather than the fully qualified
4969    name.  PHYSNAME is ignored..
4970
4971    The result is allocated on the objfile_obstack and canonicalized.  */
4972
4973 static const char *
4974 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4975                      int physname)
4976 {
4977   struct objfile *objfile = cu->objfile;
4978
4979   if (name == NULL)
4980     name = dwarf2_name (die, cu);
4981
4982   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4983      compute it by typename_concat inside GDB.  */
4984   if (cu->language == language_ada
4985       || (cu->language == language_fortran && physname))
4986     {
4987       /* For Ada unit, we prefer the linkage name over the name, as
4988          the former contains the exported name, which the user expects
4989          to be able to reference.  Ideally, we want the user to be able
4990          to reference this entity using either natural or linkage name,
4991          but we haven't started looking at this enhancement yet.  */
4992       struct attribute *attr;
4993
4994       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4995       if (attr == NULL)
4996         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4997       if (attr && DW_STRING (attr))
4998         return DW_STRING (attr);
4999     }
5000
5001   /* These are the only languages we know how to qualify names in.  */
5002   if (name != NULL
5003       && (cu->language == language_cplus || cu->language == language_java
5004           || cu->language == language_fortran))
5005     {
5006       if (die_needs_namespace (die, cu))
5007         {
5008           long length;
5009           const char *prefix;
5010           struct ui_file *buf;
5011
5012           prefix = determine_prefix (die, cu);
5013           buf = mem_fileopen ();
5014           if (*prefix != '\0')
5015             {
5016               char *prefixed_name = typename_concat (NULL, prefix, name,
5017                                                      physname, cu);
5018
5019               fputs_unfiltered (prefixed_name, buf);
5020               xfree (prefixed_name);
5021             }
5022           else
5023             fputs_unfiltered (name, buf);
5024
5025           /* Template parameters may be specified in the DIE's DW_AT_name, or
5026              as children with DW_TAG_template_type_param or
5027              DW_TAG_value_type_param.  If the latter, add them to the name
5028              here.  If the name already has template parameters, then
5029              skip this step; some versions of GCC emit both, and
5030              it is more efficient to use the pre-computed name.
5031
5032              Something to keep in mind about this process: it is very
5033              unlikely, or in some cases downright impossible, to produce
5034              something that will match the mangled name of a function.
5035              If the definition of the function has the same debug info,
5036              we should be able to match up with it anyway.  But fallbacks
5037              using the minimal symbol, for instance to find a method
5038              implemented in a stripped copy of libstdc++, will not work.
5039              If we do not have debug info for the definition, we will have to
5040              match them up some other way.
5041
5042              When we do name matching there is a related problem with function
5043              templates; two instantiated function templates are allowed to
5044              differ only by their return types, which we do not add here.  */
5045
5046           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5047             {
5048               struct attribute *attr;
5049               struct die_info *child;
5050               int first = 1;
5051
5052               die->building_fullname = 1;
5053
5054               for (child = die->child; child != NULL; child = child->sibling)
5055                 {
5056                   struct type *type;
5057                   long value;
5058                   gdb_byte *bytes;
5059                   struct dwarf2_locexpr_baton *baton;
5060                   struct value *v;
5061
5062                   if (child->tag != DW_TAG_template_type_param
5063                       && child->tag != DW_TAG_template_value_param)
5064                     continue;
5065
5066                   if (first)
5067                     {
5068                       fputs_unfiltered ("<", buf);
5069                       first = 0;
5070                     }
5071                   else
5072                     fputs_unfiltered (", ", buf);
5073
5074                   attr = dwarf2_attr (child, DW_AT_type, cu);
5075                   if (attr == NULL)
5076                     {
5077                       complaint (&symfile_complaints,
5078                                  _("template parameter missing DW_AT_type"));
5079                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5080                       continue;
5081                     }
5082                   type = die_type (child, cu);
5083
5084                   if (child->tag == DW_TAG_template_type_param)
5085                     {
5086                       c_print_type (type, "", buf, -1, 0);
5087                       continue;
5088                     }
5089
5090                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5091                   if (attr == NULL)
5092                     {
5093                       complaint (&symfile_complaints,
5094                                  _("template parameter missing "
5095                                    "DW_AT_const_value"));
5096                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5097                       continue;
5098                     }
5099
5100                   dwarf2_const_value_attr (attr, type, name,
5101                                            &cu->comp_unit_obstack, cu,
5102                                            &value, &bytes, &baton);
5103
5104                   if (TYPE_NOSIGN (type))
5105                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5106                        changed, this can use value_print instead.  */
5107                     c_printchar (value, type, buf);
5108                   else
5109                     {
5110                       struct value_print_options opts;
5111
5112                       if (baton != NULL)
5113                         v = dwarf2_evaluate_loc_desc (type, NULL,
5114                                                       baton->data,
5115                                                       baton->size,
5116                                                       baton->per_cu);
5117                       else if (bytes != NULL)
5118                         {
5119                           v = allocate_value (type);
5120                           memcpy (value_contents_writeable (v), bytes,
5121                                   TYPE_LENGTH (type));
5122                         }
5123                       else
5124                         v = value_from_longest (type, value);
5125
5126                       /* Specify decimal so that we do not depend on
5127                          the radix.  */
5128                       get_formatted_print_options (&opts, 'd');
5129                       opts.raw = 1;
5130                       value_print (v, buf, &opts);
5131                       release_value (v);
5132                       value_free (v);
5133                     }
5134                 }
5135
5136               die->building_fullname = 0;
5137
5138               if (!first)
5139                 {
5140                   /* Close the argument list, with a space if necessary
5141                      (nested templates).  */
5142                   char last_char = '\0';
5143                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5144                   if (last_char == '>')
5145                     fputs_unfiltered (" >", buf);
5146                   else
5147                     fputs_unfiltered (">", buf);
5148                 }
5149             }
5150
5151           /* For Java and C++ methods, append formal parameter type
5152              information, if PHYSNAME.  */
5153
5154           if (physname && die->tag == DW_TAG_subprogram
5155               && (cu->language == language_cplus
5156                   || cu->language == language_java))
5157             {
5158               struct type *type = read_type_die (die, cu);
5159
5160               c_type_print_args (type, buf, 1, cu->language);
5161
5162               if (cu->language == language_java)
5163                 {
5164                   /* For java, we must append the return type to method
5165                      names.  */
5166                   if (die->tag == DW_TAG_subprogram)
5167                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5168                                      0, 0);
5169                 }
5170               else if (cu->language == language_cplus)
5171                 {
5172                   /* Assume that an artificial first parameter is
5173                      "this", but do not crash if it is not.  RealView
5174                      marks unnamed (and thus unused) parameters as
5175                      artificial; there is no way to differentiate
5176                      the two cases.  */
5177                   if (TYPE_NFIELDS (type) > 0
5178                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5179                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5180                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5181                                                                         0))))
5182                     fputs_unfiltered (" const", buf);
5183                 }
5184             }
5185
5186           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5187                                        &length);
5188           ui_file_delete (buf);
5189
5190           if (cu->language == language_cplus)
5191             {
5192               char *cname
5193                 = dwarf2_canonicalize_name (name, cu,
5194                                             &objfile->objfile_obstack);
5195
5196               if (cname != NULL)
5197                 name = cname;
5198             }
5199         }
5200     }
5201
5202   return name;
5203 }
5204
5205 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5206    If scope qualifiers are appropriate they will be added.  The result
5207    will be allocated on the objfile_obstack, or NULL if the DIE does
5208    not have a name.  NAME may either be from a previous call to
5209    dwarf2_name or NULL.
5210
5211    The output string will be canonicalized (if C++/Java).  */
5212
5213 static const char *
5214 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5215 {
5216   return dwarf2_compute_name (name, die, cu, 0);
5217 }
5218
5219 /* Construct a physname for the given DIE in CU.  NAME may either be
5220    from a previous call to dwarf2_name or NULL.  The result will be
5221    allocated on the objfile_objstack or NULL if the DIE does not have a
5222    name.
5223
5224    The output string will be canonicalized (if C++/Java).  */
5225
5226 static const char *
5227 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5228 {
5229   struct objfile *objfile = cu->objfile;
5230   struct attribute *attr;
5231   const char *retval, *mangled = NULL, *canon = NULL;
5232   struct cleanup *back_to;
5233   int need_copy = 1;
5234
5235   /* In this case dwarf2_compute_name is just a shortcut not building anything
5236      on its own.  */
5237   if (!die_needs_namespace (die, cu))
5238     return dwarf2_compute_name (name, die, cu, 1);
5239
5240   back_to = make_cleanup (null_cleanup, NULL);
5241
5242   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5243   if (!attr)
5244     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5245
5246   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5247      has computed.  */
5248   if (attr && DW_STRING (attr))
5249     {
5250       char *demangled;
5251
5252       mangled = DW_STRING (attr);
5253
5254       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5255          type.  It is easier for GDB users to search for such functions as
5256          `name(params)' than `long name(params)'.  In such case the minimal
5257          symbol names do not match the full symbol names but for template
5258          functions there is never a need to look up their definition from their
5259          declaration so the only disadvantage remains the minimal symbol
5260          variant `long name(params)' does not have the proper inferior type.
5261          */
5262
5263       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5264                                             | (cu->language == language_java
5265                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5266                                                : DMGL_RET_DROP)));
5267       if (demangled)
5268         {
5269           make_cleanup (xfree, demangled);
5270           canon = demangled;
5271         }
5272       else
5273         {
5274           canon = mangled;
5275           need_copy = 0;
5276         }
5277     }
5278
5279   if (canon == NULL || check_physname)
5280     {
5281       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5282
5283       if (canon != NULL && strcmp (physname, canon) != 0)
5284         {
5285           /* It may not mean a bug in GDB.  The compiler could also
5286              compute DW_AT_linkage_name incorrectly.  But in such case
5287              GDB would need to be bug-to-bug compatible.  */
5288
5289           complaint (&symfile_complaints,
5290                      _("Computed physname <%s> does not match demangled <%s> "
5291                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5292                      physname, canon, mangled, die->offset, objfile->name);
5293
5294           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5295              is available here - over computed PHYSNAME.  It is safer
5296              against both buggy GDB and buggy compilers.  */
5297
5298           retval = canon;
5299         }
5300       else
5301         {
5302           retval = physname;
5303           need_copy = 0;
5304         }
5305     }
5306   else
5307     retval = canon;
5308
5309   if (need_copy)
5310     retval = obsavestring (retval, strlen (retval),
5311                            &objfile->objfile_obstack);
5312
5313   do_cleanups (back_to);
5314   return retval;
5315 }
5316
5317 /* Read the import statement specified by the given die and record it.  */
5318
5319 static void
5320 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5321 {
5322   struct objfile *objfile = cu->objfile;
5323   struct attribute *import_attr;
5324   struct die_info *imported_die, *child_die;
5325   struct dwarf2_cu *imported_cu;
5326   const char *imported_name;
5327   const char *imported_name_prefix;
5328   const char *canonical_name;
5329   const char *import_alias;
5330   const char *imported_declaration = NULL;
5331   const char *import_prefix;
5332   VEC (const_char_ptr) *excludes = NULL;
5333   struct cleanup *cleanups;
5334
5335   char *temp;
5336
5337   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5338   if (import_attr == NULL)
5339     {
5340       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5341                  dwarf_tag_name (die->tag));
5342       return;
5343     }
5344
5345   imported_cu = cu;
5346   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5347   imported_name = dwarf2_name (imported_die, imported_cu);
5348   if (imported_name == NULL)
5349     {
5350       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5351
5352         The import in the following code:
5353         namespace A
5354           {
5355             typedef int B;
5356           }
5357
5358         int main ()
5359           {
5360             using A::B;
5361             B b;
5362             return b;
5363           }
5364
5365         ...
5366          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5367             <52>   DW_AT_decl_file   : 1
5368             <53>   DW_AT_decl_line   : 6
5369             <54>   DW_AT_import      : <0x75>
5370          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5371             <59>   DW_AT_name        : B
5372             <5b>   DW_AT_decl_file   : 1
5373             <5c>   DW_AT_decl_line   : 2
5374             <5d>   DW_AT_type        : <0x6e>
5375         ...
5376          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5377             <76>   DW_AT_byte_size   : 4
5378             <77>   DW_AT_encoding    : 5        (signed)
5379
5380         imports the wrong die ( 0x75 instead of 0x58 ).
5381         This case will be ignored until the gcc bug is fixed.  */
5382       return;
5383     }
5384
5385   /* Figure out the local name after import.  */
5386   import_alias = dwarf2_name (die, cu);
5387
5388   /* Figure out where the statement is being imported to.  */
5389   import_prefix = determine_prefix (die, cu);
5390
5391   /* Figure out what the scope of the imported die is and prepend it
5392      to the name of the imported die.  */
5393   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5394
5395   if (imported_die->tag != DW_TAG_namespace
5396       && imported_die->tag != DW_TAG_module)
5397     {
5398       imported_declaration = imported_name;
5399       canonical_name = imported_name_prefix;
5400     }
5401   else if (strlen (imported_name_prefix) > 0)
5402     {
5403       temp = alloca (strlen (imported_name_prefix)
5404                      + 2 + strlen (imported_name) + 1);
5405       strcpy (temp, imported_name_prefix);
5406       strcat (temp, "::");
5407       strcat (temp, imported_name);
5408       canonical_name = temp;
5409     }
5410   else
5411     canonical_name = imported_name;
5412
5413   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5414
5415   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5416     for (child_die = die->child; child_die && child_die->tag;
5417          child_die = sibling_die (child_die))
5418       {
5419         /* DWARF-4: A Fortran use statement with a “rename list” may be
5420            represented by an imported module entry with an import attribute
5421            referring to the module and owned entries corresponding to those
5422            entities that are renamed as part of being imported.  */
5423
5424         if (child_die->tag != DW_TAG_imported_declaration)
5425           {
5426             complaint (&symfile_complaints,
5427                        _("child DW_TAG_imported_declaration expected "
5428                          "- DIE at 0x%x [in module %s]"),
5429                        child_die->offset, objfile->name);
5430             continue;
5431           }
5432
5433         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5434         if (import_attr == NULL)
5435           {
5436             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5437                        dwarf_tag_name (child_die->tag));
5438             continue;
5439           }
5440
5441         imported_cu = cu;
5442         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5443                                               &imported_cu);
5444         imported_name = dwarf2_name (imported_die, imported_cu);
5445         if (imported_name == NULL)
5446           {
5447             complaint (&symfile_complaints,
5448                        _("child DW_TAG_imported_declaration has unknown "
5449                          "imported name - DIE at 0x%x [in module %s]"),
5450                        child_die->offset, objfile->name);
5451             continue;
5452           }
5453
5454         VEC_safe_push (const_char_ptr, excludes, imported_name);
5455
5456         process_die (child_die, cu);
5457       }
5458
5459   cp_add_using_directive (import_prefix,
5460                           canonical_name,
5461                           import_alias,
5462                           imported_declaration,
5463                           excludes,
5464                           &objfile->objfile_obstack);
5465
5466   do_cleanups (cleanups);
5467 }
5468
5469 /* Cleanup function for read_file_scope.  */
5470
5471 static void
5472 free_cu_line_header (void *arg)
5473 {
5474   struct dwarf2_cu *cu = arg;
5475
5476   free_line_header (cu->line_header);
5477   cu->line_header = NULL;
5478 }
5479
5480 static void
5481 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5482                          char **name, char **comp_dir)
5483 {
5484   struct attribute *attr;
5485
5486   *name = NULL;
5487   *comp_dir = NULL;
5488
5489   /* Find the filename.  Do not use dwarf2_name here, since the filename
5490      is not a source language identifier.  */
5491   attr = dwarf2_attr (die, DW_AT_name, cu);
5492   if (attr)
5493     {
5494       *name = DW_STRING (attr);
5495     }
5496
5497   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5498   if (attr)
5499     *comp_dir = DW_STRING (attr);
5500   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5501     {
5502       *comp_dir = ldirname (*name);
5503       if (*comp_dir != NULL)
5504         make_cleanup (xfree, *comp_dir);
5505     }
5506   if (*comp_dir != NULL)
5507     {
5508       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5509          directory, get rid of it.  */
5510       char *cp = strchr (*comp_dir, ':');
5511
5512       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5513         *comp_dir = cp + 1;
5514     }
5515
5516   if (*name == NULL)
5517     *name = "<unknown>";
5518 }
5519
5520 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5521    DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5522    COMP_DIR is the compilation directory.
5523    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
5524
5525 static void
5526 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5527                         const char *comp_dir, int want_line_info)
5528 {
5529   struct attribute *attr;
5530   struct objfile *objfile = cu->objfile;
5531   bfd *abfd = objfile->obfd;
5532
5533   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5534   if (attr)
5535     {
5536       unsigned int line_offset = DW_UNSND (attr);
5537       struct line_header *line_header
5538         = dwarf_decode_line_header (line_offset, abfd, cu);
5539
5540       if (line_header)
5541         {
5542           cu->line_header = line_header;
5543           make_cleanup (free_cu_line_header, cu);
5544           dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5545         }
5546     }
5547 }
5548
5549 /* Process DW_TAG_compile_unit.  */
5550
5551 static void
5552 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5553 {
5554   struct objfile *objfile = cu->objfile;
5555   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5556   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5557   CORE_ADDR highpc = ((CORE_ADDR) 0);
5558   struct attribute *attr;
5559   char *name = NULL;
5560   char *comp_dir = NULL;
5561   struct die_info *child_die;
5562   bfd *abfd = objfile->obfd;
5563   CORE_ADDR baseaddr;
5564
5565   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5566
5567   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5568
5569   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5570      from finish_block.  */
5571   if (lowpc == ((CORE_ADDR) -1))
5572     lowpc = highpc;
5573   lowpc += baseaddr;
5574   highpc += baseaddr;
5575
5576   find_file_and_directory (die, cu, &name, &comp_dir);
5577
5578   attr = dwarf2_attr (die, DW_AT_language, cu);
5579   if (attr)
5580     {
5581       set_cu_language (DW_UNSND (attr), cu);
5582     }
5583
5584   attr = dwarf2_attr (die, DW_AT_producer, cu);
5585   if (attr)
5586     cu->producer = DW_STRING (attr);
5587
5588   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5589      standardised yet.  As a workaround for the language detection we fall
5590      back to the DW_AT_producer string.  */
5591   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5592     cu->language = language_opencl;
5593
5594   /* We assume that we're processing GCC output.  */
5595   processing_gcc_compilation = 2;
5596
5597   processing_has_namespace_info = 0;
5598
5599   start_symtab (name, comp_dir, lowpc);
5600   record_debugformat ("DWARF 2");
5601   record_producer (cu->producer);
5602
5603   /* Decode line number information if present.  We do this before
5604      processing child DIEs, so that the line header table is available
5605      for DW_AT_decl_file.  */
5606   handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5607
5608   /* Process all dies in compilation unit.  */
5609   if (die->child != NULL)
5610     {
5611       child_die = die->child;
5612       while (child_die && child_die->tag)
5613         {
5614           process_die (child_die, cu);
5615           child_die = sibling_die (child_die);
5616         }
5617     }
5618
5619   /* Decode macro information, if present.  Dwarf 2 macro information
5620      refers to information in the line number info statement program
5621      header, so we can only read it if we've read the header
5622      successfully.  */
5623   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5624   if (attr && cu->line_header)
5625     {
5626       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5627         complaint (&symfile_complaints,
5628                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5629
5630       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5631                            comp_dir, abfd, cu,
5632                            &dwarf2_per_objfile->macro, 1);
5633     }
5634   else
5635     {
5636       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5637       if (attr && cu->line_header)
5638         {
5639           unsigned int macro_offset = DW_UNSND (attr);
5640
5641           dwarf_decode_macros (cu->line_header, macro_offset,
5642                                comp_dir, abfd, cu,
5643                                &dwarf2_per_objfile->macinfo, 0);
5644         }
5645     }
5646
5647   do_cleanups (back_to);
5648 }
5649
5650 /* Process DW_TAG_type_unit.
5651    For TUs we want to skip the first top level sibling if it's not the
5652    actual type being defined by this TU.  In this case the first top
5653    level sibling is there to provide context only.  */
5654
5655 static void
5656 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5657 {
5658   struct objfile *objfile = cu->objfile;
5659   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5660   CORE_ADDR lowpc;
5661   struct attribute *attr;
5662   char *name = NULL;
5663   char *comp_dir = NULL;
5664   struct die_info *child_die;
5665   bfd *abfd = objfile->obfd;
5666
5667   /* start_symtab needs a low pc, but we don't really have one.
5668      Do what read_file_scope would do in the absence of such info.  */
5669   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5670
5671   /* Find the filename.  Do not use dwarf2_name here, since the filename
5672      is not a source language identifier.  */
5673   attr = dwarf2_attr (die, DW_AT_name, cu);
5674   if (attr)
5675     name = DW_STRING (attr);
5676
5677   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5678   if (attr)
5679     comp_dir = DW_STRING (attr);
5680   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5681     {
5682       comp_dir = ldirname (name);
5683       if (comp_dir != NULL)
5684         make_cleanup (xfree, comp_dir);
5685     }
5686
5687   if (name == NULL)
5688     name = "<unknown>";
5689
5690   attr = dwarf2_attr (die, DW_AT_language, cu);
5691   if (attr)
5692     set_cu_language (DW_UNSND (attr), cu);
5693
5694   /* This isn't technically needed today.  It is done for symmetry
5695      with read_file_scope.  */
5696   attr = dwarf2_attr (die, DW_AT_producer, cu);
5697   if (attr)
5698     cu->producer = DW_STRING (attr);
5699
5700   /* We assume that we're processing GCC output.  */
5701   processing_gcc_compilation = 2;
5702
5703   processing_has_namespace_info = 0;
5704
5705   start_symtab (name, comp_dir, lowpc);
5706   record_debugformat ("DWARF 2");
5707   record_producer (cu->producer);
5708
5709   /* Decode line number information if present.  We do this before
5710      processing child DIEs, so that the line header table is available
5711      for DW_AT_decl_file.
5712      We don't need the pc/line-number mapping for type units.  */
5713   handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5714
5715   /* Process the dies in the type unit.  */
5716   if (die->child == NULL)
5717     {
5718       dump_die_for_error (die);
5719       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5720              bfd_get_filename (abfd));
5721     }
5722
5723   child_die = die->child;
5724
5725   while (child_die && child_die->tag)
5726     {
5727       process_die (child_die, cu);
5728
5729       child_die = sibling_die (child_die);
5730     }
5731
5732   do_cleanups (back_to);
5733 }
5734
5735 /* qsort helper for inherit_abstract_dies.  */
5736
5737 static int
5738 unsigned_int_compar (const void *ap, const void *bp)
5739 {
5740   unsigned int a = *(unsigned int *) ap;
5741   unsigned int b = *(unsigned int *) bp;
5742
5743   return (a > b) - (b > a);
5744 }
5745
5746 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5747    Inherit only the children of the DW_AT_abstract_origin DIE not being
5748    already referenced by DW_AT_abstract_origin from the children of the
5749    current DIE.  */
5750
5751 static void
5752 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5753 {
5754   struct die_info *child_die;
5755   unsigned die_children_count;
5756   /* CU offsets which were referenced by children of the current DIE.  */
5757   unsigned *offsets;
5758   unsigned *offsets_end, *offsetp;
5759   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5760   struct die_info *origin_die;
5761   /* Iterator of the ORIGIN_DIE children.  */
5762   struct die_info *origin_child_die;
5763   struct cleanup *cleanups;
5764   struct attribute *attr;
5765   struct dwarf2_cu *origin_cu;
5766   struct pending **origin_previous_list_in_scope;
5767
5768   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5769   if (!attr)
5770     return;
5771
5772   /* Note that following die references may follow to a die in a
5773      different cu.  */
5774
5775   origin_cu = cu;
5776   origin_die = follow_die_ref (die, attr, &origin_cu);
5777
5778   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5779      symbols in.  */
5780   origin_previous_list_in_scope = origin_cu->list_in_scope;
5781   origin_cu->list_in_scope = cu->list_in_scope;
5782
5783   if (die->tag != origin_die->tag
5784       && !(die->tag == DW_TAG_inlined_subroutine
5785            && origin_die->tag == DW_TAG_subprogram))
5786     complaint (&symfile_complaints,
5787                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5788                die->offset, origin_die->offset);
5789
5790   child_die = die->child;
5791   die_children_count = 0;
5792   while (child_die && child_die->tag)
5793     {
5794       child_die = sibling_die (child_die);
5795       die_children_count++;
5796     }
5797   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5798   cleanups = make_cleanup (xfree, offsets);
5799
5800   offsets_end = offsets;
5801   child_die = die->child;
5802   while (child_die && child_die->tag)
5803     {
5804       /* For each CHILD_DIE, find the corresponding child of
5805          ORIGIN_DIE.  If there is more than one layer of
5806          DW_AT_abstract_origin, follow them all; there shouldn't be,
5807          but GCC versions at least through 4.4 generate this (GCC PR
5808          40573).  */
5809       struct die_info *child_origin_die = child_die;
5810       struct dwarf2_cu *child_origin_cu = cu;
5811
5812       while (1)
5813         {
5814           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5815                               child_origin_cu);
5816           if (attr == NULL)
5817             break;
5818           child_origin_die = follow_die_ref (child_origin_die, attr,
5819                                              &child_origin_cu);
5820         }
5821
5822       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5823          counterpart may exist.  */
5824       if (child_origin_die != child_die)
5825         {
5826           if (child_die->tag != child_origin_die->tag
5827               && !(child_die->tag == DW_TAG_inlined_subroutine
5828                    && child_origin_die->tag == DW_TAG_subprogram))
5829             complaint (&symfile_complaints,
5830                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5831                          "different tags"), child_die->offset,
5832                        child_origin_die->offset);
5833           if (child_origin_die->parent != origin_die)
5834             complaint (&symfile_complaints,
5835                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5836                          "different parents"), child_die->offset,
5837                        child_origin_die->offset);
5838           else
5839             *offsets_end++ = child_origin_die->offset;
5840         }
5841       child_die = sibling_die (child_die);
5842     }
5843   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5844          unsigned_int_compar);
5845   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5846     if (offsetp[-1] == *offsetp)
5847       complaint (&symfile_complaints,
5848                  _("Multiple children of DIE 0x%x refer "
5849                    "to DIE 0x%x as their abstract origin"),
5850                  die->offset, *offsetp);
5851
5852   offsetp = offsets;
5853   origin_child_die = origin_die->child;
5854   while (origin_child_die && origin_child_die->tag)
5855     {
5856       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5857       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5858         offsetp++;
5859       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5860         {
5861           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5862           process_die (origin_child_die, origin_cu);
5863         }
5864       origin_child_die = sibling_die (origin_child_die);
5865     }
5866   origin_cu->list_in_scope = origin_previous_list_in_scope;
5867
5868   do_cleanups (cleanups);
5869 }
5870
5871 static void
5872 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5873 {
5874   struct objfile *objfile = cu->objfile;
5875   struct context_stack *new;
5876   CORE_ADDR lowpc;
5877   CORE_ADDR highpc;
5878   struct die_info *child_die;
5879   struct attribute *attr, *call_line, *call_file;
5880   char *name;
5881   CORE_ADDR baseaddr;
5882   struct block *block;
5883   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5884   VEC (symbolp) *template_args = NULL;
5885   struct template_symbol *templ_func = NULL;
5886
5887   if (inlined_func)
5888     {
5889       /* If we do not have call site information, we can't show the
5890          caller of this inlined function.  That's too confusing, so
5891          only use the scope for local variables.  */
5892       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5893       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5894       if (call_line == NULL || call_file == NULL)
5895         {
5896           read_lexical_block_scope (die, cu);
5897           return;
5898         }
5899     }
5900
5901   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5902
5903   name = dwarf2_name (die, cu);
5904
5905   /* Ignore functions with missing or empty names.  These are actually
5906      illegal according to the DWARF standard.  */
5907   if (name == NULL)
5908     {
5909       complaint (&symfile_complaints,
5910                  _("missing name for subprogram DIE at %d"), die->offset);
5911       return;
5912     }
5913
5914   /* Ignore functions with missing or invalid low and high pc attributes.  */
5915   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5916     {
5917       attr = dwarf2_attr (die, DW_AT_external, cu);
5918       if (!attr || !DW_UNSND (attr))
5919         complaint (&symfile_complaints,
5920                    _("cannot get low and high bounds "
5921                      "for subprogram DIE at %d"),
5922                    die->offset);
5923       return;
5924     }
5925
5926   lowpc += baseaddr;
5927   highpc += baseaddr;
5928
5929   /* If we have any template arguments, then we must allocate a
5930      different sort of symbol.  */
5931   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5932     {
5933       if (child_die->tag == DW_TAG_template_type_param
5934           || child_die->tag == DW_TAG_template_value_param)
5935         {
5936           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5937                                        struct template_symbol);
5938           templ_func->base.is_cplus_template_function = 1;
5939           break;
5940         }
5941     }
5942
5943   new = push_context (0, lowpc);
5944   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5945                                (struct symbol *) templ_func);
5946
5947   /* If there is a location expression for DW_AT_frame_base, record
5948      it.  */
5949   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5950   if (attr)
5951     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5952        expression is being recorded directly in the function's symbol
5953        and not in a separate frame-base object.  I guess this hack is
5954        to avoid adding some sort of frame-base adjunct/annex to the
5955        function's symbol :-(.  The problem with doing this is that it
5956        results in a function symbol with a location expression that
5957        has nothing to do with the location of the function, ouch!  The
5958        relationship should be: a function's symbol has-a frame base; a
5959        frame-base has-a location expression.  */
5960     dwarf2_symbol_mark_computed (attr, new->name, cu);
5961
5962   cu->list_in_scope = &local_symbols;
5963
5964   if (die->child != NULL)
5965     {
5966       child_die = die->child;
5967       while (child_die && child_die->tag)
5968         {
5969           if (child_die->tag == DW_TAG_template_type_param
5970               || child_die->tag == DW_TAG_template_value_param)
5971             {
5972               struct symbol *arg = new_symbol (child_die, NULL, cu);
5973
5974               if (arg != NULL)
5975                 VEC_safe_push (symbolp, template_args, arg);
5976             }
5977           else
5978             process_die (child_die, cu);
5979           child_die = sibling_die (child_die);
5980         }
5981     }
5982
5983   inherit_abstract_dies (die, cu);
5984
5985   /* If we have a DW_AT_specification, we might need to import using
5986      directives from the context of the specification DIE.  See the
5987      comment in determine_prefix.  */
5988   if (cu->language == language_cplus
5989       && dwarf2_attr (die, DW_AT_specification, cu))
5990     {
5991       struct dwarf2_cu *spec_cu = cu;
5992       struct die_info *spec_die = die_specification (die, &spec_cu);
5993
5994       while (spec_die)
5995         {
5996           child_die = spec_die->child;
5997           while (child_die && child_die->tag)
5998             {
5999               if (child_die->tag == DW_TAG_imported_module)
6000                 process_die (child_die, spec_cu);
6001               child_die = sibling_die (child_die);
6002             }
6003
6004           /* In some cases, GCC generates specification DIEs that
6005              themselves contain DW_AT_specification attributes.  */
6006           spec_die = die_specification (spec_die, &spec_cu);
6007         }
6008     }
6009
6010   new = pop_context ();
6011   /* Make a block for the local symbols within.  */
6012   block = finish_block (new->name, &local_symbols, new->old_blocks,
6013                         lowpc, highpc, objfile);
6014
6015   /* For C++, set the block's scope.  */
6016   if (cu->language == language_cplus || cu->language == language_fortran)
6017     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6018                         determine_prefix (die, cu),
6019                         processing_has_namespace_info);
6020
6021   /* If we have address ranges, record them.  */
6022   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6023
6024   /* Attach template arguments to function.  */
6025   if (! VEC_empty (symbolp, template_args))
6026     {
6027       gdb_assert (templ_func != NULL);
6028
6029       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6030       templ_func->template_arguments
6031         = obstack_alloc (&objfile->objfile_obstack,
6032                          (templ_func->n_template_arguments
6033                           * sizeof (struct symbol *)));
6034       memcpy (templ_func->template_arguments,
6035               VEC_address (symbolp, template_args),
6036               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6037       VEC_free (symbolp, template_args);
6038     }
6039
6040   /* In C++, we can have functions nested inside functions (e.g., when
6041      a function declares a class that has methods).  This means that
6042      when we finish processing a function scope, we may need to go
6043      back to building a containing block's symbol lists.  */
6044   local_symbols = new->locals;
6045   param_symbols = new->params;
6046   using_directives = new->using_directives;
6047
6048   /* If we've finished processing a top-level function, subsequent
6049      symbols go in the file symbol list.  */
6050   if (outermost_context_p ())
6051     cu->list_in_scope = &file_symbols;
6052 }
6053
6054 /* Process all the DIES contained within a lexical block scope.  Start
6055    a new scope, process the dies, and then close the scope.  */
6056
6057 static void
6058 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6059 {
6060   struct objfile *objfile = cu->objfile;
6061   struct context_stack *new;
6062   CORE_ADDR lowpc, highpc;
6063   struct die_info *child_die;
6064   CORE_ADDR baseaddr;
6065
6066   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6067
6068   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6069   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6070      as multiple lexical blocks?  Handling children in a sane way would
6071      be nasty.  Might be easier to properly extend generic blocks to
6072      describe ranges.  */
6073   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6074     return;
6075   lowpc += baseaddr;
6076   highpc += baseaddr;
6077
6078   push_context (0, lowpc);
6079   if (die->child != NULL)
6080     {
6081       child_die = die->child;
6082       while (child_die && child_die->tag)
6083         {
6084           process_die (child_die, cu);
6085           child_die = sibling_die (child_die);
6086         }
6087     }
6088   new = pop_context ();
6089
6090   if (local_symbols != NULL || using_directives != NULL)
6091     {
6092       struct block *block
6093         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6094                         highpc, objfile);
6095
6096       /* Note that recording ranges after traversing children, as we
6097          do here, means that recording a parent's ranges entails
6098          walking across all its children's ranges as they appear in
6099          the address map, which is quadratic behavior.
6100
6101          It would be nicer to record the parent's ranges before
6102          traversing its children, simply overriding whatever you find
6103          there.  But since we don't even decide whether to create a
6104          block until after we've traversed its children, that's hard
6105          to do.  */
6106       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6107     }
6108   local_symbols = new->locals;
6109   using_directives = new->using_directives;
6110 }
6111
6112 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6113
6114 static void
6115 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6116 {
6117   struct objfile *objfile = cu->objfile;
6118   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6119   CORE_ADDR pc, baseaddr;
6120   struct attribute *attr;
6121   struct call_site *call_site, call_site_local;
6122   void **slot;
6123   int nparams;
6124   struct die_info *child_die;
6125
6126   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6127
6128   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6129   if (!attr)
6130     {
6131       complaint (&symfile_complaints,
6132                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6133                    "DIE 0x%x [in module %s]"),
6134                  die->offset, objfile->name);
6135       return;
6136     }
6137   pc = DW_ADDR (attr) + baseaddr;
6138
6139   if (cu->call_site_htab == NULL)
6140     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6141                                                NULL, &objfile->objfile_obstack,
6142                                                hashtab_obstack_allocate, NULL);
6143   call_site_local.pc = pc;
6144   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6145   if (*slot != NULL)
6146     {
6147       complaint (&symfile_complaints,
6148                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6149                    "DIE 0x%x [in module %s]"),
6150                  paddress (gdbarch, pc), die->offset, objfile->name);
6151       return;
6152     }
6153
6154   /* Count parameters at the caller.  */
6155
6156   nparams = 0;
6157   for (child_die = die->child; child_die && child_die->tag;
6158        child_die = sibling_die (child_die))
6159     {
6160       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6161         {
6162           complaint (&symfile_complaints,
6163                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6164                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6165                      child_die->tag, child_die->offset, objfile->name);
6166           continue;
6167         }
6168
6169       nparams++;
6170     }
6171
6172   call_site = obstack_alloc (&objfile->objfile_obstack,
6173                              (sizeof (*call_site)
6174                               + (sizeof (*call_site->parameter)
6175                                  * (nparams - 1))));
6176   *slot = call_site;
6177   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6178   call_site->pc = pc;
6179
6180   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6181     {
6182       struct die_info *func_die;
6183
6184       /* Skip also over DW_TAG_inlined_subroutine.  */
6185       for (func_die = die->parent;
6186            func_die && func_die->tag != DW_TAG_subprogram
6187            && func_die->tag != DW_TAG_subroutine_type;
6188            func_die = func_die->parent);
6189
6190       /* DW_AT_GNU_all_call_sites is a superset
6191          of DW_AT_GNU_all_tail_call_sites.  */
6192       if (func_die
6193           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6194           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6195         {
6196           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6197              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6198              both the initial caller containing the real return address PC and
6199              the final callee containing the current PC of a chain of tail
6200              calls do not need to have the tail call list complete.  But any
6201              function candidate for a virtual tail call frame searched via
6202              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6203              determined unambiguously.  */
6204         }
6205       else
6206         {
6207           struct type *func_type = NULL;
6208
6209           if (func_die)
6210             func_type = get_die_type (func_die, cu);
6211           if (func_type != NULL)
6212             {
6213               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6214
6215               /* Enlist this call site to the function.  */
6216               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6217               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6218             }
6219           else
6220             complaint (&symfile_complaints,
6221                        _("Cannot find function owning DW_TAG_GNU_call_site "
6222                          "DIE 0x%x [in module %s]"),
6223                        die->offset, objfile->name);
6224         }
6225     }
6226
6227   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6228   if (attr == NULL)
6229     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6230   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6231   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6232     /* Keep NULL DWARF_BLOCK.  */;
6233   else if (attr_form_is_block (attr))
6234     {
6235       struct dwarf2_locexpr_baton *dlbaton;
6236
6237       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6238       dlbaton->data = DW_BLOCK (attr)->data;
6239       dlbaton->size = DW_BLOCK (attr)->size;
6240       dlbaton->per_cu = cu->per_cu;
6241
6242       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6243     }
6244   else if (is_ref_attr (attr))
6245     {
6246       struct dwarf2_cu *target_cu = cu;
6247       struct die_info *target_die;
6248
6249       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6250       gdb_assert (target_cu->objfile == objfile);
6251       if (die_is_declaration (target_die, target_cu))
6252         {
6253           const char *target_physname;
6254
6255           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6256           if (target_physname == NULL)
6257             complaint (&symfile_complaints,
6258                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6259                          "physname, for referencing DIE 0x%x [in module %s]"),
6260                        die->offset, objfile->name);
6261           else
6262             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6263         }
6264       else
6265         {
6266           CORE_ADDR lowpc;
6267
6268           /* DW_AT_entry_pc should be preferred.  */
6269           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6270             complaint (&symfile_complaints,
6271                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6272                          "low pc, for referencing DIE 0x%x [in module %s]"),
6273                        die->offset, objfile->name);
6274           else
6275             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6276         }
6277     }
6278   else
6279     complaint (&symfile_complaints,
6280                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6281                  "block nor reference, for DIE 0x%x [in module %s]"),
6282                die->offset, objfile->name);
6283
6284   call_site->per_cu = cu->per_cu;
6285
6286   for (child_die = die->child;
6287        child_die && child_die->tag;
6288        child_die = sibling_die (child_die))
6289     {
6290       struct dwarf2_locexpr_baton *dlbaton;
6291       struct call_site_parameter *parameter;
6292
6293       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6294         {
6295           /* Already printed the complaint above.  */
6296           continue;
6297         }
6298
6299       gdb_assert (call_site->parameter_count < nparams);
6300       parameter = &call_site->parameter[call_site->parameter_count];
6301
6302       /* DW_AT_location specifies the register number.  Value of the data
6303          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6304
6305       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6306       if (!attr || !attr_form_is_block (attr))
6307         {
6308           complaint (&symfile_complaints,
6309                      _("No DW_FORM_block* DW_AT_location for "
6310                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6311                      child_die->offset, objfile->name);
6312           continue;
6313         }
6314       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6315                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6316       if (parameter->dwarf_reg == -1
6317           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6318                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6319                                         &parameter->fb_offset))
6320         {
6321           complaint (&symfile_complaints,
6322                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6323                        "for DW_FORM_block* DW_AT_location for "
6324                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6325                      child_die->offset, objfile->name);
6326           continue;
6327         }
6328
6329       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6330       if (!attr_form_is_block (attr))
6331         {
6332           complaint (&symfile_complaints,
6333                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6334                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6335                      child_die->offset, objfile->name);
6336           continue;
6337         }
6338       parameter->value = DW_BLOCK (attr)->data;
6339       parameter->value_size = DW_BLOCK (attr)->size;
6340
6341       /* Parameters are not pre-cleared by memset above.  */
6342       parameter->data_value = NULL;
6343       parameter->data_value_size = 0;
6344       call_site->parameter_count++;
6345
6346       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6347       if (attr)
6348         {
6349           if (!attr_form_is_block (attr))
6350             complaint (&symfile_complaints,
6351                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6352                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6353                        child_die->offset, objfile->name);
6354           else
6355             {
6356               parameter->data_value = DW_BLOCK (attr)->data;
6357               parameter->data_value_size = DW_BLOCK (attr)->size;
6358             }
6359         }
6360     }
6361 }
6362
6363 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6364    Return 1 if the attributes are present and valid, otherwise, return 0.
6365    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6366
6367 static int
6368 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6369                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6370                     struct partial_symtab *ranges_pst)
6371 {
6372   struct objfile *objfile = cu->objfile;
6373   struct comp_unit_head *cu_header = &cu->header;
6374   bfd *obfd = objfile->obfd;
6375   unsigned int addr_size = cu_header->addr_size;
6376   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6377   /* Base address selection entry.  */
6378   CORE_ADDR base;
6379   int found_base;
6380   unsigned int dummy;
6381   gdb_byte *buffer;
6382   CORE_ADDR marker;
6383   int low_set;
6384   CORE_ADDR low = 0;
6385   CORE_ADDR high = 0;
6386   CORE_ADDR baseaddr;
6387
6388   found_base = cu->base_known;
6389   base = cu->base_address;
6390
6391   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6392   if (offset >= dwarf2_per_objfile->ranges.size)
6393     {
6394       complaint (&symfile_complaints,
6395                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6396                  offset);
6397       return 0;
6398     }
6399   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6400
6401   /* Read in the largest possible address.  */
6402   marker = read_address (obfd, buffer, cu, &dummy);
6403   if ((marker & mask) == mask)
6404     {
6405       /* If we found the largest possible address, then
6406          read the base address.  */
6407       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6408       buffer += 2 * addr_size;
6409       offset += 2 * addr_size;
6410       found_base = 1;
6411     }
6412
6413   low_set = 0;
6414
6415   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6416
6417   while (1)
6418     {
6419       CORE_ADDR range_beginning, range_end;
6420
6421       range_beginning = read_address (obfd, buffer, cu, &dummy);
6422       buffer += addr_size;
6423       range_end = read_address (obfd, buffer, cu, &dummy);
6424       buffer += addr_size;
6425       offset += 2 * addr_size;
6426
6427       /* An end of list marker is a pair of zero addresses.  */
6428       if (range_beginning == 0 && range_end == 0)
6429         /* Found the end of list entry.  */
6430         break;
6431
6432       /* Each base address selection entry is a pair of 2 values.
6433          The first is the largest possible address, the second is
6434          the base address.  Check for a base address here.  */
6435       if ((range_beginning & mask) == mask)
6436         {
6437           /* If we found the largest possible address, then
6438              read the base address.  */
6439           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6440           found_base = 1;
6441           continue;
6442         }
6443
6444       if (!found_base)
6445         {
6446           /* We have no valid base address for the ranges
6447              data.  */
6448           complaint (&symfile_complaints,
6449                      _("Invalid .debug_ranges data (no base address)"));
6450           return 0;
6451         }
6452
6453       if (range_beginning > range_end)
6454         {
6455           /* Inverted range entries are invalid.  */
6456           complaint (&symfile_complaints,
6457                      _("Invalid .debug_ranges data (inverted range)"));
6458           return 0;
6459         }
6460
6461       /* Empty range entries have no effect.  */
6462       if (range_beginning == range_end)
6463         continue;
6464
6465       range_beginning += base;
6466       range_end += base;
6467
6468       if (ranges_pst != NULL)
6469         addrmap_set_empty (objfile->psymtabs_addrmap,
6470                            range_beginning + baseaddr,
6471                            range_end - 1 + baseaddr,
6472                            ranges_pst);
6473
6474       /* FIXME: This is recording everything as a low-high
6475          segment of consecutive addresses.  We should have a
6476          data structure for discontiguous block ranges
6477          instead.  */
6478       if (! low_set)
6479         {
6480           low = range_beginning;
6481           high = range_end;
6482           low_set = 1;
6483         }
6484       else
6485         {
6486           if (range_beginning < low)
6487             low = range_beginning;
6488           if (range_end > high)
6489             high = range_end;
6490         }
6491     }
6492
6493   if (! low_set)
6494     /* If the first entry is an end-of-list marker, the range
6495        describes an empty scope, i.e. no instructions.  */
6496     return 0;
6497
6498   if (low_return)
6499     *low_return = low;
6500   if (high_return)
6501     *high_return = high;
6502   return 1;
6503 }
6504
6505 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6506    are present and valid, otherwise, return 0.  Return -1 if the range is
6507    discontinuous, i.e. derived from DW_AT_ranges information.  */
6508 static int
6509 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6510                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6511                       struct partial_symtab *pst)
6512 {
6513   struct attribute *attr;
6514   CORE_ADDR low = 0;
6515   CORE_ADDR high = 0;
6516   int ret = 0;
6517
6518   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6519   if (attr)
6520     {
6521       high = DW_ADDR (attr);
6522       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6523       if (attr)
6524         low = DW_ADDR (attr);
6525       else
6526         /* Found high w/o low attribute.  */
6527         return 0;
6528
6529       /* Found consecutive range of addresses.  */
6530       ret = 1;
6531     }
6532   else
6533     {
6534       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6535       if (attr != NULL)
6536         {
6537           /* Value of the DW_AT_ranges attribute is the offset in the
6538              .debug_ranges section.  */
6539           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6540             return 0;
6541           /* Found discontinuous range of addresses.  */
6542           ret = -1;
6543         }
6544     }
6545
6546   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6547   if (high <= low)
6548     return 0;
6549
6550   /* When using the GNU linker, .gnu.linkonce. sections are used to
6551      eliminate duplicate copies of functions and vtables and such.
6552      The linker will arbitrarily choose one and discard the others.
6553      The AT_*_pc values for such functions refer to local labels in
6554      these sections.  If the section from that file was discarded, the
6555      labels are not in the output, so the relocs get a value of 0.
6556      If this is a discarded function, mark the pc bounds as invalid,
6557      so that GDB will ignore it.  */
6558   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6559     return 0;
6560
6561   *lowpc = low;
6562   if (highpc)
6563     *highpc = high;
6564   return ret;
6565 }
6566
6567 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6568    its low and high PC addresses.  Do nothing if these addresses could not
6569    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6570    and HIGHPC to the high address if greater than HIGHPC.  */
6571
6572 static void
6573 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6574                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6575                                  struct dwarf2_cu *cu)
6576 {
6577   CORE_ADDR low, high;
6578   struct die_info *child = die->child;
6579
6580   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6581     {
6582       *lowpc = min (*lowpc, low);
6583       *highpc = max (*highpc, high);
6584     }
6585
6586   /* If the language does not allow nested subprograms (either inside
6587      subprograms or lexical blocks), we're done.  */
6588   if (cu->language != language_ada)
6589     return;
6590
6591   /* Check all the children of the given DIE.  If it contains nested
6592      subprograms, then check their pc bounds.  Likewise, we need to
6593      check lexical blocks as well, as they may also contain subprogram
6594      definitions.  */
6595   while (child && child->tag)
6596     {
6597       if (child->tag == DW_TAG_subprogram
6598           || child->tag == DW_TAG_lexical_block)
6599         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6600       child = sibling_die (child);
6601     }
6602 }
6603
6604 /* Get the low and high pc's represented by the scope DIE, and store
6605    them in *LOWPC and *HIGHPC.  If the correct values can't be
6606    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6607
6608 static void
6609 get_scope_pc_bounds (struct die_info *die,
6610                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6611                      struct dwarf2_cu *cu)
6612 {
6613   CORE_ADDR best_low = (CORE_ADDR) -1;
6614   CORE_ADDR best_high = (CORE_ADDR) 0;
6615   CORE_ADDR current_low, current_high;
6616
6617   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6618     {
6619       best_low = current_low;
6620       best_high = current_high;
6621     }
6622   else
6623     {
6624       struct die_info *child = die->child;
6625
6626       while (child && child->tag)
6627         {
6628           switch (child->tag) {
6629           case DW_TAG_subprogram:
6630             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6631             break;
6632           case DW_TAG_namespace:
6633           case DW_TAG_module:
6634             /* FIXME: carlton/2004-01-16: Should we do this for
6635                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6636                that current GCC's always emit the DIEs corresponding
6637                to definitions of methods of classes as children of a
6638                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6639                the DIEs giving the declarations, which could be
6640                anywhere).  But I don't see any reason why the
6641                standards says that they have to be there.  */
6642             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6643
6644             if (current_low != ((CORE_ADDR) -1))
6645               {
6646                 best_low = min (best_low, current_low);
6647                 best_high = max (best_high, current_high);
6648               }
6649             break;
6650           default:
6651             /* Ignore.  */
6652             break;
6653           }
6654
6655           child = sibling_die (child);
6656         }
6657     }
6658
6659   *lowpc = best_low;
6660   *highpc = best_high;
6661 }
6662
6663 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6664    in DIE.  */
6665 static void
6666 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6667                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6668 {
6669   struct objfile *objfile = cu->objfile;
6670   struct attribute *attr;
6671
6672   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6673   if (attr)
6674     {
6675       CORE_ADDR high = DW_ADDR (attr);
6676
6677       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6678       if (attr)
6679         {
6680           CORE_ADDR low = DW_ADDR (attr);
6681
6682           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6683         }
6684     }
6685
6686   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6687   if (attr)
6688     {
6689       bfd *obfd = objfile->obfd;
6690
6691       /* The value of the DW_AT_ranges attribute is the offset of the
6692          address range list in the .debug_ranges section.  */
6693       unsigned long offset = DW_UNSND (attr);
6694       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6695
6696       /* For some target architectures, but not others, the
6697          read_address function sign-extends the addresses it returns.
6698          To recognize base address selection entries, we need a
6699          mask.  */
6700       unsigned int addr_size = cu->header.addr_size;
6701       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6702
6703       /* The base address, to which the next pair is relative.  Note
6704          that this 'base' is a DWARF concept: most entries in a range
6705          list are relative, to reduce the number of relocs against the
6706          debugging information.  This is separate from this function's
6707          'baseaddr' argument, which GDB uses to relocate debugging
6708          information from a shared library based on the address at
6709          which the library was loaded.  */
6710       CORE_ADDR base = cu->base_address;
6711       int base_known = cu->base_known;
6712
6713       gdb_assert (dwarf2_per_objfile->ranges.readin);
6714       if (offset >= dwarf2_per_objfile->ranges.size)
6715         {
6716           complaint (&symfile_complaints,
6717                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6718                      offset);
6719           return;
6720         }
6721
6722       for (;;)
6723         {
6724           unsigned int bytes_read;
6725           CORE_ADDR start, end;
6726
6727           start = read_address (obfd, buffer, cu, &bytes_read);
6728           buffer += bytes_read;
6729           end = read_address (obfd, buffer, cu, &bytes_read);
6730           buffer += bytes_read;
6731
6732           /* Did we find the end of the range list?  */
6733           if (start == 0 && end == 0)
6734             break;
6735
6736           /* Did we find a base address selection entry?  */
6737           else if ((start & base_select_mask) == base_select_mask)
6738             {
6739               base = end;
6740               base_known = 1;
6741             }
6742
6743           /* We found an ordinary address range.  */
6744           else
6745             {
6746               if (!base_known)
6747                 {
6748                   complaint (&symfile_complaints,
6749                              _("Invalid .debug_ranges data "
6750                                "(no base address)"));
6751                   return;
6752                 }
6753
6754               if (start > end)
6755                 {
6756                   /* Inverted range entries are invalid.  */
6757                   complaint (&symfile_complaints,
6758                              _("Invalid .debug_ranges data "
6759                                "(inverted range)"));
6760                   return;
6761                 }
6762
6763               /* Empty range entries have no effect.  */
6764               if (start == end)
6765                 continue;
6766
6767               record_block_range (block,
6768                                   baseaddr + base + start,
6769                                   baseaddr + base + end - 1);
6770             }
6771         }
6772     }
6773 }
6774
6775 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6776    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6777    during 4.6.0 experimental.  */
6778
6779 static int
6780 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6781 {
6782   const char *cs;
6783   int major, minor, release;
6784
6785   if (cu->producer == NULL)
6786     {
6787       /* For unknown compilers expect their behavior is DWARF version
6788          compliant.
6789
6790          GCC started to support .debug_types sections by -gdwarf-4 since
6791          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6792          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6793          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6794          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6795
6796       return 0;
6797     }
6798
6799   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6800
6801   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6802     {
6803       /* For non-GCC compilers expect their behavior is DWARF version
6804          compliant.  */
6805
6806       return 0;
6807     }
6808   cs = &cu->producer[strlen ("GNU ")];
6809   while (*cs && !isdigit (*cs))
6810     cs++;
6811   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6812     {
6813       /* Not recognized as GCC.  */
6814
6815       return 0;
6816     }
6817
6818   return major < 4 || (major == 4 && minor < 6);
6819 }
6820
6821 /* Return the default accessibility type if it is not overriden by
6822    DW_AT_accessibility.  */
6823
6824 static enum dwarf_access_attribute
6825 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6826 {
6827   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6828     {
6829       /* The default DWARF 2 accessibility for members is public, the default
6830          accessibility for inheritance is private.  */
6831
6832       if (die->tag != DW_TAG_inheritance)
6833         return DW_ACCESS_public;
6834       else
6835         return DW_ACCESS_private;
6836     }
6837   else
6838     {
6839       /* DWARF 3+ defines the default accessibility a different way.  The same
6840          rules apply now for DW_TAG_inheritance as for the members and it only
6841          depends on the container kind.  */
6842
6843       if (die->parent->tag == DW_TAG_class_type)
6844         return DW_ACCESS_private;
6845       else
6846         return DW_ACCESS_public;
6847     }
6848 }
6849
6850 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6851    offset.  If the attribute was not found return 0, otherwise return
6852    1.  If it was found but could not properly be handled, set *OFFSET
6853    to 0.  */
6854
6855 static int
6856 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6857                              LONGEST *offset)
6858 {
6859   struct attribute *attr;
6860
6861   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6862   if (attr != NULL)
6863     {
6864       *offset = 0;
6865
6866       /* Note that we do not check for a section offset first here.
6867          This is because DW_AT_data_member_location is new in DWARF 4,
6868          so if we see it, we can assume that a constant form is really
6869          a constant and not a section offset.  */
6870       if (attr_form_is_constant (attr))
6871         *offset = dwarf2_get_attr_constant_value (attr, 0);
6872       else if (attr_form_is_section_offset (attr))
6873         dwarf2_complex_location_expr_complaint ();
6874       else if (attr_form_is_block (attr))
6875         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6876       else
6877         dwarf2_complex_location_expr_complaint ();
6878
6879       return 1;
6880     }
6881
6882   return 0;
6883 }
6884
6885 /* Add an aggregate field to the field list.  */
6886
6887 static void
6888 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6889                   struct dwarf2_cu *cu)
6890 {
6891   struct objfile *objfile = cu->objfile;
6892   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6893   struct nextfield *new_field;
6894   struct attribute *attr;
6895   struct field *fp;
6896   char *fieldname = "";
6897
6898   /* Allocate a new field list entry and link it in.  */
6899   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6900   make_cleanup (xfree, new_field);
6901   memset (new_field, 0, sizeof (struct nextfield));
6902
6903   if (die->tag == DW_TAG_inheritance)
6904     {
6905       new_field->next = fip->baseclasses;
6906       fip->baseclasses = new_field;
6907     }
6908   else
6909     {
6910       new_field->next = fip->fields;
6911       fip->fields = new_field;
6912     }
6913   fip->nfields++;
6914
6915   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6916   if (attr)
6917     new_field->accessibility = DW_UNSND (attr);
6918   else
6919     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6920   if (new_field->accessibility != DW_ACCESS_public)
6921     fip->non_public_fields = 1;
6922
6923   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6924   if (attr)
6925     new_field->virtuality = DW_UNSND (attr);
6926   else
6927     new_field->virtuality = DW_VIRTUALITY_none;
6928
6929   fp = &new_field->field;
6930
6931   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6932     {
6933       LONGEST offset;
6934
6935       /* Data member other than a C++ static data member.  */
6936
6937       /* Get type of field.  */
6938       fp->type = die_type (die, cu);
6939
6940       SET_FIELD_BITPOS (*fp, 0);
6941
6942       /* Get bit size of field (zero if none).  */
6943       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6944       if (attr)
6945         {
6946           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6947         }
6948       else
6949         {
6950           FIELD_BITSIZE (*fp) = 0;
6951         }
6952
6953       /* Get bit offset of field.  */
6954       if (handle_data_member_location (die, cu, &offset))
6955         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6956       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6957       if (attr)
6958         {
6959           if (gdbarch_bits_big_endian (gdbarch))
6960             {
6961               /* For big endian bits, the DW_AT_bit_offset gives the
6962                  additional bit offset from the MSB of the containing
6963                  anonymous object to the MSB of the field.  We don't
6964                  have to do anything special since we don't need to
6965                  know the size of the anonymous object.  */
6966               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6967             }
6968           else
6969             {
6970               /* For little endian bits, compute the bit offset to the
6971                  MSB of the anonymous object, subtract off the number of
6972                  bits from the MSB of the field to the MSB of the
6973                  object, and then subtract off the number of bits of
6974                  the field itself.  The result is the bit offset of
6975                  the LSB of the field.  */
6976               int anonymous_size;
6977               int bit_offset = DW_UNSND (attr);
6978
6979               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6980               if (attr)
6981                 {
6982                   /* The size of the anonymous object containing
6983                      the bit field is explicit, so use the
6984                      indicated size (in bytes).  */
6985                   anonymous_size = DW_UNSND (attr);
6986                 }
6987               else
6988                 {
6989                   /* The size of the anonymous object containing
6990                      the bit field must be inferred from the type
6991                      attribute of the data member containing the
6992                      bit field.  */
6993                   anonymous_size = TYPE_LENGTH (fp->type);
6994                 }
6995               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6996                 - bit_offset - FIELD_BITSIZE (*fp);
6997             }
6998         }
6999
7000       /* Get name of field.  */
7001       fieldname = dwarf2_name (die, cu);
7002       if (fieldname == NULL)
7003         fieldname = "";
7004
7005       /* The name is already allocated along with this objfile, so we don't
7006          need to duplicate it for the type.  */
7007       fp->name = fieldname;
7008
7009       /* Change accessibility for artificial fields (e.g. virtual table
7010          pointer or virtual base class pointer) to private.  */
7011       if (dwarf2_attr (die, DW_AT_artificial, cu))
7012         {
7013           FIELD_ARTIFICIAL (*fp) = 1;
7014           new_field->accessibility = DW_ACCESS_private;
7015           fip->non_public_fields = 1;
7016         }
7017     }
7018   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7019     {
7020       /* C++ static member.  */
7021
7022       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7023          is a declaration, but all versions of G++ as of this writing
7024          (so through at least 3.2.1) incorrectly generate
7025          DW_TAG_variable tags.  */
7026
7027       const char *physname;
7028
7029       /* Get name of field.  */
7030       fieldname = dwarf2_name (die, cu);
7031       if (fieldname == NULL)
7032         return;
7033
7034       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7035       if (attr
7036           /* Only create a symbol if this is an external value.
7037              new_symbol checks this and puts the value in the global symbol
7038              table, which we want.  If it is not external, new_symbol
7039              will try to put the value in cu->list_in_scope which is wrong.  */
7040           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7041         {
7042           /* A static const member, not much different than an enum as far as
7043              we're concerned, except that we can support more types.  */
7044           new_symbol (die, NULL, cu);
7045         }
7046
7047       /* Get physical name.  */
7048       physname = dwarf2_physname (fieldname, die, cu);
7049
7050       /* The name is already allocated along with this objfile, so we don't
7051          need to duplicate it for the type.  */
7052       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7053       FIELD_TYPE (*fp) = die_type (die, cu);
7054       FIELD_NAME (*fp) = fieldname;
7055     }
7056   else if (die->tag == DW_TAG_inheritance)
7057     {
7058       LONGEST offset;
7059
7060       /* C++ base class field.  */
7061       if (handle_data_member_location (die, cu, &offset))
7062         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7063       FIELD_BITSIZE (*fp) = 0;
7064       FIELD_TYPE (*fp) = die_type (die, cu);
7065       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7066       fip->nbaseclasses++;
7067     }
7068 }
7069
7070 /* Add a typedef defined in the scope of the FIP's class.  */
7071
7072 static void
7073 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7074                     struct dwarf2_cu *cu)
7075 {
7076   struct objfile *objfile = cu->objfile;
7077   struct typedef_field_list *new_field;
7078   struct attribute *attr;
7079   struct typedef_field *fp;
7080   char *fieldname = "";
7081
7082   /* Allocate a new field list entry and link it in.  */
7083   new_field = xzalloc (sizeof (*new_field));
7084   make_cleanup (xfree, new_field);
7085
7086   gdb_assert (die->tag == DW_TAG_typedef);
7087
7088   fp = &new_field->field;
7089
7090   /* Get name of field.  */
7091   fp->name = dwarf2_name (die, cu);
7092   if (fp->name == NULL)
7093     return;
7094
7095   fp->type = read_type_die (die, cu);
7096
7097   new_field->next = fip->typedef_field_list;
7098   fip->typedef_field_list = new_field;
7099   fip->typedef_field_list_count++;
7100 }
7101
7102 /* Create the vector of fields, and attach it to the type.  */
7103
7104 static void
7105 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7106                               struct dwarf2_cu *cu)
7107 {
7108   int nfields = fip->nfields;
7109
7110   /* Record the field count, allocate space for the array of fields,
7111      and create blank accessibility bitfields if necessary.  */
7112   TYPE_NFIELDS (type) = nfields;
7113   TYPE_FIELDS (type) = (struct field *)
7114     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7115   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7116
7117   if (fip->non_public_fields && cu->language != language_ada)
7118     {
7119       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7120
7121       TYPE_FIELD_PRIVATE_BITS (type) =
7122         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7123       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7124
7125       TYPE_FIELD_PROTECTED_BITS (type) =
7126         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7127       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7128
7129       TYPE_FIELD_IGNORE_BITS (type) =
7130         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7131       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7132     }
7133
7134   /* If the type has baseclasses, allocate and clear a bit vector for
7135      TYPE_FIELD_VIRTUAL_BITS.  */
7136   if (fip->nbaseclasses && cu->language != language_ada)
7137     {
7138       int num_bytes = B_BYTES (fip->nbaseclasses);
7139       unsigned char *pointer;
7140
7141       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7142       pointer = TYPE_ALLOC (type, num_bytes);
7143       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7144       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7145       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7146     }
7147
7148   /* Copy the saved-up fields into the field vector.  Start from the head of
7149      the list, adding to the tail of the field array, so that they end up in
7150      the same order in the array in which they were added to the list.  */
7151   while (nfields-- > 0)
7152     {
7153       struct nextfield *fieldp;
7154
7155       if (fip->fields)
7156         {
7157           fieldp = fip->fields;
7158           fip->fields = fieldp->next;
7159         }
7160       else
7161         {
7162           fieldp = fip->baseclasses;
7163           fip->baseclasses = fieldp->next;
7164         }
7165
7166       TYPE_FIELD (type, nfields) = fieldp->field;
7167       switch (fieldp->accessibility)
7168         {
7169         case DW_ACCESS_private:
7170           if (cu->language != language_ada)
7171             SET_TYPE_FIELD_PRIVATE (type, nfields);
7172           break;
7173
7174         case DW_ACCESS_protected:
7175           if (cu->language != language_ada)
7176             SET_TYPE_FIELD_PROTECTED (type, nfields);
7177           break;
7178
7179         case DW_ACCESS_public:
7180           break;
7181
7182         default:
7183           /* Unknown accessibility.  Complain and treat it as public.  */
7184           {
7185             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7186                        fieldp->accessibility);
7187           }
7188           break;
7189         }
7190       if (nfields < fip->nbaseclasses)
7191         {
7192           switch (fieldp->virtuality)
7193             {
7194             case DW_VIRTUALITY_virtual:
7195             case DW_VIRTUALITY_pure_virtual:
7196               if (cu->language == language_ada)
7197                 error (_("unexpected virtuality in component of Ada type"));
7198               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7199               break;
7200             }
7201         }
7202     }
7203 }
7204
7205 /* Add a member function to the proper fieldlist.  */
7206
7207 static void
7208 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7209                       struct type *type, struct dwarf2_cu *cu)
7210 {
7211   struct objfile *objfile = cu->objfile;
7212   struct attribute *attr;
7213   struct fnfieldlist *flp;
7214   int i;
7215   struct fn_field *fnp;
7216   char *fieldname;
7217   struct nextfnfield *new_fnfield;
7218   struct type *this_type;
7219   enum dwarf_access_attribute accessibility;
7220
7221   if (cu->language == language_ada)
7222     error (_("unexpected member function in Ada type"));
7223
7224   /* Get name of member function.  */
7225   fieldname = dwarf2_name (die, cu);
7226   if (fieldname == NULL)
7227     return;
7228
7229   /* Look up member function name in fieldlist.  */
7230   for (i = 0; i < fip->nfnfields; i++)
7231     {
7232       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7233         break;
7234     }
7235
7236   /* Create new list element if necessary.  */
7237   if (i < fip->nfnfields)
7238     flp = &fip->fnfieldlists[i];
7239   else
7240     {
7241       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7242         {
7243           fip->fnfieldlists = (struct fnfieldlist *)
7244             xrealloc (fip->fnfieldlists,
7245                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7246                       * sizeof (struct fnfieldlist));
7247           if (fip->nfnfields == 0)
7248             make_cleanup (free_current_contents, &fip->fnfieldlists);
7249         }
7250       flp = &fip->fnfieldlists[fip->nfnfields];
7251       flp->name = fieldname;
7252       flp->length = 0;
7253       flp->head = NULL;
7254       i = fip->nfnfields++;
7255     }
7256
7257   /* Create a new member function field and chain it to the field list
7258      entry.  */
7259   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7260   make_cleanup (xfree, new_fnfield);
7261   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7262   new_fnfield->next = flp->head;
7263   flp->head = new_fnfield;
7264   flp->length++;
7265
7266   /* Fill in the member function field info.  */
7267   fnp = &new_fnfield->fnfield;
7268
7269   /* Delay processing of the physname until later.  */
7270   if (cu->language == language_cplus || cu->language == language_java)
7271     {
7272       add_to_method_list (type, i, flp->length - 1, fieldname,
7273                           die, cu);
7274     }
7275   else
7276     {
7277       const char *physname = dwarf2_physname (fieldname, die, cu);
7278       fnp->physname = physname ? physname : "";
7279     }
7280
7281   fnp->type = alloc_type (objfile);
7282   this_type = read_type_die (die, cu);
7283   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7284     {
7285       int nparams = TYPE_NFIELDS (this_type);
7286
7287       /* TYPE is the domain of this method, and THIS_TYPE is the type
7288            of the method itself (TYPE_CODE_METHOD).  */
7289       smash_to_method_type (fnp->type, type,
7290                             TYPE_TARGET_TYPE (this_type),
7291                             TYPE_FIELDS (this_type),
7292                             TYPE_NFIELDS (this_type),
7293                             TYPE_VARARGS (this_type));
7294
7295       /* Handle static member functions.
7296          Dwarf2 has no clean way to discern C++ static and non-static
7297          member functions.  G++ helps GDB by marking the first
7298          parameter for non-static member functions (which is the this
7299          pointer) as artificial.  We obtain this information from
7300          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7301       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7302         fnp->voffset = VOFFSET_STATIC;
7303     }
7304   else
7305     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7306                dwarf2_full_name (fieldname, die, cu));
7307
7308   /* Get fcontext from DW_AT_containing_type if present.  */
7309   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7310     fnp->fcontext = die_containing_type (die, cu);
7311
7312   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7313      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7314
7315   /* Get accessibility.  */
7316   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7317   if (attr)
7318     accessibility = DW_UNSND (attr);
7319   else
7320     accessibility = dwarf2_default_access_attribute (die, cu);
7321   switch (accessibility)
7322     {
7323     case DW_ACCESS_private:
7324       fnp->is_private = 1;
7325       break;
7326     case DW_ACCESS_protected:
7327       fnp->is_protected = 1;
7328       break;
7329     }
7330
7331   /* Check for artificial methods.  */
7332   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7333   if (attr && DW_UNSND (attr) != 0)
7334     fnp->is_artificial = 1;
7335
7336   /* Get index in virtual function table if it is a virtual member
7337      function.  For older versions of GCC, this is an offset in the
7338      appropriate virtual table, as specified by DW_AT_containing_type.
7339      For everyone else, it is an expression to be evaluated relative
7340      to the object address.  */
7341
7342   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7343   if (attr)
7344     {
7345       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7346         {
7347           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7348             {
7349               /* Old-style GCC.  */
7350               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7351             }
7352           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7353                    || (DW_BLOCK (attr)->size > 1
7354                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7355                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7356             {
7357               struct dwarf_block blk;
7358               int offset;
7359
7360               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7361                         ? 1 : 2);
7362               blk.size = DW_BLOCK (attr)->size - offset;
7363               blk.data = DW_BLOCK (attr)->data + offset;
7364               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7365               if ((fnp->voffset % cu->header.addr_size) != 0)
7366                 dwarf2_complex_location_expr_complaint ();
7367               else
7368                 fnp->voffset /= cu->header.addr_size;
7369               fnp->voffset += 2;
7370             }
7371           else
7372             dwarf2_complex_location_expr_complaint ();
7373
7374           if (!fnp->fcontext)
7375             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7376         }
7377       else if (attr_form_is_section_offset (attr))
7378         {
7379           dwarf2_complex_location_expr_complaint ();
7380         }
7381       else
7382         {
7383           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7384                                                  fieldname);
7385         }
7386     }
7387   else
7388     {
7389       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7390       if (attr && DW_UNSND (attr))
7391         {
7392           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7393           complaint (&symfile_complaints,
7394                      _("Member function \"%s\" (offset %d) is virtual "
7395                        "but the vtable offset is not specified"),
7396                      fieldname, die->offset);
7397           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7398           TYPE_CPLUS_DYNAMIC (type) = 1;
7399         }
7400     }
7401 }
7402
7403 /* Create the vector of member function fields, and attach it to the type.  */
7404
7405 static void
7406 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7407                                  struct dwarf2_cu *cu)
7408 {
7409   struct fnfieldlist *flp;
7410   int i;
7411
7412   if (cu->language == language_ada)
7413     error (_("unexpected member functions in Ada type"));
7414
7415   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7416   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7417     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7418
7419   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7420     {
7421       struct nextfnfield *nfp = flp->head;
7422       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7423       int k;
7424
7425       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7426       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7427       fn_flp->fn_fields = (struct fn_field *)
7428         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7429       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7430         fn_flp->fn_fields[k] = nfp->fnfield;
7431     }
7432
7433   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7434 }
7435
7436 /* Returns non-zero if NAME is the name of a vtable member in CU's
7437    language, zero otherwise.  */
7438 static int
7439 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7440 {
7441   static const char vptr[] = "_vptr";
7442   static const char vtable[] = "vtable";
7443
7444   /* Look for the C++ and Java forms of the vtable.  */
7445   if ((cu->language == language_java
7446        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7447        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7448        && is_cplus_marker (name[sizeof (vptr) - 1])))
7449     return 1;
7450
7451   return 0;
7452 }
7453
7454 /* GCC outputs unnamed structures that are really pointers to member
7455    functions, with the ABI-specified layout.  If TYPE describes
7456    such a structure, smash it into a member function type.
7457
7458    GCC shouldn't do this; it should just output pointer to member DIEs.
7459    This is GCC PR debug/28767.  */
7460
7461 static void
7462 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7463 {
7464   struct type *pfn_type, *domain_type, *new_type;
7465
7466   /* Check for a structure with no name and two children.  */
7467   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7468     return;
7469
7470   /* Check for __pfn and __delta members.  */
7471   if (TYPE_FIELD_NAME (type, 0) == NULL
7472       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7473       || TYPE_FIELD_NAME (type, 1) == NULL
7474       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7475     return;
7476
7477   /* Find the type of the method.  */
7478   pfn_type = TYPE_FIELD_TYPE (type, 0);
7479   if (pfn_type == NULL
7480       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7481       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7482     return;
7483
7484   /* Look for the "this" argument.  */
7485   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7486   if (TYPE_NFIELDS (pfn_type) == 0
7487       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7488       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7489     return;
7490
7491   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7492   new_type = alloc_type (objfile);
7493   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7494                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7495                         TYPE_VARARGS (pfn_type));
7496   smash_to_methodptr_type (type, new_type);
7497 }
7498
7499 /* Called when we find the DIE that starts a structure or union scope
7500    (definition) to create a type for the structure or union.  Fill in
7501    the type's name and general properties; the members will not be
7502    processed until process_structure_type.
7503
7504    NOTE: we need to call these functions regardless of whether or not the
7505    DIE has a DW_AT_name attribute, since it might be an anonymous
7506    structure or union.  This gets the type entered into our set of
7507    user defined types.
7508
7509    However, if the structure is incomplete (an opaque struct/union)
7510    then suppress creating a symbol table entry for it since gdb only
7511    wants to find the one with the complete definition.  Note that if
7512    it is complete, we just call new_symbol, which does it's own
7513    checking about whether the struct/union is anonymous or not (and
7514    suppresses creating a symbol table entry itself).  */
7515
7516 static struct type *
7517 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7518 {
7519   struct objfile *objfile = cu->objfile;
7520   struct type *type;
7521   struct attribute *attr;
7522   char *name;
7523
7524   /* If the definition of this type lives in .debug_types, read that type.
7525      Don't follow DW_AT_specification though, that will take us back up
7526      the chain and we want to go down.  */
7527   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7528   if (attr)
7529     {
7530       struct dwarf2_cu *type_cu = cu;
7531       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7532
7533       /* We could just recurse on read_structure_type, but we need to call
7534          get_die_type to ensure only one type for this DIE is created.
7535          This is important, for example, because for c++ classes we need
7536          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7537       type = read_type_die (type_die, type_cu);
7538
7539       /* TYPE_CU may not be the same as CU.
7540          Ensure TYPE is recorded in CU's type_hash table.  */
7541       return set_die_type (die, type, cu);
7542     }
7543
7544   type = alloc_type (objfile);
7545   INIT_CPLUS_SPECIFIC (type);
7546
7547   name = dwarf2_name (die, cu);
7548   if (name != NULL)
7549     {
7550       if (cu->language == language_cplus
7551           || cu->language == language_java)
7552         {
7553           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7554
7555           /* dwarf2_full_name might have already finished building the DIE's
7556              type.  If so, there is no need to continue.  */
7557           if (get_die_type (die, cu) != NULL)
7558             return get_die_type (die, cu);
7559
7560           TYPE_TAG_NAME (type) = full_name;
7561           if (die->tag == DW_TAG_structure_type
7562               || die->tag == DW_TAG_class_type)
7563             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7564         }
7565       else
7566         {
7567           /* The name is already allocated along with this objfile, so
7568              we don't need to duplicate it for the type.  */
7569           TYPE_TAG_NAME (type) = (char *) name;
7570           if (die->tag == DW_TAG_class_type)
7571             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7572         }
7573     }
7574
7575   if (die->tag == DW_TAG_structure_type)
7576     {
7577       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7578     }
7579   else if (die->tag == DW_TAG_union_type)
7580     {
7581       TYPE_CODE (type) = TYPE_CODE_UNION;
7582     }
7583   else
7584     {
7585       TYPE_CODE (type) = TYPE_CODE_CLASS;
7586     }
7587
7588   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7589     TYPE_DECLARED_CLASS (type) = 1;
7590
7591   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7592   if (attr)
7593     {
7594       TYPE_LENGTH (type) = DW_UNSND (attr);
7595     }
7596   else
7597     {
7598       TYPE_LENGTH (type) = 0;
7599     }
7600
7601   TYPE_STUB_SUPPORTED (type) = 1;
7602   if (die_is_declaration (die, cu))
7603     TYPE_STUB (type) = 1;
7604   else if (attr == NULL && die->child == NULL
7605            && producer_is_realview (cu->producer))
7606     /* RealView does not output the required DW_AT_declaration
7607        on incomplete types.  */
7608     TYPE_STUB (type) = 1;
7609
7610   /* We need to add the type field to the die immediately so we don't
7611      infinitely recurse when dealing with pointers to the structure
7612      type within the structure itself.  */
7613   set_die_type (die, type, cu);
7614
7615   /* set_die_type should be already done.  */
7616   set_descriptive_type (type, die, cu);
7617
7618   return type;
7619 }
7620
7621 /* Finish creating a structure or union type, including filling in
7622    its members and creating a symbol for it.  */
7623
7624 static void
7625 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7626 {
7627   struct objfile *objfile = cu->objfile;
7628   struct die_info *child_die = die->child;
7629   struct type *type;
7630
7631   type = get_die_type (die, cu);
7632   if (type == NULL)
7633     type = read_structure_type (die, cu);
7634
7635   if (die->child != NULL && ! die_is_declaration (die, cu))
7636     {
7637       struct field_info fi;
7638       struct die_info *child_die;
7639       VEC (symbolp) *template_args = NULL;
7640       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7641
7642       memset (&fi, 0, sizeof (struct field_info));
7643
7644       child_die = die->child;
7645
7646       while (child_die && child_die->tag)
7647         {
7648           if (child_die->tag == DW_TAG_member
7649               || child_die->tag == DW_TAG_variable)
7650             {
7651               /* NOTE: carlton/2002-11-05: A C++ static data member
7652                  should be a DW_TAG_member that is a declaration, but
7653                  all versions of G++ as of this writing (so through at
7654                  least 3.2.1) incorrectly generate DW_TAG_variable
7655                  tags for them instead.  */
7656               dwarf2_add_field (&fi, child_die, cu);
7657             }
7658           else if (child_die->tag == DW_TAG_subprogram)
7659             {
7660               /* C++ member function.  */
7661               dwarf2_add_member_fn (&fi, child_die, type, cu);
7662             }
7663           else if (child_die->tag == DW_TAG_inheritance)
7664             {
7665               /* C++ base class field.  */
7666               dwarf2_add_field (&fi, child_die, cu);
7667             }
7668           else if (child_die->tag == DW_TAG_typedef)
7669             dwarf2_add_typedef (&fi, child_die, cu);
7670           else if (child_die->tag == DW_TAG_template_type_param
7671                    || child_die->tag == DW_TAG_template_value_param)
7672             {
7673               struct symbol *arg = new_symbol (child_die, NULL, cu);
7674
7675               if (arg != NULL)
7676                 VEC_safe_push (symbolp, template_args, arg);
7677             }
7678
7679           child_die = sibling_die (child_die);
7680         }
7681
7682       /* Attach template arguments to type.  */
7683       if (! VEC_empty (symbolp, template_args))
7684         {
7685           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7686           TYPE_N_TEMPLATE_ARGUMENTS (type)
7687             = VEC_length (symbolp, template_args);
7688           TYPE_TEMPLATE_ARGUMENTS (type)
7689             = obstack_alloc (&objfile->objfile_obstack,
7690                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7691                               * sizeof (struct symbol *)));
7692           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7693                   VEC_address (symbolp, template_args),
7694                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7695                    * sizeof (struct symbol *)));
7696           VEC_free (symbolp, template_args);
7697         }
7698
7699       /* Attach fields and member functions to the type.  */
7700       if (fi.nfields)
7701         dwarf2_attach_fields_to_type (&fi, type, cu);
7702       if (fi.nfnfields)
7703         {
7704           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7705
7706           /* Get the type which refers to the base class (possibly this
7707              class itself) which contains the vtable pointer for the current
7708              class from the DW_AT_containing_type attribute.  This use of
7709              DW_AT_containing_type is a GNU extension.  */
7710
7711           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7712             {
7713               struct type *t = die_containing_type (die, cu);
7714
7715               TYPE_VPTR_BASETYPE (type) = t;
7716               if (type == t)
7717                 {
7718                   int i;
7719
7720                   /* Our own class provides vtbl ptr.  */
7721                   for (i = TYPE_NFIELDS (t) - 1;
7722                        i >= TYPE_N_BASECLASSES (t);
7723                        --i)
7724                     {
7725                       const char *fieldname = TYPE_FIELD_NAME (t, i);
7726
7727                       if (is_vtable_name (fieldname, cu))
7728                         {
7729                           TYPE_VPTR_FIELDNO (type) = i;
7730                           break;
7731                         }
7732                     }
7733
7734                   /* Complain if virtual function table field not found.  */
7735                   if (i < TYPE_N_BASECLASSES (t))
7736                     complaint (&symfile_complaints,
7737                                _("virtual function table pointer "
7738                                  "not found when defining class '%s'"),
7739                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7740                                "");
7741                 }
7742               else
7743                 {
7744                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7745                 }
7746             }
7747           else if (cu->producer
7748                    && strncmp (cu->producer,
7749                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7750             {
7751               /* The IBM XLC compiler does not provide direct indication
7752                  of the containing type, but the vtable pointer is
7753                  always named __vfp.  */
7754
7755               int i;
7756
7757               for (i = TYPE_NFIELDS (type) - 1;
7758                    i >= TYPE_N_BASECLASSES (type);
7759                    --i)
7760                 {
7761                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7762                     {
7763                       TYPE_VPTR_FIELDNO (type) = i;
7764                       TYPE_VPTR_BASETYPE (type) = type;
7765                       break;
7766                     }
7767                 }
7768             }
7769         }
7770
7771       /* Copy fi.typedef_field_list linked list elements content into the
7772          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7773       if (fi.typedef_field_list)
7774         {
7775           int i = fi.typedef_field_list_count;
7776
7777           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7778           TYPE_TYPEDEF_FIELD_ARRAY (type)
7779             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7780           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7781
7782           /* Reverse the list order to keep the debug info elements order.  */
7783           while (--i >= 0)
7784             {
7785               struct typedef_field *dest, *src;
7786
7787               dest = &TYPE_TYPEDEF_FIELD (type, i);
7788               src = &fi.typedef_field_list->field;
7789               fi.typedef_field_list = fi.typedef_field_list->next;
7790               *dest = *src;
7791             }
7792         }
7793
7794       do_cleanups (back_to);
7795
7796       if (HAVE_CPLUS_STRUCT (type))
7797         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7798     }
7799
7800   quirk_gcc_member_function_pointer (type, objfile);
7801
7802   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7803      snapshots) has been known to create a die giving a declaration
7804      for a class that has, as a child, a die giving a definition for a
7805      nested class.  So we have to process our children even if the
7806      current die is a declaration.  Normally, of course, a declaration
7807      won't have any children at all.  */
7808
7809   while (child_die != NULL && child_die->tag)
7810     {
7811       if (child_die->tag == DW_TAG_member
7812           || child_die->tag == DW_TAG_variable
7813           || child_die->tag == DW_TAG_inheritance
7814           || child_die->tag == DW_TAG_template_value_param
7815           || child_die->tag == DW_TAG_template_type_param)
7816         {
7817           /* Do nothing.  */
7818         }
7819       else
7820         process_die (child_die, cu);
7821
7822       child_die = sibling_die (child_die);
7823     }
7824
7825   /* Do not consider external references.  According to the DWARF standard,
7826      these DIEs are identified by the fact that they have no byte_size
7827      attribute, and a declaration attribute.  */
7828   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7829       || !die_is_declaration (die, cu))
7830     new_symbol (die, type, cu);
7831 }
7832
7833 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7834    complete the type's fields yet, or create any symbols.  */
7835
7836 static struct type *
7837 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7838 {
7839   struct objfile *objfile = cu->objfile;
7840   struct type *type;
7841   struct attribute *attr;
7842   const char *name;
7843
7844   /* If the definition of this type lives in .debug_types, read that type.
7845      Don't follow DW_AT_specification though, that will take us back up
7846      the chain and we want to go down.  */
7847   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7848   if (attr)
7849     {
7850       struct dwarf2_cu *type_cu = cu;
7851       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7852
7853       type = read_type_die (type_die, type_cu);
7854
7855       /* TYPE_CU may not be the same as CU.
7856          Ensure TYPE is recorded in CU's type_hash table.  */
7857       return set_die_type (die, type, cu);
7858     }
7859
7860   type = alloc_type (objfile);
7861
7862   TYPE_CODE (type) = TYPE_CODE_ENUM;
7863   name = dwarf2_full_name (NULL, die, cu);
7864   if (name != NULL)
7865     TYPE_TAG_NAME (type) = (char *) name;
7866
7867   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7868   if (attr)
7869     {
7870       TYPE_LENGTH (type) = DW_UNSND (attr);
7871     }
7872   else
7873     {
7874       TYPE_LENGTH (type) = 0;
7875     }
7876
7877   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7878      declared as private in the package spec, and then defined only
7879      inside the package body.  Such types are known as Taft Amendment
7880      Types.  When another package uses such a type, an incomplete DIE
7881      may be generated by the compiler.  */
7882   if (die_is_declaration (die, cu))
7883     TYPE_STUB (type) = 1;
7884
7885   return set_die_type (die, type, cu);
7886 }
7887
7888 /* Given a pointer to a die which begins an enumeration, process all
7889    the dies that define the members of the enumeration, and create the
7890    symbol for the enumeration type.
7891
7892    NOTE: We reverse the order of the element list.  */
7893
7894 static void
7895 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7896 {
7897   struct type *this_type;
7898
7899   this_type = get_die_type (die, cu);
7900   if (this_type == NULL)
7901     this_type = read_enumeration_type (die, cu);
7902
7903   if (die->child != NULL)
7904     {
7905       struct die_info *child_die;
7906       struct symbol *sym;
7907       struct field *fields = NULL;
7908       int num_fields = 0;
7909       int unsigned_enum = 1;
7910       char *name;
7911       int flag_enum = 1;
7912       ULONGEST mask = 0;
7913
7914       child_die = die->child;
7915       while (child_die && child_die->tag)
7916         {
7917           if (child_die->tag != DW_TAG_enumerator)
7918             {
7919               process_die (child_die, cu);
7920             }
7921           else
7922             {
7923               name = dwarf2_name (child_die, cu);
7924               if (name)
7925                 {
7926                   sym = new_symbol (child_die, this_type, cu);
7927                   if (SYMBOL_VALUE (sym) < 0)
7928                     {
7929                       unsigned_enum = 0;
7930                       flag_enum = 0;
7931                     }
7932                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
7933                     flag_enum = 0;
7934                   else
7935                     mask |= SYMBOL_VALUE (sym);
7936
7937                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7938                     {
7939                       fields = (struct field *)
7940                         xrealloc (fields,
7941                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7942                                   * sizeof (struct field));
7943                     }
7944
7945                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7946                   FIELD_TYPE (fields[num_fields]) = NULL;
7947                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7948                   FIELD_BITSIZE (fields[num_fields]) = 0;
7949
7950                   num_fields++;
7951                 }
7952             }
7953
7954           child_die = sibling_die (child_die);
7955         }
7956
7957       if (num_fields)
7958         {
7959           TYPE_NFIELDS (this_type) = num_fields;
7960           TYPE_FIELDS (this_type) = (struct field *)
7961             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7962           memcpy (TYPE_FIELDS (this_type), fields,
7963                   sizeof (struct field) * num_fields);
7964           xfree (fields);
7965         }
7966       if (unsigned_enum)
7967         TYPE_UNSIGNED (this_type) = 1;
7968       if (flag_enum)
7969         TYPE_FLAG_ENUM (this_type) = 1;
7970     }
7971
7972   /* If we are reading an enum from a .debug_types unit, and the enum
7973      is a declaration, and the enum is not the signatured type in the
7974      unit, then we do not want to add a symbol for it.  Adding a
7975      symbol would in some cases obscure the true definition of the
7976      enum, giving users an incomplete type when the definition is
7977      actually available.  Note that we do not want to do this for all
7978      enums which are just declarations, because C++0x allows forward
7979      enum declarations.  */
7980   if (cu->per_cu->debug_types_section
7981       && die_is_declaration (die, cu))
7982     {
7983       struct signatured_type *type_sig;
7984
7985       type_sig
7986         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7987                                             cu->per_cu->debug_types_section,
7988                                             cu->per_cu->offset);
7989       if (type_sig->type_offset != die->offset)
7990         return;
7991     }
7992
7993   new_symbol (die, this_type, cu);
7994 }
7995
7996 /* Extract all information from a DW_TAG_array_type DIE and put it in
7997    the DIE's type field.  For now, this only handles one dimensional
7998    arrays.  */
7999
8000 static struct type *
8001 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8002 {
8003   struct objfile *objfile = cu->objfile;
8004   struct die_info *child_die;
8005   struct type *type;
8006   struct type *element_type, *range_type, *index_type;
8007   struct type **range_types = NULL;
8008   struct attribute *attr;
8009   int ndim = 0;
8010   struct cleanup *back_to;
8011   char *name;
8012
8013   element_type = die_type (die, cu);
8014
8015   /* The die_type call above may have already set the type for this DIE.  */
8016   type = get_die_type (die, cu);
8017   if (type)
8018     return type;
8019
8020   /* Irix 6.2 native cc creates array types without children for
8021      arrays with unspecified length.  */
8022   if (die->child == NULL)
8023     {
8024       index_type = objfile_type (objfile)->builtin_int;
8025       range_type = create_range_type (NULL, index_type, 0, -1);
8026       type = create_array_type (NULL, element_type, range_type);
8027       return set_die_type (die, type, cu);
8028     }
8029
8030   back_to = make_cleanup (null_cleanup, NULL);
8031   child_die = die->child;
8032   while (child_die && child_die->tag)
8033     {
8034       if (child_die->tag == DW_TAG_subrange_type)
8035         {
8036           struct type *child_type = read_type_die (child_die, cu);
8037
8038           if (child_type != NULL)
8039             {
8040               /* The range type was succesfully read.  Save it for the
8041                  array type creation.  */
8042               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8043                 {
8044                   range_types = (struct type **)
8045                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8046                               * sizeof (struct type *));
8047                   if (ndim == 0)
8048                     make_cleanup (free_current_contents, &range_types);
8049                 }
8050               range_types[ndim++] = child_type;
8051             }
8052         }
8053       child_die = sibling_die (child_die);
8054     }
8055
8056   /* Dwarf2 dimensions are output from left to right, create the
8057      necessary array types in backwards order.  */
8058
8059   type = element_type;
8060
8061   if (read_array_order (die, cu) == DW_ORD_col_major)
8062     {
8063       int i = 0;
8064
8065       while (i < ndim)
8066         type = create_array_type (NULL, type, range_types[i++]);
8067     }
8068   else
8069     {
8070       while (ndim-- > 0)
8071         type = create_array_type (NULL, type, range_types[ndim]);
8072     }
8073
8074   /* Understand Dwarf2 support for vector types (like they occur on
8075      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8076      array type.  This is not part of the Dwarf2/3 standard yet, but a
8077      custom vendor extension.  The main difference between a regular
8078      array and the vector variant is that vectors are passed by value
8079      to functions.  */
8080   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8081   if (attr)
8082     make_vector_type (type);
8083
8084   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8085      implementation may choose to implement triple vectors using this
8086      attribute.  */
8087   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8088   if (attr)
8089     {
8090       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8091         TYPE_LENGTH (type) = DW_UNSND (attr);
8092       else
8093         complaint (&symfile_complaints,
8094                    _("DW_AT_byte_size for array type smaller "
8095                      "than the total size of elements"));
8096     }
8097
8098   name = dwarf2_name (die, cu);
8099   if (name)
8100     TYPE_NAME (type) = name;
8101
8102   /* Install the type in the die.  */
8103   set_die_type (die, type, cu);
8104
8105   /* set_die_type should be already done.  */
8106   set_descriptive_type (type, die, cu);
8107
8108   do_cleanups (back_to);
8109
8110   return type;
8111 }
8112
8113 static enum dwarf_array_dim_ordering
8114 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8115 {
8116   struct attribute *attr;
8117
8118   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8119
8120   if (attr) return DW_SND (attr);
8121
8122   /* GNU F77 is a special case, as at 08/2004 array type info is the
8123      opposite order to the dwarf2 specification, but data is still
8124      laid out as per normal fortran.
8125
8126      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8127      version checking.  */
8128
8129   if (cu->language == language_fortran
8130       && cu->producer && strstr (cu->producer, "GNU F77"))
8131     {
8132       return DW_ORD_row_major;
8133     }
8134
8135   switch (cu->language_defn->la_array_ordering)
8136     {
8137     case array_column_major:
8138       return DW_ORD_col_major;
8139     case array_row_major:
8140     default:
8141       return DW_ORD_row_major;
8142     };
8143 }
8144
8145 /* Extract all information from a DW_TAG_set_type DIE and put it in
8146    the DIE's type field.  */
8147
8148 static struct type *
8149 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8150 {
8151   struct type *domain_type, *set_type;
8152   struct attribute *attr;
8153
8154   domain_type = die_type (die, cu);
8155
8156   /* The die_type call above may have already set the type for this DIE.  */
8157   set_type = get_die_type (die, cu);
8158   if (set_type)
8159     return set_type;
8160
8161   set_type = create_set_type (NULL, domain_type);
8162
8163   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8164   if (attr)
8165     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8166
8167   return set_die_type (die, set_type, cu);
8168 }
8169
8170 /* First cut: install each common block member as a global variable.  */
8171
8172 static void
8173 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8174 {
8175   struct die_info *child_die;
8176   struct attribute *attr;
8177   struct symbol *sym;
8178   CORE_ADDR base = (CORE_ADDR) 0;
8179
8180   attr = dwarf2_attr (die, DW_AT_location, cu);
8181   if (attr)
8182     {
8183       /* Support the .debug_loc offsets.  */
8184       if (attr_form_is_block (attr))
8185         {
8186           base = decode_locdesc (DW_BLOCK (attr), cu);
8187         }
8188       else if (attr_form_is_section_offset (attr))
8189         {
8190           dwarf2_complex_location_expr_complaint ();
8191         }
8192       else
8193         {
8194           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8195                                                  "common block member");
8196         }
8197     }
8198   if (die->child != NULL)
8199     {
8200       child_die = die->child;
8201       while (child_die && child_die->tag)
8202         {
8203           LONGEST offset;
8204
8205           sym = new_symbol (child_die, NULL, cu);
8206           if (sym != NULL
8207               && handle_data_member_location (child_die, cu, &offset))
8208             {
8209               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8210               add_symbol_to_list (sym, &global_symbols);
8211             }
8212           child_die = sibling_die (child_die);
8213         }
8214     }
8215 }
8216
8217 /* Create a type for a C++ namespace.  */
8218
8219 static struct type *
8220 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8221 {
8222   struct objfile *objfile = cu->objfile;
8223   const char *previous_prefix, *name;
8224   int is_anonymous;
8225   struct type *type;
8226
8227   /* For extensions, reuse the type of the original namespace.  */
8228   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8229     {
8230       struct die_info *ext_die;
8231       struct dwarf2_cu *ext_cu = cu;
8232
8233       ext_die = dwarf2_extension (die, &ext_cu);
8234       type = read_type_die (ext_die, ext_cu);
8235
8236       /* EXT_CU may not be the same as CU.
8237          Ensure TYPE is recorded in CU's type_hash table.  */
8238       return set_die_type (die, type, cu);
8239     }
8240
8241   name = namespace_name (die, &is_anonymous, cu);
8242
8243   /* Now build the name of the current namespace.  */
8244
8245   previous_prefix = determine_prefix (die, cu);
8246   if (previous_prefix[0] != '\0')
8247     name = typename_concat (&objfile->objfile_obstack,
8248                             previous_prefix, name, 0, cu);
8249
8250   /* Create the type.  */
8251   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8252                     objfile);
8253   TYPE_NAME (type) = (char *) name;
8254   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8255
8256   return set_die_type (die, type, cu);
8257 }
8258
8259 /* Read a C++ namespace.  */
8260
8261 static void
8262 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8263 {
8264   struct objfile *objfile = cu->objfile;
8265   int is_anonymous;
8266
8267   /* Add a symbol associated to this if we haven't seen the namespace
8268      before.  Also, add a using directive if it's an anonymous
8269      namespace.  */
8270
8271   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8272     {
8273       struct type *type;
8274
8275       type = read_type_die (die, cu);
8276       new_symbol (die, type, cu);
8277
8278       namespace_name (die, &is_anonymous, cu);
8279       if (is_anonymous)
8280         {
8281           const char *previous_prefix = determine_prefix (die, cu);
8282
8283           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8284                                   NULL, NULL, &objfile->objfile_obstack);
8285         }
8286     }
8287
8288   if (die->child != NULL)
8289     {
8290       struct die_info *child_die = die->child;
8291
8292       while (child_die && child_die->tag)
8293         {
8294           process_die (child_die, cu);
8295           child_die = sibling_die (child_die);
8296         }
8297     }
8298 }
8299
8300 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8301    imported module.  Still we need that type as local Fortran "use ... only"
8302    declaration imports depend on the created type in determine_prefix.  */
8303
8304 static struct type *
8305 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8306 {
8307   struct objfile *objfile = cu->objfile;
8308   char *module_name;
8309   struct type *type;
8310
8311   module_name = dwarf2_name (die, cu);
8312   if (!module_name)
8313     complaint (&symfile_complaints,
8314                _("DW_TAG_module has no name, offset 0x%x"),
8315                die->offset);
8316   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8317
8318   /* determine_prefix uses TYPE_TAG_NAME.  */
8319   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8320
8321   return set_die_type (die, type, cu);
8322 }
8323
8324 /* Read a Fortran module.  */
8325
8326 static void
8327 read_module (struct die_info *die, struct dwarf2_cu *cu)
8328 {
8329   struct die_info *child_die = die->child;
8330
8331   while (child_die && child_die->tag)
8332     {
8333       process_die (child_die, cu);
8334       child_die = sibling_die (child_die);
8335     }
8336 }
8337
8338 /* Return the name of the namespace represented by DIE.  Set
8339    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8340    namespace.  */
8341
8342 static const char *
8343 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8344 {
8345   struct die_info *current_die;
8346   const char *name = NULL;
8347
8348   /* Loop through the extensions until we find a name.  */
8349
8350   for (current_die = die;
8351        current_die != NULL;
8352        current_die = dwarf2_extension (die, &cu))
8353     {
8354       name = dwarf2_name (current_die, cu);
8355       if (name != NULL)
8356         break;
8357     }
8358
8359   /* Is it an anonymous namespace?  */
8360
8361   *is_anonymous = (name == NULL);
8362   if (*is_anonymous)
8363     name = CP_ANONYMOUS_NAMESPACE_STR;
8364
8365   return name;
8366 }
8367
8368 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8369    the user defined type vector.  */
8370
8371 static struct type *
8372 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8373 {
8374   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8375   struct comp_unit_head *cu_header = &cu->header;
8376   struct type *type;
8377   struct attribute *attr_byte_size;
8378   struct attribute *attr_address_class;
8379   int byte_size, addr_class;
8380   struct type *target_type;
8381
8382   target_type = die_type (die, cu);
8383
8384   /* The die_type call above may have already set the type for this DIE.  */
8385   type = get_die_type (die, cu);
8386   if (type)
8387     return type;
8388
8389   type = lookup_pointer_type (target_type);
8390
8391   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8392   if (attr_byte_size)
8393     byte_size = DW_UNSND (attr_byte_size);
8394   else
8395     byte_size = cu_header->addr_size;
8396
8397   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8398   if (attr_address_class)
8399     addr_class = DW_UNSND (attr_address_class);
8400   else
8401     addr_class = DW_ADDR_none;
8402
8403   /* If the pointer size or address class is different than the
8404      default, create a type variant marked as such and set the
8405      length accordingly.  */
8406   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8407     {
8408       if (gdbarch_address_class_type_flags_p (gdbarch))
8409         {
8410           int type_flags;
8411
8412           type_flags = gdbarch_address_class_type_flags
8413                          (gdbarch, byte_size, addr_class);
8414           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8415                       == 0);
8416           type = make_type_with_address_space (type, type_flags);
8417         }
8418       else if (TYPE_LENGTH (type) != byte_size)
8419         {
8420           complaint (&symfile_complaints,
8421                      _("invalid pointer size %d"), byte_size);
8422         }
8423       else
8424         {
8425           /* Should we also complain about unhandled address classes?  */
8426         }
8427     }
8428
8429   TYPE_LENGTH (type) = byte_size;
8430   return set_die_type (die, type, cu);
8431 }
8432
8433 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8434    the user defined type vector.  */
8435
8436 static struct type *
8437 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8438 {
8439   struct type *type;
8440   struct type *to_type;
8441   struct type *domain;
8442
8443   to_type = die_type (die, cu);
8444   domain = die_containing_type (die, cu);
8445
8446   /* The calls above may have already set the type for this DIE.  */
8447   type = get_die_type (die, cu);
8448   if (type)
8449     return type;
8450
8451   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8452     type = lookup_methodptr_type (to_type);
8453   else
8454     type = lookup_memberptr_type (to_type, domain);
8455
8456   return set_die_type (die, type, cu);
8457 }
8458
8459 /* Extract all information from a DW_TAG_reference_type DIE and add to
8460    the user defined type vector.  */
8461
8462 static struct type *
8463 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8464 {
8465   struct comp_unit_head *cu_header = &cu->header;
8466   struct type *type, *target_type;
8467   struct attribute *attr;
8468
8469   target_type = die_type (die, cu);
8470
8471   /* The die_type call above may have already set the type for this DIE.  */
8472   type = get_die_type (die, cu);
8473   if (type)
8474     return type;
8475
8476   type = lookup_reference_type (target_type);
8477   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8478   if (attr)
8479     {
8480       TYPE_LENGTH (type) = DW_UNSND (attr);
8481     }
8482   else
8483     {
8484       TYPE_LENGTH (type) = cu_header->addr_size;
8485     }
8486   return set_die_type (die, type, cu);
8487 }
8488
8489 static struct type *
8490 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8491 {
8492   struct type *base_type, *cv_type;
8493
8494   base_type = die_type (die, cu);
8495
8496   /* The die_type call above may have already set the type for this DIE.  */
8497   cv_type = get_die_type (die, cu);
8498   if (cv_type)
8499     return cv_type;
8500
8501   /* In case the const qualifier is applied to an array type, the element type
8502      is so qualified, not the array type (section 6.7.3 of C99).  */
8503   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8504     {
8505       struct type *el_type, *inner_array;
8506
8507       base_type = copy_type (base_type);
8508       inner_array = base_type;
8509
8510       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8511         {
8512           TYPE_TARGET_TYPE (inner_array) =
8513             copy_type (TYPE_TARGET_TYPE (inner_array));
8514           inner_array = TYPE_TARGET_TYPE (inner_array);
8515         }
8516
8517       el_type = TYPE_TARGET_TYPE (inner_array);
8518       TYPE_TARGET_TYPE (inner_array) =
8519         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8520
8521       return set_die_type (die, base_type, cu);
8522     }
8523
8524   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8525   return set_die_type (die, cv_type, cu);
8526 }
8527
8528 static struct type *
8529 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8530 {
8531   struct type *base_type, *cv_type;
8532
8533   base_type = die_type (die, cu);
8534
8535   /* The die_type call above may have already set the type for this DIE.  */
8536   cv_type = get_die_type (die, cu);
8537   if (cv_type)
8538     return cv_type;
8539
8540   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8541   return set_die_type (die, cv_type, cu);
8542 }
8543
8544 /* Extract all information from a DW_TAG_string_type DIE and add to
8545    the user defined type vector.  It isn't really a user defined type,
8546    but it behaves like one, with other DIE's using an AT_user_def_type
8547    attribute to reference it.  */
8548
8549 static struct type *
8550 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8551 {
8552   struct objfile *objfile = cu->objfile;
8553   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8554   struct type *type, *range_type, *index_type, *char_type;
8555   struct attribute *attr;
8556   unsigned int length;
8557
8558   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8559   if (attr)
8560     {
8561       length = DW_UNSND (attr);
8562     }
8563   else
8564     {
8565       /* Check for the DW_AT_byte_size attribute.  */
8566       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8567       if (attr)
8568         {
8569           length = DW_UNSND (attr);
8570         }
8571       else
8572         {
8573           length = 1;
8574         }
8575     }
8576
8577   index_type = objfile_type (objfile)->builtin_int;
8578   range_type = create_range_type (NULL, index_type, 1, length);
8579   char_type = language_string_char_type (cu->language_defn, gdbarch);
8580   type = create_string_type (NULL, char_type, range_type);
8581
8582   return set_die_type (die, type, cu);
8583 }
8584
8585 /* Handle DIES due to C code like:
8586
8587    struct foo
8588    {
8589    int (*funcp)(int a, long l);
8590    int b;
8591    };
8592
8593    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8594
8595 static struct type *
8596 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8597 {
8598   struct objfile *objfile = cu->objfile;
8599   struct type *type;            /* Type that this function returns.  */
8600   struct type *ftype;           /* Function that returns above type.  */
8601   struct attribute *attr;
8602
8603   type = die_type (die, cu);
8604
8605   /* The die_type call above may have already set the type for this DIE.  */
8606   ftype = get_die_type (die, cu);
8607   if (ftype)
8608     return ftype;
8609
8610   ftype = lookup_function_type (type);
8611
8612   /* All functions in C++, Pascal and Java have prototypes.  */
8613   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8614   if ((attr && (DW_UNSND (attr) != 0))
8615       || cu->language == language_cplus
8616       || cu->language == language_java
8617       || cu->language == language_pascal)
8618     TYPE_PROTOTYPED (ftype) = 1;
8619   else if (producer_is_realview (cu->producer))
8620     /* RealView does not emit DW_AT_prototyped.  We can not
8621        distinguish prototyped and unprototyped functions; default to
8622        prototyped, since that is more common in modern code (and
8623        RealView warns about unprototyped functions).  */
8624     TYPE_PROTOTYPED (ftype) = 1;
8625
8626   /* Store the calling convention in the type if it's available in
8627      the subroutine die.  Otherwise set the calling convention to
8628      the default value DW_CC_normal.  */
8629   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8630   if (attr)
8631     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8632   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8633     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8634   else
8635     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8636
8637   /* We need to add the subroutine type to the die immediately so
8638      we don't infinitely recurse when dealing with parameters
8639      declared as the same subroutine type.  */
8640   set_die_type (die, ftype, cu);
8641
8642   if (die->child != NULL)
8643     {
8644       struct type *void_type = objfile_type (objfile)->builtin_void;
8645       struct die_info *child_die;
8646       int nparams, iparams;
8647
8648       /* Count the number of parameters.
8649          FIXME: GDB currently ignores vararg functions, but knows about
8650          vararg member functions.  */
8651       nparams = 0;
8652       child_die = die->child;
8653       while (child_die && child_die->tag)
8654         {
8655           if (child_die->tag == DW_TAG_formal_parameter)
8656             nparams++;
8657           else if (child_die->tag == DW_TAG_unspecified_parameters)
8658             TYPE_VARARGS (ftype) = 1;
8659           child_die = sibling_die (child_die);
8660         }
8661
8662       /* Allocate storage for parameters and fill them in.  */
8663       TYPE_NFIELDS (ftype) = nparams;
8664       TYPE_FIELDS (ftype) = (struct field *)
8665         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8666
8667       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8668          even if we error out during the parameters reading below.  */
8669       for (iparams = 0; iparams < nparams; iparams++)
8670         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8671
8672       iparams = 0;
8673       child_die = die->child;
8674       while (child_die && child_die->tag)
8675         {
8676           if (child_die->tag == DW_TAG_formal_parameter)
8677             {
8678               struct type *arg_type;
8679
8680               /* DWARF version 2 has no clean way to discern C++
8681                  static and non-static member functions.  G++ helps
8682                  GDB by marking the first parameter for non-static
8683                  member functions (which is the this pointer) as
8684                  artificial.  We pass this information to
8685                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8686
8687                  DWARF version 3 added DW_AT_object_pointer, which GCC
8688                  4.5 does not yet generate.  */
8689               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8690               if (attr)
8691                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8692               else
8693                 {
8694                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8695
8696                   /* GCC/43521: In java, the formal parameter
8697                      "this" is sometimes not marked with DW_AT_artificial.  */
8698                   if (cu->language == language_java)
8699                     {
8700                       const char *name = dwarf2_name (child_die, cu);
8701
8702                       if (name && !strcmp (name, "this"))
8703                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8704                     }
8705                 }
8706               arg_type = die_type (child_die, cu);
8707
8708               /* RealView does not mark THIS as const, which the testsuite
8709                  expects.  GCC marks THIS as const in method definitions,
8710                  but not in the class specifications (GCC PR 43053).  */
8711               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8712                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8713                 {
8714                   int is_this = 0;
8715                   struct dwarf2_cu *arg_cu = cu;
8716                   const char *name = dwarf2_name (child_die, cu);
8717
8718                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8719                   if (attr)
8720                     {
8721                       /* If the compiler emits this, use it.  */
8722                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8723                         is_this = 1;
8724                     }
8725                   else if (name && strcmp (name, "this") == 0)
8726                     /* Function definitions will have the argument names.  */
8727                     is_this = 1;
8728                   else if (name == NULL && iparams == 0)
8729                     /* Declarations may not have the names, so like
8730                        elsewhere in GDB, assume an artificial first
8731                        argument is "this".  */
8732                     is_this = 1;
8733
8734                   if (is_this)
8735                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8736                                              arg_type, 0);
8737                 }
8738
8739               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8740               iparams++;
8741             }
8742           child_die = sibling_die (child_die);
8743         }
8744     }
8745
8746   return ftype;
8747 }
8748
8749 static struct type *
8750 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8751 {
8752   struct objfile *objfile = cu->objfile;
8753   const char *name = NULL;
8754   struct type *this_type, *target_type;
8755
8756   name = dwarf2_full_name (NULL, die, cu);
8757   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8758                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8759   TYPE_NAME (this_type) = (char *) name;
8760   set_die_type (die, this_type, cu);
8761   target_type = die_type (die, cu);
8762   if (target_type != this_type)
8763     TYPE_TARGET_TYPE (this_type) = target_type;
8764   else
8765     {
8766       /* Self-referential typedefs are, it seems, not allowed by the DWARF
8767          spec and cause infinite loops in GDB.  */
8768       complaint (&symfile_complaints,
8769                  _("Self-referential DW_TAG_typedef "
8770                    "- DIE at 0x%x [in module %s]"),
8771                  die->offset, objfile->name);
8772       TYPE_TARGET_TYPE (this_type) = NULL;
8773     }
8774   return this_type;
8775 }
8776
8777 /* Find a representation of a given base type and install
8778    it in the TYPE field of the die.  */
8779
8780 static struct type *
8781 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8782 {
8783   struct objfile *objfile = cu->objfile;
8784   struct type *type;
8785   struct attribute *attr;
8786   int encoding = 0, size = 0;
8787   char *name;
8788   enum type_code code = TYPE_CODE_INT;
8789   int type_flags = 0;
8790   struct type *target_type = NULL;
8791
8792   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8793   if (attr)
8794     {
8795       encoding = DW_UNSND (attr);
8796     }
8797   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8798   if (attr)
8799     {
8800       size = DW_UNSND (attr);
8801     }
8802   name = dwarf2_name (die, cu);
8803   if (!name)
8804     {
8805       complaint (&symfile_complaints,
8806                  _("DW_AT_name missing from DW_TAG_base_type"));
8807     }
8808
8809   switch (encoding)
8810     {
8811       case DW_ATE_address:
8812         /* Turn DW_ATE_address into a void * pointer.  */
8813         code = TYPE_CODE_PTR;
8814         type_flags |= TYPE_FLAG_UNSIGNED;
8815         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8816         break;
8817       case DW_ATE_boolean:
8818         code = TYPE_CODE_BOOL;
8819         type_flags |= TYPE_FLAG_UNSIGNED;
8820         break;
8821       case DW_ATE_complex_float:
8822         code = TYPE_CODE_COMPLEX;
8823         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8824         break;
8825       case DW_ATE_decimal_float:
8826         code = TYPE_CODE_DECFLOAT;
8827         break;
8828       case DW_ATE_float:
8829         code = TYPE_CODE_FLT;
8830         break;
8831       case DW_ATE_signed:
8832         break;
8833       case DW_ATE_unsigned:
8834         type_flags |= TYPE_FLAG_UNSIGNED;
8835         if (cu->language == language_fortran
8836             && name
8837             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8838           code = TYPE_CODE_CHAR;
8839         break;
8840       case DW_ATE_signed_char:
8841         if (cu->language == language_ada || cu->language == language_m2
8842             || cu->language == language_pascal
8843             || cu->language == language_fortran)
8844           code = TYPE_CODE_CHAR;
8845         break;
8846       case DW_ATE_unsigned_char:
8847         if (cu->language == language_ada || cu->language == language_m2
8848             || cu->language == language_pascal
8849             || cu->language == language_fortran)
8850           code = TYPE_CODE_CHAR;
8851         type_flags |= TYPE_FLAG_UNSIGNED;
8852         break;
8853       case DW_ATE_UTF:
8854         /* We just treat this as an integer and then recognize the
8855            type by name elsewhere.  */
8856         break;
8857
8858       default:
8859         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8860                    dwarf_type_encoding_name (encoding));
8861         break;
8862     }
8863
8864   type = init_type (code, size, type_flags, NULL, objfile);
8865   TYPE_NAME (type) = name;
8866   TYPE_TARGET_TYPE (type) = target_type;
8867
8868   if (name && strcmp (name, "char") == 0)
8869     TYPE_NOSIGN (type) = 1;
8870
8871   return set_die_type (die, type, cu);
8872 }
8873
8874 /* Read the given DW_AT_subrange DIE.  */
8875
8876 static struct type *
8877 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8878 {
8879   struct type *base_type;
8880   struct type *range_type;
8881   struct attribute *attr;
8882   LONGEST low = 0;
8883   LONGEST high = -1;
8884   char *name;
8885   LONGEST negative_mask;
8886
8887   base_type = die_type (die, cu);
8888   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8889   check_typedef (base_type);
8890
8891   /* The die_type call above may have already set the type for this DIE.  */
8892   range_type = get_die_type (die, cu);
8893   if (range_type)
8894     return range_type;
8895
8896   if (cu->language == language_fortran)
8897     {
8898       /* FORTRAN implies a lower bound of 1, if not given.  */
8899       low = 1;
8900     }
8901
8902   /* FIXME: For variable sized arrays either of these could be
8903      a variable rather than a constant value.  We'll allow it,
8904      but we don't know how to handle it.  */
8905   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8906   if (attr)
8907     low = dwarf2_get_attr_constant_value (attr, 0);
8908
8909   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8910   if (attr)
8911     {
8912       if (attr_form_is_block (attr) || is_ref_attr (attr))
8913         {
8914           /* GCC encodes arrays with unspecified or dynamic length
8915              with a DW_FORM_block1 attribute or a reference attribute.
8916              FIXME: GDB does not yet know how to handle dynamic
8917              arrays properly, treat them as arrays with unspecified
8918              length for now.
8919
8920              FIXME: jimb/2003-09-22: GDB does not really know
8921              how to handle arrays of unspecified length
8922              either; we just represent them as zero-length
8923              arrays.  Choose an appropriate upper bound given
8924              the lower bound we've computed above.  */
8925           high = low - 1;
8926         }
8927       else
8928         high = dwarf2_get_attr_constant_value (attr, 1);
8929     }
8930   else
8931     {
8932       attr = dwarf2_attr (die, DW_AT_count, cu);
8933       if (attr)
8934         {
8935           int count = dwarf2_get_attr_constant_value (attr, 1);
8936           high = low + count - 1;
8937         }
8938       else
8939         {
8940           /* Unspecified array length.  */
8941           high = low - 1;
8942         }
8943     }
8944
8945   /* Dwarf-2 specifications explicitly allows to create subrange types
8946      without specifying a base type.
8947      In that case, the base type must be set to the type of
8948      the lower bound, upper bound or count, in that order, if any of these
8949      three attributes references an object that has a type.
8950      If no base type is found, the Dwarf-2 specifications say that
8951      a signed integer type of size equal to the size of an address should
8952      be used.
8953      For the following C code: `extern char gdb_int [];'
8954      GCC produces an empty range DIE.
8955      FIXME: muller/2010-05-28: Possible references to object for low bound,
8956      high bound or count are not yet handled by this code.  */
8957   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8958     {
8959       struct objfile *objfile = cu->objfile;
8960       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8961       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8962       struct type *int_type = objfile_type (objfile)->builtin_int;
8963
8964       /* Test "int", "long int", and "long long int" objfile types,
8965          and select the first one having a size above or equal to the
8966          architecture address size.  */
8967       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8968         base_type = int_type;
8969       else
8970         {
8971           int_type = objfile_type (objfile)->builtin_long;
8972           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8973             base_type = int_type;
8974           else
8975             {
8976               int_type = objfile_type (objfile)->builtin_long_long;
8977               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8978                 base_type = int_type;
8979             }
8980         }
8981     }
8982
8983   negative_mask =
8984     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8985   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8986     low |= negative_mask;
8987   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8988     high |= negative_mask;
8989
8990   range_type = create_range_type (NULL, base_type, low, high);
8991
8992   /* Mark arrays with dynamic length at least as an array of unspecified
8993      length.  GDB could check the boundary but before it gets implemented at
8994      least allow accessing the array elements.  */
8995   if (attr && attr_form_is_block (attr))
8996     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8997
8998   /* Ada expects an empty array on no boundary attributes.  */
8999   if (attr == NULL && cu->language != language_ada)
9000     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9001
9002   name = dwarf2_name (die, cu);
9003   if (name)
9004     TYPE_NAME (range_type) = name;
9005
9006   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9007   if (attr)
9008     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9009
9010   set_die_type (die, range_type, cu);
9011
9012   /* set_die_type should be already done.  */
9013   set_descriptive_type (range_type, die, cu);
9014
9015   return range_type;
9016 }
9017
9018 static struct type *
9019 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9020 {
9021   struct type *type;
9022
9023   /* For now, we only support the C meaning of an unspecified type: void.  */
9024
9025   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9026   TYPE_NAME (type) = dwarf2_name (die, cu);
9027
9028   return set_die_type (die, type, cu);
9029 }
9030
9031 /* Trivial hash function for die_info: the hash value of a DIE
9032    is its offset in .debug_info for this objfile.  */
9033
9034 static hashval_t
9035 die_hash (const void *item)
9036 {
9037   const struct die_info *die = item;
9038
9039   return die->offset;
9040 }
9041
9042 /* Trivial comparison function for die_info structures: two DIEs
9043    are equal if they have the same offset.  */
9044
9045 static int
9046 die_eq (const void *item_lhs, const void *item_rhs)
9047 {
9048   const struct die_info *die_lhs = item_lhs;
9049   const struct die_info *die_rhs = item_rhs;
9050
9051   return die_lhs->offset == die_rhs->offset;
9052 }
9053
9054 /* Read a whole compilation unit into a linked list of dies.  */
9055
9056 static struct die_info *
9057 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9058 {
9059   struct die_reader_specs reader_specs;
9060   int read_abbrevs = 0;
9061   struct cleanup *back_to = NULL;
9062   struct die_info *die;
9063
9064   if (cu->dwarf2_abbrevs == NULL)
9065     {
9066       dwarf2_read_abbrevs (cu);
9067       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9068       read_abbrevs = 1;
9069     }
9070
9071   gdb_assert (cu->die_hash == NULL);
9072   cu->die_hash
9073     = htab_create_alloc_ex (cu->header.length / 12,
9074                             die_hash,
9075                             die_eq,
9076                             NULL,
9077                             &cu->comp_unit_obstack,
9078                             hashtab_obstack_allocate,
9079                             dummy_obstack_deallocate);
9080
9081   init_cu_die_reader (&reader_specs, cu);
9082
9083   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9084
9085   if (read_abbrevs)
9086     do_cleanups (back_to);
9087
9088   return die;
9089 }
9090
9091 /* Main entry point for reading a DIE and all children.
9092    Read the DIE and dump it if requested.  */
9093
9094 static struct die_info *
9095 read_die_and_children (const struct die_reader_specs *reader,
9096                        gdb_byte *info_ptr,
9097                        gdb_byte **new_info_ptr,
9098                        struct die_info *parent)
9099 {
9100   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9101                                                      new_info_ptr, parent);
9102
9103   if (dwarf2_die_debug)
9104     {
9105       fprintf_unfiltered (gdb_stdlog,
9106                           "\nRead die from %s of %s:\n",
9107                           (reader->cu->per_cu->debug_types_section
9108                            ? ".debug_types"
9109                            : ".debug_info"),
9110                           reader->abfd->filename);
9111       dump_die (result, dwarf2_die_debug);
9112     }
9113
9114   return result;
9115 }
9116
9117 /* Read a single die and all its descendents.  Set the die's sibling
9118    field to NULL; set other fields in the die correctly, and set all
9119    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9120    location of the info_ptr after reading all of those dies.  PARENT
9121    is the parent of the die in question.  */
9122
9123 static struct die_info *
9124 read_die_and_children_1 (const struct die_reader_specs *reader,
9125                          gdb_byte *info_ptr,
9126                          gdb_byte **new_info_ptr,
9127                          struct die_info *parent)
9128 {
9129   struct die_info *die;
9130   gdb_byte *cur_ptr;
9131   int has_children;
9132
9133   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9134   if (die == NULL)
9135     {
9136       *new_info_ptr = cur_ptr;
9137       return NULL;
9138     }
9139   store_in_ref_table (die, reader->cu);
9140
9141   if (has_children)
9142     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9143   else
9144     {
9145       die->child = NULL;
9146       *new_info_ptr = cur_ptr;
9147     }
9148
9149   die->sibling = NULL;
9150   die->parent = parent;
9151   return die;
9152 }
9153
9154 /* Read a die, all of its descendents, and all of its siblings; set
9155    all of the fields of all of the dies correctly.  Arguments are as
9156    in read_die_and_children.  */
9157
9158 static struct die_info *
9159 read_die_and_siblings (const struct die_reader_specs *reader,
9160                        gdb_byte *info_ptr,
9161                        gdb_byte **new_info_ptr,
9162                        struct die_info *parent)
9163 {
9164   struct die_info *first_die, *last_sibling;
9165   gdb_byte *cur_ptr;
9166
9167   cur_ptr = info_ptr;
9168   first_die = last_sibling = NULL;
9169
9170   while (1)
9171     {
9172       struct die_info *die
9173         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9174
9175       if (die == NULL)
9176         {
9177           *new_info_ptr = cur_ptr;
9178           return first_die;
9179         }
9180
9181       if (!first_die)
9182         first_die = die;
9183       else
9184         last_sibling->sibling = die;
9185
9186       last_sibling = die;
9187     }
9188 }
9189
9190 /* Read the die from the .debug_info section buffer.  Set DIEP to
9191    point to a newly allocated die with its information, except for its
9192    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9193    whether the die has children or not.  */
9194
9195 static gdb_byte *
9196 read_full_die (const struct die_reader_specs *reader,
9197                struct die_info **diep, gdb_byte *info_ptr,
9198                int *has_children)
9199 {
9200   unsigned int abbrev_number, bytes_read, i, offset;
9201   struct abbrev_info *abbrev;
9202   struct die_info *die;
9203   struct dwarf2_cu *cu = reader->cu;
9204   bfd *abfd = reader->abfd;
9205
9206   offset = info_ptr - reader->buffer;
9207   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9208   info_ptr += bytes_read;
9209   if (!abbrev_number)
9210     {
9211       *diep = NULL;
9212       *has_children = 0;
9213       return info_ptr;
9214     }
9215
9216   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9217   if (!abbrev)
9218     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9219            abbrev_number,
9220            bfd_get_filename (abfd));
9221
9222   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9223   die->offset = offset;
9224   die->tag = abbrev->tag;
9225   die->abbrev = abbrev_number;
9226
9227   die->num_attrs = abbrev->num_attrs;
9228
9229   for (i = 0; i < abbrev->num_attrs; ++i)
9230     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9231                                abfd, info_ptr, cu);
9232
9233   *diep = die;
9234   *has_children = abbrev->has_children;
9235   return info_ptr;
9236 }
9237
9238 /* In DWARF version 2, the description of the debugging information is
9239    stored in a separate .debug_abbrev section.  Before we read any
9240    dies from a section we read in all abbreviations and install them
9241    in a hash table.  This function also sets flags in CU describing
9242    the data found in the abbrev table.  */
9243
9244 static void
9245 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9246 {
9247   bfd *abfd = cu->objfile->obfd;
9248   struct comp_unit_head *cu_header = &cu->header;
9249   gdb_byte *abbrev_ptr;
9250   struct abbrev_info *cur_abbrev;
9251   unsigned int abbrev_number, bytes_read, abbrev_name;
9252   unsigned int abbrev_form, hash_number;
9253   struct attr_abbrev *cur_attrs;
9254   unsigned int allocated_attrs;
9255
9256   /* Initialize dwarf2 abbrevs.  */
9257   obstack_init (&cu->abbrev_obstack);
9258   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9259                                       (ABBREV_HASH_SIZE
9260                                        * sizeof (struct abbrev_info *)));
9261   memset (cu->dwarf2_abbrevs, 0,
9262           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9263
9264   dwarf2_read_section (dwarf2_per_objfile->objfile,
9265                        &dwarf2_per_objfile->abbrev);
9266   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9267   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9268   abbrev_ptr += bytes_read;
9269
9270   allocated_attrs = ATTR_ALLOC_CHUNK;
9271   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9272
9273   /* Loop until we reach an abbrev number of 0.  */
9274   while (abbrev_number)
9275     {
9276       cur_abbrev = dwarf_alloc_abbrev (cu);
9277
9278       /* read in abbrev header */
9279       cur_abbrev->number = abbrev_number;
9280       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9281       abbrev_ptr += bytes_read;
9282       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9283       abbrev_ptr += 1;
9284
9285       if (cur_abbrev->tag == DW_TAG_namespace)
9286         cu->has_namespace_info = 1;
9287
9288       /* now read in declarations */
9289       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9290       abbrev_ptr += bytes_read;
9291       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9292       abbrev_ptr += bytes_read;
9293       while (abbrev_name)
9294         {
9295           if (cur_abbrev->num_attrs == allocated_attrs)
9296             {
9297               allocated_attrs += ATTR_ALLOC_CHUNK;
9298               cur_attrs
9299                 = xrealloc (cur_attrs, (allocated_attrs
9300                                         * sizeof (struct attr_abbrev)));
9301             }
9302
9303           /* Record whether this compilation unit might have
9304              inter-compilation-unit references.  If we don't know what form
9305              this attribute will have, then it might potentially be a
9306              DW_FORM_ref_addr, so we conservatively expect inter-CU
9307              references.  */
9308
9309           if (abbrev_form == DW_FORM_ref_addr
9310               || abbrev_form == DW_FORM_indirect)
9311             cu->has_form_ref_addr = 1;
9312
9313           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9314           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9315           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9316           abbrev_ptr += bytes_read;
9317           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9318           abbrev_ptr += bytes_read;
9319         }
9320
9321       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9322                                          (cur_abbrev->num_attrs
9323                                           * sizeof (struct attr_abbrev)));
9324       memcpy (cur_abbrev->attrs, cur_attrs,
9325               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9326
9327       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9328       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9329       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9330
9331       /* Get next abbreviation.
9332          Under Irix6 the abbreviations for a compilation unit are not
9333          always properly terminated with an abbrev number of 0.
9334          Exit loop if we encounter an abbreviation which we have
9335          already read (which means we are about to read the abbreviations
9336          for the next compile unit) or if the end of the abbreviation
9337          table is reached.  */
9338       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9339           >= dwarf2_per_objfile->abbrev.size)
9340         break;
9341       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9342       abbrev_ptr += bytes_read;
9343       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9344         break;
9345     }
9346
9347   xfree (cur_attrs);
9348 }
9349
9350 /* Release the memory used by the abbrev table for a compilation unit.  */
9351
9352 static void
9353 dwarf2_free_abbrev_table (void *ptr_to_cu)
9354 {
9355   struct dwarf2_cu *cu = ptr_to_cu;
9356
9357   obstack_free (&cu->abbrev_obstack, NULL);
9358   cu->dwarf2_abbrevs = NULL;
9359 }
9360
9361 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9362
9363 static struct abbrev_info *
9364 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9365 {
9366   unsigned int hash_number;
9367   struct abbrev_info *abbrev;
9368
9369   hash_number = number % ABBREV_HASH_SIZE;
9370   abbrev = cu->dwarf2_abbrevs[hash_number];
9371
9372   while (abbrev)
9373     {
9374       if (abbrev->number == number)
9375         return abbrev;
9376       else
9377         abbrev = abbrev->next;
9378     }
9379   return NULL;
9380 }
9381
9382 /* Returns nonzero if TAG represents a type that we might generate a partial
9383    symbol for.  */
9384
9385 static int
9386 is_type_tag_for_partial (int tag)
9387 {
9388   switch (tag)
9389     {
9390 #if 0
9391     /* Some types that would be reasonable to generate partial symbols for,
9392        that we don't at present.  */
9393     case DW_TAG_array_type:
9394     case DW_TAG_file_type:
9395     case DW_TAG_ptr_to_member_type:
9396     case DW_TAG_set_type:
9397     case DW_TAG_string_type:
9398     case DW_TAG_subroutine_type:
9399 #endif
9400     case DW_TAG_base_type:
9401     case DW_TAG_class_type:
9402     case DW_TAG_interface_type:
9403     case DW_TAG_enumeration_type:
9404     case DW_TAG_structure_type:
9405     case DW_TAG_subrange_type:
9406     case DW_TAG_typedef:
9407     case DW_TAG_union_type:
9408       return 1;
9409     default:
9410       return 0;
9411     }
9412 }
9413
9414 /* Load all DIEs that are interesting for partial symbols into memory.  */
9415
9416 static struct partial_die_info *
9417 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9418                    int building_psymtab, struct dwarf2_cu *cu)
9419 {
9420   struct objfile *objfile = cu->objfile;
9421   struct partial_die_info *part_die;
9422   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9423   struct abbrev_info *abbrev;
9424   unsigned int bytes_read;
9425   unsigned int load_all = 0;
9426
9427   int nesting_level = 1;
9428
9429   parent_die = NULL;
9430   last_die = NULL;
9431
9432   if (cu->per_cu && cu->per_cu->load_all_dies)
9433     load_all = 1;
9434
9435   cu->partial_dies
9436     = htab_create_alloc_ex (cu->header.length / 12,
9437                             partial_die_hash,
9438                             partial_die_eq,
9439                             NULL,
9440                             &cu->comp_unit_obstack,
9441                             hashtab_obstack_allocate,
9442                             dummy_obstack_deallocate);
9443
9444   part_die = obstack_alloc (&cu->comp_unit_obstack,
9445                             sizeof (struct partial_die_info));
9446
9447   while (1)
9448     {
9449       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9450
9451       /* A NULL abbrev means the end of a series of children.  */
9452       if (abbrev == NULL)
9453         {
9454           if (--nesting_level == 0)
9455             {
9456               /* PART_DIE was probably the last thing allocated on the
9457                  comp_unit_obstack, so we could call obstack_free
9458                  here.  We don't do that because the waste is small,
9459                  and will be cleaned up when we're done with this
9460                  compilation unit.  This way, we're also more robust
9461                  against other users of the comp_unit_obstack.  */
9462               return first_die;
9463             }
9464           info_ptr += bytes_read;
9465           last_die = parent_die;
9466           parent_die = parent_die->die_parent;
9467           continue;
9468         }
9469
9470       /* Check for template arguments.  We never save these; if
9471          they're seen, we just mark the parent, and go on our way.  */
9472       if (parent_die != NULL
9473           && cu->language == language_cplus
9474           && (abbrev->tag == DW_TAG_template_type_param
9475               || abbrev->tag == DW_TAG_template_value_param))
9476         {
9477           parent_die->has_template_arguments = 1;
9478
9479           if (!load_all)
9480             {
9481               /* We don't need a partial DIE for the template argument.  */
9482               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9483                                        cu);
9484               continue;
9485             }
9486         }
9487
9488       /* We only recurse into subprograms looking for template arguments.
9489          Skip their other children.  */
9490       if (!load_all
9491           && cu->language == language_cplus
9492           && parent_die != NULL
9493           && parent_die->tag == DW_TAG_subprogram)
9494         {
9495           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9496           continue;
9497         }
9498
9499       /* Check whether this DIE is interesting enough to save.  Normally
9500          we would not be interested in members here, but there may be
9501          later variables referencing them via DW_AT_specification (for
9502          static members).  */
9503       if (!load_all
9504           && !is_type_tag_for_partial (abbrev->tag)
9505           && abbrev->tag != DW_TAG_constant
9506           && abbrev->tag != DW_TAG_enumerator
9507           && abbrev->tag != DW_TAG_subprogram
9508           && abbrev->tag != DW_TAG_lexical_block
9509           && abbrev->tag != DW_TAG_variable
9510           && abbrev->tag != DW_TAG_namespace
9511           && abbrev->tag != DW_TAG_module
9512           && abbrev->tag != DW_TAG_member)
9513         {
9514           /* Otherwise we skip to the next sibling, if any.  */
9515           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9516           continue;
9517         }
9518
9519       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9520                                    buffer, info_ptr, cu);
9521
9522       /* This two-pass algorithm for processing partial symbols has a
9523          high cost in cache pressure.  Thus, handle some simple cases
9524          here which cover the majority of C partial symbols.  DIEs
9525          which neither have specification tags in them, nor could have
9526          specification tags elsewhere pointing at them, can simply be
9527          processed and discarded.
9528
9529          This segment is also optional; scan_partial_symbols and
9530          add_partial_symbol will handle these DIEs if we chain
9531          them in normally.  When compilers which do not emit large
9532          quantities of duplicate debug information are more common,
9533          this code can probably be removed.  */
9534
9535       /* Any complete simple types at the top level (pretty much all
9536          of them, for a language without namespaces), can be processed
9537          directly.  */
9538       if (parent_die == NULL
9539           && part_die->has_specification == 0
9540           && part_die->is_declaration == 0
9541           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9542               || part_die->tag == DW_TAG_base_type
9543               || part_die->tag == DW_TAG_subrange_type))
9544         {
9545           if (building_psymtab && part_die->name != NULL)
9546             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9547                                  VAR_DOMAIN, LOC_TYPEDEF,
9548                                  &objfile->static_psymbols,
9549                                  0, (CORE_ADDR) 0, cu->language, objfile);
9550           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9551           continue;
9552         }
9553
9554       /* The exception for DW_TAG_typedef with has_children above is
9555          a workaround of GCC PR debug/47510.  In the case of this complaint
9556          type_name_no_tag_or_error will error on such types later.
9557
9558          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9559          it could not find the child DIEs referenced later, this is checked
9560          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9561
9562       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9563         complaint (&symfile_complaints,
9564                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9565                      "- DIE at 0x%x [in module %s]"),
9566                    part_die->offset, objfile->name);
9567
9568       /* If we're at the second level, and we're an enumerator, and
9569          our parent has no specification (meaning possibly lives in a
9570          namespace elsewhere), then we can add the partial symbol now
9571          instead of queueing it.  */
9572       if (part_die->tag == DW_TAG_enumerator
9573           && parent_die != NULL
9574           && parent_die->die_parent == NULL
9575           && parent_die->tag == DW_TAG_enumeration_type
9576           && parent_die->has_specification == 0)
9577         {
9578           if (part_die->name == NULL)
9579             complaint (&symfile_complaints,
9580                        _("malformed enumerator DIE ignored"));
9581           else if (building_psymtab)
9582             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9583                                  VAR_DOMAIN, LOC_CONST,
9584                                  (cu->language == language_cplus
9585                                   || cu->language == language_java)
9586                                  ? &objfile->global_psymbols
9587                                  : &objfile->static_psymbols,
9588                                  0, (CORE_ADDR) 0, cu->language, objfile);
9589
9590           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9591           continue;
9592         }
9593
9594       /* We'll save this DIE so link it in.  */
9595       part_die->die_parent = parent_die;
9596       part_die->die_sibling = NULL;
9597       part_die->die_child = NULL;
9598
9599       if (last_die && last_die == parent_die)
9600         last_die->die_child = part_die;
9601       else if (last_die)
9602         last_die->die_sibling = part_die;
9603
9604       last_die = part_die;
9605
9606       if (first_die == NULL)
9607         first_die = part_die;
9608
9609       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9610          find interesting need to be in the hash table, because we
9611          also have the parent/sibling/child chains; only those that we
9612          might refer to by offset later during partial symbol reading.
9613
9614          For now this means things that might have be the target of a
9615          DW_AT_specification, DW_AT_abstract_origin, or
9616          DW_AT_extension.  DW_AT_extension will refer only to
9617          namespaces; DW_AT_abstract_origin refers to functions (and
9618          many things under the function DIE, but we do not recurse
9619          into function DIEs during partial symbol reading) and
9620          possibly variables as well; DW_AT_specification refers to
9621          declarations.  Declarations ought to have the DW_AT_declaration
9622          flag.  It happens that GCC forgets to put it in sometimes, but
9623          only for functions, not for types.
9624
9625          Adding more things than necessary to the hash table is harmless
9626          except for the performance cost.  Adding too few will result in
9627          wasted time in find_partial_die, when we reread the compilation
9628          unit with load_all_dies set.  */
9629
9630       if (load_all
9631           || abbrev->tag == DW_TAG_constant
9632           || abbrev->tag == DW_TAG_subprogram
9633           || abbrev->tag == DW_TAG_variable
9634           || abbrev->tag == DW_TAG_namespace
9635           || part_die->is_declaration)
9636         {
9637           void **slot;
9638
9639           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9640                                            part_die->offset, INSERT);
9641           *slot = part_die;
9642         }
9643
9644       part_die = obstack_alloc (&cu->comp_unit_obstack,
9645                                 sizeof (struct partial_die_info));
9646
9647       /* For some DIEs we want to follow their children (if any).  For C
9648          we have no reason to follow the children of structures; for other
9649          languages we have to, so that we can get at method physnames
9650          to infer fully qualified class names, for DW_AT_specification,
9651          and for C++ template arguments.  For C++, we also look one level
9652          inside functions to find template arguments (if the name of the
9653          function does not already contain the template arguments).
9654
9655          For Ada, we need to scan the children of subprograms and lexical
9656          blocks as well because Ada allows the definition of nested
9657          entities that could be interesting for the debugger, such as
9658          nested subprograms for instance.  */
9659       if (last_die->has_children
9660           && (load_all
9661               || last_die->tag == DW_TAG_namespace
9662               || last_die->tag == DW_TAG_module
9663               || last_die->tag == DW_TAG_enumeration_type
9664               || (cu->language == language_cplus
9665                   && last_die->tag == DW_TAG_subprogram
9666                   && (last_die->name == NULL
9667                       || strchr (last_die->name, '<') == NULL))
9668               || (cu->language != language_c
9669                   && (last_die->tag == DW_TAG_class_type
9670                       || last_die->tag == DW_TAG_interface_type
9671                       || last_die->tag == DW_TAG_structure_type
9672                       || last_die->tag == DW_TAG_union_type))
9673               || (cu->language == language_ada
9674                   && (last_die->tag == DW_TAG_subprogram
9675                       || last_die->tag == DW_TAG_lexical_block))))
9676         {
9677           nesting_level++;
9678           parent_die = last_die;
9679           continue;
9680         }
9681
9682       /* Otherwise we skip to the next sibling, if any.  */
9683       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9684
9685       /* Back to the top, do it again.  */
9686     }
9687 }
9688
9689 /* Read a minimal amount of information into the minimal die structure.  */
9690
9691 static gdb_byte *
9692 read_partial_die (struct partial_die_info *part_die,
9693                   struct abbrev_info *abbrev,
9694                   unsigned int abbrev_len, bfd *abfd,
9695                   gdb_byte *buffer, gdb_byte *info_ptr,
9696                   struct dwarf2_cu *cu)
9697 {
9698   struct objfile *objfile = cu->objfile;
9699   unsigned int i;
9700   struct attribute attr;
9701   int has_low_pc_attr = 0;
9702   int has_high_pc_attr = 0;
9703
9704   memset (part_die, 0, sizeof (struct partial_die_info));
9705
9706   part_die->offset = info_ptr - buffer;
9707
9708   info_ptr += abbrev_len;
9709
9710   if (abbrev == NULL)
9711     return info_ptr;
9712
9713   part_die->tag = abbrev->tag;
9714   part_die->has_children = abbrev->has_children;
9715
9716   for (i = 0; i < abbrev->num_attrs; ++i)
9717     {
9718       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9719
9720       /* Store the data if it is of an attribute we want to keep in a
9721          partial symbol table.  */
9722       switch (attr.name)
9723         {
9724         case DW_AT_name:
9725           switch (part_die->tag)
9726             {
9727             case DW_TAG_compile_unit:
9728             case DW_TAG_type_unit:
9729               /* Compilation units have a DW_AT_name that is a filename, not
9730                  a source language identifier.  */
9731             case DW_TAG_enumeration_type:
9732             case DW_TAG_enumerator:
9733               /* These tags always have simple identifiers already; no need
9734                  to canonicalize them.  */
9735               part_die->name = DW_STRING (&attr);
9736               break;
9737             default:
9738               part_die->name
9739                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9740                                             &objfile->objfile_obstack);
9741               break;
9742             }
9743           break;
9744         case DW_AT_linkage_name:
9745         case DW_AT_MIPS_linkage_name:
9746           /* Note that both forms of linkage name might appear.  We
9747              assume they will be the same, and we only store the last
9748              one we see.  */
9749           if (cu->language == language_ada)
9750             part_die->name = DW_STRING (&attr);
9751           part_die->linkage_name = DW_STRING (&attr);
9752           break;
9753         case DW_AT_low_pc:
9754           has_low_pc_attr = 1;
9755           part_die->lowpc = DW_ADDR (&attr);
9756           break;
9757         case DW_AT_high_pc:
9758           has_high_pc_attr = 1;
9759           part_die->highpc = DW_ADDR (&attr);
9760           break;
9761         case DW_AT_location:
9762           /* Support the .debug_loc offsets.  */
9763           if (attr_form_is_block (&attr))
9764             {
9765                part_die->locdesc = DW_BLOCK (&attr);
9766             }
9767           else if (attr_form_is_section_offset (&attr))
9768             {
9769               dwarf2_complex_location_expr_complaint ();
9770             }
9771           else
9772             {
9773               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9774                                                      "partial symbol information");
9775             }
9776           break;
9777         case DW_AT_external:
9778           part_die->is_external = DW_UNSND (&attr);
9779           break;
9780         case DW_AT_declaration:
9781           part_die->is_declaration = DW_UNSND (&attr);
9782           break;
9783         case DW_AT_type:
9784           part_die->has_type = 1;
9785           break;
9786         case DW_AT_abstract_origin:
9787         case DW_AT_specification:
9788         case DW_AT_extension:
9789           part_die->has_specification = 1;
9790           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9791           break;
9792         case DW_AT_sibling:
9793           /* Ignore absolute siblings, they might point outside of
9794              the current compile unit.  */
9795           if (attr.form == DW_FORM_ref_addr)
9796             complaint (&symfile_complaints,
9797                        _("ignoring absolute DW_AT_sibling"));
9798           else
9799             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9800           break;
9801         case DW_AT_byte_size:
9802           part_die->has_byte_size = 1;
9803           break;
9804         case DW_AT_calling_convention:
9805           /* DWARF doesn't provide a way to identify a program's source-level
9806              entry point.  DW_AT_calling_convention attributes are only meant
9807              to describe functions' calling conventions.
9808
9809              However, because it's a necessary piece of information in
9810              Fortran, and because DW_CC_program is the only piece of debugging
9811              information whose definition refers to a 'main program' at all,
9812              several compilers have begun marking Fortran main programs with
9813              DW_CC_program --- even when those functions use the standard
9814              calling conventions.
9815
9816              So until DWARF specifies a way to provide this information and
9817              compilers pick up the new representation, we'll support this
9818              practice.  */
9819           if (DW_UNSND (&attr) == DW_CC_program
9820               && cu->language == language_fortran)
9821             {
9822               set_main_name (part_die->name);
9823
9824               /* As this DIE has a static linkage the name would be difficult
9825                  to look up later.  */
9826               language_of_main = language_fortran;
9827             }
9828           break;
9829         default:
9830           break;
9831         }
9832     }
9833
9834   if (has_low_pc_attr && has_high_pc_attr)
9835     {
9836       /* When using the GNU linker, .gnu.linkonce. sections are used to
9837          eliminate duplicate copies of functions and vtables and such.
9838          The linker will arbitrarily choose one and discard the others.
9839          The AT_*_pc values for such functions refer to local labels in
9840          these sections.  If the section from that file was discarded, the
9841          labels are not in the output, so the relocs get a value of 0.
9842          If this is a discarded function, mark the pc bounds as invalid,
9843          so that GDB will ignore it.  */
9844       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9845         {
9846           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9847
9848           complaint (&symfile_complaints,
9849                      _("DW_AT_low_pc %s is zero "
9850                        "for DIE at 0x%x [in module %s]"),
9851                      paddress (gdbarch, part_die->lowpc),
9852                      part_die->offset, objfile->name);
9853         }
9854       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9855       else if (part_die->lowpc >= part_die->highpc)
9856         {
9857           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9858
9859           complaint (&symfile_complaints,
9860                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9861                        "for DIE at 0x%x [in module %s]"),
9862                      paddress (gdbarch, part_die->lowpc),
9863                      paddress (gdbarch, part_die->highpc),
9864                      part_die->offset, objfile->name);
9865         }
9866       else
9867         part_die->has_pc_info = 1;
9868     }
9869
9870   return info_ptr;
9871 }
9872
9873 /* Find a cached partial DIE at OFFSET in CU.  */
9874
9875 static struct partial_die_info *
9876 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9877 {
9878   struct partial_die_info *lookup_die = NULL;
9879   struct partial_die_info part_die;
9880
9881   part_die.offset = offset;
9882   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9883
9884   return lookup_die;
9885 }
9886
9887 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9888    except in the case of .debug_types DIEs which do not reference
9889    outside their CU (they do however referencing other types via
9890    DW_FORM_ref_sig8).  */
9891
9892 static struct partial_die_info *
9893 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9894 {
9895   struct objfile *objfile = cu->objfile;
9896   struct dwarf2_per_cu_data *per_cu = NULL;
9897   struct partial_die_info *pd = NULL;
9898
9899   if (cu->per_cu->debug_types_section)
9900     {
9901       pd = find_partial_die_in_comp_unit (offset, cu);
9902       if (pd != NULL)
9903         return pd;
9904       goto not_found;
9905     }
9906
9907   if (offset_in_cu_p (&cu->header, offset))
9908     {
9909       pd = find_partial_die_in_comp_unit (offset, cu);
9910       if (pd != NULL)
9911         return pd;
9912     }
9913
9914   per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9915
9916   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9917     load_partial_comp_unit (per_cu);
9918
9919   per_cu->cu->last_used = 0;
9920   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9921
9922   if (pd == NULL && per_cu->load_all_dies == 0)
9923     {
9924       struct cleanup *back_to;
9925       struct partial_die_info comp_unit_die;
9926       struct abbrev_info *abbrev;
9927       unsigned int bytes_read;
9928       char *info_ptr;
9929
9930       per_cu->load_all_dies = 1;
9931
9932       /* Re-read the DIEs.  */
9933       back_to = make_cleanup (null_cleanup, 0);
9934       if (per_cu->cu->dwarf2_abbrevs == NULL)
9935         {
9936           dwarf2_read_abbrevs (per_cu->cu);
9937           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9938         }
9939       info_ptr = (dwarf2_per_objfile->info.buffer
9940                   + per_cu->cu->header.offset
9941                   + per_cu->cu->header.first_die_offset);
9942       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9943       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9944                                    objfile->obfd,
9945                                    dwarf2_per_objfile->info.buffer, info_ptr,
9946                                    per_cu->cu);
9947       if (comp_unit_die.has_children)
9948         load_partial_dies (objfile->obfd,
9949                            dwarf2_per_objfile->info.buffer, info_ptr,
9950                            0, per_cu->cu);
9951       do_cleanups (back_to);
9952
9953       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9954     }
9955
9956  not_found:
9957
9958   if (pd == NULL)
9959     internal_error (__FILE__, __LINE__,
9960                     _("could not find partial DIE 0x%x "
9961                       "in cache [from module %s]\n"),
9962                     offset, bfd_get_filename (objfile->obfd));
9963   return pd;
9964 }
9965
9966 /* See if we can figure out if the class lives in a namespace.  We do
9967    this by looking for a member function; its demangled name will
9968    contain namespace info, if there is any.  */
9969
9970 static void
9971 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9972                                   struct dwarf2_cu *cu)
9973 {
9974   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9975      what template types look like, because the demangler
9976      frequently doesn't give the same name as the debug info.  We
9977      could fix this by only using the demangled name to get the
9978      prefix (but see comment in read_structure_type).  */
9979
9980   struct partial_die_info *real_pdi;
9981   struct partial_die_info *child_pdi;
9982
9983   /* If this DIE (this DIE's specification, if any) has a parent, then
9984      we should not do this.  We'll prepend the parent's fully qualified
9985      name when we create the partial symbol.  */
9986
9987   real_pdi = struct_pdi;
9988   while (real_pdi->has_specification)
9989     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9990
9991   if (real_pdi->die_parent != NULL)
9992     return;
9993
9994   for (child_pdi = struct_pdi->die_child;
9995        child_pdi != NULL;
9996        child_pdi = child_pdi->die_sibling)
9997     {
9998       if (child_pdi->tag == DW_TAG_subprogram
9999           && child_pdi->linkage_name != NULL)
10000         {
10001           char *actual_class_name
10002             = language_class_name_from_physname (cu->language_defn,
10003                                                  child_pdi->linkage_name);
10004           if (actual_class_name != NULL)
10005             {
10006               struct_pdi->name
10007                 = obsavestring (actual_class_name,
10008                                 strlen (actual_class_name),
10009                                 &cu->objfile->objfile_obstack);
10010               xfree (actual_class_name);
10011             }
10012           break;
10013         }
10014     }
10015 }
10016
10017 /* Adjust PART_DIE before generating a symbol for it.  This function
10018    may set the is_external flag or change the DIE's name.  */
10019
10020 static void
10021 fixup_partial_die (struct partial_die_info *part_die,
10022                    struct dwarf2_cu *cu)
10023 {
10024   /* Once we've fixed up a die, there's no point in doing so again.
10025      This also avoids a memory leak if we were to call
10026      guess_partial_die_structure_name multiple times.  */
10027   if (part_die->fixup_called)
10028     return;
10029
10030   /* If we found a reference attribute and the DIE has no name, try
10031      to find a name in the referred to DIE.  */
10032
10033   if (part_die->name == NULL && part_die->has_specification)
10034     {
10035       struct partial_die_info *spec_die;
10036
10037       spec_die = find_partial_die (part_die->spec_offset, cu);
10038
10039       fixup_partial_die (spec_die, cu);
10040
10041       if (spec_die->name)
10042         {
10043           part_die->name = spec_die->name;
10044
10045           /* Copy DW_AT_external attribute if it is set.  */
10046           if (spec_die->is_external)
10047             part_die->is_external = spec_die->is_external;
10048         }
10049     }
10050
10051   /* Set default names for some unnamed DIEs.  */
10052
10053   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10054     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10055
10056   /* If there is no parent die to provide a namespace, and there are
10057      children, see if we can determine the namespace from their linkage
10058      name.
10059      NOTE: We need to do this even if cu->has_namespace_info != 0.
10060      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
10061   if (cu->language == language_cplus
10062       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10063       && part_die->die_parent == NULL
10064       && part_die->has_children
10065       && (part_die->tag == DW_TAG_class_type
10066           || part_die->tag == DW_TAG_structure_type
10067           || part_die->tag == DW_TAG_union_type))
10068     guess_partial_die_structure_name (part_die, cu);
10069
10070   /* GCC might emit a nameless struct or union that has a linkage
10071      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10072   if (part_die->name == NULL
10073       && (part_die->tag == DW_TAG_class_type
10074           || part_die->tag == DW_TAG_interface_type
10075           || part_die->tag == DW_TAG_structure_type
10076           || part_die->tag == DW_TAG_union_type)
10077       && part_die->linkage_name != NULL)
10078     {
10079       char *demangled;
10080
10081       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10082       if (demangled)
10083         {
10084           const char *base;
10085
10086           /* Strip any leading namespaces/classes, keep only the base name.
10087              DW_AT_name for named DIEs does not contain the prefixes.  */
10088           base = strrchr (demangled, ':');
10089           if (base && base > demangled && base[-1] == ':')
10090             base++;
10091           else
10092             base = demangled;
10093
10094           part_die->name = obsavestring (base, strlen (base),
10095                                          &cu->objfile->objfile_obstack);
10096           xfree (demangled);
10097         }
10098     }
10099
10100   part_die->fixup_called = 1;
10101 }
10102
10103 /* Read an attribute value described by an attribute form.  */
10104
10105 static gdb_byte *
10106 read_attribute_value (struct attribute *attr, unsigned form,
10107                       bfd *abfd, gdb_byte *info_ptr,
10108                       struct dwarf2_cu *cu)
10109 {
10110   struct comp_unit_head *cu_header = &cu->header;
10111   unsigned int bytes_read;
10112   struct dwarf_block *blk;
10113
10114   attr->form = form;
10115   switch (form)
10116     {
10117     case DW_FORM_ref_addr:
10118       if (cu->header.version == 2)
10119         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10120       else
10121         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10122                                       &cu->header, &bytes_read);
10123       info_ptr += bytes_read;
10124       break;
10125     case DW_FORM_addr:
10126       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10127       info_ptr += bytes_read;
10128       break;
10129     case DW_FORM_block2:
10130       blk = dwarf_alloc_block (cu);
10131       blk->size = read_2_bytes (abfd, info_ptr);
10132       info_ptr += 2;
10133       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10134       info_ptr += blk->size;
10135       DW_BLOCK (attr) = blk;
10136       break;
10137     case DW_FORM_block4:
10138       blk = dwarf_alloc_block (cu);
10139       blk->size = read_4_bytes (abfd, info_ptr);
10140       info_ptr += 4;
10141       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10142       info_ptr += blk->size;
10143       DW_BLOCK (attr) = blk;
10144       break;
10145     case DW_FORM_data2:
10146       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10147       info_ptr += 2;
10148       break;
10149     case DW_FORM_data4:
10150       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10151       info_ptr += 4;
10152       break;
10153     case DW_FORM_data8:
10154       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10155       info_ptr += 8;
10156       break;
10157     case DW_FORM_sec_offset:
10158       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10159       info_ptr += bytes_read;
10160       break;
10161     case DW_FORM_string:
10162       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10163       DW_STRING_IS_CANONICAL (attr) = 0;
10164       info_ptr += bytes_read;
10165       break;
10166     case DW_FORM_strp:
10167       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10168                                                &bytes_read);
10169       DW_STRING_IS_CANONICAL (attr) = 0;
10170       info_ptr += bytes_read;
10171       break;
10172     case DW_FORM_exprloc:
10173     case DW_FORM_block:
10174       blk = dwarf_alloc_block (cu);
10175       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10176       info_ptr += bytes_read;
10177       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10178       info_ptr += blk->size;
10179       DW_BLOCK (attr) = blk;
10180       break;
10181     case DW_FORM_block1:
10182       blk = dwarf_alloc_block (cu);
10183       blk->size = read_1_byte (abfd, info_ptr);
10184       info_ptr += 1;
10185       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10186       info_ptr += blk->size;
10187       DW_BLOCK (attr) = blk;
10188       break;
10189     case DW_FORM_data1:
10190       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10191       info_ptr += 1;
10192       break;
10193     case DW_FORM_flag:
10194       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10195       info_ptr += 1;
10196       break;
10197     case DW_FORM_flag_present:
10198       DW_UNSND (attr) = 1;
10199       break;
10200     case DW_FORM_sdata:
10201       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10202       info_ptr += bytes_read;
10203       break;
10204     case DW_FORM_udata:
10205       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10206       info_ptr += bytes_read;
10207       break;
10208     case DW_FORM_ref1:
10209       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10210       info_ptr += 1;
10211       break;
10212     case DW_FORM_ref2:
10213       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10214       info_ptr += 2;
10215       break;
10216     case DW_FORM_ref4:
10217       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10218       info_ptr += 4;
10219       break;
10220     case DW_FORM_ref8:
10221       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10222       info_ptr += 8;
10223       break;
10224     case DW_FORM_ref_sig8:
10225       /* Convert the signature to something we can record in DW_UNSND
10226          for later lookup.
10227          NOTE: This is NULL if the type wasn't found.  */
10228       DW_SIGNATURED_TYPE (attr) =
10229         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10230       info_ptr += 8;
10231       break;
10232     case DW_FORM_ref_udata:
10233       DW_ADDR (attr) = (cu->header.offset
10234                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10235       info_ptr += bytes_read;
10236       break;
10237     case DW_FORM_indirect:
10238       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10239       info_ptr += bytes_read;
10240       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10241       break;
10242     default:
10243       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10244              dwarf_form_name (form),
10245              bfd_get_filename (abfd));
10246     }
10247
10248   /* We have seen instances where the compiler tried to emit a byte
10249      size attribute of -1 which ended up being encoded as an unsigned
10250      0xffffffff.  Although 0xffffffff is technically a valid size value,
10251      an object of this size seems pretty unlikely so we can relatively
10252      safely treat these cases as if the size attribute was invalid and
10253      treat them as zero by default.  */
10254   if (attr->name == DW_AT_byte_size
10255       && form == DW_FORM_data4
10256       && DW_UNSND (attr) >= 0xffffffff)
10257     {
10258       complaint
10259         (&symfile_complaints,
10260          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10261          hex_string (DW_UNSND (attr)));
10262       DW_UNSND (attr) = 0;
10263     }
10264
10265   return info_ptr;
10266 }
10267
10268 /* Read an attribute described by an abbreviated attribute.  */
10269
10270 static gdb_byte *
10271 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10272                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10273 {
10274   attr->name = abbrev->name;
10275   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10276 }
10277
10278 /* Read dwarf information from a buffer.  */
10279
10280 static unsigned int
10281 read_1_byte (bfd *abfd, gdb_byte *buf)
10282 {
10283   return bfd_get_8 (abfd, buf);
10284 }
10285
10286 static int
10287 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10288 {
10289   return bfd_get_signed_8 (abfd, buf);
10290 }
10291
10292 static unsigned int
10293 read_2_bytes (bfd *abfd, gdb_byte *buf)
10294 {
10295   return bfd_get_16 (abfd, buf);
10296 }
10297
10298 static int
10299 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10300 {
10301   return bfd_get_signed_16 (abfd, buf);
10302 }
10303
10304 static unsigned int
10305 read_4_bytes (bfd *abfd, gdb_byte *buf)
10306 {
10307   return bfd_get_32 (abfd, buf);
10308 }
10309
10310 static int
10311 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10312 {
10313   return bfd_get_signed_32 (abfd, buf);
10314 }
10315
10316 static ULONGEST
10317 read_8_bytes (bfd *abfd, gdb_byte *buf)
10318 {
10319   return bfd_get_64 (abfd, buf);
10320 }
10321
10322 static CORE_ADDR
10323 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10324               unsigned int *bytes_read)
10325 {
10326   struct comp_unit_head *cu_header = &cu->header;
10327   CORE_ADDR retval = 0;
10328
10329   if (cu_header->signed_addr_p)
10330     {
10331       switch (cu_header->addr_size)
10332         {
10333         case 2:
10334           retval = bfd_get_signed_16 (abfd, buf);
10335           break;
10336         case 4:
10337           retval = bfd_get_signed_32 (abfd, buf);
10338           break;
10339         case 8:
10340           retval = bfd_get_signed_64 (abfd, buf);
10341           break;
10342         default:
10343           internal_error (__FILE__, __LINE__,
10344                           _("read_address: bad switch, signed [in module %s]"),
10345                           bfd_get_filename (abfd));
10346         }
10347     }
10348   else
10349     {
10350       switch (cu_header->addr_size)
10351         {
10352         case 2:
10353           retval = bfd_get_16 (abfd, buf);
10354           break;
10355         case 4:
10356           retval = bfd_get_32 (abfd, buf);
10357           break;
10358         case 8:
10359           retval = bfd_get_64 (abfd, buf);
10360           break;
10361         default:
10362           internal_error (__FILE__, __LINE__,
10363                           _("read_address: bad switch, "
10364                             "unsigned [in module %s]"),
10365                           bfd_get_filename (abfd));
10366         }
10367     }
10368
10369   *bytes_read = cu_header->addr_size;
10370   return retval;
10371 }
10372
10373 /* Read the initial length from a section.  The (draft) DWARF 3
10374    specification allows the initial length to take up either 4 bytes
10375    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10376    bytes describe the length and all offsets will be 8 bytes in length
10377    instead of 4.
10378
10379    An older, non-standard 64-bit format is also handled by this
10380    function.  The older format in question stores the initial length
10381    as an 8-byte quantity without an escape value.  Lengths greater
10382    than 2^32 aren't very common which means that the initial 4 bytes
10383    is almost always zero.  Since a length value of zero doesn't make
10384    sense for the 32-bit format, this initial zero can be considered to
10385    be an escape value which indicates the presence of the older 64-bit
10386    format.  As written, the code can't detect (old format) lengths
10387    greater than 4GB.  If it becomes necessary to handle lengths
10388    somewhat larger than 4GB, we could allow other small values (such
10389    as the non-sensical values of 1, 2, and 3) to also be used as
10390    escape values indicating the presence of the old format.
10391
10392    The value returned via bytes_read should be used to increment the
10393    relevant pointer after calling read_initial_length().
10394
10395    [ Note:  read_initial_length() and read_offset() are based on the
10396      document entitled "DWARF Debugging Information Format", revision
10397      3, draft 8, dated November 19, 2001.  This document was obtained
10398      from:
10399
10400         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10401
10402      This document is only a draft and is subject to change.  (So beware.)
10403
10404      Details regarding the older, non-standard 64-bit format were
10405      determined empirically by examining 64-bit ELF files produced by
10406      the SGI toolchain on an IRIX 6.5 machine.
10407
10408      - Kevin, July 16, 2002
10409    ] */
10410
10411 static LONGEST
10412 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10413 {
10414   LONGEST length = bfd_get_32 (abfd, buf);
10415
10416   if (length == 0xffffffff)
10417     {
10418       length = bfd_get_64 (abfd, buf + 4);
10419       *bytes_read = 12;
10420     }
10421   else if (length == 0)
10422     {
10423       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10424       length = bfd_get_64 (abfd, buf);
10425       *bytes_read = 8;
10426     }
10427   else
10428     {
10429       *bytes_read = 4;
10430     }
10431
10432   return length;
10433 }
10434
10435 /* Cover function for read_initial_length.
10436    Returns the length of the object at BUF, and stores the size of the
10437    initial length in *BYTES_READ and stores the size that offsets will be in
10438    *OFFSET_SIZE.
10439    If the initial length size is not equivalent to that specified in
10440    CU_HEADER then issue a complaint.
10441    This is useful when reading non-comp-unit headers.  */
10442
10443 static LONGEST
10444 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10445                                         const struct comp_unit_head *cu_header,
10446                                         unsigned int *bytes_read,
10447                                         unsigned int *offset_size)
10448 {
10449   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10450
10451   gdb_assert (cu_header->initial_length_size == 4
10452               || cu_header->initial_length_size == 8
10453               || cu_header->initial_length_size == 12);
10454
10455   if (cu_header->initial_length_size != *bytes_read)
10456     complaint (&symfile_complaints,
10457                _("intermixed 32-bit and 64-bit DWARF sections"));
10458
10459   *offset_size = (*bytes_read == 4) ? 4 : 8;
10460   return length;
10461 }
10462
10463 /* Read an offset from the data stream.  The size of the offset is
10464    given by cu_header->offset_size.  */
10465
10466 static LONGEST
10467 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10468              unsigned int *bytes_read)
10469 {
10470   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10471
10472   *bytes_read = cu_header->offset_size;
10473   return offset;
10474 }
10475
10476 /* Read an offset from the data stream.  */
10477
10478 static LONGEST
10479 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10480 {
10481   LONGEST retval = 0;
10482
10483   switch (offset_size)
10484     {
10485     case 4:
10486       retval = bfd_get_32 (abfd, buf);
10487       break;
10488     case 8:
10489       retval = bfd_get_64 (abfd, buf);
10490       break;
10491     default:
10492       internal_error (__FILE__, __LINE__,
10493                       _("read_offset_1: bad switch [in module %s]"),
10494                       bfd_get_filename (abfd));
10495     }
10496
10497   return retval;
10498 }
10499
10500 static gdb_byte *
10501 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10502 {
10503   /* If the size of a host char is 8 bits, we can return a pointer
10504      to the buffer, otherwise we have to copy the data to a buffer
10505      allocated on the temporary obstack.  */
10506   gdb_assert (HOST_CHAR_BIT == 8);
10507   return buf;
10508 }
10509
10510 static char *
10511 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10512 {
10513   /* If the size of a host char is 8 bits, we can return a pointer
10514      to the string, otherwise we have to copy the string to a buffer
10515      allocated on the temporary obstack.  */
10516   gdb_assert (HOST_CHAR_BIT == 8);
10517   if (*buf == '\0')
10518     {
10519       *bytes_read_ptr = 1;
10520       return NULL;
10521     }
10522   *bytes_read_ptr = strlen ((char *) buf) + 1;
10523   return (char *) buf;
10524 }
10525
10526 static char *
10527 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10528 {
10529   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10530   if (dwarf2_per_objfile->str.buffer == NULL)
10531     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10532            bfd_get_filename (abfd));
10533   if (str_offset >= dwarf2_per_objfile->str.size)
10534     error (_("DW_FORM_strp pointing outside of "
10535              ".debug_str section [in module %s]"),
10536            bfd_get_filename (abfd));
10537   gdb_assert (HOST_CHAR_BIT == 8);
10538   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10539     return NULL;
10540   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10541 }
10542
10543 static char *
10544 read_indirect_string (bfd *abfd, gdb_byte *buf,
10545                       const struct comp_unit_head *cu_header,
10546                       unsigned int *bytes_read_ptr)
10547 {
10548   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10549
10550   return read_indirect_string_at_offset (abfd, str_offset);
10551 }
10552
10553 static unsigned long
10554 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10555 {
10556   unsigned long result;
10557   unsigned int num_read;
10558   int i, shift;
10559   unsigned char byte;
10560
10561   result = 0;
10562   shift = 0;
10563   num_read = 0;
10564   i = 0;
10565   while (1)
10566     {
10567       byte = bfd_get_8 (abfd, buf);
10568       buf++;
10569       num_read++;
10570       result |= ((unsigned long)(byte & 127) << shift);
10571       if ((byte & 128) == 0)
10572         {
10573           break;
10574         }
10575       shift += 7;
10576     }
10577   *bytes_read_ptr = num_read;
10578   return result;
10579 }
10580
10581 static long
10582 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10583 {
10584   long result;
10585   int i, shift, num_read;
10586   unsigned char byte;
10587
10588   result = 0;
10589   shift = 0;
10590   num_read = 0;
10591   i = 0;
10592   while (1)
10593     {
10594       byte = bfd_get_8 (abfd, buf);
10595       buf++;
10596       num_read++;
10597       result |= ((long)(byte & 127) << shift);
10598       shift += 7;
10599       if ((byte & 128) == 0)
10600         {
10601           break;
10602         }
10603     }
10604   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10605     result |= -(((long)1) << shift);
10606   *bytes_read_ptr = num_read;
10607   return result;
10608 }
10609
10610 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10611
10612 static gdb_byte *
10613 skip_leb128 (bfd *abfd, gdb_byte *buf)
10614 {
10615   int byte;
10616
10617   while (1)
10618     {
10619       byte = bfd_get_8 (abfd, buf);
10620       buf++;
10621       if ((byte & 128) == 0)
10622         return buf;
10623     }
10624 }
10625
10626 static void
10627 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10628 {
10629   switch (lang)
10630     {
10631     case DW_LANG_C89:
10632     case DW_LANG_C99:
10633     case DW_LANG_C:
10634       cu->language = language_c;
10635       break;
10636     case DW_LANG_C_plus_plus:
10637       cu->language = language_cplus;
10638       break;
10639     case DW_LANG_D:
10640       cu->language = language_d;
10641       break;
10642     case DW_LANG_Fortran77:
10643     case DW_LANG_Fortran90:
10644     case DW_LANG_Fortran95:
10645       cu->language = language_fortran;
10646       break;
10647     case DW_LANG_Mips_Assembler:
10648       cu->language = language_asm;
10649       break;
10650     case DW_LANG_Java:
10651       cu->language = language_java;
10652       break;
10653     case DW_LANG_Ada83:
10654     case DW_LANG_Ada95:
10655       cu->language = language_ada;
10656       break;
10657     case DW_LANG_Modula2:
10658       cu->language = language_m2;
10659       break;
10660     case DW_LANG_Pascal83:
10661       cu->language = language_pascal;
10662       break;
10663     case DW_LANG_ObjC:
10664       cu->language = language_objc;
10665       break;
10666     case DW_LANG_Cobol74:
10667     case DW_LANG_Cobol85:
10668     default:
10669       cu->language = language_minimal;
10670       break;
10671     }
10672   cu->language_defn = language_def (cu->language);
10673 }
10674
10675 /* Return the named attribute or NULL if not there.  */
10676
10677 static struct attribute *
10678 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10679 {
10680   unsigned int i;
10681   struct attribute *spec = NULL;
10682
10683   for (i = 0; i < die->num_attrs; ++i)
10684     {
10685       if (die->attrs[i].name == name)
10686         return &die->attrs[i];
10687       if (die->attrs[i].name == DW_AT_specification
10688           || die->attrs[i].name == DW_AT_abstract_origin)
10689         spec = &die->attrs[i];
10690     }
10691
10692   if (spec)
10693     {
10694       die = follow_die_ref (die, spec, &cu);
10695       return dwarf2_attr (die, name, cu);
10696     }
10697
10698   return NULL;
10699 }
10700
10701 /* Return the named attribute or NULL if not there,
10702    but do not follow DW_AT_specification, etc.
10703    This is for use in contexts where we're reading .debug_types dies.
10704    Following DW_AT_specification, DW_AT_abstract_origin will take us
10705    back up the chain, and we want to go down.  */
10706
10707 static struct attribute *
10708 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10709                        struct dwarf2_cu *cu)
10710 {
10711   unsigned int i;
10712
10713   for (i = 0; i < die->num_attrs; ++i)
10714     if (die->attrs[i].name == name)
10715       return &die->attrs[i];
10716
10717   return NULL;
10718 }
10719
10720 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10721    and holds a non-zero value.  This function should only be used for
10722    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10723
10724 static int
10725 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10726 {
10727   struct attribute *attr = dwarf2_attr (die, name, cu);
10728
10729   return (attr && DW_UNSND (attr));
10730 }
10731
10732 static int
10733 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10736      which value is non-zero.  However, we have to be careful with
10737      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10738      (via dwarf2_flag_true_p) follows this attribute.  So we may
10739      end up accidently finding a declaration attribute that belongs
10740      to a different DIE referenced by the specification attribute,
10741      even though the given DIE does not have a declaration attribute.  */
10742   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10743           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10744 }
10745
10746 /* Return the die giving the specification for DIE, if there is
10747    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10748    containing the return value on output.  If there is no
10749    specification, but there is an abstract origin, that is
10750    returned.  */
10751
10752 static struct die_info *
10753 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10754 {
10755   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10756                                              *spec_cu);
10757
10758   if (spec_attr == NULL)
10759     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10760
10761   if (spec_attr == NULL)
10762     return NULL;
10763   else
10764     return follow_die_ref (die, spec_attr, spec_cu);
10765 }
10766
10767 /* Free the line_header structure *LH, and any arrays and strings it
10768    refers to.
10769    NOTE: This is also used as a "cleanup" function.  */
10770
10771 static void
10772 free_line_header (struct line_header *lh)
10773 {
10774   if (lh->standard_opcode_lengths)
10775     xfree (lh->standard_opcode_lengths);
10776
10777   /* Remember that all the lh->file_names[i].name pointers are
10778      pointers into debug_line_buffer, and don't need to be freed.  */
10779   if (lh->file_names)
10780     xfree (lh->file_names);
10781
10782   /* Similarly for the include directory names.  */
10783   if (lh->include_dirs)
10784     xfree (lh->include_dirs);
10785
10786   xfree (lh);
10787 }
10788
10789 /* Add an entry to LH's include directory table.  */
10790
10791 static void
10792 add_include_dir (struct line_header *lh, char *include_dir)
10793 {
10794   /* Grow the array if necessary.  */
10795   if (lh->include_dirs_size == 0)
10796     {
10797       lh->include_dirs_size = 1; /* for testing */
10798       lh->include_dirs = xmalloc (lh->include_dirs_size
10799                                   * sizeof (*lh->include_dirs));
10800     }
10801   else if (lh->num_include_dirs >= lh->include_dirs_size)
10802     {
10803       lh->include_dirs_size *= 2;
10804       lh->include_dirs = xrealloc (lh->include_dirs,
10805                                    (lh->include_dirs_size
10806                                     * sizeof (*lh->include_dirs)));
10807     }
10808
10809   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10810 }
10811
10812 /* Add an entry to LH's file name table.  */
10813
10814 static void
10815 add_file_name (struct line_header *lh,
10816                char *name,
10817                unsigned int dir_index,
10818                unsigned int mod_time,
10819                unsigned int length)
10820 {
10821   struct file_entry *fe;
10822
10823   /* Grow the array if necessary.  */
10824   if (lh->file_names_size == 0)
10825     {
10826       lh->file_names_size = 1; /* for testing */
10827       lh->file_names = xmalloc (lh->file_names_size
10828                                 * sizeof (*lh->file_names));
10829     }
10830   else if (lh->num_file_names >= lh->file_names_size)
10831     {
10832       lh->file_names_size *= 2;
10833       lh->file_names = xrealloc (lh->file_names,
10834                                  (lh->file_names_size
10835                                   * sizeof (*lh->file_names)));
10836     }
10837
10838   fe = &lh->file_names[lh->num_file_names++];
10839   fe->name = name;
10840   fe->dir_index = dir_index;
10841   fe->mod_time = mod_time;
10842   fe->length = length;
10843   fe->included_p = 0;
10844   fe->symtab = NULL;
10845 }
10846
10847 /* Read the statement program header starting at OFFSET in
10848    .debug_line, according to the endianness of ABFD.  Return a pointer
10849    to a struct line_header, allocated using xmalloc.
10850
10851    NOTE: the strings in the include directory and file name tables of
10852    the returned object point into debug_line_buffer, and must not be
10853    freed.  */
10854
10855 static struct line_header *
10856 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10857                           struct dwarf2_cu *cu)
10858 {
10859   struct cleanup *back_to;
10860   struct line_header *lh;
10861   gdb_byte *line_ptr;
10862   unsigned int bytes_read, offset_size;
10863   int i;
10864   char *cur_dir, *cur_file;
10865
10866   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10867   if (dwarf2_per_objfile->line.buffer == NULL)
10868     {
10869       complaint (&symfile_complaints, _("missing .debug_line section"));
10870       return 0;
10871     }
10872
10873   /* Make sure that at least there's room for the total_length field.
10874      That could be 12 bytes long, but we're just going to fudge that.  */
10875   if (offset + 4 >= dwarf2_per_objfile->line.size)
10876     {
10877       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10878       return 0;
10879     }
10880
10881   lh = xmalloc (sizeof (*lh));
10882   memset (lh, 0, sizeof (*lh));
10883   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10884                           (void *) lh);
10885
10886   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10887
10888   /* Read in the header.  */
10889   lh->total_length =
10890     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10891                                             &bytes_read, &offset_size);
10892   line_ptr += bytes_read;
10893   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10894                                      + dwarf2_per_objfile->line.size))
10895     {
10896       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10897       return 0;
10898     }
10899   lh->statement_program_end = line_ptr + lh->total_length;
10900   lh->version = read_2_bytes (abfd, line_ptr);
10901   line_ptr += 2;
10902   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10903   line_ptr += offset_size;
10904   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10905   line_ptr += 1;
10906   if (lh->version >= 4)
10907     {
10908       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10909       line_ptr += 1;
10910     }
10911   else
10912     lh->maximum_ops_per_instruction = 1;
10913
10914   if (lh->maximum_ops_per_instruction == 0)
10915     {
10916       lh->maximum_ops_per_instruction = 1;
10917       complaint (&symfile_complaints,
10918                  _("invalid maximum_ops_per_instruction "
10919                    "in `.debug_line' section"));
10920     }
10921
10922   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10923   line_ptr += 1;
10924   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10925   line_ptr += 1;
10926   lh->line_range = read_1_byte (abfd, line_ptr);
10927   line_ptr += 1;
10928   lh->opcode_base = read_1_byte (abfd, line_ptr);
10929   line_ptr += 1;
10930   lh->standard_opcode_lengths
10931     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10932
10933   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10934   for (i = 1; i < lh->opcode_base; ++i)
10935     {
10936       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10937       line_ptr += 1;
10938     }
10939
10940   /* Read directory table.  */
10941   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10942     {
10943       line_ptr += bytes_read;
10944       add_include_dir (lh, cur_dir);
10945     }
10946   line_ptr += bytes_read;
10947
10948   /* Read file name table.  */
10949   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10950     {
10951       unsigned int dir_index, mod_time, length;
10952
10953       line_ptr += bytes_read;
10954       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10955       line_ptr += bytes_read;
10956       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10957       line_ptr += bytes_read;
10958       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10959       line_ptr += bytes_read;
10960
10961       add_file_name (lh, cur_file, dir_index, mod_time, length);
10962     }
10963   line_ptr += bytes_read;
10964   lh->statement_program_start = line_ptr;
10965
10966   if (line_ptr > (dwarf2_per_objfile->line.buffer
10967                   + dwarf2_per_objfile->line.size))
10968     complaint (&symfile_complaints,
10969                _("line number info header doesn't "
10970                  "fit in `.debug_line' section"));
10971
10972   discard_cleanups (back_to);
10973   return lh;
10974 }
10975
10976 /* Subroutine of dwarf_decode_lines to simplify it.
10977    Return the file name of the psymtab for included file FILE_INDEX
10978    in line header LH of PST.
10979    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10980    If space for the result is malloc'd, it will be freed by a cleanup.
10981    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10982
10983 static char *
10984 psymtab_include_file_name (const struct line_header *lh, int file_index,
10985                            const struct partial_symtab *pst,
10986                            const char *comp_dir)
10987 {
10988   const struct file_entry fe = lh->file_names [file_index];
10989   char *include_name = fe.name;
10990   char *include_name_to_compare = include_name;
10991   char *dir_name = NULL;
10992   const char *pst_filename;
10993   char *copied_name = NULL;
10994   int file_is_pst;
10995
10996   if (fe.dir_index)
10997     dir_name = lh->include_dirs[fe.dir_index - 1];
10998
10999   if (!IS_ABSOLUTE_PATH (include_name)
11000       && (dir_name != NULL || comp_dir != NULL))
11001     {
11002       /* Avoid creating a duplicate psymtab for PST.
11003          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11004          Before we do the comparison, however, we need to account
11005          for DIR_NAME and COMP_DIR.
11006          First prepend dir_name (if non-NULL).  If we still don't
11007          have an absolute path prepend comp_dir (if non-NULL).
11008          However, the directory we record in the include-file's
11009          psymtab does not contain COMP_DIR (to match the
11010          corresponding symtab(s)).
11011
11012          Example:
11013
11014          bash$ cd /tmp
11015          bash$ gcc -g ./hello.c
11016          include_name = "hello.c"
11017          dir_name = "."
11018          DW_AT_comp_dir = comp_dir = "/tmp"
11019          DW_AT_name = "./hello.c"  */
11020
11021       if (dir_name != NULL)
11022         {
11023           include_name = concat (dir_name, SLASH_STRING,
11024                                  include_name, (char *)NULL);
11025           include_name_to_compare = include_name;
11026           make_cleanup (xfree, include_name);
11027         }
11028       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11029         {
11030           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11031                                             include_name, (char *)NULL);
11032         }
11033     }
11034
11035   pst_filename = pst->filename;
11036   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11037     {
11038       copied_name = concat (pst->dirname, SLASH_STRING,
11039                             pst_filename, (char *)NULL);
11040       pst_filename = copied_name;
11041     }
11042
11043   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11044
11045   if (include_name_to_compare != include_name)
11046     xfree (include_name_to_compare);
11047   if (copied_name != NULL)
11048     xfree (copied_name);
11049
11050   if (file_is_pst)
11051     return NULL;
11052   return include_name;
11053 }
11054
11055 /* Ignore this record_line request.  */
11056
11057 static void
11058 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11059 {
11060   return;
11061 }
11062
11063 /* Subroutine of dwarf_decode_lines to simplify it.
11064    Process the line number information in LH.  */
11065
11066 static void
11067 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11068                       struct dwarf2_cu *cu, struct partial_symtab *pst)
11069 {
11070   gdb_byte *line_ptr, *extended_end;
11071   gdb_byte *line_end;
11072   unsigned int bytes_read, extended_len;
11073   unsigned char op_code, extended_op, adj_opcode;
11074   CORE_ADDR baseaddr;
11075   struct objfile *objfile = cu->objfile;
11076   bfd *abfd = objfile->obfd;
11077   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11078   const int decode_for_pst_p = (pst != NULL);
11079   struct subfile *last_subfile = NULL;
11080   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11081     = record_line;
11082
11083   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11084
11085   line_ptr = lh->statement_program_start;
11086   line_end = lh->statement_program_end;
11087
11088   /* Read the statement sequences until there's nothing left.  */
11089   while (line_ptr < line_end)
11090     {
11091       /* state machine registers  */
11092       CORE_ADDR address = 0;
11093       unsigned int file = 1;
11094       unsigned int line = 1;
11095       unsigned int column = 0;
11096       int is_stmt = lh->default_is_stmt;
11097       int basic_block = 0;
11098       int end_sequence = 0;
11099       CORE_ADDR addr;
11100       unsigned char op_index = 0;
11101
11102       if (!decode_for_pst_p && lh->num_file_names >= file)
11103         {
11104           /* Start a subfile for the current file of the state machine.  */
11105           /* lh->include_dirs and lh->file_names are 0-based, but the
11106              directory and file name numbers in the statement program
11107              are 1-based.  */
11108           struct file_entry *fe = &lh->file_names[file - 1];
11109           char *dir = NULL;
11110
11111           if (fe->dir_index)
11112             dir = lh->include_dirs[fe->dir_index - 1];
11113
11114           dwarf2_start_subfile (fe->name, dir, comp_dir);
11115         }
11116
11117       /* Decode the table.  */
11118       while (!end_sequence)
11119         {
11120           op_code = read_1_byte (abfd, line_ptr);
11121           line_ptr += 1;
11122           if (line_ptr > line_end)
11123             {
11124               dwarf2_debug_line_missing_end_sequence_complaint ();
11125               break;
11126             }
11127
11128           if (op_code >= lh->opcode_base)
11129             {
11130               /* Special operand.  */
11131               adj_opcode = op_code - lh->opcode_base;
11132               address += (((op_index + (adj_opcode / lh->line_range))
11133                            / lh->maximum_ops_per_instruction)
11134                           * lh->minimum_instruction_length);
11135               op_index = ((op_index + (adj_opcode / lh->line_range))
11136                           % lh->maximum_ops_per_instruction);
11137               line += lh->line_base + (adj_opcode % lh->line_range);
11138               if (lh->num_file_names < file || file == 0)
11139                 dwarf2_debug_line_missing_file_complaint ();
11140               /* For now we ignore lines not starting on an
11141                  instruction boundary.  */
11142               else if (op_index == 0)
11143                 {
11144                   lh->file_names[file - 1].included_p = 1;
11145                   if (!decode_for_pst_p && is_stmt)
11146                     {
11147                       if (last_subfile != current_subfile)
11148                         {
11149                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11150                           if (last_subfile)
11151                             (*p_record_line) (last_subfile, 0, addr);
11152                           last_subfile = current_subfile;
11153                         }
11154                       /* Append row to matrix using current values.  */
11155                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11156                       (*p_record_line) (current_subfile, line, addr);
11157                     }
11158                 }
11159               basic_block = 0;
11160             }
11161           else switch (op_code)
11162             {
11163             case DW_LNS_extended_op:
11164               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11165                                                    &bytes_read);
11166               line_ptr += bytes_read;
11167               extended_end = line_ptr + extended_len;
11168               extended_op = read_1_byte (abfd, line_ptr);
11169               line_ptr += 1;
11170               switch (extended_op)
11171                 {
11172                 case DW_LNE_end_sequence:
11173                   p_record_line = record_line;
11174                   end_sequence = 1;
11175                   break;
11176                 case DW_LNE_set_address:
11177                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11178
11179                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11180                     {
11181                       /* This line table is for a function which has been
11182                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11183
11184                       long line_offset
11185                         = line_ptr - dwarf2_per_objfile->line.buffer;
11186
11187                       complaint (&symfile_complaints,
11188                                  _(".debug_line address at offset 0x%lx is 0 "
11189                                    "[in module %s]"),
11190                                  line_offset, objfile->name);
11191                       p_record_line = noop_record_line;
11192                     }
11193
11194                   op_index = 0;
11195                   line_ptr += bytes_read;
11196                   address += baseaddr;
11197                   break;
11198                 case DW_LNE_define_file:
11199                   {
11200                     char *cur_file;
11201                     unsigned int dir_index, mod_time, length;
11202
11203                     cur_file = read_direct_string (abfd, line_ptr,
11204                                                    &bytes_read);
11205                     line_ptr += bytes_read;
11206                     dir_index =
11207                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11208                     line_ptr += bytes_read;
11209                     mod_time =
11210                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11211                     line_ptr += bytes_read;
11212                     length =
11213                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11214                     line_ptr += bytes_read;
11215                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11216                   }
11217                   break;
11218                 case DW_LNE_set_discriminator:
11219                   /* The discriminator is not interesting to the debugger;
11220                      just ignore it.  */
11221                   line_ptr = extended_end;
11222                   break;
11223                 default:
11224                   complaint (&symfile_complaints,
11225                              _("mangled .debug_line section"));
11226                   return;
11227                 }
11228               /* Make sure that we parsed the extended op correctly.  If e.g.
11229                  we expected a different address size than the producer used,
11230                  we may have read the wrong number of bytes.  */
11231               if (line_ptr != extended_end)
11232                 {
11233                   complaint (&symfile_complaints,
11234                              _("mangled .debug_line section"));
11235                   return;
11236                 }
11237               break;
11238             case DW_LNS_copy:
11239               if (lh->num_file_names < file || file == 0)
11240                 dwarf2_debug_line_missing_file_complaint ();
11241               else
11242                 {
11243                   lh->file_names[file - 1].included_p = 1;
11244                   if (!decode_for_pst_p && is_stmt)
11245                     {
11246                       if (last_subfile != current_subfile)
11247                         {
11248                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11249                           if (last_subfile)
11250                             (*p_record_line) (last_subfile, 0, addr);
11251                           last_subfile = current_subfile;
11252                         }
11253                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11254                       (*p_record_line) (current_subfile, line, addr);
11255                     }
11256                 }
11257               basic_block = 0;
11258               break;
11259             case DW_LNS_advance_pc:
11260               {
11261                 CORE_ADDR adjust
11262                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11263
11264                 address += (((op_index + adjust)
11265                              / lh->maximum_ops_per_instruction)
11266                             * lh->minimum_instruction_length);
11267                 op_index = ((op_index + adjust)
11268                             % lh->maximum_ops_per_instruction);
11269                 line_ptr += bytes_read;
11270               }
11271               break;
11272             case DW_LNS_advance_line:
11273               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11274               line_ptr += bytes_read;
11275               break;
11276             case DW_LNS_set_file:
11277               {
11278                 /* The arrays lh->include_dirs and lh->file_names are
11279                    0-based, but the directory and file name numbers in
11280                    the statement program are 1-based.  */
11281                 struct file_entry *fe;
11282                 char *dir = NULL;
11283
11284                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11285                 line_ptr += bytes_read;
11286                 if (lh->num_file_names < file || file == 0)
11287                   dwarf2_debug_line_missing_file_complaint ();
11288                 else
11289                   {
11290                     fe = &lh->file_names[file - 1];
11291                     if (fe->dir_index)
11292                       dir = lh->include_dirs[fe->dir_index - 1];
11293                     if (!decode_for_pst_p)
11294                       {
11295                         last_subfile = current_subfile;
11296                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11297                       }
11298                   }
11299               }
11300               break;
11301             case DW_LNS_set_column:
11302               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11303               line_ptr += bytes_read;
11304               break;
11305             case DW_LNS_negate_stmt:
11306               is_stmt = (!is_stmt);
11307               break;
11308             case DW_LNS_set_basic_block:
11309               basic_block = 1;
11310               break;
11311             /* Add to the address register of the state machine the
11312                address increment value corresponding to special opcode
11313                255.  I.e., this value is scaled by the minimum
11314                instruction length since special opcode 255 would have
11315                scaled the increment.  */
11316             case DW_LNS_const_add_pc:
11317               {
11318                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11319
11320                 address += (((op_index + adjust)
11321                              / lh->maximum_ops_per_instruction)
11322                             * lh->minimum_instruction_length);
11323                 op_index = ((op_index + adjust)
11324                             % lh->maximum_ops_per_instruction);
11325               }
11326               break;
11327             case DW_LNS_fixed_advance_pc:
11328               address += read_2_bytes (abfd, line_ptr);
11329               op_index = 0;
11330               line_ptr += 2;
11331               break;
11332             default:
11333               {
11334                 /* Unknown standard opcode, ignore it.  */
11335                 int i;
11336
11337                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11338                   {
11339                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11340                     line_ptr += bytes_read;
11341                   }
11342               }
11343             }
11344         }
11345       if (lh->num_file_names < file || file == 0)
11346         dwarf2_debug_line_missing_file_complaint ();
11347       else
11348         {
11349           lh->file_names[file - 1].included_p = 1;
11350           if (!decode_for_pst_p)
11351             {
11352               addr = gdbarch_addr_bits_remove (gdbarch, address);
11353               (*p_record_line) (current_subfile, 0, addr);
11354             }
11355         }
11356     }
11357 }
11358
11359 /* Decode the Line Number Program (LNP) for the given line_header
11360    structure and CU.  The actual information extracted and the type
11361    of structures created from the LNP depends on the value of PST.
11362
11363    1. If PST is NULL, then this procedure uses the data from the program
11364       to create all necessary symbol tables, and their linetables.
11365
11366    2. If PST is not NULL, this procedure reads the program to determine
11367       the list of files included by the unit represented by PST, and
11368       builds all the associated partial symbol tables.
11369
11370    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11371    It is used for relative paths in the line table.
11372    NOTE: When processing partial symtabs (pst != NULL),
11373    comp_dir == pst->dirname.
11374
11375    NOTE: It is important that psymtabs have the same file name (via strcmp)
11376    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11377    symtab we don't use it in the name of the psymtabs we create.
11378    E.g. expand_line_sal requires this when finding psymtabs to expand.
11379    A good testcase for this is mb-inline.exp.  */
11380
11381 static void
11382 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11383                     struct dwarf2_cu *cu, struct partial_symtab *pst,
11384                     int want_line_info)
11385 {
11386   struct objfile *objfile = cu->objfile;
11387   const int decode_for_pst_p = (pst != NULL);
11388   struct subfile *first_subfile = current_subfile;
11389
11390   if (want_line_info)
11391     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11392
11393   if (decode_for_pst_p)
11394     {
11395       int file_index;
11396
11397       /* Now that we're done scanning the Line Header Program, we can
11398          create the psymtab of each included file.  */
11399       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11400         if (lh->file_names[file_index].included_p == 1)
11401           {
11402             char *include_name =
11403               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11404             if (include_name != NULL)
11405               dwarf2_create_include_psymtab (include_name, pst, objfile);
11406           }
11407     }
11408   else
11409     {
11410       /* Make sure a symtab is created for every file, even files
11411          which contain only variables (i.e. no code with associated
11412          line numbers).  */
11413       int i;
11414
11415       for (i = 0; i < lh->num_file_names; i++)
11416         {
11417           char *dir = NULL;
11418           struct file_entry *fe;
11419
11420           fe = &lh->file_names[i];
11421           if (fe->dir_index)
11422             dir = lh->include_dirs[fe->dir_index - 1];
11423           dwarf2_start_subfile (fe->name, dir, comp_dir);
11424
11425           /* Skip the main file; we don't need it, and it must be
11426              allocated last, so that it will show up before the
11427              non-primary symtabs in the objfile's symtab list.  */
11428           if (current_subfile == first_subfile)
11429             continue;
11430
11431           if (current_subfile->symtab == NULL)
11432             current_subfile->symtab = allocate_symtab (current_subfile->name,
11433                                                        objfile);
11434           fe->symtab = current_subfile->symtab;
11435         }
11436     }
11437 }
11438
11439 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11440    DIRNAME the name of the source directory which contains FILENAME
11441    or NULL if not known.  COMP_DIR is the compilation directory for the
11442    linetable's compilation unit or NULL if not known.
11443    This routine tries to keep line numbers from identical absolute and
11444    relative file names in a common subfile.
11445
11446    Using the `list' example from the GDB testsuite, which resides in
11447    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11448    of /srcdir/list0.c yields the following debugging information for list0.c:
11449
11450    DW_AT_name:          /srcdir/list0.c
11451    DW_AT_comp_dir:              /compdir
11452    files.files[0].name: list0.h
11453    files.files[0].dir:  /srcdir
11454    files.files[1].name: list0.c
11455    files.files[1].dir:  /srcdir
11456
11457    The line number information for list0.c has to end up in a single
11458    subfile, so that `break /srcdir/list0.c:1' works as expected.
11459    start_subfile will ensure that this happens provided that we pass the
11460    concatenation of files.files[1].dir and files.files[1].name as the
11461    subfile's name.  */
11462
11463 static void
11464 dwarf2_start_subfile (char *filename, const char *dirname,
11465                       const char *comp_dir)
11466 {
11467   char *fullname;
11468
11469   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11470      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11471      second argument to start_subfile.  To be consistent, we do the
11472      same here.  In order not to lose the line information directory,
11473      we concatenate it to the filename when it makes sense.
11474      Note that the Dwarf3 standard says (speaking of filenames in line
11475      information): ``The directory index is ignored for file names
11476      that represent full path names''.  Thus ignoring dirname in the
11477      `else' branch below isn't an issue.  */
11478
11479   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11480     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11481   else
11482     fullname = filename;
11483
11484   start_subfile (fullname, comp_dir);
11485
11486   if (fullname != filename)
11487     xfree (fullname);
11488 }
11489
11490 static void
11491 var_decode_location (struct attribute *attr, struct symbol *sym,
11492                      struct dwarf2_cu *cu)
11493 {
11494   struct objfile *objfile = cu->objfile;
11495   struct comp_unit_head *cu_header = &cu->header;
11496
11497   /* NOTE drow/2003-01-30: There used to be a comment and some special
11498      code here to turn a symbol with DW_AT_external and a
11499      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11500      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11501      with some versions of binutils) where shared libraries could have
11502      relocations against symbols in their debug information - the
11503      minimal symbol would have the right address, but the debug info
11504      would not.  It's no longer necessary, because we will explicitly
11505      apply relocations when we read in the debug information now.  */
11506
11507   /* A DW_AT_location attribute with no contents indicates that a
11508      variable has been optimized away.  */
11509   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11510     {
11511       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11512       return;
11513     }
11514
11515   /* Handle one degenerate form of location expression specially, to
11516      preserve GDB's previous behavior when section offsets are
11517      specified.  If this is just a DW_OP_addr then mark this symbol
11518      as LOC_STATIC.  */
11519
11520   if (attr_form_is_block (attr)
11521       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11522       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11523     {
11524       unsigned int dummy;
11525
11526       SYMBOL_VALUE_ADDRESS (sym) =
11527         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11528       SYMBOL_CLASS (sym) = LOC_STATIC;
11529       fixup_symbol_section (sym, objfile);
11530       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11531                                               SYMBOL_SECTION (sym));
11532       return;
11533     }
11534
11535   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11536      expression evaluator, and use LOC_COMPUTED only when necessary
11537      (i.e. when the value of a register or memory location is
11538      referenced, or a thread-local block, etc.).  Then again, it might
11539      not be worthwhile.  I'm assuming that it isn't unless performance
11540      or memory numbers show me otherwise.  */
11541
11542   dwarf2_symbol_mark_computed (attr, sym, cu);
11543   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11544
11545   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11546     cu->has_loclist = 1;
11547 }
11548
11549 /* Given a pointer to a DWARF information entry, figure out if we need
11550    to make a symbol table entry for it, and if so, create a new entry
11551    and return a pointer to it.
11552    If TYPE is NULL, determine symbol type from the die, otherwise
11553    used the passed type.
11554    If SPACE is not NULL, use it to hold the new symbol.  If it is
11555    NULL, allocate a new symbol on the objfile's obstack.  */
11556
11557 static struct symbol *
11558 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11559                  struct symbol *space)
11560 {
11561   struct objfile *objfile = cu->objfile;
11562   struct symbol *sym = NULL;
11563   char *name;
11564   struct attribute *attr = NULL;
11565   struct attribute *attr2 = NULL;
11566   CORE_ADDR baseaddr;
11567   struct pending **list_to_add = NULL;
11568
11569   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11570
11571   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11572
11573   name = dwarf2_name (die, cu);
11574   if (name)
11575     {
11576       const char *linkagename;
11577       int suppress_add = 0;
11578
11579       if (space)
11580         sym = space;
11581       else
11582         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11583       OBJSTAT (objfile, n_syms++);
11584
11585       /* Cache this symbol's name and the name's demangled form (if any).  */
11586       SYMBOL_SET_LANGUAGE (sym, cu->language);
11587       linkagename = dwarf2_physname (name, die, cu);
11588       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11589
11590       /* Fortran does not have mangling standard and the mangling does differ
11591          between gfortran, iFort etc.  */
11592       if (cu->language == language_fortran
11593           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11594         symbol_set_demangled_name (&(sym->ginfo),
11595                                    (char *) dwarf2_full_name (name, die, cu),
11596                                    NULL);
11597
11598       /* Default assumptions.
11599          Use the passed type or decode it from the die.  */
11600       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11601       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11602       if (type != NULL)
11603         SYMBOL_TYPE (sym) = type;
11604       else
11605         SYMBOL_TYPE (sym) = die_type (die, cu);
11606       attr = dwarf2_attr (die,
11607                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11608                           cu);
11609       if (attr)
11610         {
11611           SYMBOL_LINE (sym) = DW_UNSND (attr);
11612         }
11613
11614       attr = dwarf2_attr (die,
11615                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11616                           cu);
11617       if (attr)
11618         {
11619           int file_index = DW_UNSND (attr);
11620
11621           if (cu->line_header == NULL
11622               || file_index > cu->line_header->num_file_names)
11623             complaint (&symfile_complaints,
11624                        _("file index out of range"));
11625           else if (file_index > 0)
11626             {
11627               struct file_entry *fe;
11628
11629               fe = &cu->line_header->file_names[file_index - 1];
11630               SYMBOL_SYMTAB (sym) = fe->symtab;
11631             }
11632         }
11633
11634       switch (die->tag)
11635         {
11636         case DW_TAG_label:
11637           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11638           if (attr)
11639             {
11640               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11641             }
11642           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11643           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11644           SYMBOL_CLASS (sym) = LOC_LABEL;
11645           add_symbol_to_list (sym, cu->list_in_scope);
11646           break;
11647         case DW_TAG_subprogram:
11648           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11649              finish_block.  */
11650           SYMBOL_CLASS (sym) = LOC_BLOCK;
11651           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11652           if ((attr2 && (DW_UNSND (attr2) != 0))
11653               || cu->language == language_ada)
11654             {
11655               /* Subprograms marked external are stored as a global symbol.
11656                  Ada subprograms, whether marked external or not, are always
11657                  stored as a global symbol, because we want to be able to
11658                  access them globally.  For instance, we want to be able
11659                  to break on a nested subprogram without having to
11660                  specify the context.  */
11661               list_to_add = &global_symbols;
11662             }
11663           else
11664             {
11665               list_to_add = cu->list_in_scope;
11666             }
11667           break;
11668         case DW_TAG_inlined_subroutine:
11669           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11670              finish_block.  */
11671           SYMBOL_CLASS (sym) = LOC_BLOCK;
11672           SYMBOL_INLINED (sym) = 1;
11673           /* Do not add the symbol to any lists.  It will be found via
11674              BLOCK_FUNCTION from the blockvector.  */
11675           break;
11676         case DW_TAG_template_value_param:
11677           suppress_add = 1;
11678           /* Fall through.  */
11679         case DW_TAG_constant:
11680         case DW_TAG_variable:
11681         case DW_TAG_member:
11682           /* Compilation with minimal debug info may result in
11683              variables with missing type entries.  Change the
11684              misleading `void' type to something sensible.  */
11685           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11686             SYMBOL_TYPE (sym)
11687               = objfile_type (objfile)->nodebug_data_symbol;
11688
11689           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11690           /* In the case of DW_TAG_member, we should only be called for
11691              static const members.  */
11692           if (die->tag == DW_TAG_member)
11693             {
11694               /* dwarf2_add_field uses die_is_declaration,
11695                  so we do the same.  */
11696               gdb_assert (die_is_declaration (die, cu));
11697               gdb_assert (attr);
11698             }
11699           if (attr)
11700             {
11701               dwarf2_const_value (attr, sym, cu);
11702               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11703               if (!suppress_add)
11704                 {
11705                   if (attr2 && (DW_UNSND (attr2) != 0))
11706                     list_to_add = &global_symbols;
11707                   else
11708                     list_to_add = cu->list_in_scope;
11709                 }
11710               break;
11711             }
11712           attr = dwarf2_attr (die, DW_AT_location, cu);
11713           if (attr)
11714             {
11715               var_decode_location (attr, sym, cu);
11716               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11717               if (SYMBOL_CLASS (sym) == LOC_STATIC
11718                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11719                   && !dwarf2_per_objfile->has_section_at_zero)
11720                 {
11721                   /* When a static variable is eliminated by the linker,
11722                      the corresponding debug information is not stripped
11723                      out, but the variable address is set to null;
11724                      do not add such variables into symbol table.  */
11725                 }
11726               else if (attr2 && (DW_UNSND (attr2) != 0))
11727                 {
11728                   /* Workaround gfortran PR debug/40040 - it uses
11729                      DW_AT_location for variables in -fPIC libraries which may
11730                      get overriden by other libraries/executable and get
11731                      a different address.  Resolve it by the minimal symbol
11732                      which may come from inferior's executable using copy
11733                      relocation.  Make this workaround only for gfortran as for
11734                      other compilers GDB cannot guess the minimal symbol
11735                      Fortran mangling kind.  */
11736                   if (cu->language == language_fortran && die->parent
11737                       && die->parent->tag == DW_TAG_module
11738                       && cu->producer
11739                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11740                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11741
11742                   /* A variable with DW_AT_external is never static,
11743                      but it may be block-scoped.  */
11744                   list_to_add = (cu->list_in_scope == &file_symbols
11745                                  ? &global_symbols : cu->list_in_scope);
11746                 }
11747               else
11748                 list_to_add = cu->list_in_scope;
11749             }
11750           else
11751             {
11752               /* We do not know the address of this symbol.
11753                  If it is an external symbol and we have type information
11754                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11755                  The address of the variable will then be determined from
11756                  the minimal symbol table whenever the variable is
11757                  referenced.  */
11758               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11759               if (attr2 && (DW_UNSND (attr2) != 0)
11760                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11761                 {
11762                   /* A variable with DW_AT_external is never static, but it
11763                      may be block-scoped.  */
11764                   list_to_add = (cu->list_in_scope == &file_symbols
11765                                  ? &global_symbols : cu->list_in_scope);
11766
11767                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11768                 }
11769               else if (!die_is_declaration (die, cu))
11770                 {
11771                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11772                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11773                   if (!suppress_add)
11774                     list_to_add = cu->list_in_scope;
11775                 }
11776             }
11777           break;
11778         case DW_TAG_formal_parameter:
11779           /* If we are inside a function, mark this as an argument.  If
11780              not, we might be looking at an argument to an inlined function
11781              when we do not have enough information to show inlined frames;
11782              pretend it's a local variable in that case so that the user can
11783              still see it.  */
11784           if (context_stack_depth > 0
11785               && context_stack[context_stack_depth - 1].name != NULL)
11786             SYMBOL_IS_ARGUMENT (sym) = 1;
11787           attr = dwarf2_attr (die, DW_AT_location, cu);
11788           if (attr)
11789             {
11790               var_decode_location (attr, sym, cu);
11791             }
11792           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11793           if (attr)
11794             {
11795               dwarf2_const_value (attr, sym, cu);
11796             }
11797
11798           list_to_add = cu->list_in_scope;
11799           break;
11800         case DW_TAG_unspecified_parameters:
11801           /* From varargs functions; gdb doesn't seem to have any
11802              interest in this information, so just ignore it for now.
11803              (FIXME?) */
11804           break;
11805         case DW_TAG_template_type_param:
11806           suppress_add = 1;
11807           /* Fall through.  */
11808         case DW_TAG_class_type:
11809         case DW_TAG_interface_type:
11810         case DW_TAG_structure_type:
11811         case DW_TAG_union_type:
11812         case DW_TAG_set_type:
11813         case DW_TAG_enumeration_type:
11814           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11815           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11816
11817           {
11818             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11819                really ever be static objects: otherwise, if you try
11820                to, say, break of a class's method and you're in a file
11821                which doesn't mention that class, it won't work unless
11822                the check for all static symbols in lookup_symbol_aux
11823                saves you.  See the OtherFileClass tests in
11824                gdb.c++/namespace.exp.  */
11825
11826             if (!suppress_add)
11827               {
11828                 list_to_add = (cu->list_in_scope == &file_symbols
11829                                && (cu->language == language_cplus
11830                                    || cu->language == language_java)
11831                                ? &global_symbols : cu->list_in_scope);
11832
11833                 /* The semantics of C++ state that "struct foo {
11834                    ... }" also defines a typedef for "foo".  A Java
11835                    class declaration also defines a typedef for the
11836                    class.  */
11837                 if (cu->language == language_cplus
11838                     || cu->language == language_java
11839                     || cu->language == language_ada)
11840                   {
11841                     /* The symbol's name is already allocated along
11842                        with this objfile, so we don't need to
11843                        duplicate it for the type.  */
11844                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11845                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11846                   }
11847               }
11848           }
11849           break;
11850         case DW_TAG_typedef:
11851           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11852           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11853           list_to_add = cu->list_in_scope;
11854           break;
11855         case DW_TAG_base_type:
11856         case DW_TAG_subrange_type:
11857           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11858           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11859           list_to_add = cu->list_in_scope;
11860           break;
11861         case DW_TAG_enumerator:
11862           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11863           if (attr)
11864             {
11865               dwarf2_const_value (attr, sym, cu);
11866             }
11867           {
11868             /* NOTE: carlton/2003-11-10: See comment above in the
11869                DW_TAG_class_type, etc. block.  */
11870
11871             list_to_add = (cu->list_in_scope == &file_symbols
11872                            && (cu->language == language_cplus
11873                                || cu->language == language_java)
11874                            ? &global_symbols : cu->list_in_scope);
11875           }
11876           break;
11877         case DW_TAG_namespace:
11878           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11879           list_to_add = &global_symbols;
11880           break;
11881         default:
11882           /* Not a tag we recognize.  Hopefully we aren't processing
11883              trash data, but since we must specifically ignore things
11884              we don't recognize, there is nothing else we should do at
11885              this point.  */
11886           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11887                      dwarf_tag_name (die->tag));
11888           break;
11889         }
11890
11891       if (suppress_add)
11892         {
11893           sym->hash_next = objfile->template_symbols;
11894           objfile->template_symbols = sym;
11895           list_to_add = NULL;
11896         }
11897
11898       if (list_to_add != NULL)
11899         add_symbol_to_list (sym, list_to_add);
11900
11901       /* For the benefit of old versions of GCC, check for anonymous
11902          namespaces based on the demangled name.  */
11903       if (!processing_has_namespace_info
11904           && cu->language == language_cplus)
11905         cp_scan_for_anonymous_namespaces (sym, objfile);
11906     }
11907   return (sym);
11908 }
11909
11910 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11911
11912 static struct symbol *
11913 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11914 {
11915   return new_symbol_full (die, type, cu, NULL);
11916 }
11917
11918 /* Given an attr with a DW_FORM_dataN value in host byte order,
11919    zero-extend it as appropriate for the symbol's type.  The DWARF
11920    standard (v4) is not entirely clear about the meaning of using
11921    DW_FORM_dataN for a constant with a signed type, where the type is
11922    wider than the data.  The conclusion of a discussion on the DWARF
11923    list was that this is unspecified.  We choose to always zero-extend
11924    because that is the interpretation long in use by GCC.  */
11925
11926 static gdb_byte *
11927 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11928                          const char *name, struct obstack *obstack,
11929                          struct dwarf2_cu *cu, long *value, int bits)
11930 {
11931   struct objfile *objfile = cu->objfile;
11932   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11933                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11934   LONGEST l = DW_UNSND (attr);
11935
11936   if (bits < sizeof (*value) * 8)
11937     {
11938       l &= ((LONGEST) 1 << bits) - 1;
11939       *value = l;
11940     }
11941   else if (bits == sizeof (*value) * 8)
11942     *value = l;
11943   else
11944     {
11945       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11946       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11947       return bytes;
11948     }
11949
11950   return NULL;
11951 }
11952
11953 /* Read a constant value from an attribute.  Either set *VALUE, or if
11954    the value does not fit in *VALUE, set *BYTES - either already
11955    allocated on the objfile obstack, or newly allocated on OBSTACK,
11956    or, set *BATON, if we translated the constant to a location
11957    expression.  */
11958
11959 static void
11960 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11961                          const char *name, struct obstack *obstack,
11962                          struct dwarf2_cu *cu,
11963                          long *value, gdb_byte **bytes,
11964                          struct dwarf2_locexpr_baton **baton)
11965 {
11966   struct objfile *objfile = cu->objfile;
11967   struct comp_unit_head *cu_header = &cu->header;
11968   struct dwarf_block *blk;
11969   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11970                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11971
11972   *value = 0;
11973   *bytes = NULL;
11974   *baton = NULL;
11975
11976   switch (attr->form)
11977     {
11978     case DW_FORM_addr:
11979       {
11980         gdb_byte *data;
11981
11982         if (TYPE_LENGTH (type) != cu_header->addr_size)
11983           dwarf2_const_value_length_mismatch_complaint (name,
11984                                                         cu_header->addr_size,
11985                                                         TYPE_LENGTH (type));
11986         /* Symbols of this form are reasonably rare, so we just
11987            piggyback on the existing location code rather than writing
11988            a new implementation of symbol_computed_ops.  */
11989         *baton = obstack_alloc (&objfile->objfile_obstack,
11990                                 sizeof (struct dwarf2_locexpr_baton));
11991         (*baton)->per_cu = cu->per_cu;
11992         gdb_assert ((*baton)->per_cu);
11993
11994         (*baton)->size = 2 + cu_header->addr_size;
11995         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11996         (*baton)->data = data;
11997
11998         data[0] = DW_OP_addr;
11999         store_unsigned_integer (&data[1], cu_header->addr_size,
12000                                 byte_order, DW_ADDR (attr));
12001         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12002       }
12003       break;
12004     case DW_FORM_string:
12005     case DW_FORM_strp:
12006       /* DW_STRING is already allocated on the objfile obstack, point
12007          directly to it.  */
12008       *bytes = (gdb_byte *) DW_STRING (attr);
12009       break;
12010     case DW_FORM_block1:
12011     case DW_FORM_block2:
12012     case DW_FORM_block4:
12013     case DW_FORM_block:
12014     case DW_FORM_exprloc:
12015       blk = DW_BLOCK (attr);
12016       if (TYPE_LENGTH (type) != blk->size)
12017         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12018                                                       TYPE_LENGTH (type));
12019       *bytes = blk->data;
12020       break;
12021
12022       /* The DW_AT_const_value attributes are supposed to carry the
12023          symbol's value "represented as it would be on the target
12024          architecture."  By the time we get here, it's already been
12025          converted to host endianness, so we just need to sign- or
12026          zero-extend it as appropriate.  */
12027     case DW_FORM_data1:
12028       *bytes = dwarf2_const_value_data (attr, type, name,
12029                                         obstack, cu, value, 8);
12030       break;
12031     case DW_FORM_data2:
12032       *bytes = dwarf2_const_value_data (attr, type, name,
12033                                         obstack, cu, value, 16);
12034       break;
12035     case DW_FORM_data4:
12036       *bytes = dwarf2_const_value_data (attr, type, name,
12037                                         obstack, cu, value, 32);
12038       break;
12039     case DW_FORM_data8:
12040       *bytes = dwarf2_const_value_data (attr, type, name,
12041                                         obstack, cu, value, 64);
12042       break;
12043
12044     case DW_FORM_sdata:
12045       *value = DW_SND (attr);
12046       break;
12047
12048     case DW_FORM_udata:
12049       *value = DW_UNSND (attr);
12050       break;
12051
12052     default:
12053       complaint (&symfile_complaints,
12054                  _("unsupported const value attribute form: '%s'"),
12055                  dwarf_form_name (attr->form));
12056       *value = 0;
12057       break;
12058     }
12059 }
12060
12061
12062 /* Copy constant value from an attribute to a symbol.  */
12063
12064 static void
12065 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12066                     struct dwarf2_cu *cu)
12067 {
12068   struct objfile *objfile = cu->objfile;
12069   struct comp_unit_head *cu_header = &cu->header;
12070   long value;
12071   gdb_byte *bytes;
12072   struct dwarf2_locexpr_baton *baton;
12073
12074   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12075                            SYMBOL_PRINT_NAME (sym),
12076                            &objfile->objfile_obstack, cu,
12077                            &value, &bytes, &baton);
12078
12079   if (baton != NULL)
12080     {
12081       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12082       SYMBOL_LOCATION_BATON (sym) = baton;
12083       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12084     }
12085   else if (bytes != NULL)
12086      {
12087       SYMBOL_VALUE_BYTES (sym) = bytes;
12088       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12089     }
12090   else
12091     {
12092       SYMBOL_VALUE (sym) = value;
12093       SYMBOL_CLASS (sym) = LOC_CONST;
12094     }
12095 }
12096
12097 /* Return the type of the die in question using its DW_AT_type attribute.  */
12098
12099 static struct type *
12100 die_type (struct die_info *die, struct dwarf2_cu *cu)
12101 {
12102   struct attribute *type_attr;
12103
12104   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12105   if (!type_attr)
12106     {
12107       /* A missing DW_AT_type represents a void type.  */
12108       return objfile_type (cu->objfile)->builtin_void;
12109     }
12110
12111   return lookup_die_type (die, type_attr, cu);
12112 }
12113
12114 /* True iff CU's producer generates GNAT Ada auxiliary information
12115    that allows to find parallel types through that information instead
12116    of having to do expensive parallel lookups by type name.  */
12117
12118 static int
12119 need_gnat_info (struct dwarf2_cu *cu)
12120 {
12121   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12122      of GNAT produces this auxiliary information, without any indication
12123      that it is produced.  Part of enhancing the FSF version of GNAT
12124      to produce that information will be to put in place an indicator
12125      that we can use in order to determine whether the descriptive type
12126      info is available or not.  One suggestion that has been made is
12127      to use a new attribute, attached to the CU die.  For now, assume
12128      that the descriptive type info is not available.  */
12129   return 0;
12130 }
12131
12132 /* Return the auxiliary type of the die in question using its
12133    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12134    attribute is not present.  */
12135
12136 static struct type *
12137 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12138 {
12139   struct attribute *type_attr;
12140
12141   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12142   if (!type_attr)
12143     return NULL;
12144
12145   return lookup_die_type (die, type_attr, cu);
12146 }
12147
12148 /* If DIE has a descriptive_type attribute, then set the TYPE's
12149    descriptive type accordingly.  */
12150
12151 static void
12152 set_descriptive_type (struct type *type, struct die_info *die,
12153                       struct dwarf2_cu *cu)
12154 {
12155   struct type *descriptive_type = die_descriptive_type (die, cu);
12156
12157   if (descriptive_type)
12158     {
12159       ALLOCATE_GNAT_AUX_TYPE (type);
12160       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12161     }
12162 }
12163
12164 /* Return the containing type of the die in question using its
12165    DW_AT_containing_type attribute.  */
12166
12167 static struct type *
12168 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12169 {
12170   struct attribute *type_attr;
12171
12172   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12173   if (!type_attr)
12174     error (_("Dwarf Error: Problem turning containing type into gdb type "
12175              "[in module %s]"), cu->objfile->name);
12176
12177   return lookup_die_type (die, type_attr, cu);
12178 }
12179
12180 /* Look up the type of DIE in CU using its type attribute ATTR.
12181    If there is no type substitute an error marker.  */
12182
12183 static struct type *
12184 lookup_die_type (struct die_info *die, struct attribute *attr,
12185                  struct dwarf2_cu *cu)
12186 {
12187   struct objfile *objfile = cu->objfile;
12188   struct type *this_type;
12189
12190   /* First see if we have it cached.  */
12191
12192   if (is_ref_attr (attr))
12193     {
12194       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12195
12196       this_type = get_die_type_at_offset (offset, cu->per_cu);
12197     }
12198   else if (attr->form == DW_FORM_ref_sig8)
12199     {
12200       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12201       struct dwarf2_cu *sig_cu;
12202       unsigned int offset;
12203
12204       /* sig_type will be NULL if the signatured type is missing from
12205          the debug info.  */
12206       if (sig_type == NULL)
12207         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12208                  "at 0x%x [in module %s]"),
12209                die->offset, objfile->name);
12210
12211       gdb_assert (sig_type->per_cu.debug_types_section);
12212       offset = sig_type->per_cu.offset + sig_type->type_offset;
12213       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12214     }
12215   else
12216     {
12217       dump_die_for_error (die);
12218       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12219              dwarf_attr_name (attr->name), objfile->name);
12220     }
12221
12222   /* If not cached we need to read it in.  */
12223
12224   if (this_type == NULL)
12225     {
12226       struct die_info *type_die;
12227       struct dwarf2_cu *type_cu = cu;
12228
12229       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12230       /* If the type is cached, we should have found it above.  */
12231       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12232       this_type = read_type_die_1 (type_die, type_cu);
12233     }
12234
12235   /* If we still don't have a type use an error marker.  */
12236
12237   if (this_type == NULL)
12238     {
12239       char *message, *saved;
12240
12241       /* read_type_die already issued a complaint.  */
12242       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12243                             objfile->name,
12244                             cu->header.offset,
12245                             die->offset);
12246       saved = obstack_copy0 (&objfile->objfile_obstack,
12247                              message, strlen (message));
12248       xfree (message);
12249
12250       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12251     }
12252
12253   return this_type;
12254 }
12255
12256 /* Return the type in DIE, CU.
12257    Returns NULL for invalid types.
12258
12259    This first does a lookup in the appropriate type_hash table,
12260    and only reads the die in if necessary.
12261
12262    NOTE: This can be called when reading in partial or full symbols.  */
12263
12264 static struct type *
12265 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12266 {
12267   struct type *this_type;
12268
12269   this_type = get_die_type (die, cu);
12270   if (this_type)
12271     return this_type;
12272
12273   return read_type_die_1 (die, cu);
12274 }
12275
12276 /* Read the type in DIE, CU.
12277    Returns NULL for invalid types.  */
12278
12279 static struct type *
12280 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12281 {
12282   struct type *this_type = NULL;
12283
12284   switch (die->tag)
12285     {
12286     case DW_TAG_class_type:
12287     case DW_TAG_interface_type:
12288     case DW_TAG_structure_type:
12289     case DW_TAG_union_type:
12290       this_type = read_structure_type (die, cu);
12291       break;
12292     case DW_TAG_enumeration_type:
12293       this_type = read_enumeration_type (die, cu);
12294       break;
12295     case DW_TAG_subprogram:
12296     case DW_TAG_subroutine_type:
12297     case DW_TAG_inlined_subroutine:
12298       this_type = read_subroutine_type (die, cu);
12299       break;
12300     case DW_TAG_array_type:
12301       this_type = read_array_type (die, cu);
12302       break;
12303     case DW_TAG_set_type:
12304       this_type = read_set_type (die, cu);
12305       break;
12306     case DW_TAG_pointer_type:
12307       this_type = read_tag_pointer_type (die, cu);
12308       break;
12309     case DW_TAG_ptr_to_member_type:
12310       this_type = read_tag_ptr_to_member_type (die, cu);
12311       break;
12312     case DW_TAG_reference_type:
12313       this_type = read_tag_reference_type (die, cu);
12314       break;
12315     case DW_TAG_const_type:
12316       this_type = read_tag_const_type (die, cu);
12317       break;
12318     case DW_TAG_volatile_type:
12319       this_type = read_tag_volatile_type (die, cu);
12320       break;
12321     case DW_TAG_string_type:
12322       this_type = read_tag_string_type (die, cu);
12323       break;
12324     case DW_TAG_typedef:
12325       this_type = read_typedef (die, cu);
12326       break;
12327     case DW_TAG_subrange_type:
12328       this_type = read_subrange_type (die, cu);
12329       break;
12330     case DW_TAG_base_type:
12331       this_type = read_base_type (die, cu);
12332       break;
12333     case DW_TAG_unspecified_type:
12334       this_type = read_unspecified_type (die, cu);
12335       break;
12336     case DW_TAG_namespace:
12337       this_type = read_namespace_type (die, cu);
12338       break;
12339     case DW_TAG_module:
12340       this_type = read_module_type (die, cu);
12341       break;
12342     default:
12343       complaint (&symfile_complaints,
12344                  _("unexpected tag in read_type_die: '%s'"),
12345                  dwarf_tag_name (die->tag));
12346       break;
12347     }
12348
12349   return this_type;
12350 }
12351
12352 /* See if we can figure out if the class lives in a namespace.  We do
12353    this by looking for a member function; its demangled name will
12354    contain namespace info, if there is any.
12355    Return the computed name or NULL.
12356    Space for the result is allocated on the objfile's obstack.
12357    This is the full-die version of guess_partial_die_structure_name.
12358    In this case we know DIE has no useful parent.  */
12359
12360 static char *
12361 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12362 {
12363   struct die_info *spec_die;
12364   struct dwarf2_cu *spec_cu;
12365   struct die_info *child;
12366
12367   spec_cu = cu;
12368   spec_die = die_specification (die, &spec_cu);
12369   if (spec_die != NULL)
12370     {
12371       die = spec_die;
12372       cu = spec_cu;
12373     }
12374
12375   for (child = die->child;
12376        child != NULL;
12377        child = child->sibling)
12378     {
12379       if (child->tag == DW_TAG_subprogram)
12380         {
12381           struct attribute *attr;
12382
12383           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12384           if (attr == NULL)
12385             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12386           if (attr != NULL)
12387             {
12388               char *actual_name
12389                 = language_class_name_from_physname (cu->language_defn,
12390                                                      DW_STRING (attr));
12391               char *name = NULL;
12392
12393               if (actual_name != NULL)
12394                 {
12395                   char *die_name = dwarf2_name (die, cu);
12396
12397                   if (die_name != NULL
12398                       && strcmp (die_name, actual_name) != 0)
12399                     {
12400                       /* Strip off the class name from the full name.
12401                          We want the prefix.  */
12402                       int die_name_len = strlen (die_name);
12403                       int actual_name_len = strlen (actual_name);
12404
12405                       /* Test for '::' as a sanity check.  */
12406                       if (actual_name_len > die_name_len + 2
12407                           && actual_name[actual_name_len
12408                                          - die_name_len - 1] == ':')
12409                         name =
12410                           obsavestring (actual_name,
12411                                         actual_name_len - die_name_len - 2,
12412                                         &cu->objfile->objfile_obstack);
12413                     }
12414                 }
12415               xfree (actual_name);
12416               return name;
12417             }
12418         }
12419     }
12420
12421   return NULL;
12422 }
12423
12424 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12425    prefix part in such case.  See
12426    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12427
12428 static char *
12429 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12430 {
12431   struct attribute *attr;
12432   char *base;
12433
12434   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12435       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12436     return NULL;
12437
12438   attr = dwarf2_attr (die, DW_AT_name, cu);
12439   if (attr != NULL && DW_STRING (attr) != NULL)
12440     return NULL;
12441
12442   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12443   if (attr == NULL)
12444     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12445   if (attr == NULL || DW_STRING (attr) == NULL)
12446     return NULL;
12447
12448   /* dwarf2_name had to be already called.  */
12449   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12450
12451   /* Strip the base name, keep any leading namespaces/classes.  */
12452   base = strrchr (DW_STRING (attr), ':');
12453   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12454     return "";
12455
12456   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12457                        &cu->objfile->objfile_obstack);
12458 }
12459
12460 /* Return the name of the namespace/class that DIE is defined within,
12461    or "" if we can't tell.  The caller should not xfree the result.
12462
12463    For example, if we're within the method foo() in the following
12464    code:
12465
12466    namespace N {
12467      class C {
12468        void foo () {
12469        }
12470      };
12471    }
12472
12473    then determine_prefix on foo's die will return "N::C".  */
12474
12475 static const char *
12476 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12477 {
12478   struct die_info *parent, *spec_die;
12479   struct dwarf2_cu *spec_cu;
12480   struct type *parent_type;
12481   char *retval;
12482
12483   if (cu->language != language_cplus && cu->language != language_java
12484       && cu->language != language_fortran)
12485     return "";
12486
12487   retval = anonymous_struct_prefix (die, cu);
12488   if (retval)
12489     return retval;
12490
12491   /* We have to be careful in the presence of DW_AT_specification.
12492      For example, with GCC 3.4, given the code
12493
12494      namespace N {
12495        void foo() {
12496          // Definition of N::foo.
12497        }
12498      }
12499
12500      then we'll have a tree of DIEs like this:
12501
12502      1: DW_TAG_compile_unit
12503        2: DW_TAG_namespace        // N
12504          3: DW_TAG_subprogram     // declaration of N::foo
12505        4: DW_TAG_subprogram       // definition of N::foo
12506             DW_AT_specification   // refers to die #3
12507
12508      Thus, when processing die #4, we have to pretend that we're in
12509      the context of its DW_AT_specification, namely the contex of die
12510      #3.  */
12511   spec_cu = cu;
12512   spec_die = die_specification (die, &spec_cu);
12513   if (spec_die == NULL)
12514     parent = die->parent;
12515   else
12516     {
12517       parent = spec_die->parent;
12518       cu = spec_cu;
12519     }
12520
12521   if (parent == NULL)
12522     return "";
12523   else if (parent->building_fullname)
12524     {
12525       const char *name;
12526       const char *parent_name;
12527
12528       /* It has been seen on RealView 2.2 built binaries,
12529          DW_TAG_template_type_param types actually _defined_ as
12530          children of the parent class:
12531
12532          enum E {};
12533          template class <class Enum> Class{};
12534          Class<enum E> class_e;
12535
12536          1: DW_TAG_class_type (Class)
12537            2: DW_TAG_enumeration_type (E)
12538              3: DW_TAG_enumerator (enum1:0)
12539              3: DW_TAG_enumerator (enum2:1)
12540              ...
12541            2: DW_TAG_template_type_param
12542               DW_AT_type  DW_FORM_ref_udata (E)
12543
12544          Besides being broken debug info, it can put GDB into an
12545          infinite loop.  Consider:
12546
12547          When we're building the full name for Class<E>, we'll start
12548          at Class, and go look over its template type parameters,
12549          finding E.  We'll then try to build the full name of E, and
12550          reach here.  We're now trying to build the full name of E,
12551          and look over the parent DIE for containing scope.  In the
12552          broken case, if we followed the parent DIE of E, we'd again
12553          find Class, and once again go look at its template type
12554          arguments, etc., etc.  Simply don't consider such parent die
12555          as source-level parent of this die (it can't be, the language
12556          doesn't allow it), and break the loop here.  */
12557       name = dwarf2_name (die, cu);
12558       parent_name = dwarf2_name (parent, cu);
12559       complaint (&symfile_complaints,
12560                  _("template param type '%s' defined within parent '%s'"),
12561                  name ? name : "<unknown>",
12562                  parent_name ? parent_name : "<unknown>");
12563       return "";
12564     }
12565   else
12566     switch (parent->tag)
12567       {
12568       case DW_TAG_namespace:
12569         parent_type = read_type_die (parent, cu);
12570         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12571            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12572            Work around this problem here.  */
12573         if (cu->language == language_cplus
12574             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12575           return "";
12576         /* We give a name to even anonymous namespaces.  */
12577         return TYPE_TAG_NAME (parent_type);
12578       case DW_TAG_class_type:
12579       case DW_TAG_interface_type:
12580       case DW_TAG_structure_type:
12581       case DW_TAG_union_type:
12582       case DW_TAG_module:
12583         parent_type = read_type_die (parent, cu);
12584         if (TYPE_TAG_NAME (parent_type) != NULL)
12585           return TYPE_TAG_NAME (parent_type);
12586         else
12587           /* An anonymous structure is only allowed non-static data
12588              members; no typedefs, no member functions, et cetera.
12589              So it does not need a prefix.  */
12590           return "";
12591       case DW_TAG_compile_unit:
12592         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12593         if (cu->language == language_cplus
12594             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12595             && die->child != NULL
12596             && (die->tag == DW_TAG_class_type
12597                 || die->tag == DW_TAG_structure_type
12598                 || die->tag == DW_TAG_union_type))
12599           {
12600             char *name = guess_full_die_structure_name (die, cu);
12601             if (name != NULL)
12602               return name;
12603           }
12604         return "";
12605       default:
12606         return determine_prefix (parent, cu);
12607       }
12608 }
12609
12610 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12611    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12612    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12613    an obconcat, otherwise allocate storage for the result.  The CU argument is
12614    used to determine the language and hence, the appropriate separator.  */
12615
12616 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12617
12618 static char *
12619 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12620                  int physname, struct dwarf2_cu *cu)
12621 {
12622   const char *lead = "";
12623   const char *sep;
12624
12625   if (suffix == NULL || suffix[0] == '\0'
12626       || prefix == NULL || prefix[0] == '\0')
12627     sep = "";
12628   else if (cu->language == language_java)
12629     sep = ".";
12630   else if (cu->language == language_fortran && physname)
12631     {
12632       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12633          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12634
12635       lead = "__";
12636       sep = "_MOD_";
12637     }
12638   else
12639     sep = "::";
12640
12641   if (prefix == NULL)
12642     prefix = "";
12643   if (suffix == NULL)
12644     suffix = "";
12645
12646   if (obs == NULL)
12647     {
12648       char *retval
12649         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12650
12651       strcpy (retval, lead);
12652       strcat (retval, prefix);
12653       strcat (retval, sep);
12654       strcat (retval, suffix);
12655       return retval;
12656     }
12657   else
12658     {
12659       /* We have an obstack.  */
12660       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12661     }
12662 }
12663
12664 /* Return sibling of die, NULL if no sibling.  */
12665
12666 static struct die_info *
12667 sibling_die (struct die_info *die)
12668 {
12669   return die->sibling;
12670 }
12671
12672 /* Get name of a die, return NULL if not found.  */
12673
12674 static char *
12675 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12676                           struct obstack *obstack)
12677 {
12678   if (name && cu->language == language_cplus)
12679     {
12680       char *canon_name = cp_canonicalize_string (name);
12681
12682       if (canon_name != NULL)
12683         {
12684           if (strcmp (canon_name, name) != 0)
12685             name = obsavestring (canon_name, strlen (canon_name),
12686                                  obstack);
12687           xfree (canon_name);
12688         }
12689     }
12690
12691   return name;
12692 }
12693
12694 /* Get name of a die, return NULL if not found.  */
12695
12696 static char *
12697 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12698 {
12699   struct attribute *attr;
12700
12701   attr = dwarf2_attr (die, DW_AT_name, cu);
12702   if ((!attr || !DW_STRING (attr))
12703       && die->tag != DW_TAG_class_type
12704       && die->tag != DW_TAG_interface_type
12705       && die->tag != DW_TAG_structure_type
12706       && die->tag != DW_TAG_union_type)
12707     return NULL;
12708
12709   switch (die->tag)
12710     {
12711     case DW_TAG_compile_unit:
12712       /* Compilation units have a DW_AT_name that is a filename, not
12713          a source language identifier.  */
12714     case DW_TAG_enumeration_type:
12715     case DW_TAG_enumerator:
12716       /* These tags always have simple identifiers already; no need
12717          to canonicalize them.  */
12718       return DW_STRING (attr);
12719
12720     case DW_TAG_subprogram:
12721       /* Java constructors will all be named "<init>", so return
12722          the class name when we see this special case.  */
12723       if (cu->language == language_java
12724           && DW_STRING (attr) != NULL
12725           && strcmp (DW_STRING (attr), "<init>") == 0)
12726         {
12727           struct dwarf2_cu *spec_cu = cu;
12728           struct die_info *spec_die;
12729
12730           /* GCJ will output '<init>' for Java constructor names.
12731              For this special case, return the name of the parent class.  */
12732
12733           /* GCJ may output suprogram DIEs with AT_specification set.
12734              If so, use the name of the specified DIE.  */
12735           spec_die = die_specification (die, &spec_cu);
12736           if (spec_die != NULL)
12737             return dwarf2_name (spec_die, spec_cu);
12738
12739           do
12740             {
12741               die = die->parent;
12742               if (die->tag == DW_TAG_class_type)
12743                 return dwarf2_name (die, cu);
12744             }
12745           while (die->tag != DW_TAG_compile_unit);
12746         }
12747       break;
12748
12749     case DW_TAG_class_type:
12750     case DW_TAG_interface_type:
12751     case DW_TAG_structure_type:
12752     case DW_TAG_union_type:
12753       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12754          structures or unions.  These were of the form "._%d" in GCC 4.1,
12755          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12756          and GCC 4.4.  We work around this problem by ignoring these.  */
12757       if (attr && DW_STRING (attr)
12758           && (strncmp (DW_STRING (attr), "._", 2) == 0
12759               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12760         return NULL;
12761
12762       /* GCC might emit a nameless typedef that has a linkage name.  See
12763          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12764       if (!attr || DW_STRING (attr) == NULL)
12765         {
12766           char *demangled = NULL;
12767
12768           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12769           if (attr == NULL)
12770             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12771
12772           if (attr == NULL || DW_STRING (attr) == NULL)
12773             return NULL;
12774
12775           /* Avoid demangling DW_STRING (attr) the second time on a second
12776              call for the same DIE.  */
12777           if (!DW_STRING_IS_CANONICAL (attr))
12778             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12779
12780           if (demangled)
12781             {
12782               char *base;
12783
12784               /* FIXME: we already did this for the partial symbol... */
12785               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12786                                                &cu->objfile->objfile_obstack);
12787               DW_STRING_IS_CANONICAL (attr) = 1;
12788               xfree (demangled);
12789
12790               /* Strip any leading namespaces/classes, keep only the base name.
12791                  DW_AT_name for named DIEs does not contain the prefixes.  */
12792               base = strrchr (DW_STRING (attr), ':');
12793               if (base && base > DW_STRING (attr) && base[-1] == ':')
12794                 return &base[1];
12795               else
12796                 return DW_STRING (attr);
12797             }
12798         }
12799       break;
12800
12801     default:
12802       break;
12803     }
12804
12805   if (!DW_STRING_IS_CANONICAL (attr))
12806     {
12807       DW_STRING (attr)
12808         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12809                                     &cu->objfile->objfile_obstack);
12810       DW_STRING_IS_CANONICAL (attr) = 1;
12811     }
12812   return DW_STRING (attr);
12813 }
12814
12815 /* Return the die that this die in an extension of, or NULL if there
12816    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12817    containing the return value on output.  */
12818
12819 static struct die_info *
12820 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12821 {
12822   struct attribute *attr;
12823
12824   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12825   if (attr == NULL)
12826     return NULL;
12827
12828   return follow_die_ref (die, attr, ext_cu);
12829 }
12830
12831 /* Convert a DIE tag into its string name.  */
12832
12833 static char *
12834 dwarf_tag_name (unsigned tag)
12835 {
12836   switch (tag)
12837     {
12838     case DW_TAG_padding:
12839       return "DW_TAG_padding";
12840     case DW_TAG_array_type:
12841       return "DW_TAG_array_type";
12842     case DW_TAG_class_type:
12843       return "DW_TAG_class_type";
12844     case DW_TAG_entry_point:
12845       return "DW_TAG_entry_point";
12846     case DW_TAG_enumeration_type:
12847       return "DW_TAG_enumeration_type";
12848     case DW_TAG_formal_parameter:
12849       return "DW_TAG_formal_parameter";
12850     case DW_TAG_imported_declaration:
12851       return "DW_TAG_imported_declaration";
12852     case DW_TAG_label:
12853       return "DW_TAG_label";
12854     case DW_TAG_lexical_block:
12855       return "DW_TAG_lexical_block";
12856     case DW_TAG_member:
12857       return "DW_TAG_member";
12858     case DW_TAG_pointer_type:
12859       return "DW_TAG_pointer_type";
12860     case DW_TAG_reference_type:
12861       return "DW_TAG_reference_type";
12862     case DW_TAG_compile_unit:
12863       return "DW_TAG_compile_unit";
12864     case DW_TAG_string_type:
12865       return "DW_TAG_string_type";
12866     case DW_TAG_structure_type:
12867       return "DW_TAG_structure_type";
12868     case DW_TAG_subroutine_type:
12869       return "DW_TAG_subroutine_type";
12870     case DW_TAG_typedef:
12871       return "DW_TAG_typedef";
12872     case DW_TAG_union_type:
12873       return "DW_TAG_union_type";
12874     case DW_TAG_unspecified_parameters:
12875       return "DW_TAG_unspecified_parameters";
12876     case DW_TAG_variant:
12877       return "DW_TAG_variant";
12878     case DW_TAG_common_block:
12879       return "DW_TAG_common_block";
12880     case DW_TAG_common_inclusion:
12881       return "DW_TAG_common_inclusion";
12882     case DW_TAG_inheritance:
12883       return "DW_TAG_inheritance";
12884     case DW_TAG_inlined_subroutine:
12885       return "DW_TAG_inlined_subroutine";
12886     case DW_TAG_module:
12887       return "DW_TAG_module";
12888     case DW_TAG_ptr_to_member_type:
12889       return "DW_TAG_ptr_to_member_type";
12890     case DW_TAG_set_type:
12891       return "DW_TAG_set_type";
12892     case DW_TAG_subrange_type:
12893       return "DW_TAG_subrange_type";
12894     case DW_TAG_with_stmt:
12895       return "DW_TAG_with_stmt";
12896     case DW_TAG_access_declaration:
12897       return "DW_TAG_access_declaration";
12898     case DW_TAG_base_type:
12899       return "DW_TAG_base_type";
12900     case DW_TAG_catch_block:
12901       return "DW_TAG_catch_block";
12902     case DW_TAG_const_type:
12903       return "DW_TAG_const_type";
12904     case DW_TAG_constant:
12905       return "DW_TAG_constant";
12906     case DW_TAG_enumerator:
12907       return "DW_TAG_enumerator";
12908     case DW_TAG_file_type:
12909       return "DW_TAG_file_type";
12910     case DW_TAG_friend:
12911       return "DW_TAG_friend";
12912     case DW_TAG_namelist:
12913       return "DW_TAG_namelist";
12914     case DW_TAG_namelist_item:
12915       return "DW_TAG_namelist_item";
12916     case DW_TAG_packed_type:
12917       return "DW_TAG_packed_type";
12918     case DW_TAG_subprogram:
12919       return "DW_TAG_subprogram";
12920     case DW_TAG_template_type_param:
12921       return "DW_TAG_template_type_param";
12922     case DW_TAG_template_value_param:
12923       return "DW_TAG_template_value_param";
12924     case DW_TAG_thrown_type:
12925       return "DW_TAG_thrown_type";
12926     case DW_TAG_try_block:
12927       return "DW_TAG_try_block";
12928     case DW_TAG_variant_part:
12929       return "DW_TAG_variant_part";
12930     case DW_TAG_variable:
12931       return "DW_TAG_variable";
12932     case DW_TAG_volatile_type:
12933       return "DW_TAG_volatile_type";
12934     case DW_TAG_dwarf_procedure:
12935       return "DW_TAG_dwarf_procedure";
12936     case DW_TAG_restrict_type:
12937       return "DW_TAG_restrict_type";
12938     case DW_TAG_interface_type:
12939       return "DW_TAG_interface_type";
12940     case DW_TAG_namespace:
12941       return "DW_TAG_namespace";
12942     case DW_TAG_imported_module:
12943       return "DW_TAG_imported_module";
12944     case DW_TAG_unspecified_type:
12945       return "DW_TAG_unspecified_type";
12946     case DW_TAG_partial_unit:
12947       return "DW_TAG_partial_unit";
12948     case DW_TAG_imported_unit:
12949       return "DW_TAG_imported_unit";
12950     case DW_TAG_condition:
12951       return "DW_TAG_condition";
12952     case DW_TAG_shared_type:
12953       return "DW_TAG_shared_type";
12954     case DW_TAG_type_unit:
12955       return "DW_TAG_type_unit";
12956     case DW_TAG_MIPS_loop:
12957       return "DW_TAG_MIPS_loop";
12958     case DW_TAG_HP_array_descriptor:
12959       return "DW_TAG_HP_array_descriptor";
12960     case DW_TAG_format_label:
12961       return "DW_TAG_format_label";
12962     case DW_TAG_function_template:
12963       return "DW_TAG_function_template";
12964     case DW_TAG_class_template:
12965       return "DW_TAG_class_template";
12966     case DW_TAG_GNU_BINCL:
12967       return "DW_TAG_GNU_BINCL";
12968     case DW_TAG_GNU_EINCL:
12969       return "DW_TAG_GNU_EINCL";
12970     case DW_TAG_upc_shared_type:
12971       return "DW_TAG_upc_shared_type";
12972     case DW_TAG_upc_strict_type:
12973       return "DW_TAG_upc_strict_type";
12974     case DW_TAG_upc_relaxed_type:
12975       return "DW_TAG_upc_relaxed_type";
12976     case DW_TAG_PGI_kanji_type:
12977       return "DW_TAG_PGI_kanji_type";
12978     case DW_TAG_PGI_interface_block:
12979       return "DW_TAG_PGI_interface_block";
12980     case DW_TAG_GNU_call_site:
12981       return "DW_TAG_GNU_call_site";
12982     default:
12983       return "DW_TAG_<unknown>";
12984     }
12985 }
12986
12987 /* Convert a DWARF attribute code into its string name.  */
12988
12989 static char *
12990 dwarf_attr_name (unsigned attr)
12991 {
12992   switch (attr)
12993     {
12994     case DW_AT_sibling:
12995       return "DW_AT_sibling";
12996     case DW_AT_location:
12997       return "DW_AT_location";
12998     case DW_AT_name:
12999       return "DW_AT_name";
13000     case DW_AT_ordering:
13001       return "DW_AT_ordering";
13002     case DW_AT_subscr_data:
13003       return "DW_AT_subscr_data";
13004     case DW_AT_byte_size:
13005       return "DW_AT_byte_size";
13006     case DW_AT_bit_offset:
13007       return "DW_AT_bit_offset";
13008     case DW_AT_bit_size:
13009       return "DW_AT_bit_size";
13010     case DW_AT_element_list:
13011       return "DW_AT_element_list";
13012     case DW_AT_stmt_list:
13013       return "DW_AT_stmt_list";
13014     case DW_AT_low_pc:
13015       return "DW_AT_low_pc";
13016     case DW_AT_high_pc:
13017       return "DW_AT_high_pc";
13018     case DW_AT_language:
13019       return "DW_AT_language";
13020     case DW_AT_member:
13021       return "DW_AT_member";
13022     case DW_AT_discr:
13023       return "DW_AT_discr";
13024     case DW_AT_discr_value:
13025       return "DW_AT_discr_value";
13026     case DW_AT_visibility:
13027       return "DW_AT_visibility";
13028     case DW_AT_import:
13029       return "DW_AT_import";
13030     case DW_AT_string_length:
13031       return "DW_AT_string_length";
13032     case DW_AT_common_reference:
13033       return "DW_AT_common_reference";
13034     case DW_AT_comp_dir:
13035       return "DW_AT_comp_dir";
13036     case DW_AT_const_value:
13037       return "DW_AT_const_value";
13038     case DW_AT_containing_type:
13039       return "DW_AT_containing_type";
13040     case DW_AT_default_value:
13041       return "DW_AT_default_value";
13042     case DW_AT_inline:
13043       return "DW_AT_inline";
13044     case DW_AT_is_optional:
13045       return "DW_AT_is_optional";
13046     case DW_AT_lower_bound:
13047       return "DW_AT_lower_bound";
13048     case DW_AT_producer:
13049       return "DW_AT_producer";
13050     case DW_AT_prototyped:
13051       return "DW_AT_prototyped";
13052     case DW_AT_return_addr:
13053       return "DW_AT_return_addr";
13054     case DW_AT_start_scope:
13055       return "DW_AT_start_scope";
13056     case DW_AT_bit_stride:
13057       return "DW_AT_bit_stride";
13058     case DW_AT_upper_bound:
13059       return "DW_AT_upper_bound";
13060     case DW_AT_abstract_origin:
13061       return "DW_AT_abstract_origin";
13062     case DW_AT_accessibility:
13063       return "DW_AT_accessibility";
13064     case DW_AT_address_class:
13065       return "DW_AT_address_class";
13066     case DW_AT_artificial:
13067       return "DW_AT_artificial";
13068     case DW_AT_base_types:
13069       return "DW_AT_base_types";
13070     case DW_AT_calling_convention:
13071       return "DW_AT_calling_convention";
13072     case DW_AT_count:
13073       return "DW_AT_count";
13074     case DW_AT_data_member_location:
13075       return "DW_AT_data_member_location";
13076     case DW_AT_decl_column:
13077       return "DW_AT_decl_column";
13078     case DW_AT_decl_file:
13079       return "DW_AT_decl_file";
13080     case DW_AT_decl_line:
13081       return "DW_AT_decl_line";
13082     case DW_AT_declaration:
13083       return "DW_AT_declaration";
13084     case DW_AT_discr_list:
13085       return "DW_AT_discr_list";
13086     case DW_AT_encoding:
13087       return "DW_AT_encoding";
13088     case DW_AT_external:
13089       return "DW_AT_external";
13090     case DW_AT_frame_base:
13091       return "DW_AT_frame_base";
13092     case DW_AT_friend:
13093       return "DW_AT_friend";
13094     case DW_AT_identifier_case:
13095       return "DW_AT_identifier_case";
13096     case DW_AT_macro_info:
13097       return "DW_AT_macro_info";
13098     case DW_AT_namelist_items:
13099       return "DW_AT_namelist_items";
13100     case DW_AT_priority:
13101       return "DW_AT_priority";
13102     case DW_AT_segment:
13103       return "DW_AT_segment";
13104     case DW_AT_specification:
13105       return "DW_AT_specification";
13106     case DW_AT_static_link:
13107       return "DW_AT_static_link";
13108     case DW_AT_type:
13109       return "DW_AT_type";
13110     case DW_AT_use_location:
13111       return "DW_AT_use_location";
13112     case DW_AT_variable_parameter:
13113       return "DW_AT_variable_parameter";
13114     case DW_AT_virtuality:
13115       return "DW_AT_virtuality";
13116     case DW_AT_vtable_elem_location:
13117       return "DW_AT_vtable_elem_location";
13118     /* DWARF 3 values.  */
13119     case DW_AT_allocated:
13120       return "DW_AT_allocated";
13121     case DW_AT_associated:
13122       return "DW_AT_associated";
13123     case DW_AT_data_location:
13124       return "DW_AT_data_location";
13125     case DW_AT_byte_stride:
13126       return "DW_AT_byte_stride";
13127     case DW_AT_entry_pc:
13128       return "DW_AT_entry_pc";
13129     case DW_AT_use_UTF8:
13130       return "DW_AT_use_UTF8";
13131     case DW_AT_extension:
13132       return "DW_AT_extension";
13133     case DW_AT_ranges:
13134       return "DW_AT_ranges";
13135     case DW_AT_trampoline:
13136       return "DW_AT_trampoline";
13137     case DW_AT_call_column:
13138       return "DW_AT_call_column";
13139     case DW_AT_call_file:
13140       return "DW_AT_call_file";
13141     case DW_AT_call_line:
13142       return "DW_AT_call_line";
13143     case DW_AT_description:
13144       return "DW_AT_description";
13145     case DW_AT_binary_scale:
13146       return "DW_AT_binary_scale";
13147     case DW_AT_decimal_scale:
13148       return "DW_AT_decimal_scale";
13149     case DW_AT_small:
13150       return "DW_AT_small";
13151     case DW_AT_decimal_sign:
13152       return "DW_AT_decimal_sign";
13153     case DW_AT_digit_count:
13154       return "DW_AT_digit_count";
13155     case DW_AT_picture_string:
13156       return "DW_AT_picture_string";
13157     case DW_AT_mutable:
13158       return "DW_AT_mutable";
13159     case DW_AT_threads_scaled:
13160       return "DW_AT_threads_scaled";
13161     case DW_AT_explicit:
13162       return "DW_AT_explicit";
13163     case DW_AT_object_pointer:
13164       return "DW_AT_object_pointer";
13165     case DW_AT_endianity:
13166       return "DW_AT_endianity";
13167     case DW_AT_elemental:
13168       return "DW_AT_elemental";
13169     case DW_AT_pure:
13170       return "DW_AT_pure";
13171     case DW_AT_recursive:
13172       return "DW_AT_recursive";
13173     /* DWARF 4 values.  */
13174     case DW_AT_signature:
13175       return "DW_AT_signature";
13176     case DW_AT_linkage_name:
13177       return "DW_AT_linkage_name";
13178     /* SGI/MIPS extensions.  */
13179 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13180     case DW_AT_MIPS_fde:
13181       return "DW_AT_MIPS_fde";
13182 #endif
13183     case DW_AT_MIPS_loop_begin:
13184       return "DW_AT_MIPS_loop_begin";
13185     case DW_AT_MIPS_tail_loop_begin:
13186       return "DW_AT_MIPS_tail_loop_begin";
13187     case DW_AT_MIPS_epilog_begin:
13188       return "DW_AT_MIPS_epilog_begin";
13189     case DW_AT_MIPS_loop_unroll_factor:
13190       return "DW_AT_MIPS_loop_unroll_factor";
13191     case DW_AT_MIPS_software_pipeline_depth:
13192       return "DW_AT_MIPS_software_pipeline_depth";
13193     case DW_AT_MIPS_linkage_name:
13194       return "DW_AT_MIPS_linkage_name";
13195     case DW_AT_MIPS_stride:
13196       return "DW_AT_MIPS_stride";
13197     case DW_AT_MIPS_abstract_name:
13198       return "DW_AT_MIPS_abstract_name";
13199     case DW_AT_MIPS_clone_origin:
13200       return "DW_AT_MIPS_clone_origin";
13201     case DW_AT_MIPS_has_inlines:
13202       return "DW_AT_MIPS_has_inlines";
13203     /* HP extensions.  */
13204 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13205     case DW_AT_HP_block_index:
13206       return "DW_AT_HP_block_index";
13207 #endif
13208     case DW_AT_HP_unmodifiable:
13209       return "DW_AT_HP_unmodifiable";
13210     case DW_AT_HP_actuals_stmt_list:
13211       return "DW_AT_HP_actuals_stmt_list";
13212     case DW_AT_HP_proc_per_section:
13213       return "DW_AT_HP_proc_per_section";
13214     case DW_AT_HP_raw_data_ptr:
13215       return "DW_AT_HP_raw_data_ptr";
13216     case DW_AT_HP_pass_by_reference:
13217       return "DW_AT_HP_pass_by_reference";
13218     case DW_AT_HP_opt_level:
13219       return "DW_AT_HP_opt_level";
13220     case DW_AT_HP_prof_version_id:
13221       return "DW_AT_HP_prof_version_id";
13222     case DW_AT_HP_opt_flags:
13223       return "DW_AT_HP_opt_flags";
13224     case DW_AT_HP_cold_region_low_pc:
13225       return "DW_AT_HP_cold_region_low_pc";
13226     case DW_AT_HP_cold_region_high_pc:
13227       return "DW_AT_HP_cold_region_high_pc";
13228     case DW_AT_HP_all_variables_modifiable:
13229       return "DW_AT_HP_all_variables_modifiable";
13230     case DW_AT_HP_linkage_name:
13231       return "DW_AT_HP_linkage_name";
13232     case DW_AT_HP_prof_flags:
13233       return "DW_AT_HP_prof_flags";
13234     /* GNU extensions.  */
13235     case DW_AT_sf_names:
13236       return "DW_AT_sf_names";
13237     case DW_AT_src_info:
13238       return "DW_AT_src_info";
13239     case DW_AT_mac_info:
13240       return "DW_AT_mac_info";
13241     case DW_AT_src_coords:
13242       return "DW_AT_src_coords";
13243     case DW_AT_body_begin:
13244       return "DW_AT_body_begin";
13245     case DW_AT_body_end:
13246       return "DW_AT_body_end";
13247     case DW_AT_GNU_vector:
13248       return "DW_AT_GNU_vector";
13249     case DW_AT_GNU_odr_signature:
13250       return "DW_AT_GNU_odr_signature";
13251     /* VMS extensions.  */
13252     case DW_AT_VMS_rtnbeg_pd_address:
13253       return "DW_AT_VMS_rtnbeg_pd_address";
13254     /* UPC extension.  */
13255     case DW_AT_upc_threads_scaled:
13256       return "DW_AT_upc_threads_scaled";
13257     /* PGI (STMicroelectronics) extensions.  */
13258     case DW_AT_PGI_lbase:
13259       return "DW_AT_PGI_lbase";
13260     case DW_AT_PGI_soffset:
13261       return "DW_AT_PGI_soffset";
13262     case DW_AT_PGI_lstride:
13263       return "DW_AT_PGI_lstride";
13264     default:
13265       return "DW_AT_<unknown>";
13266     }
13267 }
13268
13269 /* Convert a DWARF value form code into its string name.  */
13270
13271 static char *
13272 dwarf_form_name (unsigned form)
13273 {
13274   switch (form)
13275     {
13276     case DW_FORM_addr:
13277       return "DW_FORM_addr";
13278     case DW_FORM_block2:
13279       return "DW_FORM_block2";
13280     case DW_FORM_block4:
13281       return "DW_FORM_block4";
13282     case DW_FORM_data2:
13283       return "DW_FORM_data2";
13284     case DW_FORM_data4:
13285       return "DW_FORM_data4";
13286     case DW_FORM_data8:
13287       return "DW_FORM_data8";
13288     case DW_FORM_string:
13289       return "DW_FORM_string";
13290     case DW_FORM_block:
13291       return "DW_FORM_block";
13292     case DW_FORM_block1:
13293       return "DW_FORM_block1";
13294     case DW_FORM_data1:
13295       return "DW_FORM_data1";
13296     case DW_FORM_flag:
13297       return "DW_FORM_flag";
13298     case DW_FORM_sdata:
13299       return "DW_FORM_sdata";
13300     case DW_FORM_strp:
13301       return "DW_FORM_strp";
13302     case DW_FORM_udata:
13303       return "DW_FORM_udata";
13304     case DW_FORM_ref_addr:
13305       return "DW_FORM_ref_addr";
13306     case DW_FORM_ref1:
13307       return "DW_FORM_ref1";
13308     case DW_FORM_ref2:
13309       return "DW_FORM_ref2";
13310     case DW_FORM_ref4:
13311       return "DW_FORM_ref4";
13312     case DW_FORM_ref8:
13313       return "DW_FORM_ref8";
13314     case DW_FORM_ref_udata:
13315       return "DW_FORM_ref_udata";
13316     case DW_FORM_indirect:
13317       return "DW_FORM_indirect";
13318     case DW_FORM_sec_offset:
13319       return "DW_FORM_sec_offset";
13320     case DW_FORM_exprloc:
13321       return "DW_FORM_exprloc";
13322     case DW_FORM_flag_present:
13323       return "DW_FORM_flag_present";
13324     case DW_FORM_ref_sig8:
13325       return "DW_FORM_ref_sig8";
13326     default:
13327       return "DW_FORM_<unknown>";
13328     }
13329 }
13330
13331 /* Convert a DWARF stack opcode into its string name.  */
13332
13333 const char *
13334 dwarf_stack_op_name (unsigned op)
13335 {
13336   switch (op)
13337     {
13338     case DW_OP_addr:
13339       return "DW_OP_addr";
13340     case DW_OP_deref:
13341       return "DW_OP_deref";
13342     case DW_OP_const1u:
13343       return "DW_OP_const1u";
13344     case DW_OP_const1s:
13345       return "DW_OP_const1s";
13346     case DW_OP_const2u:
13347       return "DW_OP_const2u";
13348     case DW_OP_const2s:
13349       return "DW_OP_const2s";
13350     case DW_OP_const4u:
13351       return "DW_OP_const4u";
13352     case DW_OP_const4s:
13353       return "DW_OP_const4s";
13354     case DW_OP_const8u:
13355       return "DW_OP_const8u";
13356     case DW_OP_const8s:
13357       return "DW_OP_const8s";
13358     case DW_OP_constu:
13359       return "DW_OP_constu";
13360     case DW_OP_consts:
13361       return "DW_OP_consts";
13362     case DW_OP_dup:
13363       return "DW_OP_dup";
13364     case DW_OP_drop:
13365       return "DW_OP_drop";
13366     case DW_OP_over:
13367       return "DW_OP_over";
13368     case DW_OP_pick:
13369       return "DW_OP_pick";
13370     case DW_OP_swap:
13371       return "DW_OP_swap";
13372     case DW_OP_rot:
13373       return "DW_OP_rot";
13374     case DW_OP_xderef:
13375       return "DW_OP_xderef";
13376     case DW_OP_abs:
13377       return "DW_OP_abs";
13378     case DW_OP_and:
13379       return "DW_OP_and";
13380     case DW_OP_div:
13381       return "DW_OP_div";
13382     case DW_OP_minus:
13383       return "DW_OP_minus";
13384     case DW_OP_mod:
13385       return "DW_OP_mod";
13386     case DW_OP_mul:
13387       return "DW_OP_mul";
13388     case DW_OP_neg:
13389       return "DW_OP_neg";
13390     case DW_OP_not:
13391       return "DW_OP_not";
13392     case DW_OP_or:
13393       return "DW_OP_or";
13394     case DW_OP_plus:
13395       return "DW_OP_plus";
13396     case DW_OP_plus_uconst:
13397       return "DW_OP_plus_uconst";
13398     case DW_OP_shl:
13399       return "DW_OP_shl";
13400     case DW_OP_shr:
13401       return "DW_OP_shr";
13402     case DW_OP_shra:
13403       return "DW_OP_shra";
13404     case DW_OP_xor:
13405       return "DW_OP_xor";
13406     case DW_OP_bra:
13407       return "DW_OP_bra";
13408     case DW_OP_eq:
13409       return "DW_OP_eq";
13410     case DW_OP_ge:
13411       return "DW_OP_ge";
13412     case DW_OP_gt:
13413       return "DW_OP_gt";
13414     case DW_OP_le:
13415       return "DW_OP_le";
13416     case DW_OP_lt:
13417       return "DW_OP_lt";
13418     case DW_OP_ne:
13419       return "DW_OP_ne";
13420     case DW_OP_skip:
13421       return "DW_OP_skip";
13422     case DW_OP_lit0:
13423       return "DW_OP_lit0";
13424     case DW_OP_lit1:
13425       return "DW_OP_lit1";
13426     case DW_OP_lit2:
13427       return "DW_OP_lit2";
13428     case DW_OP_lit3:
13429       return "DW_OP_lit3";
13430     case DW_OP_lit4:
13431       return "DW_OP_lit4";
13432     case DW_OP_lit5:
13433       return "DW_OP_lit5";
13434     case DW_OP_lit6:
13435       return "DW_OP_lit6";
13436     case DW_OP_lit7:
13437       return "DW_OP_lit7";
13438     case DW_OP_lit8:
13439       return "DW_OP_lit8";
13440     case DW_OP_lit9:
13441       return "DW_OP_lit9";
13442     case DW_OP_lit10:
13443       return "DW_OP_lit10";
13444     case DW_OP_lit11:
13445       return "DW_OP_lit11";
13446     case DW_OP_lit12:
13447       return "DW_OP_lit12";
13448     case DW_OP_lit13:
13449       return "DW_OP_lit13";
13450     case DW_OP_lit14:
13451       return "DW_OP_lit14";
13452     case DW_OP_lit15:
13453       return "DW_OP_lit15";
13454     case DW_OP_lit16:
13455       return "DW_OP_lit16";
13456     case DW_OP_lit17:
13457       return "DW_OP_lit17";
13458     case DW_OP_lit18:
13459       return "DW_OP_lit18";
13460     case DW_OP_lit19:
13461       return "DW_OP_lit19";
13462     case DW_OP_lit20:
13463       return "DW_OP_lit20";
13464     case DW_OP_lit21:
13465       return "DW_OP_lit21";
13466     case DW_OP_lit22:
13467       return "DW_OP_lit22";
13468     case DW_OP_lit23:
13469       return "DW_OP_lit23";
13470     case DW_OP_lit24:
13471       return "DW_OP_lit24";
13472     case DW_OP_lit25:
13473       return "DW_OP_lit25";
13474     case DW_OP_lit26:
13475       return "DW_OP_lit26";
13476     case DW_OP_lit27:
13477       return "DW_OP_lit27";
13478     case DW_OP_lit28:
13479       return "DW_OP_lit28";
13480     case DW_OP_lit29:
13481       return "DW_OP_lit29";
13482     case DW_OP_lit30:
13483       return "DW_OP_lit30";
13484     case DW_OP_lit31:
13485       return "DW_OP_lit31";
13486     case DW_OP_reg0:
13487       return "DW_OP_reg0";
13488     case DW_OP_reg1:
13489       return "DW_OP_reg1";
13490     case DW_OP_reg2:
13491       return "DW_OP_reg2";
13492     case DW_OP_reg3:
13493       return "DW_OP_reg3";
13494     case DW_OP_reg4:
13495       return "DW_OP_reg4";
13496     case DW_OP_reg5:
13497       return "DW_OP_reg5";
13498     case DW_OP_reg6:
13499       return "DW_OP_reg6";
13500     case DW_OP_reg7:
13501       return "DW_OP_reg7";
13502     case DW_OP_reg8:
13503       return "DW_OP_reg8";
13504     case DW_OP_reg9:
13505       return "DW_OP_reg9";
13506     case DW_OP_reg10:
13507       return "DW_OP_reg10";
13508     case DW_OP_reg11:
13509       return "DW_OP_reg11";
13510     case DW_OP_reg12:
13511       return "DW_OP_reg12";
13512     case DW_OP_reg13:
13513       return "DW_OP_reg13";
13514     case DW_OP_reg14:
13515       return "DW_OP_reg14";
13516     case DW_OP_reg15:
13517       return "DW_OP_reg15";
13518     case DW_OP_reg16:
13519       return "DW_OP_reg16";
13520     case DW_OP_reg17:
13521       return "DW_OP_reg17";
13522     case DW_OP_reg18:
13523       return "DW_OP_reg18";
13524     case DW_OP_reg19:
13525       return "DW_OP_reg19";
13526     case DW_OP_reg20:
13527       return "DW_OP_reg20";
13528     case DW_OP_reg21:
13529       return "DW_OP_reg21";
13530     case DW_OP_reg22:
13531       return "DW_OP_reg22";
13532     case DW_OP_reg23:
13533       return "DW_OP_reg23";
13534     case DW_OP_reg24:
13535       return "DW_OP_reg24";
13536     case DW_OP_reg25:
13537       return "DW_OP_reg25";
13538     case DW_OP_reg26:
13539       return "DW_OP_reg26";
13540     case DW_OP_reg27:
13541       return "DW_OP_reg27";
13542     case DW_OP_reg28:
13543       return "DW_OP_reg28";
13544     case DW_OP_reg29:
13545       return "DW_OP_reg29";
13546     case DW_OP_reg30:
13547       return "DW_OP_reg30";
13548     case DW_OP_reg31:
13549       return "DW_OP_reg31";
13550     case DW_OP_breg0:
13551       return "DW_OP_breg0";
13552     case DW_OP_breg1:
13553       return "DW_OP_breg1";
13554     case DW_OP_breg2:
13555       return "DW_OP_breg2";
13556     case DW_OP_breg3:
13557       return "DW_OP_breg3";
13558     case DW_OP_breg4:
13559       return "DW_OP_breg4";
13560     case DW_OP_breg5:
13561       return "DW_OP_breg5";
13562     case DW_OP_breg6:
13563       return "DW_OP_breg6";
13564     case DW_OP_breg7:
13565       return "DW_OP_breg7";
13566     case DW_OP_breg8:
13567       return "DW_OP_breg8";
13568     case DW_OP_breg9:
13569       return "DW_OP_breg9";
13570     case DW_OP_breg10:
13571       return "DW_OP_breg10";
13572     case DW_OP_breg11:
13573       return "DW_OP_breg11";
13574     case DW_OP_breg12:
13575       return "DW_OP_breg12";
13576     case DW_OP_breg13:
13577       return "DW_OP_breg13";
13578     case DW_OP_breg14:
13579       return "DW_OP_breg14";
13580     case DW_OP_breg15:
13581       return "DW_OP_breg15";
13582     case DW_OP_breg16:
13583       return "DW_OP_breg16";
13584     case DW_OP_breg17:
13585       return "DW_OP_breg17";
13586     case DW_OP_breg18:
13587       return "DW_OP_breg18";
13588     case DW_OP_breg19:
13589       return "DW_OP_breg19";
13590     case DW_OP_breg20:
13591       return "DW_OP_breg20";
13592     case DW_OP_breg21:
13593       return "DW_OP_breg21";
13594     case DW_OP_breg22:
13595       return "DW_OP_breg22";
13596     case DW_OP_breg23:
13597       return "DW_OP_breg23";
13598     case DW_OP_breg24:
13599       return "DW_OP_breg24";
13600     case DW_OP_breg25:
13601       return "DW_OP_breg25";
13602     case DW_OP_breg26:
13603       return "DW_OP_breg26";
13604     case DW_OP_breg27:
13605       return "DW_OP_breg27";
13606     case DW_OP_breg28:
13607       return "DW_OP_breg28";
13608     case DW_OP_breg29:
13609       return "DW_OP_breg29";
13610     case DW_OP_breg30:
13611       return "DW_OP_breg30";
13612     case DW_OP_breg31:
13613       return "DW_OP_breg31";
13614     case DW_OP_regx:
13615       return "DW_OP_regx";
13616     case DW_OP_fbreg:
13617       return "DW_OP_fbreg";
13618     case DW_OP_bregx:
13619       return "DW_OP_bregx";
13620     case DW_OP_piece:
13621       return "DW_OP_piece";
13622     case DW_OP_deref_size:
13623       return "DW_OP_deref_size";
13624     case DW_OP_xderef_size:
13625       return "DW_OP_xderef_size";
13626     case DW_OP_nop:
13627       return "DW_OP_nop";
13628     /* DWARF 3 extensions.  */
13629     case DW_OP_push_object_address:
13630       return "DW_OP_push_object_address";
13631     case DW_OP_call2:
13632       return "DW_OP_call2";
13633     case DW_OP_call4:
13634       return "DW_OP_call4";
13635     case DW_OP_call_ref:
13636       return "DW_OP_call_ref";
13637     case DW_OP_form_tls_address:
13638       return "DW_OP_form_tls_address";
13639     case DW_OP_call_frame_cfa:
13640       return "DW_OP_call_frame_cfa";
13641     case DW_OP_bit_piece:
13642       return "DW_OP_bit_piece";
13643     /* DWARF 4 extensions.  */
13644     case DW_OP_implicit_value:
13645       return "DW_OP_implicit_value";
13646     case DW_OP_stack_value:
13647       return "DW_OP_stack_value";
13648     /* GNU extensions.  */
13649     case DW_OP_GNU_push_tls_address:
13650       return "DW_OP_GNU_push_tls_address";
13651     case DW_OP_GNU_uninit:
13652       return "DW_OP_GNU_uninit";
13653     case DW_OP_GNU_implicit_pointer:
13654       return "DW_OP_GNU_implicit_pointer";
13655     case DW_OP_GNU_entry_value:
13656       return "DW_OP_GNU_entry_value";
13657     case DW_OP_GNU_const_type:
13658       return "DW_OP_GNU_const_type";
13659     case DW_OP_GNU_regval_type:
13660       return "DW_OP_GNU_regval_type";
13661     case DW_OP_GNU_deref_type:
13662       return "DW_OP_GNU_deref_type";
13663     case DW_OP_GNU_convert:
13664       return "DW_OP_GNU_convert";
13665     case DW_OP_GNU_reinterpret:
13666       return "DW_OP_GNU_reinterpret";
13667     default:
13668       return NULL;
13669     }
13670 }
13671
13672 static char *
13673 dwarf_bool_name (unsigned mybool)
13674 {
13675   if (mybool)
13676     return "TRUE";
13677   else
13678     return "FALSE";
13679 }
13680
13681 /* Convert a DWARF type code into its string name.  */
13682
13683 static char *
13684 dwarf_type_encoding_name (unsigned enc)
13685 {
13686   switch (enc)
13687     {
13688     case DW_ATE_void:
13689       return "DW_ATE_void";
13690     case DW_ATE_address:
13691       return "DW_ATE_address";
13692     case DW_ATE_boolean:
13693       return "DW_ATE_boolean";
13694     case DW_ATE_complex_float:
13695       return "DW_ATE_complex_float";
13696     case DW_ATE_float:
13697       return "DW_ATE_float";
13698     case DW_ATE_signed:
13699       return "DW_ATE_signed";
13700     case DW_ATE_signed_char:
13701       return "DW_ATE_signed_char";
13702     case DW_ATE_unsigned:
13703       return "DW_ATE_unsigned";
13704     case DW_ATE_unsigned_char:
13705       return "DW_ATE_unsigned_char";
13706     /* DWARF 3.  */
13707     case DW_ATE_imaginary_float:
13708       return "DW_ATE_imaginary_float";
13709     case DW_ATE_packed_decimal:
13710       return "DW_ATE_packed_decimal";
13711     case DW_ATE_numeric_string:
13712       return "DW_ATE_numeric_string";
13713     case DW_ATE_edited:
13714       return "DW_ATE_edited";
13715     case DW_ATE_signed_fixed:
13716       return "DW_ATE_signed_fixed";
13717     case DW_ATE_unsigned_fixed:
13718       return "DW_ATE_unsigned_fixed";
13719     case DW_ATE_decimal_float:
13720       return "DW_ATE_decimal_float";
13721     /* DWARF 4.  */
13722     case DW_ATE_UTF:
13723       return "DW_ATE_UTF";
13724     /* HP extensions.  */
13725     case DW_ATE_HP_float80:
13726       return "DW_ATE_HP_float80";
13727     case DW_ATE_HP_complex_float80:
13728       return "DW_ATE_HP_complex_float80";
13729     case DW_ATE_HP_float128:
13730       return "DW_ATE_HP_float128";
13731     case DW_ATE_HP_complex_float128:
13732       return "DW_ATE_HP_complex_float128";
13733     case DW_ATE_HP_floathpintel:
13734       return "DW_ATE_HP_floathpintel";
13735     case DW_ATE_HP_imaginary_float80:
13736       return "DW_ATE_HP_imaginary_float80";
13737     case DW_ATE_HP_imaginary_float128:
13738       return "DW_ATE_HP_imaginary_float128";
13739     default:
13740       return "DW_ATE_<unknown>";
13741     }
13742 }
13743
13744 /* Convert a DWARF call frame info operation to its string name.  */
13745
13746 #if 0
13747 static char *
13748 dwarf_cfi_name (unsigned cfi_opc)
13749 {
13750   switch (cfi_opc)
13751     {
13752     case DW_CFA_advance_loc:
13753       return "DW_CFA_advance_loc";
13754     case DW_CFA_offset:
13755       return "DW_CFA_offset";
13756     case DW_CFA_restore:
13757       return "DW_CFA_restore";
13758     case DW_CFA_nop:
13759       return "DW_CFA_nop";
13760     case DW_CFA_set_loc:
13761       return "DW_CFA_set_loc";
13762     case DW_CFA_advance_loc1:
13763       return "DW_CFA_advance_loc1";
13764     case DW_CFA_advance_loc2:
13765       return "DW_CFA_advance_loc2";
13766     case DW_CFA_advance_loc4:
13767       return "DW_CFA_advance_loc4";
13768     case DW_CFA_offset_extended:
13769       return "DW_CFA_offset_extended";
13770     case DW_CFA_restore_extended:
13771       return "DW_CFA_restore_extended";
13772     case DW_CFA_undefined:
13773       return "DW_CFA_undefined";
13774     case DW_CFA_same_value:
13775       return "DW_CFA_same_value";
13776     case DW_CFA_register:
13777       return "DW_CFA_register";
13778     case DW_CFA_remember_state:
13779       return "DW_CFA_remember_state";
13780     case DW_CFA_restore_state:
13781       return "DW_CFA_restore_state";
13782     case DW_CFA_def_cfa:
13783       return "DW_CFA_def_cfa";
13784     case DW_CFA_def_cfa_register:
13785       return "DW_CFA_def_cfa_register";
13786     case DW_CFA_def_cfa_offset:
13787       return "DW_CFA_def_cfa_offset";
13788     /* DWARF 3.  */
13789     case DW_CFA_def_cfa_expression:
13790       return "DW_CFA_def_cfa_expression";
13791     case DW_CFA_expression:
13792       return "DW_CFA_expression";
13793     case DW_CFA_offset_extended_sf:
13794       return "DW_CFA_offset_extended_sf";
13795     case DW_CFA_def_cfa_sf:
13796       return "DW_CFA_def_cfa_sf";
13797     case DW_CFA_def_cfa_offset_sf:
13798       return "DW_CFA_def_cfa_offset_sf";
13799     case DW_CFA_val_offset:
13800       return "DW_CFA_val_offset";
13801     case DW_CFA_val_offset_sf:
13802       return "DW_CFA_val_offset_sf";
13803     case DW_CFA_val_expression:
13804       return "DW_CFA_val_expression";
13805     /* SGI/MIPS specific.  */
13806     case DW_CFA_MIPS_advance_loc8:
13807       return "DW_CFA_MIPS_advance_loc8";
13808     /* GNU extensions.  */
13809     case DW_CFA_GNU_window_save:
13810       return "DW_CFA_GNU_window_save";
13811     case DW_CFA_GNU_args_size:
13812       return "DW_CFA_GNU_args_size";
13813     case DW_CFA_GNU_negative_offset_extended:
13814       return "DW_CFA_GNU_negative_offset_extended";
13815     default:
13816       return "DW_CFA_<unknown>";
13817     }
13818 }
13819 #endif
13820
13821 static void
13822 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13823 {
13824   unsigned int i;
13825
13826   print_spaces (indent, f);
13827   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13828            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13829
13830   if (die->parent != NULL)
13831     {
13832       print_spaces (indent, f);
13833       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13834                           die->parent->offset);
13835     }
13836
13837   print_spaces (indent, f);
13838   fprintf_unfiltered (f, "  has children: %s\n",
13839            dwarf_bool_name (die->child != NULL));
13840
13841   print_spaces (indent, f);
13842   fprintf_unfiltered (f, "  attributes:\n");
13843
13844   for (i = 0; i < die->num_attrs; ++i)
13845     {
13846       print_spaces (indent, f);
13847       fprintf_unfiltered (f, "    %s (%s) ",
13848                dwarf_attr_name (die->attrs[i].name),
13849                dwarf_form_name (die->attrs[i].form));
13850
13851       switch (die->attrs[i].form)
13852         {
13853         case DW_FORM_ref_addr:
13854         case DW_FORM_addr:
13855           fprintf_unfiltered (f, "address: ");
13856           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13857           break;
13858         case DW_FORM_block2:
13859         case DW_FORM_block4:
13860         case DW_FORM_block:
13861         case DW_FORM_block1:
13862           fprintf_unfiltered (f, "block: size %d",
13863                               DW_BLOCK (&die->attrs[i])->size);
13864           break;
13865         case DW_FORM_exprloc:
13866           fprintf_unfiltered (f, "expression: size %u",
13867                               DW_BLOCK (&die->attrs[i])->size);
13868           break;
13869         case DW_FORM_ref1:
13870         case DW_FORM_ref2:
13871         case DW_FORM_ref4:
13872           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13873                               (long) (DW_ADDR (&die->attrs[i])));
13874           break;
13875         case DW_FORM_data1:
13876         case DW_FORM_data2:
13877         case DW_FORM_data4:
13878         case DW_FORM_data8:
13879         case DW_FORM_udata:
13880         case DW_FORM_sdata:
13881           fprintf_unfiltered (f, "constant: %s",
13882                               pulongest (DW_UNSND (&die->attrs[i])));
13883           break;
13884         case DW_FORM_sec_offset:
13885           fprintf_unfiltered (f, "section offset: %s",
13886                               pulongest (DW_UNSND (&die->attrs[i])));
13887           break;
13888         case DW_FORM_ref_sig8:
13889           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13890             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13891                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13892           else
13893             fprintf_unfiltered (f, "signatured type, offset: unknown");
13894           break;
13895         case DW_FORM_string:
13896         case DW_FORM_strp:
13897           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13898                    DW_STRING (&die->attrs[i])
13899                    ? DW_STRING (&die->attrs[i]) : "",
13900                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13901           break;
13902         case DW_FORM_flag:
13903           if (DW_UNSND (&die->attrs[i]))
13904             fprintf_unfiltered (f, "flag: TRUE");
13905           else
13906             fprintf_unfiltered (f, "flag: FALSE");
13907           break;
13908         case DW_FORM_flag_present:
13909           fprintf_unfiltered (f, "flag: TRUE");
13910           break;
13911         case DW_FORM_indirect:
13912           /* The reader will have reduced the indirect form to
13913              the "base form" so this form should not occur.  */
13914           fprintf_unfiltered (f, 
13915                               "unexpected attribute form: DW_FORM_indirect");
13916           break;
13917         default:
13918           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13919                    die->attrs[i].form);
13920           break;
13921         }
13922       fprintf_unfiltered (f, "\n");
13923     }
13924 }
13925
13926 static void
13927 dump_die_for_error (struct die_info *die)
13928 {
13929   dump_die_shallow (gdb_stderr, 0, die);
13930 }
13931
13932 static void
13933 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13934 {
13935   int indent = level * 4;
13936
13937   gdb_assert (die != NULL);
13938
13939   if (level >= max_level)
13940     return;
13941
13942   dump_die_shallow (f, indent, die);
13943
13944   if (die->child != NULL)
13945     {
13946       print_spaces (indent, f);
13947       fprintf_unfiltered (f, "  Children:");
13948       if (level + 1 < max_level)
13949         {
13950           fprintf_unfiltered (f, "\n");
13951           dump_die_1 (f, level + 1, max_level, die->child);
13952         }
13953       else
13954         {
13955           fprintf_unfiltered (f,
13956                               " [not printed, max nesting level reached]\n");
13957         }
13958     }
13959
13960   if (die->sibling != NULL && level > 0)
13961     {
13962       dump_die_1 (f, level, max_level, die->sibling);
13963     }
13964 }
13965
13966 /* This is called from the pdie macro in gdbinit.in.
13967    It's not static so gcc will keep a copy callable from gdb.  */
13968
13969 void
13970 dump_die (struct die_info *die, int max_level)
13971 {
13972   dump_die_1 (gdb_stdlog, 0, max_level, die);
13973 }
13974
13975 static void
13976 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13977 {
13978   void **slot;
13979
13980   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13981
13982   *slot = die;
13983 }
13984
13985 static int
13986 is_ref_attr (struct attribute *attr)
13987 {
13988   switch (attr->form)
13989     {
13990     case DW_FORM_ref_addr:
13991     case DW_FORM_ref1:
13992     case DW_FORM_ref2:
13993     case DW_FORM_ref4:
13994     case DW_FORM_ref8:
13995     case DW_FORM_ref_udata:
13996       return 1;
13997     default:
13998       return 0;
13999     }
14000 }
14001
14002 static unsigned int
14003 dwarf2_get_ref_die_offset (struct attribute *attr)
14004 {
14005   if (is_ref_attr (attr))
14006     return DW_ADDR (attr);
14007
14008   complaint (&symfile_complaints,
14009              _("unsupported die ref attribute form: '%s'"),
14010              dwarf_form_name (attr->form));
14011   return 0;
14012 }
14013
14014 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14015  * the value held by the attribute is not constant.  */
14016
14017 static LONGEST
14018 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14019 {
14020   if (attr->form == DW_FORM_sdata)
14021     return DW_SND (attr);
14022   else if (attr->form == DW_FORM_udata
14023            || attr->form == DW_FORM_data1
14024            || attr->form == DW_FORM_data2
14025            || attr->form == DW_FORM_data4
14026            || attr->form == DW_FORM_data8)
14027     return DW_UNSND (attr);
14028   else
14029     {
14030       complaint (&symfile_complaints,
14031                  _("Attribute value is not a constant (%s)"),
14032                  dwarf_form_name (attr->form));
14033       return default_value;
14034     }
14035 }
14036
14037 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14038    unit and add it to our queue.
14039    The result is non-zero if PER_CU was queued, otherwise the result is zero
14040    meaning either PER_CU is already queued or it is already loaded.  */
14041
14042 static int
14043 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14044                        struct dwarf2_per_cu_data *per_cu)
14045 {
14046   /* We may arrive here during partial symbol reading, if we need full
14047      DIEs to process an unusual case (e.g. template arguments).  Do
14048      not queue PER_CU, just tell our caller to load its DIEs.  */
14049   if (dwarf2_per_objfile->reading_partial_symbols)
14050     {
14051       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14052         return 1;
14053       return 0;
14054     }
14055
14056   /* Mark the dependence relation so that we don't flush PER_CU
14057      too early.  */
14058   dwarf2_add_dependence (this_cu, per_cu);
14059
14060   /* If it's already on the queue, we have nothing to do.  */
14061   if (per_cu->queued)
14062     return 0;
14063
14064   /* If the compilation unit is already loaded, just mark it as
14065      used.  */
14066   if (per_cu->cu != NULL)
14067     {
14068       per_cu->cu->last_used = 0;
14069       return 0;
14070     }
14071
14072   /* Add it to the queue.  */
14073   queue_comp_unit (per_cu);
14074
14075   return 1;
14076 }
14077
14078 /* Follow reference or signature attribute ATTR of SRC_DIE.
14079    On entry *REF_CU is the CU of SRC_DIE.
14080    On exit *REF_CU is the CU of the result.  */
14081
14082 static struct die_info *
14083 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14084                        struct dwarf2_cu **ref_cu)
14085 {
14086   struct die_info *die;
14087
14088   if (is_ref_attr (attr))
14089     die = follow_die_ref (src_die, attr, ref_cu);
14090   else if (attr->form == DW_FORM_ref_sig8)
14091     die = follow_die_sig (src_die, attr, ref_cu);
14092   else
14093     {
14094       dump_die_for_error (src_die);
14095       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14096              (*ref_cu)->objfile->name);
14097     }
14098
14099   return die;
14100 }
14101
14102 /* Follow reference OFFSET.
14103    On entry *REF_CU is the CU of the source die referencing OFFSET.
14104    On exit *REF_CU is the CU of the result.
14105    Returns NULL if OFFSET is invalid.  */
14106
14107 static struct die_info *
14108 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14109 {
14110   struct die_info temp_die;
14111   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14112
14113   gdb_assert (cu->per_cu != NULL);
14114
14115   target_cu = cu;
14116
14117   if (cu->per_cu->debug_types_section)
14118     {
14119       /* .debug_types CUs cannot reference anything outside their CU.
14120          If they need to, they have to reference a signatured type via
14121          DW_FORM_ref_sig8.  */
14122       if (! offset_in_cu_p (&cu->header, offset))
14123         return NULL;
14124     }
14125   else if (! offset_in_cu_p (&cu->header, offset))
14126     {
14127       struct dwarf2_per_cu_data *per_cu;
14128
14129       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14130
14131       /* If necessary, add it to the queue and load its DIEs.  */
14132       if (maybe_queue_comp_unit (cu, per_cu))
14133         load_full_comp_unit (per_cu);
14134
14135       target_cu = per_cu->cu;
14136     }
14137   else if (cu->dies == NULL)
14138     {
14139       /* We're loading full DIEs during partial symbol reading.  */
14140       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14141       load_full_comp_unit (cu->per_cu);
14142     }
14143
14144   *ref_cu = target_cu;
14145   temp_die.offset = offset;
14146   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14147 }
14148
14149 /* Follow reference attribute ATTR of SRC_DIE.
14150    On entry *REF_CU is the CU of SRC_DIE.
14151    On exit *REF_CU is the CU of the result.  */
14152
14153 static struct die_info *
14154 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14155                 struct dwarf2_cu **ref_cu)
14156 {
14157   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14158   struct dwarf2_cu *cu = *ref_cu;
14159   struct die_info *die;
14160
14161   die = follow_die_offset (offset, ref_cu);
14162   if (!die)
14163     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14164            "at 0x%x [in module %s]"),
14165            offset, src_die->offset, cu->objfile->name);
14166
14167   return die;
14168 }
14169
14170 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14171    Returned value is intended for DW_OP_call*.  Returned
14172    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14173
14174 struct dwarf2_locexpr_baton
14175 dwarf2_fetch_die_location_block (unsigned int offset,
14176                                  struct dwarf2_per_cu_data *per_cu,
14177                                  CORE_ADDR (*get_frame_pc) (void *baton),
14178                                  void *baton)
14179 {
14180   struct dwarf2_cu *cu;
14181   struct die_info *die;
14182   struct attribute *attr;
14183   struct dwarf2_locexpr_baton retval;
14184
14185   dw2_setup (per_cu->objfile);
14186
14187   if (per_cu->cu == NULL)
14188     load_cu (per_cu);
14189   cu = per_cu->cu;
14190
14191   die = follow_die_offset (offset, &cu);
14192   if (!die)
14193     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14194            offset, per_cu->objfile->name);
14195
14196   attr = dwarf2_attr (die, DW_AT_location, cu);
14197   if (!attr)
14198     {
14199       /* DWARF: "If there is no such attribute, then there is no effect.".
14200          DATA is ignored if SIZE is 0.  */
14201
14202       retval.data = NULL;
14203       retval.size = 0;
14204     }
14205   else if (attr_form_is_section_offset (attr))
14206     {
14207       struct dwarf2_loclist_baton loclist_baton;
14208       CORE_ADDR pc = (*get_frame_pc) (baton);
14209       size_t size;
14210
14211       fill_in_loclist_baton (cu, &loclist_baton, attr);
14212
14213       retval.data = dwarf2_find_location_expression (&loclist_baton,
14214                                                      &size, pc);
14215       retval.size = size;
14216     }
14217   else
14218     {
14219       if (!attr_form_is_block (attr))
14220         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14221                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14222                offset, per_cu->objfile->name);
14223
14224       retval.data = DW_BLOCK (attr)->data;
14225       retval.size = DW_BLOCK (attr)->size;
14226     }
14227   retval.per_cu = cu->per_cu;
14228
14229   age_cached_comp_units ();
14230
14231   return retval;
14232 }
14233
14234 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14235    PER_CU.  */
14236
14237 struct type *
14238 dwarf2_get_die_type (unsigned int die_offset,
14239                      struct dwarf2_per_cu_data *per_cu)
14240 {
14241   dw2_setup (per_cu->objfile);
14242   return get_die_type_at_offset (die_offset, per_cu);
14243 }
14244
14245 /* Follow the signature attribute ATTR in SRC_DIE.
14246    On entry *REF_CU is the CU of SRC_DIE.
14247    On exit *REF_CU is the CU of the result.  */
14248
14249 static struct die_info *
14250 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14251                 struct dwarf2_cu **ref_cu)
14252 {
14253   struct objfile *objfile = (*ref_cu)->objfile;
14254   struct die_info temp_die;
14255   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14256   struct dwarf2_cu *sig_cu;
14257   struct die_info *die;
14258
14259   /* sig_type will be NULL if the signatured type is missing from
14260      the debug info.  */
14261   if (sig_type == NULL)
14262     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14263              "at 0x%x [in module %s]"),
14264            src_die->offset, objfile->name);
14265
14266   /* If necessary, add it to the queue and load its DIEs.  */
14267
14268   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14269     read_signatured_type (sig_type);
14270
14271   gdb_assert (sig_type->per_cu.cu != NULL);
14272
14273   sig_cu = sig_type->per_cu.cu;
14274   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14275   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14276   if (die)
14277     {
14278       *ref_cu = sig_cu;
14279       return die;
14280     }
14281
14282   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14283          "from DIE at 0x%x [in module %s]"),
14284          sig_type->type_offset, src_die->offset, objfile->name);
14285 }
14286
14287 /* Given an offset of a signatured type, return its signatured_type.  */
14288
14289 static struct signatured_type *
14290 lookup_signatured_type_at_offset (struct objfile *objfile,
14291                                   struct dwarf2_section_info *section,
14292                                   unsigned int offset)
14293 {
14294   gdb_byte *info_ptr = section->buffer + offset;
14295   unsigned int length, initial_length_size;
14296   unsigned int sig_offset;
14297   struct signatured_type find_entry, *type_sig;
14298
14299   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14300   sig_offset = (initial_length_size
14301                 + 2 /*version*/
14302                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14303                 + 1 /*address_size*/);
14304   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14305   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14306
14307   /* This is only used to lookup previously recorded types.
14308      If we didn't find it, it's our bug.  */
14309   gdb_assert (type_sig != NULL);
14310   gdb_assert (offset == type_sig->per_cu.offset);
14311
14312   return type_sig;
14313 }
14314
14315 /* Load the DIEs associated with type unit PER_CU into memory.  */
14316
14317 static void
14318 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14319 {
14320   struct objfile *objfile = per_cu->objfile;
14321   struct dwarf2_section_info *sect = per_cu->debug_types_section;
14322   unsigned int offset = per_cu->offset;
14323   struct signatured_type *type_sig;
14324
14325   dwarf2_read_section (objfile, sect);
14326
14327   /* We have the section offset, but we need the signature to do the
14328      hash table lookup.  */
14329   /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14330      the signature to assert we found the right one.
14331      Ok, but it's a lot of work.  We should simplify things so any needed
14332      assert doesn't require all this clumsiness.  */
14333   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14334
14335   gdb_assert (type_sig->per_cu.cu == NULL);
14336
14337   read_signatured_type (type_sig);
14338
14339   gdb_assert (type_sig->per_cu.cu != NULL);
14340 }
14341
14342 /* Read in a signatured type and build its CU and DIEs.  */
14343
14344 static void
14345 read_signatured_type (struct signatured_type *type_sig)
14346 {
14347   struct objfile *objfile = type_sig->per_cu.objfile;
14348   gdb_byte *types_ptr;
14349   struct die_reader_specs reader_specs;
14350   struct dwarf2_cu *cu;
14351   ULONGEST signature;
14352   struct cleanup *back_to, *free_cu_cleanup;
14353   struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14354
14355   dwarf2_read_section (objfile, section);
14356   types_ptr = section->buffer + type_sig->per_cu.offset;
14357
14358   gdb_assert (type_sig->per_cu.cu == NULL);
14359
14360   cu = xmalloc (sizeof (*cu));
14361   init_one_comp_unit (cu, &type_sig->per_cu);
14362
14363   /* If an error occurs while loading, release our storage.  */
14364   free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14365
14366   types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14367                                              &signature, NULL);
14368   gdb_assert (signature == type_sig->signature);
14369
14370   cu->die_hash
14371     = htab_create_alloc_ex (cu->header.length / 12,
14372                             die_hash,
14373                             die_eq,
14374                             NULL,
14375                             &cu->comp_unit_obstack,
14376                             hashtab_obstack_allocate,
14377                             dummy_obstack_deallocate);
14378
14379   dwarf2_read_abbrevs (cu);
14380   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14381
14382   init_cu_die_reader (&reader_specs, cu);
14383
14384   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14385                                     NULL /*parent*/);
14386
14387   /* We try not to read any attributes in this function, because not
14388      all CUs needed for references have been loaded yet, and symbol
14389      table processing isn't initialized.  But we have to set the CU language,
14390      or we won't be able to build types correctly.  */
14391   prepare_one_comp_unit (cu, cu->dies);
14392
14393   do_cleanups (back_to);
14394
14395   /* We've successfully allocated this compilation unit.  Let our caller
14396      clean it up when finished with it.  */
14397   discard_cleanups (free_cu_cleanup);
14398
14399   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14400   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14401 }
14402
14403 /* Decode simple location descriptions.
14404    Given a pointer to a dwarf block that defines a location, compute
14405    the location and return the value.
14406
14407    NOTE drow/2003-11-18: This function is called in two situations
14408    now: for the address of static or global variables (partial symbols
14409    only) and for offsets into structures which are expected to be
14410    (more or less) constant.  The partial symbol case should go away,
14411    and only the constant case should remain.  That will let this
14412    function complain more accurately.  A few special modes are allowed
14413    without complaint for global variables (for instance, global
14414    register values and thread-local values).
14415
14416    A location description containing no operations indicates that the
14417    object is optimized out.  The return value is 0 for that case.
14418    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14419    callers will only want a very basic result and this can become a
14420    complaint.
14421
14422    Note that stack[0] is unused except as a default error return.  */
14423
14424 static CORE_ADDR
14425 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14426 {
14427   struct objfile *objfile = cu->objfile;
14428   int i;
14429   int size = blk->size;
14430   gdb_byte *data = blk->data;
14431   CORE_ADDR stack[64];
14432   int stacki;
14433   unsigned int bytes_read, unsnd;
14434   gdb_byte op;
14435
14436   i = 0;
14437   stacki = 0;
14438   stack[stacki] = 0;
14439   stack[++stacki] = 0;
14440
14441   while (i < size)
14442     {
14443       op = data[i++];
14444       switch (op)
14445         {
14446         case DW_OP_lit0:
14447         case DW_OP_lit1:
14448         case DW_OP_lit2:
14449         case DW_OP_lit3:
14450         case DW_OP_lit4:
14451         case DW_OP_lit5:
14452         case DW_OP_lit6:
14453         case DW_OP_lit7:
14454         case DW_OP_lit8:
14455         case DW_OP_lit9:
14456         case DW_OP_lit10:
14457         case DW_OP_lit11:
14458         case DW_OP_lit12:
14459         case DW_OP_lit13:
14460         case DW_OP_lit14:
14461         case DW_OP_lit15:
14462         case DW_OP_lit16:
14463         case DW_OP_lit17:
14464         case DW_OP_lit18:
14465         case DW_OP_lit19:
14466         case DW_OP_lit20:
14467         case DW_OP_lit21:
14468         case DW_OP_lit22:
14469         case DW_OP_lit23:
14470         case DW_OP_lit24:
14471         case DW_OP_lit25:
14472         case DW_OP_lit26:
14473         case DW_OP_lit27:
14474         case DW_OP_lit28:
14475         case DW_OP_lit29:
14476         case DW_OP_lit30:
14477         case DW_OP_lit31:
14478           stack[++stacki] = op - DW_OP_lit0;
14479           break;
14480
14481         case DW_OP_reg0:
14482         case DW_OP_reg1:
14483         case DW_OP_reg2:
14484         case DW_OP_reg3:
14485         case DW_OP_reg4:
14486         case DW_OP_reg5:
14487         case DW_OP_reg6:
14488         case DW_OP_reg7:
14489         case DW_OP_reg8:
14490         case DW_OP_reg9:
14491         case DW_OP_reg10:
14492         case DW_OP_reg11:
14493         case DW_OP_reg12:
14494         case DW_OP_reg13:
14495         case DW_OP_reg14:
14496         case DW_OP_reg15:
14497         case DW_OP_reg16:
14498         case DW_OP_reg17:
14499         case DW_OP_reg18:
14500         case DW_OP_reg19:
14501         case DW_OP_reg20:
14502         case DW_OP_reg21:
14503         case DW_OP_reg22:
14504         case DW_OP_reg23:
14505         case DW_OP_reg24:
14506         case DW_OP_reg25:
14507         case DW_OP_reg26:
14508         case DW_OP_reg27:
14509         case DW_OP_reg28:
14510         case DW_OP_reg29:
14511         case DW_OP_reg30:
14512         case DW_OP_reg31:
14513           stack[++stacki] = op - DW_OP_reg0;
14514           if (i < size)
14515             dwarf2_complex_location_expr_complaint ();
14516           break;
14517
14518         case DW_OP_regx:
14519           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14520           i += bytes_read;
14521           stack[++stacki] = unsnd;
14522           if (i < size)
14523             dwarf2_complex_location_expr_complaint ();
14524           break;
14525
14526         case DW_OP_addr:
14527           stack[++stacki] = read_address (objfile->obfd, &data[i],
14528                                           cu, &bytes_read);
14529           i += bytes_read;
14530           break;
14531
14532         case DW_OP_const1u:
14533           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14534           i += 1;
14535           break;
14536
14537         case DW_OP_const1s:
14538           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14539           i += 1;
14540           break;
14541
14542         case DW_OP_const2u:
14543           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14544           i += 2;
14545           break;
14546
14547         case DW_OP_const2s:
14548           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14549           i += 2;
14550           break;
14551
14552         case DW_OP_const4u:
14553           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14554           i += 4;
14555           break;
14556
14557         case DW_OP_const4s:
14558           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14559           i += 4;
14560           break;
14561
14562         case DW_OP_const8u:
14563           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14564           i += 8;
14565           break;
14566
14567         case DW_OP_constu:
14568           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14569                                                   &bytes_read);
14570           i += bytes_read;
14571           break;
14572
14573         case DW_OP_consts:
14574           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14575           i += bytes_read;
14576           break;
14577
14578         case DW_OP_dup:
14579           stack[stacki + 1] = stack[stacki];
14580           stacki++;
14581           break;
14582
14583         case DW_OP_plus:
14584           stack[stacki - 1] += stack[stacki];
14585           stacki--;
14586           break;
14587
14588         case DW_OP_plus_uconst:
14589           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14590                                                  &bytes_read);
14591           i += bytes_read;
14592           break;
14593
14594         case DW_OP_minus:
14595           stack[stacki - 1] -= stack[stacki];
14596           stacki--;
14597           break;
14598
14599         case DW_OP_deref:
14600           /* If we're not the last op, then we definitely can't encode
14601              this using GDB's address_class enum.  This is valid for partial
14602              global symbols, although the variable's address will be bogus
14603              in the psymtab.  */
14604           if (i < size)
14605             dwarf2_complex_location_expr_complaint ();
14606           break;
14607
14608         case DW_OP_GNU_push_tls_address:
14609           /* The top of the stack has the offset from the beginning
14610              of the thread control block at which the variable is located.  */
14611           /* Nothing should follow this operator, so the top of stack would
14612              be returned.  */
14613           /* This is valid for partial global symbols, but the variable's
14614              address will be bogus in the psymtab.  Make it always at least
14615              non-zero to not look as a variable garbage collected by linker
14616              which have DW_OP_addr 0.  */
14617           if (i < size)
14618             dwarf2_complex_location_expr_complaint ();
14619           stack[stacki]++;
14620           break;
14621
14622         case DW_OP_GNU_uninit:
14623           break;
14624
14625         default:
14626           {
14627             const char *name = dwarf_stack_op_name (op);
14628
14629             if (name)
14630               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14631                          name);
14632             else
14633               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14634                          op);
14635           }
14636
14637           return (stack[stacki]);
14638         }
14639
14640       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14641          outside of the allocated space.  Also enforce minimum>0.  */
14642       if (stacki >= ARRAY_SIZE (stack) - 1)
14643         {
14644           complaint (&symfile_complaints,
14645                      _("location description stack overflow"));
14646           return 0;
14647         }
14648
14649       if (stacki <= 0)
14650         {
14651           complaint (&symfile_complaints,
14652                      _("location description stack underflow"));
14653           return 0;
14654         }
14655     }
14656   return (stack[stacki]);
14657 }
14658
14659 /* memory allocation interface */
14660
14661 static struct dwarf_block *
14662 dwarf_alloc_block (struct dwarf2_cu *cu)
14663 {
14664   struct dwarf_block *blk;
14665
14666   blk = (struct dwarf_block *)
14667     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14668   return (blk);
14669 }
14670
14671 static struct abbrev_info *
14672 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14673 {
14674   struct abbrev_info *abbrev;
14675
14676   abbrev = (struct abbrev_info *)
14677     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14678   memset (abbrev, 0, sizeof (struct abbrev_info));
14679   return (abbrev);
14680 }
14681
14682 static struct die_info *
14683 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14684 {
14685   struct die_info *die;
14686   size_t size = sizeof (struct die_info);
14687
14688   if (num_attrs > 1)
14689     size += (num_attrs - 1) * sizeof (struct attribute);
14690
14691   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14692   memset (die, 0, sizeof (struct die_info));
14693   return (die);
14694 }
14695
14696 \f
14697 /* Macro support.  */
14698
14699 /* Return the full name of file number I in *LH's file name table.
14700    Use COMP_DIR as the name of the current directory of the
14701    compilation.  The result is allocated using xmalloc; the caller is
14702    responsible for freeing it.  */
14703 static char *
14704 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14705 {
14706   /* Is the file number a valid index into the line header's file name
14707      table?  Remember that file numbers start with one, not zero.  */
14708   if (1 <= file && file <= lh->num_file_names)
14709     {
14710       struct file_entry *fe = &lh->file_names[file - 1];
14711
14712       if (IS_ABSOLUTE_PATH (fe->name))
14713         return xstrdup (fe->name);
14714       else
14715         {
14716           const char *dir;
14717           int dir_len;
14718           char *full_name;
14719
14720           if (fe->dir_index)
14721             dir = lh->include_dirs[fe->dir_index - 1];
14722           else
14723             dir = comp_dir;
14724
14725           if (dir)
14726             {
14727               dir_len = strlen (dir);
14728               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14729               strcpy (full_name, dir);
14730               full_name[dir_len] = '/';
14731               strcpy (full_name + dir_len + 1, fe->name);
14732               return full_name;
14733             }
14734           else
14735             return xstrdup (fe->name);
14736         }
14737     }
14738   else
14739     {
14740       /* The compiler produced a bogus file number.  We can at least
14741          record the macro definitions made in the file, even if we
14742          won't be able to find the file by name.  */
14743       char fake_name[80];
14744
14745       sprintf (fake_name, "<bad macro file number %d>", file);
14746
14747       complaint (&symfile_complaints,
14748                  _("bad file number in macro information (%d)"),
14749                  file);
14750
14751       return xstrdup (fake_name);
14752     }
14753 }
14754
14755
14756 static struct macro_source_file *
14757 macro_start_file (int file, int line,
14758                   struct macro_source_file *current_file,
14759                   const char *comp_dir,
14760                   struct line_header *lh, struct objfile *objfile)
14761 {
14762   /* The full name of this source file.  */
14763   char *full_name = file_full_name (file, lh, comp_dir);
14764
14765   /* We don't create a macro table for this compilation unit
14766      at all until we actually get a filename.  */
14767   if (! pending_macros)
14768     pending_macros = new_macro_table (&objfile->objfile_obstack,
14769                                       objfile->macro_cache);
14770
14771   if (! current_file)
14772     /* If we have no current file, then this must be the start_file
14773        directive for the compilation unit's main source file.  */
14774     current_file = macro_set_main (pending_macros, full_name);
14775   else
14776     current_file = macro_include (current_file, line, full_name);
14777
14778   xfree (full_name);
14779
14780   return current_file;
14781 }
14782
14783
14784 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14785    followed by a null byte.  */
14786 static char *
14787 copy_string (const char *buf, int len)
14788 {
14789   char *s = xmalloc (len + 1);
14790
14791   memcpy (s, buf, len);
14792   s[len] = '\0';
14793   return s;
14794 }
14795
14796
14797 static const char *
14798 consume_improper_spaces (const char *p, const char *body)
14799 {
14800   if (*p == ' ')
14801     {
14802       complaint (&symfile_complaints,
14803                  _("macro definition contains spaces "
14804                    "in formal argument list:\n`%s'"),
14805                  body);
14806
14807       while (*p == ' ')
14808         p++;
14809     }
14810
14811   return p;
14812 }
14813
14814
14815 static void
14816 parse_macro_definition (struct macro_source_file *file, int line,
14817                         const char *body)
14818 {
14819   const char *p;
14820
14821   /* The body string takes one of two forms.  For object-like macro
14822      definitions, it should be:
14823
14824         <macro name> " " <definition>
14825
14826      For function-like macro definitions, it should be:
14827
14828         <macro name> "() " <definition>
14829      or
14830         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14831
14832      Spaces may appear only where explicitly indicated, and in the
14833      <definition>.
14834
14835      The Dwarf 2 spec says that an object-like macro's name is always
14836      followed by a space, but versions of GCC around March 2002 omit
14837      the space when the macro's definition is the empty string.
14838
14839      The Dwarf 2 spec says that there should be no spaces between the
14840      formal arguments in a function-like macro's formal argument list,
14841      but versions of GCC around March 2002 include spaces after the
14842      commas.  */
14843
14844
14845   /* Find the extent of the macro name.  The macro name is terminated
14846      by either a space or null character (for an object-like macro) or
14847      an opening paren (for a function-like macro).  */
14848   for (p = body; *p; p++)
14849     if (*p == ' ' || *p == '(')
14850       break;
14851
14852   if (*p == ' ' || *p == '\0')
14853     {
14854       /* It's an object-like macro.  */
14855       int name_len = p - body;
14856       char *name = copy_string (body, name_len);
14857       const char *replacement;
14858
14859       if (*p == ' ')
14860         replacement = body + name_len + 1;
14861       else
14862         {
14863           dwarf2_macro_malformed_definition_complaint (body);
14864           replacement = body + name_len;
14865         }
14866
14867       macro_define_object (file, line, name, replacement);
14868
14869       xfree (name);
14870     }
14871   else if (*p == '(')
14872     {
14873       /* It's a function-like macro.  */
14874       char *name = copy_string (body, p - body);
14875       int argc = 0;
14876       int argv_size = 1;
14877       char **argv = xmalloc (argv_size * sizeof (*argv));
14878
14879       p++;
14880
14881       p = consume_improper_spaces (p, body);
14882
14883       /* Parse the formal argument list.  */
14884       while (*p && *p != ')')
14885         {
14886           /* Find the extent of the current argument name.  */
14887           const char *arg_start = p;
14888
14889           while (*p && *p != ',' && *p != ')' && *p != ' ')
14890             p++;
14891
14892           if (! *p || p == arg_start)
14893             dwarf2_macro_malformed_definition_complaint (body);
14894           else
14895             {
14896               /* Make sure argv has room for the new argument.  */
14897               if (argc >= argv_size)
14898                 {
14899                   argv_size *= 2;
14900                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14901                 }
14902
14903               argv[argc++] = copy_string (arg_start, p - arg_start);
14904             }
14905
14906           p = consume_improper_spaces (p, body);
14907
14908           /* Consume the comma, if present.  */
14909           if (*p == ',')
14910             {
14911               p++;
14912
14913               p = consume_improper_spaces (p, body);
14914             }
14915         }
14916
14917       if (*p == ')')
14918         {
14919           p++;
14920
14921           if (*p == ' ')
14922             /* Perfectly formed definition, no complaints.  */
14923             macro_define_function (file, line, name,
14924                                    argc, (const char **) argv,
14925                                    p + 1);
14926           else if (*p == '\0')
14927             {
14928               /* Complain, but do define it.  */
14929               dwarf2_macro_malformed_definition_complaint (body);
14930               macro_define_function (file, line, name,
14931                                      argc, (const char **) argv,
14932                                      p);
14933             }
14934           else
14935             /* Just complain.  */
14936             dwarf2_macro_malformed_definition_complaint (body);
14937         }
14938       else
14939         /* Just complain.  */
14940         dwarf2_macro_malformed_definition_complaint (body);
14941
14942       xfree (name);
14943       {
14944         int i;
14945
14946         for (i = 0; i < argc; i++)
14947           xfree (argv[i]);
14948       }
14949       xfree (argv);
14950     }
14951   else
14952     dwarf2_macro_malformed_definition_complaint (body);
14953 }
14954
14955 /* Skip some bytes from BYTES according to the form given in FORM.
14956    Returns the new pointer.  */
14957
14958 static gdb_byte *
14959 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14960                  enum dwarf_form form,
14961                  unsigned int offset_size,
14962                  struct dwarf2_section_info *section)
14963 {
14964   unsigned int bytes_read;
14965
14966   switch (form)
14967     {
14968     case DW_FORM_data1:
14969     case DW_FORM_flag:
14970       ++bytes;
14971       break;
14972
14973     case DW_FORM_data2:
14974       bytes += 2;
14975       break;
14976
14977     case DW_FORM_data4:
14978       bytes += 4;
14979       break;
14980
14981     case DW_FORM_data8:
14982       bytes += 8;
14983       break;
14984
14985     case DW_FORM_string:
14986       read_direct_string (abfd, bytes, &bytes_read);
14987       bytes += bytes_read;
14988       break;
14989
14990     case DW_FORM_sec_offset:
14991     case DW_FORM_strp:
14992       bytes += offset_size;
14993       break;
14994
14995     case DW_FORM_block:
14996       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14997       bytes += bytes_read;
14998       break;
14999
15000     case DW_FORM_block1:
15001       bytes += 1 + read_1_byte (abfd, bytes);
15002       break;
15003     case DW_FORM_block2:
15004       bytes += 2 + read_2_bytes (abfd, bytes);
15005       break;
15006     case DW_FORM_block4:
15007       bytes += 4 + read_4_bytes (abfd, bytes);
15008       break;
15009
15010     case DW_FORM_sdata:
15011     case DW_FORM_udata:
15012       bytes = skip_leb128 (abfd, bytes);
15013       break;
15014
15015     default:
15016       {
15017       complain:
15018         complaint (&symfile_complaints,
15019                    _("invalid form 0x%x in `%s'"),
15020                    form,
15021                    section->asection->name);
15022         return NULL;
15023       }
15024     }
15025
15026   return bytes;
15027 }
15028
15029 /* A helper for dwarf_decode_macros that handles skipping an unknown
15030    opcode.  Returns an updated pointer to the macro data buffer; or,
15031    on error, issues a complaint and returns NULL.  */
15032
15033 static gdb_byte *
15034 skip_unknown_opcode (unsigned int opcode,
15035                      gdb_byte **opcode_definitions,
15036                      gdb_byte *mac_ptr,
15037                      bfd *abfd,
15038                      unsigned int offset_size,
15039                      struct dwarf2_section_info *section)
15040 {
15041   unsigned int bytes_read, i;
15042   unsigned long arg;
15043   gdb_byte *defn;
15044
15045   if (opcode_definitions[opcode] == NULL)
15046     {
15047       complaint (&symfile_complaints,
15048                  _("unrecognized DW_MACFINO opcode 0x%x"),
15049                  opcode);
15050       return NULL;
15051     }
15052
15053   defn = opcode_definitions[opcode];
15054   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15055   defn += bytes_read;
15056
15057   for (i = 0; i < arg; ++i)
15058     {
15059       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15060       if (mac_ptr == NULL)
15061         {
15062           /* skip_form_bytes already issued the complaint.  */
15063           return NULL;
15064         }
15065     }
15066
15067   return mac_ptr;
15068 }
15069
15070 /* A helper function which parses the header of a macro section.
15071    If the macro section is the extended (for now called "GNU") type,
15072    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15073    the header, or issues a complaint and returns NULL on error.  */
15074
15075 static gdb_byte *
15076 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15077                           bfd *abfd,
15078                           gdb_byte *mac_ptr,
15079                           unsigned int *offset_size,
15080                           int section_is_gnu)
15081 {
15082   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15083
15084   if (section_is_gnu)
15085     {
15086       unsigned int version, flags;
15087
15088       version = read_2_bytes (abfd, mac_ptr);
15089       if (version != 4)
15090         {
15091           complaint (&symfile_complaints,
15092                      _("unrecognized version `%d' in .debug_macro section"),
15093                      version);
15094           return NULL;
15095         }
15096       mac_ptr += 2;
15097
15098       flags = read_1_byte (abfd, mac_ptr);
15099       ++mac_ptr;
15100       *offset_size = (flags & 1) ? 8 : 4;
15101
15102       if ((flags & 2) != 0)
15103         /* We don't need the line table offset.  */
15104         mac_ptr += *offset_size;
15105
15106       /* Vendor opcode descriptions.  */
15107       if ((flags & 4) != 0)
15108         {
15109           unsigned int i, count;
15110
15111           count = read_1_byte (abfd, mac_ptr);
15112           ++mac_ptr;
15113           for (i = 0; i < count; ++i)
15114             {
15115               unsigned int opcode, bytes_read;
15116               unsigned long arg;
15117
15118               opcode = read_1_byte (abfd, mac_ptr);
15119               ++mac_ptr;
15120               opcode_definitions[opcode] = mac_ptr;
15121               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15122               mac_ptr += bytes_read;
15123               mac_ptr += arg;
15124             }
15125         }
15126     }
15127
15128   return mac_ptr;
15129 }
15130
15131 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15132    including DW_MACRO_GNU_transparent_include.  */
15133
15134 static void
15135 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15136                           struct macro_source_file *current_file,
15137                           struct line_header *lh, char *comp_dir,
15138                           struct dwarf2_section_info *section,
15139                           int section_is_gnu,
15140                           unsigned int offset_size,
15141                           struct objfile *objfile,
15142                           htab_t include_hash)
15143 {
15144   enum dwarf_macro_record_type macinfo_type;
15145   int at_commandline;
15146   gdb_byte *opcode_definitions[256];
15147
15148   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15149                                       &offset_size, section_is_gnu);
15150   if (mac_ptr == NULL)
15151     {
15152       /* We already issued a complaint.  */
15153       return;
15154     }
15155
15156   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15157      GDB is still reading the definitions from command line.  First
15158      DW_MACINFO_start_file will need to be ignored as it was already executed
15159      to create CURRENT_FILE for the main source holding also the command line
15160      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15161      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15162
15163   at_commandline = 1;
15164
15165   do
15166     {
15167       /* Do we at least have room for a macinfo type byte?  */
15168       if (mac_ptr >= mac_end)
15169         {
15170           dwarf2_macros_too_long_complaint (section);
15171           break;
15172         }
15173
15174       macinfo_type = read_1_byte (abfd, mac_ptr);
15175       mac_ptr++;
15176
15177       /* Note that we rely on the fact that the corresponding GNU and
15178          DWARF constants are the same.  */
15179       switch (macinfo_type)
15180         {
15181           /* A zero macinfo type indicates the end of the macro
15182              information.  */
15183         case 0:
15184           break;
15185
15186         case DW_MACRO_GNU_define:
15187         case DW_MACRO_GNU_undef:
15188         case DW_MACRO_GNU_define_indirect:
15189         case DW_MACRO_GNU_undef_indirect:
15190           {
15191             unsigned int bytes_read;
15192             int line;
15193             char *body;
15194             int is_define;
15195
15196             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15197             mac_ptr += bytes_read;
15198
15199             if (macinfo_type == DW_MACRO_GNU_define
15200                 || macinfo_type == DW_MACRO_GNU_undef)
15201               {
15202                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15203                 mac_ptr += bytes_read;
15204               }
15205             else
15206               {
15207                 LONGEST str_offset;
15208
15209                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15210                 mac_ptr += offset_size;
15211
15212                 body = read_indirect_string_at_offset (abfd, str_offset);
15213               }
15214
15215             is_define = (macinfo_type == DW_MACRO_GNU_define
15216                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15217             if (! current_file)
15218               {
15219                 /* DWARF violation as no main source is present.  */
15220                 complaint (&symfile_complaints,
15221                            _("debug info with no main source gives macro %s "
15222                              "on line %d: %s"),
15223                            is_define ? _("definition") : _("undefinition"),
15224                            line, body);
15225                 break;
15226               }
15227             if ((line == 0 && !at_commandline)
15228                 || (line != 0 && at_commandline))
15229               complaint (&symfile_complaints,
15230                          _("debug info gives %s macro %s with %s line %d: %s"),
15231                          at_commandline ? _("command-line") : _("in-file"),
15232                          is_define ? _("definition") : _("undefinition"),
15233                          line == 0 ? _("zero") : _("non-zero"), line, body);
15234
15235             if (is_define)
15236               parse_macro_definition (current_file, line, body);
15237             else
15238               {
15239                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15240                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15241                 macro_undef (current_file, line, body);
15242               }
15243           }
15244           break;
15245
15246         case DW_MACRO_GNU_start_file:
15247           {
15248             unsigned int bytes_read;
15249             int line, file;
15250
15251             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15252             mac_ptr += bytes_read;
15253             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15254             mac_ptr += bytes_read;
15255
15256             if ((line == 0 && !at_commandline)
15257                 || (line != 0 && at_commandline))
15258               complaint (&symfile_complaints,
15259                          _("debug info gives source %d included "
15260                            "from %s at %s line %d"),
15261                          file, at_commandline ? _("command-line") : _("file"),
15262                          line == 0 ? _("zero") : _("non-zero"), line);
15263
15264             if (at_commandline)
15265               {
15266                 /* This DW_MACRO_GNU_start_file was executed in the
15267                    pass one.  */
15268                 at_commandline = 0;
15269               }
15270             else
15271               current_file = macro_start_file (file, line,
15272                                                current_file, comp_dir,
15273                                                lh, objfile);
15274           }
15275           break;
15276
15277         case DW_MACRO_GNU_end_file:
15278           if (! current_file)
15279             complaint (&symfile_complaints,
15280                        _("macro debug info has an unmatched "
15281                          "`close_file' directive"));
15282           else
15283             {
15284               current_file = current_file->included_by;
15285               if (! current_file)
15286                 {
15287                   enum dwarf_macro_record_type next_type;
15288
15289                   /* GCC circa March 2002 doesn't produce the zero
15290                      type byte marking the end of the compilation
15291                      unit.  Complain if it's not there, but exit no
15292                      matter what.  */
15293
15294                   /* Do we at least have room for a macinfo type byte?  */
15295                   if (mac_ptr >= mac_end)
15296                     {
15297                       dwarf2_macros_too_long_complaint (section);
15298                       return;
15299                     }
15300
15301                   /* We don't increment mac_ptr here, so this is just
15302                      a look-ahead.  */
15303                   next_type = read_1_byte (abfd, mac_ptr);
15304                   if (next_type != 0)
15305                     complaint (&symfile_complaints,
15306                                _("no terminating 0-type entry for "
15307                                  "macros in `.debug_macinfo' section"));
15308
15309                   return;
15310                 }
15311             }
15312           break;
15313
15314         case DW_MACRO_GNU_transparent_include:
15315           {
15316             LONGEST offset;
15317             void **slot;
15318
15319             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15320             mac_ptr += offset_size;
15321
15322             slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15323             if (*slot != NULL)
15324               {
15325                 /* This has actually happened; see
15326                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
15327                 complaint (&symfile_complaints,
15328                            _("recursive DW_MACRO_GNU_transparent_include in "
15329                              ".debug_macro section"));
15330               }
15331             else
15332               {
15333                 *slot = mac_ptr;
15334
15335                 dwarf_decode_macro_bytes (abfd,
15336                                           section->buffer + offset,
15337                                           mac_end, current_file,
15338                                           lh, comp_dir,
15339                                           section, section_is_gnu,
15340                                           offset_size, objfile, include_hash);
15341
15342                 htab_remove_elt (include_hash, mac_ptr);
15343               }
15344           }
15345           break;
15346
15347         case DW_MACINFO_vendor_ext:
15348           if (!section_is_gnu)
15349             {
15350               unsigned int bytes_read;
15351               int constant;
15352
15353               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15354               mac_ptr += bytes_read;
15355               read_direct_string (abfd, mac_ptr, &bytes_read);
15356               mac_ptr += bytes_read;
15357
15358               /* We don't recognize any vendor extensions.  */
15359               break;
15360             }
15361           /* FALLTHROUGH */
15362
15363         default:
15364           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15365                                          mac_ptr, abfd, offset_size,
15366                                          section);
15367           if (mac_ptr == NULL)
15368             return;
15369           break;
15370         }
15371     } while (macinfo_type != 0);
15372 }
15373
15374 static void
15375 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15376                      char *comp_dir, bfd *abfd,
15377                      struct dwarf2_cu *cu,
15378                      struct dwarf2_section_info *section,
15379                      int section_is_gnu)
15380 {
15381   struct objfile *objfile = dwarf2_per_objfile->objfile;
15382   gdb_byte *mac_ptr, *mac_end;
15383   struct macro_source_file *current_file = 0;
15384   enum dwarf_macro_record_type macinfo_type;
15385   unsigned int offset_size = cu->header.offset_size;
15386   gdb_byte *opcode_definitions[256];
15387   struct cleanup *cleanup;
15388   htab_t include_hash;
15389   void **slot;
15390
15391   dwarf2_read_section (objfile, section);
15392   if (section->buffer == NULL)
15393     {
15394       complaint (&symfile_complaints, _("missing %s section"),
15395                  section->asection->name);
15396       return;
15397     }
15398
15399   /* First pass: Find the name of the base filename.
15400      This filename is needed in order to process all macros whose definition
15401      (or undefinition) comes from the command line.  These macros are defined
15402      before the first DW_MACINFO_start_file entry, and yet still need to be
15403      associated to the base file.
15404
15405      To determine the base file name, we scan the macro definitions until we
15406      reach the first DW_MACINFO_start_file entry.  We then initialize
15407      CURRENT_FILE accordingly so that any macro definition found before the
15408      first DW_MACINFO_start_file can still be associated to the base file.  */
15409
15410   mac_ptr = section->buffer + offset;
15411   mac_end = section->buffer + section->size;
15412
15413   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15414                                       &offset_size, section_is_gnu);
15415   if (mac_ptr == NULL)
15416     {
15417       /* We already issued a complaint.  */
15418       return;
15419     }
15420
15421   do
15422     {
15423       /* Do we at least have room for a macinfo type byte?  */
15424       if (mac_ptr >= mac_end)
15425         {
15426           /* Complaint is printed during the second pass as GDB will probably
15427              stop the first pass earlier upon finding
15428              DW_MACINFO_start_file.  */
15429           break;
15430         }
15431
15432       macinfo_type = read_1_byte (abfd, mac_ptr);
15433       mac_ptr++;
15434
15435       /* Note that we rely on the fact that the corresponding GNU and
15436          DWARF constants are the same.  */
15437       switch (macinfo_type)
15438         {
15439           /* A zero macinfo type indicates the end of the macro
15440              information.  */
15441         case 0:
15442           break;
15443
15444         case DW_MACRO_GNU_define:
15445         case DW_MACRO_GNU_undef:
15446           /* Only skip the data by MAC_PTR.  */
15447           {
15448             unsigned int bytes_read;
15449
15450             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15451             mac_ptr += bytes_read;
15452             read_direct_string (abfd, mac_ptr, &bytes_read);
15453             mac_ptr += bytes_read;
15454           }
15455           break;
15456
15457         case DW_MACRO_GNU_start_file:
15458           {
15459             unsigned int bytes_read;
15460             int line, file;
15461
15462             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15463             mac_ptr += bytes_read;
15464             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15465             mac_ptr += bytes_read;
15466
15467             current_file = macro_start_file (file, line, current_file,
15468                                              comp_dir, lh, objfile);
15469           }
15470           break;
15471
15472         case DW_MACRO_GNU_end_file:
15473           /* No data to skip by MAC_PTR.  */
15474           break;
15475
15476         case DW_MACRO_GNU_define_indirect:
15477         case DW_MACRO_GNU_undef_indirect:
15478           {
15479             unsigned int bytes_read;
15480
15481             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15482             mac_ptr += bytes_read;
15483             mac_ptr += offset_size;
15484           }
15485           break;
15486
15487         case DW_MACRO_GNU_transparent_include:
15488           /* Note that, according to the spec, a transparent include
15489              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15490              skip this opcode.  */
15491           mac_ptr += offset_size;
15492           break;
15493
15494         case DW_MACINFO_vendor_ext:
15495           /* Only skip the data by MAC_PTR.  */
15496           if (!section_is_gnu)
15497             {
15498               unsigned int bytes_read;
15499
15500               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15501               mac_ptr += bytes_read;
15502               read_direct_string (abfd, mac_ptr, &bytes_read);
15503               mac_ptr += bytes_read;
15504             }
15505           /* FALLTHROUGH */
15506
15507         default:
15508           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15509                                          mac_ptr, abfd, offset_size,
15510                                          section);
15511           if (mac_ptr == NULL)
15512             return;
15513           break;
15514         }
15515     } while (macinfo_type != 0 && current_file == NULL);
15516
15517   /* Second pass: Process all entries.
15518
15519      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15520      command-line macro definitions/undefinitions.  This flag is unset when we
15521      reach the first DW_MACINFO_start_file entry.  */
15522
15523   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15524                                     NULL, xcalloc, xfree);
15525   cleanup = make_cleanup_htab_delete (include_hash);
15526   mac_ptr = section->buffer + offset;
15527   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15528   *slot = mac_ptr;
15529   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15530                             current_file, lh, comp_dir, section, section_is_gnu,
15531                             offset_size, objfile, include_hash);
15532   do_cleanups (cleanup);
15533 }
15534
15535 /* Check if the attribute's form is a DW_FORM_block*
15536    if so return true else false.  */
15537 static int
15538 attr_form_is_block (struct attribute *attr)
15539 {
15540   return (attr == NULL ? 0 :
15541       attr->form == DW_FORM_block1
15542       || attr->form == DW_FORM_block2
15543       || attr->form == DW_FORM_block4
15544       || attr->form == DW_FORM_block
15545       || attr->form == DW_FORM_exprloc);
15546 }
15547
15548 /* Return non-zero if ATTR's value is a section offset --- classes
15549    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15550    You may use DW_UNSND (attr) to retrieve such offsets.
15551
15552    Section 7.5.4, "Attribute Encodings", explains that no attribute
15553    may have a value that belongs to more than one of these classes; it
15554    would be ambiguous if we did, because we use the same forms for all
15555    of them.  */
15556 static int
15557 attr_form_is_section_offset (struct attribute *attr)
15558 {
15559   return (attr->form == DW_FORM_data4
15560           || attr->form == DW_FORM_data8
15561           || attr->form == DW_FORM_sec_offset);
15562 }
15563
15564
15565 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15566    zero otherwise.  When this function returns true, you can apply
15567    dwarf2_get_attr_constant_value to it.
15568
15569    However, note that for some attributes you must check
15570    attr_form_is_section_offset before using this test.  DW_FORM_data4
15571    and DW_FORM_data8 are members of both the constant class, and of
15572    the classes that contain offsets into other debug sections
15573    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15574    that, if an attribute's can be either a constant or one of the
15575    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15576    taken as section offsets, not constants.  */
15577 static int
15578 attr_form_is_constant (struct attribute *attr)
15579 {
15580   switch (attr->form)
15581     {
15582     case DW_FORM_sdata:
15583     case DW_FORM_udata:
15584     case DW_FORM_data1:
15585     case DW_FORM_data2:
15586     case DW_FORM_data4:
15587     case DW_FORM_data8:
15588       return 1;
15589     default:
15590       return 0;
15591     }
15592 }
15593
15594 /* A helper function that fills in a dwarf2_loclist_baton.  */
15595
15596 static void
15597 fill_in_loclist_baton (struct dwarf2_cu *cu,
15598                        struct dwarf2_loclist_baton *baton,
15599                        struct attribute *attr)
15600 {
15601   dwarf2_read_section (dwarf2_per_objfile->objfile,
15602                        &dwarf2_per_objfile->loc);
15603
15604   baton->per_cu = cu->per_cu;
15605   gdb_assert (baton->per_cu);
15606   /* We don't know how long the location list is, but make sure we
15607      don't run off the edge of the section.  */
15608   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15609   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15610   baton->base_address = cu->base_address;
15611 }
15612
15613 static void
15614 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15615                              struct dwarf2_cu *cu)
15616 {
15617   struct objfile *objfile = dwarf2_per_objfile->objfile;
15618
15619   if (attr_form_is_section_offset (attr)
15620       /* ".debug_loc" may not exist at all, or the offset may be outside
15621          the section.  If so, fall through to the complaint in the
15622          other branch.  */
15623       && DW_UNSND (attr) < dwarf2_section_size (objfile,
15624                                                 &dwarf2_per_objfile->loc))
15625     {
15626       struct dwarf2_loclist_baton *baton;
15627
15628       baton = obstack_alloc (&objfile->objfile_obstack,
15629                              sizeof (struct dwarf2_loclist_baton));
15630
15631       fill_in_loclist_baton (cu, baton, attr);
15632
15633       if (cu->base_known == 0)
15634         complaint (&symfile_complaints,
15635                    _("Location list used without "
15636                      "specifying the CU base address."));
15637
15638       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15639       SYMBOL_LOCATION_BATON (sym) = baton;
15640     }
15641   else
15642     {
15643       struct dwarf2_locexpr_baton *baton;
15644
15645       baton = obstack_alloc (&objfile->objfile_obstack,
15646                              sizeof (struct dwarf2_locexpr_baton));
15647       baton->per_cu = cu->per_cu;
15648       gdb_assert (baton->per_cu);
15649
15650       if (attr_form_is_block (attr))
15651         {
15652           /* Note that we're just copying the block's data pointer
15653              here, not the actual data.  We're still pointing into the
15654              info_buffer for SYM's objfile; right now we never release
15655              that buffer, but when we do clean up properly this may
15656              need to change.  */
15657           baton->size = DW_BLOCK (attr)->size;
15658           baton->data = DW_BLOCK (attr)->data;
15659         }
15660       else
15661         {
15662           dwarf2_invalid_attrib_class_complaint ("location description",
15663                                                  SYMBOL_NATURAL_NAME (sym));
15664           baton->size = 0;
15665         }
15666
15667       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15668       SYMBOL_LOCATION_BATON (sym) = baton;
15669     }
15670 }
15671
15672 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15673    came from a separate debuginfo file, then the master objfile is
15674    returned.  */
15675
15676 struct objfile *
15677 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15678 {
15679   struct objfile *objfile = per_cu->objfile;
15680
15681   /* Return the master objfile, so that we can report and look up the
15682      correct file containing this variable.  */
15683   if (objfile->separate_debug_objfile_backlink)
15684     objfile = objfile->separate_debug_objfile_backlink;
15685
15686   return objfile;
15687 }
15688
15689 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15690    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15691    CU_HEADERP first.  */
15692
15693 static const struct comp_unit_head *
15694 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15695                        struct dwarf2_per_cu_data *per_cu)
15696 {
15697   struct objfile *objfile;
15698   struct dwarf2_per_objfile *per_objfile;
15699   gdb_byte *info_ptr;
15700
15701   if (per_cu->cu)
15702     return &per_cu->cu->header;
15703
15704   objfile = per_cu->objfile;
15705   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15706   info_ptr = per_objfile->info.buffer + per_cu->offset;
15707
15708   memset (cu_headerp, 0, sizeof (*cu_headerp));
15709   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15710
15711   return cu_headerp;
15712 }
15713
15714 /* Return the address size given in the compilation unit header for CU.  */
15715
15716 int
15717 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15718 {
15719   struct comp_unit_head cu_header_local;
15720   const struct comp_unit_head *cu_headerp;
15721
15722   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15723
15724   return cu_headerp->addr_size;
15725 }
15726
15727 /* Return the offset size given in the compilation unit header for CU.  */
15728
15729 int
15730 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15731 {
15732   struct comp_unit_head cu_header_local;
15733   const struct comp_unit_head *cu_headerp;
15734
15735   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15736
15737   return cu_headerp->offset_size;
15738 }
15739
15740 /* See its dwarf2loc.h declaration.  */
15741
15742 int
15743 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15744 {
15745   struct comp_unit_head cu_header_local;
15746   const struct comp_unit_head *cu_headerp;
15747
15748   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15749
15750   if (cu_headerp->version == 2)
15751     return cu_headerp->addr_size;
15752   else
15753     return cu_headerp->offset_size;
15754 }
15755
15756 /* Return the text offset of the CU.  The returned offset comes from
15757    this CU's objfile.  If this objfile came from a separate debuginfo
15758    file, then the offset may be different from the corresponding
15759    offset in the parent objfile.  */
15760
15761 CORE_ADDR
15762 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15763 {
15764   struct objfile *objfile = per_cu->objfile;
15765
15766   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15767 }
15768
15769 /* Locate the .debug_info compilation unit from CU's objfile which contains
15770    the DIE at OFFSET.  Raises an error on failure.  */
15771
15772 static struct dwarf2_per_cu_data *
15773 dwarf2_find_containing_comp_unit (unsigned int offset,
15774                                   struct objfile *objfile)
15775 {
15776   struct dwarf2_per_cu_data *this_cu;
15777   int low, high;
15778
15779   low = 0;
15780   high = dwarf2_per_objfile->n_comp_units - 1;
15781   while (high > low)
15782     {
15783       int mid = low + (high - low) / 2;
15784
15785       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15786         high = mid;
15787       else
15788         low = mid + 1;
15789     }
15790   gdb_assert (low == high);
15791   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15792     {
15793       if (low == 0)
15794         error (_("Dwarf Error: could not find partial DIE containing "
15795                "offset 0x%lx [in module %s]"),
15796                (long) offset, bfd_get_filename (objfile->obfd));
15797
15798       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15799       return dwarf2_per_objfile->all_comp_units[low-1];
15800     }
15801   else
15802     {
15803       this_cu = dwarf2_per_objfile->all_comp_units[low];
15804       if (low == dwarf2_per_objfile->n_comp_units - 1
15805           && offset >= this_cu->offset + this_cu->length)
15806         error (_("invalid dwarf2 offset %u"), offset);
15807       gdb_assert (offset < this_cu->offset + this_cu->length);
15808       return this_cu;
15809     }
15810 }
15811
15812 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
15813
15814 static void
15815 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15816 {
15817   memset (cu, 0, sizeof (*cu));
15818   per_cu->cu = cu;
15819   cu->per_cu = per_cu;
15820   cu->objfile = per_cu->objfile;
15821   obstack_init (&cu->comp_unit_obstack);
15822 }
15823
15824 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15825
15826 static void
15827 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15828 {
15829   struct attribute *attr;
15830
15831   /* Set the language we're debugging.  */
15832   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15833   if (attr)
15834     set_cu_language (DW_UNSND (attr), cu);
15835   else
15836     {
15837       cu->language = language_minimal;
15838       cu->language_defn = language_def (cu->language);
15839     }
15840 }
15841
15842 /* Release one cached compilation unit, CU.  We unlink it from the tree
15843    of compilation units, but we don't remove it from the read_in_chain;
15844    the caller is responsible for that.
15845    NOTE: DATA is a void * because this function is also used as a
15846    cleanup routine.  */
15847
15848 static void
15849 free_heap_comp_unit (void *data)
15850 {
15851   struct dwarf2_cu *cu = data;
15852
15853   gdb_assert (cu->per_cu != NULL);
15854   cu->per_cu->cu = NULL;
15855   cu->per_cu = NULL;
15856
15857   obstack_free (&cu->comp_unit_obstack, NULL);
15858
15859   xfree (cu);
15860 }
15861
15862 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15863    when we're finished with it.  We can't free the pointer itself, but be
15864    sure to unlink it from the cache.  Also release any associated storage
15865    and perform cache maintenance.
15866
15867    Only used during partial symbol parsing.  */
15868
15869 static void
15870 free_stack_comp_unit (void *data)
15871 {
15872   struct dwarf2_cu *cu = data;
15873
15874   gdb_assert (cu->per_cu != NULL);
15875   cu->per_cu->cu = NULL;
15876   cu->per_cu = NULL;
15877
15878   obstack_free (&cu->comp_unit_obstack, NULL);
15879   cu->partial_dies = NULL;
15880
15881   /* The previous code only did this if per_cu != NULL.
15882      But that would always succeed, so now we just unconditionally do
15883      the aging.  This seems like the wrong place to do such aging,
15884      but cleaning that up is left for later.  */
15885   age_cached_comp_units ();
15886 }
15887
15888 /* Free all cached compilation units.  */
15889
15890 static void
15891 free_cached_comp_units (void *data)
15892 {
15893   struct dwarf2_per_cu_data *per_cu, **last_chain;
15894
15895   per_cu = dwarf2_per_objfile->read_in_chain;
15896   last_chain = &dwarf2_per_objfile->read_in_chain;
15897   while (per_cu != NULL)
15898     {
15899       struct dwarf2_per_cu_data *next_cu;
15900
15901       next_cu = per_cu->cu->read_in_chain;
15902
15903       free_heap_comp_unit (per_cu->cu);
15904       *last_chain = next_cu;
15905
15906       per_cu = next_cu;
15907     }
15908 }
15909
15910 /* Increase the age counter on each cached compilation unit, and free
15911    any that are too old.  */
15912
15913 static void
15914 age_cached_comp_units (void)
15915 {
15916   struct dwarf2_per_cu_data *per_cu, **last_chain;
15917
15918   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15919   per_cu = dwarf2_per_objfile->read_in_chain;
15920   while (per_cu != NULL)
15921     {
15922       per_cu->cu->last_used ++;
15923       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15924         dwarf2_mark (per_cu->cu);
15925       per_cu = per_cu->cu->read_in_chain;
15926     }
15927
15928   per_cu = dwarf2_per_objfile->read_in_chain;
15929   last_chain = &dwarf2_per_objfile->read_in_chain;
15930   while (per_cu != NULL)
15931     {
15932       struct dwarf2_per_cu_data *next_cu;
15933
15934       next_cu = per_cu->cu->read_in_chain;
15935
15936       if (!per_cu->cu->mark)
15937         {
15938           free_heap_comp_unit (per_cu->cu);
15939           *last_chain = next_cu;
15940         }
15941       else
15942         last_chain = &per_cu->cu->read_in_chain;
15943
15944       per_cu = next_cu;
15945     }
15946 }
15947
15948 /* Remove a single compilation unit from the cache.  */
15949
15950 static void
15951 free_one_cached_comp_unit (void *target_cu)
15952 {
15953   struct dwarf2_per_cu_data *per_cu, **last_chain;
15954
15955   per_cu = dwarf2_per_objfile->read_in_chain;
15956   last_chain = &dwarf2_per_objfile->read_in_chain;
15957   while (per_cu != NULL)
15958     {
15959       struct dwarf2_per_cu_data *next_cu;
15960
15961       next_cu = per_cu->cu->read_in_chain;
15962
15963       if (per_cu->cu == target_cu)
15964         {
15965           free_heap_comp_unit (per_cu->cu);
15966           *last_chain = next_cu;
15967           break;
15968         }
15969       else
15970         last_chain = &per_cu->cu->read_in_chain;
15971
15972       per_cu = next_cu;
15973     }
15974 }
15975
15976 /* Release all extra memory associated with OBJFILE.  */
15977
15978 void
15979 dwarf2_free_objfile (struct objfile *objfile)
15980 {
15981   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15982
15983   if (dwarf2_per_objfile == NULL)
15984     return;
15985
15986   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
15987   free_cached_comp_units (NULL);
15988
15989   if (dwarf2_per_objfile->quick_file_names_table)
15990     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15991
15992   /* Everything else should be on the objfile obstack.  */
15993 }
15994
15995 /* A pair of DIE offset and GDB type pointer.  We store these
15996    in a hash table separate from the DIEs, and preserve them
15997    when the DIEs are flushed out of cache.  */
15998
15999 struct dwarf2_offset_and_type
16000 {
16001   unsigned int offset;
16002   struct type *type;
16003 };
16004
16005 /* Hash function for a dwarf2_offset_and_type.  */
16006
16007 static hashval_t
16008 offset_and_type_hash (const void *item)
16009 {
16010   const struct dwarf2_offset_and_type *ofs = item;
16011
16012   return ofs->offset;
16013 }
16014
16015 /* Equality function for a dwarf2_offset_and_type.  */
16016
16017 static int
16018 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16019 {
16020   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16021   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16022
16023   return ofs_lhs->offset == ofs_rhs->offset;
16024 }
16025
16026 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16027    table if necessary.  For convenience, return TYPE.
16028
16029    The DIEs reading must have careful ordering to:
16030     * Not cause infite loops trying to read in DIEs as a prerequisite for
16031       reading current DIE.
16032     * Not trying to dereference contents of still incompletely read in types
16033       while reading in other DIEs.
16034     * Enable referencing still incompletely read in types just by a pointer to
16035       the type without accessing its fields.
16036
16037    Therefore caller should follow these rules:
16038      * Try to fetch any prerequisite types we may need to build this DIE type
16039        before building the type and calling set_die_type.
16040      * After building type call set_die_type for current DIE as soon as
16041        possible before fetching more types to complete the current type.
16042      * Make the type as complete as possible before fetching more types.  */
16043
16044 static struct type *
16045 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16046 {
16047   struct dwarf2_offset_and_type **slot, ofs;
16048   struct objfile *objfile = cu->objfile;
16049   htab_t *type_hash_ptr;
16050
16051   /* For Ada types, make sure that the gnat-specific data is always
16052      initialized (if not already set).  There are a few types where
16053      we should not be doing so, because the type-specific area is
16054      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16055      where the type-specific area is used to store the floatformat).
16056      But this is not a problem, because the gnat-specific information
16057      is actually not needed for these types.  */
16058   if (need_gnat_info (cu)
16059       && TYPE_CODE (type) != TYPE_CODE_FUNC
16060       && TYPE_CODE (type) != TYPE_CODE_FLT
16061       && !HAVE_GNAT_AUX_INFO (type))
16062     INIT_GNAT_SPECIFIC (type);
16063
16064   if (cu->per_cu->debug_types_section)
16065     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16066   else
16067     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16068
16069   if (*type_hash_ptr == NULL)
16070     {
16071       *type_hash_ptr
16072         = htab_create_alloc_ex (127,
16073                                 offset_and_type_hash,
16074                                 offset_and_type_eq,
16075                                 NULL,
16076                                 &objfile->objfile_obstack,
16077                                 hashtab_obstack_allocate,
16078                                 dummy_obstack_deallocate);
16079     }
16080
16081   ofs.offset = die->offset;
16082   ofs.type = type;
16083   slot = (struct dwarf2_offset_and_type **)
16084     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16085   if (*slot)
16086     complaint (&symfile_complaints,
16087                _("A problem internal to GDB: DIE 0x%x has type already set"),
16088                die->offset);
16089   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16090   **slot = ofs;
16091   return type;
16092 }
16093
16094 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16095    table, or return NULL if the die does not have a saved type.  */
16096
16097 static struct type *
16098 get_die_type_at_offset (unsigned int offset,
16099                         struct dwarf2_per_cu_data *per_cu)
16100 {
16101   struct dwarf2_offset_and_type *slot, ofs;
16102   htab_t type_hash;
16103
16104   if (per_cu->debug_types_section)
16105     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16106   else
16107     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16108   if (type_hash == NULL)
16109     return NULL;
16110
16111   ofs.offset = offset;
16112   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16113   if (slot)
16114     return slot->type;
16115   else
16116     return NULL;
16117 }
16118
16119 /* Look up the type for DIE in the appropriate type_hash table,
16120    or return NULL if DIE does not have a saved type.  */
16121
16122 static struct type *
16123 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16124 {
16125   return get_die_type_at_offset (die->offset, cu->per_cu);
16126 }
16127
16128 /* Add a dependence relationship from CU to REF_PER_CU.  */
16129
16130 static void
16131 dwarf2_add_dependence (struct dwarf2_cu *cu,
16132                        struct dwarf2_per_cu_data *ref_per_cu)
16133 {
16134   void **slot;
16135
16136   if (cu->dependencies == NULL)
16137     cu->dependencies
16138       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16139                               NULL, &cu->comp_unit_obstack,
16140                               hashtab_obstack_allocate,
16141                               dummy_obstack_deallocate);
16142
16143   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16144   if (*slot == NULL)
16145     *slot = ref_per_cu;
16146 }
16147
16148 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16149    Set the mark field in every compilation unit in the
16150    cache that we must keep because we are keeping CU.  */
16151
16152 static int
16153 dwarf2_mark_helper (void **slot, void *data)
16154 {
16155   struct dwarf2_per_cu_data *per_cu;
16156
16157   per_cu = (struct dwarf2_per_cu_data *) *slot;
16158
16159   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16160      reading of the chain.  As such dependencies remain valid it is not much
16161      useful to track and undo them during QUIT cleanups.  */
16162   if (per_cu->cu == NULL)
16163     return 1;
16164
16165   if (per_cu->cu->mark)
16166     return 1;
16167   per_cu->cu->mark = 1;
16168
16169   if (per_cu->cu->dependencies != NULL)
16170     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16171
16172   return 1;
16173 }
16174
16175 /* Set the mark field in CU and in every other compilation unit in the
16176    cache that we must keep because we are keeping CU.  */
16177
16178 static void
16179 dwarf2_mark (struct dwarf2_cu *cu)
16180 {
16181   if (cu->mark)
16182     return;
16183   cu->mark = 1;
16184   if (cu->dependencies != NULL)
16185     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16186 }
16187
16188 static void
16189 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16190 {
16191   while (per_cu)
16192     {
16193       per_cu->cu->mark = 0;
16194       per_cu = per_cu->cu->read_in_chain;
16195     }
16196 }
16197
16198 /* Trivial hash function for partial_die_info: the hash value of a DIE
16199    is its offset in .debug_info for this objfile.  */
16200
16201 static hashval_t
16202 partial_die_hash (const void *item)
16203 {
16204   const struct partial_die_info *part_die = item;
16205
16206   return part_die->offset;
16207 }
16208
16209 /* Trivial comparison function for partial_die_info structures: two DIEs
16210    are equal if they have the same offset.  */
16211
16212 static int
16213 partial_die_eq (const void *item_lhs, const void *item_rhs)
16214 {
16215   const struct partial_die_info *part_die_lhs = item_lhs;
16216   const struct partial_die_info *part_die_rhs = item_rhs;
16217
16218   return part_die_lhs->offset == part_die_rhs->offset;
16219 }
16220
16221 static struct cmd_list_element *set_dwarf2_cmdlist;
16222 static struct cmd_list_element *show_dwarf2_cmdlist;
16223
16224 static void
16225 set_dwarf2_cmd (char *args, int from_tty)
16226 {
16227   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16228 }
16229
16230 static void
16231 show_dwarf2_cmd (char *args, int from_tty)
16232 {
16233   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16234 }
16235
16236 /* If section described by INFO was mmapped, munmap it now.  */
16237
16238 static void
16239 munmap_section_buffer (struct dwarf2_section_info *info)
16240 {
16241   if (info->map_addr != NULL)
16242     {
16243 #ifdef HAVE_MMAP
16244       int res;
16245
16246       res = munmap (info->map_addr, info->map_len);
16247       gdb_assert (res == 0);
16248 #else
16249       /* Without HAVE_MMAP, we should never be here to begin with.  */
16250       gdb_assert_not_reached ("no mmap support");
16251 #endif
16252     }
16253 }
16254
16255 /* munmap debug sections for OBJFILE, if necessary.  */
16256
16257 static void
16258 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16259 {
16260   struct dwarf2_per_objfile *data = d;
16261   int ix;
16262   struct dwarf2_section_info *section;
16263
16264   /* This is sorted according to the order they're defined in to make it easier
16265      to keep in sync.  */
16266   munmap_section_buffer (&data->info);
16267   munmap_section_buffer (&data->abbrev);
16268   munmap_section_buffer (&data->line);
16269   munmap_section_buffer (&data->loc);
16270   munmap_section_buffer (&data->macinfo);
16271   munmap_section_buffer (&data->macro);
16272   munmap_section_buffer (&data->str);
16273   munmap_section_buffer (&data->ranges);
16274   munmap_section_buffer (&data->frame);
16275   munmap_section_buffer (&data->eh_frame);
16276   munmap_section_buffer (&data->gdb_index);
16277
16278   for (ix = 0;
16279        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16280        ++ix)
16281     munmap_section_buffer (section);
16282
16283   VEC_free (dwarf2_section_info_def, data->types);
16284 }
16285
16286 \f
16287 /* The "save gdb-index" command.  */
16288
16289 /* The contents of the hash table we create when building the string
16290    table.  */
16291 struct strtab_entry
16292 {
16293   offset_type offset;
16294   const char *str;
16295 };
16296
16297 /* Hash function for a strtab_entry.
16298
16299    Function is used only during write_hash_table so no index format backward
16300    compatibility is needed.  */
16301
16302 static hashval_t
16303 hash_strtab_entry (const void *e)
16304 {
16305   const struct strtab_entry *entry = e;
16306   return mapped_index_string_hash (INT_MAX, entry->str);
16307 }
16308
16309 /* Equality function for a strtab_entry.  */
16310
16311 static int
16312 eq_strtab_entry (const void *a, const void *b)
16313 {
16314   const struct strtab_entry *ea = a;
16315   const struct strtab_entry *eb = b;
16316   return !strcmp (ea->str, eb->str);
16317 }
16318
16319 /* Create a strtab_entry hash table.  */
16320
16321 static htab_t
16322 create_strtab (void)
16323 {
16324   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16325                             xfree, xcalloc, xfree);
16326 }
16327
16328 /* Add a string to the constant pool.  Return the string's offset in
16329    host order.  */
16330
16331 static offset_type
16332 add_string (htab_t table, struct obstack *cpool, const char *str)
16333 {
16334   void **slot;
16335   struct strtab_entry entry;
16336   struct strtab_entry *result;
16337
16338   entry.str = str;
16339   slot = htab_find_slot (table, &entry, INSERT);
16340   if (*slot)
16341     result = *slot;
16342   else
16343     {
16344       result = XNEW (struct strtab_entry);
16345       result->offset = obstack_object_size (cpool);
16346       result->str = str;
16347       obstack_grow_str0 (cpool, str);
16348       *slot = result;
16349     }
16350   return result->offset;
16351 }
16352
16353 /* An entry in the symbol table.  */
16354 struct symtab_index_entry
16355 {
16356   /* The name of the symbol.  */
16357   const char *name;
16358   /* The offset of the name in the constant pool.  */
16359   offset_type index_offset;
16360   /* A sorted vector of the indices of all the CUs that hold an object
16361      of this name.  */
16362   VEC (offset_type) *cu_indices;
16363 };
16364
16365 /* The symbol table.  This is a power-of-2-sized hash table.  */
16366 struct mapped_symtab
16367 {
16368   offset_type n_elements;
16369   offset_type size;
16370   struct symtab_index_entry **data;
16371 };
16372
16373 /* Hash function for a symtab_index_entry.  */
16374
16375 static hashval_t
16376 hash_symtab_entry (const void *e)
16377 {
16378   const struct symtab_index_entry *entry = e;
16379   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16380                          sizeof (offset_type) * VEC_length (offset_type,
16381                                                             entry->cu_indices),
16382                          0);
16383 }
16384
16385 /* Equality function for a symtab_index_entry.  */
16386
16387 static int
16388 eq_symtab_entry (const void *a, const void *b)
16389 {
16390   const struct symtab_index_entry *ea = a;
16391   const struct symtab_index_entry *eb = b;
16392   int len = VEC_length (offset_type, ea->cu_indices);
16393   if (len != VEC_length (offset_type, eb->cu_indices))
16394     return 0;
16395   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16396                   VEC_address (offset_type, eb->cu_indices),
16397                   sizeof (offset_type) * len);
16398 }
16399
16400 /* Destroy a symtab_index_entry.  */
16401
16402 static void
16403 delete_symtab_entry (void *p)
16404 {
16405   struct symtab_index_entry *entry = p;
16406   VEC_free (offset_type, entry->cu_indices);
16407   xfree (entry);
16408 }
16409
16410 /* Create a hash table holding symtab_index_entry objects.  */
16411
16412 static htab_t
16413 create_symbol_hash_table (void)
16414 {
16415   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16416                             delete_symtab_entry, xcalloc, xfree);
16417 }
16418
16419 /* Create a new mapped symtab object.  */
16420
16421 static struct mapped_symtab *
16422 create_mapped_symtab (void)
16423 {
16424   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16425   symtab->n_elements = 0;
16426   symtab->size = 1024;
16427   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16428   return symtab;
16429 }
16430
16431 /* Destroy a mapped_symtab.  */
16432
16433 static void
16434 cleanup_mapped_symtab (void *p)
16435 {
16436   struct mapped_symtab *symtab = p;
16437   /* The contents of the array are freed when the other hash table is
16438      destroyed.  */
16439   xfree (symtab->data);
16440   xfree (symtab);
16441 }
16442
16443 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16444    the slot.
16445    
16446    Function is used only during write_hash_table so no index format backward
16447    compatibility is needed.  */
16448
16449 static struct symtab_index_entry **
16450 find_slot (struct mapped_symtab *symtab, const char *name)
16451 {
16452   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16453
16454   index = hash & (symtab->size - 1);
16455   step = ((hash * 17) & (symtab->size - 1)) | 1;
16456
16457   for (;;)
16458     {
16459       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16460         return &symtab->data[index];
16461       index = (index + step) & (symtab->size - 1);
16462     }
16463 }
16464
16465 /* Expand SYMTAB's hash table.  */
16466
16467 static void
16468 hash_expand (struct mapped_symtab *symtab)
16469 {
16470   offset_type old_size = symtab->size;
16471   offset_type i;
16472   struct symtab_index_entry **old_entries = symtab->data;
16473
16474   symtab->size *= 2;
16475   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16476
16477   for (i = 0; i < old_size; ++i)
16478     {
16479       if (old_entries[i])
16480         {
16481           struct symtab_index_entry **slot = find_slot (symtab,
16482                                                         old_entries[i]->name);
16483           *slot = old_entries[i];
16484         }
16485     }
16486
16487   xfree (old_entries);
16488 }
16489
16490 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16491    is the index of the CU in which the symbol appears.  */
16492
16493 static void
16494 add_index_entry (struct mapped_symtab *symtab, const char *name,
16495                  offset_type cu_index)
16496 {
16497   struct symtab_index_entry **slot;
16498
16499   ++symtab->n_elements;
16500   if (4 * symtab->n_elements / 3 >= symtab->size)
16501     hash_expand (symtab);
16502
16503   slot = find_slot (symtab, name);
16504   if (!*slot)
16505     {
16506       *slot = XNEW (struct symtab_index_entry);
16507       (*slot)->name = name;
16508       (*slot)->cu_indices = NULL;
16509     }
16510   /* Don't push an index twice.  Due to how we add entries we only
16511      have to check the last one.  */ 
16512   if (VEC_empty (offset_type, (*slot)->cu_indices)
16513       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16514     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16515 }
16516
16517 /* Add a vector of indices to the constant pool.  */
16518
16519 static offset_type
16520 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16521                       struct symtab_index_entry *entry)
16522 {
16523   void **slot;
16524
16525   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16526   if (!*slot)
16527     {
16528       offset_type len = VEC_length (offset_type, entry->cu_indices);
16529       offset_type val = MAYBE_SWAP (len);
16530       offset_type iter;
16531       int i;
16532
16533       *slot = entry;
16534       entry->index_offset = obstack_object_size (cpool);
16535
16536       obstack_grow (cpool, &val, sizeof (val));
16537       for (i = 0;
16538            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16539            ++i)
16540         {
16541           val = MAYBE_SWAP (iter);
16542           obstack_grow (cpool, &val, sizeof (val));
16543         }
16544     }
16545   else
16546     {
16547       struct symtab_index_entry *old_entry = *slot;
16548       entry->index_offset = old_entry->index_offset;
16549       entry = old_entry;
16550     }
16551   return entry->index_offset;
16552 }
16553
16554 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16555    constant pool entries going into the obstack CPOOL.  */
16556
16557 static void
16558 write_hash_table (struct mapped_symtab *symtab,
16559                   struct obstack *output, struct obstack *cpool)
16560 {
16561   offset_type i;
16562   htab_t symbol_hash_table;
16563   htab_t str_table;
16564
16565   symbol_hash_table = create_symbol_hash_table ();
16566   str_table = create_strtab ();
16567
16568   /* We add all the index vectors to the constant pool first, to
16569      ensure alignment is ok.  */
16570   for (i = 0; i < symtab->size; ++i)
16571     {
16572       if (symtab->data[i])
16573         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16574     }
16575
16576   /* Now write out the hash table.  */
16577   for (i = 0; i < symtab->size; ++i)
16578     {
16579       offset_type str_off, vec_off;
16580
16581       if (symtab->data[i])
16582         {
16583           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16584           vec_off = symtab->data[i]->index_offset;
16585         }
16586       else
16587         {
16588           /* While 0 is a valid constant pool index, it is not valid
16589              to have 0 for both offsets.  */
16590           str_off = 0;
16591           vec_off = 0;
16592         }
16593
16594       str_off = MAYBE_SWAP (str_off);
16595       vec_off = MAYBE_SWAP (vec_off);
16596
16597       obstack_grow (output, &str_off, sizeof (str_off));
16598       obstack_grow (output, &vec_off, sizeof (vec_off));
16599     }
16600
16601   htab_delete (str_table);
16602   htab_delete (symbol_hash_table);
16603 }
16604
16605 /* Struct to map psymtab to CU index in the index file.  */
16606 struct psymtab_cu_index_map
16607 {
16608   struct partial_symtab *psymtab;
16609   unsigned int cu_index;
16610 };
16611
16612 static hashval_t
16613 hash_psymtab_cu_index (const void *item)
16614 {
16615   const struct psymtab_cu_index_map *map = item;
16616
16617   return htab_hash_pointer (map->psymtab);
16618 }
16619
16620 static int
16621 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16622 {
16623   const struct psymtab_cu_index_map *lhs = item_lhs;
16624   const struct psymtab_cu_index_map *rhs = item_rhs;
16625
16626   return lhs->psymtab == rhs->psymtab;
16627 }
16628
16629 /* Helper struct for building the address table.  */
16630 struct addrmap_index_data
16631 {
16632   struct objfile *objfile;
16633   struct obstack *addr_obstack;
16634   htab_t cu_index_htab;
16635
16636   /* Non-zero if the previous_* fields are valid.
16637      We can't write an entry until we see the next entry (since it is only then
16638      that we know the end of the entry).  */
16639   int previous_valid;
16640   /* Index of the CU in the table of all CUs in the index file.  */
16641   unsigned int previous_cu_index;
16642   /* Start address of the CU.  */
16643   CORE_ADDR previous_cu_start;
16644 };
16645
16646 /* Write an address entry to OBSTACK.  */
16647
16648 static void
16649 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16650                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16651 {
16652   offset_type cu_index_to_write;
16653   char addr[8];
16654   CORE_ADDR baseaddr;
16655
16656   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16657
16658   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16659   obstack_grow (obstack, addr, 8);
16660   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16661   obstack_grow (obstack, addr, 8);
16662   cu_index_to_write = MAYBE_SWAP (cu_index);
16663   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16664 }
16665
16666 /* Worker function for traversing an addrmap to build the address table.  */
16667
16668 static int
16669 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16670 {
16671   struct addrmap_index_data *data = datap;
16672   struct partial_symtab *pst = obj;
16673   offset_type cu_index;
16674   void **slot;
16675
16676   if (data->previous_valid)
16677     add_address_entry (data->objfile, data->addr_obstack,
16678                        data->previous_cu_start, start_addr,
16679                        data->previous_cu_index);
16680
16681   data->previous_cu_start = start_addr;
16682   if (pst != NULL)
16683     {
16684       struct psymtab_cu_index_map find_map, *map;
16685       find_map.psymtab = pst;
16686       map = htab_find (data->cu_index_htab, &find_map);
16687       gdb_assert (map != NULL);
16688       data->previous_cu_index = map->cu_index;
16689       data->previous_valid = 1;
16690     }
16691   else
16692       data->previous_valid = 0;
16693
16694   return 0;
16695 }
16696
16697 /* Write OBJFILE's address map to OBSTACK.
16698    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16699    in the index file.  */
16700
16701 static void
16702 write_address_map (struct objfile *objfile, struct obstack *obstack,
16703                    htab_t cu_index_htab)
16704 {
16705   struct addrmap_index_data addrmap_index_data;
16706
16707   /* When writing the address table, we have to cope with the fact that
16708      the addrmap iterator only provides the start of a region; we have to
16709      wait until the next invocation to get the start of the next region.  */
16710
16711   addrmap_index_data.objfile = objfile;
16712   addrmap_index_data.addr_obstack = obstack;
16713   addrmap_index_data.cu_index_htab = cu_index_htab;
16714   addrmap_index_data.previous_valid = 0;
16715
16716   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16717                    &addrmap_index_data);
16718
16719   /* It's highly unlikely the last entry (end address = 0xff...ff)
16720      is valid, but we should still handle it.
16721      The end address is recorded as the start of the next region, but that
16722      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16723      anyway.  */
16724   if (addrmap_index_data.previous_valid)
16725     add_address_entry (objfile, obstack,
16726                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16727                        addrmap_index_data.previous_cu_index);
16728 }
16729
16730 /* Add a list of partial symbols to SYMTAB.  */
16731
16732 static void
16733 write_psymbols (struct mapped_symtab *symtab,
16734                 htab_t psyms_seen,
16735                 struct partial_symbol **psymp,
16736                 int count,
16737                 offset_type cu_index,
16738                 int is_static)
16739 {
16740   for (; count-- > 0; ++psymp)
16741     {
16742       void **slot, *lookup;
16743
16744       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16745         error (_("Ada is not currently supported by the index"));
16746
16747       /* We only want to add a given psymbol once.  However, we also
16748          want to account for whether it is global or static.  So, we
16749          may add it twice, using slightly different values.  */
16750       if (is_static)
16751         {
16752           uintptr_t val = 1 | (uintptr_t) *psymp;
16753
16754           lookup = (void *) val;
16755         }
16756       else
16757         lookup = *psymp;
16758
16759       /* Only add a given psymbol once.  */
16760       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16761       if (!*slot)
16762         {
16763           *slot = lookup;
16764           add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16765         }
16766     }
16767 }
16768
16769 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16770    exception if there is an error.  */
16771
16772 static void
16773 write_obstack (FILE *file, struct obstack *obstack)
16774 {
16775   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16776               file)
16777       != obstack_object_size (obstack))
16778     error (_("couldn't data write to file"));
16779 }
16780
16781 /* Unlink a file if the argument is not NULL.  */
16782
16783 static void
16784 unlink_if_set (void *p)
16785 {
16786   char **filename = p;
16787   if (*filename)
16788     unlink (*filename);
16789 }
16790
16791 /* A helper struct used when iterating over debug_types.  */
16792 struct signatured_type_index_data
16793 {
16794   struct objfile *objfile;
16795   struct mapped_symtab *symtab;
16796   struct obstack *types_list;
16797   htab_t psyms_seen;
16798   int cu_index;
16799 };
16800
16801 /* A helper function that writes a single signatured_type to an
16802    obstack.  */
16803
16804 static int
16805 write_one_signatured_type (void **slot, void *d)
16806 {
16807   struct signatured_type_index_data *info = d;
16808   struct signatured_type *entry = (struct signatured_type *) *slot;
16809   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16810   struct partial_symtab *psymtab = per_cu->v.psymtab;
16811   gdb_byte val[8];
16812
16813   write_psymbols (info->symtab,
16814                   info->psyms_seen,
16815                   info->objfile->global_psymbols.list
16816                   + psymtab->globals_offset,
16817                   psymtab->n_global_syms, info->cu_index,
16818                   0);
16819   write_psymbols (info->symtab,
16820                   info->psyms_seen,
16821                   info->objfile->static_psymbols.list
16822                   + psymtab->statics_offset,
16823                   psymtab->n_static_syms, info->cu_index,
16824                   1);
16825
16826   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16827   obstack_grow (info->types_list, val, 8);
16828   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16829   obstack_grow (info->types_list, val, 8);
16830   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16831   obstack_grow (info->types_list, val, 8);
16832
16833   ++info->cu_index;
16834
16835   return 1;
16836 }
16837
16838 /* Create an index file for OBJFILE in the directory DIR.  */
16839
16840 static void
16841 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16842 {
16843   struct cleanup *cleanup;
16844   char *filename, *cleanup_filename;
16845   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16846   struct obstack cu_list, types_cu_list;
16847   int i;
16848   FILE *out_file;
16849   struct mapped_symtab *symtab;
16850   offset_type val, size_of_contents, total_len;
16851   struct stat st;
16852   char buf[8];
16853   htab_t psyms_seen;
16854   htab_t cu_index_htab;
16855   struct psymtab_cu_index_map *psymtab_cu_index_map;
16856
16857   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16858     return;
16859
16860   if (dwarf2_per_objfile->using_index)
16861     error (_("Cannot use an index to create the index"));
16862
16863   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16864     error (_("Cannot make an index when the file has multiple .debug_types sections"));
16865
16866   if (stat (objfile->name, &st) < 0)
16867     perror_with_name (objfile->name);
16868
16869   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16870                      INDEX_SUFFIX, (char *) NULL);
16871   cleanup = make_cleanup (xfree, filename);
16872
16873   out_file = fopen (filename, "wb");
16874   if (!out_file)
16875     error (_("Can't open `%s' for writing"), filename);
16876
16877   cleanup_filename = filename;
16878   make_cleanup (unlink_if_set, &cleanup_filename);
16879
16880   symtab = create_mapped_symtab ();
16881   make_cleanup (cleanup_mapped_symtab, symtab);
16882
16883   obstack_init (&addr_obstack);
16884   make_cleanup_obstack_free (&addr_obstack);
16885
16886   obstack_init (&cu_list);
16887   make_cleanup_obstack_free (&cu_list);
16888
16889   obstack_init (&types_cu_list);
16890   make_cleanup_obstack_free (&types_cu_list);
16891
16892   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16893                                   NULL, xcalloc, xfree);
16894   make_cleanup_htab_delete (psyms_seen);
16895
16896   /* While we're scanning CU's create a table that maps a psymtab pointer
16897      (which is what addrmap records) to its index (which is what is recorded
16898      in the index file).  This will later be needed to write the address
16899      table.  */
16900   cu_index_htab = htab_create_alloc (100,
16901                                      hash_psymtab_cu_index,
16902                                      eq_psymtab_cu_index,
16903                                      NULL, xcalloc, xfree);
16904   make_cleanup_htab_delete (cu_index_htab);
16905   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16906     xmalloc (sizeof (struct psymtab_cu_index_map)
16907              * dwarf2_per_objfile->n_comp_units);
16908   make_cleanup (xfree, psymtab_cu_index_map);
16909
16910   /* The CU list is already sorted, so we don't need to do additional
16911      work here.  Also, the debug_types entries do not appear in
16912      all_comp_units, but only in their own hash table.  */
16913   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16914     {
16915       struct dwarf2_per_cu_data *per_cu
16916         = dwarf2_per_objfile->all_comp_units[i];
16917       struct partial_symtab *psymtab = per_cu->v.psymtab;
16918       gdb_byte val[8];
16919       struct psymtab_cu_index_map *map;
16920       void **slot;
16921
16922       write_psymbols (symtab,
16923                       psyms_seen,
16924                       objfile->global_psymbols.list + psymtab->globals_offset,
16925                       psymtab->n_global_syms, i,
16926                       0);
16927       write_psymbols (symtab,
16928                       psyms_seen,
16929                       objfile->static_psymbols.list + psymtab->statics_offset,
16930                       psymtab->n_static_syms, i,
16931                       1);
16932
16933       map = &psymtab_cu_index_map[i];
16934       map->psymtab = psymtab;
16935       map->cu_index = i;
16936       slot = htab_find_slot (cu_index_htab, map, INSERT);
16937       gdb_assert (slot != NULL);
16938       gdb_assert (*slot == NULL);
16939       *slot = map;
16940
16941       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16942       obstack_grow (&cu_list, val, 8);
16943       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16944       obstack_grow (&cu_list, val, 8);
16945     }
16946
16947   /* Dump the address map.  */
16948   write_address_map (objfile, &addr_obstack, cu_index_htab);
16949
16950   /* Write out the .debug_type entries, if any.  */
16951   if (dwarf2_per_objfile->signatured_types)
16952     {
16953       struct signatured_type_index_data sig_data;
16954
16955       sig_data.objfile = objfile;
16956       sig_data.symtab = symtab;
16957       sig_data.types_list = &types_cu_list;
16958       sig_data.psyms_seen = psyms_seen;
16959       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16960       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16961                               write_one_signatured_type, &sig_data);
16962     }
16963
16964   obstack_init (&constant_pool);
16965   make_cleanup_obstack_free (&constant_pool);
16966   obstack_init (&symtab_obstack);
16967   make_cleanup_obstack_free (&symtab_obstack);
16968   write_hash_table (symtab, &symtab_obstack, &constant_pool);
16969
16970   obstack_init (&contents);
16971   make_cleanup_obstack_free (&contents);
16972   size_of_contents = 6 * sizeof (offset_type);
16973   total_len = size_of_contents;
16974
16975   /* The version number.  */
16976   val = MAYBE_SWAP (5);
16977   obstack_grow (&contents, &val, sizeof (val));
16978
16979   /* The offset of the CU list from the start of the file.  */
16980   val = MAYBE_SWAP (total_len);
16981   obstack_grow (&contents, &val, sizeof (val));
16982   total_len += obstack_object_size (&cu_list);
16983
16984   /* The offset of the types CU list from the start of the file.  */
16985   val = MAYBE_SWAP (total_len);
16986   obstack_grow (&contents, &val, sizeof (val));
16987   total_len += obstack_object_size (&types_cu_list);
16988
16989   /* The offset of the address table from the start of the file.  */
16990   val = MAYBE_SWAP (total_len);
16991   obstack_grow (&contents, &val, sizeof (val));
16992   total_len += obstack_object_size (&addr_obstack);
16993
16994   /* The offset of the symbol table from the start of the file.  */
16995   val = MAYBE_SWAP (total_len);
16996   obstack_grow (&contents, &val, sizeof (val));
16997   total_len += obstack_object_size (&symtab_obstack);
16998
16999   /* The offset of the constant pool from the start of the file.  */
17000   val = MAYBE_SWAP (total_len);
17001   obstack_grow (&contents, &val, sizeof (val));
17002   total_len += obstack_object_size (&constant_pool);
17003
17004   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17005
17006   write_obstack (out_file, &contents);
17007   write_obstack (out_file, &cu_list);
17008   write_obstack (out_file, &types_cu_list);
17009   write_obstack (out_file, &addr_obstack);
17010   write_obstack (out_file, &symtab_obstack);
17011   write_obstack (out_file, &constant_pool);
17012
17013   fclose (out_file);
17014
17015   /* We want to keep the file, so we set cleanup_filename to NULL
17016      here.  See unlink_if_set.  */
17017   cleanup_filename = NULL;
17018
17019   do_cleanups (cleanup);
17020 }
17021
17022 /* Implementation of the `save gdb-index' command.
17023    
17024    Note that the file format used by this command is documented in the
17025    GDB manual.  Any changes here must be documented there.  */
17026
17027 static void
17028 save_gdb_index_command (char *arg, int from_tty)
17029 {
17030   struct objfile *objfile;
17031
17032   if (!arg || !*arg)
17033     error (_("usage: save gdb-index DIRECTORY"));
17034
17035   ALL_OBJFILES (objfile)
17036   {
17037     struct stat st;
17038
17039     /* If the objfile does not correspond to an actual file, skip it.  */
17040     if (stat (objfile->name, &st) < 0)
17041       continue;
17042
17043     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17044     if (dwarf2_per_objfile)
17045       {
17046         volatile struct gdb_exception except;
17047
17048         TRY_CATCH (except, RETURN_MASK_ERROR)
17049           {
17050             write_psymtabs_to_index (objfile, arg);
17051           }
17052         if (except.reason < 0)
17053           exception_fprintf (gdb_stderr, except,
17054                              _("Error while writing index for `%s': "),
17055                              objfile->name);
17056       }
17057   }
17058 }
17059
17060 \f
17061
17062 int dwarf2_always_disassemble;
17063
17064 static void
17065 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17066                                 struct cmd_list_element *c, const char *value)
17067 {
17068   fprintf_filtered (file,
17069                     _("Whether to always disassemble "
17070                       "DWARF expressions is %s.\n"),
17071                     value);
17072 }
17073
17074 static void
17075 show_check_physname (struct ui_file *file, int from_tty,
17076                      struct cmd_list_element *c, const char *value)
17077 {
17078   fprintf_filtered (file,
17079                     _("Whether to check \"physname\" is %s.\n"),
17080                     value);
17081 }
17082
17083 void _initialize_dwarf2_read (void);
17084
17085 void
17086 _initialize_dwarf2_read (void)
17087 {
17088   struct cmd_list_element *c;
17089
17090   dwarf2_objfile_data_key
17091     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17092
17093   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17094 Set DWARF 2 specific variables.\n\
17095 Configure DWARF 2 variables such as the cache size"),
17096                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17097                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17098
17099   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17100 Show DWARF 2 specific variables\n\
17101 Show DWARF 2 variables such as the cache size"),
17102                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17103                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17104
17105   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17106                             &dwarf2_max_cache_age, _("\
17107 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17108 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17109 A higher limit means that cached compilation units will be stored\n\
17110 in memory longer, and more total memory will be used.  Zero disables\n\
17111 caching, which can slow down startup."),
17112                             NULL,
17113                             show_dwarf2_max_cache_age,
17114                             &set_dwarf2_cmdlist,
17115                             &show_dwarf2_cmdlist);
17116
17117   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17118                            &dwarf2_always_disassemble, _("\
17119 Set whether `info address' always disassembles DWARF expressions."), _("\
17120 Show whether `info address' always disassembles DWARF expressions."), _("\
17121 When enabled, DWARF expressions are always printed in an assembly-like\n\
17122 syntax.  When disabled, expressions will be printed in a more\n\
17123 conversational style, when possible."),
17124                            NULL,
17125                            show_dwarf2_always_disassemble,
17126                            &set_dwarf2_cmdlist,
17127                            &show_dwarf2_cmdlist);
17128
17129   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17130 Set debugging of the dwarf2 DIE reader."), _("\
17131 Show debugging of the dwarf2 DIE reader."), _("\
17132 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17133 The value is the maximum depth to print."),
17134                             NULL,
17135                             NULL,
17136                             &setdebuglist, &showdebuglist);
17137
17138   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17139 Set cross-checking of \"physname\" code against demangler."), _("\
17140 Show cross-checking of \"physname\" code against demangler."), _("\
17141 When enabled, GDB's internal \"physname\" code is checked against\n\
17142 the demangler."),
17143                            NULL, show_check_physname,
17144                            &setdebuglist, &showdebuglist);
17145
17146   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17147                _("\
17148 Save a gdb-index file.\n\
17149 Usage: save gdb-index DIRECTORY"),
17150                &save_cmdlist);
17151   set_cmd_completer (c, filename_completer);
17152 }