OSDN Git Service

* breakpoint.c (wrapper.h): Don't include.
[pf3gnuchains/pf3gnuchains4x.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 *, bfd *,
990                                 struct dwarf2_cu *, struct partial_symtab *);
991
992 static void dwarf2_start_subfile (char *, const char *, const char *);
993
994 static struct symbol *new_symbol (struct die_info *, struct type *,
995                                   struct dwarf2_cu *);
996
997 static struct symbol *new_symbol_full (struct die_info *, struct type *,
998                                        struct dwarf2_cu *, struct symbol *);
999
1000 static void dwarf2_const_value (struct attribute *, struct symbol *,
1001                                 struct dwarf2_cu *);
1002
1003 static void dwarf2_const_value_attr (struct attribute *attr,
1004                                      struct type *type,
1005                                      const char *name,
1006                                      struct obstack *obstack,
1007                                      struct dwarf2_cu *cu, long *value,
1008                                      gdb_byte **bytes,
1009                                      struct dwarf2_locexpr_baton **baton);
1010
1011 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1012
1013 static int need_gnat_info (struct dwarf2_cu *);
1014
1015 static struct type *die_descriptive_type (struct die_info *,
1016                                           struct dwarf2_cu *);
1017
1018 static void set_descriptive_type (struct type *, struct die_info *,
1019                                   struct dwarf2_cu *);
1020
1021 static struct type *die_containing_type (struct die_info *,
1022                                          struct dwarf2_cu *);
1023
1024 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1025                                      struct dwarf2_cu *);
1026
1027 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1028
1029 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1030
1031 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1032
1033 static char *typename_concat (struct obstack *obs, const char *prefix,
1034                               const char *suffix, int physname,
1035                               struct dwarf2_cu *cu);
1036
1037 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1038
1039 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1040
1041 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1046
1047 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1048                                struct dwarf2_cu *, struct partial_symtab *);
1049
1050 static int dwarf2_get_pc_bounds (struct die_info *,
1051                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1052                                  struct partial_symtab *);
1053
1054 static void get_scope_pc_bounds (struct die_info *,
1055                                  CORE_ADDR *, CORE_ADDR *,
1056                                  struct dwarf2_cu *);
1057
1058 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1059                                         CORE_ADDR, struct dwarf2_cu *);
1060
1061 static void dwarf2_add_field (struct field_info *, struct die_info *,
1062                               struct dwarf2_cu *);
1063
1064 static void dwarf2_attach_fields_to_type (struct field_info *,
1065                                           struct type *, struct dwarf2_cu *);
1066
1067 static void dwarf2_add_member_fn (struct field_info *,
1068                                   struct die_info *, struct type *,
1069                                   struct dwarf2_cu *);
1070
1071 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1072                                              struct type *,
1073                                              struct dwarf2_cu *);
1074
1075 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1080
1081 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1084
1085 static struct type *read_module_type (struct die_info *die,
1086                                       struct dwarf2_cu *cu);
1087
1088 static const char *namespace_name (struct die_info *die,
1089                                    int *is_anonymous, struct dwarf2_cu *);
1090
1091 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1092
1093 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1094
1095 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1096                                                        struct dwarf2_cu *);
1097
1098 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1099
1100 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1101                                                  gdb_byte *info_ptr,
1102                                                  gdb_byte **new_info_ptr,
1103                                                  struct die_info *parent);
1104
1105 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1106                                                gdb_byte *info_ptr,
1107                                                gdb_byte **new_info_ptr,
1108                                                struct die_info *parent);
1109
1110 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1111                                                gdb_byte *info_ptr,
1112                                                gdb_byte **new_info_ptr,
1113                                                struct die_info *parent);
1114
1115 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1116                                 struct die_info **, gdb_byte *,
1117                                 int *);
1118
1119 static void process_die (struct die_info *, struct dwarf2_cu *);
1120
1121 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1122                                        struct obstack *);
1123
1124 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1125
1126 static const char *dwarf2_full_name (char *name,
1127                                      struct die_info *die,
1128                                      struct dwarf2_cu *cu);
1129
1130 static struct die_info *dwarf2_extension (struct die_info *die,
1131                                           struct dwarf2_cu **);
1132
1133 static char *dwarf_tag_name (unsigned int);
1134
1135 static char *dwarf_attr_name (unsigned int);
1136
1137 static char *dwarf_form_name (unsigned int);
1138
1139 static char *dwarf_bool_name (unsigned int);
1140
1141 static char *dwarf_type_encoding_name (unsigned int);
1142
1143 #if 0
1144 static char *dwarf_cfi_name (unsigned int);
1145 #endif
1146
1147 static struct die_info *sibling_die (struct die_info *);
1148
1149 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1150
1151 static void dump_die_for_error (struct die_info *);
1152
1153 static void dump_die_1 (struct ui_file *, int level, int max_level,
1154                         struct die_info *);
1155
1156 /*static*/ void dump_die (struct die_info *, int max_level);
1157
1158 static void store_in_ref_table (struct die_info *,
1159                                 struct dwarf2_cu *);
1160
1161 static int is_ref_attr (struct attribute *);
1162
1163 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1164
1165 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1166
1167 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1168                                                struct attribute *,
1169                                                struct dwarf2_cu **);
1170
1171 static struct die_info *follow_die_ref (struct die_info *,
1172                                         struct attribute *,
1173                                         struct dwarf2_cu **);
1174
1175 static struct die_info *follow_die_sig (struct die_info *,
1176                                         struct attribute *,
1177                                         struct dwarf2_cu **);
1178
1179 static struct signatured_type *lookup_signatured_type_at_offset
1180     (struct objfile *objfile,
1181      struct dwarf2_section_info *section,
1182      unsigned int offset);
1183
1184 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1185
1186 static void read_signatured_type (struct signatured_type *type_sig);
1187
1188 /* memory allocation interface */
1189
1190 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1191
1192 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1193
1194 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1195
1196 static void dwarf_decode_macros (struct line_header *, unsigned int,
1197                                  char *, bfd *, struct dwarf2_cu *,
1198                                  struct dwarf2_section_info *,
1199                                  int);
1200
1201 static int attr_form_is_block (struct attribute *);
1202
1203 static int attr_form_is_section_offset (struct attribute *);
1204
1205 static int attr_form_is_constant (struct attribute *);
1206
1207 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1208                                    struct dwarf2_loclist_baton *baton,
1209                                    struct attribute *attr);
1210
1211 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1212                                          struct symbol *sym,
1213                                          struct dwarf2_cu *cu);
1214
1215 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1216                                struct abbrev_info *abbrev,
1217                                struct dwarf2_cu *cu);
1218
1219 static void free_stack_comp_unit (void *);
1220
1221 static hashval_t partial_die_hash (const void *item);
1222
1223 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1224
1225 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1226   (unsigned int offset, struct objfile *objfile);
1227
1228 static void init_one_comp_unit (struct dwarf2_cu *cu,
1229                                 struct dwarf2_per_cu_data *per_cu);
1230
1231 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1232                                    struct die_info *comp_unit_die);
1233
1234 static void free_heap_comp_unit (void *);
1235
1236 static void free_cached_comp_units (void *);
1237
1238 static void age_cached_comp_units (void);
1239
1240 static void free_one_cached_comp_unit (void *);
1241
1242 static struct type *set_die_type (struct die_info *, struct type *,
1243                                   struct dwarf2_cu *);
1244
1245 static void create_all_comp_units (struct objfile *);
1246
1247 static int create_debug_types_hash_table (struct objfile *objfile);
1248
1249 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1250
1251 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1252
1253 static void dwarf2_add_dependence (struct dwarf2_cu *,
1254                                    struct dwarf2_per_cu_data *);
1255
1256 static void dwarf2_mark (struct dwarf2_cu *);
1257
1258 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1259
1260 static struct type *get_die_type_at_offset (unsigned int,
1261                                             struct dwarf2_per_cu_data *per_cu);
1262
1263 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1264
1265 static void dwarf2_release_queue (void *dummy);
1266
1267 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1268
1269 static void process_queue (void);
1270
1271 static void find_file_and_directory (struct die_info *die,
1272                                      struct dwarf2_cu *cu,
1273                                      char **name, char **comp_dir);
1274
1275 static char *file_full_name (int file, struct line_header *lh,
1276                              const char *comp_dir);
1277
1278 static gdb_byte *read_and_check_comp_unit_head
1279   (struct comp_unit_head *header,
1280    struct dwarf2_section_info *section, gdb_byte *info_ptr,
1281    int is_debug_types_section);
1282
1283 static void init_cu_die_reader (struct die_reader_specs *reader,
1284                                 struct dwarf2_cu *cu);
1285
1286 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1287
1288 #if WORDS_BIGENDIAN
1289
1290 /* Convert VALUE between big- and little-endian.  */
1291 static offset_type
1292 byte_swap (offset_type value)
1293 {
1294   offset_type result;
1295
1296   result = (value & 0xff) << 24;
1297   result |= (value & 0xff00) << 8;
1298   result |= (value & 0xff0000) >> 8;
1299   result |= (value & 0xff000000) >> 24;
1300   return result;
1301 }
1302
1303 #define MAYBE_SWAP(V)  byte_swap (V)
1304
1305 #else
1306 #define MAYBE_SWAP(V) (V)
1307 #endif /* WORDS_BIGENDIAN */
1308
1309 /* The suffix for an index file.  */
1310 #define INDEX_SUFFIX ".gdb-index"
1311
1312 static const char *dwarf2_physname (char *name, struct die_info *die,
1313                                     struct dwarf2_cu *cu);
1314
1315 /* Try to locate the sections we need for DWARF 2 debugging
1316    information and return true if we have enough to do something.
1317    NAMES points to the dwarf2 section names, or is NULL if the standard
1318    ELF names are used.  */
1319
1320 int
1321 dwarf2_has_info (struct objfile *objfile,
1322                  const struct dwarf2_debug_sections *names)
1323 {
1324   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1325   if (!dwarf2_per_objfile)
1326     {
1327       /* Initialize per-objfile state.  */
1328       struct dwarf2_per_objfile *data
1329         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1330
1331       memset (data, 0, sizeof (*data));
1332       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1333       dwarf2_per_objfile = data;
1334
1335       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1336                              (void *) names);
1337       dwarf2_per_objfile->objfile = objfile;
1338     }
1339   return (dwarf2_per_objfile->info.asection != NULL
1340           && dwarf2_per_objfile->abbrev.asection != NULL);
1341 }
1342
1343 /* When loading sections, we look either for uncompressed section or for
1344    compressed section names.  */
1345
1346 static int
1347 section_is_p (const char *section_name,
1348               const struct dwarf2_section_names *names)
1349 {
1350   if (names->normal != NULL
1351       && strcmp (section_name, names->normal) == 0)
1352     return 1;
1353   if (names->compressed != NULL
1354       && strcmp (section_name, names->compressed) == 0)
1355     return 1;
1356   return 0;
1357 }
1358
1359 /* This function is mapped across the sections and remembers the
1360    offset and size of each of the debugging sections we are interested
1361    in.  */
1362
1363 static void
1364 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1365 {
1366   const struct dwarf2_debug_sections *names;
1367
1368   if (vnames == NULL)
1369     names = &dwarf2_elf_names;
1370   else
1371     names = (const struct dwarf2_debug_sections *) vnames;
1372
1373   if (section_is_p (sectp->name, &names->info))
1374     {
1375       dwarf2_per_objfile->info.asection = sectp;
1376       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1377     }
1378   else if (section_is_p (sectp->name, &names->abbrev))
1379     {
1380       dwarf2_per_objfile->abbrev.asection = sectp;
1381       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1382     }
1383   else if (section_is_p (sectp->name, &names->line))
1384     {
1385       dwarf2_per_objfile->line.asection = sectp;
1386       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1387     }
1388   else if (section_is_p (sectp->name, &names->loc))
1389     {
1390       dwarf2_per_objfile->loc.asection = sectp;
1391       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1392     }
1393   else if (section_is_p (sectp->name, &names->macinfo))
1394     {
1395       dwarf2_per_objfile->macinfo.asection = sectp;
1396       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1397     }
1398   else if (section_is_p (sectp->name, &names->macro))
1399     {
1400       dwarf2_per_objfile->macro.asection = sectp;
1401       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1402     }
1403   else if (section_is_p (sectp->name, &names->str))
1404     {
1405       dwarf2_per_objfile->str.asection = sectp;
1406       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1407     }
1408   else if (section_is_p (sectp->name, &names->frame))
1409     {
1410       dwarf2_per_objfile->frame.asection = sectp;
1411       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1412     }
1413   else if (section_is_p (sectp->name, &names->eh_frame))
1414     {
1415       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1416
1417       if (aflag & SEC_HAS_CONTENTS)
1418         {
1419           dwarf2_per_objfile->eh_frame.asection = sectp;
1420           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1421         }
1422     }
1423   else if (section_is_p (sectp->name, &names->ranges))
1424     {
1425       dwarf2_per_objfile->ranges.asection = sectp;
1426       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1427     }
1428   else if (section_is_p (sectp->name, &names->types))
1429     {
1430       struct dwarf2_section_info type_section;
1431
1432       memset (&type_section, 0, sizeof (type_section));
1433       type_section.asection = sectp;
1434       type_section.size = bfd_get_section_size (sectp);
1435
1436       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1437                      &type_section);
1438     }
1439   else if (section_is_p (sectp->name, &names->gdb_index))
1440     {
1441       dwarf2_per_objfile->gdb_index.asection = sectp;
1442       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1443     }
1444
1445   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1446       && bfd_section_vma (abfd, sectp) == 0)
1447     dwarf2_per_objfile->has_section_at_zero = 1;
1448 }
1449
1450 /* Decompress a section that was compressed using zlib.  Store the
1451    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1452
1453 static void
1454 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1455                          gdb_byte **outbuf, bfd_size_type *outsize)
1456 {
1457   bfd *abfd = objfile->obfd;
1458 #ifndef HAVE_ZLIB_H
1459   error (_("Support for zlib-compressed DWARF data (from '%s') "
1460            "is disabled in this copy of GDB"),
1461          bfd_get_filename (abfd));
1462 #else
1463   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1464   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1465   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1466   bfd_size_type uncompressed_size;
1467   gdb_byte *uncompressed_buffer;
1468   z_stream strm;
1469   int rc;
1470   int header_size = 12;
1471
1472   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1473       || bfd_bread (compressed_buffer,
1474                     compressed_size, abfd) != compressed_size)
1475     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1476            bfd_get_filename (abfd));
1477
1478   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1479      by the uncompressed section size, 8 bytes in big-endian order.  */
1480   if (compressed_size < header_size
1481       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1482     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1483            bfd_get_filename (abfd));
1484   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1485   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1486   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1487   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1488   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1489   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1490   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1491   uncompressed_size += compressed_buffer[11];
1492
1493   /* It is possible the section consists of several compressed
1494      buffers concatenated together, so we uncompress in a loop.  */
1495   strm.zalloc = NULL;
1496   strm.zfree = NULL;
1497   strm.opaque = NULL;
1498   strm.avail_in = compressed_size - header_size;
1499   strm.next_in = (Bytef*) compressed_buffer + header_size;
1500   strm.avail_out = uncompressed_size;
1501   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1502                                        uncompressed_size);
1503   rc = inflateInit (&strm);
1504   while (strm.avail_in > 0)
1505     {
1506       if (rc != Z_OK)
1507         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1508                bfd_get_filename (abfd), rc);
1509       strm.next_out = ((Bytef*) uncompressed_buffer
1510                        + (uncompressed_size - strm.avail_out));
1511       rc = inflate (&strm, Z_FINISH);
1512       if (rc != Z_STREAM_END)
1513         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1514                bfd_get_filename (abfd), rc);
1515       rc = inflateReset (&strm);
1516     }
1517   rc = inflateEnd (&strm);
1518   if (rc != Z_OK
1519       || strm.avail_out != 0)
1520     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1521            bfd_get_filename (abfd), rc);
1522
1523   do_cleanups (cleanup);
1524   *outbuf = uncompressed_buffer;
1525   *outsize = uncompressed_size;
1526 #endif
1527 }
1528
1529 /* A helper function that decides whether a section is empty.  */
1530
1531 static int
1532 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1533 {
1534   return info->asection == NULL || info->size == 0;
1535 }
1536
1537 /* Read the contents of the section INFO from object file specified by
1538    OBJFILE, store info about the section into INFO.
1539    If the section is compressed, uncompress it before returning.  */
1540
1541 static void
1542 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1543 {
1544   bfd *abfd = objfile->obfd;
1545   asection *sectp = info->asection;
1546   gdb_byte *buf, *retbuf;
1547   unsigned char header[4];
1548
1549   if (info->readin)
1550     return;
1551   info->buffer = NULL;
1552   info->map_addr = NULL;
1553   info->readin = 1;
1554
1555   if (dwarf2_section_empty_p (info))
1556     return;
1557
1558   /* Check if the file has a 4-byte header indicating compression.  */
1559   if (info->size > sizeof (header)
1560       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1561       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1562     {
1563       /* Upon decompression, update the buffer and its size.  */
1564       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1565         {
1566           zlib_decompress_section (objfile, sectp, &info->buffer,
1567                                    &info->size);
1568           return;
1569         }
1570     }
1571
1572 #ifdef HAVE_MMAP
1573   if (pagesize == 0)
1574     pagesize = getpagesize ();
1575
1576   /* Only try to mmap sections which are large enough: we don't want to
1577      waste space due to fragmentation.  Also, only try mmap for sections
1578      without relocations.  */
1579
1580   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1581     {
1582       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1583                          MAP_PRIVATE, sectp->filepos,
1584                          &info->map_addr, &info->map_len);
1585
1586       if ((caddr_t)info->buffer != MAP_FAILED)
1587         {
1588 #if HAVE_POSIX_MADVISE
1589           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1590 #endif
1591           return;
1592         }
1593     }
1594 #endif
1595
1596   /* If we get here, we are a normal, not-compressed section.  */
1597   info->buffer = buf
1598     = obstack_alloc (&objfile->objfile_obstack, info->size);
1599
1600   /* When debugging .o files, we may need to apply relocations; see
1601      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1602      We never compress sections in .o files, so we only need to
1603      try this when the section is not compressed.  */
1604   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1605   if (retbuf != NULL)
1606     {
1607       info->buffer = retbuf;
1608       return;
1609     }
1610
1611   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1612       || bfd_bread (buf, info->size, abfd) != info->size)
1613     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1614            bfd_get_filename (abfd));
1615 }
1616
1617 /* A helper function that returns the size of a section in a safe way.
1618    If you are positive that the section has been read before using the
1619    size, then it is safe to refer to the dwarf2_section_info object's
1620    "size" field directly.  In other cases, you must call this
1621    function, because for compressed sections the size field is not set
1622    correctly until the section has been read.  */
1623
1624 static bfd_size_type
1625 dwarf2_section_size (struct objfile *objfile,
1626                      struct dwarf2_section_info *info)
1627 {
1628   if (!info->readin)
1629     dwarf2_read_section (objfile, info);
1630   return info->size;
1631 }
1632
1633 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1634    SECTION_NAME.  */
1635
1636 void
1637 dwarf2_get_section_info (struct objfile *objfile,
1638                          enum dwarf2_section_enum sect,
1639                          asection **sectp, gdb_byte **bufp,
1640                          bfd_size_type *sizep)
1641 {
1642   struct dwarf2_per_objfile *data
1643     = objfile_data (objfile, dwarf2_objfile_data_key);
1644   struct dwarf2_section_info *info;
1645
1646   /* We may see an objfile without any DWARF, in which case we just
1647      return nothing.  */
1648   if (data == NULL)
1649     {
1650       *sectp = NULL;
1651       *bufp = NULL;
1652       *sizep = 0;
1653       return;
1654     }
1655   switch (sect)
1656     {
1657     case DWARF2_DEBUG_FRAME:
1658       info = &data->frame;
1659       break;
1660     case DWARF2_EH_FRAME:
1661       info = &data->eh_frame;
1662       break;
1663     default:
1664       gdb_assert_not_reached ("unexpected section");
1665     }
1666
1667   dwarf2_read_section (objfile, info);
1668
1669   *sectp = info->asection;
1670   *bufp = info->buffer;
1671   *sizep = info->size;
1672 }
1673
1674 \f
1675 /* DWARF quick_symbols_functions support.  */
1676
1677 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1678    unique line tables, so we maintain a separate table of all .debug_line
1679    derived entries to support the sharing.
1680    All the quick functions need is the list of file names.  We discard the
1681    line_header when we're done and don't need to record it here.  */
1682 struct quick_file_names
1683 {
1684   /* The offset in .debug_line of the line table.  We hash on this.  */
1685   unsigned int offset;
1686
1687   /* The number of entries in file_names, real_names.  */
1688   unsigned int num_file_names;
1689
1690   /* The file names from the line table, after being run through
1691      file_full_name.  */
1692   const char **file_names;
1693
1694   /* The file names from the line table after being run through
1695      gdb_realpath.  These are computed lazily.  */
1696   const char **real_names;
1697 };
1698
1699 /* When using the index (and thus not using psymtabs), each CU has an
1700    object of this type.  This is used to hold information needed by
1701    the various "quick" methods.  */
1702 struct dwarf2_per_cu_quick_data
1703 {
1704   /* The file table.  This can be NULL if there was no file table
1705      or it's currently not read in.
1706      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1707   struct quick_file_names *file_names;
1708
1709   /* The corresponding symbol table.  This is NULL if symbols for this
1710      CU have not yet been read.  */
1711   struct symtab *symtab;
1712
1713   /* A temporary mark bit used when iterating over all CUs in
1714      expand_symtabs_matching.  */
1715   unsigned int mark : 1;
1716
1717   /* True if we've tried to read the file table and found there isn't one.
1718      There will be no point in trying to read it again next time.  */
1719   unsigned int no_file_data : 1;
1720 };
1721
1722 /* Hash function for a quick_file_names.  */
1723
1724 static hashval_t
1725 hash_file_name_entry (const void *e)
1726 {
1727   const struct quick_file_names *file_data = e;
1728
1729   return file_data->offset;
1730 }
1731
1732 /* Equality function for a quick_file_names.  */
1733
1734 static int
1735 eq_file_name_entry (const void *a, const void *b)
1736 {
1737   const struct quick_file_names *ea = a;
1738   const struct quick_file_names *eb = b;
1739
1740   return ea->offset == eb->offset;
1741 }
1742
1743 /* Delete function for a quick_file_names.  */
1744
1745 static void
1746 delete_file_name_entry (void *e)
1747 {
1748   struct quick_file_names *file_data = e;
1749   int i;
1750
1751   for (i = 0; i < file_data->num_file_names; ++i)
1752     {
1753       xfree ((void*) file_data->file_names[i]);
1754       if (file_data->real_names)
1755         xfree ((void*) file_data->real_names[i]);
1756     }
1757
1758   /* The space for the struct itself lives on objfile_obstack,
1759      so we don't free it here.  */
1760 }
1761
1762 /* Create a quick_file_names hash table.  */
1763
1764 static htab_t
1765 create_quick_file_names_table (unsigned int nr_initial_entries)
1766 {
1767   return htab_create_alloc (nr_initial_entries,
1768                             hash_file_name_entry, eq_file_name_entry,
1769                             delete_file_name_entry, xcalloc, xfree);
1770 }
1771
1772 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1773    have to be created afterwards.  You should call age_cached_comp_units after
1774    processing PER_CU->CU.  dw2_setup must have been already called.  */
1775
1776 static void
1777 load_cu (struct dwarf2_per_cu_data *per_cu)
1778 {
1779   if (per_cu->debug_types_section)
1780     load_full_type_unit (per_cu);
1781   else
1782     load_full_comp_unit (per_cu);
1783
1784   gdb_assert (per_cu->cu != NULL);
1785
1786   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1787 }
1788
1789 /* Read in the symbols for PER_CU.  */
1790
1791 static void
1792 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1793 {
1794   struct cleanup *back_to;
1795
1796   back_to = make_cleanup (dwarf2_release_queue, NULL);
1797
1798   queue_comp_unit (per_cu);
1799
1800   load_cu (per_cu);
1801
1802   process_queue ();
1803
1804   /* Age the cache, releasing compilation units that have not
1805      been used recently.  */
1806   age_cached_comp_units ();
1807
1808   do_cleanups (back_to);
1809 }
1810
1811 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1812    the objfile from which this CU came.  Returns the resulting symbol
1813    table.  */
1814
1815 static struct symtab *
1816 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1817 {
1818   if (!per_cu->v.quick->symtab)
1819     {
1820       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1821       increment_reading_symtab ();
1822       dw2_do_instantiate_symtab (per_cu);
1823       do_cleanups (back_to);
1824     }
1825   return per_cu->v.quick->symtab;
1826 }
1827
1828 /* Return the CU given its index.  */
1829
1830 static struct dwarf2_per_cu_data *
1831 dw2_get_cu (int index)
1832 {
1833   if (index >= dwarf2_per_objfile->n_comp_units)
1834     {
1835       index -= dwarf2_per_objfile->n_comp_units;
1836       return dwarf2_per_objfile->all_type_units[index];
1837     }
1838   return dwarf2_per_objfile->all_comp_units[index];
1839 }
1840
1841 /* A helper function that knows how to read a 64-bit value in a way
1842    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1843    otherwise.  */
1844
1845 static int
1846 extract_cu_value (const char *bytes, ULONGEST *result)
1847 {
1848   if (sizeof (ULONGEST) < 8)
1849     {
1850       int i;
1851
1852       /* Ignore the upper 4 bytes if they are all zero.  */
1853       for (i = 0; i < 4; ++i)
1854         if (bytes[i + 4] != 0)
1855           return 0;
1856
1857       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1858     }
1859   else
1860     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1861   return 1;
1862 }
1863
1864 /* Read the CU list from the mapped index, and use it to create all
1865    the CU objects for this objfile.  Return 0 if something went wrong,
1866    1 if everything went ok.  */
1867
1868 static int
1869 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1870                        offset_type cu_list_elements)
1871 {
1872   offset_type i;
1873
1874   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1875   dwarf2_per_objfile->all_comp_units
1876     = obstack_alloc (&objfile->objfile_obstack,
1877                      dwarf2_per_objfile->n_comp_units
1878                      * sizeof (struct dwarf2_per_cu_data *));
1879
1880   for (i = 0; i < cu_list_elements; i += 2)
1881     {
1882       struct dwarf2_per_cu_data *the_cu;
1883       ULONGEST offset, length;
1884
1885       if (!extract_cu_value (cu_list, &offset)
1886           || !extract_cu_value (cu_list + 8, &length))
1887         return 0;
1888       cu_list += 2 * 8;
1889
1890       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1891                                struct dwarf2_per_cu_data);
1892       the_cu->offset = offset;
1893       the_cu->length = length;
1894       the_cu->objfile = objfile;
1895       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1896                                         struct dwarf2_per_cu_quick_data);
1897       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1898     }
1899
1900   return 1;
1901 }
1902
1903 /* Create the signatured type hash table from the index.  */
1904
1905 static int
1906 create_signatured_type_table_from_index (struct objfile *objfile,
1907                                          struct dwarf2_section_info *section,
1908                                          const gdb_byte *bytes,
1909                                          offset_type elements)
1910 {
1911   offset_type i;
1912   htab_t sig_types_hash;
1913
1914   dwarf2_per_objfile->n_type_units = elements / 3;
1915   dwarf2_per_objfile->all_type_units
1916     = obstack_alloc (&objfile->objfile_obstack,
1917                      dwarf2_per_objfile->n_type_units
1918                      * sizeof (struct dwarf2_per_cu_data *));
1919
1920   sig_types_hash = allocate_signatured_type_table (objfile);
1921
1922   for (i = 0; i < elements; i += 3)
1923     {
1924       struct signatured_type *type_sig;
1925       ULONGEST offset, type_offset, signature;
1926       void **slot;
1927
1928       if (!extract_cu_value (bytes, &offset)
1929           || !extract_cu_value (bytes + 8, &type_offset))
1930         return 0;
1931       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1932       bytes += 3 * 8;
1933
1934       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1935                                  struct signatured_type);
1936       type_sig->signature = signature;
1937       type_sig->type_offset = type_offset;
1938       type_sig->per_cu.debug_types_section = section;
1939       type_sig->per_cu.offset = offset;
1940       type_sig->per_cu.objfile = objfile;
1941       type_sig->per_cu.v.quick
1942         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1943                           struct dwarf2_per_cu_quick_data);
1944
1945       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1946       *slot = type_sig;
1947
1948       dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1949     }
1950
1951   dwarf2_per_objfile->signatured_types = sig_types_hash;
1952
1953   return 1;
1954 }
1955
1956 /* Read the address map data from the mapped index, and use it to
1957    populate the objfile's psymtabs_addrmap.  */
1958
1959 static void
1960 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1961 {
1962   const gdb_byte *iter, *end;
1963   struct obstack temp_obstack;
1964   struct addrmap *mutable_map;
1965   struct cleanup *cleanup;
1966   CORE_ADDR baseaddr;
1967
1968   obstack_init (&temp_obstack);
1969   cleanup = make_cleanup_obstack_free (&temp_obstack);
1970   mutable_map = addrmap_create_mutable (&temp_obstack);
1971
1972   iter = index->address_table;
1973   end = iter + index->address_table_size;
1974
1975   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1976
1977   while (iter < end)
1978     {
1979       ULONGEST hi, lo, cu_index;
1980       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1981       iter += 8;
1982       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1983       iter += 8;
1984       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1985       iter += 4;
1986       
1987       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1988                          dw2_get_cu (cu_index));
1989     }
1990
1991   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1992                                                     &objfile->objfile_obstack);
1993   do_cleanups (cleanup);
1994 }
1995
1996 /* The hash function for strings in the mapped index.  This is the same as
1997    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1998    implementation.  This is necessary because the hash function is tied to the
1999    format of the mapped index file.  The hash values do not have to match with
2000    SYMBOL_HASH_NEXT.
2001    
2002    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2003
2004 static hashval_t
2005 mapped_index_string_hash (int index_version, const void *p)
2006 {
2007   const unsigned char *str = (const unsigned char *) p;
2008   hashval_t r = 0;
2009   unsigned char c;
2010
2011   while ((c = *str++) != 0)
2012     {
2013       if (index_version >= 5)
2014         c = tolower (c);
2015       r = r * 67 + c - 113;
2016     }
2017
2018   return r;
2019 }
2020
2021 /* Find a slot in the mapped index INDEX for the object named NAME.
2022    If NAME is found, set *VEC_OUT to point to the CU vector in the
2023    constant pool and return 1.  If NAME cannot be found, return 0.  */
2024
2025 static int
2026 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2027                           offset_type **vec_out)
2028 {
2029   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2030   offset_type hash;
2031   offset_type slot, step;
2032   int (*cmp) (const char *, const char *);
2033
2034   if (current_language->la_language == language_cplus
2035       || current_language->la_language == language_java
2036       || current_language->la_language == language_fortran)
2037     {
2038       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2039          not contain any.  */
2040       const char *paren = strchr (name, '(');
2041
2042       if (paren)
2043         {
2044           char *dup;
2045
2046           dup = xmalloc (paren - name + 1);
2047           memcpy (dup, name, paren - name);
2048           dup[paren - name] = 0;
2049
2050           make_cleanup (xfree, dup);
2051           name = dup;
2052         }
2053     }
2054
2055   /* Index version 4 did not support case insensitive searches.  But the
2056      indexes for case insensitive languages are built in lowercase, therefore
2057      simulate our NAME being searched is also lowercased.  */
2058   hash = mapped_index_string_hash ((index->version == 4
2059                                     && case_sensitivity == case_sensitive_off
2060                                     ? 5 : index->version),
2061                                    name);
2062
2063   slot = hash & (index->symbol_table_slots - 1);
2064   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2065   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2066
2067   for (;;)
2068     {
2069       /* Convert a slot number to an offset into the table.  */
2070       offset_type i = 2 * slot;
2071       const char *str;
2072       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2073         {
2074           do_cleanups (back_to);
2075           return 0;
2076         }
2077
2078       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2079       if (!cmp (name, str))
2080         {
2081           *vec_out = (offset_type *) (index->constant_pool
2082                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2083           do_cleanups (back_to);
2084           return 1;
2085         }
2086
2087       slot = (slot + step) & (index->symbol_table_slots - 1);
2088     }
2089 }
2090
2091 /* Read the index file.  If everything went ok, initialize the "quick"
2092    elements of all the CUs and return 1.  Otherwise, return 0.  */
2093
2094 static int
2095 dwarf2_read_index (struct objfile *objfile)
2096 {
2097   char *addr;
2098   struct mapped_index *map;
2099   offset_type *metadata;
2100   const gdb_byte *cu_list;
2101   const gdb_byte *types_list = NULL;
2102   offset_type version, cu_list_elements;
2103   offset_type types_list_elements = 0;
2104   int i;
2105
2106   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2107     return 0;
2108
2109   /* Older elfutils strip versions could keep the section in the main
2110      executable while splitting it for the separate debug info file.  */
2111   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2112        & SEC_HAS_CONTENTS) == 0)
2113     return 0;
2114
2115   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2116
2117   addr = dwarf2_per_objfile->gdb_index.buffer;
2118   /* Version check.  */
2119   version = MAYBE_SWAP (*(offset_type *) addr);
2120   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2121      causes the index to behave very poorly for certain requests.  Version 3
2122      contained incomplete addrmap.  So, it seems better to just ignore such
2123      indices.  Index version 4 uses a different hash function than index
2124      version 5 and later.  */
2125   if (version < 4)
2126     return 0;
2127   /* Indexes with higher version than the one supported by GDB may be no
2128      longer backward compatible.  */
2129   if (version > 5)
2130     return 0;
2131
2132   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2133   map->version = version;
2134   map->total_size = dwarf2_per_objfile->gdb_index.size;
2135
2136   metadata = (offset_type *) (addr + sizeof (offset_type));
2137
2138   i = 0;
2139   cu_list = addr + MAYBE_SWAP (metadata[i]);
2140   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2141                       / 8);
2142   ++i;
2143
2144   types_list = addr + MAYBE_SWAP (metadata[i]);
2145   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2146                           - MAYBE_SWAP (metadata[i]))
2147                          / 8);
2148   ++i;
2149
2150   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2151   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2152                              - MAYBE_SWAP (metadata[i]));
2153   ++i;
2154
2155   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2156   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2157                               - MAYBE_SWAP (metadata[i]))
2158                              / (2 * sizeof (offset_type)));
2159   ++i;
2160
2161   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2162
2163   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2164     return 0;
2165
2166   if (types_list_elements)
2167     {
2168       struct dwarf2_section_info *section;
2169
2170       /* We can only handle a single .debug_types when we have an
2171          index.  */
2172       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2173         return 0;
2174
2175       section = VEC_index (dwarf2_section_info_def,
2176                            dwarf2_per_objfile->types, 0);
2177
2178       if (!create_signatured_type_table_from_index (objfile, section,
2179                                                     types_list,
2180                                                     types_list_elements))
2181         return 0;
2182     }
2183
2184   create_addrmap_from_index (objfile, map);
2185
2186   dwarf2_per_objfile->index_table = map;
2187   dwarf2_per_objfile->using_index = 1;
2188   dwarf2_per_objfile->quick_file_names_table =
2189     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2190
2191   return 1;
2192 }
2193
2194 /* A helper for the "quick" functions which sets the global
2195    dwarf2_per_objfile according to OBJFILE.  */
2196
2197 static void
2198 dw2_setup (struct objfile *objfile)
2199 {
2200   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2201   gdb_assert (dwarf2_per_objfile);
2202 }
2203
2204 /* A helper for the "quick" functions which attempts to read the line
2205    table for THIS_CU.  */
2206
2207 static struct quick_file_names *
2208 dw2_get_file_names (struct objfile *objfile,
2209                     struct dwarf2_per_cu_data *this_cu)
2210 {
2211   bfd *abfd = objfile->obfd;
2212   struct line_header *lh;
2213   struct attribute *attr;
2214   struct cleanup *cleanups;
2215   struct die_info *comp_unit_die;
2216   struct dwarf2_section_info* sec;
2217   gdb_byte *info_ptr;
2218   int has_children, i;
2219   struct dwarf2_cu cu;
2220   unsigned int bytes_read;
2221   struct die_reader_specs reader_specs;
2222   char *name, *comp_dir;
2223   void **slot;
2224   struct quick_file_names *qfn;
2225   unsigned int line_offset;
2226
2227   if (this_cu->v.quick->file_names != NULL)
2228     return this_cu->v.quick->file_names;
2229   /* If we know there is no line data, no point in looking again.  */
2230   if (this_cu->v.quick->no_file_data)
2231     return NULL;
2232
2233   init_one_comp_unit (&cu, this_cu);
2234   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2235
2236   if (this_cu->debug_types_section)
2237     sec = this_cu->debug_types_section;
2238   else
2239     sec = &dwarf2_per_objfile->info;
2240   dwarf2_read_section (objfile, sec);
2241   info_ptr = sec->buffer + this_cu->offset;
2242
2243   info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2244                                             this_cu->debug_types_section != NULL);
2245
2246   /* Skip dummy compilation units.  */
2247   if (info_ptr >= (sec->buffer + sec->size)
2248       || peek_abbrev_code (abfd, info_ptr) == 0)
2249     {
2250       do_cleanups (cleanups);
2251       return NULL;
2252     }
2253
2254   dwarf2_read_abbrevs (&cu);
2255   make_cleanup (dwarf2_free_abbrev_table, &cu);
2256
2257   init_cu_die_reader (&reader_specs, &cu);
2258   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2259                  &has_children);
2260
2261   lh = NULL;
2262   slot = NULL;
2263   line_offset = 0;
2264   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2265   if (attr)
2266     {
2267       struct quick_file_names find_entry;
2268
2269       line_offset = DW_UNSND (attr);
2270
2271       /* We may have already read in this line header (TU line header sharing).
2272          If we have we're done.  */
2273       find_entry.offset = line_offset;
2274       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2275                              &find_entry, INSERT);
2276       if (*slot != NULL)
2277         {
2278           do_cleanups (cleanups);
2279           this_cu->v.quick->file_names = *slot;
2280           return *slot;
2281         }
2282
2283       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2284     }
2285   if (lh == NULL)
2286     {
2287       do_cleanups (cleanups);
2288       this_cu->v.quick->no_file_data = 1;
2289       return NULL;
2290     }
2291
2292   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2293   qfn->offset = line_offset;
2294   gdb_assert (slot != NULL);
2295   *slot = qfn;
2296
2297   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2298
2299   qfn->num_file_names = lh->num_file_names;
2300   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2301                                    lh->num_file_names * sizeof (char *));
2302   for (i = 0; i < lh->num_file_names; ++i)
2303     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2304   qfn->real_names = NULL;
2305
2306   free_line_header (lh);
2307   do_cleanups (cleanups);
2308
2309   this_cu->v.quick->file_names = qfn;
2310   return qfn;
2311 }
2312
2313 /* A helper for the "quick" functions which computes and caches the
2314    real path for a given file name from the line table.  */
2315
2316 static const char *
2317 dw2_get_real_path (struct objfile *objfile,
2318                    struct quick_file_names *qfn, int index)
2319 {
2320   if (qfn->real_names == NULL)
2321     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2322                                       qfn->num_file_names, sizeof (char *));
2323
2324   if (qfn->real_names[index] == NULL)
2325     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2326
2327   return qfn->real_names[index];
2328 }
2329
2330 static struct symtab *
2331 dw2_find_last_source_symtab (struct objfile *objfile)
2332 {
2333   int index;
2334
2335   dw2_setup (objfile);
2336   index = dwarf2_per_objfile->n_comp_units - 1;
2337   return dw2_instantiate_symtab (dw2_get_cu (index));
2338 }
2339
2340 /* Traversal function for dw2_forget_cached_source_info.  */
2341
2342 static int
2343 dw2_free_cached_file_names (void **slot, void *info)
2344 {
2345   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2346
2347   if (file_data->real_names)
2348     {
2349       int i;
2350
2351       for (i = 0; i < file_data->num_file_names; ++i)
2352         {
2353           xfree ((void*) file_data->real_names[i]);
2354           file_data->real_names[i] = NULL;
2355         }
2356     }
2357
2358   return 1;
2359 }
2360
2361 static void
2362 dw2_forget_cached_source_info (struct objfile *objfile)
2363 {
2364   dw2_setup (objfile);
2365
2366   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2367                           dw2_free_cached_file_names, NULL);
2368 }
2369
2370 /* Helper function for dw2_map_symtabs_matching_filename that expands
2371    the symtabs and calls the iterator.  */
2372
2373 static int
2374 dw2_map_expand_apply (struct objfile *objfile,
2375                       struct dwarf2_per_cu_data *per_cu,
2376                       const char *name,
2377                       const char *full_path, const char *real_path,
2378                       int (*callback) (struct symtab *, void *),
2379                       void *data)
2380 {
2381   struct symtab *last_made = objfile->symtabs;
2382
2383   /* Don't visit already-expanded CUs.  */
2384   if (per_cu->v.quick->symtab)
2385     return 0;
2386
2387   /* This may expand more than one symtab, and we want to iterate over
2388      all of them.  */
2389   dw2_instantiate_symtab (per_cu);
2390
2391   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2392                                     objfile->symtabs, last_made);
2393 }
2394
2395 /* Implementation of the map_symtabs_matching_filename method.  */
2396
2397 static int
2398 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2399                                    const char *full_path, const char *real_path,
2400                                    int (*callback) (struct symtab *, void *),
2401                                    void *data)
2402 {
2403   int i;
2404   const char *name_basename = lbasename (name);
2405   int check_basename = name_basename == name;
2406   struct dwarf2_per_cu_data *base_cu = NULL;
2407
2408   dw2_setup (objfile);
2409
2410   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2411                    + dwarf2_per_objfile->n_type_units); ++i)
2412     {
2413       int j;
2414       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2415       struct quick_file_names *file_data;
2416
2417       /* We only need to look at symtabs not already expanded.  */
2418       if (per_cu->v.quick->symtab)
2419         continue;
2420
2421       file_data = dw2_get_file_names (objfile, per_cu);
2422       if (file_data == NULL)
2423         continue;
2424
2425       for (j = 0; j < file_data->num_file_names; ++j)
2426         {
2427           const char *this_name = file_data->file_names[j];
2428
2429           if (FILENAME_CMP (name, this_name) == 0)
2430             {
2431               if (dw2_map_expand_apply (objfile, per_cu,
2432                                         name, full_path, real_path,
2433                                         callback, data))
2434                 return 1;
2435             }
2436
2437           if (check_basename && ! base_cu
2438               && FILENAME_CMP (lbasename (this_name), name) == 0)
2439             base_cu = per_cu;
2440
2441           /* Before we invoke realpath, which can get expensive when many
2442              files are involved, do a quick comparison of the basenames.  */
2443           if (! basenames_may_differ
2444               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2445             continue;
2446
2447           if (full_path != NULL)
2448             {
2449               const char *this_real_name = dw2_get_real_path (objfile,
2450                                                               file_data, j);
2451
2452               if (this_real_name != NULL
2453                   && FILENAME_CMP (full_path, this_real_name) == 0)
2454                 {
2455                   if (dw2_map_expand_apply (objfile, per_cu,
2456                                             name, full_path, real_path,
2457                                             callback, data))
2458                     return 1;
2459                 }
2460             }
2461
2462           if (real_path != NULL)
2463             {
2464               const char *this_real_name = dw2_get_real_path (objfile,
2465                                                               file_data, j);
2466
2467               if (this_real_name != NULL
2468                   && FILENAME_CMP (real_path, this_real_name) == 0)
2469                 {
2470                   if (dw2_map_expand_apply (objfile, per_cu,
2471                                             name, full_path, real_path,
2472                                             callback, data))
2473                     return 1;
2474                 }
2475             }
2476         }
2477     }
2478
2479   if (base_cu)
2480     {
2481       if (dw2_map_expand_apply (objfile, base_cu,
2482                                 name, full_path, real_path,
2483                                 callback, data))
2484         return 1;
2485     }
2486
2487   return 0;
2488 }
2489
2490 static struct symtab *
2491 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2492                    const char *name, domain_enum domain)
2493 {
2494   /* We do all the work in the pre_expand_symtabs_matching hook
2495      instead.  */
2496   return NULL;
2497 }
2498
2499 /* A helper function that expands all symtabs that hold an object
2500    named NAME.  */
2501
2502 static void
2503 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2504 {
2505   dw2_setup (objfile);
2506
2507   /* index_table is NULL if OBJF_READNOW.  */
2508   if (dwarf2_per_objfile->index_table)
2509     {
2510       offset_type *vec;
2511
2512       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2513                                     name, &vec))
2514         {
2515           offset_type i, len = MAYBE_SWAP (*vec);
2516           for (i = 0; i < len; ++i)
2517             {
2518               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2519               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2520
2521               dw2_instantiate_symtab (per_cu);
2522             }
2523         }
2524     }
2525 }
2526
2527 static void
2528 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2529                                  enum block_enum block_kind, const char *name,
2530                                  domain_enum domain)
2531 {
2532   dw2_do_expand_symtabs_matching (objfile, name);
2533 }
2534
2535 static void
2536 dw2_print_stats (struct objfile *objfile)
2537 {
2538   int i, count;
2539
2540   dw2_setup (objfile);
2541   count = 0;
2542   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2543                    + dwarf2_per_objfile->n_type_units); ++i)
2544     {
2545       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2546
2547       if (!per_cu->v.quick->symtab)
2548         ++count;
2549     }
2550   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2551 }
2552
2553 static void
2554 dw2_dump (struct objfile *objfile)
2555 {
2556   /* Nothing worth printing.  */
2557 }
2558
2559 static void
2560 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2561               struct section_offsets *delta)
2562 {
2563   /* There's nothing to relocate here.  */
2564 }
2565
2566 static void
2567 dw2_expand_symtabs_for_function (struct objfile *objfile,
2568                                  const char *func_name)
2569 {
2570   dw2_do_expand_symtabs_matching (objfile, func_name);
2571 }
2572
2573 static void
2574 dw2_expand_all_symtabs (struct objfile *objfile)
2575 {
2576   int i;
2577
2578   dw2_setup (objfile);
2579
2580   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2581                    + dwarf2_per_objfile->n_type_units); ++i)
2582     {
2583       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2584
2585       dw2_instantiate_symtab (per_cu);
2586     }
2587 }
2588
2589 static void
2590 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2591                                   const char *filename)
2592 {
2593   int i;
2594
2595   dw2_setup (objfile);
2596
2597   /* We don't need to consider type units here.
2598      This is only called for examining code, e.g. expand_line_sal.
2599      There can be an order of magnitude (or more) more type units
2600      than comp units, and we avoid them if we can.  */
2601
2602   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2603     {
2604       int j;
2605       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2606       struct quick_file_names *file_data;
2607
2608       /* We only need to look at symtabs not already expanded.  */
2609       if (per_cu->v.quick->symtab)
2610         continue;
2611
2612       file_data = dw2_get_file_names (objfile, per_cu);
2613       if (file_data == NULL)
2614         continue;
2615
2616       for (j = 0; j < file_data->num_file_names; ++j)
2617         {
2618           const char *this_name = file_data->file_names[j];
2619           if (FILENAME_CMP (this_name, filename) == 0)
2620             {
2621               dw2_instantiate_symtab (per_cu);
2622               break;
2623             }
2624         }
2625     }
2626 }
2627
2628 static const char *
2629 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2630 {
2631   struct dwarf2_per_cu_data *per_cu;
2632   offset_type *vec;
2633   struct quick_file_names *file_data;
2634
2635   dw2_setup (objfile);
2636
2637   /* index_table is NULL if OBJF_READNOW.  */
2638   if (!dwarf2_per_objfile->index_table)
2639     {
2640       struct symtab *s;
2641
2642       ALL_OBJFILE_SYMTABS (objfile, s)
2643         if (s->primary)
2644           {
2645             struct blockvector *bv = BLOCKVECTOR (s);
2646             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2647             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2648
2649             if (sym)
2650               return sym->symtab->filename;
2651           }
2652       return NULL;
2653     }
2654
2655   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2656                                  name, &vec))
2657     return NULL;
2658
2659   /* Note that this just looks at the very first one named NAME -- but
2660      actually we are looking for a function.  find_main_filename
2661      should be rewritten so that it doesn't require a custom hook.  It
2662      could just use the ordinary symbol tables.  */
2663   /* vec[0] is the length, which must always be >0.  */
2664   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2665
2666   file_data = dw2_get_file_names (objfile, per_cu);
2667   if (file_data == NULL)
2668     return NULL;
2669
2670   return file_data->file_names[file_data->num_file_names - 1];
2671 }
2672
2673 static void
2674 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2675                           struct objfile *objfile, int global,
2676                           int (*callback) (struct block *,
2677                                            struct symbol *, void *),
2678                           void *data, symbol_compare_ftype *match,
2679                           symbol_compare_ftype *ordered_compare)
2680 {
2681   /* Currently unimplemented; used for Ada.  The function can be called if the
2682      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2683      does not look for non-Ada symbols this function should just return.  */
2684 }
2685
2686 static void
2687 dw2_expand_symtabs_matching
2688   (struct objfile *objfile,
2689    int (*file_matcher) (const char *, void *),
2690    int (*name_matcher) (const struct language_defn *, const char *, void *),
2691    enum search_domain kind,
2692    void *data)
2693 {
2694   int i;
2695   offset_type iter;
2696   struct mapped_index *index;
2697
2698   dw2_setup (objfile);
2699
2700   /* index_table is NULL if OBJF_READNOW.  */
2701   if (!dwarf2_per_objfile->index_table)
2702     return;
2703   index = dwarf2_per_objfile->index_table;
2704
2705   if (file_matcher != NULL)
2706     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2707                      + dwarf2_per_objfile->n_type_units); ++i)
2708       {
2709         int j;
2710         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2711         struct quick_file_names *file_data;
2712
2713         per_cu->v.quick->mark = 0;
2714
2715         /* We only need to look at symtabs not already expanded.  */
2716         if (per_cu->v.quick->symtab)
2717           continue;
2718
2719         file_data = dw2_get_file_names (objfile, per_cu);
2720         if (file_data == NULL)
2721           continue;
2722
2723         for (j = 0; j < file_data->num_file_names; ++j)
2724           {
2725             if (file_matcher (file_data->file_names[j], data))
2726               {
2727                 per_cu->v.quick->mark = 1;
2728                 break;
2729               }
2730           }
2731       }
2732
2733   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2734     {
2735       offset_type idx = 2 * iter;
2736       const char *name;
2737       offset_type *vec, vec_len, vec_idx;
2738
2739       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2740         continue;
2741
2742       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2743
2744       if (! (*name_matcher) (current_language, name, data))
2745         continue;
2746
2747       /* The name was matched, now expand corresponding CUs that were
2748          marked.  */
2749       vec = (offset_type *) (index->constant_pool
2750                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2751       vec_len = MAYBE_SWAP (vec[0]);
2752       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2753         {
2754           struct dwarf2_per_cu_data *per_cu;
2755
2756           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2757           if (file_matcher == NULL || per_cu->v.quick->mark)
2758             dw2_instantiate_symtab (per_cu);
2759         }
2760     }
2761 }
2762
2763 static struct symtab *
2764 dw2_find_pc_sect_symtab (struct objfile *objfile,
2765                          struct minimal_symbol *msymbol,
2766                          CORE_ADDR pc,
2767                          struct obj_section *section,
2768                          int warn_if_readin)
2769 {
2770   struct dwarf2_per_cu_data *data;
2771
2772   dw2_setup (objfile);
2773
2774   if (!objfile->psymtabs_addrmap)
2775     return NULL;
2776
2777   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2778   if (!data)
2779     return NULL;
2780
2781   if (warn_if_readin && data->v.quick->symtab)
2782     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2783              paddress (get_objfile_arch (objfile), pc));
2784
2785   return dw2_instantiate_symtab (data);
2786 }
2787
2788 static void
2789 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2790                           void *data, int need_fullname)
2791 {
2792   int i;
2793
2794   dw2_setup (objfile);
2795
2796   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2797                    + dwarf2_per_objfile->n_type_units); ++i)
2798     {
2799       int j;
2800       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2801       struct quick_file_names *file_data;
2802
2803       /* We only need to look at symtabs not already expanded.  */
2804       if (per_cu->v.quick->symtab)
2805         continue;
2806
2807       file_data = dw2_get_file_names (objfile, per_cu);
2808       if (file_data == NULL)
2809         continue;
2810
2811       for (j = 0; j < file_data->num_file_names; ++j)
2812         {
2813           const char *this_real_name;
2814
2815           if (need_fullname)
2816             this_real_name = dw2_get_real_path (objfile, file_data, j);
2817           else
2818             this_real_name = NULL;
2819           (*fun) (file_data->file_names[j], this_real_name, data);
2820         }
2821     }
2822 }
2823
2824 static int
2825 dw2_has_symbols (struct objfile *objfile)
2826 {
2827   return 1;
2828 }
2829
2830 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2831 {
2832   dw2_has_symbols,
2833   dw2_find_last_source_symtab,
2834   dw2_forget_cached_source_info,
2835   dw2_map_symtabs_matching_filename,
2836   dw2_lookup_symbol,
2837   dw2_pre_expand_symtabs_matching,
2838   dw2_print_stats,
2839   dw2_dump,
2840   dw2_relocate,
2841   dw2_expand_symtabs_for_function,
2842   dw2_expand_all_symtabs,
2843   dw2_expand_symtabs_with_filename,
2844   dw2_find_symbol_file,
2845   dw2_map_matching_symbols,
2846   dw2_expand_symtabs_matching,
2847   dw2_find_pc_sect_symtab,
2848   dw2_map_symbol_filenames
2849 };
2850
2851 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2852    file will use psymtabs, or 1 if using the GNU index.  */
2853
2854 int
2855 dwarf2_initialize_objfile (struct objfile *objfile)
2856 {
2857   /* If we're about to read full symbols, don't bother with the
2858      indices.  In this case we also don't care if some other debug
2859      format is making psymtabs, because they are all about to be
2860      expanded anyway.  */
2861   if ((objfile->flags & OBJF_READNOW))
2862     {
2863       int i;
2864
2865       dwarf2_per_objfile->using_index = 1;
2866       create_all_comp_units (objfile);
2867       create_debug_types_hash_table (objfile);
2868       dwarf2_per_objfile->quick_file_names_table =
2869         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2870
2871       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2872                        + dwarf2_per_objfile->n_type_units); ++i)
2873         {
2874           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2875
2876           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2877                                             struct dwarf2_per_cu_quick_data);
2878         }
2879
2880       /* Return 1 so that gdb sees the "quick" functions.  However,
2881          these functions will be no-ops because we will have expanded
2882          all symtabs.  */
2883       return 1;
2884     }
2885
2886   if (dwarf2_read_index (objfile))
2887     return 1;
2888
2889   return 0;
2890 }
2891
2892 \f
2893
2894 /* Build a partial symbol table.  */
2895
2896 void
2897 dwarf2_build_psymtabs (struct objfile *objfile)
2898 {
2899   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2900     {
2901       init_psymbol_list (objfile, 1024);
2902     }
2903
2904   dwarf2_build_psymtabs_hard (objfile);
2905 }
2906
2907 /* Return TRUE if OFFSET is within CU_HEADER.  */
2908
2909 static inline int
2910 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2911 {
2912   unsigned int bottom = cu_header->offset;
2913   unsigned int top = (cu_header->offset
2914                       + cu_header->length
2915                       + cu_header->initial_length_size);
2916
2917   return (offset >= bottom && offset < top);
2918 }
2919
2920 /* Read in the comp unit header information from the debug_info at info_ptr.
2921    NOTE: This leaves members offset, first_die_offset to be filled in
2922    by the caller.  */
2923
2924 static gdb_byte *
2925 read_comp_unit_head (struct comp_unit_head *cu_header,
2926                      gdb_byte *info_ptr, bfd *abfd)
2927 {
2928   int signed_addr;
2929   unsigned int bytes_read;
2930
2931   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2932   cu_header->initial_length_size = bytes_read;
2933   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2934   info_ptr += bytes_read;
2935   cu_header->version = read_2_bytes (abfd, info_ptr);
2936   info_ptr += 2;
2937   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2938                                           &bytes_read);
2939   info_ptr += bytes_read;
2940   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2941   info_ptr += 1;
2942   signed_addr = bfd_get_sign_extend_vma (abfd);
2943   if (signed_addr < 0)
2944     internal_error (__FILE__, __LINE__,
2945                     _("read_comp_unit_head: dwarf from non elf file"));
2946   cu_header->signed_addr_p = signed_addr;
2947
2948   return info_ptr;
2949 }
2950
2951 /* Subroutine of read_and_check_comp_unit_head and
2952    read_and_check_type_unit_head to simplify them.
2953    Perform various error checking on the header.  */
2954
2955 static void
2956 error_check_comp_unit_head (struct comp_unit_head *header,
2957                             struct dwarf2_section_info *section)
2958 {
2959   bfd *abfd = section->asection->owner;
2960   const char *filename = bfd_get_filename (abfd);
2961
2962   if (header->version != 2 && header->version != 3 && header->version != 4)
2963     error (_("Dwarf Error: wrong version in compilation unit header "
2964            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2965            filename);
2966
2967   if (header->abbrev_offset
2968       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2969                               &dwarf2_per_objfile->abbrev))
2970     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2971            "(offset 0x%lx + 6) [in module %s]"),
2972            (long) header->abbrev_offset, (long) header->offset,
2973            filename);
2974
2975   /* Cast to unsigned long to use 64-bit arithmetic when possible to
2976      avoid potential 32-bit overflow.  */
2977   if (((unsigned long) header->offset
2978        + header->length + header->initial_length_size)
2979       > section->size)
2980     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2981            "(offset 0x%lx + 0) [in module %s]"),
2982            (long) header->length, (long) header->offset,
2983            filename);
2984 }
2985
2986 /* Read in a CU/TU header and perform some basic error checking.
2987    The contents of the header are stored in HEADER.
2988    The result is a pointer to the start of the first DIE.  */
2989
2990 static gdb_byte *
2991 read_and_check_comp_unit_head (struct comp_unit_head *header,
2992                                struct dwarf2_section_info *section,
2993                                gdb_byte *info_ptr,
2994                                int is_debug_types_section)
2995 {
2996   gdb_byte *beg_of_comp_unit = info_ptr;
2997   bfd *abfd = section->asection->owner;
2998
2999   header->offset = beg_of_comp_unit - section->buffer;
3000
3001   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3002
3003   /* If we're reading a type unit, skip over the signature and
3004      type_offset fields.  */
3005   if (is_debug_types_section)
3006     info_ptr += 8 /*signature*/ + header->offset_size;
3007
3008   header->first_die_offset = info_ptr - beg_of_comp_unit;
3009
3010   error_check_comp_unit_head (header, section);
3011
3012   return info_ptr;
3013 }
3014
3015 /* Read in the types comp unit header information from .debug_types entry at
3016    types_ptr.  The result is a pointer to one past the end of the header.  */
3017
3018 static gdb_byte *
3019 read_and_check_type_unit_head (struct comp_unit_head *header,
3020                                struct dwarf2_section_info *section,
3021                                gdb_byte *info_ptr,
3022                                ULONGEST *signature, unsigned int *type_offset)
3023 {
3024   gdb_byte *beg_of_comp_unit = info_ptr;
3025   bfd *abfd = section->asection->owner;
3026
3027   header->offset = beg_of_comp_unit - section->buffer;
3028
3029   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3030
3031   /* If we're reading a type unit, skip over the signature and
3032      type_offset fields.  */
3033   if (signature != NULL)
3034     *signature = read_8_bytes (abfd, info_ptr);
3035   info_ptr += 8;
3036   if (type_offset != NULL)
3037     *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3038   info_ptr += header->offset_size;
3039
3040   header->first_die_offset = info_ptr - beg_of_comp_unit;
3041
3042   error_check_comp_unit_head (header, section);
3043
3044   return info_ptr;
3045 }
3046
3047 /* Allocate a new partial symtab for file named NAME and mark this new
3048    partial symtab as being an include of PST.  */
3049
3050 static void
3051 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3052                                struct objfile *objfile)
3053 {
3054   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3055
3056   subpst->section_offsets = pst->section_offsets;
3057   subpst->textlow = 0;
3058   subpst->texthigh = 0;
3059
3060   subpst->dependencies = (struct partial_symtab **)
3061     obstack_alloc (&objfile->objfile_obstack,
3062                    sizeof (struct partial_symtab *));
3063   subpst->dependencies[0] = pst;
3064   subpst->number_of_dependencies = 1;
3065
3066   subpst->globals_offset = 0;
3067   subpst->n_global_syms = 0;
3068   subpst->statics_offset = 0;
3069   subpst->n_static_syms = 0;
3070   subpst->symtab = NULL;
3071   subpst->read_symtab = pst->read_symtab;
3072   subpst->readin = 0;
3073
3074   /* No private part is necessary for include psymtabs.  This property
3075      can be used to differentiate between such include psymtabs and
3076      the regular ones.  */
3077   subpst->read_symtab_private = NULL;
3078 }
3079
3080 /* Read the Line Number Program data and extract the list of files
3081    included by the source file represented by PST.  Build an include
3082    partial symtab for each of these included files.  */
3083
3084 static void
3085 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3086                                struct die_info *die,
3087                                struct partial_symtab *pst)
3088 {
3089   struct objfile *objfile = cu->objfile;
3090   bfd *abfd = objfile->obfd;
3091   struct line_header *lh = NULL;
3092   struct attribute *attr;
3093
3094   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3095   if (attr)
3096     {
3097       unsigned int line_offset = DW_UNSND (attr);
3098
3099       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3100     }
3101   if (lh == NULL)
3102     return;  /* No linetable, so no includes.  */
3103
3104   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3105   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3106
3107   free_line_header (lh);
3108 }
3109
3110 static hashval_t
3111 hash_type_signature (const void *item)
3112 {
3113   const struct signatured_type *type_sig = item;
3114
3115   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3116   return type_sig->signature;
3117 }
3118
3119 static int
3120 eq_type_signature (const void *item_lhs, const void *item_rhs)
3121 {
3122   const struct signatured_type *lhs = item_lhs;
3123   const struct signatured_type *rhs = item_rhs;
3124
3125   return lhs->signature == rhs->signature;
3126 }
3127
3128 /* Allocate a hash table for signatured types.  */
3129
3130 static htab_t
3131 allocate_signatured_type_table (struct objfile *objfile)
3132 {
3133   return htab_create_alloc_ex (41,
3134                                hash_type_signature,
3135                                eq_type_signature,
3136                                NULL,
3137                                &objfile->objfile_obstack,
3138                                hashtab_obstack_allocate,
3139                                dummy_obstack_deallocate);
3140 }
3141
3142 /* A helper function to add a signatured type CU to a table.  */
3143
3144 static int
3145 add_signatured_type_cu_to_table (void **slot, void *datum)
3146 {
3147   struct signatured_type *sigt = *slot;
3148   struct dwarf2_per_cu_data ***datap = datum;
3149
3150   **datap = &sigt->per_cu;
3151   ++*datap;
3152
3153   return 1;
3154 }
3155
3156 /* Create the hash table of all entries in the .debug_types section(s).
3157    The result is zero if there is an error (e.g. missing .debug_types section),
3158    otherwise non-zero.  */
3159
3160 static int
3161 create_debug_types_hash_table (struct objfile *objfile)
3162 {
3163   htab_t types_htab = NULL;
3164   struct dwarf2_per_cu_data **iter;
3165   int ix;
3166   struct dwarf2_section_info *section;
3167
3168   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3169     {
3170       dwarf2_per_objfile->signatured_types = NULL;
3171       return 0;
3172     }
3173
3174   for (ix = 0;
3175        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3176                     ix, section);
3177        ++ix)
3178     {
3179       gdb_byte *info_ptr, *end_ptr;
3180
3181       dwarf2_read_section (objfile, section);
3182       info_ptr = section->buffer;
3183
3184       if (info_ptr == NULL)
3185         continue;
3186
3187       if (types_htab == NULL)
3188         types_htab = allocate_signatured_type_table (objfile);
3189
3190       if (dwarf2_die_debug)
3191         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3192
3193       end_ptr = info_ptr + section->size;
3194       while (info_ptr < end_ptr)
3195         {
3196           unsigned int offset;
3197           unsigned int type_offset;
3198           ULONGEST signature;
3199           struct signatured_type *type_sig;
3200           void **slot;
3201           gdb_byte *ptr = info_ptr;
3202           struct comp_unit_head header;
3203
3204           offset = ptr - section->buffer;
3205
3206           /* We need to read the type's signature in order to build the hash
3207              table, but we don't need anything else just yet.  */
3208
3209           ptr = read_and_check_type_unit_head (&header, section, ptr,
3210                                                &signature, &type_offset);
3211
3212           /* Skip dummy type units.  */
3213           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3214             {
3215               info_ptr = info_ptr + header.initial_length_size + header.length;
3216               continue;
3217             }
3218
3219           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3220           memset (type_sig, 0, sizeof (*type_sig));
3221           type_sig->signature = signature;
3222           type_sig->type_offset = type_offset;
3223           type_sig->per_cu.objfile = objfile;
3224           type_sig->per_cu.debug_types_section = section;
3225           type_sig->per_cu.offset = offset;
3226
3227           slot = htab_find_slot (types_htab, type_sig, INSERT);
3228           gdb_assert (slot != NULL);
3229           if (*slot != NULL)
3230             {
3231               const struct signatured_type *dup_sig = *slot;
3232
3233               complaint (&symfile_complaints,
3234                          _("debug type entry at offset 0x%x is duplicate to the "
3235                            "entry at offset 0x%x, signature 0x%s"),
3236                          offset, dup_sig->per_cu.offset,
3237                          phex (signature, sizeof (signature)));
3238               gdb_assert (signature == dup_sig->signature);
3239             }
3240           *slot = type_sig;
3241
3242           if (dwarf2_die_debug)
3243             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3244                                 offset, phex (signature, sizeof (signature)));
3245
3246           info_ptr = info_ptr + header.initial_length_size + header.length;
3247         }
3248     }
3249
3250   dwarf2_per_objfile->signatured_types = types_htab;
3251
3252   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3253   dwarf2_per_objfile->all_type_units
3254     = obstack_alloc (&objfile->objfile_obstack,
3255                      dwarf2_per_objfile->n_type_units
3256                      * sizeof (struct dwarf2_per_cu_data *));
3257   iter = &dwarf2_per_objfile->all_type_units[0];
3258   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3259   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3260               == dwarf2_per_objfile->n_type_units);
3261
3262   return 1;
3263 }
3264
3265 /* Lookup a signature based type.
3266    Returns NULL if SIG is not present in the table.  */
3267
3268 static struct signatured_type *
3269 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3270 {
3271   struct signatured_type find_entry, *entry;
3272
3273   if (dwarf2_per_objfile->signatured_types == NULL)
3274     {
3275       complaint (&symfile_complaints,
3276                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3277       return 0;
3278     }
3279
3280   find_entry.signature = sig;
3281   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3282   return entry;
3283 }
3284
3285 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3286
3287 static void
3288 init_cu_die_reader (struct die_reader_specs *reader,
3289                     struct dwarf2_cu *cu)
3290 {
3291   reader->abfd = cu->objfile->obfd;
3292   reader->cu = cu;
3293   if (cu->per_cu->debug_types_section)
3294     {
3295       gdb_assert (cu->per_cu->debug_types_section->readin);
3296       reader->buffer = cu->per_cu->debug_types_section->buffer;
3297     }
3298   else
3299     {
3300       gdb_assert (dwarf2_per_objfile->info.readin);
3301       reader->buffer = dwarf2_per_objfile->info.buffer;
3302     }
3303 }
3304
3305 /* Find the base address of the compilation unit for range lists and
3306    location lists.  It will normally be specified by DW_AT_low_pc.
3307    In DWARF-3 draft 4, the base address could be overridden by
3308    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3309    compilation units with discontinuous ranges.  */
3310
3311 static void
3312 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3313 {
3314   struct attribute *attr;
3315
3316   cu->base_known = 0;
3317   cu->base_address = 0;
3318
3319   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3320   if (attr)
3321     {
3322       cu->base_address = DW_ADDR (attr);
3323       cu->base_known = 1;
3324     }
3325   else
3326     {
3327       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3328       if (attr)
3329         {
3330           cu->base_address = DW_ADDR (attr);
3331           cu->base_known = 1;
3332         }
3333     }
3334 }
3335
3336 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3337    to combine the common parts.
3338    Process compilation unit THIS_CU for a psymtab.
3339    SECTION is the section the CU/TU comes from,
3340    either .debug_info or .debug_types.  */
3341
3342 void
3343 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3344                            struct dwarf2_section_info *section,
3345                            int is_debug_types_section)
3346 {
3347   struct objfile *objfile = this_cu->objfile;
3348   bfd *abfd = objfile->obfd;
3349   gdb_byte *buffer = section->buffer;
3350   gdb_byte *info_ptr = buffer + this_cu->offset;
3351   unsigned int buffer_size = section->size;
3352   gdb_byte *beg_of_comp_unit = info_ptr;
3353   struct die_info *comp_unit_die;
3354   struct partial_symtab *pst;
3355   CORE_ADDR baseaddr;
3356   struct cleanup *back_to_inner;
3357   struct dwarf2_cu cu;
3358   int has_children, has_pc_info;
3359   struct attribute *attr;
3360   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3361   struct die_reader_specs reader_specs;
3362   const char *filename;
3363
3364   /* If this compilation unit was already read in, free the
3365      cached copy in order to read it in again.  This is
3366      necessary because we skipped some symbols when we first
3367      read in the compilation unit (see load_partial_dies).
3368      This problem could be avoided, but the benefit is
3369      unclear.  */
3370   if (this_cu->cu != NULL)
3371     free_one_cached_comp_unit (this_cu->cu);
3372
3373   /* Note that this is a pointer to our stack frame, being
3374      added to a global data structure.  It will be cleaned up
3375      in free_stack_comp_unit when we finish with this
3376      compilation unit.  */
3377   init_one_comp_unit (&cu, this_cu);
3378   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3379
3380   info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3381                                             is_debug_types_section);
3382
3383   /* Skip dummy compilation units.  */
3384   if (info_ptr >= buffer + buffer_size
3385       || peek_abbrev_code (abfd, info_ptr) == 0)
3386     {
3387       do_cleanups (back_to_inner);
3388       return;
3389     }
3390
3391   cu.list_in_scope = &file_symbols;
3392
3393   /* Read the abbrevs for this compilation unit into a table.  */
3394   dwarf2_read_abbrevs (&cu);
3395   make_cleanup (dwarf2_free_abbrev_table, &cu);
3396
3397   /* Read the compilation unit die.  */
3398   init_cu_die_reader (&reader_specs, &cu);
3399   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3400                             &has_children);
3401
3402   if (is_debug_types_section)
3403     {
3404       /* LENGTH has not been set yet for type units.  */
3405       gdb_assert (this_cu->offset == cu.header.offset);
3406       this_cu->length = cu.header.length + cu.header.initial_length_size;
3407     }
3408   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3409     {
3410       do_cleanups (back_to_inner);
3411       return;
3412     }
3413
3414   prepare_one_comp_unit (&cu, comp_unit_die);
3415
3416   /* Allocate a new partial symbol table structure.  */
3417   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3418   if (attr == NULL || !DW_STRING (attr))
3419     filename = "";
3420   else
3421     filename = DW_STRING (attr);
3422   pst = start_psymtab_common (objfile, objfile->section_offsets,
3423                               filename,
3424                               /* TEXTLOW and TEXTHIGH are set below.  */
3425                               0,
3426                               objfile->global_psymbols.next,
3427                               objfile->static_psymbols.next);
3428   pst->psymtabs_addrmap_supported = 1;
3429
3430   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3431   if (attr != NULL)
3432     pst->dirname = DW_STRING (attr);
3433
3434   pst->read_symtab_private = this_cu;
3435
3436   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3437
3438   /* Store the function that reads in the rest of the symbol table.  */
3439   pst->read_symtab = dwarf2_psymtab_to_symtab;
3440
3441   this_cu->v.psymtab = pst;
3442
3443   dwarf2_find_base_address (comp_unit_die, &cu);
3444
3445   /* Possibly set the default values of LOWPC and HIGHPC from
3446      `DW_AT_ranges'.  */
3447   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3448                                       &best_highpc, &cu, pst);
3449   if (has_pc_info == 1 && best_lowpc < best_highpc)
3450     /* Store the contiguous range if it is not empty; it can be empty for
3451        CUs with no code.  */
3452     addrmap_set_empty (objfile->psymtabs_addrmap,
3453                        best_lowpc + baseaddr,
3454                        best_highpc + baseaddr - 1, pst);
3455
3456   /* Check if comp unit has_children.
3457      If so, read the rest of the partial symbols from this comp unit.
3458      If not, there's no more debug_info for this comp unit.  */
3459   if (has_children)
3460     {
3461       struct partial_die_info *first_die;
3462       CORE_ADDR lowpc, highpc;
3463
3464       lowpc = ((CORE_ADDR) -1);
3465       highpc = ((CORE_ADDR) 0);
3466
3467       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3468
3469       scan_partial_symbols (first_die, &lowpc, &highpc,
3470                             ! has_pc_info, &cu);
3471
3472       /* If we didn't find a lowpc, set it to highpc to avoid
3473          complaints from `maint check'.  */
3474       if (lowpc == ((CORE_ADDR) -1))
3475         lowpc = highpc;
3476
3477       /* If the compilation unit didn't have an explicit address range,
3478          then use the information extracted from its child dies.  */
3479       if (! has_pc_info)
3480         {
3481           best_lowpc = lowpc;
3482           best_highpc = highpc;
3483         }
3484     }
3485   pst->textlow = best_lowpc + baseaddr;
3486   pst->texthigh = best_highpc + baseaddr;
3487
3488   pst->n_global_syms = objfile->global_psymbols.next -
3489     (objfile->global_psymbols.list + pst->globals_offset);
3490   pst->n_static_syms = objfile->static_psymbols.next -
3491     (objfile->static_psymbols.list + pst->statics_offset);
3492   sort_pst_symbols (pst);
3493
3494   if (is_debug_types_section)
3495     {
3496       /* It's not clear we want to do anything with stmt lists here.
3497          Waiting to see what gcc ultimately does.  */
3498     }
3499   else
3500     {
3501       /* Get the list of files included in the current compilation unit,
3502          and build a psymtab for each of them.  */
3503       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3504     }
3505
3506   do_cleanups (back_to_inner);
3507 }
3508
3509 /* Traversal function for htab_traverse_noresize.
3510    Process one .debug_types comp-unit.  */
3511
3512 static int
3513 process_type_comp_unit (void **slot, void *info)
3514 {
3515   struct signatured_type *entry = (struct signatured_type *) *slot;
3516   struct dwarf2_per_cu_data *this_cu;
3517
3518   gdb_assert (info == NULL);
3519   this_cu = &entry->per_cu;
3520
3521   gdb_assert (this_cu->debug_types_section->readin);
3522   process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3523
3524   return 1;
3525 }
3526
3527 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3528    Build partial symbol tables for the .debug_types comp-units.  */
3529
3530 static void
3531 build_type_psymtabs (struct objfile *objfile)
3532 {
3533   if (! create_debug_types_hash_table (objfile))
3534     return;
3535
3536   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3537                           process_type_comp_unit, NULL);
3538 }
3539
3540 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3541
3542 static void
3543 psymtabs_addrmap_cleanup (void *o)
3544 {
3545   struct objfile *objfile = o;
3546
3547   objfile->psymtabs_addrmap = NULL;
3548 }
3549
3550 /* Build the partial symbol table by doing a quick pass through the
3551    .debug_info and .debug_abbrev sections.  */
3552
3553 static void
3554 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3555 {
3556   struct cleanup *back_to, *addrmap_cleanup;
3557   struct obstack temp_obstack;
3558   int i;
3559
3560   dwarf2_per_objfile->reading_partial_symbols = 1;
3561
3562   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3563
3564   /* Any cached compilation units will be linked by the per-objfile
3565      read_in_chain.  Make sure to free them when we're done.  */
3566   back_to = make_cleanup (free_cached_comp_units, NULL);
3567
3568   build_type_psymtabs (objfile);
3569
3570   create_all_comp_units (objfile);
3571
3572   /* Create a temporary address map on a temporary obstack.  We later
3573      copy this to the final obstack.  */
3574   obstack_init (&temp_obstack);
3575   make_cleanup_obstack_free (&temp_obstack);
3576   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3577   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3578
3579   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3580     {
3581       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3582
3583       process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3584     }
3585
3586   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3587                                                     &objfile->objfile_obstack);
3588   discard_cleanups (addrmap_cleanup);
3589
3590   do_cleanups (back_to);
3591 }
3592
3593 /* Load the partial DIEs for a secondary CU into memory.  */
3594
3595 static void
3596 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3597 {
3598   struct objfile *objfile = this_cu->objfile;
3599   bfd *abfd = objfile->obfd;
3600   gdb_byte *info_ptr;
3601   struct die_info *comp_unit_die;
3602   struct dwarf2_cu *cu;
3603   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3604   int has_children;
3605   struct die_reader_specs reader_specs;
3606   int read_cu = 0;
3607   struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3608
3609   gdb_assert (! this_cu->debug_types_section);
3610
3611   gdb_assert (section->readin);
3612   info_ptr = section->buffer + this_cu->offset;
3613
3614   if (this_cu->cu == NULL)
3615     {
3616       cu = xmalloc (sizeof (*cu));
3617       init_one_comp_unit (cu, this_cu);
3618
3619       read_cu = 1;
3620
3621       /* If an error occurs while loading, release our storage.  */
3622       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3623
3624       info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3625                                                 0);
3626
3627       /* Skip dummy compilation units.  */
3628       if (info_ptr >= (section->buffer + section->size)
3629           || peek_abbrev_code (abfd, info_ptr) == 0)
3630         {
3631           do_cleanups (free_cu_cleanup);
3632           return;
3633         }
3634
3635       /* Link this CU into read_in_chain.  */
3636       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3637       dwarf2_per_objfile->read_in_chain = this_cu;
3638     }
3639   else
3640     {
3641       cu = this_cu->cu;
3642       info_ptr += cu->header.first_die_offset;
3643     }
3644
3645   /* Read the abbrevs for this compilation unit into a table.  */
3646   gdb_assert (cu->dwarf2_abbrevs == NULL);
3647   dwarf2_read_abbrevs (cu);
3648   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3649
3650   /* Read the compilation unit die.  */
3651   init_cu_die_reader (&reader_specs, cu);
3652   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3653                             &has_children);
3654
3655   prepare_one_comp_unit (cu, comp_unit_die);
3656
3657   /* Check if comp unit has_children.
3658      If so, read the rest of the partial symbols from this comp unit.
3659      If not, there's no more debug_info for this comp unit.  */
3660   if (has_children)
3661     load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3662
3663   do_cleanups (free_abbrevs_cleanup);
3664
3665   if (read_cu)
3666     {
3667       /* We've successfully allocated this compilation unit.  Let our
3668          caller clean it up when finished with it.  */
3669       discard_cleanups (free_cu_cleanup);
3670     }
3671 }
3672
3673 /* Create a list of all compilation units in OBJFILE.
3674    This is only done for -readnow and building partial symtabs.  */
3675
3676 static void
3677 create_all_comp_units (struct objfile *objfile)
3678 {
3679   int n_allocated;
3680   int n_comp_units;
3681   struct dwarf2_per_cu_data **all_comp_units;
3682   gdb_byte *info_ptr;
3683
3684   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3685   info_ptr = dwarf2_per_objfile->info.buffer;
3686
3687   n_comp_units = 0;
3688   n_allocated = 10;
3689   all_comp_units = xmalloc (n_allocated
3690                             * sizeof (struct dwarf2_per_cu_data *));
3691
3692   while (info_ptr < dwarf2_per_objfile->info.buffer
3693          + dwarf2_per_objfile->info.size)
3694     {
3695       unsigned int length, initial_length_size;
3696       struct dwarf2_per_cu_data *this_cu;
3697       unsigned int offset;
3698
3699       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3700
3701       /* Read just enough information to find out where the next
3702          compilation unit is.  */
3703       length = read_initial_length (objfile->obfd, info_ptr,
3704                                     &initial_length_size);
3705
3706       /* Save the compilation unit for later lookup.  */
3707       this_cu = obstack_alloc (&objfile->objfile_obstack,
3708                                sizeof (struct dwarf2_per_cu_data));
3709       memset (this_cu, 0, sizeof (*this_cu));
3710       this_cu->offset = offset;
3711       this_cu->length = length + initial_length_size;
3712       this_cu->objfile = objfile;
3713
3714       if (n_comp_units == n_allocated)
3715         {
3716           n_allocated *= 2;
3717           all_comp_units = xrealloc (all_comp_units,
3718                                      n_allocated
3719                                      * sizeof (struct dwarf2_per_cu_data *));
3720         }
3721       all_comp_units[n_comp_units++] = this_cu;
3722
3723       info_ptr = info_ptr + this_cu->length;
3724     }
3725
3726   dwarf2_per_objfile->all_comp_units
3727     = obstack_alloc (&objfile->objfile_obstack,
3728                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3729   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3730           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3731   xfree (all_comp_units);
3732   dwarf2_per_objfile->n_comp_units = n_comp_units;
3733 }
3734
3735 /* Process all loaded DIEs for compilation unit CU, starting at
3736    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3737    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3738    DW_AT_ranges).  If NEED_PC is set, then this function will set
3739    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3740    and record the covered ranges in the addrmap.  */
3741
3742 static void
3743 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3744                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3745 {
3746   struct partial_die_info *pdi;
3747
3748   /* Now, march along the PDI's, descending into ones which have
3749      interesting children but skipping the children of the other ones,
3750      until we reach the end of the compilation unit.  */
3751
3752   pdi = first_die;
3753
3754   while (pdi != NULL)
3755     {
3756       fixup_partial_die (pdi, cu);
3757
3758       /* Anonymous namespaces or modules have no name but have interesting
3759          children, so we need to look at them.  Ditto for anonymous
3760          enums.  */
3761
3762       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3763           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3764         {
3765           switch (pdi->tag)
3766             {
3767             case DW_TAG_subprogram:
3768               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3769               break;
3770             case DW_TAG_constant:
3771             case DW_TAG_variable:
3772             case DW_TAG_typedef:
3773             case DW_TAG_union_type:
3774               if (!pdi->is_declaration)
3775                 {
3776                   add_partial_symbol (pdi, cu);
3777                 }
3778               break;
3779             case DW_TAG_class_type:
3780             case DW_TAG_interface_type:
3781             case DW_TAG_structure_type:
3782               if (!pdi->is_declaration)
3783                 {
3784                   add_partial_symbol (pdi, cu);
3785                 }
3786               break;
3787             case DW_TAG_enumeration_type:
3788               if (!pdi->is_declaration)
3789                 add_partial_enumeration (pdi, cu);
3790               break;
3791             case DW_TAG_base_type:
3792             case DW_TAG_subrange_type:
3793               /* File scope base type definitions are added to the partial
3794                  symbol table.  */
3795               add_partial_symbol (pdi, cu);
3796               break;
3797             case DW_TAG_namespace:
3798               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3799               break;
3800             case DW_TAG_module:
3801               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3802               break;
3803             default:
3804               break;
3805             }
3806         }
3807
3808       /* If the die has a sibling, skip to the sibling.  */
3809
3810       pdi = pdi->die_sibling;
3811     }
3812 }
3813
3814 /* Functions used to compute the fully scoped name of a partial DIE.
3815
3816    Normally, this is simple.  For C++, the parent DIE's fully scoped
3817    name is concatenated with "::" and the partial DIE's name.  For
3818    Java, the same thing occurs except that "." is used instead of "::".
3819    Enumerators are an exception; they use the scope of their parent
3820    enumeration type, i.e. the name of the enumeration type is not
3821    prepended to the enumerator.
3822
3823    There are two complexities.  One is DW_AT_specification; in this
3824    case "parent" means the parent of the target of the specification,
3825    instead of the direct parent of the DIE.  The other is compilers
3826    which do not emit DW_TAG_namespace; in this case we try to guess
3827    the fully qualified name of structure types from their members'
3828    linkage names.  This must be done using the DIE's children rather
3829    than the children of any DW_AT_specification target.  We only need
3830    to do this for structures at the top level, i.e. if the target of
3831    any DW_AT_specification (if any; otherwise the DIE itself) does not
3832    have a parent.  */
3833
3834 /* Compute the scope prefix associated with PDI's parent, in
3835    compilation unit CU.  The result will be allocated on CU's
3836    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3837    field.  NULL is returned if no prefix is necessary.  */
3838 static char *
3839 partial_die_parent_scope (struct partial_die_info *pdi,
3840                           struct dwarf2_cu *cu)
3841 {
3842   char *grandparent_scope;
3843   struct partial_die_info *parent, *real_pdi;
3844
3845   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3846      then this means the parent of the specification DIE.  */
3847
3848   real_pdi = pdi;
3849   while (real_pdi->has_specification)
3850     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3851
3852   parent = real_pdi->die_parent;
3853   if (parent == NULL)
3854     return NULL;
3855
3856   if (parent->scope_set)
3857     return parent->scope;
3858
3859   fixup_partial_die (parent, cu);
3860
3861   grandparent_scope = partial_die_parent_scope (parent, cu);
3862
3863   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3864      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3865      Work around this problem here.  */
3866   if (cu->language == language_cplus
3867       && parent->tag == DW_TAG_namespace
3868       && strcmp (parent->name, "::") == 0
3869       && grandparent_scope == NULL)
3870     {
3871       parent->scope = NULL;
3872       parent->scope_set = 1;
3873       return NULL;
3874     }
3875
3876   if (pdi->tag == DW_TAG_enumerator)
3877     /* Enumerators should not get the name of the enumeration as a prefix.  */
3878     parent->scope = grandparent_scope;
3879   else if (parent->tag == DW_TAG_namespace
3880       || parent->tag == DW_TAG_module
3881       || parent->tag == DW_TAG_structure_type
3882       || parent->tag == DW_TAG_class_type
3883       || parent->tag == DW_TAG_interface_type
3884       || parent->tag == DW_TAG_union_type
3885       || parent->tag == DW_TAG_enumeration_type)
3886     {
3887       if (grandparent_scope == NULL)
3888         parent->scope = parent->name;
3889       else
3890         parent->scope = typename_concat (&cu->comp_unit_obstack,
3891                                          grandparent_scope,
3892                                          parent->name, 0, cu);
3893     }
3894   else
3895     {
3896       /* FIXME drow/2004-04-01: What should we be doing with
3897          function-local names?  For partial symbols, we should probably be
3898          ignoring them.  */
3899       complaint (&symfile_complaints,
3900                  _("unhandled containing DIE tag %d for DIE at %d"),
3901                  parent->tag, pdi->offset);
3902       parent->scope = grandparent_scope;
3903     }
3904
3905   parent->scope_set = 1;
3906   return parent->scope;
3907 }
3908
3909 /* Return the fully scoped name associated with PDI, from compilation unit
3910    CU.  The result will be allocated with malloc.  */
3911 static char *
3912 partial_die_full_name (struct partial_die_info *pdi,
3913                        struct dwarf2_cu *cu)
3914 {
3915   char *parent_scope;
3916
3917   /* If this is a template instantiation, we can not work out the
3918      template arguments from partial DIEs.  So, unfortunately, we have
3919      to go through the full DIEs.  At least any work we do building
3920      types here will be reused if full symbols are loaded later.  */
3921   if (pdi->has_template_arguments)
3922     {
3923       fixup_partial_die (pdi, cu);
3924
3925       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3926         {
3927           struct die_info *die;
3928           struct attribute attr;
3929           struct dwarf2_cu *ref_cu = cu;
3930
3931           attr.name = 0;
3932           attr.form = DW_FORM_ref_addr;
3933           attr.u.addr = pdi->offset;
3934           die = follow_die_ref (NULL, &attr, &ref_cu);
3935
3936           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3937         }
3938     }
3939
3940   parent_scope = partial_die_parent_scope (pdi, cu);
3941   if (parent_scope == NULL)
3942     return NULL;
3943   else
3944     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3945 }
3946
3947 static void
3948 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3949 {
3950   struct objfile *objfile = cu->objfile;
3951   CORE_ADDR addr = 0;
3952   char *actual_name = NULL;
3953   const struct partial_symbol *psym = NULL;
3954   CORE_ADDR baseaddr;
3955   int built_actual_name = 0;
3956
3957   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3958
3959   actual_name = partial_die_full_name (pdi, cu);
3960   if (actual_name)
3961     built_actual_name = 1;
3962
3963   if (actual_name == NULL)
3964     actual_name = pdi->name;
3965
3966   switch (pdi->tag)
3967     {
3968     case DW_TAG_subprogram:
3969       if (pdi->is_external || cu->language == language_ada)
3970         {
3971           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3972              of the global scope.  But in Ada, we want to be able to access
3973              nested procedures globally.  So all Ada subprograms are stored
3974              in the global scope.  */
3975           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3976              mst_text, objfile); */
3977           add_psymbol_to_list (actual_name, strlen (actual_name),
3978                                built_actual_name,
3979                                VAR_DOMAIN, LOC_BLOCK,
3980                                &objfile->global_psymbols,
3981                                0, pdi->lowpc + baseaddr,
3982                                cu->language, objfile);
3983         }
3984       else
3985         {
3986           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3987              mst_file_text, objfile); */
3988           add_psymbol_to_list (actual_name, strlen (actual_name),
3989                                built_actual_name,
3990                                VAR_DOMAIN, LOC_BLOCK,
3991                                &objfile->static_psymbols,
3992                                0, pdi->lowpc + baseaddr,
3993                                cu->language, objfile);
3994         }
3995       break;
3996     case DW_TAG_constant:
3997       {
3998         struct psymbol_allocation_list *list;
3999
4000         if (pdi->is_external)
4001           list = &objfile->global_psymbols;
4002         else
4003           list = &objfile->static_psymbols;
4004         add_psymbol_to_list (actual_name, strlen (actual_name),
4005                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4006                              list, 0, 0, cu->language, objfile);
4007       }
4008       break;
4009     case DW_TAG_variable:
4010       if (pdi->locdesc)
4011         addr = decode_locdesc (pdi->locdesc, cu);
4012
4013       if (pdi->locdesc
4014           && addr == 0
4015           && !dwarf2_per_objfile->has_section_at_zero)
4016         {
4017           /* A global or static variable may also have been stripped
4018              out by the linker if unused, in which case its address
4019              will be nullified; do not add such variables into partial
4020              symbol table then.  */
4021         }
4022       else if (pdi->is_external)
4023         {
4024           /* Global Variable.
4025              Don't enter into the minimal symbol tables as there is
4026              a minimal symbol table entry from the ELF symbols already.
4027              Enter into partial symbol table if it has a location
4028              descriptor or a type.
4029              If the location descriptor is missing, new_symbol will create
4030              a LOC_UNRESOLVED symbol, the address of the variable will then
4031              be determined from the minimal symbol table whenever the variable
4032              is referenced.
4033              The address for the partial symbol table entry is not
4034              used by GDB, but it comes in handy for debugging partial symbol
4035              table building.  */
4036
4037           if (pdi->locdesc || pdi->has_type)
4038             add_psymbol_to_list (actual_name, strlen (actual_name),
4039                                  built_actual_name,
4040                                  VAR_DOMAIN, LOC_STATIC,
4041                                  &objfile->global_psymbols,
4042                                  0, addr + baseaddr,
4043                                  cu->language, objfile);
4044         }
4045       else
4046         {
4047           /* Static Variable.  Skip symbols without location descriptors.  */
4048           if (pdi->locdesc == NULL)
4049             {
4050               if (built_actual_name)
4051                 xfree (actual_name);
4052               return;
4053             }
4054           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4055              mst_file_data, objfile); */
4056           add_psymbol_to_list (actual_name, strlen (actual_name),
4057                                built_actual_name,
4058                                VAR_DOMAIN, LOC_STATIC,
4059                                &objfile->static_psymbols,
4060                                0, addr + baseaddr,
4061                                cu->language, objfile);
4062         }
4063       break;
4064     case DW_TAG_typedef:
4065     case DW_TAG_base_type:
4066     case DW_TAG_subrange_type:
4067       add_psymbol_to_list (actual_name, strlen (actual_name),
4068                            built_actual_name,
4069                            VAR_DOMAIN, LOC_TYPEDEF,
4070                            &objfile->static_psymbols,
4071                            0, (CORE_ADDR) 0, cu->language, objfile);
4072       break;
4073     case DW_TAG_namespace:
4074       add_psymbol_to_list (actual_name, strlen (actual_name),
4075                            built_actual_name,
4076                            VAR_DOMAIN, LOC_TYPEDEF,
4077                            &objfile->global_psymbols,
4078                            0, (CORE_ADDR) 0, cu->language, objfile);
4079       break;
4080     case DW_TAG_class_type:
4081     case DW_TAG_interface_type:
4082     case DW_TAG_structure_type:
4083     case DW_TAG_union_type:
4084     case DW_TAG_enumeration_type:
4085       /* Skip external references.  The DWARF standard says in the section
4086          about "Structure, Union, and Class Type Entries": "An incomplete
4087          structure, union or class type is represented by a structure,
4088          union or class entry that does not have a byte size attribute
4089          and that has a DW_AT_declaration attribute."  */
4090       if (!pdi->has_byte_size && pdi->is_declaration)
4091         {
4092           if (built_actual_name)
4093             xfree (actual_name);
4094           return;
4095         }
4096
4097       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4098          static vs. global.  */
4099       add_psymbol_to_list (actual_name, strlen (actual_name),
4100                            built_actual_name,
4101                            STRUCT_DOMAIN, LOC_TYPEDEF,
4102                            (cu->language == language_cplus
4103                             || cu->language == language_java)
4104                            ? &objfile->global_psymbols
4105                            : &objfile->static_psymbols,
4106                            0, (CORE_ADDR) 0, cu->language, objfile);
4107
4108       break;
4109     case DW_TAG_enumerator:
4110       add_psymbol_to_list (actual_name, strlen (actual_name),
4111                            built_actual_name,
4112                            VAR_DOMAIN, LOC_CONST,
4113                            (cu->language == language_cplus
4114                             || cu->language == language_java)
4115                            ? &objfile->global_psymbols
4116                            : &objfile->static_psymbols,
4117                            0, (CORE_ADDR) 0, cu->language, objfile);
4118       break;
4119     default:
4120       break;
4121     }
4122
4123   if (built_actual_name)
4124     xfree (actual_name);
4125 }
4126
4127 /* Read a partial die corresponding to a namespace; also, add a symbol
4128    corresponding to that namespace to the symbol table.  NAMESPACE is
4129    the name of the enclosing namespace.  */
4130
4131 static void
4132 add_partial_namespace (struct partial_die_info *pdi,
4133                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4134                        int need_pc, struct dwarf2_cu *cu)
4135 {
4136   /* Add a symbol for the namespace.  */
4137
4138   add_partial_symbol (pdi, cu);
4139
4140   /* Now scan partial symbols in that namespace.  */
4141
4142   if (pdi->has_children)
4143     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4144 }
4145
4146 /* Read a partial die corresponding to a Fortran module.  */
4147
4148 static void
4149 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4150                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4151 {
4152   /* Now scan partial symbols in that module.  */
4153
4154   if (pdi->has_children)
4155     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4156 }
4157
4158 /* Read a partial die corresponding to a subprogram and create a partial
4159    symbol for that subprogram.  When the CU language allows it, this
4160    routine also defines a partial symbol for each nested subprogram
4161    that this subprogram contains.
4162
4163    DIE my also be a lexical block, in which case we simply search
4164    recursively for suprograms defined inside that lexical block.
4165    Again, this is only performed when the CU language allows this
4166    type of definitions.  */
4167
4168 static void
4169 add_partial_subprogram (struct partial_die_info *pdi,
4170                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4171                         int need_pc, struct dwarf2_cu *cu)
4172 {
4173   if (pdi->tag == DW_TAG_subprogram)
4174     {
4175       if (pdi->has_pc_info)
4176         {
4177           if (pdi->lowpc < *lowpc)
4178             *lowpc = pdi->lowpc;
4179           if (pdi->highpc > *highpc)
4180             *highpc = pdi->highpc;
4181           if (need_pc)
4182             {
4183               CORE_ADDR baseaddr;
4184               struct objfile *objfile = cu->objfile;
4185
4186               baseaddr = ANOFFSET (objfile->section_offsets,
4187                                    SECT_OFF_TEXT (objfile));
4188               addrmap_set_empty (objfile->psymtabs_addrmap,
4189                                  pdi->lowpc + baseaddr,
4190                                  pdi->highpc - 1 + baseaddr,
4191                                  cu->per_cu->v.psymtab);
4192             }
4193           if (!pdi->is_declaration)
4194             /* Ignore subprogram DIEs that do not have a name, they are
4195                illegal.  Do not emit a complaint at this point, we will
4196                do so when we convert this psymtab into a symtab.  */
4197             if (pdi->name)
4198               add_partial_symbol (pdi, cu);
4199         }
4200     }
4201
4202   if (! pdi->has_children)
4203     return;
4204
4205   if (cu->language == language_ada)
4206     {
4207       pdi = pdi->die_child;
4208       while (pdi != NULL)
4209         {
4210           fixup_partial_die (pdi, cu);
4211           if (pdi->tag == DW_TAG_subprogram
4212               || pdi->tag == DW_TAG_lexical_block)
4213             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4214           pdi = pdi->die_sibling;
4215         }
4216     }
4217 }
4218
4219 /* Read a partial die corresponding to an enumeration type.  */
4220
4221 static void
4222 add_partial_enumeration (struct partial_die_info *enum_pdi,
4223                          struct dwarf2_cu *cu)
4224 {
4225   struct partial_die_info *pdi;
4226
4227   if (enum_pdi->name != NULL)
4228     add_partial_symbol (enum_pdi, cu);
4229
4230   pdi = enum_pdi->die_child;
4231   while (pdi)
4232     {
4233       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4234         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4235       else
4236         add_partial_symbol (pdi, cu);
4237       pdi = pdi->die_sibling;
4238     }
4239 }
4240
4241 /* Return the initial uleb128 in the die at INFO_PTR.  */
4242
4243 static unsigned int
4244 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4245 {
4246   unsigned int bytes_read;
4247
4248   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4249 }
4250
4251 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4252    Return the corresponding abbrev, or NULL if the number is zero (indicating
4253    an empty DIE).  In either case *BYTES_READ will be set to the length of
4254    the initial number.  */
4255
4256 static struct abbrev_info *
4257 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4258                  struct dwarf2_cu *cu)
4259 {
4260   bfd *abfd = cu->objfile->obfd;
4261   unsigned int abbrev_number;
4262   struct abbrev_info *abbrev;
4263
4264   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4265
4266   if (abbrev_number == 0)
4267     return NULL;
4268
4269   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4270   if (!abbrev)
4271     {
4272       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4273              abbrev_number, bfd_get_filename (abfd));
4274     }
4275
4276   return abbrev;
4277 }
4278
4279 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4280    Returns a pointer to the end of a series of DIEs, terminated by an empty
4281    DIE.  Any children of the skipped DIEs will also be skipped.  */
4282
4283 static gdb_byte *
4284 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4285 {
4286   struct abbrev_info *abbrev;
4287   unsigned int bytes_read;
4288
4289   while (1)
4290     {
4291       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4292       if (abbrev == NULL)
4293         return info_ptr + bytes_read;
4294       else
4295         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4296     }
4297 }
4298
4299 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4300    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4301    abbrev corresponding to that skipped uleb128 should be passed in
4302    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4303    children.  */
4304
4305 static gdb_byte *
4306 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4307               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4308 {
4309   unsigned int bytes_read;
4310   struct attribute attr;
4311   bfd *abfd = cu->objfile->obfd;
4312   unsigned int form, i;
4313
4314   for (i = 0; i < abbrev->num_attrs; i++)
4315     {
4316       /* The only abbrev we care about is DW_AT_sibling.  */
4317       if (abbrev->attrs[i].name == DW_AT_sibling)
4318         {
4319           read_attribute (&attr, &abbrev->attrs[i],
4320                           abfd, info_ptr, cu);
4321           if (attr.form == DW_FORM_ref_addr)
4322             complaint (&symfile_complaints,
4323                        _("ignoring absolute DW_AT_sibling"));
4324           else
4325             return buffer + dwarf2_get_ref_die_offset (&attr);
4326         }
4327
4328       /* If it isn't DW_AT_sibling, skip this attribute.  */
4329       form = abbrev->attrs[i].form;
4330     skip_attribute:
4331       switch (form)
4332         {
4333         case DW_FORM_ref_addr:
4334           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4335              and later it is offset sized.  */
4336           if (cu->header.version == 2)
4337             info_ptr += cu->header.addr_size;
4338           else
4339             info_ptr += cu->header.offset_size;
4340           break;
4341         case DW_FORM_addr:
4342           info_ptr += cu->header.addr_size;
4343           break;
4344         case DW_FORM_data1:
4345         case DW_FORM_ref1:
4346         case DW_FORM_flag:
4347           info_ptr += 1;
4348           break;
4349         case DW_FORM_flag_present:
4350           break;
4351         case DW_FORM_data2:
4352         case DW_FORM_ref2:
4353           info_ptr += 2;
4354           break;
4355         case DW_FORM_data4:
4356         case DW_FORM_ref4:
4357           info_ptr += 4;
4358           break;
4359         case DW_FORM_data8:
4360         case DW_FORM_ref8:
4361         case DW_FORM_ref_sig8:
4362           info_ptr += 8;
4363           break;
4364         case DW_FORM_string:
4365           read_direct_string (abfd, info_ptr, &bytes_read);
4366           info_ptr += bytes_read;
4367           break;
4368         case DW_FORM_sec_offset:
4369         case DW_FORM_strp:
4370           info_ptr += cu->header.offset_size;
4371           break;
4372         case DW_FORM_exprloc:
4373         case DW_FORM_block:
4374           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4375           info_ptr += bytes_read;
4376           break;
4377         case DW_FORM_block1:
4378           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4379           break;
4380         case DW_FORM_block2:
4381           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4382           break;
4383         case DW_FORM_block4:
4384           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4385           break;
4386         case DW_FORM_sdata:
4387         case DW_FORM_udata:
4388         case DW_FORM_ref_udata:
4389           info_ptr = skip_leb128 (abfd, info_ptr);
4390           break;
4391         case DW_FORM_indirect:
4392           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4393           info_ptr += bytes_read;
4394           /* We need to continue parsing from here, so just go back to
4395              the top.  */
4396           goto skip_attribute;
4397
4398         default:
4399           error (_("Dwarf Error: Cannot handle %s "
4400                    "in DWARF reader [in module %s]"),
4401                  dwarf_form_name (form),
4402                  bfd_get_filename (abfd));
4403         }
4404     }
4405
4406   if (abbrev->has_children)
4407     return skip_children (buffer, info_ptr, cu);
4408   else
4409     return info_ptr;
4410 }
4411
4412 /* Locate ORIG_PDI's sibling.
4413    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4414    in BUFFER.  */
4415
4416 static gdb_byte *
4417 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4418                     gdb_byte *buffer, gdb_byte *info_ptr,
4419                     bfd *abfd, struct dwarf2_cu *cu)
4420 {
4421   /* Do we know the sibling already?  */
4422
4423   if (orig_pdi->sibling)
4424     return orig_pdi->sibling;
4425
4426   /* Are there any children to deal with?  */
4427
4428   if (!orig_pdi->has_children)
4429     return info_ptr;
4430
4431   /* Skip the children the long way.  */
4432
4433   return skip_children (buffer, info_ptr, cu);
4434 }
4435
4436 /* Expand this partial symbol table into a full symbol table.  */
4437
4438 static void
4439 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4440 {
4441   if (pst != NULL)
4442     {
4443       if (pst->readin)
4444         {
4445           warning (_("bug: psymtab for %s is already read in."),
4446                    pst->filename);
4447         }
4448       else
4449         {
4450           if (info_verbose)
4451             {
4452               printf_filtered (_("Reading in symbols for %s..."),
4453                                pst->filename);
4454               gdb_flush (gdb_stdout);
4455             }
4456
4457           /* Restore our global data.  */
4458           dwarf2_per_objfile = objfile_data (pst->objfile,
4459                                              dwarf2_objfile_data_key);
4460
4461           /* If this psymtab is constructed from a debug-only objfile, the
4462              has_section_at_zero flag will not necessarily be correct.  We
4463              can get the correct value for this flag by looking at the data
4464              associated with the (presumably stripped) associated objfile.  */
4465           if (pst->objfile->separate_debug_objfile_backlink)
4466             {
4467               struct dwarf2_per_objfile *dpo_backlink
4468                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4469                                 dwarf2_objfile_data_key);
4470
4471               dwarf2_per_objfile->has_section_at_zero
4472                 = dpo_backlink->has_section_at_zero;
4473             }
4474
4475           dwarf2_per_objfile->reading_partial_symbols = 0;
4476
4477           psymtab_to_symtab_1 (pst);
4478
4479           /* Finish up the debug error message.  */
4480           if (info_verbose)
4481             printf_filtered (_("done.\n"));
4482         }
4483     }
4484 }
4485 \f
4486 /* Reading in full CUs.  */
4487
4488 /* Add PER_CU to the queue.  */
4489
4490 static void
4491 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4492 {
4493   struct dwarf2_queue_item *item;
4494
4495   per_cu->queued = 1;
4496   item = xmalloc (sizeof (*item));
4497   item->per_cu = per_cu;
4498   item->next = NULL;
4499
4500   if (dwarf2_queue == NULL)
4501     dwarf2_queue = item;
4502   else
4503     dwarf2_queue_tail->next = item;
4504
4505   dwarf2_queue_tail = item;
4506 }
4507
4508 /* Process the queue.  */
4509
4510 static void
4511 process_queue (void)
4512 {
4513   struct dwarf2_queue_item *item, *next_item;
4514
4515   /* The queue starts out with one item, but following a DIE reference
4516      may load a new CU, adding it to the end of the queue.  */
4517   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4518     {
4519       if (dwarf2_per_objfile->using_index
4520           ? !item->per_cu->v.quick->symtab
4521           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4522         process_full_comp_unit (item->per_cu);
4523
4524       item->per_cu->queued = 0;
4525       next_item = item->next;
4526       xfree (item);
4527     }
4528
4529   dwarf2_queue_tail = NULL;
4530 }
4531
4532 /* Free all allocated queue entries.  This function only releases anything if
4533    an error was thrown; if the queue was processed then it would have been
4534    freed as we went along.  */
4535
4536 static void
4537 dwarf2_release_queue (void *dummy)
4538 {
4539   struct dwarf2_queue_item *item, *last;
4540
4541   item = dwarf2_queue;
4542   while (item)
4543     {
4544       /* Anything still marked queued is likely to be in an
4545          inconsistent state, so discard it.  */
4546       if (item->per_cu->queued)
4547         {
4548           if (item->per_cu->cu != NULL)
4549             free_one_cached_comp_unit (item->per_cu->cu);
4550           item->per_cu->queued = 0;
4551         }
4552
4553       last = item;
4554       item = item->next;
4555       xfree (last);
4556     }
4557
4558   dwarf2_queue = dwarf2_queue_tail = NULL;
4559 }
4560
4561 /* Read in full symbols for PST, and anything it depends on.  */
4562
4563 static void
4564 psymtab_to_symtab_1 (struct partial_symtab *pst)
4565 {
4566   struct dwarf2_per_cu_data *per_cu;
4567   struct cleanup *back_to;
4568   int i;
4569
4570   for (i = 0; i < pst->number_of_dependencies; i++)
4571     if (!pst->dependencies[i]->readin)
4572       {
4573         /* Inform about additional files that need to be read in.  */
4574         if (info_verbose)
4575           {
4576             /* FIXME: i18n: Need to make this a single string.  */
4577             fputs_filtered (" ", gdb_stdout);
4578             wrap_here ("");
4579             fputs_filtered ("and ", gdb_stdout);
4580             wrap_here ("");
4581             printf_filtered ("%s...", pst->dependencies[i]->filename);
4582             wrap_here ("");     /* Flush output.  */
4583             gdb_flush (gdb_stdout);
4584           }
4585         psymtab_to_symtab_1 (pst->dependencies[i]);
4586       }
4587
4588   per_cu = pst->read_symtab_private;
4589
4590   if (per_cu == NULL)
4591     {
4592       /* It's an include file, no symbols to read for it.
4593          Everything is in the parent symtab.  */
4594       pst->readin = 1;
4595       return;
4596     }
4597
4598   dw2_do_instantiate_symtab (per_cu);
4599 }
4600
4601 /* Load the DIEs associated with PER_CU into memory.  */
4602
4603 static void
4604 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4605 {
4606   struct objfile *objfile = per_cu->objfile;
4607   bfd *abfd = objfile->obfd;
4608   struct dwarf2_cu *cu;
4609   unsigned int offset;
4610   gdb_byte *info_ptr, *beg_of_comp_unit;
4611   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4612   struct attribute *attr;
4613   int read_cu = 0;
4614
4615   gdb_assert (! per_cu->debug_types_section);
4616
4617   /* Set local variables from the partial symbol table info.  */
4618   offset = per_cu->offset;
4619
4620   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4621   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4622   beg_of_comp_unit = info_ptr;
4623
4624   if (per_cu->cu == NULL)
4625     {
4626       cu = xmalloc (sizeof (*cu));
4627       init_one_comp_unit (cu, per_cu);
4628
4629       read_cu = 1;
4630
4631       /* If an error occurs while loading, release our storage.  */
4632       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4633
4634       /* Read in the comp_unit header.  */
4635       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4636
4637       /* Skip dummy compilation units.  */
4638       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4639                        + dwarf2_per_objfile->info.size)
4640           || peek_abbrev_code (abfd, info_ptr) == 0)
4641         {
4642           do_cleanups (free_cu_cleanup);
4643           return;
4644         }
4645
4646       /* Complete the cu_header.  */
4647       cu->header.offset = offset;
4648       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4649
4650       /* Read the abbrevs for this compilation unit.  */
4651       dwarf2_read_abbrevs (cu);
4652       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4653
4654       /* Link this CU into read_in_chain.  */
4655       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4656       dwarf2_per_objfile->read_in_chain = per_cu;
4657     }
4658   else
4659     {
4660       cu = per_cu->cu;
4661       info_ptr += cu->header.first_die_offset;
4662     }
4663
4664   cu->dies = read_comp_unit (info_ptr, cu);
4665
4666   /* We try not to read any attributes in this function, because not
4667      all CUs needed for references have been loaded yet, and symbol
4668      table processing isn't initialized.  But we have to set the CU language,
4669      or we won't be able to build types correctly.  */
4670   prepare_one_comp_unit (cu, cu->dies);
4671
4672   /* Similarly, if we do not read the producer, we can not apply
4673      producer-specific interpretation.  */
4674   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4675   if (attr)
4676     cu->producer = DW_STRING (attr);
4677
4678   if (read_cu)
4679     {
4680       do_cleanups (free_abbrevs_cleanup);
4681
4682       /* We've successfully allocated this compilation unit.  Let our
4683          caller clean it up when finished with it.  */
4684       discard_cleanups (free_cu_cleanup);
4685     }
4686 }
4687
4688 /* Add a DIE to the delayed physname list.  */
4689
4690 static void
4691 add_to_method_list (struct type *type, int fnfield_index, int index,
4692                     const char *name, struct die_info *die,
4693                     struct dwarf2_cu *cu)
4694 {
4695   struct delayed_method_info mi;
4696   mi.type = type;
4697   mi.fnfield_index = fnfield_index;
4698   mi.index = index;
4699   mi.name = name;
4700   mi.die = die;
4701   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4702 }
4703
4704 /* A cleanup for freeing the delayed method list.  */
4705
4706 static void
4707 free_delayed_list (void *ptr)
4708 {
4709   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4710   if (cu->method_list != NULL)
4711     {
4712       VEC_free (delayed_method_info, cu->method_list);
4713       cu->method_list = NULL;
4714     }
4715 }
4716
4717 /* Compute the physnames of any methods on the CU's method list.
4718
4719    The computation of method physnames is delayed in order to avoid the
4720    (bad) condition that one of the method's formal parameters is of an as yet
4721    incomplete type.  */
4722
4723 static void
4724 compute_delayed_physnames (struct dwarf2_cu *cu)
4725 {
4726   int i;
4727   struct delayed_method_info *mi;
4728   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4729     {
4730       const char *physname;
4731       struct fn_fieldlist *fn_flp
4732         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4733       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4734       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4735     }
4736 }
4737
4738 /* Generate full symbol information for PER_CU, whose DIEs have
4739    already been loaded into memory.  */
4740
4741 static void
4742 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4743 {
4744   struct dwarf2_cu *cu = per_cu->cu;
4745   struct objfile *objfile = per_cu->objfile;
4746   CORE_ADDR lowpc, highpc;
4747   struct symtab *symtab;
4748   struct cleanup *back_to, *delayed_list_cleanup;
4749   CORE_ADDR baseaddr;
4750
4751   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4752
4753   buildsym_init ();
4754   back_to = make_cleanup (really_free_pendings, NULL);
4755   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4756
4757   cu->list_in_scope = &file_symbols;
4758
4759   /* Do line number decoding in read_file_scope () */
4760   process_die (cu->dies, cu);
4761
4762   /* Now that we have processed all the DIEs in the CU, all the types 
4763      should be complete, and it should now be safe to compute all of the
4764      physnames.  */
4765   compute_delayed_physnames (cu);
4766   do_cleanups (delayed_list_cleanup);
4767
4768   /* Some compilers don't define a DW_AT_high_pc attribute for the
4769      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4770      it, by scanning the DIE's below the compilation unit.  */
4771   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4772
4773   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4774
4775   if (symtab != NULL)
4776     {
4777       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4778
4779       /* Set symtab language to language from DW_AT_language.  If the
4780          compilation is from a C file generated by language preprocessors, do
4781          not set the language if it was already deduced by start_subfile.  */
4782       if (!(cu->language == language_c && symtab->language != language_c))
4783         symtab->language = cu->language;
4784
4785       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4786          produce DW_AT_location with location lists but it can be possibly
4787          invalid without -fvar-tracking.
4788
4789          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4790          needed, it would be wrong due to missing DW_AT_producer there.
4791
4792          Still one can confuse GDB by using non-standard GCC compilation
4793          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4794          */ 
4795       if (cu->has_loclist && gcc_4_minor >= 0)
4796         symtab->locations_valid = 1;
4797
4798       if (gcc_4_minor >= 5)
4799         symtab->epilogue_unwind_valid = 1;
4800
4801       symtab->call_site_htab = cu->call_site_htab;
4802     }
4803
4804   if (dwarf2_per_objfile->using_index)
4805     per_cu->v.quick->symtab = symtab;
4806   else
4807     {
4808       struct partial_symtab *pst = per_cu->v.psymtab;
4809       pst->symtab = symtab;
4810       pst->readin = 1;
4811     }
4812
4813   do_cleanups (back_to);
4814 }
4815
4816 /* Process a die and its children.  */
4817
4818 static void
4819 process_die (struct die_info *die, struct dwarf2_cu *cu)
4820 {
4821   switch (die->tag)
4822     {
4823     case DW_TAG_padding:
4824       break;
4825     case DW_TAG_compile_unit:
4826       read_file_scope (die, cu);
4827       break;
4828     case DW_TAG_type_unit:
4829       read_type_unit_scope (die, cu);
4830       break;
4831     case DW_TAG_subprogram:
4832     case DW_TAG_inlined_subroutine:
4833       read_func_scope (die, cu);
4834       break;
4835     case DW_TAG_lexical_block:
4836     case DW_TAG_try_block:
4837     case DW_TAG_catch_block:
4838       read_lexical_block_scope (die, cu);
4839       break;
4840     case DW_TAG_GNU_call_site:
4841       read_call_site_scope (die, cu);
4842       break;
4843     case DW_TAG_class_type:
4844     case DW_TAG_interface_type:
4845     case DW_TAG_structure_type:
4846     case DW_TAG_union_type:
4847       process_structure_scope (die, cu);
4848       break;
4849     case DW_TAG_enumeration_type:
4850       process_enumeration_scope (die, cu);
4851       break;
4852
4853     /* These dies have a type, but processing them does not create
4854        a symbol or recurse to process the children.  Therefore we can
4855        read them on-demand through read_type_die.  */
4856     case DW_TAG_subroutine_type:
4857     case DW_TAG_set_type:
4858     case DW_TAG_array_type:
4859     case DW_TAG_pointer_type:
4860     case DW_TAG_ptr_to_member_type:
4861     case DW_TAG_reference_type:
4862     case DW_TAG_string_type:
4863       break;
4864
4865     case DW_TAG_base_type:
4866     case DW_TAG_subrange_type:
4867     case DW_TAG_typedef:
4868       /* Add a typedef symbol for the type definition, if it has a
4869          DW_AT_name.  */
4870       new_symbol (die, read_type_die (die, cu), cu);
4871       break;
4872     case DW_TAG_common_block:
4873       read_common_block (die, cu);
4874       break;
4875     case DW_TAG_common_inclusion:
4876       break;
4877     case DW_TAG_namespace:
4878       processing_has_namespace_info = 1;
4879       read_namespace (die, cu);
4880       break;
4881     case DW_TAG_module:
4882       processing_has_namespace_info = 1;
4883       read_module (die, cu);
4884       break;
4885     case DW_TAG_imported_declaration:
4886     case DW_TAG_imported_module:
4887       processing_has_namespace_info = 1;
4888       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4889                                  || cu->language != language_fortran))
4890         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4891                    dwarf_tag_name (die->tag));
4892       read_import_statement (die, cu);
4893       break;
4894     default:
4895       new_symbol (die, NULL, cu);
4896       break;
4897     }
4898 }
4899
4900 /* A helper function for dwarf2_compute_name which determines whether DIE
4901    needs to have the name of the scope prepended to the name listed in the
4902    die.  */
4903
4904 static int
4905 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4906 {
4907   struct attribute *attr;
4908
4909   switch (die->tag)
4910     {
4911     case DW_TAG_namespace:
4912     case DW_TAG_typedef:
4913     case DW_TAG_class_type:
4914     case DW_TAG_interface_type:
4915     case DW_TAG_structure_type:
4916     case DW_TAG_union_type:
4917     case DW_TAG_enumeration_type:
4918     case DW_TAG_enumerator:
4919     case DW_TAG_subprogram:
4920     case DW_TAG_member:
4921       return 1;
4922
4923     case DW_TAG_variable:
4924     case DW_TAG_constant:
4925       /* We only need to prefix "globally" visible variables.  These include
4926          any variable marked with DW_AT_external or any variable that
4927          lives in a namespace.  [Variables in anonymous namespaces
4928          require prefixing, but they are not DW_AT_external.]  */
4929
4930       if (dwarf2_attr (die, DW_AT_specification, cu))
4931         {
4932           struct dwarf2_cu *spec_cu = cu;
4933
4934           return die_needs_namespace (die_specification (die, &spec_cu),
4935                                       spec_cu);
4936         }
4937
4938       attr = dwarf2_attr (die, DW_AT_external, cu);
4939       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4940           && die->parent->tag != DW_TAG_module)
4941         return 0;
4942       /* A variable in a lexical block of some kind does not need a
4943          namespace, even though in C++ such variables may be external
4944          and have a mangled name.  */
4945       if (die->parent->tag ==  DW_TAG_lexical_block
4946           || die->parent->tag ==  DW_TAG_try_block
4947           || die->parent->tag ==  DW_TAG_catch_block
4948           || die->parent->tag == DW_TAG_subprogram)
4949         return 0;
4950       return 1;
4951
4952     default:
4953       return 0;
4954     }
4955 }
4956
4957 /* Retrieve the last character from a mem_file.  */
4958
4959 static void
4960 do_ui_file_peek_last (void *object, const char *buffer, long length)
4961 {
4962   char *last_char_p = (char *) object;
4963
4964   if (length > 0)
4965     *last_char_p = buffer[length - 1];
4966 }
4967
4968 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4969    compute the physname for the object, which include a method's
4970    formal parameters (C++/Java) and return type (Java).
4971
4972    For Ada, return the DIE's linkage name rather than the fully qualified
4973    name.  PHYSNAME is ignored..
4974
4975    The result is allocated on the objfile_obstack and canonicalized.  */
4976
4977 static const char *
4978 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4979                      int physname)
4980 {
4981   struct objfile *objfile = cu->objfile;
4982
4983   if (name == NULL)
4984     name = dwarf2_name (die, cu);
4985
4986   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4987      compute it by typename_concat inside GDB.  */
4988   if (cu->language == language_ada
4989       || (cu->language == language_fortran && physname))
4990     {
4991       /* For Ada unit, we prefer the linkage name over the name, as
4992          the former contains the exported name, which the user expects
4993          to be able to reference.  Ideally, we want the user to be able
4994          to reference this entity using either natural or linkage name,
4995          but we haven't started looking at this enhancement yet.  */
4996       struct attribute *attr;
4997
4998       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4999       if (attr == NULL)
5000         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5001       if (attr && DW_STRING (attr))
5002         return DW_STRING (attr);
5003     }
5004
5005   /* These are the only languages we know how to qualify names in.  */
5006   if (name != NULL
5007       && (cu->language == language_cplus || cu->language == language_java
5008           || cu->language == language_fortran))
5009     {
5010       if (die_needs_namespace (die, cu))
5011         {
5012           long length;
5013           char *prefix;
5014           struct ui_file *buf;
5015
5016           prefix = determine_prefix (die, cu);
5017           buf = mem_fileopen ();
5018           if (*prefix != '\0')
5019             {
5020               char *prefixed_name = typename_concat (NULL, prefix, name,
5021                                                      physname, cu);
5022
5023               fputs_unfiltered (prefixed_name, buf);
5024               xfree (prefixed_name);
5025             }
5026           else
5027             fputs_unfiltered (name, buf);
5028
5029           /* Template parameters may be specified in the DIE's DW_AT_name, or
5030              as children with DW_TAG_template_type_param or
5031              DW_TAG_value_type_param.  If the latter, add them to the name
5032              here.  If the name already has template parameters, then
5033              skip this step; some versions of GCC emit both, and
5034              it is more efficient to use the pre-computed name.
5035
5036              Something to keep in mind about this process: it is very
5037              unlikely, or in some cases downright impossible, to produce
5038              something that will match the mangled name of a function.
5039              If the definition of the function has the same debug info,
5040              we should be able to match up with it anyway.  But fallbacks
5041              using the minimal symbol, for instance to find a method
5042              implemented in a stripped copy of libstdc++, will not work.
5043              If we do not have debug info for the definition, we will have to
5044              match them up some other way.
5045
5046              When we do name matching there is a related problem with function
5047              templates; two instantiated function templates are allowed to
5048              differ only by their return types, which we do not add here.  */
5049
5050           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5051             {
5052               struct attribute *attr;
5053               struct die_info *child;
5054               int first = 1;
5055
5056               die->building_fullname = 1;
5057
5058               for (child = die->child; child != NULL; child = child->sibling)
5059                 {
5060                   struct type *type;
5061                   long value;
5062                   gdb_byte *bytes;
5063                   struct dwarf2_locexpr_baton *baton;
5064                   struct value *v;
5065
5066                   if (child->tag != DW_TAG_template_type_param
5067                       && child->tag != DW_TAG_template_value_param)
5068                     continue;
5069
5070                   if (first)
5071                     {
5072                       fputs_unfiltered ("<", buf);
5073                       first = 0;
5074                     }
5075                   else
5076                     fputs_unfiltered (", ", buf);
5077
5078                   attr = dwarf2_attr (child, DW_AT_type, cu);
5079                   if (attr == NULL)
5080                     {
5081                       complaint (&symfile_complaints,
5082                                  _("template parameter missing DW_AT_type"));
5083                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5084                       continue;
5085                     }
5086                   type = die_type (child, cu);
5087
5088                   if (child->tag == DW_TAG_template_type_param)
5089                     {
5090                       c_print_type (type, "", buf, -1, 0);
5091                       continue;
5092                     }
5093
5094                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5095                   if (attr == NULL)
5096                     {
5097                       complaint (&symfile_complaints,
5098                                  _("template parameter missing "
5099                                    "DW_AT_const_value"));
5100                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5101                       continue;
5102                     }
5103
5104                   dwarf2_const_value_attr (attr, type, name,
5105                                            &cu->comp_unit_obstack, cu,
5106                                            &value, &bytes, &baton);
5107
5108                   if (TYPE_NOSIGN (type))
5109                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5110                        changed, this can use value_print instead.  */
5111                     c_printchar (value, type, buf);
5112                   else
5113                     {
5114                       struct value_print_options opts;
5115
5116                       if (baton != NULL)
5117                         v = dwarf2_evaluate_loc_desc (type, NULL,
5118                                                       baton->data,
5119                                                       baton->size,
5120                                                       baton->per_cu);
5121                       else if (bytes != NULL)
5122                         {
5123                           v = allocate_value (type);
5124                           memcpy (value_contents_writeable (v), bytes,
5125                                   TYPE_LENGTH (type));
5126                         }
5127                       else
5128                         v = value_from_longest (type, value);
5129
5130                       /* Specify decimal so that we do not depend on
5131                          the radix.  */
5132                       get_formatted_print_options (&opts, 'd');
5133                       opts.raw = 1;
5134                       value_print (v, buf, &opts);
5135                       release_value (v);
5136                       value_free (v);
5137                     }
5138                 }
5139
5140               die->building_fullname = 0;
5141
5142               if (!first)
5143                 {
5144                   /* Close the argument list, with a space if necessary
5145                      (nested templates).  */
5146                   char last_char = '\0';
5147                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5148                   if (last_char == '>')
5149                     fputs_unfiltered (" >", buf);
5150                   else
5151                     fputs_unfiltered (">", buf);
5152                 }
5153             }
5154
5155           /* For Java and C++ methods, append formal parameter type
5156              information, if PHYSNAME.  */
5157
5158           if (physname && die->tag == DW_TAG_subprogram
5159               && (cu->language == language_cplus
5160                   || cu->language == language_java))
5161             {
5162               struct type *type = read_type_die (die, cu);
5163
5164               c_type_print_args (type, buf, 1, cu->language);
5165
5166               if (cu->language == language_java)
5167                 {
5168                   /* For java, we must append the return type to method
5169                      names.  */
5170                   if (die->tag == DW_TAG_subprogram)
5171                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5172                                      0, 0);
5173                 }
5174               else if (cu->language == language_cplus)
5175                 {
5176                   /* Assume that an artificial first parameter is
5177                      "this", but do not crash if it is not.  RealView
5178                      marks unnamed (and thus unused) parameters as
5179                      artificial; there is no way to differentiate
5180                      the two cases.  */
5181                   if (TYPE_NFIELDS (type) > 0
5182                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5183                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5184                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5185                                                                         0))))
5186                     fputs_unfiltered (" const", buf);
5187                 }
5188             }
5189
5190           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5191                                        &length);
5192           ui_file_delete (buf);
5193
5194           if (cu->language == language_cplus)
5195             {
5196               char *cname
5197                 = dwarf2_canonicalize_name (name, cu,
5198                                             &objfile->objfile_obstack);
5199
5200               if (cname != NULL)
5201                 name = cname;
5202             }
5203         }
5204     }
5205
5206   return name;
5207 }
5208
5209 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5210    If scope qualifiers are appropriate they will be added.  The result
5211    will be allocated on the objfile_obstack, or NULL if the DIE does
5212    not have a name.  NAME may either be from a previous call to
5213    dwarf2_name or NULL.
5214
5215    The output string will be canonicalized (if C++/Java).  */
5216
5217 static const char *
5218 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5219 {
5220   return dwarf2_compute_name (name, die, cu, 0);
5221 }
5222
5223 /* Construct a physname for the given DIE in CU.  NAME may either be
5224    from a previous call to dwarf2_name or NULL.  The result will be
5225    allocated on the objfile_objstack or NULL if the DIE does not have a
5226    name.
5227
5228    The output string will be canonicalized (if C++/Java).  */
5229
5230 static const char *
5231 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5232 {
5233   struct objfile *objfile = cu->objfile;
5234   struct attribute *attr;
5235   const char *retval, *mangled = NULL, *canon = NULL;
5236   struct cleanup *back_to;
5237   int need_copy = 1;
5238
5239   /* In this case dwarf2_compute_name is just a shortcut not building anything
5240      on its own.  */
5241   if (!die_needs_namespace (die, cu))
5242     return dwarf2_compute_name (name, die, cu, 1);
5243
5244   back_to = make_cleanup (null_cleanup, NULL);
5245
5246   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5247   if (!attr)
5248     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5249
5250   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5251      has computed.  */
5252   if (attr && DW_STRING (attr))
5253     {
5254       char *demangled;
5255
5256       mangled = DW_STRING (attr);
5257
5258       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5259          type.  It is easier for GDB users to search for such functions as
5260          `name(params)' than `long name(params)'.  In such case the minimal
5261          symbol names do not match the full symbol names but for template
5262          functions there is never a need to look up their definition from their
5263          declaration so the only disadvantage remains the minimal symbol
5264          variant `long name(params)' does not have the proper inferior type.
5265          */
5266
5267       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5268                                             | (cu->language == language_java
5269                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5270                                                : DMGL_RET_DROP)));
5271       if (demangled)
5272         {
5273           make_cleanup (xfree, demangled);
5274           canon = demangled;
5275         }
5276       else
5277         {
5278           canon = mangled;
5279           need_copy = 0;
5280         }
5281     }
5282
5283   if (canon == NULL || check_physname)
5284     {
5285       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5286
5287       if (canon != NULL && strcmp (physname, canon) != 0)
5288         {
5289           /* It may not mean a bug in GDB.  The compiler could also
5290              compute DW_AT_linkage_name incorrectly.  But in such case
5291              GDB would need to be bug-to-bug compatible.  */
5292
5293           complaint (&symfile_complaints,
5294                      _("Computed physname <%s> does not match demangled <%s> "
5295                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5296                      physname, canon, mangled, die->offset, objfile->name);
5297
5298           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5299              is available here - over computed PHYSNAME.  It is safer
5300              against both buggy GDB and buggy compilers.  */
5301
5302           retval = canon;
5303         }
5304       else
5305         {
5306           retval = physname;
5307           need_copy = 0;
5308         }
5309     }
5310   else
5311     retval = canon;
5312
5313   if (need_copy)
5314     retval = obsavestring (retval, strlen (retval),
5315                            &objfile->objfile_obstack);
5316
5317   do_cleanups (back_to);
5318   return retval;
5319 }
5320
5321 /* Read the import statement specified by the given die and record it.  */
5322
5323 static void
5324 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5325 {
5326   struct objfile *objfile = cu->objfile;
5327   struct attribute *import_attr;
5328   struct die_info *imported_die, *child_die;
5329   struct dwarf2_cu *imported_cu;
5330   const char *imported_name;
5331   const char *imported_name_prefix;
5332   const char *canonical_name;
5333   const char *import_alias;
5334   const char *imported_declaration = NULL;
5335   const char *import_prefix;
5336   VEC (const_char_ptr) *excludes = NULL;
5337   struct cleanup *cleanups;
5338
5339   char *temp;
5340
5341   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5342   if (import_attr == NULL)
5343     {
5344       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5345                  dwarf_tag_name (die->tag));
5346       return;
5347     }
5348
5349   imported_cu = cu;
5350   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5351   imported_name = dwarf2_name (imported_die, imported_cu);
5352   if (imported_name == NULL)
5353     {
5354       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5355
5356         The import in the following code:
5357         namespace A
5358           {
5359             typedef int B;
5360           }
5361
5362         int main ()
5363           {
5364             using A::B;
5365             B b;
5366             return b;
5367           }
5368
5369         ...
5370          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5371             <52>   DW_AT_decl_file   : 1
5372             <53>   DW_AT_decl_line   : 6
5373             <54>   DW_AT_import      : <0x75>
5374          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5375             <59>   DW_AT_name        : B
5376             <5b>   DW_AT_decl_file   : 1
5377             <5c>   DW_AT_decl_line   : 2
5378             <5d>   DW_AT_type        : <0x6e>
5379         ...
5380          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5381             <76>   DW_AT_byte_size   : 4
5382             <77>   DW_AT_encoding    : 5        (signed)
5383
5384         imports the wrong die ( 0x75 instead of 0x58 ).
5385         This case will be ignored until the gcc bug is fixed.  */
5386       return;
5387     }
5388
5389   /* Figure out the local name after import.  */
5390   import_alias = dwarf2_name (die, cu);
5391
5392   /* Figure out where the statement is being imported to.  */
5393   import_prefix = determine_prefix (die, cu);
5394
5395   /* Figure out what the scope of the imported die is and prepend it
5396      to the name of the imported die.  */
5397   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5398
5399   if (imported_die->tag != DW_TAG_namespace
5400       && imported_die->tag != DW_TAG_module)
5401     {
5402       imported_declaration = imported_name;
5403       canonical_name = imported_name_prefix;
5404     }
5405   else if (strlen (imported_name_prefix) > 0)
5406     {
5407       temp = alloca (strlen (imported_name_prefix)
5408                      + 2 + strlen (imported_name) + 1);
5409       strcpy (temp, imported_name_prefix);
5410       strcat (temp, "::");
5411       strcat (temp, imported_name);
5412       canonical_name = temp;
5413     }
5414   else
5415     canonical_name = imported_name;
5416
5417   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5418
5419   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5420     for (child_die = die->child; child_die && child_die->tag;
5421          child_die = sibling_die (child_die))
5422       {
5423         /* DWARF-4: A Fortran use statement with a “rename list” may be
5424            represented by an imported module entry with an import attribute
5425            referring to the module and owned entries corresponding to those
5426            entities that are renamed as part of being imported.  */
5427
5428         if (child_die->tag != DW_TAG_imported_declaration)
5429           {
5430             complaint (&symfile_complaints,
5431                        _("child DW_TAG_imported_declaration expected "
5432                          "- DIE at 0x%x [in module %s]"),
5433                        child_die->offset, objfile->name);
5434             continue;
5435           }
5436
5437         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5438         if (import_attr == NULL)
5439           {
5440             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5441                        dwarf_tag_name (child_die->tag));
5442             continue;
5443           }
5444
5445         imported_cu = cu;
5446         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5447                                               &imported_cu);
5448         imported_name = dwarf2_name (imported_die, imported_cu);
5449         if (imported_name == NULL)
5450           {
5451             complaint (&symfile_complaints,
5452                        _("child DW_TAG_imported_declaration has unknown "
5453                          "imported name - DIE at 0x%x [in module %s]"),
5454                        child_die->offset, objfile->name);
5455             continue;
5456           }
5457
5458         VEC_safe_push (const_char_ptr, excludes, imported_name);
5459
5460         process_die (child_die, cu);
5461       }
5462
5463   cp_add_using_directive (import_prefix,
5464                           canonical_name,
5465                           import_alias,
5466                           imported_declaration,
5467                           excludes,
5468                           &objfile->objfile_obstack);
5469
5470   do_cleanups (cleanups);
5471 }
5472
5473 /* Cleanup function for read_file_scope.  */
5474
5475 static void
5476 free_cu_line_header (void *arg)
5477 {
5478   struct dwarf2_cu *cu = arg;
5479
5480   free_line_header (cu->line_header);
5481   cu->line_header = NULL;
5482 }
5483
5484 static void
5485 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5486                          char **name, char **comp_dir)
5487 {
5488   struct attribute *attr;
5489
5490   *name = NULL;
5491   *comp_dir = NULL;
5492
5493   /* Find the filename.  Do not use dwarf2_name here, since the filename
5494      is not a source language identifier.  */
5495   attr = dwarf2_attr (die, DW_AT_name, cu);
5496   if (attr)
5497     {
5498       *name = DW_STRING (attr);
5499     }
5500
5501   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5502   if (attr)
5503     *comp_dir = DW_STRING (attr);
5504   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5505     {
5506       *comp_dir = ldirname (*name);
5507       if (*comp_dir != NULL)
5508         make_cleanup (xfree, *comp_dir);
5509     }
5510   if (*comp_dir != NULL)
5511     {
5512       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5513          directory, get rid of it.  */
5514       char *cp = strchr (*comp_dir, ':');
5515
5516       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5517         *comp_dir = cp + 1;
5518     }
5519
5520   if (*name == NULL)
5521     *name = "<unknown>";
5522 }
5523
5524 /* Handle DW_AT_stmt_list for a compilation unit.  */
5525
5526 static void
5527 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5528                         const char *comp_dir)
5529 {
5530   struct attribute *attr;
5531   struct objfile *objfile = cu->objfile;
5532   bfd *abfd = objfile->obfd;
5533
5534   /* Decode line number information if present.  We do this before
5535      processing child DIEs, so that the line header table is available
5536      for DW_AT_decl_file.  */
5537   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5538   if (attr)
5539     {
5540       unsigned int line_offset = DW_UNSND (attr);
5541       struct line_header *line_header
5542         = dwarf_decode_line_header (line_offset, abfd, cu);
5543
5544       if (line_header)
5545         {
5546           cu->line_header = line_header;
5547           make_cleanup (free_cu_line_header, cu);
5548           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5549         }
5550     }
5551 }
5552
5553 /* Process DW_TAG_compile_unit.  */
5554
5555 static void
5556 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5557 {
5558   struct objfile *objfile = cu->objfile;
5559   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5560   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5561   CORE_ADDR highpc = ((CORE_ADDR) 0);
5562   struct attribute *attr;
5563   char *name = NULL;
5564   char *comp_dir = NULL;
5565   struct die_info *child_die;
5566   bfd *abfd = objfile->obfd;
5567   CORE_ADDR baseaddr;
5568
5569   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5570
5571   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5572
5573   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5574      from finish_block.  */
5575   if (lowpc == ((CORE_ADDR) -1))
5576     lowpc = highpc;
5577   lowpc += baseaddr;
5578   highpc += baseaddr;
5579
5580   find_file_and_directory (die, cu, &name, &comp_dir);
5581
5582   attr = dwarf2_attr (die, DW_AT_language, cu);
5583   if (attr)
5584     {
5585       set_cu_language (DW_UNSND (attr), cu);
5586     }
5587
5588   attr = dwarf2_attr (die, DW_AT_producer, cu);
5589   if (attr)
5590     cu->producer = DW_STRING (attr);
5591
5592   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5593      standardised yet.  As a workaround for the language detection we fall
5594      back to the DW_AT_producer string.  */
5595   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5596     cu->language = language_opencl;
5597
5598   /* We assume that we're processing GCC output.  */
5599   processing_gcc_compilation = 2;
5600
5601   processing_has_namespace_info = 0;
5602
5603   start_symtab (name, comp_dir, lowpc);
5604   record_debugformat ("DWARF 2");
5605   record_producer (cu->producer);
5606
5607   handle_DW_AT_stmt_list (die, cu, comp_dir);
5608
5609   /* Process all dies in compilation unit.  */
5610   if (die->child != NULL)
5611     {
5612       child_die = die->child;
5613       while (child_die && child_die->tag)
5614         {
5615           process_die (child_die, cu);
5616           child_die = sibling_die (child_die);
5617         }
5618     }
5619
5620   /* Decode macro information, if present.  Dwarf 2 macro information
5621      refers to information in the line number info statement program
5622      header, so we can only read it if we've read the header
5623      successfully.  */
5624   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5625   if (attr && cu->line_header)
5626     {
5627       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5628         complaint (&symfile_complaints,
5629                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5630
5631       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5632                            comp_dir, abfd, cu,
5633                            &dwarf2_per_objfile->macro, 1);
5634     }
5635   else
5636     {
5637       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5638       if (attr && cu->line_header)
5639         {
5640           unsigned int macro_offset = DW_UNSND (attr);
5641
5642           dwarf_decode_macros (cu->line_header, macro_offset,
5643                                comp_dir, abfd, cu,
5644                                &dwarf2_per_objfile->macinfo, 0);
5645         }
5646     }
5647
5648   do_cleanups (back_to);
5649 }
5650
5651 /* Process DW_TAG_type_unit.
5652    For TUs we want to skip the first top level sibling if it's not the
5653    actual type being defined by this TU.  In this case the first top
5654    level sibling is there to provide context only.  */
5655
5656 static void
5657 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5658 {
5659   struct objfile *objfile = cu->objfile;
5660   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5661   CORE_ADDR lowpc;
5662   struct attribute *attr;
5663   char *name = NULL;
5664   char *comp_dir = NULL;
5665   struct die_info *child_die;
5666   bfd *abfd = objfile->obfd;
5667
5668   /* start_symtab needs a low pc, but we don't really have one.
5669      Do what read_file_scope would do in the absence of such info.  */
5670   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5671
5672   /* Find the filename.  Do not use dwarf2_name here, since the filename
5673      is not a source language identifier.  */
5674   attr = dwarf2_attr (die, DW_AT_name, cu);
5675   if (attr)
5676     name = DW_STRING (attr);
5677
5678   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5679   if (attr)
5680     comp_dir = DW_STRING (attr);
5681   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5682     {
5683       comp_dir = ldirname (name);
5684       if (comp_dir != NULL)
5685         make_cleanup (xfree, comp_dir);
5686     }
5687
5688   if (name == NULL)
5689     name = "<unknown>";
5690
5691   attr = dwarf2_attr (die, DW_AT_language, cu);
5692   if (attr)
5693     set_cu_language (DW_UNSND (attr), cu);
5694
5695   /* This isn't technically needed today.  It is done for symmetry
5696      with read_file_scope.  */
5697   attr = dwarf2_attr (die, DW_AT_producer, cu);
5698   if (attr)
5699     cu->producer = DW_STRING (attr);
5700
5701   /* We assume that we're processing GCC output.  */
5702   processing_gcc_compilation = 2;
5703
5704   processing_has_namespace_info = 0;
5705
5706   start_symtab (name, comp_dir, lowpc);
5707   record_debugformat ("DWARF 2");
5708   record_producer (cu->producer);
5709
5710   handle_DW_AT_stmt_list (die, cu, comp_dir);
5711
5712   /* Process the dies in the type unit.  */
5713   if (die->child == NULL)
5714     {
5715       dump_die_for_error (die);
5716       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5717              bfd_get_filename (abfd));
5718     }
5719
5720   child_die = die->child;
5721
5722   while (child_die && child_die->tag)
5723     {
5724       process_die (child_die, cu);
5725
5726       child_die = sibling_die (child_die);
5727     }
5728
5729   do_cleanups (back_to);
5730 }
5731
5732 /* qsort helper for inherit_abstract_dies.  */
5733
5734 static int
5735 unsigned_int_compar (const void *ap, const void *bp)
5736 {
5737   unsigned int a = *(unsigned int *) ap;
5738   unsigned int b = *(unsigned int *) bp;
5739
5740   return (a > b) - (b > a);
5741 }
5742
5743 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5744    Inherit only the children of the DW_AT_abstract_origin DIE not being
5745    already referenced by DW_AT_abstract_origin from the children of the
5746    current DIE.  */
5747
5748 static void
5749 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5750 {
5751   struct die_info *child_die;
5752   unsigned die_children_count;
5753   /* CU offsets which were referenced by children of the current DIE.  */
5754   unsigned *offsets;
5755   unsigned *offsets_end, *offsetp;
5756   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5757   struct die_info *origin_die;
5758   /* Iterator of the ORIGIN_DIE children.  */
5759   struct die_info *origin_child_die;
5760   struct cleanup *cleanups;
5761   struct attribute *attr;
5762   struct dwarf2_cu *origin_cu;
5763   struct pending **origin_previous_list_in_scope;
5764
5765   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5766   if (!attr)
5767     return;
5768
5769   /* Note that following die references may follow to a die in a
5770      different cu.  */
5771
5772   origin_cu = cu;
5773   origin_die = follow_die_ref (die, attr, &origin_cu);
5774
5775   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5776      symbols in.  */
5777   origin_previous_list_in_scope = origin_cu->list_in_scope;
5778   origin_cu->list_in_scope = cu->list_in_scope;
5779
5780   if (die->tag != origin_die->tag
5781       && !(die->tag == DW_TAG_inlined_subroutine
5782            && origin_die->tag == DW_TAG_subprogram))
5783     complaint (&symfile_complaints,
5784                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5785                die->offset, origin_die->offset);
5786
5787   child_die = die->child;
5788   die_children_count = 0;
5789   while (child_die && child_die->tag)
5790     {
5791       child_die = sibling_die (child_die);
5792       die_children_count++;
5793     }
5794   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5795   cleanups = make_cleanup (xfree, offsets);
5796
5797   offsets_end = offsets;
5798   child_die = die->child;
5799   while (child_die && child_die->tag)
5800     {
5801       /* For each CHILD_DIE, find the corresponding child of
5802          ORIGIN_DIE.  If there is more than one layer of
5803          DW_AT_abstract_origin, follow them all; there shouldn't be,
5804          but GCC versions at least through 4.4 generate this (GCC PR
5805          40573).  */
5806       struct die_info *child_origin_die = child_die;
5807       struct dwarf2_cu *child_origin_cu = cu;
5808
5809       while (1)
5810         {
5811           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5812                               child_origin_cu);
5813           if (attr == NULL)
5814             break;
5815           child_origin_die = follow_die_ref (child_origin_die, attr,
5816                                              &child_origin_cu);
5817         }
5818
5819       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5820          counterpart may exist.  */
5821       if (child_origin_die != child_die)
5822         {
5823           if (child_die->tag != child_origin_die->tag
5824               && !(child_die->tag == DW_TAG_inlined_subroutine
5825                    && child_origin_die->tag == DW_TAG_subprogram))
5826             complaint (&symfile_complaints,
5827                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5828                          "different tags"), child_die->offset,
5829                        child_origin_die->offset);
5830           if (child_origin_die->parent != origin_die)
5831             complaint (&symfile_complaints,
5832                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5833                          "different parents"), child_die->offset,
5834                        child_origin_die->offset);
5835           else
5836             *offsets_end++ = child_origin_die->offset;
5837         }
5838       child_die = sibling_die (child_die);
5839     }
5840   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5841          unsigned_int_compar);
5842   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5843     if (offsetp[-1] == *offsetp)
5844       complaint (&symfile_complaints,
5845                  _("Multiple children of DIE 0x%x refer "
5846                    "to DIE 0x%x as their abstract origin"),
5847                  die->offset, *offsetp);
5848
5849   offsetp = offsets;
5850   origin_child_die = origin_die->child;
5851   while (origin_child_die && origin_child_die->tag)
5852     {
5853       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5854       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5855         offsetp++;
5856       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5857         {
5858           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5859           process_die (origin_child_die, origin_cu);
5860         }
5861       origin_child_die = sibling_die (origin_child_die);
5862     }
5863   origin_cu->list_in_scope = origin_previous_list_in_scope;
5864
5865   do_cleanups (cleanups);
5866 }
5867
5868 static void
5869 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5870 {
5871   struct objfile *objfile = cu->objfile;
5872   struct context_stack *new;
5873   CORE_ADDR lowpc;
5874   CORE_ADDR highpc;
5875   struct die_info *child_die;
5876   struct attribute *attr, *call_line, *call_file;
5877   char *name;
5878   CORE_ADDR baseaddr;
5879   struct block *block;
5880   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5881   VEC (symbolp) *template_args = NULL;
5882   struct template_symbol *templ_func = NULL;
5883
5884   if (inlined_func)
5885     {
5886       /* If we do not have call site information, we can't show the
5887          caller of this inlined function.  That's too confusing, so
5888          only use the scope for local variables.  */
5889       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5890       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5891       if (call_line == NULL || call_file == NULL)
5892         {
5893           read_lexical_block_scope (die, cu);
5894           return;
5895         }
5896     }
5897
5898   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5899
5900   name = dwarf2_name (die, cu);
5901
5902   /* Ignore functions with missing or empty names.  These are actually
5903      illegal according to the DWARF standard.  */
5904   if (name == NULL)
5905     {
5906       complaint (&symfile_complaints,
5907                  _("missing name for subprogram DIE at %d"), die->offset);
5908       return;
5909     }
5910
5911   /* Ignore functions with missing or invalid low and high pc attributes.  */
5912   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5913     {
5914       attr = dwarf2_attr (die, DW_AT_external, cu);
5915       if (!attr || !DW_UNSND (attr))
5916         complaint (&symfile_complaints,
5917                    _("cannot get low and high bounds "
5918                      "for subprogram DIE at %d"),
5919                    die->offset);
5920       return;
5921     }
5922
5923   lowpc += baseaddr;
5924   highpc += baseaddr;
5925
5926   /* If we have any template arguments, then we must allocate a
5927      different sort of symbol.  */
5928   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5929     {
5930       if (child_die->tag == DW_TAG_template_type_param
5931           || child_die->tag == DW_TAG_template_value_param)
5932         {
5933           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5934                                        struct template_symbol);
5935           templ_func->base.is_cplus_template_function = 1;
5936           break;
5937         }
5938     }
5939
5940   new = push_context (0, lowpc);
5941   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5942                                (struct symbol *) templ_func);
5943
5944   /* If there is a location expression for DW_AT_frame_base, record
5945      it.  */
5946   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5947   if (attr)
5948     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5949        expression is being recorded directly in the function's symbol
5950        and not in a separate frame-base object.  I guess this hack is
5951        to avoid adding some sort of frame-base adjunct/annex to the
5952        function's symbol :-(.  The problem with doing this is that it
5953        results in a function symbol with a location expression that
5954        has nothing to do with the location of the function, ouch!  The
5955        relationship should be: a function's symbol has-a frame base; a
5956        frame-base has-a location expression.  */
5957     dwarf2_symbol_mark_computed (attr, new->name, cu);
5958
5959   cu->list_in_scope = &local_symbols;
5960
5961   if (die->child != NULL)
5962     {
5963       child_die = die->child;
5964       while (child_die && child_die->tag)
5965         {
5966           if (child_die->tag == DW_TAG_template_type_param
5967               || child_die->tag == DW_TAG_template_value_param)
5968             {
5969               struct symbol *arg = new_symbol (child_die, NULL, cu);
5970
5971               if (arg != NULL)
5972                 VEC_safe_push (symbolp, template_args, arg);
5973             }
5974           else
5975             process_die (child_die, cu);
5976           child_die = sibling_die (child_die);
5977         }
5978     }
5979
5980   inherit_abstract_dies (die, cu);
5981
5982   /* If we have a DW_AT_specification, we might need to import using
5983      directives from the context of the specification DIE.  See the
5984      comment in determine_prefix.  */
5985   if (cu->language == language_cplus
5986       && dwarf2_attr (die, DW_AT_specification, cu))
5987     {
5988       struct dwarf2_cu *spec_cu = cu;
5989       struct die_info *spec_die = die_specification (die, &spec_cu);
5990
5991       while (spec_die)
5992         {
5993           child_die = spec_die->child;
5994           while (child_die && child_die->tag)
5995             {
5996               if (child_die->tag == DW_TAG_imported_module)
5997                 process_die (child_die, spec_cu);
5998               child_die = sibling_die (child_die);
5999             }
6000
6001           /* In some cases, GCC generates specification DIEs that
6002              themselves contain DW_AT_specification attributes.  */
6003           spec_die = die_specification (spec_die, &spec_cu);
6004         }
6005     }
6006
6007   new = pop_context ();
6008   /* Make a block for the local symbols within.  */
6009   block = finish_block (new->name, &local_symbols, new->old_blocks,
6010                         lowpc, highpc, objfile);
6011
6012   /* For C++, set the block's scope.  */
6013   if (cu->language == language_cplus || cu->language == language_fortran)
6014     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6015                         determine_prefix (die, cu),
6016                         processing_has_namespace_info);
6017
6018   /* If we have address ranges, record them.  */
6019   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6020
6021   /* Attach template arguments to function.  */
6022   if (! VEC_empty (symbolp, template_args))
6023     {
6024       gdb_assert (templ_func != NULL);
6025
6026       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6027       templ_func->template_arguments
6028         = obstack_alloc (&objfile->objfile_obstack,
6029                          (templ_func->n_template_arguments
6030                           * sizeof (struct symbol *)));
6031       memcpy (templ_func->template_arguments,
6032               VEC_address (symbolp, template_args),
6033               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6034       VEC_free (symbolp, template_args);
6035     }
6036
6037   /* In C++, we can have functions nested inside functions (e.g., when
6038      a function declares a class that has methods).  This means that
6039      when we finish processing a function scope, we may need to go
6040      back to building a containing block's symbol lists.  */
6041   local_symbols = new->locals;
6042   param_symbols = new->params;
6043   using_directives = new->using_directives;
6044
6045   /* If we've finished processing a top-level function, subsequent
6046      symbols go in the file symbol list.  */
6047   if (outermost_context_p ())
6048     cu->list_in_scope = &file_symbols;
6049 }
6050
6051 /* Process all the DIES contained within a lexical block scope.  Start
6052    a new scope, process the dies, and then close the scope.  */
6053
6054 static void
6055 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6056 {
6057   struct objfile *objfile = cu->objfile;
6058   struct context_stack *new;
6059   CORE_ADDR lowpc, highpc;
6060   struct die_info *child_die;
6061   CORE_ADDR baseaddr;
6062
6063   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6064
6065   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6066   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6067      as multiple lexical blocks?  Handling children in a sane way would
6068      be nasty.  Might be easier to properly extend generic blocks to
6069      describe ranges.  */
6070   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6071     return;
6072   lowpc += baseaddr;
6073   highpc += baseaddr;
6074
6075   push_context (0, lowpc);
6076   if (die->child != NULL)
6077     {
6078       child_die = die->child;
6079       while (child_die && child_die->tag)
6080         {
6081           process_die (child_die, cu);
6082           child_die = sibling_die (child_die);
6083         }
6084     }
6085   new = pop_context ();
6086
6087   if (local_symbols != NULL || using_directives != NULL)
6088     {
6089       struct block *block
6090         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6091                         highpc, objfile);
6092
6093       /* Note that recording ranges after traversing children, as we
6094          do here, means that recording a parent's ranges entails
6095          walking across all its children's ranges as they appear in
6096          the address map, which is quadratic behavior.
6097
6098          It would be nicer to record the parent's ranges before
6099          traversing its children, simply overriding whatever you find
6100          there.  But since we don't even decide whether to create a
6101          block until after we've traversed its children, that's hard
6102          to do.  */
6103       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6104     }
6105   local_symbols = new->locals;
6106   using_directives = new->using_directives;
6107 }
6108
6109 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6110
6111 static void
6112 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6113 {
6114   struct objfile *objfile = cu->objfile;
6115   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6116   CORE_ADDR pc, baseaddr;
6117   struct attribute *attr;
6118   struct call_site *call_site, call_site_local;
6119   void **slot;
6120   int nparams;
6121   struct die_info *child_die;
6122
6123   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6124
6125   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6126   if (!attr)
6127     {
6128       complaint (&symfile_complaints,
6129                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6130                    "DIE 0x%x [in module %s]"),
6131                  die->offset, objfile->name);
6132       return;
6133     }
6134   pc = DW_ADDR (attr) + baseaddr;
6135
6136   if (cu->call_site_htab == NULL)
6137     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6138                                                NULL, &objfile->objfile_obstack,
6139                                                hashtab_obstack_allocate, NULL);
6140   call_site_local.pc = pc;
6141   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6142   if (*slot != NULL)
6143     {
6144       complaint (&symfile_complaints,
6145                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6146                    "DIE 0x%x [in module %s]"),
6147                  paddress (gdbarch, pc), die->offset, objfile->name);
6148       return;
6149     }
6150
6151   /* Count parameters at the caller.  */
6152
6153   nparams = 0;
6154   for (child_die = die->child; child_die && child_die->tag;
6155        child_die = sibling_die (child_die))
6156     {
6157       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6158         {
6159           complaint (&symfile_complaints,
6160                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6161                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6162                      child_die->tag, child_die->offset, objfile->name);
6163           continue;
6164         }
6165
6166       nparams++;
6167     }
6168
6169   call_site = obstack_alloc (&objfile->objfile_obstack,
6170                              (sizeof (*call_site)
6171                               + (sizeof (*call_site->parameter)
6172                                  * (nparams - 1))));
6173   *slot = call_site;
6174   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6175   call_site->pc = pc;
6176
6177   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6178     {
6179       struct die_info *func_die;
6180
6181       /* Skip also over DW_TAG_inlined_subroutine.  */
6182       for (func_die = die->parent;
6183            func_die && func_die->tag != DW_TAG_subprogram
6184            && func_die->tag != DW_TAG_subroutine_type;
6185            func_die = func_die->parent);
6186
6187       /* DW_AT_GNU_all_call_sites is a superset
6188          of DW_AT_GNU_all_tail_call_sites.  */
6189       if (func_die
6190           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6191           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6192         {
6193           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6194              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6195              both the initial caller containing the real return address PC and
6196              the final callee containing the current PC of a chain of tail
6197              calls do not need to have the tail call list complete.  But any
6198              function candidate for a virtual tail call frame searched via
6199              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6200              determined unambiguously.  */
6201         }
6202       else
6203         {
6204           struct type *func_type = NULL;
6205
6206           if (func_die)
6207             func_type = get_die_type (func_die, cu);
6208           if (func_type != NULL)
6209             {
6210               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6211
6212               /* Enlist this call site to the function.  */
6213               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6214               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6215             }
6216           else
6217             complaint (&symfile_complaints,
6218                        _("Cannot find function owning DW_TAG_GNU_call_site "
6219                          "DIE 0x%x [in module %s]"),
6220                        die->offset, objfile->name);
6221         }
6222     }
6223
6224   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6225   if (attr == NULL)
6226     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6227   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6228   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6229     /* Keep NULL DWARF_BLOCK.  */;
6230   else if (attr_form_is_block (attr))
6231     {
6232       struct dwarf2_locexpr_baton *dlbaton;
6233
6234       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6235       dlbaton->data = DW_BLOCK (attr)->data;
6236       dlbaton->size = DW_BLOCK (attr)->size;
6237       dlbaton->per_cu = cu->per_cu;
6238
6239       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6240     }
6241   else if (is_ref_attr (attr))
6242     {
6243       struct dwarf2_cu *target_cu = cu;
6244       struct die_info *target_die;
6245
6246       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6247       gdb_assert (target_cu->objfile == objfile);
6248       if (die_is_declaration (target_die, target_cu))
6249         {
6250           const char *target_physname;
6251
6252           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6253           if (target_physname == NULL)
6254             complaint (&symfile_complaints,
6255                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6256                          "physname, for referencing DIE 0x%x [in module %s]"),
6257                        die->offset, objfile->name);
6258           else
6259             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6260         }
6261       else
6262         {
6263           CORE_ADDR lowpc;
6264
6265           /* DW_AT_entry_pc should be preferred.  */
6266           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6267             complaint (&symfile_complaints,
6268                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6269                          "low pc, for referencing DIE 0x%x [in module %s]"),
6270                        die->offset, objfile->name);
6271           else
6272             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6273         }
6274     }
6275   else
6276     complaint (&symfile_complaints,
6277                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6278                  "block nor reference, for DIE 0x%x [in module %s]"),
6279                die->offset, objfile->name);
6280
6281   call_site->per_cu = cu->per_cu;
6282
6283   for (child_die = die->child;
6284        child_die && child_die->tag;
6285        child_die = sibling_die (child_die))
6286     {
6287       struct dwarf2_locexpr_baton *dlbaton;
6288       struct call_site_parameter *parameter;
6289
6290       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6291         {
6292           /* Already printed the complaint above.  */
6293           continue;
6294         }
6295
6296       gdb_assert (call_site->parameter_count < nparams);
6297       parameter = &call_site->parameter[call_site->parameter_count];
6298
6299       /* DW_AT_location specifies the register number.  Value of the data
6300          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6301
6302       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6303       if (!attr || !attr_form_is_block (attr))
6304         {
6305           complaint (&symfile_complaints,
6306                      _("No DW_FORM_block* DW_AT_location for "
6307                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6308                      child_die->offset, objfile->name);
6309           continue;
6310         }
6311       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6312                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6313       if (parameter->dwarf_reg == -1
6314           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6315                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6316                                         &parameter->fb_offset))
6317         {
6318           complaint (&symfile_complaints,
6319                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6320                        "for DW_FORM_block* DW_AT_location for "
6321                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6322                      child_die->offset, objfile->name);
6323           continue;
6324         }
6325
6326       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6327       if (!attr_form_is_block (attr))
6328         {
6329           complaint (&symfile_complaints,
6330                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6331                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6332                      child_die->offset, objfile->name);
6333           continue;
6334         }
6335       parameter->value = DW_BLOCK (attr)->data;
6336       parameter->value_size = DW_BLOCK (attr)->size;
6337
6338       /* Parameters are not pre-cleared by memset above.  */
6339       parameter->data_value = NULL;
6340       parameter->data_value_size = 0;
6341       call_site->parameter_count++;
6342
6343       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6344       if (attr)
6345         {
6346           if (!attr_form_is_block (attr))
6347             complaint (&symfile_complaints,
6348                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6349                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6350                        child_die->offset, objfile->name);
6351           else
6352             {
6353               parameter->data_value = DW_BLOCK (attr)->data;
6354               parameter->data_value_size = DW_BLOCK (attr)->size;
6355             }
6356         }
6357     }
6358 }
6359
6360 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6361    Return 1 if the attributes are present and valid, otherwise, return 0.
6362    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6363
6364 static int
6365 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6366                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6367                     struct partial_symtab *ranges_pst)
6368 {
6369   struct objfile *objfile = cu->objfile;
6370   struct comp_unit_head *cu_header = &cu->header;
6371   bfd *obfd = objfile->obfd;
6372   unsigned int addr_size = cu_header->addr_size;
6373   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6374   /* Base address selection entry.  */
6375   CORE_ADDR base;
6376   int found_base;
6377   unsigned int dummy;
6378   gdb_byte *buffer;
6379   CORE_ADDR marker;
6380   int low_set;
6381   CORE_ADDR low = 0;
6382   CORE_ADDR high = 0;
6383   CORE_ADDR baseaddr;
6384
6385   found_base = cu->base_known;
6386   base = cu->base_address;
6387
6388   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6389   if (offset >= dwarf2_per_objfile->ranges.size)
6390     {
6391       complaint (&symfile_complaints,
6392                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6393                  offset);
6394       return 0;
6395     }
6396   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6397
6398   /* Read in the largest possible address.  */
6399   marker = read_address (obfd, buffer, cu, &dummy);
6400   if ((marker & mask) == mask)
6401     {
6402       /* If we found the largest possible address, then
6403          read the base address.  */
6404       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6405       buffer += 2 * addr_size;
6406       offset += 2 * addr_size;
6407       found_base = 1;
6408     }
6409
6410   low_set = 0;
6411
6412   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6413
6414   while (1)
6415     {
6416       CORE_ADDR range_beginning, range_end;
6417
6418       range_beginning = read_address (obfd, buffer, cu, &dummy);
6419       buffer += addr_size;
6420       range_end = read_address (obfd, buffer, cu, &dummy);
6421       buffer += addr_size;
6422       offset += 2 * addr_size;
6423
6424       /* An end of list marker is a pair of zero addresses.  */
6425       if (range_beginning == 0 && range_end == 0)
6426         /* Found the end of list entry.  */
6427         break;
6428
6429       /* Each base address selection entry is a pair of 2 values.
6430          The first is the largest possible address, the second is
6431          the base address.  Check for a base address here.  */
6432       if ((range_beginning & mask) == mask)
6433         {
6434           /* If we found the largest possible address, then
6435              read the base address.  */
6436           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6437           found_base = 1;
6438           continue;
6439         }
6440
6441       if (!found_base)
6442         {
6443           /* We have no valid base address for the ranges
6444              data.  */
6445           complaint (&symfile_complaints,
6446                      _("Invalid .debug_ranges data (no base address)"));
6447           return 0;
6448         }
6449
6450       if (range_beginning > range_end)
6451         {
6452           /* Inverted range entries are invalid.  */
6453           complaint (&symfile_complaints,
6454                      _("Invalid .debug_ranges data (inverted range)"));
6455           return 0;
6456         }
6457
6458       /* Empty range entries have no effect.  */
6459       if (range_beginning == range_end)
6460         continue;
6461
6462       range_beginning += base;
6463       range_end += base;
6464
6465       if (ranges_pst != NULL)
6466         addrmap_set_empty (objfile->psymtabs_addrmap,
6467                            range_beginning + baseaddr,
6468                            range_end - 1 + baseaddr,
6469                            ranges_pst);
6470
6471       /* FIXME: This is recording everything as a low-high
6472          segment of consecutive addresses.  We should have a
6473          data structure for discontiguous block ranges
6474          instead.  */
6475       if (! low_set)
6476         {
6477           low = range_beginning;
6478           high = range_end;
6479           low_set = 1;
6480         }
6481       else
6482         {
6483           if (range_beginning < low)
6484             low = range_beginning;
6485           if (range_end > high)
6486             high = range_end;
6487         }
6488     }
6489
6490   if (! low_set)
6491     /* If the first entry is an end-of-list marker, the range
6492        describes an empty scope, i.e. no instructions.  */
6493     return 0;
6494
6495   if (low_return)
6496     *low_return = low;
6497   if (high_return)
6498     *high_return = high;
6499   return 1;
6500 }
6501
6502 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6503    are present and valid, otherwise, return 0.  Return -1 if the range is
6504    discontinuous, i.e. derived from DW_AT_ranges information.  */
6505 static int
6506 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6507                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6508                       struct partial_symtab *pst)
6509 {
6510   struct attribute *attr;
6511   CORE_ADDR low = 0;
6512   CORE_ADDR high = 0;
6513   int ret = 0;
6514
6515   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6516   if (attr)
6517     {
6518       high = DW_ADDR (attr);
6519       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6520       if (attr)
6521         low = DW_ADDR (attr);
6522       else
6523         /* Found high w/o low attribute.  */
6524         return 0;
6525
6526       /* Found consecutive range of addresses.  */
6527       ret = 1;
6528     }
6529   else
6530     {
6531       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6532       if (attr != NULL)
6533         {
6534           /* Value of the DW_AT_ranges attribute is the offset in the
6535              .debug_ranges section.  */
6536           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6537             return 0;
6538           /* Found discontinuous range of addresses.  */
6539           ret = -1;
6540         }
6541     }
6542
6543   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6544   if (high <= low)
6545     return 0;
6546
6547   /* When using the GNU linker, .gnu.linkonce. sections are used to
6548      eliminate duplicate copies of functions and vtables and such.
6549      The linker will arbitrarily choose one and discard the others.
6550      The AT_*_pc values for such functions refer to local labels in
6551      these sections.  If the section from that file was discarded, the
6552      labels are not in the output, so the relocs get a value of 0.
6553      If this is a discarded function, mark the pc bounds as invalid,
6554      so that GDB will ignore it.  */
6555   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6556     return 0;
6557
6558   *lowpc = low;
6559   if (highpc)
6560     *highpc = high;
6561   return ret;
6562 }
6563
6564 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6565    its low and high PC addresses.  Do nothing if these addresses could not
6566    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6567    and HIGHPC to the high address if greater than HIGHPC.  */
6568
6569 static void
6570 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6571                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6572                                  struct dwarf2_cu *cu)
6573 {
6574   CORE_ADDR low, high;
6575   struct die_info *child = die->child;
6576
6577   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6578     {
6579       *lowpc = min (*lowpc, low);
6580       *highpc = max (*highpc, high);
6581     }
6582
6583   /* If the language does not allow nested subprograms (either inside
6584      subprograms or lexical blocks), we're done.  */
6585   if (cu->language != language_ada)
6586     return;
6587
6588   /* Check all the children of the given DIE.  If it contains nested
6589      subprograms, then check their pc bounds.  Likewise, we need to
6590      check lexical blocks as well, as they may also contain subprogram
6591      definitions.  */
6592   while (child && child->tag)
6593     {
6594       if (child->tag == DW_TAG_subprogram
6595           || child->tag == DW_TAG_lexical_block)
6596         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6597       child = sibling_die (child);
6598     }
6599 }
6600
6601 /* Get the low and high pc's represented by the scope DIE, and store
6602    them in *LOWPC and *HIGHPC.  If the correct values can't be
6603    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6604
6605 static void
6606 get_scope_pc_bounds (struct die_info *die,
6607                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6608                      struct dwarf2_cu *cu)
6609 {
6610   CORE_ADDR best_low = (CORE_ADDR) -1;
6611   CORE_ADDR best_high = (CORE_ADDR) 0;
6612   CORE_ADDR current_low, current_high;
6613
6614   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6615     {
6616       best_low = current_low;
6617       best_high = current_high;
6618     }
6619   else
6620     {
6621       struct die_info *child = die->child;
6622
6623       while (child && child->tag)
6624         {
6625           switch (child->tag) {
6626           case DW_TAG_subprogram:
6627             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6628             break;
6629           case DW_TAG_namespace:
6630           case DW_TAG_module:
6631             /* FIXME: carlton/2004-01-16: Should we do this for
6632                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6633                that current GCC's always emit the DIEs corresponding
6634                to definitions of methods of classes as children of a
6635                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6636                the DIEs giving the declarations, which could be
6637                anywhere).  But I don't see any reason why the
6638                standards says that they have to be there.  */
6639             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6640
6641             if (current_low != ((CORE_ADDR) -1))
6642               {
6643                 best_low = min (best_low, current_low);
6644                 best_high = max (best_high, current_high);
6645               }
6646             break;
6647           default:
6648             /* Ignore.  */
6649             break;
6650           }
6651
6652           child = sibling_die (child);
6653         }
6654     }
6655
6656   *lowpc = best_low;
6657   *highpc = best_high;
6658 }
6659
6660 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6661    in DIE.  */
6662 static void
6663 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6664                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6665 {
6666   struct objfile *objfile = cu->objfile;
6667   struct attribute *attr;
6668
6669   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6670   if (attr)
6671     {
6672       CORE_ADDR high = DW_ADDR (attr);
6673
6674       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6675       if (attr)
6676         {
6677           CORE_ADDR low = DW_ADDR (attr);
6678
6679           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6680         }
6681     }
6682
6683   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6684   if (attr)
6685     {
6686       bfd *obfd = objfile->obfd;
6687
6688       /* The value of the DW_AT_ranges attribute is the offset of the
6689          address range list in the .debug_ranges section.  */
6690       unsigned long offset = DW_UNSND (attr);
6691       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6692
6693       /* For some target architectures, but not others, the
6694          read_address function sign-extends the addresses it returns.
6695          To recognize base address selection entries, we need a
6696          mask.  */
6697       unsigned int addr_size = cu->header.addr_size;
6698       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6699
6700       /* The base address, to which the next pair is relative.  Note
6701          that this 'base' is a DWARF concept: most entries in a range
6702          list are relative, to reduce the number of relocs against the
6703          debugging information.  This is separate from this function's
6704          'baseaddr' argument, which GDB uses to relocate debugging
6705          information from a shared library based on the address at
6706          which the library was loaded.  */
6707       CORE_ADDR base = cu->base_address;
6708       int base_known = cu->base_known;
6709
6710       gdb_assert (dwarf2_per_objfile->ranges.readin);
6711       if (offset >= dwarf2_per_objfile->ranges.size)
6712         {
6713           complaint (&symfile_complaints,
6714                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6715                      offset);
6716           return;
6717         }
6718
6719       for (;;)
6720         {
6721           unsigned int bytes_read;
6722           CORE_ADDR start, end;
6723
6724           start = read_address (obfd, buffer, cu, &bytes_read);
6725           buffer += bytes_read;
6726           end = read_address (obfd, buffer, cu, &bytes_read);
6727           buffer += bytes_read;
6728
6729           /* Did we find the end of the range list?  */
6730           if (start == 0 && end == 0)
6731             break;
6732
6733           /* Did we find a base address selection entry?  */
6734           else if ((start & base_select_mask) == base_select_mask)
6735             {
6736               base = end;
6737               base_known = 1;
6738             }
6739
6740           /* We found an ordinary address range.  */
6741           else
6742             {
6743               if (!base_known)
6744                 {
6745                   complaint (&symfile_complaints,
6746                              _("Invalid .debug_ranges data "
6747                                "(no base address)"));
6748                   return;
6749                 }
6750
6751               if (start > end)
6752                 {
6753                   /* Inverted range entries are invalid.  */
6754                   complaint (&symfile_complaints,
6755                              _("Invalid .debug_ranges data "
6756                                "(inverted range)"));
6757                   return;
6758                 }
6759
6760               /* Empty range entries have no effect.  */
6761               if (start == end)
6762                 continue;
6763
6764               record_block_range (block,
6765                                   baseaddr + base + start,
6766                                   baseaddr + base + end - 1);
6767             }
6768         }
6769     }
6770 }
6771
6772 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6773    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6774    during 4.6.0 experimental.  */
6775
6776 static int
6777 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6778 {
6779   const char *cs;
6780   int major, minor, release;
6781
6782   if (cu->producer == NULL)
6783     {
6784       /* For unknown compilers expect their behavior is DWARF version
6785          compliant.
6786
6787          GCC started to support .debug_types sections by -gdwarf-4 since
6788          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6789          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6790          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6791          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6792
6793       return 0;
6794     }
6795
6796   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6797
6798   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6799     {
6800       /* For non-GCC compilers expect their behavior is DWARF version
6801          compliant.  */
6802
6803       return 0;
6804     }
6805   cs = &cu->producer[strlen ("GNU ")];
6806   while (*cs && !isdigit (*cs))
6807     cs++;
6808   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6809     {
6810       /* Not recognized as GCC.  */
6811
6812       return 0;
6813     }
6814
6815   return major < 4 || (major == 4 && minor < 6);
6816 }
6817
6818 /* Return the default accessibility type if it is not overriden by
6819    DW_AT_accessibility.  */
6820
6821 static enum dwarf_access_attribute
6822 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6823 {
6824   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6825     {
6826       /* The default DWARF 2 accessibility for members is public, the default
6827          accessibility for inheritance is private.  */
6828
6829       if (die->tag != DW_TAG_inheritance)
6830         return DW_ACCESS_public;
6831       else
6832         return DW_ACCESS_private;
6833     }
6834   else
6835     {
6836       /* DWARF 3+ defines the default accessibility a different way.  The same
6837          rules apply now for DW_TAG_inheritance as for the members and it only
6838          depends on the container kind.  */
6839
6840       if (die->parent->tag == DW_TAG_class_type)
6841         return DW_ACCESS_private;
6842       else
6843         return DW_ACCESS_public;
6844     }
6845 }
6846
6847 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6848    offset.  If the attribute was not found return 0, otherwise return
6849    1.  If it was found but could not properly be handled, set *OFFSET
6850    to 0.  */
6851
6852 static int
6853 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6854                              LONGEST *offset)
6855 {
6856   struct attribute *attr;
6857
6858   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6859   if (attr != NULL)
6860     {
6861       *offset = 0;
6862
6863       /* Note that we do not check for a section offset first here.
6864          This is because DW_AT_data_member_location is new in DWARF 4,
6865          so if we see it, we can assume that a constant form is really
6866          a constant and not a section offset.  */
6867       if (attr_form_is_constant (attr))
6868         *offset = dwarf2_get_attr_constant_value (attr, 0);
6869       else if (attr_form_is_section_offset (attr))
6870         dwarf2_complex_location_expr_complaint ();
6871       else if (attr_form_is_block (attr))
6872         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6873       else
6874         dwarf2_complex_location_expr_complaint ();
6875
6876       return 1;
6877     }
6878
6879   return 0;
6880 }
6881
6882 /* Add an aggregate field to the field list.  */
6883
6884 static void
6885 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6886                   struct dwarf2_cu *cu)
6887 {
6888   struct objfile *objfile = cu->objfile;
6889   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6890   struct nextfield *new_field;
6891   struct attribute *attr;
6892   struct field *fp;
6893   char *fieldname = "";
6894
6895   /* Allocate a new field list entry and link it in.  */
6896   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6897   make_cleanup (xfree, new_field);
6898   memset (new_field, 0, sizeof (struct nextfield));
6899
6900   if (die->tag == DW_TAG_inheritance)
6901     {
6902       new_field->next = fip->baseclasses;
6903       fip->baseclasses = new_field;
6904     }
6905   else
6906     {
6907       new_field->next = fip->fields;
6908       fip->fields = new_field;
6909     }
6910   fip->nfields++;
6911
6912   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6913   if (attr)
6914     new_field->accessibility = DW_UNSND (attr);
6915   else
6916     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6917   if (new_field->accessibility != DW_ACCESS_public)
6918     fip->non_public_fields = 1;
6919
6920   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6921   if (attr)
6922     new_field->virtuality = DW_UNSND (attr);
6923   else
6924     new_field->virtuality = DW_VIRTUALITY_none;
6925
6926   fp = &new_field->field;
6927
6928   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6929     {
6930       LONGEST offset;
6931
6932       /* Data member other than a C++ static data member.  */
6933
6934       /* Get type of field.  */
6935       fp->type = die_type (die, cu);
6936
6937       SET_FIELD_BITPOS (*fp, 0);
6938
6939       /* Get bit size of field (zero if none).  */
6940       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6941       if (attr)
6942         {
6943           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6944         }
6945       else
6946         {
6947           FIELD_BITSIZE (*fp) = 0;
6948         }
6949
6950       /* Get bit offset of field.  */
6951       if (handle_data_member_location (die, cu, &offset))
6952         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6953       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6954       if (attr)
6955         {
6956           if (gdbarch_bits_big_endian (gdbarch))
6957             {
6958               /* For big endian bits, the DW_AT_bit_offset gives the
6959                  additional bit offset from the MSB of the containing
6960                  anonymous object to the MSB of the field.  We don't
6961                  have to do anything special since we don't need to
6962                  know the size of the anonymous object.  */
6963               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6964             }
6965           else
6966             {
6967               /* For little endian bits, compute the bit offset to the
6968                  MSB of the anonymous object, subtract off the number of
6969                  bits from the MSB of the field to the MSB of the
6970                  object, and then subtract off the number of bits of
6971                  the field itself.  The result is the bit offset of
6972                  the LSB of the field.  */
6973               int anonymous_size;
6974               int bit_offset = DW_UNSND (attr);
6975
6976               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6977               if (attr)
6978                 {
6979                   /* The size of the anonymous object containing
6980                      the bit field is explicit, so use the
6981                      indicated size (in bytes).  */
6982                   anonymous_size = DW_UNSND (attr);
6983                 }
6984               else
6985                 {
6986                   /* The size of the anonymous object containing
6987                      the bit field must be inferred from the type
6988                      attribute of the data member containing the
6989                      bit field.  */
6990                   anonymous_size = TYPE_LENGTH (fp->type);
6991                 }
6992               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6993                 - bit_offset - FIELD_BITSIZE (*fp);
6994             }
6995         }
6996
6997       /* Get name of field.  */
6998       fieldname = dwarf2_name (die, cu);
6999       if (fieldname == NULL)
7000         fieldname = "";
7001
7002       /* The name is already allocated along with this objfile, so we don't
7003          need to duplicate it for the type.  */
7004       fp->name = fieldname;
7005
7006       /* Change accessibility for artificial fields (e.g. virtual table
7007          pointer or virtual base class pointer) to private.  */
7008       if (dwarf2_attr (die, DW_AT_artificial, cu))
7009         {
7010           FIELD_ARTIFICIAL (*fp) = 1;
7011           new_field->accessibility = DW_ACCESS_private;
7012           fip->non_public_fields = 1;
7013         }
7014     }
7015   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7016     {
7017       /* C++ static member.  */
7018
7019       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7020          is a declaration, but all versions of G++ as of this writing
7021          (so through at least 3.2.1) incorrectly generate
7022          DW_TAG_variable tags.  */
7023
7024       const char *physname;
7025
7026       /* Get name of field.  */
7027       fieldname = dwarf2_name (die, cu);
7028       if (fieldname == NULL)
7029         return;
7030
7031       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7032       if (attr
7033           /* Only create a symbol if this is an external value.
7034              new_symbol checks this and puts the value in the global symbol
7035              table, which we want.  If it is not external, new_symbol
7036              will try to put the value in cu->list_in_scope which is wrong.  */
7037           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7038         {
7039           /* A static const member, not much different than an enum as far as
7040              we're concerned, except that we can support more types.  */
7041           new_symbol (die, NULL, cu);
7042         }
7043
7044       /* Get physical name.  */
7045       physname = dwarf2_physname (fieldname, die, cu);
7046
7047       /* The name is already allocated along with this objfile, so we don't
7048          need to duplicate it for the type.  */
7049       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7050       FIELD_TYPE (*fp) = die_type (die, cu);
7051       FIELD_NAME (*fp) = fieldname;
7052     }
7053   else if (die->tag == DW_TAG_inheritance)
7054     {
7055       LONGEST offset;
7056
7057       /* C++ base class field.  */
7058       if (handle_data_member_location (die, cu, &offset))
7059         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7060       FIELD_BITSIZE (*fp) = 0;
7061       FIELD_TYPE (*fp) = die_type (die, cu);
7062       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7063       fip->nbaseclasses++;
7064     }
7065 }
7066
7067 /* Add a typedef defined in the scope of the FIP's class.  */
7068
7069 static void
7070 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7071                     struct dwarf2_cu *cu)
7072 {
7073   struct objfile *objfile = cu->objfile;
7074   struct typedef_field_list *new_field;
7075   struct attribute *attr;
7076   struct typedef_field *fp;
7077   char *fieldname = "";
7078
7079   /* Allocate a new field list entry and link it in.  */
7080   new_field = xzalloc (sizeof (*new_field));
7081   make_cleanup (xfree, new_field);
7082
7083   gdb_assert (die->tag == DW_TAG_typedef);
7084
7085   fp = &new_field->field;
7086
7087   /* Get name of field.  */
7088   fp->name = dwarf2_name (die, cu);
7089   if (fp->name == NULL)
7090     return;
7091
7092   fp->type = read_type_die (die, cu);
7093
7094   new_field->next = fip->typedef_field_list;
7095   fip->typedef_field_list = new_field;
7096   fip->typedef_field_list_count++;
7097 }
7098
7099 /* Create the vector of fields, and attach it to the type.  */
7100
7101 static void
7102 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7103                               struct dwarf2_cu *cu)
7104 {
7105   int nfields = fip->nfields;
7106
7107   /* Record the field count, allocate space for the array of fields,
7108      and create blank accessibility bitfields if necessary.  */
7109   TYPE_NFIELDS (type) = nfields;
7110   TYPE_FIELDS (type) = (struct field *)
7111     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7112   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7113
7114   if (fip->non_public_fields && cu->language != language_ada)
7115     {
7116       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7117
7118       TYPE_FIELD_PRIVATE_BITS (type) =
7119         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7120       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7121
7122       TYPE_FIELD_PROTECTED_BITS (type) =
7123         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7124       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7125
7126       TYPE_FIELD_IGNORE_BITS (type) =
7127         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7128       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7129     }
7130
7131   /* If the type has baseclasses, allocate and clear a bit vector for
7132      TYPE_FIELD_VIRTUAL_BITS.  */
7133   if (fip->nbaseclasses && cu->language != language_ada)
7134     {
7135       int num_bytes = B_BYTES (fip->nbaseclasses);
7136       unsigned char *pointer;
7137
7138       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7139       pointer = TYPE_ALLOC (type, num_bytes);
7140       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7141       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7142       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7143     }
7144
7145   /* Copy the saved-up fields into the field vector.  Start from the head of
7146      the list, adding to the tail of the field array, so that they end up in
7147      the same order in the array in which they were added to the list.  */
7148   while (nfields-- > 0)
7149     {
7150       struct nextfield *fieldp;
7151
7152       if (fip->fields)
7153         {
7154           fieldp = fip->fields;
7155           fip->fields = fieldp->next;
7156         }
7157       else
7158         {
7159           fieldp = fip->baseclasses;
7160           fip->baseclasses = fieldp->next;
7161         }
7162
7163       TYPE_FIELD (type, nfields) = fieldp->field;
7164       switch (fieldp->accessibility)
7165         {
7166         case DW_ACCESS_private:
7167           if (cu->language != language_ada)
7168             SET_TYPE_FIELD_PRIVATE (type, nfields);
7169           break;
7170
7171         case DW_ACCESS_protected:
7172           if (cu->language != language_ada)
7173             SET_TYPE_FIELD_PROTECTED (type, nfields);
7174           break;
7175
7176         case DW_ACCESS_public:
7177           break;
7178
7179         default:
7180           /* Unknown accessibility.  Complain and treat it as public.  */
7181           {
7182             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7183                        fieldp->accessibility);
7184           }
7185           break;
7186         }
7187       if (nfields < fip->nbaseclasses)
7188         {
7189           switch (fieldp->virtuality)
7190             {
7191             case DW_VIRTUALITY_virtual:
7192             case DW_VIRTUALITY_pure_virtual:
7193               if (cu->language == language_ada)
7194                 error (_("unexpected virtuality in component of Ada type"));
7195               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7196               break;
7197             }
7198         }
7199     }
7200 }
7201
7202 /* Add a member function to the proper fieldlist.  */
7203
7204 static void
7205 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7206                       struct type *type, struct dwarf2_cu *cu)
7207 {
7208   struct objfile *objfile = cu->objfile;
7209   struct attribute *attr;
7210   struct fnfieldlist *flp;
7211   int i;
7212   struct fn_field *fnp;
7213   char *fieldname;
7214   struct nextfnfield *new_fnfield;
7215   struct type *this_type;
7216   enum dwarf_access_attribute accessibility;
7217
7218   if (cu->language == language_ada)
7219     error (_("unexpected member function in Ada type"));
7220
7221   /* Get name of member function.  */
7222   fieldname = dwarf2_name (die, cu);
7223   if (fieldname == NULL)
7224     return;
7225
7226   /* Look up member function name in fieldlist.  */
7227   for (i = 0; i < fip->nfnfields; i++)
7228     {
7229       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7230         break;
7231     }
7232
7233   /* Create new list element if necessary.  */
7234   if (i < fip->nfnfields)
7235     flp = &fip->fnfieldlists[i];
7236   else
7237     {
7238       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7239         {
7240           fip->fnfieldlists = (struct fnfieldlist *)
7241             xrealloc (fip->fnfieldlists,
7242                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7243                       * sizeof (struct fnfieldlist));
7244           if (fip->nfnfields == 0)
7245             make_cleanup (free_current_contents, &fip->fnfieldlists);
7246         }
7247       flp = &fip->fnfieldlists[fip->nfnfields];
7248       flp->name = fieldname;
7249       flp->length = 0;
7250       flp->head = NULL;
7251       i = fip->nfnfields++;
7252     }
7253
7254   /* Create a new member function field and chain it to the field list
7255      entry.  */
7256   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7257   make_cleanup (xfree, new_fnfield);
7258   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7259   new_fnfield->next = flp->head;
7260   flp->head = new_fnfield;
7261   flp->length++;
7262
7263   /* Fill in the member function field info.  */
7264   fnp = &new_fnfield->fnfield;
7265
7266   /* Delay processing of the physname until later.  */
7267   if (cu->language == language_cplus || cu->language == language_java)
7268     {
7269       add_to_method_list (type, i, flp->length - 1, fieldname,
7270                           die, cu);
7271     }
7272   else
7273     {
7274       const char *physname = dwarf2_physname (fieldname, die, cu);
7275       fnp->physname = physname ? physname : "";
7276     }
7277
7278   fnp->type = alloc_type (objfile);
7279   this_type = read_type_die (die, cu);
7280   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7281     {
7282       int nparams = TYPE_NFIELDS (this_type);
7283
7284       /* TYPE is the domain of this method, and THIS_TYPE is the type
7285            of the method itself (TYPE_CODE_METHOD).  */
7286       smash_to_method_type (fnp->type, type,
7287                             TYPE_TARGET_TYPE (this_type),
7288                             TYPE_FIELDS (this_type),
7289                             TYPE_NFIELDS (this_type),
7290                             TYPE_VARARGS (this_type));
7291
7292       /* Handle static member functions.
7293          Dwarf2 has no clean way to discern C++ static and non-static
7294          member functions.  G++ helps GDB by marking the first
7295          parameter for non-static member functions (which is the this
7296          pointer) as artificial.  We obtain this information from
7297          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7298       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7299         fnp->voffset = VOFFSET_STATIC;
7300     }
7301   else
7302     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7303                dwarf2_full_name (fieldname, die, cu));
7304
7305   /* Get fcontext from DW_AT_containing_type if present.  */
7306   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7307     fnp->fcontext = die_containing_type (die, cu);
7308
7309   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7310      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7311
7312   /* Get accessibility.  */
7313   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7314   if (attr)
7315     accessibility = DW_UNSND (attr);
7316   else
7317     accessibility = dwarf2_default_access_attribute (die, cu);
7318   switch (accessibility)
7319     {
7320     case DW_ACCESS_private:
7321       fnp->is_private = 1;
7322       break;
7323     case DW_ACCESS_protected:
7324       fnp->is_protected = 1;
7325       break;
7326     }
7327
7328   /* Check for artificial methods.  */
7329   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7330   if (attr && DW_UNSND (attr) != 0)
7331     fnp->is_artificial = 1;
7332
7333   /* Get index in virtual function table if it is a virtual member
7334      function.  For older versions of GCC, this is an offset in the
7335      appropriate virtual table, as specified by DW_AT_containing_type.
7336      For everyone else, it is an expression to be evaluated relative
7337      to the object address.  */
7338
7339   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7340   if (attr)
7341     {
7342       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7343         {
7344           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7345             {
7346               /* Old-style GCC.  */
7347               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7348             }
7349           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7350                    || (DW_BLOCK (attr)->size > 1
7351                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7352                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7353             {
7354               struct dwarf_block blk;
7355               int offset;
7356
7357               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7358                         ? 1 : 2);
7359               blk.size = DW_BLOCK (attr)->size - offset;
7360               blk.data = DW_BLOCK (attr)->data + offset;
7361               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7362               if ((fnp->voffset % cu->header.addr_size) != 0)
7363                 dwarf2_complex_location_expr_complaint ();
7364               else
7365                 fnp->voffset /= cu->header.addr_size;
7366               fnp->voffset += 2;
7367             }
7368           else
7369             dwarf2_complex_location_expr_complaint ();
7370
7371           if (!fnp->fcontext)
7372             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7373         }
7374       else if (attr_form_is_section_offset (attr))
7375         {
7376           dwarf2_complex_location_expr_complaint ();
7377         }
7378       else
7379         {
7380           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7381                                                  fieldname);
7382         }
7383     }
7384   else
7385     {
7386       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7387       if (attr && DW_UNSND (attr))
7388         {
7389           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7390           complaint (&symfile_complaints,
7391                      _("Member function \"%s\" (offset %d) is virtual "
7392                        "but the vtable offset is not specified"),
7393                      fieldname, die->offset);
7394           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7395           TYPE_CPLUS_DYNAMIC (type) = 1;
7396         }
7397     }
7398 }
7399
7400 /* Create the vector of member function fields, and attach it to the type.  */
7401
7402 static void
7403 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7404                                  struct dwarf2_cu *cu)
7405 {
7406   struct fnfieldlist *flp;
7407   int i;
7408
7409   if (cu->language == language_ada)
7410     error (_("unexpected member functions in Ada type"));
7411
7412   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7413   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7414     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7415
7416   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7417     {
7418       struct nextfnfield *nfp = flp->head;
7419       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7420       int k;
7421
7422       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7423       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7424       fn_flp->fn_fields = (struct fn_field *)
7425         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7426       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7427         fn_flp->fn_fields[k] = nfp->fnfield;
7428     }
7429
7430   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7431 }
7432
7433 /* Returns non-zero if NAME is the name of a vtable member in CU's
7434    language, zero otherwise.  */
7435 static int
7436 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7437 {
7438   static const char vptr[] = "_vptr";
7439   static const char vtable[] = "vtable";
7440
7441   /* Look for the C++ and Java forms of the vtable.  */
7442   if ((cu->language == language_java
7443        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7444        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7445        && is_cplus_marker (name[sizeof (vptr) - 1])))
7446     return 1;
7447
7448   return 0;
7449 }
7450
7451 /* GCC outputs unnamed structures that are really pointers to member
7452    functions, with the ABI-specified layout.  If TYPE describes
7453    such a structure, smash it into a member function type.
7454
7455    GCC shouldn't do this; it should just output pointer to member DIEs.
7456    This is GCC PR debug/28767.  */
7457
7458 static void
7459 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7460 {
7461   struct type *pfn_type, *domain_type, *new_type;
7462
7463   /* Check for a structure with no name and two children.  */
7464   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7465     return;
7466
7467   /* Check for __pfn and __delta members.  */
7468   if (TYPE_FIELD_NAME (type, 0) == NULL
7469       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7470       || TYPE_FIELD_NAME (type, 1) == NULL
7471       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7472     return;
7473
7474   /* Find the type of the method.  */
7475   pfn_type = TYPE_FIELD_TYPE (type, 0);
7476   if (pfn_type == NULL
7477       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7478       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7479     return;
7480
7481   /* Look for the "this" argument.  */
7482   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7483   if (TYPE_NFIELDS (pfn_type) == 0
7484       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7485       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7486     return;
7487
7488   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7489   new_type = alloc_type (objfile);
7490   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7491                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7492                         TYPE_VARARGS (pfn_type));
7493   smash_to_methodptr_type (type, new_type);
7494 }
7495
7496 /* Called when we find the DIE that starts a structure or union scope
7497    (definition) to create a type for the structure or union.  Fill in
7498    the type's name and general properties; the members will not be
7499    processed until process_structure_type.
7500
7501    NOTE: we need to call these functions regardless of whether or not the
7502    DIE has a DW_AT_name attribute, since it might be an anonymous
7503    structure or union.  This gets the type entered into our set of
7504    user defined types.
7505
7506    However, if the structure is incomplete (an opaque struct/union)
7507    then suppress creating a symbol table entry for it since gdb only
7508    wants to find the one with the complete definition.  Note that if
7509    it is complete, we just call new_symbol, which does it's own
7510    checking about whether the struct/union is anonymous or not (and
7511    suppresses creating a symbol table entry itself).  */
7512
7513 static struct type *
7514 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7515 {
7516   struct objfile *objfile = cu->objfile;
7517   struct type *type;
7518   struct attribute *attr;
7519   char *name;
7520
7521   /* If the definition of this type lives in .debug_types, read that type.
7522      Don't follow DW_AT_specification though, that will take us back up
7523      the chain and we want to go down.  */
7524   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7525   if (attr)
7526     {
7527       struct dwarf2_cu *type_cu = cu;
7528       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7529
7530       /* We could just recurse on read_structure_type, but we need to call
7531          get_die_type to ensure only one type for this DIE is created.
7532          This is important, for example, because for c++ classes we need
7533          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7534       type = read_type_die (type_die, type_cu);
7535
7536       /* TYPE_CU may not be the same as CU.
7537          Ensure TYPE is recorded in CU's type_hash table.  */
7538       return set_die_type (die, type, cu);
7539     }
7540
7541   type = alloc_type (objfile);
7542   INIT_CPLUS_SPECIFIC (type);
7543
7544   name = dwarf2_name (die, cu);
7545   if (name != NULL)
7546     {
7547       if (cu->language == language_cplus
7548           || cu->language == language_java)
7549         {
7550           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7551
7552           /* dwarf2_full_name might have already finished building the DIE's
7553              type.  If so, there is no need to continue.  */
7554           if (get_die_type (die, cu) != NULL)
7555             return get_die_type (die, cu);
7556
7557           TYPE_TAG_NAME (type) = full_name;
7558           if (die->tag == DW_TAG_structure_type
7559               || die->tag == DW_TAG_class_type)
7560             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7561         }
7562       else
7563         {
7564           /* The name is already allocated along with this objfile, so
7565              we don't need to duplicate it for the type.  */
7566           TYPE_TAG_NAME (type) = (char *) name;
7567           if (die->tag == DW_TAG_class_type)
7568             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7569         }
7570     }
7571
7572   if (die->tag == DW_TAG_structure_type)
7573     {
7574       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7575     }
7576   else if (die->tag == DW_TAG_union_type)
7577     {
7578       TYPE_CODE (type) = TYPE_CODE_UNION;
7579     }
7580   else
7581     {
7582       TYPE_CODE (type) = TYPE_CODE_CLASS;
7583     }
7584
7585   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7586     TYPE_DECLARED_CLASS (type) = 1;
7587
7588   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7589   if (attr)
7590     {
7591       TYPE_LENGTH (type) = DW_UNSND (attr);
7592     }
7593   else
7594     {
7595       TYPE_LENGTH (type) = 0;
7596     }
7597
7598   TYPE_STUB_SUPPORTED (type) = 1;
7599   if (die_is_declaration (die, cu))
7600     TYPE_STUB (type) = 1;
7601   else if (attr == NULL && die->child == NULL
7602            && producer_is_realview (cu->producer))
7603     /* RealView does not output the required DW_AT_declaration
7604        on incomplete types.  */
7605     TYPE_STUB (type) = 1;
7606
7607   /* We need to add the type field to the die immediately so we don't
7608      infinitely recurse when dealing with pointers to the structure
7609      type within the structure itself.  */
7610   set_die_type (die, type, cu);
7611
7612   /* set_die_type should be already done.  */
7613   set_descriptive_type (type, die, cu);
7614
7615   return type;
7616 }
7617
7618 /* Finish creating a structure or union type, including filling in
7619    its members and creating a symbol for it.  */
7620
7621 static void
7622 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7623 {
7624   struct objfile *objfile = cu->objfile;
7625   struct die_info *child_die = die->child;
7626   struct type *type;
7627
7628   type = get_die_type (die, cu);
7629   if (type == NULL)
7630     type = read_structure_type (die, cu);
7631
7632   if (die->child != NULL && ! die_is_declaration (die, cu))
7633     {
7634       struct field_info fi;
7635       struct die_info *child_die;
7636       VEC (symbolp) *template_args = NULL;
7637       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7638
7639       memset (&fi, 0, sizeof (struct field_info));
7640
7641       child_die = die->child;
7642
7643       while (child_die && child_die->tag)
7644         {
7645           if (child_die->tag == DW_TAG_member
7646               || child_die->tag == DW_TAG_variable)
7647             {
7648               /* NOTE: carlton/2002-11-05: A C++ static data member
7649                  should be a DW_TAG_member that is a declaration, but
7650                  all versions of G++ as of this writing (so through at
7651                  least 3.2.1) incorrectly generate DW_TAG_variable
7652                  tags for them instead.  */
7653               dwarf2_add_field (&fi, child_die, cu);
7654             }
7655           else if (child_die->tag == DW_TAG_subprogram)
7656             {
7657               /* C++ member function.  */
7658               dwarf2_add_member_fn (&fi, child_die, type, cu);
7659             }
7660           else if (child_die->tag == DW_TAG_inheritance)
7661             {
7662               /* C++ base class field.  */
7663               dwarf2_add_field (&fi, child_die, cu);
7664             }
7665           else if (child_die->tag == DW_TAG_typedef)
7666             dwarf2_add_typedef (&fi, child_die, cu);
7667           else if (child_die->tag == DW_TAG_template_type_param
7668                    || child_die->tag == DW_TAG_template_value_param)
7669             {
7670               struct symbol *arg = new_symbol (child_die, NULL, cu);
7671
7672               if (arg != NULL)
7673                 VEC_safe_push (symbolp, template_args, arg);
7674             }
7675
7676           child_die = sibling_die (child_die);
7677         }
7678
7679       /* Attach template arguments to type.  */
7680       if (! VEC_empty (symbolp, template_args))
7681         {
7682           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7683           TYPE_N_TEMPLATE_ARGUMENTS (type)
7684             = VEC_length (symbolp, template_args);
7685           TYPE_TEMPLATE_ARGUMENTS (type)
7686             = obstack_alloc (&objfile->objfile_obstack,
7687                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7688                               * sizeof (struct symbol *)));
7689           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7690                   VEC_address (symbolp, template_args),
7691                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7692                    * sizeof (struct symbol *)));
7693           VEC_free (symbolp, template_args);
7694         }
7695
7696       /* Attach fields and member functions to the type.  */
7697       if (fi.nfields)
7698         dwarf2_attach_fields_to_type (&fi, type, cu);
7699       if (fi.nfnfields)
7700         {
7701           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7702
7703           /* Get the type which refers to the base class (possibly this
7704              class itself) which contains the vtable pointer for the current
7705              class from the DW_AT_containing_type attribute.  This use of
7706              DW_AT_containing_type is a GNU extension.  */
7707
7708           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7709             {
7710               struct type *t = die_containing_type (die, cu);
7711
7712               TYPE_VPTR_BASETYPE (type) = t;
7713               if (type == t)
7714                 {
7715                   int i;
7716
7717                   /* Our own class provides vtbl ptr.  */
7718                   for (i = TYPE_NFIELDS (t) - 1;
7719                        i >= TYPE_N_BASECLASSES (t);
7720                        --i)
7721                     {
7722                       char *fieldname = TYPE_FIELD_NAME (t, i);
7723
7724                       if (is_vtable_name (fieldname, cu))
7725                         {
7726                           TYPE_VPTR_FIELDNO (type) = i;
7727                           break;
7728                         }
7729                     }
7730
7731                   /* Complain if virtual function table field not found.  */
7732                   if (i < TYPE_N_BASECLASSES (t))
7733                     complaint (&symfile_complaints,
7734                                _("virtual function table pointer "
7735                                  "not found when defining class '%s'"),
7736                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7737                                "");
7738                 }
7739               else
7740                 {
7741                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7742                 }
7743             }
7744           else if (cu->producer
7745                    && strncmp (cu->producer,
7746                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7747             {
7748               /* The IBM XLC compiler does not provide direct indication
7749                  of the containing type, but the vtable pointer is
7750                  always named __vfp.  */
7751
7752               int i;
7753
7754               for (i = TYPE_NFIELDS (type) - 1;
7755                    i >= TYPE_N_BASECLASSES (type);
7756                    --i)
7757                 {
7758                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7759                     {
7760                       TYPE_VPTR_FIELDNO (type) = i;
7761                       TYPE_VPTR_BASETYPE (type) = type;
7762                       break;
7763                     }
7764                 }
7765             }
7766         }
7767
7768       /* Copy fi.typedef_field_list linked list elements content into the
7769          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7770       if (fi.typedef_field_list)
7771         {
7772           int i = fi.typedef_field_list_count;
7773
7774           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7775           TYPE_TYPEDEF_FIELD_ARRAY (type)
7776             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7777           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7778
7779           /* Reverse the list order to keep the debug info elements order.  */
7780           while (--i >= 0)
7781             {
7782               struct typedef_field *dest, *src;
7783
7784               dest = &TYPE_TYPEDEF_FIELD (type, i);
7785               src = &fi.typedef_field_list->field;
7786               fi.typedef_field_list = fi.typedef_field_list->next;
7787               *dest = *src;
7788             }
7789         }
7790
7791       do_cleanups (back_to);
7792
7793       if (HAVE_CPLUS_STRUCT (type))
7794         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7795     }
7796
7797   quirk_gcc_member_function_pointer (type, objfile);
7798
7799   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7800      snapshots) has been known to create a die giving a declaration
7801      for a class that has, as a child, a die giving a definition for a
7802      nested class.  So we have to process our children even if the
7803      current die is a declaration.  Normally, of course, a declaration
7804      won't have any children at all.  */
7805
7806   while (child_die != NULL && child_die->tag)
7807     {
7808       if (child_die->tag == DW_TAG_member
7809           || child_die->tag == DW_TAG_variable
7810           || child_die->tag == DW_TAG_inheritance
7811           || child_die->tag == DW_TAG_template_value_param
7812           || child_die->tag == DW_TAG_template_type_param)
7813         {
7814           /* Do nothing.  */
7815         }
7816       else
7817         process_die (child_die, cu);
7818
7819       child_die = sibling_die (child_die);
7820     }
7821
7822   /* Do not consider external references.  According to the DWARF standard,
7823      these DIEs are identified by the fact that they have no byte_size
7824      attribute, and a declaration attribute.  */
7825   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7826       || !die_is_declaration (die, cu))
7827     new_symbol (die, type, cu);
7828 }
7829
7830 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7831    complete the type's fields yet, or create any symbols.  */
7832
7833 static struct type *
7834 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7835 {
7836   struct objfile *objfile = cu->objfile;
7837   struct type *type;
7838   struct attribute *attr;
7839   const char *name;
7840
7841   /* If the definition of this type lives in .debug_types, read that type.
7842      Don't follow DW_AT_specification though, that will take us back up
7843      the chain and we want to go down.  */
7844   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7845   if (attr)
7846     {
7847       struct dwarf2_cu *type_cu = cu;
7848       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7849
7850       type = read_type_die (type_die, type_cu);
7851
7852       /* TYPE_CU may not be the same as CU.
7853          Ensure TYPE is recorded in CU's type_hash table.  */
7854       return set_die_type (die, type, cu);
7855     }
7856
7857   type = alloc_type (objfile);
7858
7859   TYPE_CODE (type) = TYPE_CODE_ENUM;
7860   name = dwarf2_full_name (NULL, die, cu);
7861   if (name != NULL)
7862     TYPE_TAG_NAME (type) = (char *) name;
7863
7864   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7865   if (attr)
7866     {
7867       TYPE_LENGTH (type) = DW_UNSND (attr);
7868     }
7869   else
7870     {
7871       TYPE_LENGTH (type) = 0;
7872     }
7873
7874   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7875      declared as private in the package spec, and then defined only
7876      inside the package body.  Such types are known as Taft Amendment
7877      Types.  When another package uses such a type, an incomplete DIE
7878      may be generated by the compiler.  */
7879   if (die_is_declaration (die, cu))
7880     TYPE_STUB (type) = 1;
7881
7882   return set_die_type (die, type, cu);
7883 }
7884
7885 /* Given a pointer to a die which begins an enumeration, process all
7886    the dies that define the members of the enumeration, and create the
7887    symbol for the enumeration type.
7888
7889    NOTE: We reverse the order of the element list.  */
7890
7891 static void
7892 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7893 {
7894   struct type *this_type;
7895
7896   this_type = get_die_type (die, cu);
7897   if (this_type == NULL)
7898     this_type = read_enumeration_type (die, cu);
7899
7900   if (die->child != NULL)
7901     {
7902       struct die_info *child_die;
7903       struct symbol *sym;
7904       struct field *fields = NULL;
7905       int num_fields = 0;
7906       int unsigned_enum = 1;
7907       char *name;
7908
7909       child_die = die->child;
7910       while (child_die && child_die->tag)
7911         {
7912           if (child_die->tag != DW_TAG_enumerator)
7913             {
7914               process_die (child_die, cu);
7915             }
7916           else
7917             {
7918               name = dwarf2_name (child_die, cu);
7919               if (name)
7920                 {
7921                   sym = new_symbol (child_die, this_type, cu);
7922                   if (SYMBOL_VALUE (sym) < 0)
7923                     unsigned_enum = 0;
7924
7925                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7926                     {
7927                       fields = (struct field *)
7928                         xrealloc (fields,
7929                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7930                                   * sizeof (struct field));
7931                     }
7932
7933                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7934                   FIELD_TYPE (fields[num_fields]) = NULL;
7935                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7936                   FIELD_BITSIZE (fields[num_fields]) = 0;
7937
7938                   num_fields++;
7939                 }
7940             }
7941
7942           child_die = sibling_die (child_die);
7943         }
7944
7945       if (num_fields)
7946         {
7947           TYPE_NFIELDS (this_type) = num_fields;
7948           TYPE_FIELDS (this_type) = (struct field *)
7949             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7950           memcpy (TYPE_FIELDS (this_type), fields,
7951                   sizeof (struct field) * num_fields);
7952           xfree (fields);
7953         }
7954       if (unsigned_enum)
7955         TYPE_UNSIGNED (this_type) = 1;
7956     }
7957
7958   /* If we are reading an enum from a .debug_types unit, and the enum
7959      is a declaration, and the enum is not the signatured type in the
7960      unit, then we do not want to add a symbol for it.  Adding a
7961      symbol would in some cases obscure the true definition of the
7962      enum, giving users an incomplete type when the definition is
7963      actually available.  Note that we do not want to do this for all
7964      enums which are just declarations, because C++0x allows forward
7965      enum declarations.  */
7966   if (cu->per_cu->debug_types_section
7967       && die_is_declaration (die, cu))
7968     {
7969       struct signatured_type *type_sig;
7970
7971       type_sig
7972         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7973                                             cu->per_cu->debug_types_section,
7974                                             cu->per_cu->offset);
7975       if (type_sig->type_offset != die->offset)
7976         return;
7977     }
7978
7979   new_symbol (die, this_type, cu);
7980 }
7981
7982 /* Extract all information from a DW_TAG_array_type DIE and put it in
7983    the DIE's type field.  For now, this only handles one dimensional
7984    arrays.  */
7985
7986 static struct type *
7987 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7988 {
7989   struct objfile *objfile = cu->objfile;
7990   struct die_info *child_die;
7991   struct type *type;
7992   struct type *element_type, *range_type, *index_type;
7993   struct type **range_types = NULL;
7994   struct attribute *attr;
7995   int ndim = 0;
7996   struct cleanup *back_to;
7997   char *name;
7998
7999   element_type = die_type (die, cu);
8000
8001   /* The die_type call above may have already set the type for this DIE.  */
8002   type = get_die_type (die, cu);
8003   if (type)
8004     return type;
8005
8006   /* Irix 6.2 native cc creates array types without children for
8007      arrays with unspecified length.  */
8008   if (die->child == NULL)
8009     {
8010       index_type = objfile_type (objfile)->builtin_int;
8011       range_type = create_range_type (NULL, index_type, 0, -1);
8012       type = create_array_type (NULL, element_type, range_type);
8013       return set_die_type (die, type, cu);
8014     }
8015
8016   back_to = make_cleanup (null_cleanup, NULL);
8017   child_die = die->child;
8018   while (child_die && child_die->tag)
8019     {
8020       if (child_die->tag == DW_TAG_subrange_type)
8021         {
8022           struct type *child_type = read_type_die (child_die, cu);
8023
8024           if (child_type != NULL)
8025             {
8026               /* The range type was succesfully read.  Save it for the
8027                  array type creation.  */
8028               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8029                 {
8030                   range_types = (struct type **)
8031                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8032                               * sizeof (struct type *));
8033                   if (ndim == 0)
8034                     make_cleanup (free_current_contents, &range_types);
8035                 }
8036               range_types[ndim++] = child_type;
8037             }
8038         }
8039       child_die = sibling_die (child_die);
8040     }
8041
8042   /* Dwarf2 dimensions are output from left to right, create the
8043      necessary array types in backwards order.  */
8044
8045   type = element_type;
8046
8047   if (read_array_order (die, cu) == DW_ORD_col_major)
8048     {
8049       int i = 0;
8050
8051       while (i < ndim)
8052         type = create_array_type (NULL, type, range_types[i++]);
8053     }
8054   else
8055     {
8056       while (ndim-- > 0)
8057         type = create_array_type (NULL, type, range_types[ndim]);
8058     }
8059
8060   /* Understand Dwarf2 support for vector types (like they occur on
8061      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8062      array type.  This is not part of the Dwarf2/3 standard yet, but a
8063      custom vendor extension.  The main difference between a regular
8064      array and the vector variant is that vectors are passed by value
8065      to functions.  */
8066   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8067   if (attr)
8068     make_vector_type (type);
8069
8070   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8071      implementation may choose to implement triple vectors using this
8072      attribute.  */
8073   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8074   if (attr)
8075     {
8076       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8077         TYPE_LENGTH (type) = DW_UNSND (attr);
8078       else
8079         complaint (&symfile_complaints,
8080                    _("DW_AT_byte_size for array type smaller "
8081                      "than the total size of elements"));
8082     }
8083
8084   name = dwarf2_name (die, cu);
8085   if (name)
8086     TYPE_NAME (type) = name;
8087
8088   /* Install the type in the die.  */
8089   set_die_type (die, type, cu);
8090
8091   /* set_die_type should be already done.  */
8092   set_descriptive_type (type, die, cu);
8093
8094   do_cleanups (back_to);
8095
8096   return type;
8097 }
8098
8099 static enum dwarf_array_dim_ordering
8100 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8101 {
8102   struct attribute *attr;
8103
8104   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8105
8106   if (attr) return DW_SND (attr);
8107
8108   /* GNU F77 is a special case, as at 08/2004 array type info is the
8109      opposite order to the dwarf2 specification, but data is still
8110      laid out as per normal fortran.
8111
8112      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8113      version checking.  */
8114
8115   if (cu->language == language_fortran
8116       && cu->producer && strstr (cu->producer, "GNU F77"))
8117     {
8118       return DW_ORD_row_major;
8119     }
8120
8121   switch (cu->language_defn->la_array_ordering)
8122     {
8123     case array_column_major:
8124       return DW_ORD_col_major;
8125     case array_row_major:
8126     default:
8127       return DW_ORD_row_major;
8128     };
8129 }
8130
8131 /* Extract all information from a DW_TAG_set_type DIE and put it in
8132    the DIE's type field.  */
8133
8134 static struct type *
8135 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8136 {
8137   struct type *domain_type, *set_type;
8138   struct attribute *attr;
8139
8140   domain_type = die_type (die, cu);
8141
8142   /* The die_type call above may have already set the type for this DIE.  */
8143   set_type = get_die_type (die, cu);
8144   if (set_type)
8145     return set_type;
8146
8147   set_type = create_set_type (NULL, domain_type);
8148
8149   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8150   if (attr)
8151     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8152
8153   return set_die_type (die, set_type, cu);
8154 }
8155
8156 /* First cut: install each common block member as a global variable.  */
8157
8158 static void
8159 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8160 {
8161   struct die_info *child_die;
8162   struct attribute *attr;
8163   struct symbol *sym;
8164   CORE_ADDR base = (CORE_ADDR) 0;
8165
8166   attr = dwarf2_attr (die, DW_AT_location, cu);
8167   if (attr)
8168     {
8169       /* Support the .debug_loc offsets.  */
8170       if (attr_form_is_block (attr))
8171         {
8172           base = decode_locdesc (DW_BLOCK (attr), cu);
8173         }
8174       else if (attr_form_is_section_offset (attr))
8175         {
8176           dwarf2_complex_location_expr_complaint ();
8177         }
8178       else
8179         {
8180           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8181                                                  "common block member");
8182         }
8183     }
8184   if (die->child != NULL)
8185     {
8186       child_die = die->child;
8187       while (child_die && child_die->tag)
8188         {
8189           LONGEST offset;
8190
8191           sym = new_symbol (child_die, NULL, cu);
8192           if (sym != NULL
8193               && handle_data_member_location (child_die, cu, &offset))
8194             {
8195               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8196               add_symbol_to_list (sym, &global_symbols);
8197             }
8198           child_die = sibling_die (child_die);
8199         }
8200     }
8201 }
8202
8203 /* Create a type for a C++ namespace.  */
8204
8205 static struct type *
8206 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8207 {
8208   struct objfile *objfile = cu->objfile;
8209   const char *previous_prefix, *name;
8210   int is_anonymous;
8211   struct type *type;
8212
8213   /* For extensions, reuse the type of the original namespace.  */
8214   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8215     {
8216       struct die_info *ext_die;
8217       struct dwarf2_cu *ext_cu = cu;
8218
8219       ext_die = dwarf2_extension (die, &ext_cu);
8220       type = read_type_die (ext_die, ext_cu);
8221
8222       /* EXT_CU may not be the same as CU.
8223          Ensure TYPE is recorded in CU's type_hash table.  */
8224       return set_die_type (die, type, cu);
8225     }
8226
8227   name = namespace_name (die, &is_anonymous, cu);
8228
8229   /* Now build the name of the current namespace.  */
8230
8231   previous_prefix = determine_prefix (die, cu);
8232   if (previous_prefix[0] != '\0')
8233     name = typename_concat (&objfile->objfile_obstack,
8234                             previous_prefix, name, 0, cu);
8235
8236   /* Create the type.  */
8237   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8238                     objfile);
8239   TYPE_NAME (type) = (char *) name;
8240   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8241
8242   return set_die_type (die, type, cu);
8243 }
8244
8245 /* Read a C++ namespace.  */
8246
8247 static void
8248 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8249 {
8250   struct objfile *objfile = cu->objfile;
8251   int is_anonymous;
8252
8253   /* Add a symbol associated to this if we haven't seen the namespace
8254      before.  Also, add a using directive if it's an anonymous
8255      namespace.  */
8256
8257   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8258     {
8259       struct type *type;
8260
8261       type = read_type_die (die, cu);
8262       new_symbol (die, type, cu);
8263
8264       namespace_name (die, &is_anonymous, cu);
8265       if (is_anonymous)
8266         {
8267           const char *previous_prefix = determine_prefix (die, cu);
8268
8269           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8270                                   NULL, NULL, &objfile->objfile_obstack);
8271         }
8272     }
8273
8274   if (die->child != NULL)
8275     {
8276       struct die_info *child_die = die->child;
8277
8278       while (child_die && child_die->tag)
8279         {
8280           process_die (child_die, cu);
8281           child_die = sibling_die (child_die);
8282         }
8283     }
8284 }
8285
8286 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8287    imported module.  Still we need that type as local Fortran "use ... only"
8288    declaration imports depend on the created type in determine_prefix.  */
8289
8290 static struct type *
8291 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8292 {
8293   struct objfile *objfile = cu->objfile;
8294   char *module_name;
8295   struct type *type;
8296
8297   module_name = dwarf2_name (die, cu);
8298   if (!module_name)
8299     complaint (&symfile_complaints,
8300                _("DW_TAG_module has no name, offset 0x%x"),
8301                die->offset);
8302   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8303
8304   /* determine_prefix uses TYPE_TAG_NAME.  */
8305   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8306
8307   return set_die_type (die, type, cu);
8308 }
8309
8310 /* Read a Fortran module.  */
8311
8312 static void
8313 read_module (struct die_info *die, struct dwarf2_cu *cu)
8314 {
8315   struct die_info *child_die = die->child;
8316
8317   while (child_die && child_die->tag)
8318     {
8319       process_die (child_die, cu);
8320       child_die = sibling_die (child_die);
8321     }
8322 }
8323
8324 /* Return the name of the namespace represented by DIE.  Set
8325    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8326    namespace.  */
8327
8328 static const char *
8329 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8330 {
8331   struct die_info *current_die;
8332   const char *name = NULL;
8333
8334   /* Loop through the extensions until we find a name.  */
8335
8336   for (current_die = die;
8337        current_die != NULL;
8338        current_die = dwarf2_extension (die, &cu))
8339     {
8340       name = dwarf2_name (current_die, cu);
8341       if (name != NULL)
8342         break;
8343     }
8344
8345   /* Is it an anonymous namespace?  */
8346
8347   *is_anonymous = (name == NULL);
8348   if (*is_anonymous)
8349     name = CP_ANONYMOUS_NAMESPACE_STR;
8350
8351   return name;
8352 }
8353
8354 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8355    the user defined type vector.  */
8356
8357 static struct type *
8358 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8359 {
8360   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8361   struct comp_unit_head *cu_header = &cu->header;
8362   struct type *type;
8363   struct attribute *attr_byte_size;
8364   struct attribute *attr_address_class;
8365   int byte_size, addr_class;
8366   struct type *target_type;
8367
8368   target_type = die_type (die, cu);
8369
8370   /* The die_type call above may have already set the type for this DIE.  */
8371   type = get_die_type (die, cu);
8372   if (type)
8373     return type;
8374
8375   type = lookup_pointer_type (target_type);
8376
8377   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8378   if (attr_byte_size)
8379     byte_size = DW_UNSND (attr_byte_size);
8380   else
8381     byte_size = cu_header->addr_size;
8382
8383   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8384   if (attr_address_class)
8385     addr_class = DW_UNSND (attr_address_class);
8386   else
8387     addr_class = DW_ADDR_none;
8388
8389   /* If the pointer size or address class is different than the
8390      default, create a type variant marked as such and set the
8391      length accordingly.  */
8392   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8393     {
8394       if (gdbarch_address_class_type_flags_p (gdbarch))
8395         {
8396           int type_flags;
8397
8398           type_flags = gdbarch_address_class_type_flags
8399                          (gdbarch, byte_size, addr_class);
8400           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8401                       == 0);
8402           type = make_type_with_address_space (type, type_flags);
8403         }
8404       else if (TYPE_LENGTH (type) != byte_size)
8405         {
8406           complaint (&symfile_complaints,
8407                      _("invalid pointer size %d"), byte_size);
8408         }
8409       else
8410         {
8411           /* Should we also complain about unhandled address classes?  */
8412         }
8413     }
8414
8415   TYPE_LENGTH (type) = byte_size;
8416   return set_die_type (die, type, cu);
8417 }
8418
8419 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8420    the user defined type vector.  */
8421
8422 static struct type *
8423 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8424 {
8425   struct type *type;
8426   struct type *to_type;
8427   struct type *domain;
8428
8429   to_type = die_type (die, cu);
8430   domain = die_containing_type (die, cu);
8431
8432   /* The calls above may have already set the type for this DIE.  */
8433   type = get_die_type (die, cu);
8434   if (type)
8435     return type;
8436
8437   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8438     type = lookup_methodptr_type (to_type);
8439   else
8440     type = lookup_memberptr_type (to_type, domain);
8441
8442   return set_die_type (die, type, cu);
8443 }
8444
8445 /* Extract all information from a DW_TAG_reference_type DIE and add to
8446    the user defined type vector.  */
8447
8448 static struct type *
8449 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8450 {
8451   struct comp_unit_head *cu_header = &cu->header;
8452   struct type *type, *target_type;
8453   struct attribute *attr;
8454
8455   target_type = die_type (die, cu);
8456
8457   /* The die_type call above may have already set the type for this DIE.  */
8458   type = get_die_type (die, cu);
8459   if (type)
8460     return type;
8461
8462   type = lookup_reference_type (target_type);
8463   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8464   if (attr)
8465     {
8466       TYPE_LENGTH (type) = DW_UNSND (attr);
8467     }
8468   else
8469     {
8470       TYPE_LENGTH (type) = cu_header->addr_size;
8471     }
8472   return set_die_type (die, type, cu);
8473 }
8474
8475 static struct type *
8476 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8477 {
8478   struct type *base_type, *cv_type;
8479
8480   base_type = die_type (die, cu);
8481
8482   /* The die_type call above may have already set the type for this DIE.  */
8483   cv_type = get_die_type (die, cu);
8484   if (cv_type)
8485     return cv_type;
8486
8487   /* In case the const qualifier is applied to an array type, the element type
8488      is so qualified, not the array type (section 6.7.3 of C99).  */
8489   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8490     {
8491       struct type *el_type, *inner_array;
8492
8493       base_type = copy_type (base_type);
8494       inner_array = base_type;
8495
8496       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8497         {
8498           TYPE_TARGET_TYPE (inner_array) =
8499             copy_type (TYPE_TARGET_TYPE (inner_array));
8500           inner_array = TYPE_TARGET_TYPE (inner_array);
8501         }
8502
8503       el_type = TYPE_TARGET_TYPE (inner_array);
8504       TYPE_TARGET_TYPE (inner_array) =
8505         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8506
8507       return set_die_type (die, base_type, cu);
8508     }
8509
8510   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8511   return set_die_type (die, cv_type, cu);
8512 }
8513
8514 static struct type *
8515 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8516 {
8517   struct type *base_type, *cv_type;
8518
8519   base_type = die_type (die, cu);
8520
8521   /* The die_type call above may have already set the type for this DIE.  */
8522   cv_type = get_die_type (die, cu);
8523   if (cv_type)
8524     return cv_type;
8525
8526   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8527   return set_die_type (die, cv_type, cu);
8528 }
8529
8530 /* Extract all information from a DW_TAG_string_type DIE and add to
8531    the user defined type vector.  It isn't really a user defined type,
8532    but it behaves like one, with other DIE's using an AT_user_def_type
8533    attribute to reference it.  */
8534
8535 static struct type *
8536 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8537 {
8538   struct objfile *objfile = cu->objfile;
8539   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8540   struct type *type, *range_type, *index_type, *char_type;
8541   struct attribute *attr;
8542   unsigned int length;
8543
8544   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8545   if (attr)
8546     {
8547       length = DW_UNSND (attr);
8548     }
8549   else
8550     {
8551       /* Check for the DW_AT_byte_size attribute.  */
8552       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8553       if (attr)
8554         {
8555           length = DW_UNSND (attr);
8556         }
8557       else
8558         {
8559           length = 1;
8560         }
8561     }
8562
8563   index_type = objfile_type (objfile)->builtin_int;
8564   range_type = create_range_type (NULL, index_type, 1, length);
8565   char_type = language_string_char_type (cu->language_defn, gdbarch);
8566   type = create_string_type (NULL, char_type, range_type);
8567
8568   return set_die_type (die, type, cu);
8569 }
8570
8571 /* Handle DIES due to C code like:
8572
8573    struct foo
8574    {
8575    int (*funcp)(int a, long l);
8576    int b;
8577    };
8578
8579    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8580
8581 static struct type *
8582 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8583 {
8584   struct objfile *objfile = cu->objfile;
8585   struct type *type;            /* Type that this function returns.  */
8586   struct type *ftype;           /* Function that returns above type.  */
8587   struct attribute *attr;
8588
8589   type = die_type (die, cu);
8590
8591   /* The die_type call above may have already set the type for this DIE.  */
8592   ftype = get_die_type (die, cu);
8593   if (ftype)
8594     return ftype;
8595
8596   ftype = lookup_function_type (type);
8597
8598   /* All functions in C++, Pascal and Java have prototypes.  */
8599   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8600   if ((attr && (DW_UNSND (attr) != 0))
8601       || cu->language == language_cplus
8602       || cu->language == language_java
8603       || cu->language == language_pascal)
8604     TYPE_PROTOTYPED (ftype) = 1;
8605   else if (producer_is_realview (cu->producer))
8606     /* RealView does not emit DW_AT_prototyped.  We can not
8607        distinguish prototyped and unprototyped functions; default to
8608        prototyped, since that is more common in modern code (and
8609        RealView warns about unprototyped functions).  */
8610     TYPE_PROTOTYPED (ftype) = 1;
8611
8612   /* Store the calling convention in the type if it's available in
8613      the subroutine die.  Otherwise set the calling convention to
8614      the default value DW_CC_normal.  */
8615   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8616   if (attr)
8617     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8618   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8619     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8620   else
8621     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8622
8623   /* We need to add the subroutine type to the die immediately so
8624      we don't infinitely recurse when dealing with parameters
8625      declared as the same subroutine type.  */
8626   set_die_type (die, ftype, cu);
8627
8628   if (die->child != NULL)
8629     {
8630       struct type *void_type = objfile_type (objfile)->builtin_void;
8631       struct die_info *child_die;
8632       int nparams, iparams;
8633
8634       /* Count the number of parameters.
8635          FIXME: GDB currently ignores vararg functions, but knows about
8636          vararg member functions.  */
8637       nparams = 0;
8638       child_die = die->child;
8639       while (child_die && child_die->tag)
8640         {
8641           if (child_die->tag == DW_TAG_formal_parameter)
8642             nparams++;
8643           else if (child_die->tag == DW_TAG_unspecified_parameters)
8644             TYPE_VARARGS (ftype) = 1;
8645           child_die = sibling_die (child_die);
8646         }
8647
8648       /* Allocate storage for parameters and fill them in.  */
8649       TYPE_NFIELDS (ftype) = nparams;
8650       TYPE_FIELDS (ftype) = (struct field *)
8651         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8652
8653       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8654          even if we error out during the parameters reading below.  */
8655       for (iparams = 0; iparams < nparams; iparams++)
8656         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8657
8658       iparams = 0;
8659       child_die = die->child;
8660       while (child_die && child_die->tag)
8661         {
8662           if (child_die->tag == DW_TAG_formal_parameter)
8663             {
8664               struct type *arg_type;
8665
8666               /* DWARF version 2 has no clean way to discern C++
8667                  static and non-static member functions.  G++ helps
8668                  GDB by marking the first parameter for non-static
8669                  member functions (which is the this pointer) as
8670                  artificial.  We pass this information to
8671                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8672
8673                  DWARF version 3 added DW_AT_object_pointer, which GCC
8674                  4.5 does not yet generate.  */
8675               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8676               if (attr)
8677                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8678               else
8679                 {
8680                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8681
8682                   /* GCC/43521: In java, the formal parameter
8683                      "this" is sometimes not marked with DW_AT_artificial.  */
8684                   if (cu->language == language_java)
8685                     {
8686                       const char *name = dwarf2_name (child_die, cu);
8687
8688                       if (name && !strcmp (name, "this"))
8689                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8690                     }
8691                 }
8692               arg_type = die_type (child_die, cu);
8693
8694               /* RealView does not mark THIS as const, which the testsuite
8695                  expects.  GCC marks THIS as const in method definitions,
8696                  but not in the class specifications (GCC PR 43053).  */
8697               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8698                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8699                 {
8700                   int is_this = 0;
8701                   struct dwarf2_cu *arg_cu = cu;
8702                   const char *name = dwarf2_name (child_die, cu);
8703
8704                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8705                   if (attr)
8706                     {
8707                       /* If the compiler emits this, use it.  */
8708                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8709                         is_this = 1;
8710                     }
8711                   else if (name && strcmp (name, "this") == 0)
8712                     /* Function definitions will have the argument names.  */
8713                     is_this = 1;
8714                   else if (name == NULL && iparams == 0)
8715                     /* Declarations may not have the names, so like
8716                        elsewhere in GDB, assume an artificial first
8717                        argument is "this".  */
8718                     is_this = 1;
8719
8720                   if (is_this)
8721                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8722                                              arg_type, 0);
8723                 }
8724
8725               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8726               iparams++;
8727             }
8728           child_die = sibling_die (child_die);
8729         }
8730     }
8731
8732   return ftype;
8733 }
8734
8735 static struct type *
8736 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8737 {
8738   struct objfile *objfile = cu->objfile;
8739   const char *name = NULL;
8740   struct type *this_type, *target_type;
8741
8742   name = dwarf2_full_name (NULL, die, cu);
8743   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8744                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8745   TYPE_NAME (this_type) = (char *) name;
8746   set_die_type (die, this_type, cu);
8747   target_type = die_type (die, cu);
8748   if (target_type != this_type)
8749     TYPE_TARGET_TYPE (this_type) = target_type;
8750   else
8751     {
8752       /* Self-referential typedefs are, it seems, not allowed by the DWARF
8753          spec and cause infinite loops in GDB.  */
8754       complaint (&symfile_complaints,
8755                  _("Self-referential DW_TAG_typedef "
8756                    "- DIE at 0x%x [in module %s]"),
8757                  die->offset, objfile->name);
8758       TYPE_TARGET_TYPE (this_type) = NULL;
8759     }
8760   return this_type;
8761 }
8762
8763 /* Find a representation of a given base type and install
8764    it in the TYPE field of the die.  */
8765
8766 static struct type *
8767 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8768 {
8769   struct objfile *objfile = cu->objfile;
8770   struct type *type;
8771   struct attribute *attr;
8772   int encoding = 0, size = 0;
8773   char *name;
8774   enum type_code code = TYPE_CODE_INT;
8775   int type_flags = 0;
8776   struct type *target_type = NULL;
8777
8778   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8779   if (attr)
8780     {
8781       encoding = DW_UNSND (attr);
8782     }
8783   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8784   if (attr)
8785     {
8786       size = DW_UNSND (attr);
8787     }
8788   name = dwarf2_name (die, cu);
8789   if (!name)
8790     {
8791       complaint (&symfile_complaints,
8792                  _("DW_AT_name missing from DW_TAG_base_type"));
8793     }
8794
8795   switch (encoding)
8796     {
8797       case DW_ATE_address:
8798         /* Turn DW_ATE_address into a void * pointer.  */
8799         code = TYPE_CODE_PTR;
8800         type_flags |= TYPE_FLAG_UNSIGNED;
8801         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8802         break;
8803       case DW_ATE_boolean:
8804         code = TYPE_CODE_BOOL;
8805         type_flags |= TYPE_FLAG_UNSIGNED;
8806         break;
8807       case DW_ATE_complex_float:
8808         code = TYPE_CODE_COMPLEX;
8809         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8810         break;
8811       case DW_ATE_decimal_float:
8812         code = TYPE_CODE_DECFLOAT;
8813         break;
8814       case DW_ATE_float:
8815         code = TYPE_CODE_FLT;
8816         break;
8817       case DW_ATE_signed:
8818         break;
8819       case DW_ATE_unsigned:
8820         type_flags |= TYPE_FLAG_UNSIGNED;
8821         if (cu->language == language_fortran
8822             && name
8823             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8824           code = TYPE_CODE_CHAR;
8825         break;
8826       case DW_ATE_signed_char:
8827         if (cu->language == language_ada || cu->language == language_m2
8828             || cu->language == language_pascal
8829             || cu->language == language_fortran)
8830           code = TYPE_CODE_CHAR;
8831         break;
8832       case DW_ATE_unsigned_char:
8833         if (cu->language == language_ada || cu->language == language_m2
8834             || cu->language == language_pascal
8835             || cu->language == language_fortran)
8836           code = TYPE_CODE_CHAR;
8837         type_flags |= TYPE_FLAG_UNSIGNED;
8838         break;
8839       case DW_ATE_UTF:
8840         /* We just treat this as an integer and then recognize the
8841            type by name elsewhere.  */
8842         break;
8843
8844       default:
8845         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8846                    dwarf_type_encoding_name (encoding));
8847         break;
8848     }
8849
8850   type = init_type (code, size, type_flags, NULL, objfile);
8851   TYPE_NAME (type) = name;
8852   TYPE_TARGET_TYPE (type) = target_type;
8853
8854   if (name && strcmp (name, "char") == 0)
8855     TYPE_NOSIGN (type) = 1;
8856
8857   return set_die_type (die, type, cu);
8858 }
8859
8860 /* Read the given DW_AT_subrange DIE.  */
8861
8862 static struct type *
8863 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8864 {
8865   struct type *base_type;
8866   struct type *range_type;
8867   struct attribute *attr;
8868   LONGEST low = 0;
8869   LONGEST high = -1;
8870   char *name;
8871   LONGEST negative_mask;
8872
8873   base_type = die_type (die, cu);
8874   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8875   check_typedef (base_type);
8876
8877   /* The die_type call above may have already set the type for this DIE.  */
8878   range_type = get_die_type (die, cu);
8879   if (range_type)
8880     return range_type;
8881
8882   if (cu->language == language_fortran)
8883     {
8884       /* FORTRAN implies a lower bound of 1, if not given.  */
8885       low = 1;
8886     }
8887
8888   /* FIXME: For variable sized arrays either of these could be
8889      a variable rather than a constant value.  We'll allow it,
8890      but we don't know how to handle it.  */
8891   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8892   if (attr)
8893     low = dwarf2_get_attr_constant_value (attr, 0);
8894
8895   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8896   if (attr)
8897     {
8898       if (attr_form_is_block (attr) || is_ref_attr (attr))
8899         {
8900           /* GCC encodes arrays with unspecified or dynamic length
8901              with a DW_FORM_block1 attribute or a reference attribute.
8902              FIXME: GDB does not yet know how to handle dynamic
8903              arrays properly, treat them as arrays with unspecified
8904              length for now.
8905
8906              FIXME: jimb/2003-09-22: GDB does not really know
8907              how to handle arrays of unspecified length
8908              either; we just represent them as zero-length
8909              arrays.  Choose an appropriate upper bound given
8910              the lower bound we've computed above.  */
8911           high = low - 1;
8912         }
8913       else
8914         high = dwarf2_get_attr_constant_value (attr, 1);
8915     }
8916   else
8917     {
8918       attr = dwarf2_attr (die, DW_AT_count, cu);
8919       if (attr)
8920         {
8921           int count = dwarf2_get_attr_constant_value (attr, 1);
8922           high = low + count - 1;
8923         }
8924       else
8925         {
8926           /* Unspecified array length.  */
8927           high = low - 1;
8928         }
8929     }
8930
8931   /* Dwarf-2 specifications explicitly allows to create subrange types
8932      without specifying a base type.
8933      In that case, the base type must be set to the type of
8934      the lower bound, upper bound or count, in that order, if any of these
8935      three attributes references an object that has a type.
8936      If no base type is found, the Dwarf-2 specifications say that
8937      a signed integer type of size equal to the size of an address should
8938      be used.
8939      For the following C code: `extern char gdb_int [];'
8940      GCC produces an empty range DIE.
8941      FIXME: muller/2010-05-28: Possible references to object for low bound,
8942      high bound or count are not yet handled by this code.  */
8943   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8944     {
8945       struct objfile *objfile = cu->objfile;
8946       struct gdbarch *gdbarch = get_objfile_arch (objfile);
8947       int addr_size = gdbarch_addr_bit (gdbarch) /8;
8948       struct type *int_type = objfile_type (objfile)->builtin_int;
8949
8950       /* Test "int", "long int", and "long long int" objfile types,
8951          and select the first one having a size above or equal to the
8952          architecture address size.  */
8953       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8954         base_type = int_type;
8955       else
8956         {
8957           int_type = objfile_type (objfile)->builtin_long;
8958           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8959             base_type = int_type;
8960           else
8961             {
8962               int_type = objfile_type (objfile)->builtin_long_long;
8963               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8964                 base_type = int_type;
8965             }
8966         }
8967     }
8968
8969   negative_mask =
8970     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8971   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8972     low |= negative_mask;
8973   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8974     high |= negative_mask;
8975
8976   range_type = create_range_type (NULL, base_type, low, high);
8977
8978   /* Mark arrays with dynamic length at least as an array of unspecified
8979      length.  GDB could check the boundary but before it gets implemented at
8980      least allow accessing the array elements.  */
8981   if (attr && attr_form_is_block (attr))
8982     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8983
8984   /* Ada expects an empty array on no boundary attributes.  */
8985   if (attr == NULL && cu->language != language_ada)
8986     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8987
8988   name = dwarf2_name (die, cu);
8989   if (name)
8990     TYPE_NAME (range_type) = name;
8991
8992   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8993   if (attr)
8994     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8995
8996   set_die_type (die, range_type, cu);
8997
8998   /* set_die_type should be already done.  */
8999   set_descriptive_type (range_type, die, cu);
9000
9001   return range_type;
9002 }
9003
9004 static struct type *
9005 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9006 {
9007   struct type *type;
9008
9009   /* For now, we only support the C meaning of an unspecified type: void.  */
9010
9011   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9012   TYPE_NAME (type) = dwarf2_name (die, cu);
9013
9014   return set_die_type (die, type, cu);
9015 }
9016
9017 /* Trivial hash function for die_info: the hash value of a DIE
9018    is its offset in .debug_info for this objfile.  */
9019
9020 static hashval_t
9021 die_hash (const void *item)
9022 {
9023   const struct die_info *die = item;
9024
9025   return die->offset;
9026 }
9027
9028 /* Trivial comparison function for die_info structures: two DIEs
9029    are equal if they have the same offset.  */
9030
9031 static int
9032 die_eq (const void *item_lhs, const void *item_rhs)
9033 {
9034   const struct die_info *die_lhs = item_lhs;
9035   const struct die_info *die_rhs = item_rhs;
9036
9037   return die_lhs->offset == die_rhs->offset;
9038 }
9039
9040 /* Read a whole compilation unit into a linked list of dies.  */
9041
9042 static struct die_info *
9043 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9044 {
9045   struct die_reader_specs reader_specs;
9046   int read_abbrevs = 0;
9047   struct cleanup *back_to = NULL;
9048   struct die_info *die;
9049
9050   if (cu->dwarf2_abbrevs == NULL)
9051     {
9052       dwarf2_read_abbrevs (cu);
9053       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9054       read_abbrevs = 1;
9055     }
9056
9057   gdb_assert (cu->die_hash == NULL);
9058   cu->die_hash
9059     = htab_create_alloc_ex (cu->header.length / 12,
9060                             die_hash,
9061                             die_eq,
9062                             NULL,
9063                             &cu->comp_unit_obstack,
9064                             hashtab_obstack_allocate,
9065                             dummy_obstack_deallocate);
9066
9067   init_cu_die_reader (&reader_specs, cu);
9068
9069   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9070
9071   if (read_abbrevs)
9072     do_cleanups (back_to);
9073
9074   return die;
9075 }
9076
9077 /* Main entry point for reading a DIE and all children.
9078    Read the DIE and dump it if requested.  */
9079
9080 static struct die_info *
9081 read_die_and_children (const struct die_reader_specs *reader,
9082                        gdb_byte *info_ptr,
9083                        gdb_byte **new_info_ptr,
9084                        struct die_info *parent)
9085 {
9086   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9087                                                      new_info_ptr, parent);
9088
9089   if (dwarf2_die_debug)
9090     {
9091       fprintf_unfiltered (gdb_stdlog,
9092                           "\nRead die from %s of %s:\n",
9093                           (reader->cu->per_cu->debug_types_section
9094                            ? ".debug_types"
9095                            : ".debug_info"),
9096                           reader->abfd->filename);
9097       dump_die (result, dwarf2_die_debug);
9098     }
9099
9100   return result;
9101 }
9102
9103 /* Read a single die and all its descendents.  Set the die's sibling
9104    field to NULL; set other fields in the die correctly, and set all
9105    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9106    location of the info_ptr after reading all of those dies.  PARENT
9107    is the parent of the die in question.  */
9108
9109 static struct die_info *
9110 read_die_and_children_1 (const struct die_reader_specs *reader,
9111                          gdb_byte *info_ptr,
9112                          gdb_byte **new_info_ptr,
9113                          struct die_info *parent)
9114 {
9115   struct die_info *die;
9116   gdb_byte *cur_ptr;
9117   int has_children;
9118
9119   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9120   if (die == NULL)
9121     {
9122       *new_info_ptr = cur_ptr;
9123       return NULL;
9124     }
9125   store_in_ref_table (die, reader->cu);
9126
9127   if (has_children)
9128     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9129   else
9130     {
9131       die->child = NULL;
9132       *new_info_ptr = cur_ptr;
9133     }
9134
9135   die->sibling = NULL;
9136   die->parent = parent;
9137   return die;
9138 }
9139
9140 /* Read a die, all of its descendents, and all of its siblings; set
9141    all of the fields of all of the dies correctly.  Arguments are as
9142    in read_die_and_children.  */
9143
9144 static struct die_info *
9145 read_die_and_siblings (const struct die_reader_specs *reader,
9146                        gdb_byte *info_ptr,
9147                        gdb_byte **new_info_ptr,
9148                        struct die_info *parent)
9149 {
9150   struct die_info *first_die, *last_sibling;
9151   gdb_byte *cur_ptr;
9152
9153   cur_ptr = info_ptr;
9154   first_die = last_sibling = NULL;
9155
9156   while (1)
9157     {
9158       struct die_info *die
9159         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9160
9161       if (die == NULL)
9162         {
9163           *new_info_ptr = cur_ptr;
9164           return first_die;
9165         }
9166
9167       if (!first_die)
9168         first_die = die;
9169       else
9170         last_sibling->sibling = die;
9171
9172       last_sibling = die;
9173     }
9174 }
9175
9176 /* Read the die from the .debug_info section buffer.  Set DIEP to
9177    point to a newly allocated die with its information, except for its
9178    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9179    whether the die has children or not.  */
9180
9181 static gdb_byte *
9182 read_full_die (const struct die_reader_specs *reader,
9183                struct die_info **diep, gdb_byte *info_ptr,
9184                int *has_children)
9185 {
9186   unsigned int abbrev_number, bytes_read, i, offset;
9187   struct abbrev_info *abbrev;
9188   struct die_info *die;
9189   struct dwarf2_cu *cu = reader->cu;
9190   bfd *abfd = reader->abfd;
9191
9192   offset = info_ptr - reader->buffer;
9193   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9194   info_ptr += bytes_read;
9195   if (!abbrev_number)
9196     {
9197       *diep = NULL;
9198       *has_children = 0;
9199       return info_ptr;
9200     }
9201
9202   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9203   if (!abbrev)
9204     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9205            abbrev_number,
9206            bfd_get_filename (abfd));
9207
9208   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9209   die->offset = offset;
9210   die->tag = abbrev->tag;
9211   die->abbrev = abbrev_number;
9212
9213   die->num_attrs = abbrev->num_attrs;
9214
9215   for (i = 0; i < abbrev->num_attrs; ++i)
9216     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9217                                abfd, info_ptr, cu);
9218
9219   *diep = die;
9220   *has_children = abbrev->has_children;
9221   return info_ptr;
9222 }
9223
9224 /* In DWARF version 2, the description of the debugging information is
9225    stored in a separate .debug_abbrev section.  Before we read any
9226    dies from a section we read in all abbreviations and install them
9227    in a hash table.  This function also sets flags in CU describing
9228    the data found in the abbrev table.  */
9229
9230 static void
9231 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9232 {
9233   bfd *abfd = cu->objfile->obfd;
9234   struct comp_unit_head *cu_header = &cu->header;
9235   gdb_byte *abbrev_ptr;
9236   struct abbrev_info *cur_abbrev;
9237   unsigned int abbrev_number, bytes_read, abbrev_name;
9238   unsigned int abbrev_form, hash_number;
9239   struct attr_abbrev *cur_attrs;
9240   unsigned int allocated_attrs;
9241
9242   /* Initialize dwarf2 abbrevs.  */
9243   obstack_init (&cu->abbrev_obstack);
9244   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9245                                       (ABBREV_HASH_SIZE
9246                                        * sizeof (struct abbrev_info *)));
9247   memset (cu->dwarf2_abbrevs, 0,
9248           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9249
9250   dwarf2_read_section (dwarf2_per_objfile->objfile,
9251                        &dwarf2_per_objfile->abbrev);
9252   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9253   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9254   abbrev_ptr += bytes_read;
9255
9256   allocated_attrs = ATTR_ALLOC_CHUNK;
9257   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9258
9259   /* Loop until we reach an abbrev number of 0.  */
9260   while (abbrev_number)
9261     {
9262       cur_abbrev = dwarf_alloc_abbrev (cu);
9263
9264       /* read in abbrev header */
9265       cur_abbrev->number = abbrev_number;
9266       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9267       abbrev_ptr += bytes_read;
9268       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9269       abbrev_ptr += 1;
9270
9271       if (cur_abbrev->tag == DW_TAG_namespace)
9272         cu->has_namespace_info = 1;
9273
9274       /* now read in declarations */
9275       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9276       abbrev_ptr += bytes_read;
9277       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9278       abbrev_ptr += bytes_read;
9279       while (abbrev_name)
9280         {
9281           if (cur_abbrev->num_attrs == allocated_attrs)
9282             {
9283               allocated_attrs += ATTR_ALLOC_CHUNK;
9284               cur_attrs
9285                 = xrealloc (cur_attrs, (allocated_attrs
9286                                         * sizeof (struct attr_abbrev)));
9287             }
9288
9289           /* Record whether this compilation unit might have
9290              inter-compilation-unit references.  If we don't know what form
9291              this attribute will have, then it might potentially be a
9292              DW_FORM_ref_addr, so we conservatively expect inter-CU
9293              references.  */
9294
9295           if (abbrev_form == DW_FORM_ref_addr
9296               || abbrev_form == DW_FORM_indirect)
9297             cu->has_form_ref_addr = 1;
9298
9299           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9300           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9301           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9302           abbrev_ptr += bytes_read;
9303           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9304           abbrev_ptr += bytes_read;
9305         }
9306
9307       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9308                                          (cur_abbrev->num_attrs
9309                                           * sizeof (struct attr_abbrev)));
9310       memcpy (cur_abbrev->attrs, cur_attrs,
9311               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9312
9313       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9314       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9315       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9316
9317       /* Get next abbreviation.
9318          Under Irix6 the abbreviations for a compilation unit are not
9319          always properly terminated with an abbrev number of 0.
9320          Exit loop if we encounter an abbreviation which we have
9321          already read (which means we are about to read the abbreviations
9322          for the next compile unit) or if the end of the abbreviation
9323          table is reached.  */
9324       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9325           >= dwarf2_per_objfile->abbrev.size)
9326         break;
9327       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9328       abbrev_ptr += bytes_read;
9329       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9330         break;
9331     }
9332
9333   xfree (cur_attrs);
9334 }
9335
9336 /* Release the memory used by the abbrev table for a compilation unit.  */
9337
9338 static void
9339 dwarf2_free_abbrev_table (void *ptr_to_cu)
9340 {
9341   struct dwarf2_cu *cu = ptr_to_cu;
9342
9343   obstack_free (&cu->abbrev_obstack, NULL);
9344   cu->dwarf2_abbrevs = NULL;
9345 }
9346
9347 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9348
9349 static struct abbrev_info *
9350 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9351 {
9352   unsigned int hash_number;
9353   struct abbrev_info *abbrev;
9354
9355   hash_number = number % ABBREV_HASH_SIZE;
9356   abbrev = cu->dwarf2_abbrevs[hash_number];
9357
9358   while (abbrev)
9359     {
9360       if (abbrev->number == number)
9361         return abbrev;
9362       else
9363         abbrev = abbrev->next;
9364     }
9365   return NULL;
9366 }
9367
9368 /* Returns nonzero if TAG represents a type that we might generate a partial
9369    symbol for.  */
9370
9371 static int
9372 is_type_tag_for_partial (int tag)
9373 {
9374   switch (tag)
9375     {
9376 #if 0
9377     /* Some types that would be reasonable to generate partial symbols for,
9378        that we don't at present.  */
9379     case DW_TAG_array_type:
9380     case DW_TAG_file_type:
9381     case DW_TAG_ptr_to_member_type:
9382     case DW_TAG_set_type:
9383     case DW_TAG_string_type:
9384     case DW_TAG_subroutine_type:
9385 #endif
9386     case DW_TAG_base_type:
9387     case DW_TAG_class_type:
9388     case DW_TAG_interface_type:
9389     case DW_TAG_enumeration_type:
9390     case DW_TAG_structure_type:
9391     case DW_TAG_subrange_type:
9392     case DW_TAG_typedef:
9393     case DW_TAG_union_type:
9394       return 1;
9395     default:
9396       return 0;
9397     }
9398 }
9399
9400 /* Load all DIEs that are interesting for partial symbols into memory.  */
9401
9402 static struct partial_die_info *
9403 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9404                    int building_psymtab, struct dwarf2_cu *cu)
9405 {
9406   struct objfile *objfile = cu->objfile;
9407   struct partial_die_info *part_die;
9408   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9409   struct abbrev_info *abbrev;
9410   unsigned int bytes_read;
9411   unsigned int load_all = 0;
9412
9413   int nesting_level = 1;
9414
9415   parent_die = NULL;
9416   last_die = NULL;
9417
9418   if (cu->per_cu && cu->per_cu->load_all_dies)
9419     load_all = 1;
9420
9421   cu->partial_dies
9422     = htab_create_alloc_ex (cu->header.length / 12,
9423                             partial_die_hash,
9424                             partial_die_eq,
9425                             NULL,
9426                             &cu->comp_unit_obstack,
9427                             hashtab_obstack_allocate,
9428                             dummy_obstack_deallocate);
9429
9430   part_die = obstack_alloc (&cu->comp_unit_obstack,
9431                             sizeof (struct partial_die_info));
9432
9433   while (1)
9434     {
9435       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9436
9437       /* A NULL abbrev means the end of a series of children.  */
9438       if (abbrev == NULL)
9439         {
9440           if (--nesting_level == 0)
9441             {
9442               /* PART_DIE was probably the last thing allocated on the
9443                  comp_unit_obstack, so we could call obstack_free
9444                  here.  We don't do that because the waste is small,
9445                  and will be cleaned up when we're done with this
9446                  compilation unit.  This way, we're also more robust
9447                  against other users of the comp_unit_obstack.  */
9448               return first_die;
9449             }
9450           info_ptr += bytes_read;
9451           last_die = parent_die;
9452           parent_die = parent_die->die_parent;
9453           continue;
9454         }
9455
9456       /* Check for template arguments.  We never save these; if
9457          they're seen, we just mark the parent, and go on our way.  */
9458       if (parent_die != NULL
9459           && cu->language == language_cplus
9460           && (abbrev->tag == DW_TAG_template_type_param
9461               || abbrev->tag == DW_TAG_template_value_param))
9462         {
9463           parent_die->has_template_arguments = 1;
9464
9465           if (!load_all)
9466             {
9467               /* We don't need a partial DIE for the template argument.  */
9468               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9469                                        cu);
9470               continue;
9471             }
9472         }
9473
9474       /* We only recurse into subprograms looking for template arguments.
9475          Skip their other children.  */
9476       if (!load_all
9477           && cu->language == language_cplus
9478           && parent_die != NULL
9479           && parent_die->tag == DW_TAG_subprogram)
9480         {
9481           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9482           continue;
9483         }
9484
9485       /* Check whether this DIE is interesting enough to save.  Normally
9486          we would not be interested in members here, but there may be
9487          later variables referencing them via DW_AT_specification (for
9488          static members).  */
9489       if (!load_all
9490           && !is_type_tag_for_partial (abbrev->tag)
9491           && abbrev->tag != DW_TAG_constant
9492           && abbrev->tag != DW_TAG_enumerator
9493           && abbrev->tag != DW_TAG_subprogram
9494           && abbrev->tag != DW_TAG_lexical_block
9495           && abbrev->tag != DW_TAG_variable
9496           && abbrev->tag != DW_TAG_namespace
9497           && abbrev->tag != DW_TAG_module
9498           && abbrev->tag != DW_TAG_member)
9499         {
9500           /* Otherwise we skip to the next sibling, if any.  */
9501           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9502           continue;
9503         }
9504
9505       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9506                                    buffer, info_ptr, cu);
9507
9508       /* This two-pass algorithm for processing partial symbols has a
9509          high cost in cache pressure.  Thus, handle some simple cases
9510          here which cover the majority of C partial symbols.  DIEs
9511          which neither have specification tags in them, nor could have
9512          specification tags elsewhere pointing at them, can simply be
9513          processed and discarded.
9514
9515          This segment is also optional; scan_partial_symbols and
9516          add_partial_symbol will handle these DIEs if we chain
9517          them in normally.  When compilers which do not emit large
9518          quantities of duplicate debug information are more common,
9519          this code can probably be removed.  */
9520
9521       /* Any complete simple types at the top level (pretty much all
9522          of them, for a language without namespaces), can be processed
9523          directly.  */
9524       if (parent_die == NULL
9525           && part_die->has_specification == 0
9526           && part_die->is_declaration == 0
9527           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9528               || part_die->tag == DW_TAG_base_type
9529               || part_die->tag == DW_TAG_subrange_type))
9530         {
9531           if (building_psymtab && part_die->name != NULL)
9532             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9533                                  VAR_DOMAIN, LOC_TYPEDEF,
9534                                  &objfile->static_psymbols,
9535                                  0, (CORE_ADDR) 0, cu->language, objfile);
9536           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9537           continue;
9538         }
9539
9540       /* The exception for DW_TAG_typedef with has_children above is
9541          a workaround of GCC PR debug/47510.  In the case of this complaint
9542          type_name_no_tag_or_error will error on such types later.
9543
9544          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9545          it could not find the child DIEs referenced later, this is checked
9546          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9547
9548       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9549         complaint (&symfile_complaints,
9550                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9551                      "- DIE at 0x%x [in module %s]"),
9552                    part_die->offset, objfile->name);
9553
9554       /* If we're at the second level, and we're an enumerator, and
9555          our parent has no specification (meaning possibly lives in a
9556          namespace elsewhere), then we can add the partial symbol now
9557          instead of queueing it.  */
9558       if (part_die->tag == DW_TAG_enumerator
9559           && parent_die != NULL
9560           && parent_die->die_parent == NULL
9561           && parent_die->tag == DW_TAG_enumeration_type
9562           && parent_die->has_specification == 0)
9563         {
9564           if (part_die->name == NULL)
9565             complaint (&symfile_complaints,
9566                        _("malformed enumerator DIE ignored"));
9567           else if (building_psymtab)
9568             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9569                                  VAR_DOMAIN, LOC_CONST,
9570                                  (cu->language == language_cplus
9571                                   || cu->language == language_java)
9572                                  ? &objfile->global_psymbols
9573                                  : &objfile->static_psymbols,
9574                                  0, (CORE_ADDR) 0, cu->language, objfile);
9575
9576           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9577           continue;
9578         }
9579
9580       /* We'll save this DIE so link it in.  */
9581       part_die->die_parent = parent_die;
9582       part_die->die_sibling = NULL;
9583       part_die->die_child = NULL;
9584
9585       if (last_die && last_die == parent_die)
9586         last_die->die_child = part_die;
9587       else if (last_die)
9588         last_die->die_sibling = part_die;
9589
9590       last_die = part_die;
9591
9592       if (first_die == NULL)
9593         first_die = part_die;
9594
9595       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9596          find interesting need to be in the hash table, because we
9597          also have the parent/sibling/child chains; only those that we
9598          might refer to by offset later during partial symbol reading.
9599
9600          For now this means things that might have be the target of a
9601          DW_AT_specification, DW_AT_abstract_origin, or
9602          DW_AT_extension.  DW_AT_extension will refer only to
9603          namespaces; DW_AT_abstract_origin refers to functions (and
9604          many things under the function DIE, but we do not recurse
9605          into function DIEs during partial symbol reading) and
9606          possibly variables as well; DW_AT_specification refers to
9607          declarations.  Declarations ought to have the DW_AT_declaration
9608          flag.  It happens that GCC forgets to put it in sometimes, but
9609          only for functions, not for types.
9610
9611          Adding more things than necessary to the hash table is harmless
9612          except for the performance cost.  Adding too few will result in
9613          wasted time in find_partial_die, when we reread the compilation
9614          unit with load_all_dies set.  */
9615
9616       if (load_all
9617           || abbrev->tag == DW_TAG_constant
9618           || abbrev->tag == DW_TAG_subprogram
9619           || abbrev->tag == DW_TAG_variable
9620           || abbrev->tag == DW_TAG_namespace
9621           || part_die->is_declaration)
9622         {
9623           void **slot;
9624
9625           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9626                                            part_die->offset, INSERT);
9627           *slot = part_die;
9628         }
9629
9630       part_die = obstack_alloc (&cu->comp_unit_obstack,
9631                                 sizeof (struct partial_die_info));
9632
9633       /* For some DIEs we want to follow their children (if any).  For C
9634          we have no reason to follow the children of structures; for other
9635          languages we have to, so that we can get at method physnames
9636          to infer fully qualified class names, for DW_AT_specification,
9637          and for C++ template arguments.  For C++, we also look one level
9638          inside functions to find template arguments (if the name of the
9639          function does not already contain the template arguments).
9640
9641          For Ada, we need to scan the children of subprograms and lexical
9642          blocks as well because Ada allows the definition of nested
9643          entities that could be interesting for the debugger, such as
9644          nested subprograms for instance.  */
9645       if (last_die->has_children
9646           && (load_all
9647               || last_die->tag == DW_TAG_namespace
9648               || last_die->tag == DW_TAG_module
9649               || last_die->tag == DW_TAG_enumeration_type
9650               || (cu->language == language_cplus
9651                   && last_die->tag == DW_TAG_subprogram
9652                   && (last_die->name == NULL
9653                       || strchr (last_die->name, '<') == NULL))
9654               || (cu->language != language_c
9655                   && (last_die->tag == DW_TAG_class_type
9656                       || last_die->tag == DW_TAG_interface_type
9657                       || last_die->tag == DW_TAG_structure_type
9658                       || last_die->tag == DW_TAG_union_type))
9659               || (cu->language == language_ada
9660                   && (last_die->tag == DW_TAG_subprogram
9661                       || last_die->tag == DW_TAG_lexical_block))))
9662         {
9663           nesting_level++;
9664           parent_die = last_die;
9665           continue;
9666         }
9667
9668       /* Otherwise we skip to the next sibling, if any.  */
9669       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9670
9671       /* Back to the top, do it again.  */
9672     }
9673 }
9674
9675 /* Read a minimal amount of information into the minimal die structure.  */
9676
9677 static gdb_byte *
9678 read_partial_die (struct partial_die_info *part_die,
9679                   struct abbrev_info *abbrev,
9680                   unsigned int abbrev_len, bfd *abfd,
9681                   gdb_byte *buffer, gdb_byte *info_ptr,
9682                   struct dwarf2_cu *cu)
9683 {
9684   struct objfile *objfile = cu->objfile;
9685   unsigned int i;
9686   struct attribute attr;
9687   int has_low_pc_attr = 0;
9688   int has_high_pc_attr = 0;
9689
9690   memset (part_die, 0, sizeof (struct partial_die_info));
9691
9692   part_die->offset = info_ptr - buffer;
9693
9694   info_ptr += abbrev_len;
9695
9696   if (abbrev == NULL)
9697     return info_ptr;
9698
9699   part_die->tag = abbrev->tag;
9700   part_die->has_children = abbrev->has_children;
9701
9702   for (i = 0; i < abbrev->num_attrs; ++i)
9703     {
9704       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9705
9706       /* Store the data if it is of an attribute we want to keep in a
9707          partial symbol table.  */
9708       switch (attr.name)
9709         {
9710         case DW_AT_name:
9711           switch (part_die->tag)
9712             {
9713             case DW_TAG_compile_unit:
9714             case DW_TAG_type_unit:
9715               /* Compilation units have a DW_AT_name that is a filename, not
9716                  a source language identifier.  */
9717             case DW_TAG_enumeration_type:
9718             case DW_TAG_enumerator:
9719               /* These tags always have simple identifiers already; no need
9720                  to canonicalize them.  */
9721               part_die->name = DW_STRING (&attr);
9722               break;
9723             default:
9724               part_die->name
9725                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9726                                             &objfile->objfile_obstack);
9727               break;
9728             }
9729           break;
9730         case DW_AT_linkage_name:
9731         case DW_AT_MIPS_linkage_name:
9732           /* Note that both forms of linkage name might appear.  We
9733              assume they will be the same, and we only store the last
9734              one we see.  */
9735           if (cu->language == language_ada)
9736             part_die->name = DW_STRING (&attr);
9737           part_die->linkage_name = DW_STRING (&attr);
9738           break;
9739         case DW_AT_low_pc:
9740           has_low_pc_attr = 1;
9741           part_die->lowpc = DW_ADDR (&attr);
9742           break;
9743         case DW_AT_high_pc:
9744           has_high_pc_attr = 1;
9745           part_die->highpc = DW_ADDR (&attr);
9746           break;
9747         case DW_AT_location:
9748           /* Support the .debug_loc offsets.  */
9749           if (attr_form_is_block (&attr))
9750             {
9751                part_die->locdesc = DW_BLOCK (&attr);
9752             }
9753           else if (attr_form_is_section_offset (&attr))
9754             {
9755               dwarf2_complex_location_expr_complaint ();
9756             }
9757           else
9758             {
9759               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9760                                                      "partial symbol information");
9761             }
9762           break;
9763         case DW_AT_external:
9764           part_die->is_external = DW_UNSND (&attr);
9765           break;
9766         case DW_AT_declaration:
9767           part_die->is_declaration = DW_UNSND (&attr);
9768           break;
9769         case DW_AT_type:
9770           part_die->has_type = 1;
9771           break;
9772         case DW_AT_abstract_origin:
9773         case DW_AT_specification:
9774         case DW_AT_extension:
9775           part_die->has_specification = 1;
9776           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9777           break;
9778         case DW_AT_sibling:
9779           /* Ignore absolute siblings, they might point outside of
9780              the current compile unit.  */
9781           if (attr.form == DW_FORM_ref_addr)
9782             complaint (&symfile_complaints,
9783                        _("ignoring absolute DW_AT_sibling"));
9784           else
9785             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9786           break;
9787         case DW_AT_byte_size:
9788           part_die->has_byte_size = 1;
9789           break;
9790         case DW_AT_calling_convention:
9791           /* DWARF doesn't provide a way to identify a program's source-level
9792              entry point.  DW_AT_calling_convention attributes are only meant
9793              to describe functions' calling conventions.
9794
9795              However, because it's a necessary piece of information in
9796              Fortran, and because DW_CC_program is the only piece of debugging
9797              information whose definition refers to a 'main program' at all,
9798              several compilers have begun marking Fortran main programs with
9799              DW_CC_program --- even when those functions use the standard
9800              calling conventions.
9801
9802              So until DWARF specifies a way to provide this information and
9803              compilers pick up the new representation, we'll support this
9804              practice.  */
9805           if (DW_UNSND (&attr) == DW_CC_program
9806               && cu->language == language_fortran)
9807             {
9808               set_main_name (part_die->name);
9809
9810               /* As this DIE has a static linkage the name would be difficult
9811                  to look up later.  */
9812               language_of_main = language_fortran;
9813             }
9814           break;
9815         default:
9816           break;
9817         }
9818     }
9819
9820   if (has_low_pc_attr && has_high_pc_attr)
9821     {
9822       /* When using the GNU linker, .gnu.linkonce. sections are used to
9823          eliminate duplicate copies of functions and vtables and such.
9824          The linker will arbitrarily choose one and discard the others.
9825          The AT_*_pc values for such functions refer to local labels in
9826          these sections.  If the section from that file was discarded, the
9827          labels are not in the output, so the relocs get a value of 0.
9828          If this is a discarded function, mark the pc bounds as invalid,
9829          so that GDB will ignore it.  */
9830       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9831         {
9832           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9833
9834           complaint (&symfile_complaints,
9835                      _("DW_AT_low_pc %s is zero "
9836                        "for DIE at 0x%x [in module %s]"),
9837                      paddress (gdbarch, part_die->lowpc),
9838                      part_die->offset, objfile->name);
9839         }
9840       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9841       else if (part_die->lowpc >= part_die->highpc)
9842         {
9843           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9844
9845           complaint (&symfile_complaints,
9846                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9847                        "for DIE at 0x%x [in module %s]"),
9848                      paddress (gdbarch, part_die->lowpc),
9849                      paddress (gdbarch, part_die->highpc),
9850                      part_die->offset, objfile->name);
9851         }
9852       else
9853         part_die->has_pc_info = 1;
9854     }
9855
9856   return info_ptr;
9857 }
9858
9859 /* Find a cached partial DIE at OFFSET in CU.  */
9860
9861 static struct partial_die_info *
9862 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9863 {
9864   struct partial_die_info *lookup_die = NULL;
9865   struct partial_die_info part_die;
9866
9867   part_die.offset = offset;
9868   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9869
9870   return lookup_die;
9871 }
9872
9873 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9874    except in the case of .debug_types DIEs which do not reference
9875    outside their CU (they do however referencing other types via
9876    DW_FORM_ref_sig8).  */
9877
9878 static struct partial_die_info *
9879 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9880 {
9881   struct objfile *objfile = cu->objfile;
9882   struct dwarf2_per_cu_data *per_cu = NULL;
9883   struct partial_die_info *pd = NULL;
9884
9885   if (cu->per_cu->debug_types_section)
9886     {
9887       pd = find_partial_die_in_comp_unit (offset, cu);
9888       if (pd != NULL)
9889         return pd;
9890       goto not_found;
9891     }
9892
9893   if (offset_in_cu_p (&cu->header, offset))
9894     {
9895       pd = find_partial_die_in_comp_unit (offset, cu);
9896       if (pd != NULL)
9897         return pd;
9898     }
9899
9900   per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9901
9902   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9903     load_partial_comp_unit (per_cu);
9904
9905   per_cu->cu->last_used = 0;
9906   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9907
9908   if (pd == NULL && per_cu->load_all_dies == 0)
9909     {
9910       struct cleanup *back_to;
9911       struct partial_die_info comp_unit_die;
9912       struct abbrev_info *abbrev;
9913       unsigned int bytes_read;
9914       char *info_ptr;
9915
9916       per_cu->load_all_dies = 1;
9917
9918       /* Re-read the DIEs.  */
9919       back_to = make_cleanup (null_cleanup, 0);
9920       if (per_cu->cu->dwarf2_abbrevs == NULL)
9921         {
9922           dwarf2_read_abbrevs (per_cu->cu);
9923           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9924         }
9925       info_ptr = (dwarf2_per_objfile->info.buffer
9926                   + per_cu->cu->header.offset
9927                   + per_cu->cu->header.first_die_offset);
9928       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9929       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9930                                    objfile->obfd,
9931                                    dwarf2_per_objfile->info.buffer, info_ptr,
9932                                    per_cu->cu);
9933       if (comp_unit_die.has_children)
9934         load_partial_dies (objfile->obfd,
9935                            dwarf2_per_objfile->info.buffer, info_ptr,
9936                            0, per_cu->cu);
9937       do_cleanups (back_to);
9938
9939       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9940     }
9941
9942  not_found:
9943
9944   if (pd == NULL)
9945     internal_error (__FILE__, __LINE__,
9946                     _("could not find partial DIE 0x%x "
9947                       "in cache [from module %s]\n"),
9948                     offset, bfd_get_filename (objfile->obfd));
9949   return pd;
9950 }
9951
9952 /* See if we can figure out if the class lives in a namespace.  We do
9953    this by looking for a member function; its demangled name will
9954    contain namespace info, if there is any.  */
9955
9956 static void
9957 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9958                                   struct dwarf2_cu *cu)
9959 {
9960   /* NOTE: carlton/2003-10-07: Getting the info this way changes
9961      what template types look like, because the demangler
9962      frequently doesn't give the same name as the debug info.  We
9963      could fix this by only using the demangled name to get the
9964      prefix (but see comment in read_structure_type).  */
9965
9966   struct partial_die_info *real_pdi;
9967   struct partial_die_info *child_pdi;
9968
9969   /* If this DIE (this DIE's specification, if any) has a parent, then
9970      we should not do this.  We'll prepend the parent's fully qualified
9971      name when we create the partial symbol.  */
9972
9973   real_pdi = struct_pdi;
9974   while (real_pdi->has_specification)
9975     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9976
9977   if (real_pdi->die_parent != NULL)
9978     return;
9979
9980   for (child_pdi = struct_pdi->die_child;
9981        child_pdi != NULL;
9982        child_pdi = child_pdi->die_sibling)
9983     {
9984       if (child_pdi->tag == DW_TAG_subprogram
9985           && child_pdi->linkage_name != NULL)
9986         {
9987           char *actual_class_name
9988             = language_class_name_from_physname (cu->language_defn,
9989                                                  child_pdi->linkage_name);
9990           if (actual_class_name != NULL)
9991             {
9992               struct_pdi->name
9993                 = obsavestring (actual_class_name,
9994                                 strlen (actual_class_name),
9995                                 &cu->objfile->objfile_obstack);
9996               xfree (actual_class_name);
9997             }
9998           break;
9999         }
10000     }
10001 }
10002
10003 /* Adjust PART_DIE before generating a symbol for it.  This function
10004    may set the is_external flag or change the DIE's name.  */
10005
10006 static void
10007 fixup_partial_die (struct partial_die_info *part_die,
10008                    struct dwarf2_cu *cu)
10009 {
10010   /* Once we've fixed up a die, there's no point in doing so again.
10011      This also avoids a memory leak if we were to call
10012      guess_partial_die_structure_name multiple times.  */
10013   if (part_die->fixup_called)
10014     return;
10015
10016   /* If we found a reference attribute and the DIE has no name, try
10017      to find a name in the referred to DIE.  */
10018
10019   if (part_die->name == NULL && part_die->has_specification)
10020     {
10021       struct partial_die_info *spec_die;
10022
10023       spec_die = find_partial_die (part_die->spec_offset, cu);
10024
10025       fixup_partial_die (spec_die, cu);
10026
10027       if (spec_die->name)
10028         {
10029           part_die->name = spec_die->name;
10030
10031           /* Copy DW_AT_external attribute if it is set.  */
10032           if (spec_die->is_external)
10033             part_die->is_external = spec_die->is_external;
10034         }
10035     }
10036
10037   /* Set default names for some unnamed DIEs.  */
10038
10039   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10040     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10041
10042   /* If there is no parent die to provide a namespace, and there are
10043      children, see if we can determine the namespace from their linkage
10044      name.
10045      NOTE: We need to do this even if cu->has_namespace_info != 0.
10046      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
10047   if (cu->language == language_cplus
10048       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10049       && part_die->die_parent == NULL
10050       && part_die->has_children
10051       && (part_die->tag == DW_TAG_class_type
10052           || part_die->tag == DW_TAG_structure_type
10053           || part_die->tag == DW_TAG_union_type))
10054     guess_partial_die_structure_name (part_die, cu);
10055
10056   /* GCC might emit a nameless struct or union that has a linkage
10057      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10058   if (part_die->name == NULL
10059       && (part_die->tag == DW_TAG_class_type
10060           || part_die->tag == DW_TAG_interface_type
10061           || part_die->tag == DW_TAG_structure_type
10062           || part_die->tag == DW_TAG_union_type)
10063       && part_die->linkage_name != NULL)
10064     {
10065       char *demangled;
10066
10067       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10068       if (demangled)
10069         {
10070           const char *base;
10071
10072           /* Strip any leading namespaces/classes, keep only the base name.
10073              DW_AT_name for named DIEs does not contain the prefixes.  */
10074           base = strrchr (demangled, ':');
10075           if (base && base > demangled && base[-1] == ':')
10076             base++;
10077           else
10078             base = demangled;
10079
10080           part_die->name = obsavestring (base, strlen (base),
10081                                          &cu->objfile->objfile_obstack);
10082           xfree (demangled);
10083         }
10084     }
10085
10086   part_die->fixup_called = 1;
10087 }
10088
10089 /* Read an attribute value described by an attribute form.  */
10090
10091 static gdb_byte *
10092 read_attribute_value (struct attribute *attr, unsigned form,
10093                       bfd *abfd, gdb_byte *info_ptr,
10094                       struct dwarf2_cu *cu)
10095 {
10096   struct comp_unit_head *cu_header = &cu->header;
10097   unsigned int bytes_read;
10098   struct dwarf_block *blk;
10099
10100   attr->form = form;
10101   switch (form)
10102     {
10103     case DW_FORM_ref_addr:
10104       if (cu->header.version == 2)
10105         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10106       else
10107         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10108                                       &cu->header, &bytes_read);
10109       info_ptr += bytes_read;
10110       break;
10111     case DW_FORM_addr:
10112       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10113       info_ptr += bytes_read;
10114       break;
10115     case DW_FORM_block2:
10116       blk = dwarf_alloc_block (cu);
10117       blk->size = read_2_bytes (abfd, info_ptr);
10118       info_ptr += 2;
10119       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10120       info_ptr += blk->size;
10121       DW_BLOCK (attr) = blk;
10122       break;
10123     case DW_FORM_block4:
10124       blk = dwarf_alloc_block (cu);
10125       blk->size = read_4_bytes (abfd, info_ptr);
10126       info_ptr += 4;
10127       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10128       info_ptr += blk->size;
10129       DW_BLOCK (attr) = blk;
10130       break;
10131     case DW_FORM_data2:
10132       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10133       info_ptr += 2;
10134       break;
10135     case DW_FORM_data4:
10136       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10137       info_ptr += 4;
10138       break;
10139     case DW_FORM_data8:
10140       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10141       info_ptr += 8;
10142       break;
10143     case DW_FORM_sec_offset:
10144       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10145       info_ptr += bytes_read;
10146       break;
10147     case DW_FORM_string:
10148       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10149       DW_STRING_IS_CANONICAL (attr) = 0;
10150       info_ptr += bytes_read;
10151       break;
10152     case DW_FORM_strp:
10153       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10154                                                &bytes_read);
10155       DW_STRING_IS_CANONICAL (attr) = 0;
10156       info_ptr += bytes_read;
10157       break;
10158     case DW_FORM_exprloc:
10159     case DW_FORM_block:
10160       blk = dwarf_alloc_block (cu);
10161       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10162       info_ptr += bytes_read;
10163       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10164       info_ptr += blk->size;
10165       DW_BLOCK (attr) = blk;
10166       break;
10167     case DW_FORM_block1:
10168       blk = dwarf_alloc_block (cu);
10169       blk->size = read_1_byte (abfd, info_ptr);
10170       info_ptr += 1;
10171       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10172       info_ptr += blk->size;
10173       DW_BLOCK (attr) = blk;
10174       break;
10175     case DW_FORM_data1:
10176       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10177       info_ptr += 1;
10178       break;
10179     case DW_FORM_flag:
10180       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10181       info_ptr += 1;
10182       break;
10183     case DW_FORM_flag_present:
10184       DW_UNSND (attr) = 1;
10185       break;
10186     case DW_FORM_sdata:
10187       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10188       info_ptr += bytes_read;
10189       break;
10190     case DW_FORM_udata:
10191       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10192       info_ptr += bytes_read;
10193       break;
10194     case DW_FORM_ref1:
10195       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10196       info_ptr += 1;
10197       break;
10198     case DW_FORM_ref2:
10199       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10200       info_ptr += 2;
10201       break;
10202     case DW_FORM_ref4:
10203       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10204       info_ptr += 4;
10205       break;
10206     case DW_FORM_ref8:
10207       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10208       info_ptr += 8;
10209       break;
10210     case DW_FORM_ref_sig8:
10211       /* Convert the signature to something we can record in DW_UNSND
10212          for later lookup.
10213          NOTE: This is NULL if the type wasn't found.  */
10214       DW_SIGNATURED_TYPE (attr) =
10215         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10216       info_ptr += 8;
10217       break;
10218     case DW_FORM_ref_udata:
10219       DW_ADDR (attr) = (cu->header.offset
10220                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10221       info_ptr += bytes_read;
10222       break;
10223     case DW_FORM_indirect:
10224       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10225       info_ptr += bytes_read;
10226       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10227       break;
10228     default:
10229       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10230              dwarf_form_name (form),
10231              bfd_get_filename (abfd));
10232     }
10233
10234   /* We have seen instances where the compiler tried to emit a byte
10235      size attribute of -1 which ended up being encoded as an unsigned
10236      0xffffffff.  Although 0xffffffff is technically a valid size value,
10237      an object of this size seems pretty unlikely so we can relatively
10238      safely treat these cases as if the size attribute was invalid and
10239      treat them as zero by default.  */
10240   if (attr->name == DW_AT_byte_size
10241       && form == DW_FORM_data4
10242       && DW_UNSND (attr) >= 0xffffffff)
10243     {
10244       complaint
10245         (&symfile_complaints,
10246          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10247          hex_string (DW_UNSND (attr)));
10248       DW_UNSND (attr) = 0;
10249     }
10250
10251   return info_ptr;
10252 }
10253
10254 /* Read an attribute described by an abbreviated attribute.  */
10255
10256 static gdb_byte *
10257 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10258                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10259 {
10260   attr->name = abbrev->name;
10261   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10262 }
10263
10264 /* Read dwarf information from a buffer.  */
10265
10266 static unsigned int
10267 read_1_byte (bfd *abfd, gdb_byte *buf)
10268 {
10269   return bfd_get_8 (abfd, buf);
10270 }
10271
10272 static int
10273 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10274 {
10275   return bfd_get_signed_8 (abfd, buf);
10276 }
10277
10278 static unsigned int
10279 read_2_bytes (bfd *abfd, gdb_byte *buf)
10280 {
10281   return bfd_get_16 (abfd, buf);
10282 }
10283
10284 static int
10285 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10286 {
10287   return bfd_get_signed_16 (abfd, buf);
10288 }
10289
10290 static unsigned int
10291 read_4_bytes (bfd *abfd, gdb_byte *buf)
10292 {
10293   return bfd_get_32 (abfd, buf);
10294 }
10295
10296 static int
10297 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10298 {
10299   return bfd_get_signed_32 (abfd, buf);
10300 }
10301
10302 static ULONGEST
10303 read_8_bytes (bfd *abfd, gdb_byte *buf)
10304 {
10305   return bfd_get_64 (abfd, buf);
10306 }
10307
10308 static CORE_ADDR
10309 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10310               unsigned int *bytes_read)
10311 {
10312   struct comp_unit_head *cu_header = &cu->header;
10313   CORE_ADDR retval = 0;
10314
10315   if (cu_header->signed_addr_p)
10316     {
10317       switch (cu_header->addr_size)
10318         {
10319         case 2:
10320           retval = bfd_get_signed_16 (abfd, buf);
10321           break;
10322         case 4:
10323           retval = bfd_get_signed_32 (abfd, buf);
10324           break;
10325         case 8:
10326           retval = bfd_get_signed_64 (abfd, buf);
10327           break;
10328         default:
10329           internal_error (__FILE__, __LINE__,
10330                           _("read_address: bad switch, signed [in module %s]"),
10331                           bfd_get_filename (abfd));
10332         }
10333     }
10334   else
10335     {
10336       switch (cu_header->addr_size)
10337         {
10338         case 2:
10339           retval = bfd_get_16 (abfd, buf);
10340           break;
10341         case 4:
10342           retval = bfd_get_32 (abfd, buf);
10343           break;
10344         case 8:
10345           retval = bfd_get_64 (abfd, buf);
10346           break;
10347         default:
10348           internal_error (__FILE__, __LINE__,
10349                           _("read_address: bad switch, "
10350                             "unsigned [in module %s]"),
10351                           bfd_get_filename (abfd));
10352         }
10353     }
10354
10355   *bytes_read = cu_header->addr_size;
10356   return retval;
10357 }
10358
10359 /* Read the initial length from a section.  The (draft) DWARF 3
10360    specification allows the initial length to take up either 4 bytes
10361    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10362    bytes describe the length and all offsets will be 8 bytes in length
10363    instead of 4.
10364
10365    An older, non-standard 64-bit format is also handled by this
10366    function.  The older format in question stores the initial length
10367    as an 8-byte quantity without an escape value.  Lengths greater
10368    than 2^32 aren't very common which means that the initial 4 bytes
10369    is almost always zero.  Since a length value of zero doesn't make
10370    sense for the 32-bit format, this initial zero can be considered to
10371    be an escape value which indicates the presence of the older 64-bit
10372    format.  As written, the code can't detect (old format) lengths
10373    greater than 4GB.  If it becomes necessary to handle lengths
10374    somewhat larger than 4GB, we could allow other small values (such
10375    as the non-sensical values of 1, 2, and 3) to also be used as
10376    escape values indicating the presence of the old format.
10377
10378    The value returned via bytes_read should be used to increment the
10379    relevant pointer after calling read_initial_length().
10380
10381    [ Note:  read_initial_length() and read_offset() are based on the
10382      document entitled "DWARF Debugging Information Format", revision
10383      3, draft 8, dated November 19, 2001.  This document was obtained
10384      from:
10385
10386         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10387
10388      This document is only a draft and is subject to change.  (So beware.)
10389
10390      Details regarding the older, non-standard 64-bit format were
10391      determined empirically by examining 64-bit ELF files produced by
10392      the SGI toolchain on an IRIX 6.5 machine.
10393
10394      - Kevin, July 16, 2002
10395    ] */
10396
10397 static LONGEST
10398 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10399 {
10400   LONGEST length = bfd_get_32 (abfd, buf);
10401
10402   if (length == 0xffffffff)
10403     {
10404       length = bfd_get_64 (abfd, buf + 4);
10405       *bytes_read = 12;
10406     }
10407   else if (length == 0)
10408     {
10409       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10410       length = bfd_get_64 (abfd, buf);
10411       *bytes_read = 8;
10412     }
10413   else
10414     {
10415       *bytes_read = 4;
10416     }
10417
10418   return length;
10419 }
10420
10421 /* Cover function for read_initial_length.
10422    Returns the length of the object at BUF, and stores the size of the
10423    initial length in *BYTES_READ and stores the size that offsets will be in
10424    *OFFSET_SIZE.
10425    If the initial length size is not equivalent to that specified in
10426    CU_HEADER then issue a complaint.
10427    This is useful when reading non-comp-unit headers.  */
10428
10429 static LONGEST
10430 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10431                                         const struct comp_unit_head *cu_header,
10432                                         unsigned int *bytes_read,
10433                                         unsigned int *offset_size)
10434 {
10435   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10436
10437   gdb_assert (cu_header->initial_length_size == 4
10438               || cu_header->initial_length_size == 8
10439               || cu_header->initial_length_size == 12);
10440
10441   if (cu_header->initial_length_size != *bytes_read)
10442     complaint (&symfile_complaints,
10443                _("intermixed 32-bit and 64-bit DWARF sections"));
10444
10445   *offset_size = (*bytes_read == 4) ? 4 : 8;
10446   return length;
10447 }
10448
10449 /* Read an offset from the data stream.  The size of the offset is
10450    given by cu_header->offset_size.  */
10451
10452 static LONGEST
10453 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10454              unsigned int *bytes_read)
10455 {
10456   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10457
10458   *bytes_read = cu_header->offset_size;
10459   return offset;
10460 }
10461
10462 /* Read an offset from the data stream.  */
10463
10464 static LONGEST
10465 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10466 {
10467   LONGEST retval = 0;
10468
10469   switch (offset_size)
10470     {
10471     case 4:
10472       retval = bfd_get_32 (abfd, buf);
10473       break;
10474     case 8:
10475       retval = bfd_get_64 (abfd, buf);
10476       break;
10477     default:
10478       internal_error (__FILE__, __LINE__,
10479                       _("read_offset_1: bad switch [in module %s]"),
10480                       bfd_get_filename (abfd));
10481     }
10482
10483   return retval;
10484 }
10485
10486 static gdb_byte *
10487 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10488 {
10489   /* If the size of a host char is 8 bits, we can return a pointer
10490      to the buffer, otherwise we have to copy the data to a buffer
10491      allocated on the temporary obstack.  */
10492   gdb_assert (HOST_CHAR_BIT == 8);
10493   return buf;
10494 }
10495
10496 static char *
10497 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10498 {
10499   /* If the size of a host char is 8 bits, we can return a pointer
10500      to the string, otherwise we have to copy the string to a buffer
10501      allocated on the temporary obstack.  */
10502   gdb_assert (HOST_CHAR_BIT == 8);
10503   if (*buf == '\0')
10504     {
10505       *bytes_read_ptr = 1;
10506       return NULL;
10507     }
10508   *bytes_read_ptr = strlen ((char *) buf) + 1;
10509   return (char *) buf;
10510 }
10511
10512 static char *
10513 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10514 {
10515   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10516   if (dwarf2_per_objfile->str.buffer == NULL)
10517     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10518            bfd_get_filename (abfd));
10519   if (str_offset >= dwarf2_per_objfile->str.size)
10520     error (_("DW_FORM_strp pointing outside of "
10521              ".debug_str section [in module %s]"),
10522            bfd_get_filename (abfd));
10523   gdb_assert (HOST_CHAR_BIT == 8);
10524   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10525     return NULL;
10526   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10527 }
10528
10529 static char *
10530 read_indirect_string (bfd *abfd, gdb_byte *buf,
10531                       const struct comp_unit_head *cu_header,
10532                       unsigned int *bytes_read_ptr)
10533 {
10534   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10535
10536   return read_indirect_string_at_offset (abfd, str_offset);
10537 }
10538
10539 static unsigned long
10540 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10541 {
10542   unsigned long result;
10543   unsigned int num_read;
10544   int i, shift;
10545   unsigned char byte;
10546
10547   result = 0;
10548   shift = 0;
10549   num_read = 0;
10550   i = 0;
10551   while (1)
10552     {
10553       byte = bfd_get_8 (abfd, buf);
10554       buf++;
10555       num_read++;
10556       result |= ((unsigned long)(byte & 127) << shift);
10557       if ((byte & 128) == 0)
10558         {
10559           break;
10560         }
10561       shift += 7;
10562     }
10563   *bytes_read_ptr = num_read;
10564   return result;
10565 }
10566
10567 static long
10568 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10569 {
10570   long result;
10571   int i, shift, num_read;
10572   unsigned char byte;
10573
10574   result = 0;
10575   shift = 0;
10576   num_read = 0;
10577   i = 0;
10578   while (1)
10579     {
10580       byte = bfd_get_8 (abfd, buf);
10581       buf++;
10582       num_read++;
10583       result |= ((long)(byte & 127) << shift);
10584       shift += 7;
10585       if ((byte & 128) == 0)
10586         {
10587           break;
10588         }
10589     }
10590   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10591     result |= -(((long)1) << shift);
10592   *bytes_read_ptr = num_read;
10593   return result;
10594 }
10595
10596 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10597
10598 static gdb_byte *
10599 skip_leb128 (bfd *abfd, gdb_byte *buf)
10600 {
10601   int byte;
10602
10603   while (1)
10604     {
10605       byte = bfd_get_8 (abfd, buf);
10606       buf++;
10607       if ((byte & 128) == 0)
10608         return buf;
10609     }
10610 }
10611
10612 static void
10613 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10614 {
10615   switch (lang)
10616     {
10617     case DW_LANG_C89:
10618     case DW_LANG_C99:
10619     case DW_LANG_C:
10620       cu->language = language_c;
10621       break;
10622     case DW_LANG_C_plus_plus:
10623       cu->language = language_cplus;
10624       break;
10625     case DW_LANG_D:
10626       cu->language = language_d;
10627       break;
10628     case DW_LANG_Fortran77:
10629     case DW_LANG_Fortran90:
10630     case DW_LANG_Fortran95:
10631       cu->language = language_fortran;
10632       break;
10633     case DW_LANG_Mips_Assembler:
10634       cu->language = language_asm;
10635       break;
10636     case DW_LANG_Java:
10637       cu->language = language_java;
10638       break;
10639     case DW_LANG_Ada83:
10640     case DW_LANG_Ada95:
10641       cu->language = language_ada;
10642       break;
10643     case DW_LANG_Modula2:
10644       cu->language = language_m2;
10645       break;
10646     case DW_LANG_Pascal83:
10647       cu->language = language_pascal;
10648       break;
10649     case DW_LANG_ObjC:
10650       cu->language = language_objc;
10651       break;
10652     case DW_LANG_Cobol74:
10653     case DW_LANG_Cobol85:
10654     default:
10655       cu->language = language_minimal;
10656       break;
10657     }
10658   cu->language_defn = language_def (cu->language);
10659 }
10660
10661 /* Return the named attribute or NULL if not there.  */
10662
10663 static struct attribute *
10664 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10665 {
10666   unsigned int i;
10667   struct attribute *spec = NULL;
10668
10669   for (i = 0; i < die->num_attrs; ++i)
10670     {
10671       if (die->attrs[i].name == name)
10672         return &die->attrs[i];
10673       if (die->attrs[i].name == DW_AT_specification
10674           || die->attrs[i].name == DW_AT_abstract_origin)
10675         spec = &die->attrs[i];
10676     }
10677
10678   if (spec)
10679     {
10680       die = follow_die_ref (die, spec, &cu);
10681       return dwarf2_attr (die, name, cu);
10682     }
10683
10684   return NULL;
10685 }
10686
10687 /* Return the named attribute or NULL if not there,
10688    but do not follow DW_AT_specification, etc.
10689    This is for use in contexts where we're reading .debug_types dies.
10690    Following DW_AT_specification, DW_AT_abstract_origin will take us
10691    back up the chain, and we want to go down.  */
10692
10693 static struct attribute *
10694 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10695                        struct dwarf2_cu *cu)
10696 {
10697   unsigned int i;
10698
10699   for (i = 0; i < die->num_attrs; ++i)
10700     if (die->attrs[i].name == name)
10701       return &die->attrs[i];
10702
10703   return NULL;
10704 }
10705
10706 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10707    and holds a non-zero value.  This function should only be used for
10708    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10709
10710 static int
10711 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10712 {
10713   struct attribute *attr = dwarf2_attr (die, name, cu);
10714
10715   return (attr && DW_UNSND (attr));
10716 }
10717
10718 static int
10719 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10720 {
10721   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10722      which value is non-zero.  However, we have to be careful with
10723      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10724      (via dwarf2_flag_true_p) follows this attribute.  So we may
10725      end up accidently finding a declaration attribute that belongs
10726      to a different DIE referenced by the specification attribute,
10727      even though the given DIE does not have a declaration attribute.  */
10728   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10729           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10730 }
10731
10732 /* Return the die giving the specification for DIE, if there is
10733    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10734    containing the return value on output.  If there is no
10735    specification, but there is an abstract origin, that is
10736    returned.  */
10737
10738 static struct die_info *
10739 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10740 {
10741   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10742                                              *spec_cu);
10743
10744   if (spec_attr == NULL)
10745     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10746
10747   if (spec_attr == NULL)
10748     return NULL;
10749   else
10750     return follow_die_ref (die, spec_attr, spec_cu);
10751 }
10752
10753 /* Free the line_header structure *LH, and any arrays and strings it
10754    refers to.
10755    NOTE: This is also used as a "cleanup" function.  */
10756
10757 static void
10758 free_line_header (struct line_header *lh)
10759 {
10760   if (lh->standard_opcode_lengths)
10761     xfree (lh->standard_opcode_lengths);
10762
10763   /* Remember that all the lh->file_names[i].name pointers are
10764      pointers into debug_line_buffer, and don't need to be freed.  */
10765   if (lh->file_names)
10766     xfree (lh->file_names);
10767
10768   /* Similarly for the include directory names.  */
10769   if (lh->include_dirs)
10770     xfree (lh->include_dirs);
10771
10772   xfree (lh);
10773 }
10774
10775 /* Add an entry to LH's include directory table.  */
10776
10777 static void
10778 add_include_dir (struct line_header *lh, char *include_dir)
10779 {
10780   /* Grow the array if necessary.  */
10781   if (lh->include_dirs_size == 0)
10782     {
10783       lh->include_dirs_size = 1; /* for testing */
10784       lh->include_dirs = xmalloc (lh->include_dirs_size
10785                                   * sizeof (*lh->include_dirs));
10786     }
10787   else if (lh->num_include_dirs >= lh->include_dirs_size)
10788     {
10789       lh->include_dirs_size *= 2;
10790       lh->include_dirs = xrealloc (lh->include_dirs,
10791                                    (lh->include_dirs_size
10792                                     * sizeof (*lh->include_dirs)));
10793     }
10794
10795   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10796 }
10797
10798 /* Add an entry to LH's file name table.  */
10799
10800 static void
10801 add_file_name (struct line_header *lh,
10802                char *name,
10803                unsigned int dir_index,
10804                unsigned int mod_time,
10805                unsigned int length)
10806 {
10807   struct file_entry *fe;
10808
10809   /* Grow the array if necessary.  */
10810   if (lh->file_names_size == 0)
10811     {
10812       lh->file_names_size = 1; /* for testing */
10813       lh->file_names = xmalloc (lh->file_names_size
10814                                 * sizeof (*lh->file_names));
10815     }
10816   else if (lh->num_file_names >= lh->file_names_size)
10817     {
10818       lh->file_names_size *= 2;
10819       lh->file_names = xrealloc (lh->file_names,
10820                                  (lh->file_names_size
10821                                   * sizeof (*lh->file_names)));
10822     }
10823
10824   fe = &lh->file_names[lh->num_file_names++];
10825   fe->name = name;
10826   fe->dir_index = dir_index;
10827   fe->mod_time = mod_time;
10828   fe->length = length;
10829   fe->included_p = 0;
10830   fe->symtab = NULL;
10831 }
10832
10833 /* Read the statement program header starting at OFFSET in
10834    .debug_line, according to the endianness of ABFD.  Return a pointer
10835    to a struct line_header, allocated using xmalloc.
10836
10837    NOTE: the strings in the include directory and file name tables of
10838    the returned object point into debug_line_buffer, and must not be
10839    freed.  */
10840
10841 static struct line_header *
10842 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10843                           struct dwarf2_cu *cu)
10844 {
10845   struct cleanup *back_to;
10846   struct line_header *lh;
10847   gdb_byte *line_ptr;
10848   unsigned int bytes_read, offset_size;
10849   int i;
10850   char *cur_dir, *cur_file;
10851
10852   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10853   if (dwarf2_per_objfile->line.buffer == NULL)
10854     {
10855       complaint (&symfile_complaints, _("missing .debug_line section"));
10856       return 0;
10857     }
10858
10859   /* Make sure that at least there's room for the total_length field.
10860      That could be 12 bytes long, but we're just going to fudge that.  */
10861   if (offset + 4 >= dwarf2_per_objfile->line.size)
10862     {
10863       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10864       return 0;
10865     }
10866
10867   lh = xmalloc (sizeof (*lh));
10868   memset (lh, 0, sizeof (*lh));
10869   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10870                           (void *) lh);
10871
10872   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10873
10874   /* Read in the header.  */
10875   lh->total_length =
10876     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10877                                             &bytes_read, &offset_size);
10878   line_ptr += bytes_read;
10879   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10880                                      + dwarf2_per_objfile->line.size))
10881     {
10882       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10883       return 0;
10884     }
10885   lh->statement_program_end = line_ptr + lh->total_length;
10886   lh->version = read_2_bytes (abfd, line_ptr);
10887   line_ptr += 2;
10888   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10889   line_ptr += offset_size;
10890   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10891   line_ptr += 1;
10892   if (lh->version >= 4)
10893     {
10894       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10895       line_ptr += 1;
10896     }
10897   else
10898     lh->maximum_ops_per_instruction = 1;
10899
10900   if (lh->maximum_ops_per_instruction == 0)
10901     {
10902       lh->maximum_ops_per_instruction = 1;
10903       complaint (&symfile_complaints,
10904                  _("invalid maximum_ops_per_instruction "
10905                    "in `.debug_line' section"));
10906     }
10907
10908   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10909   line_ptr += 1;
10910   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10911   line_ptr += 1;
10912   lh->line_range = read_1_byte (abfd, line_ptr);
10913   line_ptr += 1;
10914   lh->opcode_base = read_1_byte (abfd, line_ptr);
10915   line_ptr += 1;
10916   lh->standard_opcode_lengths
10917     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10918
10919   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10920   for (i = 1; i < lh->opcode_base; ++i)
10921     {
10922       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10923       line_ptr += 1;
10924     }
10925
10926   /* Read directory table.  */
10927   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10928     {
10929       line_ptr += bytes_read;
10930       add_include_dir (lh, cur_dir);
10931     }
10932   line_ptr += bytes_read;
10933
10934   /* Read file name table.  */
10935   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10936     {
10937       unsigned int dir_index, mod_time, length;
10938
10939       line_ptr += bytes_read;
10940       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10941       line_ptr += bytes_read;
10942       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10943       line_ptr += bytes_read;
10944       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10945       line_ptr += bytes_read;
10946
10947       add_file_name (lh, cur_file, dir_index, mod_time, length);
10948     }
10949   line_ptr += bytes_read;
10950   lh->statement_program_start = line_ptr;
10951
10952   if (line_ptr > (dwarf2_per_objfile->line.buffer
10953                   + dwarf2_per_objfile->line.size))
10954     complaint (&symfile_complaints,
10955                _("line number info header doesn't "
10956                  "fit in `.debug_line' section"));
10957
10958   discard_cleanups (back_to);
10959   return lh;
10960 }
10961
10962 /* Subroutine of dwarf_decode_lines to simplify it.
10963    Return the file name of the psymtab for included file FILE_INDEX
10964    in line header LH of PST.
10965    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10966    If space for the result is malloc'd, it will be freed by a cleanup.
10967    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
10968
10969 static char *
10970 psymtab_include_file_name (const struct line_header *lh, int file_index,
10971                            const struct partial_symtab *pst,
10972                            const char *comp_dir)
10973 {
10974   const struct file_entry fe = lh->file_names [file_index];
10975   char *include_name = fe.name;
10976   char *include_name_to_compare = include_name;
10977   char *dir_name = NULL;
10978   const char *pst_filename;
10979   char *copied_name = NULL;
10980   int file_is_pst;
10981
10982   if (fe.dir_index)
10983     dir_name = lh->include_dirs[fe.dir_index - 1];
10984
10985   if (!IS_ABSOLUTE_PATH (include_name)
10986       && (dir_name != NULL || comp_dir != NULL))
10987     {
10988       /* Avoid creating a duplicate psymtab for PST.
10989          We do this by comparing INCLUDE_NAME and PST_FILENAME.
10990          Before we do the comparison, however, we need to account
10991          for DIR_NAME and COMP_DIR.
10992          First prepend dir_name (if non-NULL).  If we still don't
10993          have an absolute path prepend comp_dir (if non-NULL).
10994          However, the directory we record in the include-file's
10995          psymtab does not contain COMP_DIR (to match the
10996          corresponding symtab(s)).
10997
10998          Example:
10999
11000          bash$ cd /tmp
11001          bash$ gcc -g ./hello.c
11002          include_name = "hello.c"
11003          dir_name = "."
11004          DW_AT_comp_dir = comp_dir = "/tmp"
11005          DW_AT_name = "./hello.c"  */
11006
11007       if (dir_name != NULL)
11008         {
11009           include_name = concat (dir_name, SLASH_STRING,
11010                                  include_name, (char *)NULL);
11011           include_name_to_compare = include_name;
11012           make_cleanup (xfree, include_name);
11013         }
11014       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11015         {
11016           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11017                                             include_name, (char *)NULL);
11018         }
11019     }
11020
11021   pst_filename = pst->filename;
11022   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11023     {
11024       copied_name = concat (pst->dirname, SLASH_STRING,
11025                             pst_filename, (char *)NULL);
11026       pst_filename = copied_name;
11027     }
11028
11029   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11030
11031   if (include_name_to_compare != include_name)
11032     xfree (include_name_to_compare);
11033   if (copied_name != NULL)
11034     xfree (copied_name);
11035
11036   if (file_is_pst)
11037     return NULL;
11038   return include_name;
11039 }
11040
11041 /* Ignore this record_line request.  */
11042
11043 static void
11044 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11045 {
11046   return;
11047 }
11048
11049 /* Decode the Line Number Program (LNP) for the given line_header
11050    structure and CU.  The actual information extracted and the type
11051    of structures created from the LNP depends on the value of PST.
11052
11053    1. If PST is NULL, then this procedure uses the data from the program
11054       to create all necessary symbol tables, and their linetables.
11055
11056    2. If PST is not NULL, this procedure reads the program to determine
11057       the list of files included by the unit represented by PST, and
11058       builds all the associated partial symbol tables.
11059
11060    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11061    It is used for relative paths in the line table.
11062    NOTE: When processing partial symtabs (pst != NULL),
11063    comp_dir == pst->dirname.
11064
11065    NOTE: It is important that psymtabs have the same file name (via strcmp)
11066    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11067    symtab we don't use it in the name of the psymtabs we create.
11068    E.g. expand_line_sal requires this when finding psymtabs to expand.
11069    A good testcase for this is mb-inline.exp.  */
11070
11071 static void
11072 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11073                     struct dwarf2_cu *cu, struct partial_symtab *pst)
11074 {
11075   gdb_byte *line_ptr, *extended_end;
11076   gdb_byte *line_end;
11077   unsigned int bytes_read, extended_len;
11078   unsigned char op_code, extended_op, adj_opcode;
11079   CORE_ADDR baseaddr;
11080   struct objfile *objfile = cu->objfile;
11081   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11082   const int decode_for_pst_p = (pst != NULL);
11083   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11084   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11085     = record_line;
11086
11087   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11088
11089   line_ptr = lh->statement_program_start;
11090   line_end = lh->statement_program_end;
11091
11092   /* Read the statement sequences until there's nothing left.  */
11093   while (line_ptr < line_end)
11094     {
11095       /* state machine registers  */
11096       CORE_ADDR address = 0;
11097       unsigned int file = 1;
11098       unsigned int line = 1;
11099       unsigned int column = 0;
11100       int is_stmt = lh->default_is_stmt;
11101       int basic_block = 0;
11102       int end_sequence = 0;
11103       CORE_ADDR addr;
11104       unsigned char op_index = 0;
11105
11106       if (!decode_for_pst_p && lh->num_file_names >= file)
11107         {
11108           /* Start a subfile for the current file of the state machine.  */
11109           /* lh->include_dirs and lh->file_names are 0-based, but the
11110              directory and file name numbers in the statement program
11111              are 1-based.  */
11112           struct file_entry *fe = &lh->file_names[file - 1];
11113           char *dir = NULL;
11114
11115           if (fe->dir_index)
11116             dir = lh->include_dirs[fe->dir_index - 1];
11117
11118           dwarf2_start_subfile (fe->name, dir, comp_dir);
11119         }
11120
11121       /* Decode the table.  */
11122       while (!end_sequence)
11123         {
11124           op_code = read_1_byte (abfd, line_ptr);
11125           line_ptr += 1;
11126           if (line_ptr > line_end)
11127             {
11128               dwarf2_debug_line_missing_end_sequence_complaint ();
11129               break;
11130             }
11131
11132           if (op_code >= lh->opcode_base)
11133             {
11134               /* Special operand.  */
11135               adj_opcode = op_code - lh->opcode_base;
11136               address += (((op_index + (adj_opcode / lh->line_range))
11137                            / lh->maximum_ops_per_instruction)
11138                           * lh->minimum_instruction_length);
11139               op_index = ((op_index + (adj_opcode / lh->line_range))
11140                           % lh->maximum_ops_per_instruction);
11141               line += lh->line_base + (adj_opcode % lh->line_range);
11142               if (lh->num_file_names < file || file == 0)
11143                 dwarf2_debug_line_missing_file_complaint ();
11144               /* For now we ignore lines not starting on an
11145                  instruction boundary.  */
11146               else if (op_index == 0)
11147                 {
11148                   lh->file_names[file - 1].included_p = 1;
11149                   if (!decode_for_pst_p && is_stmt)
11150                     {
11151                       if (last_subfile != current_subfile)
11152                         {
11153                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11154                           if (last_subfile)
11155                             (*p_record_line) (last_subfile, 0, addr);
11156                           last_subfile = current_subfile;
11157                         }
11158                       /* Append row to matrix using current values.  */
11159                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11160                       (*p_record_line) (current_subfile, line, addr);
11161                     }
11162                 }
11163               basic_block = 0;
11164             }
11165           else switch (op_code)
11166             {
11167             case DW_LNS_extended_op:
11168               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11169                                                    &bytes_read);
11170               line_ptr += bytes_read;
11171               extended_end = line_ptr + extended_len;
11172               extended_op = read_1_byte (abfd, line_ptr);
11173               line_ptr += 1;
11174               switch (extended_op)
11175                 {
11176                 case DW_LNE_end_sequence:
11177                   p_record_line = record_line;
11178                   end_sequence = 1;
11179                   break;
11180                 case DW_LNE_set_address:
11181                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11182
11183                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11184                     {
11185                       /* This line table is for a function which has been
11186                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11187
11188                       long line_offset
11189                         = line_ptr - dwarf2_per_objfile->line.buffer;
11190
11191                       complaint (&symfile_complaints,
11192                                  _(".debug_line address at offset 0x%lx is 0 "
11193                                    "[in module %s]"),
11194                                  line_offset, objfile->name);
11195                       p_record_line = noop_record_line;
11196                     }
11197
11198                   op_index = 0;
11199                   line_ptr += bytes_read;
11200                   address += baseaddr;
11201                   break;
11202                 case DW_LNE_define_file:
11203                   {
11204                     char *cur_file;
11205                     unsigned int dir_index, mod_time, length;
11206
11207                     cur_file = read_direct_string (abfd, line_ptr,
11208                                                    &bytes_read);
11209                     line_ptr += bytes_read;
11210                     dir_index =
11211                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11212                     line_ptr += bytes_read;
11213                     mod_time =
11214                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11215                     line_ptr += bytes_read;
11216                     length =
11217                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11218                     line_ptr += bytes_read;
11219                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11220                   }
11221                   break;
11222                 case DW_LNE_set_discriminator:
11223                   /* The discriminator is not interesting to the debugger;
11224                      just ignore it.  */
11225                   line_ptr = extended_end;
11226                   break;
11227                 default:
11228                   complaint (&symfile_complaints,
11229                              _("mangled .debug_line section"));
11230                   return;
11231                 }
11232               /* Make sure that we parsed the extended op correctly.  If e.g.
11233                  we expected a different address size than the producer used,
11234                  we may have read the wrong number of bytes.  */
11235               if (line_ptr != extended_end)
11236                 {
11237                   complaint (&symfile_complaints,
11238                              _("mangled .debug_line section"));
11239                   return;
11240                 }
11241               break;
11242             case DW_LNS_copy:
11243               if (lh->num_file_names < file || file == 0)
11244                 dwarf2_debug_line_missing_file_complaint ();
11245               else
11246                 {
11247                   lh->file_names[file - 1].included_p = 1;
11248                   if (!decode_for_pst_p && is_stmt)
11249                     {
11250                       if (last_subfile != current_subfile)
11251                         {
11252                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11253                           if (last_subfile)
11254                             (*p_record_line) (last_subfile, 0, addr);
11255                           last_subfile = current_subfile;
11256                         }
11257                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11258                       (*p_record_line) (current_subfile, line, addr);
11259                     }
11260                 }
11261               basic_block = 0;
11262               break;
11263             case DW_LNS_advance_pc:
11264               {
11265                 CORE_ADDR adjust
11266                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11267
11268                 address += (((op_index + adjust)
11269                              / lh->maximum_ops_per_instruction)
11270                             * lh->minimum_instruction_length);
11271                 op_index = ((op_index + adjust)
11272                             % lh->maximum_ops_per_instruction);
11273                 line_ptr += bytes_read;
11274               }
11275               break;
11276             case DW_LNS_advance_line:
11277               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11278               line_ptr += bytes_read;
11279               break;
11280             case DW_LNS_set_file:
11281               {
11282                 /* The arrays lh->include_dirs and lh->file_names are
11283                    0-based, but the directory and file name numbers in
11284                    the statement program are 1-based.  */
11285                 struct file_entry *fe;
11286                 char *dir = NULL;
11287
11288                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11289                 line_ptr += bytes_read;
11290                 if (lh->num_file_names < file || file == 0)
11291                   dwarf2_debug_line_missing_file_complaint ();
11292                 else
11293                   {
11294                     fe = &lh->file_names[file - 1];
11295                     if (fe->dir_index)
11296                       dir = lh->include_dirs[fe->dir_index - 1];
11297                     if (!decode_for_pst_p)
11298                       {
11299                         last_subfile = current_subfile;
11300                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11301                       }
11302                   }
11303               }
11304               break;
11305             case DW_LNS_set_column:
11306               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11307               line_ptr += bytes_read;
11308               break;
11309             case DW_LNS_negate_stmt:
11310               is_stmt = (!is_stmt);
11311               break;
11312             case DW_LNS_set_basic_block:
11313               basic_block = 1;
11314               break;
11315             /* Add to the address register of the state machine the
11316                address increment value corresponding to special opcode
11317                255.  I.e., this value is scaled by the minimum
11318                instruction length since special opcode 255 would have
11319                scaled the increment.  */
11320             case DW_LNS_const_add_pc:
11321               {
11322                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11323
11324                 address += (((op_index + adjust)
11325                              / lh->maximum_ops_per_instruction)
11326                             * lh->minimum_instruction_length);
11327                 op_index = ((op_index + adjust)
11328                             % lh->maximum_ops_per_instruction);
11329               }
11330               break;
11331             case DW_LNS_fixed_advance_pc:
11332               address += read_2_bytes (abfd, line_ptr);
11333               op_index = 0;
11334               line_ptr += 2;
11335               break;
11336             default:
11337               {
11338                 /* Unknown standard opcode, ignore it.  */
11339                 int i;
11340
11341                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11342                   {
11343                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11344                     line_ptr += bytes_read;
11345                   }
11346               }
11347             }
11348         }
11349       if (lh->num_file_names < file || file == 0)
11350         dwarf2_debug_line_missing_file_complaint ();
11351       else
11352         {
11353           lh->file_names[file - 1].included_p = 1;
11354           if (!decode_for_pst_p)
11355             {
11356               addr = gdbarch_addr_bits_remove (gdbarch, address);
11357               (*p_record_line) (current_subfile, 0, addr);
11358             }
11359         }
11360     }
11361
11362   if (decode_for_pst_p)
11363     {
11364       int file_index;
11365
11366       /* Now that we're done scanning the Line Header Program, we can
11367          create the psymtab of each included file.  */
11368       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11369         if (lh->file_names[file_index].included_p == 1)
11370           {
11371             char *include_name =
11372               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11373             if (include_name != NULL)
11374               dwarf2_create_include_psymtab (include_name, pst, objfile);
11375           }
11376     }
11377   else
11378     {
11379       /* Make sure a symtab is created for every file, even files
11380          which contain only variables (i.e. no code with associated
11381          line numbers).  */
11382
11383       int i;
11384       struct file_entry *fe;
11385
11386       for (i = 0; i < lh->num_file_names; i++)
11387         {
11388           char *dir = NULL;
11389
11390           fe = &lh->file_names[i];
11391           if (fe->dir_index)
11392             dir = lh->include_dirs[fe->dir_index - 1];
11393           dwarf2_start_subfile (fe->name, dir, comp_dir);
11394
11395           /* Skip the main file; we don't need it, and it must be
11396              allocated last, so that it will show up before the
11397              non-primary symtabs in the objfile's symtab list.  */
11398           if (current_subfile == first_subfile)
11399             continue;
11400
11401           if (current_subfile->symtab == NULL)
11402             current_subfile->symtab = allocate_symtab (current_subfile->name,
11403                                                        objfile);
11404           fe->symtab = current_subfile->symtab;
11405         }
11406     }
11407 }
11408
11409 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11410    DIRNAME the name of the source directory which contains FILENAME
11411    or NULL if not known.  COMP_DIR is the compilation directory for the
11412    linetable's compilation unit or NULL if not known.
11413    This routine tries to keep line numbers from identical absolute and
11414    relative file names in a common subfile.
11415
11416    Using the `list' example from the GDB testsuite, which resides in
11417    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11418    of /srcdir/list0.c yields the following debugging information for list0.c:
11419
11420    DW_AT_name:          /srcdir/list0.c
11421    DW_AT_comp_dir:              /compdir
11422    files.files[0].name: list0.h
11423    files.files[0].dir:  /srcdir
11424    files.files[1].name: list0.c
11425    files.files[1].dir:  /srcdir
11426
11427    The line number information for list0.c has to end up in a single
11428    subfile, so that `break /srcdir/list0.c:1' works as expected.
11429    start_subfile will ensure that this happens provided that we pass the
11430    concatenation of files.files[1].dir and files.files[1].name as the
11431    subfile's name.  */
11432
11433 static void
11434 dwarf2_start_subfile (char *filename, const char *dirname,
11435                       const char *comp_dir)
11436 {
11437   char *fullname;
11438
11439   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11440      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11441      second argument to start_subfile.  To be consistent, we do the
11442      same here.  In order not to lose the line information directory,
11443      we concatenate it to the filename when it makes sense.
11444      Note that the Dwarf3 standard says (speaking of filenames in line
11445      information): ``The directory index is ignored for file names
11446      that represent full path names''.  Thus ignoring dirname in the
11447      `else' branch below isn't an issue.  */
11448
11449   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11450     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11451   else
11452     fullname = filename;
11453
11454   start_subfile (fullname, comp_dir);
11455
11456   if (fullname != filename)
11457     xfree (fullname);
11458 }
11459
11460 static void
11461 var_decode_location (struct attribute *attr, struct symbol *sym,
11462                      struct dwarf2_cu *cu)
11463 {
11464   struct objfile *objfile = cu->objfile;
11465   struct comp_unit_head *cu_header = &cu->header;
11466
11467   /* NOTE drow/2003-01-30: There used to be a comment and some special
11468      code here to turn a symbol with DW_AT_external and a
11469      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11470      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11471      with some versions of binutils) where shared libraries could have
11472      relocations against symbols in their debug information - the
11473      minimal symbol would have the right address, but the debug info
11474      would not.  It's no longer necessary, because we will explicitly
11475      apply relocations when we read in the debug information now.  */
11476
11477   /* A DW_AT_location attribute with no contents indicates that a
11478      variable has been optimized away.  */
11479   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11480     {
11481       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11482       return;
11483     }
11484
11485   /* Handle one degenerate form of location expression specially, to
11486      preserve GDB's previous behavior when section offsets are
11487      specified.  If this is just a DW_OP_addr then mark this symbol
11488      as LOC_STATIC.  */
11489
11490   if (attr_form_is_block (attr)
11491       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11492       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11493     {
11494       unsigned int dummy;
11495
11496       SYMBOL_VALUE_ADDRESS (sym) =
11497         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11498       SYMBOL_CLASS (sym) = LOC_STATIC;
11499       fixup_symbol_section (sym, objfile);
11500       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11501                                               SYMBOL_SECTION (sym));
11502       return;
11503     }
11504
11505   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11506      expression evaluator, and use LOC_COMPUTED only when necessary
11507      (i.e. when the value of a register or memory location is
11508      referenced, or a thread-local block, etc.).  Then again, it might
11509      not be worthwhile.  I'm assuming that it isn't unless performance
11510      or memory numbers show me otherwise.  */
11511
11512   dwarf2_symbol_mark_computed (attr, sym, cu);
11513   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11514
11515   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11516     cu->has_loclist = 1;
11517 }
11518
11519 /* Given a pointer to a DWARF information entry, figure out if we need
11520    to make a symbol table entry for it, and if so, create a new entry
11521    and return a pointer to it.
11522    If TYPE is NULL, determine symbol type from the die, otherwise
11523    used the passed type.
11524    If SPACE is not NULL, use it to hold the new symbol.  If it is
11525    NULL, allocate a new symbol on the objfile's obstack.  */
11526
11527 static struct symbol *
11528 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11529                  struct symbol *space)
11530 {
11531   struct objfile *objfile = cu->objfile;
11532   struct symbol *sym = NULL;
11533   char *name;
11534   struct attribute *attr = NULL;
11535   struct attribute *attr2 = NULL;
11536   CORE_ADDR baseaddr;
11537   struct pending **list_to_add = NULL;
11538
11539   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11540
11541   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11542
11543   name = dwarf2_name (die, cu);
11544   if (name)
11545     {
11546       const char *linkagename;
11547       int suppress_add = 0;
11548
11549       if (space)
11550         sym = space;
11551       else
11552         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11553       OBJSTAT (objfile, n_syms++);
11554
11555       /* Cache this symbol's name and the name's demangled form (if any).  */
11556       SYMBOL_SET_LANGUAGE (sym, cu->language);
11557       linkagename = dwarf2_physname (name, die, cu);
11558       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11559
11560       /* Fortran does not have mangling standard and the mangling does differ
11561          between gfortran, iFort etc.  */
11562       if (cu->language == language_fortran
11563           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11564         symbol_set_demangled_name (&(sym->ginfo),
11565                                    (char *) dwarf2_full_name (name, die, cu),
11566                                    NULL);
11567
11568       /* Default assumptions.
11569          Use the passed type or decode it from the die.  */
11570       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11571       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11572       if (type != NULL)
11573         SYMBOL_TYPE (sym) = type;
11574       else
11575         SYMBOL_TYPE (sym) = die_type (die, cu);
11576       attr = dwarf2_attr (die,
11577                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11578                           cu);
11579       if (attr)
11580         {
11581           SYMBOL_LINE (sym) = DW_UNSND (attr);
11582         }
11583
11584       attr = dwarf2_attr (die,
11585                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11586                           cu);
11587       if (attr)
11588         {
11589           int file_index = DW_UNSND (attr);
11590
11591           if (cu->line_header == NULL
11592               || file_index > cu->line_header->num_file_names)
11593             complaint (&symfile_complaints,
11594                        _("file index out of range"));
11595           else if (file_index > 0)
11596             {
11597               struct file_entry *fe;
11598
11599               fe = &cu->line_header->file_names[file_index - 1];
11600               SYMBOL_SYMTAB (sym) = fe->symtab;
11601             }
11602         }
11603
11604       switch (die->tag)
11605         {
11606         case DW_TAG_label:
11607           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11608           if (attr)
11609             {
11610               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11611             }
11612           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11613           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11614           SYMBOL_CLASS (sym) = LOC_LABEL;
11615           add_symbol_to_list (sym, cu->list_in_scope);
11616           break;
11617         case DW_TAG_subprogram:
11618           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11619              finish_block.  */
11620           SYMBOL_CLASS (sym) = LOC_BLOCK;
11621           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11622           if ((attr2 && (DW_UNSND (attr2) != 0))
11623               || cu->language == language_ada)
11624             {
11625               /* Subprograms marked external are stored as a global symbol.
11626                  Ada subprograms, whether marked external or not, are always
11627                  stored as a global symbol, because we want to be able to
11628                  access them globally.  For instance, we want to be able
11629                  to break on a nested subprogram without having to
11630                  specify the context.  */
11631               list_to_add = &global_symbols;
11632             }
11633           else
11634             {
11635               list_to_add = cu->list_in_scope;
11636             }
11637           break;
11638         case DW_TAG_inlined_subroutine:
11639           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11640              finish_block.  */
11641           SYMBOL_CLASS (sym) = LOC_BLOCK;
11642           SYMBOL_INLINED (sym) = 1;
11643           /* Do not add the symbol to any lists.  It will be found via
11644              BLOCK_FUNCTION from the blockvector.  */
11645           break;
11646         case DW_TAG_template_value_param:
11647           suppress_add = 1;
11648           /* Fall through.  */
11649         case DW_TAG_constant:
11650         case DW_TAG_variable:
11651         case DW_TAG_member:
11652           /* Compilation with minimal debug info may result in
11653              variables with missing type entries.  Change the
11654              misleading `void' type to something sensible.  */
11655           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11656             SYMBOL_TYPE (sym)
11657               = objfile_type (objfile)->nodebug_data_symbol;
11658
11659           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11660           /* In the case of DW_TAG_member, we should only be called for
11661              static const members.  */
11662           if (die->tag == DW_TAG_member)
11663             {
11664               /* dwarf2_add_field uses die_is_declaration,
11665                  so we do the same.  */
11666               gdb_assert (die_is_declaration (die, cu));
11667               gdb_assert (attr);
11668             }
11669           if (attr)
11670             {
11671               dwarf2_const_value (attr, sym, cu);
11672               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11673               if (!suppress_add)
11674                 {
11675                   if (attr2 && (DW_UNSND (attr2) != 0))
11676                     list_to_add = &global_symbols;
11677                   else
11678                     list_to_add = cu->list_in_scope;
11679                 }
11680               break;
11681             }
11682           attr = dwarf2_attr (die, DW_AT_location, cu);
11683           if (attr)
11684             {
11685               var_decode_location (attr, sym, cu);
11686               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11687               if (SYMBOL_CLASS (sym) == LOC_STATIC
11688                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11689                   && !dwarf2_per_objfile->has_section_at_zero)
11690                 {
11691                   /* When a static variable is eliminated by the linker,
11692                      the corresponding debug information is not stripped
11693                      out, but the variable address is set to null;
11694                      do not add such variables into symbol table.  */
11695                 }
11696               else if (attr2 && (DW_UNSND (attr2) != 0))
11697                 {
11698                   /* Workaround gfortran PR debug/40040 - it uses
11699                      DW_AT_location for variables in -fPIC libraries which may
11700                      get overriden by other libraries/executable and get
11701                      a different address.  Resolve it by the minimal symbol
11702                      which may come from inferior's executable using copy
11703                      relocation.  Make this workaround only for gfortran as for
11704                      other compilers GDB cannot guess the minimal symbol
11705                      Fortran mangling kind.  */
11706                   if (cu->language == language_fortran && die->parent
11707                       && die->parent->tag == DW_TAG_module
11708                       && cu->producer
11709                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11710                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11711
11712                   /* A variable with DW_AT_external is never static,
11713                      but it may be block-scoped.  */
11714                   list_to_add = (cu->list_in_scope == &file_symbols
11715                                  ? &global_symbols : cu->list_in_scope);
11716                 }
11717               else
11718                 list_to_add = cu->list_in_scope;
11719             }
11720           else
11721             {
11722               /* We do not know the address of this symbol.
11723                  If it is an external symbol and we have type information
11724                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11725                  The address of the variable will then be determined from
11726                  the minimal symbol table whenever the variable is
11727                  referenced.  */
11728               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11729               if (attr2 && (DW_UNSND (attr2) != 0)
11730                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11731                 {
11732                   /* A variable with DW_AT_external is never static, but it
11733                      may be block-scoped.  */
11734                   list_to_add = (cu->list_in_scope == &file_symbols
11735                                  ? &global_symbols : cu->list_in_scope);
11736
11737                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11738                 }
11739               else if (!die_is_declaration (die, cu))
11740                 {
11741                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11742                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11743                   if (!suppress_add)
11744                     list_to_add = cu->list_in_scope;
11745                 }
11746             }
11747           break;
11748         case DW_TAG_formal_parameter:
11749           /* If we are inside a function, mark this as an argument.  If
11750              not, we might be looking at an argument to an inlined function
11751              when we do not have enough information to show inlined frames;
11752              pretend it's a local variable in that case so that the user can
11753              still see it.  */
11754           if (context_stack_depth > 0
11755               && context_stack[context_stack_depth - 1].name != NULL)
11756             SYMBOL_IS_ARGUMENT (sym) = 1;
11757           attr = dwarf2_attr (die, DW_AT_location, cu);
11758           if (attr)
11759             {
11760               var_decode_location (attr, sym, cu);
11761             }
11762           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11763           if (attr)
11764             {
11765               dwarf2_const_value (attr, sym, cu);
11766             }
11767
11768           list_to_add = cu->list_in_scope;
11769           break;
11770         case DW_TAG_unspecified_parameters:
11771           /* From varargs functions; gdb doesn't seem to have any
11772              interest in this information, so just ignore it for now.
11773              (FIXME?) */
11774           break;
11775         case DW_TAG_template_type_param:
11776           suppress_add = 1;
11777           /* Fall through.  */
11778         case DW_TAG_class_type:
11779         case DW_TAG_interface_type:
11780         case DW_TAG_structure_type:
11781         case DW_TAG_union_type:
11782         case DW_TAG_set_type:
11783         case DW_TAG_enumeration_type:
11784           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11785           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11786
11787           {
11788             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11789                really ever be static objects: otherwise, if you try
11790                to, say, break of a class's method and you're in a file
11791                which doesn't mention that class, it won't work unless
11792                the check for all static symbols in lookup_symbol_aux
11793                saves you.  See the OtherFileClass tests in
11794                gdb.c++/namespace.exp.  */
11795
11796             if (!suppress_add)
11797               {
11798                 list_to_add = (cu->list_in_scope == &file_symbols
11799                                && (cu->language == language_cplus
11800                                    || cu->language == language_java)
11801                                ? &global_symbols : cu->list_in_scope);
11802
11803                 /* The semantics of C++ state that "struct foo {
11804                    ... }" also defines a typedef for "foo".  A Java
11805                    class declaration also defines a typedef for the
11806                    class.  */
11807                 if (cu->language == language_cplus
11808                     || cu->language == language_java
11809                     || cu->language == language_ada)
11810                   {
11811                     /* The symbol's name is already allocated along
11812                        with this objfile, so we don't need to
11813                        duplicate it for the type.  */
11814                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11815                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11816                   }
11817               }
11818           }
11819           break;
11820         case DW_TAG_typedef:
11821           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11822           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11823           list_to_add = cu->list_in_scope;
11824           break;
11825         case DW_TAG_base_type:
11826         case DW_TAG_subrange_type:
11827           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11828           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11829           list_to_add = cu->list_in_scope;
11830           break;
11831         case DW_TAG_enumerator:
11832           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11833           if (attr)
11834             {
11835               dwarf2_const_value (attr, sym, cu);
11836             }
11837           {
11838             /* NOTE: carlton/2003-11-10: See comment above in the
11839                DW_TAG_class_type, etc. block.  */
11840
11841             list_to_add = (cu->list_in_scope == &file_symbols
11842                            && (cu->language == language_cplus
11843                                || cu->language == language_java)
11844                            ? &global_symbols : cu->list_in_scope);
11845           }
11846           break;
11847         case DW_TAG_namespace:
11848           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11849           list_to_add = &global_symbols;
11850           break;
11851         default:
11852           /* Not a tag we recognize.  Hopefully we aren't processing
11853              trash data, but since we must specifically ignore things
11854              we don't recognize, there is nothing else we should do at
11855              this point.  */
11856           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11857                      dwarf_tag_name (die->tag));
11858           break;
11859         }
11860
11861       if (suppress_add)
11862         {
11863           sym->hash_next = objfile->template_symbols;
11864           objfile->template_symbols = sym;
11865           list_to_add = NULL;
11866         }
11867
11868       if (list_to_add != NULL)
11869         add_symbol_to_list (sym, list_to_add);
11870
11871       /* For the benefit of old versions of GCC, check for anonymous
11872          namespaces based on the demangled name.  */
11873       if (!processing_has_namespace_info
11874           && cu->language == language_cplus)
11875         cp_scan_for_anonymous_namespaces (sym, objfile);
11876     }
11877   return (sym);
11878 }
11879
11880 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
11881
11882 static struct symbol *
11883 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11884 {
11885   return new_symbol_full (die, type, cu, NULL);
11886 }
11887
11888 /* Given an attr with a DW_FORM_dataN value in host byte order,
11889    zero-extend it as appropriate for the symbol's type.  The DWARF
11890    standard (v4) is not entirely clear about the meaning of using
11891    DW_FORM_dataN for a constant with a signed type, where the type is
11892    wider than the data.  The conclusion of a discussion on the DWARF
11893    list was that this is unspecified.  We choose to always zero-extend
11894    because that is the interpretation long in use by GCC.  */
11895
11896 static gdb_byte *
11897 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11898                          const char *name, struct obstack *obstack,
11899                          struct dwarf2_cu *cu, long *value, int bits)
11900 {
11901   struct objfile *objfile = cu->objfile;
11902   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11903                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11904   LONGEST l = DW_UNSND (attr);
11905
11906   if (bits < sizeof (*value) * 8)
11907     {
11908       l &= ((LONGEST) 1 << bits) - 1;
11909       *value = l;
11910     }
11911   else if (bits == sizeof (*value) * 8)
11912     *value = l;
11913   else
11914     {
11915       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11916       store_unsigned_integer (bytes, bits / 8, byte_order, l);
11917       return bytes;
11918     }
11919
11920   return NULL;
11921 }
11922
11923 /* Read a constant value from an attribute.  Either set *VALUE, or if
11924    the value does not fit in *VALUE, set *BYTES - either already
11925    allocated on the objfile obstack, or newly allocated on OBSTACK,
11926    or, set *BATON, if we translated the constant to a location
11927    expression.  */
11928
11929 static void
11930 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11931                          const char *name, struct obstack *obstack,
11932                          struct dwarf2_cu *cu,
11933                          long *value, gdb_byte **bytes,
11934                          struct dwarf2_locexpr_baton **baton)
11935 {
11936   struct objfile *objfile = cu->objfile;
11937   struct comp_unit_head *cu_header = &cu->header;
11938   struct dwarf_block *blk;
11939   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11940                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11941
11942   *value = 0;
11943   *bytes = NULL;
11944   *baton = NULL;
11945
11946   switch (attr->form)
11947     {
11948     case DW_FORM_addr:
11949       {
11950         gdb_byte *data;
11951
11952         if (TYPE_LENGTH (type) != cu_header->addr_size)
11953           dwarf2_const_value_length_mismatch_complaint (name,
11954                                                         cu_header->addr_size,
11955                                                         TYPE_LENGTH (type));
11956         /* Symbols of this form are reasonably rare, so we just
11957            piggyback on the existing location code rather than writing
11958            a new implementation of symbol_computed_ops.  */
11959         *baton = obstack_alloc (&objfile->objfile_obstack,
11960                                 sizeof (struct dwarf2_locexpr_baton));
11961         (*baton)->per_cu = cu->per_cu;
11962         gdb_assert ((*baton)->per_cu);
11963
11964         (*baton)->size = 2 + cu_header->addr_size;
11965         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11966         (*baton)->data = data;
11967
11968         data[0] = DW_OP_addr;
11969         store_unsigned_integer (&data[1], cu_header->addr_size,
11970                                 byte_order, DW_ADDR (attr));
11971         data[cu_header->addr_size + 1] = DW_OP_stack_value;
11972       }
11973       break;
11974     case DW_FORM_string:
11975     case DW_FORM_strp:
11976       /* DW_STRING is already allocated on the objfile obstack, point
11977          directly to it.  */
11978       *bytes = (gdb_byte *) DW_STRING (attr);
11979       break;
11980     case DW_FORM_block1:
11981     case DW_FORM_block2:
11982     case DW_FORM_block4:
11983     case DW_FORM_block:
11984     case DW_FORM_exprloc:
11985       blk = DW_BLOCK (attr);
11986       if (TYPE_LENGTH (type) != blk->size)
11987         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11988                                                       TYPE_LENGTH (type));
11989       *bytes = blk->data;
11990       break;
11991
11992       /* The DW_AT_const_value attributes are supposed to carry the
11993          symbol's value "represented as it would be on the target
11994          architecture."  By the time we get here, it's already been
11995          converted to host endianness, so we just need to sign- or
11996          zero-extend it as appropriate.  */
11997     case DW_FORM_data1:
11998       *bytes = dwarf2_const_value_data (attr, type, name,
11999                                         obstack, cu, value, 8);
12000       break;
12001     case DW_FORM_data2:
12002       *bytes = dwarf2_const_value_data (attr, type, name,
12003                                         obstack, cu, value, 16);
12004       break;
12005     case DW_FORM_data4:
12006       *bytes = dwarf2_const_value_data (attr, type, name,
12007                                         obstack, cu, value, 32);
12008       break;
12009     case DW_FORM_data8:
12010       *bytes = dwarf2_const_value_data (attr, type, name,
12011                                         obstack, cu, value, 64);
12012       break;
12013
12014     case DW_FORM_sdata:
12015       *value = DW_SND (attr);
12016       break;
12017
12018     case DW_FORM_udata:
12019       *value = DW_UNSND (attr);
12020       break;
12021
12022     default:
12023       complaint (&symfile_complaints,
12024                  _("unsupported const value attribute form: '%s'"),
12025                  dwarf_form_name (attr->form));
12026       *value = 0;
12027       break;
12028     }
12029 }
12030
12031
12032 /* Copy constant value from an attribute to a symbol.  */
12033
12034 static void
12035 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12036                     struct dwarf2_cu *cu)
12037 {
12038   struct objfile *objfile = cu->objfile;
12039   struct comp_unit_head *cu_header = &cu->header;
12040   long value;
12041   gdb_byte *bytes;
12042   struct dwarf2_locexpr_baton *baton;
12043
12044   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12045                            SYMBOL_PRINT_NAME (sym),
12046                            &objfile->objfile_obstack, cu,
12047                            &value, &bytes, &baton);
12048
12049   if (baton != NULL)
12050     {
12051       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12052       SYMBOL_LOCATION_BATON (sym) = baton;
12053       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12054     }
12055   else if (bytes != NULL)
12056      {
12057       SYMBOL_VALUE_BYTES (sym) = bytes;
12058       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12059     }
12060   else
12061     {
12062       SYMBOL_VALUE (sym) = value;
12063       SYMBOL_CLASS (sym) = LOC_CONST;
12064     }
12065 }
12066
12067 /* Return the type of the die in question using its DW_AT_type attribute.  */
12068
12069 static struct type *
12070 die_type (struct die_info *die, struct dwarf2_cu *cu)
12071 {
12072   struct attribute *type_attr;
12073
12074   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12075   if (!type_attr)
12076     {
12077       /* A missing DW_AT_type represents a void type.  */
12078       return objfile_type (cu->objfile)->builtin_void;
12079     }
12080
12081   return lookup_die_type (die, type_attr, cu);
12082 }
12083
12084 /* True iff CU's producer generates GNAT Ada auxiliary information
12085    that allows to find parallel types through that information instead
12086    of having to do expensive parallel lookups by type name.  */
12087
12088 static int
12089 need_gnat_info (struct dwarf2_cu *cu)
12090 {
12091   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12092      of GNAT produces this auxiliary information, without any indication
12093      that it is produced.  Part of enhancing the FSF version of GNAT
12094      to produce that information will be to put in place an indicator
12095      that we can use in order to determine whether the descriptive type
12096      info is available or not.  One suggestion that has been made is
12097      to use a new attribute, attached to the CU die.  For now, assume
12098      that the descriptive type info is not available.  */
12099   return 0;
12100 }
12101
12102 /* Return the auxiliary type of the die in question using its
12103    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12104    attribute is not present.  */
12105
12106 static struct type *
12107 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12108 {
12109   struct attribute *type_attr;
12110
12111   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12112   if (!type_attr)
12113     return NULL;
12114
12115   return lookup_die_type (die, type_attr, cu);
12116 }
12117
12118 /* If DIE has a descriptive_type attribute, then set the TYPE's
12119    descriptive type accordingly.  */
12120
12121 static void
12122 set_descriptive_type (struct type *type, struct die_info *die,
12123                       struct dwarf2_cu *cu)
12124 {
12125   struct type *descriptive_type = die_descriptive_type (die, cu);
12126
12127   if (descriptive_type)
12128     {
12129       ALLOCATE_GNAT_AUX_TYPE (type);
12130       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12131     }
12132 }
12133
12134 /* Return the containing type of the die in question using its
12135    DW_AT_containing_type attribute.  */
12136
12137 static struct type *
12138 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12139 {
12140   struct attribute *type_attr;
12141
12142   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12143   if (!type_attr)
12144     error (_("Dwarf Error: Problem turning containing type into gdb type "
12145              "[in module %s]"), cu->objfile->name);
12146
12147   return lookup_die_type (die, type_attr, cu);
12148 }
12149
12150 /* Look up the type of DIE in CU using its type attribute ATTR.
12151    If there is no type substitute an error marker.  */
12152
12153 static struct type *
12154 lookup_die_type (struct die_info *die, struct attribute *attr,
12155                  struct dwarf2_cu *cu)
12156 {
12157   struct objfile *objfile = cu->objfile;
12158   struct type *this_type;
12159
12160   /* First see if we have it cached.  */
12161
12162   if (is_ref_attr (attr))
12163     {
12164       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12165
12166       this_type = get_die_type_at_offset (offset, cu->per_cu);
12167     }
12168   else if (attr->form == DW_FORM_ref_sig8)
12169     {
12170       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12171       struct dwarf2_cu *sig_cu;
12172       unsigned int offset;
12173
12174       /* sig_type will be NULL if the signatured type is missing from
12175          the debug info.  */
12176       if (sig_type == NULL)
12177         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12178                  "at 0x%x [in module %s]"),
12179                die->offset, objfile->name);
12180
12181       gdb_assert (sig_type->per_cu.debug_types_section);
12182       offset = sig_type->per_cu.offset + sig_type->type_offset;
12183       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12184     }
12185   else
12186     {
12187       dump_die_for_error (die);
12188       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12189              dwarf_attr_name (attr->name), objfile->name);
12190     }
12191
12192   /* If not cached we need to read it in.  */
12193
12194   if (this_type == NULL)
12195     {
12196       struct die_info *type_die;
12197       struct dwarf2_cu *type_cu = cu;
12198
12199       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12200       /* If the type is cached, we should have found it above.  */
12201       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12202       this_type = read_type_die_1 (type_die, type_cu);
12203     }
12204
12205   /* If we still don't have a type use an error marker.  */
12206
12207   if (this_type == NULL)
12208     {
12209       char *message, *saved;
12210
12211       /* read_type_die already issued a complaint.  */
12212       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12213                             objfile->name,
12214                             cu->header.offset,
12215                             die->offset);
12216       saved = obstack_copy0 (&objfile->objfile_obstack,
12217                              message, strlen (message));
12218       xfree (message);
12219
12220       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12221     }
12222
12223   return this_type;
12224 }
12225
12226 /* Return the type in DIE, CU.
12227    Returns NULL for invalid types.
12228
12229    This first does a lookup in the appropriate type_hash table,
12230    and only reads the die in if necessary.
12231
12232    NOTE: This can be called when reading in partial or full symbols.  */
12233
12234 static struct type *
12235 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12236 {
12237   struct type *this_type;
12238
12239   this_type = get_die_type (die, cu);
12240   if (this_type)
12241     return this_type;
12242
12243   return read_type_die_1 (die, cu);
12244 }
12245
12246 /* Read the type in DIE, CU.
12247    Returns NULL for invalid types.  */
12248
12249 static struct type *
12250 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12251 {
12252   struct type *this_type = NULL;
12253
12254   switch (die->tag)
12255     {
12256     case DW_TAG_class_type:
12257     case DW_TAG_interface_type:
12258     case DW_TAG_structure_type:
12259     case DW_TAG_union_type:
12260       this_type = read_structure_type (die, cu);
12261       break;
12262     case DW_TAG_enumeration_type:
12263       this_type = read_enumeration_type (die, cu);
12264       break;
12265     case DW_TAG_subprogram:
12266     case DW_TAG_subroutine_type:
12267     case DW_TAG_inlined_subroutine:
12268       this_type = read_subroutine_type (die, cu);
12269       break;
12270     case DW_TAG_array_type:
12271       this_type = read_array_type (die, cu);
12272       break;
12273     case DW_TAG_set_type:
12274       this_type = read_set_type (die, cu);
12275       break;
12276     case DW_TAG_pointer_type:
12277       this_type = read_tag_pointer_type (die, cu);
12278       break;
12279     case DW_TAG_ptr_to_member_type:
12280       this_type = read_tag_ptr_to_member_type (die, cu);
12281       break;
12282     case DW_TAG_reference_type:
12283       this_type = read_tag_reference_type (die, cu);
12284       break;
12285     case DW_TAG_const_type:
12286       this_type = read_tag_const_type (die, cu);
12287       break;
12288     case DW_TAG_volatile_type:
12289       this_type = read_tag_volatile_type (die, cu);
12290       break;
12291     case DW_TAG_string_type:
12292       this_type = read_tag_string_type (die, cu);
12293       break;
12294     case DW_TAG_typedef:
12295       this_type = read_typedef (die, cu);
12296       break;
12297     case DW_TAG_subrange_type:
12298       this_type = read_subrange_type (die, cu);
12299       break;
12300     case DW_TAG_base_type:
12301       this_type = read_base_type (die, cu);
12302       break;
12303     case DW_TAG_unspecified_type:
12304       this_type = read_unspecified_type (die, cu);
12305       break;
12306     case DW_TAG_namespace:
12307       this_type = read_namespace_type (die, cu);
12308       break;
12309     case DW_TAG_module:
12310       this_type = read_module_type (die, cu);
12311       break;
12312     default:
12313       complaint (&symfile_complaints,
12314                  _("unexpected tag in read_type_die: '%s'"),
12315                  dwarf_tag_name (die->tag));
12316       break;
12317     }
12318
12319   return this_type;
12320 }
12321
12322 /* See if we can figure out if the class lives in a namespace.  We do
12323    this by looking for a member function; its demangled name will
12324    contain namespace info, if there is any.
12325    Return the computed name or NULL.
12326    Space for the result is allocated on the objfile's obstack.
12327    This is the full-die version of guess_partial_die_structure_name.
12328    In this case we know DIE has no useful parent.  */
12329
12330 static char *
12331 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12332 {
12333   struct die_info *spec_die;
12334   struct dwarf2_cu *spec_cu;
12335   struct die_info *child;
12336
12337   spec_cu = cu;
12338   spec_die = die_specification (die, &spec_cu);
12339   if (spec_die != NULL)
12340     {
12341       die = spec_die;
12342       cu = spec_cu;
12343     }
12344
12345   for (child = die->child;
12346        child != NULL;
12347        child = child->sibling)
12348     {
12349       if (child->tag == DW_TAG_subprogram)
12350         {
12351           struct attribute *attr;
12352
12353           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12354           if (attr == NULL)
12355             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12356           if (attr != NULL)
12357             {
12358               char *actual_name
12359                 = language_class_name_from_physname (cu->language_defn,
12360                                                      DW_STRING (attr));
12361               char *name = NULL;
12362
12363               if (actual_name != NULL)
12364                 {
12365                   char *die_name = dwarf2_name (die, cu);
12366
12367                   if (die_name != NULL
12368                       && strcmp (die_name, actual_name) != 0)
12369                     {
12370                       /* Strip off the class name from the full name.
12371                          We want the prefix.  */
12372                       int die_name_len = strlen (die_name);
12373                       int actual_name_len = strlen (actual_name);
12374
12375                       /* Test for '::' as a sanity check.  */
12376                       if (actual_name_len > die_name_len + 2
12377                           && actual_name[actual_name_len
12378                                          - die_name_len - 1] == ':')
12379                         name =
12380                           obsavestring (actual_name,
12381                                         actual_name_len - die_name_len - 2,
12382                                         &cu->objfile->objfile_obstack);
12383                     }
12384                 }
12385               xfree (actual_name);
12386               return name;
12387             }
12388         }
12389     }
12390
12391   return NULL;
12392 }
12393
12394 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12395    prefix part in such case.  See
12396    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12397
12398 static char *
12399 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12400 {
12401   struct attribute *attr;
12402   char *base;
12403
12404   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12405       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12406     return NULL;
12407
12408   attr = dwarf2_attr (die, DW_AT_name, cu);
12409   if (attr != NULL && DW_STRING (attr) != NULL)
12410     return NULL;
12411
12412   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12413   if (attr == NULL)
12414     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12415   if (attr == NULL || DW_STRING (attr) == NULL)
12416     return NULL;
12417
12418   /* dwarf2_name had to be already called.  */
12419   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12420
12421   /* Strip the base name, keep any leading namespaces/classes.  */
12422   base = strrchr (DW_STRING (attr), ':');
12423   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12424     return "";
12425
12426   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12427                        &cu->objfile->objfile_obstack);
12428 }
12429
12430 /* Return the name of the namespace/class that DIE is defined within,
12431    or "" if we can't tell.  The caller should not xfree the result.
12432
12433    For example, if we're within the method foo() in the following
12434    code:
12435
12436    namespace N {
12437      class C {
12438        void foo () {
12439        }
12440      };
12441    }
12442
12443    then determine_prefix on foo's die will return "N::C".  */
12444
12445 static char *
12446 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12447 {
12448   struct die_info *parent, *spec_die;
12449   struct dwarf2_cu *spec_cu;
12450   struct type *parent_type;
12451   char *retval;
12452
12453   if (cu->language != language_cplus && cu->language != language_java
12454       && cu->language != language_fortran)
12455     return "";
12456
12457   retval = anonymous_struct_prefix (die, cu);
12458   if (retval)
12459     return retval;
12460
12461   /* We have to be careful in the presence of DW_AT_specification.
12462      For example, with GCC 3.4, given the code
12463
12464      namespace N {
12465        void foo() {
12466          // Definition of N::foo.
12467        }
12468      }
12469
12470      then we'll have a tree of DIEs like this:
12471
12472      1: DW_TAG_compile_unit
12473        2: DW_TAG_namespace        // N
12474          3: DW_TAG_subprogram     // declaration of N::foo
12475        4: DW_TAG_subprogram       // definition of N::foo
12476             DW_AT_specification   // refers to die #3
12477
12478      Thus, when processing die #4, we have to pretend that we're in
12479      the context of its DW_AT_specification, namely the contex of die
12480      #3.  */
12481   spec_cu = cu;
12482   spec_die = die_specification (die, &spec_cu);
12483   if (spec_die == NULL)
12484     parent = die->parent;
12485   else
12486     {
12487       parent = spec_die->parent;
12488       cu = spec_cu;
12489     }
12490
12491   if (parent == NULL)
12492     return "";
12493   else if (parent->building_fullname)
12494     {
12495       const char *name;
12496       const char *parent_name;
12497
12498       /* It has been seen on RealView 2.2 built binaries,
12499          DW_TAG_template_type_param types actually _defined_ as
12500          children of the parent class:
12501
12502          enum E {};
12503          template class <class Enum> Class{};
12504          Class<enum E> class_e;
12505
12506          1: DW_TAG_class_type (Class)
12507            2: DW_TAG_enumeration_type (E)
12508              3: DW_TAG_enumerator (enum1:0)
12509              3: DW_TAG_enumerator (enum2:1)
12510              ...
12511            2: DW_TAG_template_type_param
12512               DW_AT_type  DW_FORM_ref_udata (E)
12513
12514          Besides being broken debug info, it can put GDB into an
12515          infinite loop.  Consider:
12516
12517          When we're building the full name for Class<E>, we'll start
12518          at Class, and go look over its template type parameters,
12519          finding E.  We'll then try to build the full name of E, and
12520          reach here.  We're now trying to build the full name of E,
12521          and look over the parent DIE for containing scope.  In the
12522          broken case, if we followed the parent DIE of E, we'd again
12523          find Class, and once again go look at its template type
12524          arguments, etc., etc.  Simply don't consider such parent die
12525          as source-level parent of this die (it can't be, the language
12526          doesn't allow it), and break the loop here.  */
12527       name = dwarf2_name (die, cu);
12528       parent_name = dwarf2_name (parent, cu);
12529       complaint (&symfile_complaints,
12530                  _("template param type '%s' defined within parent '%s'"),
12531                  name ? name : "<unknown>",
12532                  parent_name ? parent_name : "<unknown>");
12533       return "";
12534     }
12535   else
12536     switch (parent->tag)
12537       {
12538       case DW_TAG_namespace:
12539         parent_type = read_type_die (parent, cu);
12540         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12541            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12542            Work around this problem here.  */
12543         if (cu->language == language_cplus
12544             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12545           return "";
12546         /* We give a name to even anonymous namespaces.  */
12547         return TYPE_TAG_NAME (parent_type);
12548       case DW_TAG_class_type:
12549       case DW_TAG_interface_type:
12550       case DW_TAG_structure_type:
12551       case DW_TAG_union_type:
12552       case DW_TAG_module:
12553         parent_type = read_type_die (parent, cu);
12554         if (TYPE_TAG_NAME (parent_type) != NULL)
12555           return TYPE_TAG_NAME (parent_type);
12556         else
12557           /* An anonymous structure is only allowed non-static data
12558              members; no typedefs, no member functions, et cetera.
12559              So it does not need a prefix.  */
12560           return "";
12561       case DW_TAG_compile_unit:
12562         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12563         if (cu->language == language_cplus
12564             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12565             && die->child != NULL
12566             && (die->tag == DW_TAG_class_type
12567                 || die->tag == DW_TAG_structure_type
12568                 || die->tag == DW_TAG_union_type))
12569           {
12570             char *name = guess_full_die_structure_name (die, cu);
12571             if (name != NULL)
12572               return name;
12573           }
12574         return "";
12575       default:
12576         return determine_prefix (parent, cu);
12577       }
12578 }
12579
12580 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12581    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12582    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12583    an obconcat, otherwise allocate storage for the result.  The CU argument is
12584    used to determine the language and hence, the appropriate separator.  */
12585
12586 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12587
12588 static char *
12589 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12590                  int physname, struct dwarf2_cu *cu)
12591 {
12592   const char *lead = "";
12593   const char *sep;
12594
12595   if (suffix == NULL || suffix[0] == '\0'
12596       || prefix == NULL || prefix[0] == '\0')
12597     sep = "";
12598   else if (cu->language == language_java)
12599     sep = ".";
12600   else if (cu->language == language_fortran && physname)
12601     {
12602       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12603          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12604
12605       lead = "__";
12606       sep = "_MOD_";
12607     }
12608   else
12609     sep = "::";
12610
12611   if (prefix == NULL)
12612     prefix = "";
12613   if (suffix == NULL)
12614     suffix = "";
12615
12616   if (obs == NULL)
12617     {
12618       char *retval
12619         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12620
12621       strcpy (retval, lead);
12622       strcat (retval, prefix);
12623       strcat (retval, sep);
12624       strcat (retval, suffix);
12625       return retval;
12626     }
12627   else
12628     {
12629       /* We have an obstack.  */
12630       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12631     }
12632 }
12633
12634 /* Return sibling of die, NULL if no sibling.  */
12635
12636 static struct die_info *
12637 sibling_die (struct die_info *die)
12638 {
12639   return die->sibling;
12640 }
12641
12642 /* Get name of a die, return NULL if not found.  */
12643
12644 static char *
12645 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12646                           struct obstack *obstack)
12647 {
12648   if (name && cu->language == language_cplus)
12649     {
12650       char *canon_name = cp_canonicalize_string (name);
12651
12652       if (canon_name != NULL)
12653         {
12654           if (strcmp (canon_name, name) != 0)
12655             name = obsavestring (canon_name, strlen (canon_name),
12656                                  obstack);
12657           xfree (canon_name);
12658         }
12659     }
12660
12661   return name;
12662 }
12663
12664 /* Get name of a die, return NULL if not found.  */
12665
12666 static char *
12667 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12668 {
12669   struct attribute *attr;
12670
12671   attr = dwarf2_attr (die, DW_AT_name, cu);
12672   if ((!attr || !DW_STRING (attr))
12673       && die->tag != DW_TAG_class_type
12674       && die->tag != DW_TAG_interface_type
12675       && die->tag != DW_TAG_structure_type
12676       && die->tag != DW_TAG_union_type)
12677     return NULL;
12678
12679   switch (die->tag)
12680     {
12681     case DW_TAG_compile_unit:
12682       /* Compilation units have a DW_AT_name that is a filename, not
12683          a source language identifier.  */
12684     case DW_TAG_enumeration_type:
12685     case DW_TAG_enumerator:
12686       /* These tags always have simple identifiers already; no need
12687          to canonicalize them.  */
12688       return DW_STRING (attr);
12689
12690     case DW_TAG_subprogram:
12691       /* Java constructors will all be named "<init>", so return
12692          the class name when we see this special case.  */
12693       if (cu->language == language_java
12694           && DW_STRING (attr) != NULL
12695           && strcmp (DW_STRING (attr), "<init>") == 0)
12696         {
12697           struct dwarf2_cu *spec_cu = cu;
12698           struct die_info *spec_die;
12699
12700           /* GCJ will output '<init>' for Java constructor names.
12701              For this special case, return the name of the parent class.  */
12702
12703           /* GCJ may output suprogram DIEs with AT_specification set.
12704              If so, use the name of the specified DIE.  */
12705           spec_die = die_specification (die, &spec_cu);
12706           if (spec_die != NULL)
12707             return dwarf2_name (spec_die, spec_cu);
12708
12709           do
12710             {
12711               die = die->parent;
12712               if (die->tag == DW_TAG_class_type)
12713                 return dwarf2_name (die, cu);
12714             }
12715           while (die->tag != DW_TAG_compile_unit);
12716         }
12717       break;
12718
12719     case DW_TAG_class_type:
12720     case DW_TAG_interface_type:
12721     case DW_TAG_structure_type:
12722     case DW_TAG_union_type:
12723       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12724          structures or unions.  These were of the form "._%d" in GCC 4.1,
12725          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12726          and GCC 4.4.  We work around this problem by ignoring these.  */
12727       if (attr && DW_STRING (attr)
12728           && (strncmp (DW_STRING (attr), "._", 2) == 0
12729               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12730         return NULL;
12731
12732       /* GCC might emit a nameless typedef that has a linkage name.  See
12733          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12734       if (!attr || DW_STRING (attr) == NULL)
12735         {
12736           char *demangled = NULL;
12737
12738           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12739           if (attr == NULL)
12740             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12741
12742           if (attr == NULL || DW_STRING (attr) == NULL)
12743             return NULL;
12744
12745           /* Avoid demangling DW_STRING (attr) the second time on a second
12746              call for the same DIE.  */
12747           if (!DW_STRING_IS_CANONICAL (attr))
12748             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12749
12750           if (demangled)
12751             {
12752               char *base;
12753
12754               /* FIXME: we already did this for the partial symbol... */
12755               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12756                                                &cu->objfile->objfile_obstack);
12757               DW_STRING_IS_CANONICAL (attr) = 1;
12758               xfree (demangled);
12759
12760               /* Strip any leading namespaces/classes, keep only the base name.
12761                  DW_AT_name for named DIEs does not contain the prefixes.  */
12762               base = strrchr (DW_STRING (attr), ':');
12763               if (base && base > DW_STRING (attr) && base[-1] == ':')
12764                 return &base[1];
12765               else
12766                 return DW_STRING (attr);
12767             }
12768         }
12769       break;
12770
12771     default:
12772       break;
12773     }
12774
12775   if (!DW_STRING_IS_CANONICAL (attr))
12776     {
12777       DW_STRING (attr)
12778         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12779                                     &cu->objfile->objfile_obstack);
12780       DW_STRING_IS_CANONICAL (attr) = 1;
12781     }
12782   return DW_STRING (attr);
12783 }
12784
12785 /* Return the die that this die in an extension of, or NULL if there
12786    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12787    containing the return value on output.  */
12788
12789 static struct die_info *
12790 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12791 {
12792   struct attribute *attr;
12793
12794   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12795   if (attr == NULL)
12796     return NULL;
12797
12798   return follow_die_ref (die, attr, ext_cu);
12799 }
12800
12801 /* Convert a DIE tag into its string name.  */
12802
12803 static char *
12804 dwarf_tag_name (unsigned tag)
12805 {
12806   switch (tag)
12807     {
12808     case DW_TAG_padding:
12809       return "DW_TAG_padding";
12810     case DW_TAG_array_type:
12811       return "DW_TAG_array_type";
12812     case DW_TAG_class_type:
12813       return "DW_TAG_class_type";
12814     case DW_TAG_entry_point:
12815       return "DW_TAG_entry_point";
12816     case DW_TAG_enumeration_type:
12817       return "DW_TAG_enumeration_type";
12818     case DW_TAG_formal_parameter:
12819       return "DW_TAG_formal_parameter";
12820     case DW_TAG_imported_declaration:
12821       return "DW_TAG_imported_declaration";
12822     case DW_TAG_label:
12823       return "DW_TAG_label";
12824     case DW_TAG_lexical_block:
12825       return "DW_TAG_lexical_block";
12826     case DW_TAG_member:
12827       return "DW_TAG_member";
12828     case DW_TAG_pointer_type:
12829       return "DW_TAG_pointer_type";
12830     case DW_TAG_reference_type:
12831       return "DW_TAG_reference_type";
12832     case DW_TAG_compile_unit:
12833       return "DW_TAG_compile_unit";
12834     case DW_TAG_string_type:
12835       return "DW_TAG_string_type";
12836     case DW_TAG_structure_type:
12837       return "DW_TAG_structure_type";
12838     case DW_TAG_subroutine_type:
12839       return "DW_TAG_subroutine_type";
12840     case DW_TAG_typedef:
12841       return "DW_TAG_typedef";
12842     case DW_TAG_union_type:
12843       return "DW_TAG_union_type";
12844     case DW_TAG_unspecified_parameters:
12845       return "DW_TAG_unspecified_parameters";
12846     case DW_TAG_variant:
12847       return "DW_TAG_variant";
12848     case DW_TAG_common_block:
12849       return "DW_TAG_common_block";
12850     case DW_TAG_common_inclusion:
12851       return "DW_TAG_common_inclusion";
12852     case DW_TAG_inheritance:
12853       return "DW_TAG_inheritance";
12854     case DW_TAG_inlined_subroutine:
12855       return "DW_TAG_inlined_subroutine";
12856     case DW_TAG_module:
12857       return "DW_TAG_module";
12858     case DW_TAG_ptr_to_member_type:
12859       return "DW_TAG_ptr_to_member_type";
12860     case DW_TAG_set_type:
12861       return "DW_TAG_set_type";
12862     case DW_TAG_subrange_type:
12863       return "DW_TAG_subrange_type";
12864     case DW_TAG_with_stmt:
12865       return "DW_TAG_with_stmt";
12866     case DW_TAG_access_declaration:
12867       return "DW_TAG_access_declaration";
12868     case DW_TAG_base_type:
12869       return "DW_TAG_base_type";
12870     case DW_TAG_catch_block:
12871       return "DW_TAG_catch_block";
12872     case DW_TAG_const_type:
12873       return "DW_TAG_const_type";
12874     case DW_TAG_constant:
12875       return "DW_TAG_constant";
12876     case DW_TAG_enumerator:
12877       return "DW_TAG_enumerator";
12878     case DW_TAG_file_type:
12879       return "DW_TAG_file_type";
12880     case DW_TAG_friend:
12881       return "DW_TAG_friend";
12882     case DW_TAG_namelist:
12883       return "DW_TAG_namelist";
12884     case DW_TAG_namelist_item:
12885       return "DW_TAG_namelist_item";
12886     case DW_TAG_packed_type:
12887       return "DW_TAG_packed_type";
12888     case DW_TAG_subprogram:
12889       return "DW_TAG_subprogram";
12890     case DW_TAG_template_type_param:
12891       return "DW_TAG_template_type_param";
12892     case DW_TAG_template_value_param:
12893       return "DW_TAG_template_value_param";
12894     case DW_TAG_thrown_type:
12895       return "DW_TAG_thrown_type";
12896     case DW_TAG_try_block:
12897       return "DW_TAG_try_block";
12898     case DW_TAG_variant_part:
12899       return "DW_TAG_variant_part";
12900     case DW_TAG_variable:
12901       return "DW_TAG_variable";
12902     case DW_TAG_volatile_type:
12903       return "DW_TAG_volatile_type";
12904     case DW_TAG_dwarf_procedure:
12905       return "DW_TAG_dwarf_procedure";
12906     case DW_TAG_restrict_type:
12907       return "DW_TAG_restrict_type";
12908     case DW_TAG_interface_type:
12909       return "DW_TAG_interface_type";
12910     case DW_TAG_namespace:
12911       return "DW_TAG_namespace";
12912     case DW_TAG_imported_module:
12913       return "DW_TAG_imported_module";
12914     case DW_TAG_unspecified_type:
12915       return "DW_TAG_unspecified_type";
12916     case DW_TAG_partial_unit:
12917       return "DW_TAG_partial_unit";
12918     case DW_TAG_imported_unit:
12919       return "DW_TAG_imported_unit";
12920     case DW_TAG_condition:
12921       return "DW_TAG_condition";
12922     case DW_TAG_shared_type:
12923       return "DW_TAG_shared_type";
12924     case DW_TAG_type_unit:
12925       return "DW_TAG_type_unit";
12926     case DW_TAG_MIPS_loop:
12927       return "DW_TAG_MIPS_loop";
12928     case DW_TAG_HP_array_descriptor:
12929       return "DW_TAG_HP_array_descriptor";
12930     case DW_TAG_format_label:
12931       return "DW_TAG_format_label";
12932     case DW_TAG_function_template:
12933       return "DW_TAG_function_template";
12934     case DW_TAG_class_template:
12935       return "DW_TAG_class_template";
12936     case DW_TAG_GNU_BINCL:
12937       return "DW_TAG_GNU_BINCL";
12938     case DW_TAG_GNU_EINCL:
12939       return "DW_TAG_GNU_EINCL";
12940     case DW_TAG_upc_shared_type:
12941       return "DW_TAG_upc_shared_type";
12942     case DW_TAG_upc_strict_type:
12943       return "DW_TAG_upc_strict_type";
12944     case DW_TAG_upc_relaxed_type:
12945       return "DW_TAG_upc_relaxed_type";
12946     case DW_TAG_PGI_kanji_type:
12947       return "DW_TAG_PGI_kanji_type";
12948     case DW_TAG_PGI_interface_block:
12949       return "DW_TAG_PGI_interface_block";
12950     case DW_TAG_GNU_call_site:
12951       return "DW_TAG_GNU_call_site";
12952     default:
12953       return "DW_TAG_<unknown>";
12954     }
12955 }
12956
12957 /* Convert a DWARF attribute code into its string name.  */
12958
12959 static char *
12960 dwarf_attr_name (unsigned attr)
12961 {
12962   switch (attr)
12963     {
12964     case DW_AT_sibling:
12965       return "DW_AT_sibling";
12966     case DW_AT_location:
12967       return "DW_AT_location";
12968     case DW_AT_name:
12969       return "DW_AT_name";
12970     case DW_AT_ordering:
12971       return "DW_AT_ordering";
12972     case DW_AT_subscr_data:
12973       return "DW_AT_subscr_data";
12974     case DW_AT_byte_size:
12975       return "DW_AT_byte_size";
12976     case DW_AT_bit_offset:
12977       return "DW_AT_bit_offset";
12978     case DW_AT_bit_size:
12979       return "DW_AT_bit_size";
12980     case DW_AT_element_list:
12981       return "DW_AT_element_list";
12982     case DW_AT_stmt_list:
12983       return "DW_AT_stmt_list";
12984     case DW_AT_low_pc:
12985       return "DW_AT_low_pc";
12986     case DW_AT_high_pc:
12987       return "DW_AT_high_pc";
12988     case DW_AT_language:
12989       return "DW_AT_language";
12990     case DW_AT_member:
12991       return "DW_AT_member";
12992     case DW_AT_discr:
12993       return "DW_AT_discr";
12994     case DW_AT_discr_value:
12995       return "DW_AT_discr_value";
12996     case DW_AT_visibility:
12997       return "DW_AT_visibility";
12998     case DW_AT_import:
12999       return "DW_AT_import";
13000     case DW_AT_string_length:
13001       return "DW_AT_string_length";
13002     case DW_AT_common_reference:
13003       return "DW_AT_common_reference";
13004     case DW_AT_comp_dir:
13005       return "DW_AT_comp_dir";
13006     case DW_AT_const_value:
13007       return "DW_AT_const_value";
13008     case DW_AT_containing_type:
13009       return "DW_AT_containing_type";
13010     case DW_AT_default_value:
13011       return "DW_AT_default_value";
13012     case DW_AT_inline:
13013       return "DW_AT_inline";
13014     case DW_AT_is_optional:
13015       return "DW_AT_is_optional";
13016     case DW_AT_lower_bound:
13017       return "DW_AT_lower_bound";
13018     case DW_AT_producer:
13019       return "DW_AT_producer";
13020     case DW_AT_prototyped:
13021       return "DW_AT_prototyped";
13022     case DW_AT_return_addr:
13023       return "DW_AT_return_addr";
13024     case DW_AT_start_scope:
13025       return "DW_AT_start_scope";
13026     case DW_AT_bit_stride:
13027       return "DW_AT_bit_stride";
13028     case DW_AT_upper_bound:
13029       return "DW_AT_upper_bound";
13030     case DW_AT_abstract_origin:
13031       return "DW_AT_abstract_origin";
13032     case DW_AT_accessibility:
13033       return "DW_AT_accessibility";
13034     case DW_AT_address_class:
13035       return "DW_AT_address_class";
13036     case DW_AT_artificial:
13037       return "DW_AT_artificial";
13038     case DW_AT_base_types:
13039       return "DW_AT_base_types";
13040     case DW_AT_calling_convention:
13041       return "DW_AT_calling_convention";
13042     case DW_AT_count:
13043       return "DW_AT_count";
13044     case DW_AT_data_member_location:
13045       return "DW_AT_data_member_location";
13046     case DW_AT_decl_column:
13047       return "DW_AT_decl_column";
13048     case DW_AT_decl_file:
13049       return "DW_AT_decl_file";
13050     case DW_AT_decl_line:
13051       return "DW_AT_decl_line";
13052     case DW_AT_declaration:
13053       return "DW_AT_declaration";
13054     case DW_AT_discr_list:
13055       return "DW_AT_discr_list";
13056     case DW_AT_encoding:
13057       return "DW_AT_encoding";
13058     case DW_AT_external:
13059       return "DW_AT_external";
13060     case DW_AT_frame_base:
13061       return "DW_AT_frame_base";
13062     case DW_AT_friend:
13063       return "DW_AT_friend";
13064     case DW_AT_identifier_case:
13065       return "DW_AT_identifier_case";
13066     case DW_AT_macro_info:
13067       return "DW_AT_macro_info";
13068     case DW_AT_namelist_items:
13069       return "DW_AT_namelist_items";
13070     case DW_AT_priority:
13071       return "DW_AT_priority";
13072     case DW_AT_segment:
13073       return "DW_AT_segment";
13074     case DW_AT_specification:
13075       return "DW_AT_specification";
13076     case DW_AT_static_link:
13077       return "DW_AT_static_link";
13078     case DW_AT_type:
13079       return "DW_AT_type";
13080     case DW_AT_use_location:
13081       return "DW_AT_use_location";
13082     case DW_AT_variable_parameter:
13083       return "DW_AT_variable_parameter";
13084     case DW_AT_virtuality:
13085       return "DW_AT_virtuality";
13086     case DW_AT_vtable_elem_location:
13087       return "DW_AT_vtable_elem_location";
13088     /* DWARF 3 values.  */
13089     case DW_AT_allocated:
13090       return "DW_AT_allocated";
13091     case DW_AT_associated:
13092       return "DW_AT_associated";
13093     case DW_AT_data_location:
13094       return "DW_AT_data_location";
13095     case DW_AT_byte_stride:
13096       return "DW_AT_byte_stride";
13097     case DW_AT_entry_pc:
13098       return "DW_AT_entry_pc";
13099     case DW_AT_use_UTF8:
13100       return "DW_AT_use_UTF8";
13101     case DW_AT_extension:
13102       return "DW_AT_extension";
13103     case DW_AT_ranges:
13104       return "DW_AT_ranges";
13105     case DW_AT_trampoline:
13106       return "DW_AT_trampoline";
13107     case DW_AT_call_column:
13108       return "DW_AT_call_column";
13109     case DW_AT_call_file:
13110       return "DW_AT_call_file";
13111     case DW_AT_call_line:
13112       return "DW_AT_call_line";
13113     case DW_AT_description:
13114       return "DW_AT_description";
13115     case DW_AT_binary_scale:
13116       return "DW_AT_binary_scale";
13117     case DW_AT_decimal_scale:
13118       return "DW_AT_decimal_scale";
13119     case DW_AT_small:
13120       return "DW_AT_small";
13121     case DW_AT_decimal_sign:
13122       return "DW_AT_decimal_sign";
13123     case DW_AT_digit_count:
13124       return "DW_AT_digit_count";
13125     case DW_AT_picture_string:
13126       return "DW_AT_picture_string";
13127     case DW_AT_mutable:
13128       return "DW_AT_mutable";
13129     case DW_AT_threads_scaled:
13130       return "DW_AT_threads_scaled";
13131     case DW_AT_explicit:
13132       return "DW_AT_explicit";
13133     case DW_AT_object_pointer:
13134       return "DW_AT_object_pointer";
13135     case DW_AT_endianity:
13136       return "DW_AT_endianity";
13137     case DW_AT_elemental:
13138       return "DW_AT_elemental";
13139     case DW_AT_pure:
13140       return "DW_AT_pure";
13141     case DW_AT_recursive:
13142       return "DW_AT_recursive";
13143     /* DWARF 4 values.  */
13144     case DW_AT_signature:
13145       return "DW_AT_signature";
13146     case DW_AT_linkage_name:
13147       return "DW_AT_linkage_name";
13148     /* SGI/MIPS extensions.  */
13149 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13150     case DW_AT_MIPS_fde:
13151       return "DW_AT_MIPS_fde";
13152 #endif
13153     case DW_AT_MIPS_loop_begin:
13154       return "DW_AT_MIPS_loop_begin";
13155     case DW_AT_MIPS_tail_loop_begin:
13156       return "DW_AT_MIPS_tail_loop_begin";
13157     case DW_AT_MIPS_epilog_begin:
13158       return "DW_AT_MIPS_epilog_begin";
13159     case DW_AT_MIPS_loop_unroll_factor:
13160       return "DW_AT_MIPS_loop_unroll_factor";
13161     case DW_AT_MIPS_software_pipeline_depth:
13162       return "DW_AT_MIPS_software_pipeline_depth";
13163     case DW_AT_MIPS_linkage_name:
13164       return "DW_AT_MIPS_linkage_name";
13165     case DW_AT_MIPS_stride:
13166       return "DW_AT_MIPS_stride";
13167     case DW_AT_MIPS_abstract_name:
13168       return "DW_AT_MIPS_abstract_name";
13169     case DW_AT_MIPS_clone_origin:
13170       return "DW_AT_MIPS_clone_origin";
13171     case DW_AT_MIPS_has_inlines:
13172       return "DW_AT_MIPS_has_inlines";
13173     /* HP extensions.  */
13174 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13175     case DW_AT_HP_block_index:
13176       return "DW_AT_HP_block_index";
13177 #endif
13178     case DW_AT_HP_unmodifiable:
13179       return "DW_AT_HP_unmodifiable";
13180     case DW_AT_HP_actuals_stmt_list:
13181       return "DW_AT_HP_actuals_stmt_list";
13182     case DW_AT_HP_proc_per_section:
13183       return "DW_AT_HP_proc_per_section";
13184     case DW_AT_HP_raw_data_ptr:
13185       return "DW_AT_HP_raw_data_ptr";
13186     case DW_AT_HP_pass_by_reference:
13187       return "DW_AT_HP_pass_by_reference";
13188     case DW_AT_HP_opt_level:
13189       return "DW_AT_HP_opt_level";
13190     case DW_AT_HP_prof_version_id:
13191       return "DW_AT_HP_prof_version_id";
13192     case DW_AT_HP_opt_flags:
13193       return "DW_AT_HP_opt_flags";
13194     case DW_AT_HP_cold_region_low_pc:
13195       return "DW_AT_HP_cold_region_low_pc";
13196     case DW_AT_HP_cold_region_high_pc:
13197       return "DW_AT_HP_cold_region_high_pc";
13198     case DW_AT_HP_all_variables_modifiable:
13199       return "DW_AT_HP_all_variables_modifiable";
13200     case DW_AT_HP_linkage_name:
13201       return "DW_AT_HP_linkage_name";
13202     case DW_AT_HP_prof_flags:
13203       return "DW_AT_HP_prof_flags";
13204     /* GNU extensions.  */
13205     case DW_AT_sf_names:
13206       return "DW_AT_sf_names";
13207     case DW_AT_src_info:
13208       return "DW_AT_src_info";
13209     case DW_AT_mac_info:
13210       return "DW_AT_mac_info";
13211     case DW_AT_src_coords:
13212       return "DW_AT_src_coords";
13213     case DW_AT_body_begin:
13214       return "DW_AT_body_begin";
13215     case DW_AT_body_end:
13216       return "DW_AT_body_end";
13217     case DW_AT_GNU_vector:
13218       return "DW_AT_GNU_vector";
13219     case DW_AT_GNU_odr_signature:
13220       return "DW_AT_GNU_odr_signature";
13221     /* VMS extensions.  */
13222     case DW_AT_VMS_rtnbeg_pd_address:
13223       return "DW_AT_VMS_rtnbeg_pd_address";
13224     /* UPC extension.  */
13225     case DW_AT_upc_threads_scaled:
13226       return "DW_AT_upc_threads_scaled";
13227     /* PGI (STMicroelectronics) extensions.  */
13228     case DW_AT_PGI_lbase:
13229       return "DW_AT_PGI_lbase";
13230     case DW_AT_PGI_soffset:
13231       return "DW_AT_PGI_soffset";
13232     case DW_AT_PGI_lstride:
13233       return "DW_AT_PGI_lstride";
13234     default:
13235       return "DW_AT_<unknown>";
13236     }
13237 }
13238
13239 /* Convert a DWARF value form code into its string name.  */
13240
13241 static char *
13242 dwarf_form_name (unsigned form)
13243 {
13244   switch (form)
13245     {
13246     case DW_FORM_addr:
13247       return "DW_FORM_addr";
13248     case DW_FORM_block2:
13249       return "DW_FORM_block2";
13250     case DW_FORM_block4:
13251       return "DW_FORM_block4";
13252     case DW_FORM_data2:
13253       return "DW_FORM_data2";
13254     case DW_FORM_data4:
13255       return "DW_FORM_data4";
13256     case DW_FORM_data8:
13257       return "DW_FORM_data8";
13258     case DW_FORM_string:
13259       return "DW_FORM_string";
13260     case DW_FORM_block:
13261       return "DW_FORM_block";
13262     case DW_FORM_block1:
13263       return "DW_FORM_block1";
13264     case DW_FORM_data1:
13265       return "DW_FORM_data1";
13266     case DW_FORM_flag:
13267       return "DW_FORM_flag";
13268     case DW_FORM_sdata:
13269       return "DW_FORM_sdata";
13270     case DW_FORM_strp:
13271       return "DW_FORM_strp";
13272     case DW_FORM_udata:
13273       return "DW_FORM_udata";
13274     case DW_FORM_ref_addr:
13275       return "DW_FORM_ref_addr";
13276     case DW_FORM_ref1:
13277       return "DW_FORM_ref1";
13278     case DW_FORM_ref2:
13279       return "DW_FORM_ref2";
13280     case DW_FORM_ref4:
13281       return "DW_FORM_ref4";
13282     case DW_FORM_ref8:
13283       return "DW_FORM_ref8";
13284     case DW_FORM_ref_udata:
13285       return "DW_FORM_ref_udata";
13286     case DW_FORM_indirect:
13287       return "DW_FORM_indirect";
13288     case DW_FORM_sec_offset:
13289       return "DW_FORM_sec_offset";
13290     case DW_FORM_exprloc:
13291       return "DW_FORM_exprloc";
13292     case DW_FORM_flag_present:
13293       return "DW_FORM_flag_present";
13294     case DW_FORM_ref_sig8:
13295       return "DW_FORM_ref_sig8";
13296     default:
13297       return "DW_FORM_<unknown>";
13298     }
13299 }
13300
13301 /* Convert a DWARF stack opcode into its string name.  */
13302
13303 const char *
13304 dwarf_stack_op_name (unsigned op)
13305 {
13306   switch (op)
13307     {
13308     case DW_OP_addr:
13309       return "DW_OP_addr";
13310     case DW_OP_deref:
13311       return "DW_OP_deref";
13312     case DW_OP_const1u:
13313       return "DW_OP_const1u";
13314     case DW_OP_const1s:
13315       return "DW_OP_const1s";
13316     case DW_OP_const2u:
13317       return "DW_OP_const2u";
13318     case DW_OP_const2s:
13319       return "DW_OP_const2s";
13320     case DW_OP_const4u:
13321       return "DW_OP_const4u";
13322     case DW_OP_const4s:
13323       return "DW_OP_const4s";
13324     case DW_OP_const8u:
13325       return "DW_OP_const8u";
13326     case DW_OP_const8s:
13327       return "DW_OP_const8s";
13328     case DW_OP_constu:
13329       return "DW_OP_constu";
13330     case DW_OP_consts:
13331       return "DW_OP_consts";
13332     case DW_OP_dup:
13333       return "DW_OP_dup";
13334     case DW_OP_drop:
13335       return "DW_OP_drop";
13336     case DW_OP_over:
13337       return "DW_OP_over";
13338     case DW_OP_pick:
13339       return "DW_OP_pick";
13340     case DW_OP_swap:
13341       return "DW_OP_swap";
13342     case DW_OP_rot:
13343       return "DW_OP_rot";
13344     case DW_OP_xderef:
13345       return "DW_OP_xderef";
13346     case DW_OP_abs:
13347       return "DW_OP_abs";
13348     case DW_OP_and:
13349       return "DW_OP_and";
13350     case DW_OP_div:
13351       return "DW_OP_div";
13352     case DW_OP_minus:
13353       return "DW_OP_minus";
13354     case DW_OP_mod:
13355       return "DW_OP_mod";
13356     case DW_OP_mul:
13357       return "DW_OP_mul";
13358     case DW_OP_neg:
13359       return "DW_OP_neg";
13360     case DW_OP_not:
13361       return "DW_OP_not";
13362     case DW_OP_or:
13363       return "DW_OP_or";
13364     case DW_OP_plus:
13365       return "DW_OP_plus";
13366     case DW_OP_plus_uconst:
13367       return "DW_OP_plus_uconst";
13368     case DW_OP_shl:
13369       return "DW_OP_shl";
13370     case DW_OP_shr:
13371       return "DW_OP_shr";
13372     case DW_OP_shra:
13373       return "DW_OP_shra";
13374     case DW_OP_xor:
13375       return "DW_OP_xor";
13376     case DW_OP_bra:
13377       return "DW_OP_bra";
13378     case DW_OP_eq:
13379       return "DW_OP_eq";
13380     case DW_OP_ge:
13381       return "DW_OP_ge";
13382     case DW_OP_gt:
13383       return "DW_OP_gt";
13384     case DW_OP_le:
13385       return "DW_OP_le";
13386     case DW_OP_lt:
13387       return "DW_OP_lt";
13388     case DW_OP_ne:
13389       return "DW_OP_ne";
13390     case DW_OP_skip:
13391       return "DW_OP_skip";
13392     case DW_OP_lit0:
13393       return "DW_OP_lit0";
13394     case DW_OP_lit1:
13395       return "DW_OP_lit1";
13396     case DW_OP_lit2:
13397       return "DW_OP_lit2";
13398     case DW_OP_lit3:
13399       return "DW_OP_lit3";
13400     case DW_OP_lit4:
13401       return "DW_OP_lit4";
13402     case DW_OP_lit5:
13403       return "DW_OP_lit5";
13404     case DW_OP_lit6:
13405       return "DW_OP_lit6";
13406     case DW_OP_lit7:
13407       return "DW_OP_lit7";
13408     case DW_OP_lit8:
13409       return "DW_OP_lit8";
13410     case DW_OP_lit9:
13411       return "DW_OP_lit9";
13412     case DW_OP_lit10:
13413       return "DW_OP_lit10";
13414     case DW_OP_lit11:
13415       return "DW_OP_lit11";
13416     case DW_OP_lit12:
13417       return "DW_OP_lit12";
13418     case DW_OP_lit13:
13419       return "DW_OP_lit13";
13420     case DW_OP_lit14:
13421       return "DW_OP_lit14";
13422     case DW_OP_lit15:
13423       return "DW_OP_lit15";
13424     case DW_OP_lit16:
13425       return "DW_OP_lit16";
13426     case DW_OP_lit17:
13427       return "DW_OP_lit17";
13428     case DW_OP_lit18:
13429       return "DW_OP_lit18";
13430     case DW_OP_lit19:
13431       return "DW_OP_lit19";
13432     case DW_OP_lit20:
13433       return "DW_OP_lit20";
13434     case DW_OP_lit21:
13435       return "DW_OP_lit21";
13436     case DW_OP_lit22:
13437       return "DW_OP_lit22";
13438     case DW_OP_lit23:
13439       return "DW_OP_lit23";
13440     case DW_OP_lit24:
13441       return "DW_OP_lit24";
13442     case DW_OP_lit25:
13443       return "DW_OP_lit25";
13444     case DW_OP_lit26:
13445       return "DW_OP_lit26";
13446     case DW_OP_lit27:
13447       return "DW_OP_lit27";
13448     case DW_OP_lit28:
13449       return "DW_OP_lit28";
13450     case DW_OP_lit29:
13451       return "DW_OP_lit29";
13452     case DW_OP_lit30:
13453       return "DW_OP_lit30";
13454     case DW_OP_lit31:
13455       return "DW_OP_lit31";
13456     case DW_OP_reg0:
13457       return "DW_OP_reg0";
13458     case DW_OP_reg1:
13459       return "DW_OP_reg1";
13460     case DW_OP_reg2:
13461       return "DW_OP_reg2";
13462     case DW_OP_reg3:
13463       return "DW_OP_reg3";
13464     case DW_OP_reg4:
13465       return "DW_OP_reg4";
13466     case DW_OP_reg5:
13467       return "DW_OP_reg5";
13468     case DW_OP_reg6:
13469       return "DW_OP_reg6";
13470     case DW_OP_reg7:
13471       return "DW_OP_reg7";
13472     case DW_OP_reg8:
13473       return "DW_OP_reg8";
13474     case DW_OP_reg9:
13475       return "DW_OP_reg9";
13476     case DW_OP_reg10:
13477       return "DW_OP_reg10";
13478     case DW_OP_reg11:
13479       return "DW_OP_reg11";
13480     case DW_OP_reg12:
13481       return "DW_OP_reg12";
13482     case DW_OP_reg13:
13483       return "DW_OP_reg13";
13484     case DW_OP_reg14:
13485       return "DW_OP_reg14";
13486     case DW_OP_reg15:
13487       return "DW_OP_reg15";
13488     case DW_OP_reg16:
13489       return "DW_OP_reg16";
13490     case DW_OP_reg17:
13491       return "DW_OP_reg17";
13492     case DW_OP_reg18:
13493       return "DW_OP_reg18";
13494     case DW_OP_reg19:
13495       return "DW_OP_reg19";
13496     case DW_OP_reg20:
13497       return "DW_OP_reg20";
13498     case DW_OP_reg21:
13499       return "DW_OP_reg21";
13500     case DW_OP_reg22:
13501       return "DW_OP_reg22";
13502     case DW_OP_reg23:
13503       return "DW_OP_reg23";
13504     case DW_OP_reg24:
13505       return "DW_OP_reg24";
13506     case DW_OP_reg25:
13507       return "DW_OP_reg25";
13508     case DW_OP_reg26:
13509       return "DW_OP_reg26";
13510     case DW_OP_reg27:
13511       return "DW_OP_reg27";
13512     case DW_OP_reg28:
13513       return "DW_OP_reg28";
13514     case DW_OP_reg29:
13515       return "DW_OP_reg29";
13516     case DW_OP_reg30:
13517       return "DW_OP_reg30";
13518     case DW_OP_reg31:
13519       return "DW_OP_reg31";
13520     case DW_OP_breg0:
13521       return "DW_OP_breg0";
13522     case DW_OP_breg1:
13523       return "DW_OP_breg1";
13524     case DW_OP_breg2:
13525       return "DW_OP_breg2";
13526     case DW_OP_breg3:
13527       return "DW_OP_breg3";
13528     case DW_OP_breg4:
13529       return "DW_OP_breg4";
13530     case DW_OP_breg5:
13531       return "DW_OP_breg5";
13532     case DW_OP_breg6:
13533       return "DW_OP_breg6";
13534     case DW_OP_breg7:
13535       return "DW_OP_breg7";
13536     case DW_OP_breg8:
13537       return "DW_OP_breg8";
13538     case DW_OP_breg9:
13539       return "DW_OP_breg9";
13540     case DW_OP_breg10:
13541       return "DW_OP_breg10";
13542     case DW_OP_breg11:
13543       return "DW_OP_breg11";
13544     case DW_OP_breg12:
13545       return "DW_OP_breg12";
13546     case DW_OP_breg13:
13547       return "DW_OP_breg13";
13548     case DW_OP_breg14:
13549       return "DW_OP_breg14";
13550     case DW_OP_breg15:
13551       return "DW_OP_breg15";
13552     case DW_OP_breg16:
13553       return "DW_OP_breg16";
13554     case DW_OP_breg17:
13555       return "DW_OP_breg17";
13556     case DW_OP_breg18:
13557       return "DW_OP_breg18";
13558     case DW_OP_breg19:
13559       return "DW_OP_breg19";
13560     case DW_OP_breg20:
13561       return "DW_OP_breg20";
13562     case DW_OP_breg21:
13563       return "DW_OP_breg21";
13564     case DW_OP_breg22:
13565       return "DW_OP_breg22";
13566     case DW_OP_breg23:
13567       return "DW_OP_breg23";
13568     case DW_OP_breg24:
13569       return "DW_OP_breg24";
13570     case DW_OP_breg25:
13571       return "DW_OP_breg25";
13572     case DW_OP_breg26:
13573       return "DW_OP_breg26";
13574     case DW_OP_breg27:
13575       return "DW_OP_breg27";
13576     case DW_OP_breg28:
13577       return "DW_OP_breg28";
13578     case DW_OP_breg29:
13579       return "DW_OP_breg29";
13580     case DW_OP_breg30:
13581       return "DW_OP_breg30";
13582     case DW_OP_breg31:
13583       return "DW_OP_breg31";
13584     case DW_OP_regx:
13585       return "DW_OP_regx";
13586     case DW_OP_fbreg:
13587       return "DW_OP_fbreg";
13588     case DW_OP_bregx:
13589       return "DW_OP_bregx";
13590     case DW_OP_piece:
13591       return "DW_OP_piece";
13592     case DW_OP_deref_size:
13593       return "DW_OP_deref_size";
13594     case DW_OP_xderef_size:
13595       return "DW_OP_xderef_size";
13596     case DW_OP_nop:
13597       return "DW_OP_nop";
13598     /* DWARF 3 extensions.  */
13599     case DW_OP_push_object_address:
13600       return "DW_OP_push_object_address";
13601     case DW_OP_call2:
13602       return "DW_OP_call2";
13603     case DW_OP_call4:
13604       return "DW_OP_call4";
13605     case DW_OP_call_ref:
13606       return "DW_OP_call_ref";
13607     case DW_OP_form_tls_address:
13608       return "DW_OP_form_tls_address";
13609     case DW_OP_call_frame_cfa:
13610       return "DW_OP_call_frame_cfa";
13611     case DW_OP_bit_piece:
13612       return "DW_OP_bit_piece";
13613     /* DWARF 4 extensions.  */
13614     case DW_OP_implicit_value:
13615       return "DW_OP_implicit_value";
13616     case DW_OP_stack_value:
13617       return "DW_OP_stack_value";
13618     /* GNU extensions.  */
13619     case DW_OP_GNU_push_tls_address:
13620       return "DW_OP_GNU_push_tls_address";
13621     case DW_OP_GNU_uninit:
13622       return "DW_OP_GNU_uninit";
13623     case DW_OP_GNU_implicit_pointer:
13624       return "DW_OP_GNU_implicit_pointer";
13625     case DW_OP_GNU_entry_value:
13626       return "DW_OP_GNU_entry_value";
13627     case DW_OP_GNU_const_type:
13628       return "DW_OP_GNU_const_type";
13629     case DW_OP_GNU_regval_type:
13630       return "DW_OP_GNU_regval_type";
13631     case DW_OP_GNU_deref_type:
13632       return "DW_OP_GNU_deref_type";
13633     case DW_OP_GNU_convert:
13634       return "DW_OP_GNU_convert";
13635     case DW_OP_GNU_reinterpret:
13636       return "DW_OP_GNU_reinterpret";
13637     default:
13638       return NULL;
13639     }
13640 }
13641
13642 static char *
13643 dwarf_bool_name (unsigned mybool)
13644 {
13645   if (mybool)
13646     return "TRUE";
13647   else
13648     return "FALSE";
13649 }
13650
13651 /* Convert a DWARF type code into its string name.  */
13652
13653 static char *
13654 dwarf_type_encoding_name (unsigned enc)
13655 {
13656   switch (enc)
13657     {
13658     case DW_ATE_void:
13659       return "DW_ATE_void";
13660     case DW_ATE_address:
13661       return "DW_ATE_address";
13662     case DW_ATE_boolean:
13663       return "DW_ATE_boolean";
13664     case DW_ATE_complex_float:
13665       return "DW_ATE_complex_float";
13666     case DW_ATE_float:
13667       return "DW_ATE_float";
13668     case DW_ATE_signed:
13669       return "DW_ATE_signed";
13670     case DW_ATE_signed_char:
13671       return "DW_ATE_signed_char";
13672     case DW_ATE_unsigned:
13673       return "DW_ATE_unsigned";
13674     case DW_ATE_unsigned_char:
13675       return "DW_ATE_unsigned_char";
13676     /* DWARF 3.  */
13677     case DW_ATE_imaginary_float:
13678       return "DW_ATE_imaginary_float";
13679     case DW_ATE_packed_decimal:
13680       return "DW_ATE_packed_decimal";
13681     case DW_ATE_numeric_string:
13682       return "DW_ATE_numeric_string";
13683     case DW_ATE_edited:
13684       return "DW_ATE_edited";
13685     case DW_ATE_signed_fixed:
13686       return "DW_ATE_signed_fixed";
13687     case DW_ATE_unsigned_fixed:
13688       return "DW_ATE_unsigned_fixed";
13689     case DW_ATE_decimal_float:
13690       return "DW_ATE_decimal_float";
13691     /* DWARF 4.  */
13692     case DW_ATE_UTF:
13693       return "DW_ATE_UTF";
13694     /* HP extensions.  */
13695     case DW_ATE_HP_float80:
13696       return "DW_ATE_HP_float80";
13697     case DW_ATE_HP_complex_float80:
13698       return "DW_ATE_HP_complex_float80";
13699     case DW_ATE_HP_float128:
13700       return "DW_ATE_HP_float128";
13701     case DW_ATE_HP_complex_float128:
13702       return "DW_ATE_HP_complex_float128";
13703     case DW_ATE_HP_floathpintel:
13704       return "DW_ATE_HP_floathpintel";
13705     case DW_ATE_HP_imaginary_float80:
13706       return "DW_ATE_HP_imaginary_float80";
13707     case DW_ATE_HP_imaginary_float128:
13708       return "DW_ATE_HP_imaginary_float128";
13709     default:
13710       return "DW_ATE_<unknown>";
13711     }
13712 }
13713
13714 /* Convert a DWARF call frame info operation to its string name.  */
13715
13716 #if 0
13717 static char *
13718 dwarf_cfi_name (unsigned cfi_opc)
13719 {
13720   switch (cfi_opc)
13721     {
13722     case DW_CFA_advance_loc:
13723       return "DW_CFA_advance_loc";
13724     case DW_CFA_offset:
13725       return "DW_CFA_offset";
13726     case DW_CFA_restore:
13727       return "DW_CFA_restore";
13728     case DW_CFA_nop:
13729       return "DW_CFA_nop";
13730     case DW_CFA_set_loc:
13731       return "DW_CFA_set_loc";
13732     case DW_CFA_advance_loc1:
13733       return "DW_CFA_advance_loc1";
13734     case DW_CFA_advance_loc2:
13735       return "DW_CFA_advance_loc2";
13736     case DW_CFA_advance_loc4:
13737       return "DW_CFA_advance_loc4";
13738     case DW_CFA_offset_extended:
13739       return "DW_CFA_offset_extended";
13740     case DW_CFA_restore_extended:
13741       return "DW_CFA_restore_extended";
13742     case DW_CFA_undefined:
13743       return "DW_CFA_undefined";
13744     case DW_CFA_same_value:
13745       return "DW_CFA_same_value";
13746     case DW_CFA_register:
13747       return "DW_CFA_register";
13748     case DW_CFA_remember_state:
13749       return "DW_CFA_remember_state";
13750     case DW_CFA_restore_state:
13751       return "DW_CFA_restore_state";
13752     case DW_CFA_def_cfa:
13753       return "DW_CFA_def_cfa";
13754     case DW_CFA_def_cfa_register:
13755       return "DW_CFA_def_cfa_register";
13756     case DW_CFA_def_cfa_offset:
13757       return "DW_CFA_def_cfa_offset";
13758     /* DWARF 3.  */
13759     case DW_CFA_def_cfa_expression:
13760       return "DW_CFA_def_cfa_expression";
13761     case DW_CFA_expression:
13762       return "DW_CFA_expression";
13763     case DW_CFA_offset_extended_sf:
13764       return "DW_CFA_offset_extended_sf";
13765     case DW_CFA_def_cfa_sf:
13766       return "DW_CFA_def_cfa_sf";
13767     case DW_CFA_def_cfa_offset_sf:
13768       return "DW_CFA_def_cfa_offset_sf";
13769     case DW_CFA_val_offset:
13770       return "DW_CFA_val_offset";
13771     case DW_CFA_val_offset_sf:
13772       return "DW_CFA_val_offset_sf";
13773     case DW_CFA_val_expression:
13774       return "DW_CFA_val_expression";
13775     /* SGI/MIPS specific.  */
13776     case DW_CFA_MIPS_advance_loc8:
13777       return "DW_CFA_MIPS_advance_loc8";
13778     /* GNU extensions.  */
13779     case DW_CFA_GNU_window_save:
13780       return "DW_CFA_GNU_window_save";
13781     case DW_CFA_GNU_args_size:
13782       return "DW_CFA_GNU_args_size";
13783     case DW_CFA_GNU_negative_offset_extended:
13784       return "DW_CFA_GNU_negative_offset_extended";
13785     default:
13786       return "DW_CFA_<unknown>";
13787     }
13788 }
13789 #endif
13790
13791 static void
13792 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13793 {
13794   unsigned int i;
13795
13796   print_spaces (indent, f);
13797   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13798            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13799
13800   if (die->parent != NULL)
13801     {
13802       print_spaces (indent, f);
13803       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13804                           die->parent->offset);
13805     }
13806
13807   print_spaces (indent, f);
13808   fprintf_unfiltered (f, "  has children: %s\n",
13809            dwarf_bool_name (die->child != NULL));
13810
13811   print_spaces (indent, f);
13812   fprintf_unfiltered (f, "  attributes:\n");
13813
13814   for (i = 0; i < die->num_attrs; ++i)
13815     {
13816       print_spaces (indent, f);
13817       fprintf_unfiltered (f, "    %s (%s) ",
13818                dwarf_attr_name (die->attrs[i].name),
13819                dwarf_form_name (die->attrs[i].form));
13820
13821       switch (die->attrs[i].form)
13822         {
13823         case DW_FORM_ref_addr:
13824         case DW_FORM_addr:
13825           fprintf_unfiltered (f, "address: ");
13826           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13827           break;
13828         case DW_FORM_block2:
13829         case DW_FORM_block4:
13830         case DW_FORM_block:
13831         case DW_FORM_block1:
13832           fprintf_unfiltered (f, "block: size %d",
13833                               DW_BLOCK (&die->attrs[i])->size);
13834           break;
13835         case DW_FORM_exprloc:
13836           fprintf_unfiltered (f, "expression: size %u",
13837                               DW_BLOCK (&die->attrs[i])->size);
13838           break;
13839         case DW_FORM_ref1:
13840         case DW_FORM_ref2:
13841         case DW_FORM_ref4:
13842           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13843                               (long) (DW_ADDR (&die->attrs[i])));
13844           break;
13845         case DW_FORM_data1:
13846         case DW_FORM_data2:
13847         case DW_FORM_data4:
13848         case DW_FORM_data8:
13849         case DW_FORM_udata:
13850         case DW_FORM_sdata:
13851           fprintf_unfiltered (f, "constant: %s",
13852                               pulongest (DW_UNSND (&die->attrs[i])));
13853           break;
13854         case DW_FORM_sec_offset:
13855           fprintf_unfiltered (f, "section offset: %s",
13856                               pulongest (DW_UNSND (&die->attrs[i])));
13857           break;
13858         case DW_FORM_ref_sig8:
13859           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13860             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13861                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13862           else
13863             fprintf_unfiltered (f, "signatured type, offset: unknown");
13864           break;
13865         case DW_FORM_string:
13866         case DW_FORM_strp:
13867           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13868                    DW_STRING (&die->attrs[i])
13869                    ? DW_STRING (&die->attrs[i]) : "",
13870                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13871           break;
13872         case DW_FORM_flag:
13873           if (DW_UNSND (&die->attrs[i]))
13874             fprintf_unfiltered (f, "flag: TRUE");
13875           else
13876             fprintf_unfiltered (f, "flag: FALSE");
13877           break;
13878         case DW_FORM_flag_present:
13879           fprintf_unfiltered (f, "flag: TRUE");
13880           break;
13881         case DW_FORM_indirect:
13882           /* The reader will have reduced the indirect form to
13883              the "base form" so this form should not occur.  */
13884           fprintf_unfiltered (f, 
13885                               "unexpected attribute form: DW_FORM_indirect");
13886           break;
13887         default:
13888           fprintf_unfiltered (f, "unsupported attribute form: %d.",
13889                    die->attrs[i].form);
13890           break;
13891         }
13892       fprintf_unfiltered (f, "\n");
13893     }
13894 }
13895
13896 static void
13897 dump_die_for_error (struct die_info *die)
13898 {
13899   dump_die_shallow (gdb_stderr, 0, die);
13900 }
13901
13902 static void
13903 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13904 {
13905   int indent = level * 4;
13906
13907   gdb_assert (die != NULL);
13908
13909   if (level >= max_level)
13910     return;
13911
13912   dump_die_shallow (f, indent, die);
13913
13914   if (die->child != NULL)
13915     {
13916       print_spaces (indent, f);
13917       fprintf_unfiltered (f, "  Children:");
13918       if (level + 1 < max_level)
13919         {
13920           fprintf_unfiltered (f, "\n");
13921           dump_die_1 (f, level + 1, max_level, die->child);
13922         }
13923       else
13924         {
13925           fprintf_unfiltered (f,
13926                               " [not printed, max nesting level reached]\n");
13927         }
13928     }
13929
13930   if (die->sibling != NULL && level > 0)
13931     {
13932       dump_die_1 (f, level, max_level, die->sibling);
13933     }
13934 }
13935
13936 /* This is called from the pdie macro in gdbinit.in.
13937    It's not static so gcc will keep a copy callable from gdb.  */
13938
13939 void
13940 dump_die (struct die_info *die, int max_level)
13941 {
13942   dump_die_1 (gdb_stdlog, 0, max_level, die);
13943 }
13944
13945 static void
13946 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13947 {
13948   void **slot;
13949
13950   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13951
13952   *slot = die;
13953 }
13954
13955 static int
13956 is_ref_attr (struct attribute *attr)
13957 {
13958   switch (attr->form)
13959     {
13960     case DW_FORM_ref_addr:
13961     case DW_FORM_ref1:
13962     case DW_FORM_ref2:
13963     case DW_FORM_ref4:
13964     case DW_FORM_ref8:
13965     case DW_FORM_ref_udata:
13966       return 1;
13967     default:
13968       return 0;
13969     }
13970 }
13971
13972 static unsigned int
13973 dwarf2_get_ref_die_offset (struct attribute *attr)
13974 {
13975   if (is_ref_attr (attr))
13976     return DW_ADDR (attr);
13977
13978   complaint (&symfile_complaints,
13979              _("unsupported die ref attribute form: '%s'"),
13980              dwarf_form_name (attr->form));
13981   return 0;
13982 }
13983
13984 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
13985  * the value held by the attribute is not constant.  */
13986
13987 static LONGEST
13988 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13989 {
13990   if (attr->form == DW_FORM_sdata)
13991     return DW_SND (attr);
13992   else if (attr->form == DW_FORM_udata
13993            || attr->form == DW_FORM_data1
13994            || attr->form == DW_FORM_data2
13995            || attr->form == DW_FORM_data4
13996            || attr->form == DW_FORM_data8)
13997     return DW_UNSND (attr);
13998   else
13999     {
14000       complaint (&symfile_complaints,
14001                  _("Attribute value is not a constant (%s)"),
14002                  dwarf_form_name (attr->form));
14003       return default_value;
14004     }
14005 }
14006
14007 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14008    unit and add it to our queue.
14009    The result is non-zero if PER_CU was queued, otherwise the result is zero
14010    meaning either PER_CU is already queued or it is already loaded.  */
14011
14012 static int
14013 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14014                        struct dwarf2_per_cu_data *per_cu)
14015 {
14016   /* We may arrive here during partial symbol reading, if we need full
14017      DIEs to process an unusual case (e.g. template arguments).  Do
14018      not queue PER_CU, just tell our caller to load its DIEs.  */
14019   if (dwarf2_per_objfile->reading_partial_symbols)
14020     {
14021       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14022         return 1;
14023       return 0;
14024     }
14025
14026   /* Mark the dependence relation so that we don't flush PER_CU
14027      too early.  */
14028   dwarf2_add_dependence (this_cu, per_cu);
14029
14030   /* If it's already on the queue, we have nothing to do.  */
14031   if (per_cu->queued)
14032     return 0;
14033
14034   /* If the compilation unit is already loaded, just mark it as
14035      used.  */
14036   if (per_cu->cu != NULL)
14037     {
14038       per_cu->cu->last_used = 0;
14039       return 0;
14040     }
14041
14042   /* Add it to the queue.  */
14043   queue_comp_unit (per_cu);
14044
14045   return 1;
14046 }
14047
14048 /* Follow reference or signature attribute ATTR of SRC_DIE.
14049    On entry *REF_CU is the CU of SRC_DIE.
14050    On exit *REF_CU is the CU of the result.  */
14051
14052 static struct die_info *
14053 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14054                        struct dwarf2_cu **ref_cu)
14055 {
14056   struct die_info *die;
14057
14058   if (is_ref_attr (attr))
14059     die = follow_die_ref (src_die, attr, ref_cu);
14060   else if (attr->form == DW_FORM_ref_sig8)
14061     die = follow_die_sig (src_die, attr, ref_cu);
14062   else
14063     {
14064       dump_die_for_error (src_die);
14065       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14066              (*ref_cu)->objfile->name);
14067     }
14068
14069   return die;
14070 }
14071
14072 /* Follow reference OFFSET.
14073    On entry *REF_CU is the CU of the source die referencing OFFSET.
14074    On exit *REF_CU is the CU of the result.
14075    Returns NULL if OFFSET is invalid.  */
14076
14077 static struct die_info *
14078 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14079 {
14080   struct die_info temp_die;
14081   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14082
14083   gdb_assert (cu->per_cu != NULL);
14084
14085   target_cu = cu;
14086
14087   if (cu->per_cu->debug_types_section)
14088     {
14089       /* .debug_types CUs cannot reference anything outside their CU.
14090          If they need to, they have to reference a signatured type via
14091          DW_FORM_ref_sig8.  */
14092       if (! offset_in_cu_p (&cu->header, offset))
14093         return NULL;
14094     }
14095   else if (! offset_in_cu_p (&cu->header, offset))
14096     {
14097       struct dwarf2_per_cu_data *per_cu;
14098
14099       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14100
14101       /* If necessary, add it to the queue and load its DIEs.  */
14102       if (maybe_queue_comp_unit (cu, per_cu))
14103         load_full_comp_unit (per_cu);
14104
14105       target_cu = per_cu->cu;
14106     }
14107   else if (cu->dies == NULL)
14108     {
14109       /* We're loading full DIEs during partial symbol reading.  */
14110       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14111       load_full_comp_unit (cu->per_cu);
14112     }
14113
14114   *ref_cu = target_cu;
14115   temp_die.offset = offset;
14116   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14117 }
14118
14119 /* Follow reference attribute ATTR of SRC_DIE.
14120    On entry *REF_CU is the CU of SRC_DIE.
14121    On exit *REF_CU is the CU of the result.  */
14122
14123 static struct die_info *
14124 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14125                 struct dwarf2_cu **ref_cu)
14126 {
14127   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14128   struct dwarf2_cu *cu = *ref_cu;
14129   struct die_info *die;
14130
14131   die = follow_die_offset (offset, ref_cu);
14132   if (!die)
14133     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14134            "at 0x%x [in module %s]"),
14135            offset, src_die->offset, cu->objfile->name);
14136
14137   return die;
14138 }
14139
14140 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14141    Returned value is intended for DW_OP_call*.  Returned
14142    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14143
14144 struct dwarf2_locexpr_baton
14145 dwarf2_fetch_die_location_block (unsigned int offset,
14146                                  struct dwarf2_per_cu_data *per_cu,
14147                                  CORE_ADDR (*get_frame_pc) (void *baton),
14148                                  void *baton)
14149 {
14150   struct dwarf2_cu *cu;
14151   struct die_info *die;
14152   struct attribute *attr;
14153   struct dwarf2_locexpr_baton retval;
14154
14155   dw2_setup (per_cu->objfile);
14156
14157   if (per_cu->cu == NULL)
14158     load_cu (per_cu);
14159   cu = per_cu->cu;
14160
14161   die = follow_die_offset (offset, &cu);
14162   if (!die)
14163     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14164            offset, per_cu->objfile->name);
14165
14166   attr = dwarf2_attr (die, DW_AT_location, cu);
14167   if (!attr)
14168     {
14169       /* DWARF: "If there is no such attribute, then there is no effect.".
14170          DATA is ignored if SIZE is 0.  */
14171
14172       retval.data = NULL;
14173       retval.size = 0;
14174     }
14175   else if (attr_form_is_section_offset (attr))
14176     {
14177       struct dwarf2_loclist_baton loclist_baton;
14178       CORE_ADDR pc = (*get_frame_pc) (baton);
14179       size_t size;
14180
14181       fill_in_loclist_baton (cu, &loclist_baton, attr);
14182
14183       retval.data = dwarf2_find_location_expression (&loclist_baton,
14184                                                      &size, pc);
14185       retval.size = size;
14186     }
14187   else
14188     {
14189       if (!attr_form_is_block (attr))
14190         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14191                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14192                offset, per_cu->objfile->name);
14193
14194       retval.data = DW_BLOCK (attr)->data;
14195       retval.size = DW_BLOCK (attr)->size;
14196     }
14197   retval.per_cu = cu->per_cu;
14198
14199   age_cached_comp_units ();
14200
14201   return retval;
14202 }
14203
14204 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14205    PER_CU.  */
14206
14207 struct type *
14208 dwarf2_get_die_type (unsigned int die_offset,
14209                      struct dwarf2_per_cu_data *per_cu)
14210 {
14211   dw2_setup (per_cu->objfile);
14212   return get_die_type_at_offset (die_offset, per_cu);
14213 }
14214
14215 /* Follow the signature attribute ATTR in SRC_DIE.
14216    On entry *REF_CU is the CU of SRC_DIE.
14217    On exit *REF_CU is the CU of the result.  */
14218
14219 static struct die_info *
14220 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14221                 struct dwarf2_cu **ref_cu)
14222 {
14223   struct objfile *objfile = (*ref_cu)->objfile;
14224   struct die_info temp_die;
14225   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14226   struct dwarf2_cu *sig_cu;
14227   struct die_info *die;
14228
14229   /* sig_type will be NULL if the signatured type is missing from
14230      the debug info.  */
14231   if (sig_type == NULL)
14232     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14233              "at 0x%x [in module %s]"),
14234            src_die->offset, objfile->name);
14235
14236   /* If necessary, add it to the queue and load its DIEs.  */
14237
14238   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14239     read_signatured_type (sig_type);
14240
14241   gdb_assert (sig_type->per_cu.cu != NULL);
14242
14243   sig_cu = sig_type->per_cu.cu;
14244   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14245   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14246   if (die)
14247     {
14248       *ref_cu = sig_cu;
14249       return die;
14250     }
14251
14252   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14253          "from DIE at 0x%x [in module %s]"),
14254          sig_type->type_offset, src_die->offset, objfile->name);
14255 }
14256
14257 /* Given an offset of a signatured type, return its signatured_type.  */
14258
14259 static struct signatured_type *
14260 lookup_signatured_type_at_offset (struct objfile *objfile,
14261                                   struct dwarf2_section_info *section,
14262                                   unsigned int offset)
14263 {
14264   gdb_byte *info_ptr = section->buffer + offset;
14265   unsigned int length, initial_length_size;
14266   unsigned int sig_offset;
14267   struct signatured_type find_entry, *type_sig;
14268
14269   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14270   sig_offset = (initial_length_size
14271                 + 2 /*version*/
14272                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14273                 + 1 /*address_size*/);
14274   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14275   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14276
14277   /* This is only used to lookup previously recorded types.
14278      If we didn't find it, it's our bug.  */
14279   gdb_assert (type_sig != NULL);
14280   gdb_assert (offset == type_sig->per_cu.offset);
14281
14282   return type_sig;
14283 }
14284
14285 /* Load the DIEs associated with type unit PER_CU into memory.  */
14286
14287 static void
14288 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14289 {
14290   struct objfile *objfile = per_cu->objfile;
14291   struct dwarf2_section_info *sect = per_cu->debug_types_section;
14292   unsigned int offset = per_cu->offset;
14293   struct signatured_type *type_sig;
14294
14295   dwarf2_read_section (objfile, sect);
14296
14297   /* We have the section offset, but we need the signature to do the
14298      hash table lookup.  */
14299   /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14300      the signature to assert we found the right one.
14301      Ok, but it's a lot of work.  We should simplify things so any needed
14302      assert doesn't require all this clumsiness.  */
14303   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14304
14305   gdb_assert (type_sig->per_cu.cu == NULL);
14306
14307   read_signatured_type (type_sig);
14308
14309   gdb_assert (type_sig->per_cu.cu != NULL);
14310 }
14311
14312 /* Read in a signatured type and build its CU and DIEs.  */
14313
14314 static void
14315 read_signatured_type (struct signatured_type *type_sig)
14316 {
14317   struct objfile *objfile = type_sig->per_cu.objfile;
14318   gdb_byte *types_ptr;
14319   struct die_reader_specs reader_specs;
14320   struct dwarf2_cu *cu;
14321   ULONGEST signature;
14322   struct cleanup *back_to, *free_cu_cleanup;
14323   struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14324
14325   dwarf2_read_section (objfile, section);
14326   types_ptr = section->buffer + type_sig->per_cu.offset;
14327
14328   gdb_assert (type_sig->per_cu.cu == NULL);
14329
14330   cu = xmalloc (sizeof (*cu));
14331   init_one_comp_unit (cu, &type_sig->per_cu);
14332
14333   /* If an error occurs while loading, release our storage.  */
14334   free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14335
14336   types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14337                                              &signature, NULL);
14338   gdb_assert (signature == type_sig->signature);
14339
14340   cu->die_hash
14341     = htab_create_alloc_ex (cu->header.length / 12,
14342                             die_hash,
14343                             die_eq,
14344                             NULL,
14345                             &cu->comp_unit_obstack,
14346                             hashtab_obstack_allocate,
14347                             dummy_obstack_deallocate);
14348
14349   dwarf2_read_abbrevs (cu);
14350   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14351
14352   init_cu_die_reader (&reader_specs, cu);
14353
14354   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14355                                     NULL /*parent*/);
14356
14357   /* We try not to read any attributes in this function, because not
14358      all CUs needed for references have been loaded yet, and symbol
14359      table processing isn't initialized.  But we have to set the CU language,
14360      or we won't be able to build types correctly.  */
14361   prepare_one_comp_unit (cu, cu->dies);
14362
14363   do_cleanups (back_to);
14364
14365   /* We've successfully allocated this compilation unit.  Let our caller
14366      clean it up when finished with it.  */
14367   discard_cleanups (free_cu_cleanup);
14368
14369   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14370   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14371 }
14372
14373 /* Decode simple location descriptions.
14374    Given a pointer to a dwarf block that defines a location, compute
14375    the location and return the value.
14376
14377    NOTE drow/2003-11-18: This function is called in two situations
14378    now: for the address of static or global variables (partial symbols
14379    only) and for offsets into structures which are expected to be
14380    (more or less) constant.  The partial symbol case should go away,
14381    and only the constant case should remain.  That will let this
14382    function complain more accurately.  A few special modes are allowed
14383    without complaint for global variables (for instance, global
14384    register values and thread-local values).
14385
14386    A location description containing no operations indicates that the
14387    object is optimized out.  The return value is 0 for that case.
14388    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14389    callers will only want a very basic result and this can become a
14390    complaint.
14391
14392    Note that stack[0] is unused except as a default error return.  */
14393
14394 static CORE_ADDR
14395 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14396 {
14397   struct objfile *objfile = cu->objfile;
14398   int i;
14399   int size = blk->size;
14400   gdb_byte *data = blk->data;
14401   CORE_ADDR stack[64];
14402   int stacki;
14403   unsigned int bytes_read, unsnd;
14404   gdb_byte op;
14405
14406   i = 0;
14407   stacki = 0;
14408   stack[stacki] = 0;
14409   stack[++stacki] = 0;
14410
14411   while (i < size)
14412     {
14413       op = data[i++];
14414       switch (op)
14415         {
14416         case DW_OP_lit0:
14417         case DW_OP_lit1:
14418         case DW_OP_lit2:
14419         case DW_OP_lit3:
14420         case DW_OP_lit4:
14421         case DW_OP_lit5:
14422         case DW_OP_lit6:
14423         case DW_OP_lit7:
14424         case DW_OP_lit8:
14425         case DW_OP_lit9:
14426         case DW_OP_lit10:
14427         case DW_OP_lit11:
14428         case DW_OP_lit12:
14429         case DW_OP_lit13:
14430         case DW_OP_lit14:
14431         case DW_OP_lit15:
14432         case DW_OP_lit16:
14433         case DW_OP_lit17:
14434         case DW_OP_lit18:
14435         case DW_OP_lit19:
14436         case DW_OP_lit20:
14437         case DW_OP_lit21:
14438         case DW_OP_lit22:
14439         case DW_OP_lit23:
14440         case DW_OP_lit24:
14441         case DW_OP_lit25:
14442         case DW_OP_lit26:
14443         case DW_OP_lit27:
14444         case DW_OP_lit28:
14445         case DW_OP_lit29:
14446         case DW_OP_lit30:
14447         case DW_OP_lit31:
14448           stack[++stacki] = op - DW_OP_lit0;
14449           break;
14450
14451         case DW_OP_reg0:
14452         case DW_OP_reg1:
14453         case DW_OP_reg2:
14454         case DW_OP_reg3:
14455         case DW_OP_reg4:
14456         case DW_OP_reg5:
14457         case DW_OP_reg6:
14458         case DW_OP_reg7:
14459         case DW_OP_reg8:
14460         case DW_OP_reg9:
14461         case DW_OP_reg10:
14462         case DW_OP_reg11:
14463         case DW_OP_reg12:
14464         case DW_OP_reg13:
14465         case DW_OP_reg14:
14466         case DW_OP_reg15:
14467         case DW_OP_reg16:
14468         case DW_OP_reg17:
14469         case DW_OP_reg18:
14470         case DW_OP_reg19:
14471         case DW_OP_reg20:
14472         case DW_OP_reg21:
14473         case DW_OP_reg22:
14474         case DW_OP_reg23:
14475         case DW_OP_reg24:
14476         case DW_OP_reg25:
14477         case DW_OP_reg26:
14478         case DW_OP_reg27:
14479         case DW_OP_reg28:
14480         case DW_OP_reg29:
14481         case DW_OP_reg30:
14482         case DW_OP_reg31:
14483           stack[++stacki] = op - DW_OP_reg0;
14484           if (i < size)
14485             dwarf2_complex_location_expr_complaint ();
14486           break;
14487
14488         case DW_OP_regx:
14489           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14490           i += bytes_read;
14491           stack[++stacki] = unsnd;
14492           if (i < size)
14493             dwarf2_complex_location_expr_complaint ();
14494           break;
14495
14496         case DW_OP_addr:
14497           stack[++stacki] = read_address (objfile->obfd, &data[i],
14498                                           cu, &bytes_read);
14499           i += bytes_read;
14500           break;
14501
14502         case DW_OP_const1u:
14503           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14504           i += 1;
14505           break;
14506
14507         case DW_OP_const1s:
14508           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14509           i += 1;
14510           break;
14511
14512         case DW_OP_const2u:
14513           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14514           i += 2;
14515           break;
14516
14517         case DW_OP_const2s:
14518           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14519           i += 2;
14520           break;
14521
14522         case DW_OP_const4u:
14523           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14524           i += 4;
14525           break;
14526
14527         case DW_OP_const4s:
14528           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14529           i += 4;
14530           break;
14531
14532         case DW_OP_const8u:
14533           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14534           i += 8;
14535           break;
14536
14537         case DW_OP_constu:
14538           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14539                                                   &bytes_read);
14540           i += bytes_read;
14541           break;
14542
14543         case DW_OP_consts:
14544           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14545           i += bytes_read;
14546           break;
14547
14548         case DW_OP_dup:
14549           stack[stacki + 1] = stack[stacki];
14550           stacki++;
14551           break;
14552
14553         case DW_OP_plus:
14554           stack[stacki - 1] += stack[stacki];
14555           stacki--;
14556           break;
14557
14558         case DW_OP_plus_uconst:
14559           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14560                                                  &bytes_read);
14561           i += bytes_read;
14562           break;
14563
14564         case DW_OP_minus:
14565           stack[stacki - 1] -= stack[stacki];
14566           stacki--;
14567           break;
14568
14569         case DW_OP_deref:
14570           /* If we're not the last op, then we definitely can't encode
14571              this using GDB's address_class enum.  This is valid for partial
14572              global symbols, although the variable's address will be bogus
14573              in the psymtab.  */
14574           if (i < size)
14575             dwarf2_complex_location_expr_complaint ();
14576           break;
14577
14578         case DW_OP_GNU_push_tls_address:
14579           /* The top of the stack has the offset from the beginning
14580              of the thread control block at which the variable is located.  */
14581           /* Nothing should follow this operator, so the top of stack would
14582              be returned.  */
14583           /* This is valid for partial global symbols, but the variable's
14584              address will be bogus in the psymtab.  Make it always at least
14585              non-zero to not look as a variable garbage collected by linker
14586              which have DW_OP_addr 0.  */
14587           if (i < size)
14588             dwarf2_complex_location_expr_complaint ();
14589           stack[stacki]++;
14590           break;
14591
14592         case DW_OP_GNU_uninit:
14593           break;
14594
14595         default:
14596           {
14597             const char *name = dwarf_stack_op_name (op);
14598
14599             if (name)
14600               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14601                          name);
14602             else
14603               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14604                          op);
14605           }
14606
14607           return (stack[stacki]);
14608         }
14609
14610       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14611          outside of the allocated space.  Also enforce minimum>0.  */
14612       if (stacki >= ARRAY_SIZE (stack) - 1)
14613         {
14614           complaint (&symfile_complaints,
14615                      _("location description stack overflow"));
14616           return 0;
14617         }
14618
14619       if (stacki <= 0)
14620         {
14621           complaint (&symfile_complaints,
14622                      _("location description stack underflow"));
14623           return 0;
14624         }
14625     }
14626   return (stack[stacki]);
14627 }
14628
14629 /* memory allocation interface */
14630
14631 static struct dwarf_block *
14632 dwarf_alloc_block (struct dwarf2_cu *cu)
14633 {
14634   struct dwarf_block *blk;
14635
14636   blk = (struct dwarf_block *)
14637     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14638   return (blk);
14639 }
14640
14641 static struct abbrev_info *
14642 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14643 {
14644   struct abbrev_info *abbrev;
14645
14646   abbrev = (struct abbrev_info *)
14647     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14648   memset (abbrev, 0, sizeof (struct abbrev_info));
14649   return (abbrev);
14650 }
14651
14652 static struct die_info *
14653 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14654 {
14655   struct die_info *die;
14656   size_t size = sizeof (struct die_info);
14657
14658   if (num_attrs > 1)
14659     size += (num_attrs - 1) * sizeof (struct attribute);
14660
14661   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14662   memset (die, 0, sizeof (struct die_info));
14663   return (die);
14664 }
14665
14666 \f
14667 /* Macro support.  */
14668
14669 /* Return the full name of file number I in *LH's file name table.
14670    Use COMP_DIR as the name of the current directory of the
14671    compilation.  The result is allocated using xmalloc; the caller is
14672    responsible for freeing it.  */
14673 static char *
14674 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14675 {
14676   /* Is the file number a valid index into the line header's file name
14677      table?  Remember that file numbers start with one, not zero.  */
14678   if (1 <= file && file <= lh->num_file_names)
14679     {
14680       struct file_entry *fe = &lh->file_names[file - 1];
14681
14682       if (IS_ABSOLUTE_PATH (fe->name))
14683         return xstrdup (fe->name);
14684       else
14685         {
14686           const char *dir;
14687           int dir_len;
14688           char *full_name;
14689
14690           if (fe->dir_index)
14691             dir = lh->include_dirs[fe->dir_index - 1];
14692           else
14693             dir = comp_dir;
14694
14695           if (dir)
14696             {
14697               dir_len = strlen (dir);
14698               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14699               strcpy (full_name, dir);
14700               full_name[dir_len] = '/';
14701               strcpy (full_name + dir_len + 1, fe->name);
14702               return full_name;
14703             }
14704           else
14705             return xstrdup (fe->name);
14706         }
14707     }
14708   else
14709     {
14710       /* The compiler produced a bogus file number.  We can at least
14711          record the macro definitions made in the file, even if we
14712          won't be able to find the file by name.  */
14713       char fake_name[80];
14714
14715       sprintf (fake_name, "<bad macro file number %d>", file);
14716
14717       complaint (&symfile_complaints,
14718                  _("bad file number in macro information (%d)"),
14719                  file);
14720
14721       return xstrdup (fake_name);
14722     }
14723 }
14724
14725
14726 static struct macro_source_file *
14727 macro_start_file (int file, int line,
14728                   struct macro_source_file *current_file,
14729                   const char *comp_dir,
14730                   struct line_header *lh, struct objfile *objfile)
14731 {
14732   /* The full name of this source file.  */
14733   char *full_name = file_full_name (file, lh, comp_dir);
14734
14735   /* We don't create a macro table for this compilation unit
14736      at all until we actually get a filename.  */
14737   if (! pending_macros)
14738     pending_macros = new_macro_table (&objfile->objfile_obstack,
14739                                       objfile->macro_cache);
14740
14741   if (! current_file)
14742     /* If we have no current file, then this must be the start_file
14743        directive for the compilation unit's main source file.  */
14744     current_file = macro_set_main (pending_macros, full_name);
14745   else
14746     current_file = macro_include (current_file, line, full_name);
14747
14748   xfree (full_name);
14749
14750   return current_file;
14751 }
14752
14753
14754 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14755    followed by a null byte.  */
14756 static char *
14757 copy_string (const char *buf, int len)
14758 {
14759   char *s = xmalloc (len + 1);
14760
14761   memcpy (s, buf, len);
14762   s[len] = '\0';
14763   return s;
14764 }
14765
14766
14767 static const char *
14768 consume_improper_spaces (const char *p, const char *body)
14769 {
14770   if (*p == ' ')
14771     {
14772       complaint (&symfile_complaints,
14773                  _("macro definition contains spaces "
14774                    "in formal argument list:\n`%s'"),
14775                  body);
14776
14777       while (*p == ' ')
14778         p++;
14779     }
14780
14781   return p;
14782 }
14783
14784
14785 static void
14786 parse_macro_definition (struct macro_source_file *file, int line,
14787                         const char *body)
14788 {
14789   const char *p;
14790
14791   /* The body string takes one of two forms.  For object-like macro
14792      definitions, it should be:
14793
14794         <macro name> " " <definition>
14795
14796      For function-like macro definitions, it should be:
14797
14798         <macro name> "() " <definition>
14799      or
14800         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14801
14802      Spaces may appear only where explicitly indicated, and in the
14803      <definition>.
14804
14805      The Dwarf 2 spec says that an object-like macro's name is always
14806      followed by a space, but versions of GCC around March 2002 omit
14807      the space when the macro's definition is the empty string.
14808
14809      The Dwarf 2 spec says that there should be no spaces between the
14810      formal arguments in a function-like macro's formal argument list,
14811      but versions of GCC around March 2002 include spaces after the
14812      commas.  */
14813
14814
14815   /* Find the extent of the macro name.  The macro name is terminated
14816      by either a space or null character (for an object-like macro) or
14817      an opening paren (for a function-like macro).  */
14818   for (p = body; *p; p++)
14819     if (*p == ' ' || *p == '(')
14820       break;
14821
14822   if (*p == ' ' || *p == '\0')
14823     {
14824       /* It's an object-like macro.  */
14825       int name_len = p - body;
14826       char *name = copy_string (body, name_len);
14827       const char *replacement;
14828
14829       if (*p == ' ')
14830         replacement = body + name_len + 1;
14831       else
14832         {
14833           dwarf2_macro_malformed_definition_complaint (body);
14834           replacement = body + name_len;
14835         }
14836
14837       macro_define_object (file, line, name, replacement);
14838
14839       xfree (name);
14840     }
14841   else if (*p == '(')
14842     {
14843       /* It's a function-like macro.  */
14844       char *name = copy_string (body, p - body);
14845       int argc = 0;
14846       int argv_size = 1;
14847       char **argv = xmalloc (argv_size * sizeof (*argv));
14848
14849       p++;
14850
14851       p = consume_improper_spaces (p, body);
14852
14853       /* Parse the formal argument list.  */
14854       while (*p && *p != ')')
14855         {
14856           /* Find the extent of the current argument name.  */
14857           const char *arg_start = p;
14858
14859           while (*p && *p != ',' && *p != ')' && *p != ' ')
14860             p++;
14861
14862           if (! *p || p == arg_start)
14863             dwarf2_macro_malformed_definition_complaint (body);
14864           else
14865             {
14866               /* Make sure argv has room for the new argument.  */
14867               if (argc >= argv_size)
14868                 {
14869                   argv_size *= 2;
14870                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14871                 }
14872
14873               argv[argc++] = copy_string (arg_start, p - arg_start);
14874             }
14875
14876           p = consume_improper_spaces (p, body);
14877
14878           /* Consume the comma, if present.  */
14879           if (*p == ',')
14880             {
14881               p++;
14882
14883               p = consume_improper_spaces (p, body);
14884             }
14885         }
14886
14887       if (*p == ')')
14888         {
14889           p++;
14890
14891           if (*p == ' ')
14892             /* Perfectly formed definition, no complaints.  */
14893             macro_define_function (file, line, name,
14894                                    argc, (const char **) argv,
14895                                    p + 1);
14896           else if (*p == '\0')
14897             {
14898               /* Complain, but do define it.  */
14899               dwarf2_macro_malformed_definition_complaint (body);
14900               macro_define_function (file, line, name,
14901                                      argc, (const char **) argv,
14902                                      p);
14903             }
14904           else
14905             /* Just complain.  */
14906             dwarf2_macro_malformed_definition_complaint (body);
14907         }
14908       else
14909         /* Just complain.  */
14910         dwarf2_macro_malformed_definition_complaint (body);
14911
14912       xfree (name);
14913       {
14914         int i;
14915
14916         for (i = 0; i < argc; i++)
14917           xfree (argv[i]);
14918       }
14919       xfree (argv);
14920     }
14921   else
14922     dwarf2_macro_malformed_definition_complaint (body);
14923 }
14924
14925 /* Skip some bytes from BYTES according to the form given in FORM.
14926    Returns the new pointer.  */
14927
14928 static gdb_byte *
14929 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14930                  enum dwarf_form form,
14931                  unsigned int offset_size,
14932                  struct dwarf2_section_info *section)
14933 {
14934   unsigned int bytes_read;
14935
14936   switch (form)
14937     {
14938     case DW_FORM_data1:
14939     case DW_FORM_flag:
14940       ++bytes;
14941       break;
14942
14943     case DW_FORM_data2:
14944       bytes += 2;
14945       break;
14946
14947     case DW_FORM_data4:
14948       bytes += 4;
14949       break;
14950
14951     case DW_FORM_data8:
14952       bytes += 8;
14953       break;
14954
14955     case DW_FORM_string:
14956       read_direct_string (abfd, bytes, &bytes_read);
14957       bytes += bytes_read;
14958       break;
14959
14960     case DW_FORM_sec_offset:
14961     case DW_FORM_strp:
14962       bytes += offset_size;
14963       break;
14964
14965     case DW_FORM_block:
14966       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14967       bytes += bytes_read;
14968       break;
14969
14970     case DW_FORM_block1:
14971       bytes += 1 + read_1_byte (abfd, bytes);
14972       break;
14973     case DW_FORM_block2:
14974       bytes += 2 + read_2_bytes (abfd, bytes);
14975       break;
14976     case DW_FORM_block4:
14977       bytes += 4 + read_4_bytes (abfd, bytes);
14978       break;
14979
14980     case DW_FORM_sdata:
14981     case DW_FORM_udata:
14982       bytes = skip_leb128 (abfd, bytes);
14983       break;
14984
14985     default:
14986       {
14987       complain:
14988         complaint (&symfile_complaints,
14989                    _("invalid form 0x%x in `%s'"),
14990                    form,
14991                    section->asection->name);
14992         return NULL;
14993       }
14994     }
14995
14996   return bytes;
14997 }
14998
14999 /* A helper for dwarf_decode_macros that handles skipping an unknown
15000    opcode.  Returns an updated pointer to the macro data buffer; or,
15001    on error, issues a complaint and returns NULL.  */
15002
15003 static gdb_byte *
15004 skip_unknown_opcode (unsigned int opcode,
15005                      gdb_byte **opcode_definitions,
15006                      gdb_byte *mac_ptr,
15007                      bfd *abfd,
15008                      unsigned int offset_size,
15009                      struct dwarf2_section_info *section)
15010 {
15011   unsigned int bytes_read, i;
15012   unsigned long arg;
15013   gdb_byte *defn;
15014
15015   if (opcode_definitions[opcode] == NULL)
15016     {
15017       complaint (&symfile_complaints,
15018                  _("unrecognized DW_MACFINO opcode 0x%x"),
15019                  opcode);
15020       return NULL;
15021     }
15022
15023   defn = opcode_definitions[opcode];
15024   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15025   defn += bytes_read;
15026
15027   for (i = 0; i < arg; ++i)
15028     {
15029       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15030       if (mac_ptr == NULL)
15031         {
15032           /* skip_form_bytes already issued the complaint.  */
15033           return NULL;
15034         }
15035     }
15036
15037   return mac_ptr;
15038 }
15039
15040 /* A helper function which parses the header of a macro section.
15041    If the macro section is the extended (for now called "GNU") type,
15042    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15043    the header, or issues a complaint and returns NULL on error.  */
15044
15045 static gdb_byte *
15046 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15047                           bfd *abfd,
15048                           gdb_byte *mac_ptr,
15049                           unsigned int *offset_size,
15050                           int section_is_gnu)
15051 {
15052   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15053
15054   if (section_is_gnu)
15055     {
15056       unsigned int version, flags;
15057
15058       version = read_2_bytes (abfd, mac_ptr);
15059       if (version != 4)
15060         {
15061           complaint (&symfile_complaints,
15062                      _("unrecognized version `%d' in .debug_macro section"),
15063                      version);
15064           return NULL;
15065         }
15066       mac_ptr += 2;
15067
15068       flags = read_1_byte (abfd, mac_ptr);
15069       ++mac_ptr;
15070       *offset_size = (flags & 1) ? 8 : 4;
15071
15072       if ((flags & 2) != 0)
15073         /* We don't need the line table offset.  */
15074         mac_ptr += *offset_size;
15075
15076       /* Vendor opcode descriptions.  */
15077       if ((flags & 4) != 0)
15078         {
15079           unsigned int i, count;
15080
15081           count = read_1_byte (abfd, mac_ptr);
15082           ++mac_ptr;
15083           for (i = 0; i < count; ++i)
15084             {
15085               unsigned int opcode, bytes_read;
15086               unsigned long arg;
15087
15088               opcode = read_1_byte (abfd, mac_ptr);
15089               ++mac_ptr;
15090               opcode_definitions[opcode] = mac_ptr;
15091               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15092               mac_ptr += bytes_read;
15093               mac_ptr += arg;
15094             }
15095         }
15096     }
15097
15098   return mac_ptr;
15099 }
15100
15101 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15102    including DW_GNU_MACINFO_transparent_include.  */
15103
15104 static void
15105 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15106                           struct macro_source_file *current_file,
15107                           struct line_header *lh, char *comp_dir,
15108                           struct dwarf2_section_info *section,
15109                           int section_is_gnu,
15110                           unsigned int offset_size,
15111                           struct objfile *objfile)
15112 {
15113   enum dwarf_macro_record_type macinfo_type;
15114   int at_commandline;
15115   gdb_byte *opcode_definitions[256];
15116
15117   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15118                                       &offset_size, section_is_gnu);
15119   if (mac_ptr == NULL)
15120     {
15121       /* We already issued a complaint.  */
15122       return;
15123     }
15124
15125   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15126      GDB is still reading the definitions from command line.  First
15127      DW_MACINFO_start_file will need to be ignored as it was already executed
15128      to create CURRENT_FILE for the main source holding also the command line
15129      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15130      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15131
15132   at_commandline = 1;
15133
15134   do
15135     {
15136       /* Do we at least have room for a macinfo type byte?  */
15137       if (mac_ptr >= mac_end)
15138         {
15139           dwarf2_macros_too_long_complaint (section);
15140           break;
15141         }
15142
15143       macinfo_type = read_1_byte (abfd, mac_ptr);
15144       mac_ptr++;
15145
15146       /* Note that we rely on the fact that the corresponding GNU and
15147          DWARF constants are the same.  */
15148       switch (macinfo_type)
15149         {
15150           /* A zero macinfo type indicates the end of the macro
15151              information.  */
15152         case 0:
15153           break;
15154
15155         case DW_MACRO_GNU_define:
15156         case DW_MACRO_GNU_undef:
15157         case DW_MACRO_GNU_define_indirect:
15158         case DW_MACRO_GNU_undef_indirect:
15159           {
15160             unsigned int bytes_read;
15161             int line;
15162             char *body;
15163             int is_define;
15164
15165             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15166             mac_ptr += bytes_read;
15167
15168             if (macinfo_type == DW_MACRO_GNU_define
15169                 || macinfo_type == DW_MACRO_GNU_undef)
15170               {
15171                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15172                 mac_ptr += bytes_read;
15173               }
15174             else
15175               {
15176                 LONGEST str_offset;
15177
15178                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15179                 mac_ptr += offset_size;
15180
15181                 body = read_indirect_string_at_offset (abfd, str_offset);
15182               }
15183
15184             is_define = (macinfo_type == DW_MACRO_GNU_define
15185                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15186             if (! current_file)
15187               {
15188                 /* DWARF violation as no main source is present.  */
15189                 complaint (&symfile_complaints,
15190                            _("debug info with no main source gives macro %s "
15191                              "on line %d: %s"),
15192                            is_define ? _("definition") : _("undefinition"),
15193                            line, body);
15194                 break;
15195               }
15196             if ((line == 0 && !at_commandline)
15197                 || (line != 0 && at_commandline))
15198               complaint (&symfile_complaints,
15199                          _("debug info gives %s macro %s with %s line %d: %s"),
15200                          at_commandline ? _("command-line") : _("in-file"),
15201                          is_define ? _("definition") : _("undefinition"),
15202                          line == 0 ? _("zero") : _("non-zero"), line, body);
15203
15204             if (is_define)
15205               parse_macro_definition (current_file, line, body);
15206             else
15207               {
15208                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15209                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15210                 macro_undef (current_file, line, body);
15211               }
15212           }
15213           break;
15214
15215         case DW_MACRO_GNU_start_file:
15216           {
15217             unsigned int bytes_read;
15218             int line, file;
15219
15220             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15221             mac_ptr += bytes_read;
15222             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15223             mac_ptr += bytes_read;
15224
15225             if ((line == 0 && !at_commandline)
15226                 || (line != 0 && at_commandline))
15227               complaint (&symfile_complaints,
15228                          _("debug info gives source %d included "
15229                            "from %s at %s line %d"),
15230                          file, at_commandline ? _("command-line") : _("file"),
15231                          line == 0 ? _("zero") : _("non-zero"), line);
15232
15233             if (at_commandline)
15234               {
15235                 /* This DW_MACRO_GNU_start_file was executed in the
15236                    pass one.  */
15237                 at_commandline = 0;
15238               }
15239             else
15240               current_file = macro_start_file (file, line,
15241                                                current_file, comp_dir,
15242                                                lh, objfile);
15243           }
15244           break;
15245
15246         case DW_MACRO_GNU_end_file:
15247           if (! current_file)
15248             complaint (&symfile_complaints,
15249                        _("macro debug info has an unmatched "
15250                          "`close_file' directive"));
15251           else
15252             {
15253               current_file = current_file->included_by;
15254               if (! current_file)
15255                 {
15256                   enum dwarf_macro_record_type next_type;
15257
15258                   /* GCC circa March 2002 doesn't produce the zero
15259                      type byte marking the end of the compilation
15260                      unit.  Complain if it's not there, but exit no
15261                      matter what.  */
15262
15263                   /* Do we at least have room for a macinfo type byte?  */
15264                   if (mac_ptr >= mac_end)
15265                     {
15266                       dwarf2_macros_too_long_complaint (section);
15267                       return;
15268                     }
15269
15270                   /* We don't increment mac_ptr here, so this is just
15271                      a look-ahead.  */
15272                   next_type = read_1_byte (abfd, mac_ptr);
15273                   if (next_type != 0)
15274                     complaint (&symfile_complaints,
15275                                _("no terminating 0-type entry for "
15276                                  "macros in `.debug_macinfo' section"));
15277
15278                   return;
15279                 }
15280             }
15281           break;
15282
15283         case DW_MACRO_GNU_transparent_include:
15284           {
15285             LONGEST offset;
15286
15287             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15288             mac_ptr += offset_size;
15289
15290             dwarf_decode_macro_bytes (abfd,
15291                                       section->buffer + offset,
15292                                       mac_end, current_file,
15293                                       lh, comp_dir,
15294                                       section, section_is_gnu,
15295                                       offset_size, objfile);
15296           }
15297           break;
15298
15299         case DW_MACINFO_vendor_ext:
15300           if (!section_is_gnu)
15301             {
15302               unsigned int bytes_read;
15303               int constant;
15304
15305               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15306               mac_ptr += bytes_read;
15307               read_direct_string (abfd, mac_ptr, &bytes_read);
15308               mac_ptr += bytes_read;
15309
15310               /* We don't recognize any vendor extensions.  */
15311               break;
15312             }
15313           /* FALLTHROUGH */
15314
15315         default:
15316           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15317                                          mac_ptr, abfd, offset_size,
15318                                          section);
15319           if (mac_ptr == NULL)
15320             return;
15321           break;
15322         }
15323     } while (macinfo_type != 0);
15324 }
15325
15326 static void
15327 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15328                      char *comp_dir, bfd *abfd,
15329                      struct dwarf2_cu *cu,
15330                      struct dwarf2_section_info *section,
15331                      int section_is_gnu)
15332 {
15333   struct objfile *objfile = dwarf2_per_objfile->objfile;
15334   gdb_byte *mac_ptr, *mac_end;
15335   struct macro_source_file *current_file = 0;
15336   enum dwarf_macro_record_type macinfo_type;
15337   unsigned int offset_size = cu->header.offset_size;
15338   gdb_byte *opcode_definitions[256];
15339
15340   dwarf2_read_section (objfile, section);
15341   if (section->buffer == NULL)
15342     {
15343       complaint (&symfile_complaints, _("missing %s section"),
15344                  section->asection->name);
15345       return;
15346     }
15347
15348   /* First pass: Find the name of the base filename.
15349      This filename is needed in order to process all macros whose definition
15350      (or undefinition) comes from the command line.  These macros are defined
15351      before the first DW_MACINFO_start_file entry, and yet still need to be
15352      associated to the base file.
15353
15354      To determine the base file name, we scan the macro definitions until we
15355      reach the first DW_MACINFO_start_file entry.  We then initialize
15356      CURRENT_FILE accordingly so that any macro definition found before the
15357      first DW_MACINFO_start_file can still be associated to the base file.  */
15358
15359   mac_ptr = section->buffer + offset;
15360   mac_end = section->buffer + section->size;
15361
15362   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15363                                       &offset_size, section_is_gnu);
15364   if (mac_ptr == NULL)
15365     {
15366       /* We already issued a complaint.  */
15367       return;
15368     }
15369
15370   do
15371     {
15372       /* Do we at least have room for a macinfo type byte?  */
15373       if (mac_ptr >= mac_end)
15374         {
15375           /* Complaint is printed during the second pass as GDB will probably
15376              stop the first pass earlier upon finding
15377              DW_MACINFO_start_file.  */
15378           break;
15379         }
15380
15381       macinfo_type = read_1_byte (abfd, mac_ptr);
15382       mac_ptr++;
15383
15384       /* Note that we rely on the fact that the corresponding GNU and
15385          DWARF constants are the same.  */
15386       switch (macinfo_type)
15387         {
15388           /* A zero macinfo type indicates the end of the macro
15389              information.  */
15390         case 0:
15391           break;
15392
15393         case DW_MACRO_GNU_define:
15394         case DW_MACRO_GNU_undef:
15395           /* Only skip the data by MAC_PTR.  */
15396           {
15397             unsigned int bytes_read;
15398
15399             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15400             mac_ptr += bytes_read;
15401             read_direct_string (abfd, mac_ptr, &bytes_read);
15402             mac_ptr += bytes_read;
15403           }
15404           break;
15405
15406         case DW_MACRO_GNU_start_file:
15407           {
15408             unsigned int bytes_read;
15409             int line, file;
15410
15411             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15412             mac_ptr += bytes_read;
15413             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15414             mac_ptr += bytes_read;
15415
15416             current_file = macro_start_file (file, line, current_file,
15417                                              comp_dir, lh, objfile);
15418           }
15419           break;
15420
15421         case DW_MACRO_GNU_end_file:
15422           /* No data to skip by MAC_PTR.  */
15423           break;
15424
15425         case DW_MACRO_GNU_define_indirect:
15426         case DW_MACRO_GNU_undef_indirect:
15427           {
15428             unsigned int bytes_read;
15429
15430             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15431             mac_ptr += bytes_read;
15432             mac_ptr += offset_size;
15433           }
15434           break;
15435
15436         case DW_MACRO_GNU_transparent_include:
15437           /* Note that, according to the spec, a transparent include
15438              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15439              skip this opcode.  */
15440           mac_ptr += offset_size;
15441           break;
15442
15443         case DW_MACINFO_vendor_ext:
15444           /* Only skip the data by MAC_PTR.  */
15445           if (!section_is_gnu)
15446             {
15447               unsigned int bytes_read;
15448
15449               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15450               mac_ptr += bytes_read;
15451               read_direct_string (abfd, mac_ptr, &bytes_read);
15452               mac_ptr += bytes_read;
15453             }
15454           /* FALLTHROUGH */
15455
15456         default:
15457           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15458                                          mac_ptr, abfd, offset_size,
15459                                          section);
15460           if (mac_ptr == NULL)
15461             return;
15462           break;
15463         }
15464     } while (macinfo_type != 0 && current_file == NULL);
15465
15466   /* Second pass: Process all entries.
15467
15468      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15469      command-line macro definitions/undefinitions.  This flag is unset when we
15470      reach the first DW_MACINFO_start_file entry.  */
15471
15472   dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15473                             current_file, lh, comp_dir, section, section_is_gnu,
15474                             offset_size, objfile);
15475 }
15476
15477 /* Check if the attribute's form is a DW_FORM_block*
15478    if so return true else false.  */
15479 static int
15480 attr_form_is_block (struct attribute *attr)
15481 {
15482   return (attr == NULL ? 0 :
15483       attr->form == DW_FORM_block1
15484       || attr->form == DW_FORM_block2
15485       || attr->form == DW_FORM_block4
15486       || attr->form == DW_FORM_block
15487       || attr->form == DW_FORM_exprloc);
15488 }
15489
15490 /* Return non-zero if ATTR's value is a section offset --- classes
15491    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15492    You may use DW_UNSND (attr) to retrieve such offsets.
15493
15494    Section 7.5.4, "Attribute Encodings", explains that no attribute
15495    may have a value that belongs to more than one of these classes; it
15496    would be ambiguous if we did, because we use the same forms for all
15497    of them.  */
15498 static int
15499 attr_form_is_section_offset (struct attribute *attr)
15500 {
15501   return (attr->form == DW_FORM_data4
15502           || attr->form == DW_FORM_data8
15503           || attr->form == DW_FORM_sec_offset);
15504 }
15505
15506
15507 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15508    zero otherwise.  When this function returns true, you can apply
15509    dwarf2_get_attr_constant_value to it.
15510
15511    However, note that for some attributes you must check
15512    attr_form_is_section_offset before using this test.  DW_FORM_data4
15513    and DW_FORM_data8 are members of both the constant class, and of
15514    the classes that contain offsets into other debug sections
15515    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15516    that, if an attribute's can be either a constant or one of the
15517    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15518    taken as section offsets, not constants.  */
15519 static int
15520 attr_form_is_constant (struct attribute *attr)
15521 {
15522   switch (attr->form)
15523     {
15524     case DW_FORM_sdata:
15525     case DW_FORM_udata:
15526     case DW_FORM_data1:
15527     case DW_FORM_data2:
15528     case DW_FORM_data4:
15529     case DW_FORM_data8:
15530       return 1;
15531     default:
15532       return 0;
15533     }
15534 }
15535
15536 /* A helper function that fills in a dwarf2_loclist_baton.  */
15537
15538 static void
15539 fill_in_loclist_baton (struct dwarf2_cu *cu,
15540                        struct dwarf2_loclist_baton *baton,
15541                        struct attribute *attr)
15542 {
15543   dwarf2_read_section (dwarf2_per_objfile->objfile,
15544                        &dwarf2_per_objfile->loc);
15545
15546   baton->per_cu = cu->per_cu;
15547   gdb_assert (baton->per_cu);
15548   /* We don't know how long the location list is, but make sure we
15549      don't run off the edge of the section.  */
15550   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15551   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15552   baton->base_address = cu->base_address;
15553 }
15554
15555 static void
15556 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15557                              struct dwarf2_cu *cu)
15558 {
15559   struct objfile *objfile = dwarf2_per_objfile->objfile;
15560
15561   if (attr_form_is_section_offset (attr)
15562       /* ".debug_loc" may not exist at all, or the offset may be outside
15563          the section.  If so, fall through to the complaint in the
15564          other branch.  */
15565       && DW_UNSND (attr) < dwarf2_section_size (objfile,
15566                                                 &dwarf2_per_objfile->loc))
15567     {
15568       struct dwarf2_loclist_baton *baton;
15569
15570       baton = obstack_alloc (&objfile->objfile_obstack,
15571                              sizeof (struct dwarf2_loclist_baton));
15572
15573       fill_in_loclist_baton (cu, baton, attr);
15574
15575       if (cu->base_known == 0)
15576         complaint (&symfile_complaints,
15577                    _("Location list used without "
15578                      "specifying the CU base address."));
15579
15580       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15581       SYMBOL_LOCATION_BATON (sym) = baton;
15582     }
15583   else
15584     {
15585       struct dwarf2_locexpr_baton *baton;
15586
15587       baton = obstack_alloc (&objfile->objfile_obstack,
15588                              sizeof (struct dwarf2_locexpr_baton));
15589       baton->per_cu = cu->per_cu;
15590       gdb_assert (baton->per_cu);
15591
15592       if (attr_form_is_block (attr))
15593         {
15594           /* Note that we're just copying the block's data pointer
15595              here, not the actual data.  We're still pointing into the
15596              info_buffer for SYM's objfile; right now we never release
15597              that buffer, but when we do clean up properly this may
15598              need to change.  */
15599           baton->size = DW_BLOCK (attr)->size;
15600           baton->data = DW_BLOCK (attr)->data;
15601         }
15602       else
15603         {
15604           dwarf2_invalid_attrib_class_complaint ("location description",
15605                                                  SYMBOL_NATURAL_NAME (sym));
15606           baton->size = 0;
15607         }
15608
15609       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15610       SYMBOL_LOCATION_BATON (sym) = baton;
15611     }
15612 }
15613
15614 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15615    came from a separate debuginfo file, then the master objfile is
15616    returned.  */
15617
15618 struct objfile *
15619 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15620 {
15621   struct objfile *objfile = per_cu->objfile;
15622
15623   /* Return the master objfile, so that we can report and look up the
15624      correct file containing this variable.  */
15625   if (objfile->separate_debug_objfile_backlink)
15626     objfile = objfile->separate_debug_objfile_backlink;
15627
15628   return objfile;
15629 }
15630
15631 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15632    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15633    CU_HEADERP first.  */
15634
15635 static const struct comp_unit_head *
15636 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15637                        struct dwarf2_per_cu_data *per_cu)
15638 {
15639   struct objfile *objfile;
15640   struct dwarf2_per_objfile *per_objfile;
15641   gdb_byte *info_ptr;
15642
15643   if (per_cu->cu)
15644     return &per_cu->cu->header;
15645
15646   objfile = per_cu->objfile;
15647   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15648   info_ptr = per_objfile->info.buffer + per_cu->offset;
15649
15650   memset (cu_headerp, 0, sizeof (*cu_headerp));
15651   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15652
15653   return cu_headerp;
15654 }
15655
15656 /* Return the address size given in the compilation unit header for CU.  */
15657
15658 int
15659 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15660 {
15661   struct comp_unit_head cu_header_local;
15662   const struct comp_unit_head *cu_headerp;
15663
15664   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15665
15666   return cu_headerp->addr_size;
15667 }
15668
15669 /* Return the offset size given in the compilation unit header for CU.  */
15670
15671 int
15672 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15673 {
15674   struct comp_unit_head cu_header_local;
15675   const struct comp_unit_head *cu_headerp;
15676
15677   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15678
15679   return cu_headerp->offset_size;
15680 }
15681
15682 /* See its dwarf2loc.h declaration.  */
15683
15684 int
15685 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15686 {
15687   struct comp_unit_head cu_header_local;
15688   const struct comp_unit_head *cu_headerp;
15689
15690   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15691
15692   if (cu_headerp->version == 2)
15693     return cu_headerp->addr_size;
15694   else
15695     return cu_headerp->offset_size;
15696 }
15697
15698 /* Return the text offset of the CU.  The returned offset comes from
15699    this CU's objfile.  If this objfile came from a separate debuginfo
15700    file, then the offset may be different from the corresponding
15701    offset in the parent objfile.  */
15702
15703 CORE_ADDR
15704 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15705 {
15706   struct objfile *objfile = per_cu->objfile;
15707
15708   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15709 }
15710
15711 /* Locate the .debug_info compilation unit from CU's objfile which contains
15712    the DIE at OFFSET.  Raises an error on failure.  */
15713
15714 static struct dwarf2_per_cu_data *
15715 dwarf2_find_containing_comp_unit (unsigned int offset,
15716                                   struct objfile *objfile)
15717 {
15718   struct dwarf2_per_cu_data *this_cu;
15719   int low, high;
15720
15721   low = 0;
15722   high = dwarf2_per_objfile->n_comp_units - 1;
15723   while (high > low)
15724     {
15725       int mid = low + (high - low) / 2;
15726
15727       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15728         high = mid;
15729       else
15730         low = mid + 1;
15731     }
15732   gdb_assert (low == high);
15733   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15734     {
15735       if (low == 0)
15736         error (_("Dwarf Error: could not find partial DIE containing "
15737                "offset 0x%lx [in module %s]"),
15738                (long) offset, bfd_get_filename (objfile->obfd));
15739
15740       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15741       return dwarf2_per_objfile->all_comp_units[low-1];
15742     }
15743   else
15744     {
15745       this_cu = dwarf2_per_objfile->all_comp_units[low];
15746       if (low == dwarf2_per_objfile->n_comp_units - 1
15747           && offset >= this_cu->offset + this_cu->length)
15748         error (_("invalid dwarf2 offset %u"), offset);
15749       gdb_assert (offset < this_cu->offset + this_cu->length);
15750       return this_cu;
15751     }
15752 }
15753
15754 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
15755
15756 static void
15757 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15758 {
15759   memset (cu, 0, sizeof (*cu));
15760   per_cu->cu = cu;
15761   cu->per_cu = per_cu;
15762   cu->objfile = per_cu->objfile;
15763   obstack_init (&cu->comp_unit_obstack);
15764 }
15765
15766 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15767
15768 static void
15769 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15770 {
15771   struct attribute *attr;
15772
15773   /* Set the language we're debugging.  */
15774   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15775   if (attr)
15776     set_cu_language (DW_UNSND (attr), cu);
15777   else
15778     {
15779       cu->language = language_minimal;
15780       cu->language_defn = language_def (cu->language);
15781     }
15782 }
15783
15784 /* Release one cached compilation unit, CU.  We unlink it from the tree
15785    of compilation units, but we don't remove it from the read_in_chain;
15786    the caller is responsible for that.
15787    NOTE: DATA is a void * because this function is also used as a
15788    cleanup routine.  */
15789
15790 static void
15791 free_heap_comp_unit (void *data)
15792 {
15793   struct dwarf2_cu *cu = data;
15794
15795   gdb_assert (cu->per_cu != NULL);
15796   cu->per_cu->cu = NULL;
15797   cu->per_cu = NULL;
15798
15799   obstack_free (&cu->comp_unit_obstack, NULL);
15800
15801   xfree (cu);
15802 }
15803
15804 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15805    when we're finished with it.  We can't free the pointer itself, but be
15806    sure to unlink it from the cache.  Also release any associated storage
15807    and perform cache maintenance.
15808
15809    Only used during partial symbol parsing.  */
15810
15811 static void
15812 free_stack_comp_unit (void *data)
15813 {
15814   struct dwarf2_cu *cu = data;
15815
15816   gdb_assert (cu->per_cu != NULL);
15817   cu->per_cu->cu = NULL;
15818   cu->per_cu = NULL;
15819
15820   obstack_free (&cu->comp_unit_obstack, NULL);
15821   cu->partial_dies = NULL;
15822
15823   /* The previous code only did this if per_cu != NULL.
15824      But that would always succeed, so now we just unconditionally do
15825      the aging.  This seems like the wrong place to do such aging,
15826      but cleaning that up is left for later.  */
15827   age_cached_comp_units ();
15828 }
15829
15830 /* Free all cached compilation units.  */
15831
15832 static void
15833 free_cached_comp_units (void *data)
15834 {
15835   struct dwarf2_per_cu_data *per_cu, **last_chain;
15836
15837   per_cu = dwarf2_per_objfile->read_in_chain;
15838   last_chain = &dwarf2_per_objfile->read_in_chain;
15839   while (per_cu != NULL)
15840     {
15841       struct dwarf2_per_cu_data *next_cu;
15842
15843       next_cu = per_cu->cu->read_in_chain;
15844
15845       free_heap_comp_unit (per_cu->cu);
15846       *last_chain = next_cu;
15847
15848       per_cu = next_cu;
15849     }
15850 }
15851
15852 /* Increase the age counter on each cached compilation unit, and free
15853    any that are too old.  */
15854
15855 static void
15856 age_cached_comp_units (void)
15857 {
15858   struct dwarf2_per_cu_data *per_cu, **last_chain;
15859
15860   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15861   per_cu = dwarf2_per_objfile->read_in_chain;
15862   while (per_cu != NULL)
15863     {
15864       per_cu->cu->last_used ++;
15865       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15866         dwarf2_mark (per_cu->cu);
15867       per_cu = per_cu->cu->read_in_chain;
15868     }
15869
15870   per_cu = dwarf2_per_objfile->read_in_chain;
15871   last_chain = &dwarf2_per_objfile->read_in_chain;
15872   while (per_cu != NULL)
15873     {
15874       struct dwarf2_per_cu_data *next_cu;
15875
15876       next_cu = per_cu->cu->read_in_chain;
15877
15878       if (!per_cu->cu->mark)
15879         {
15880           free_heap_comp_unit (per_cu->cu);
15881           *last_chain = next_cu;
15882         }
15883       else
15884         last_chain = &per_cu->cu->read_in_chain;
15885
15886       per_cu = next_cu;
15887     }
15888 }
15889
15890 /* Remove a single compilation unit from the cache.  */
15891
15892 static void
15893 free_one_cached_comp_unit (void *target_cu)
15894 {
15895   struct dwarf2_per_cu_data *per_cu, **last_chain;
15896
15897   per_cu = dwarf2_per_objfile->read_in_chain;
15898   last_chain = &dwarf2_per_objfile->read_in_chain;
15899   while (per_cu != NULL)
15900     {
15901       struct dwarf2_per_cu_data *next_cu;
15902
15903       next_cu = per_cu->cu->read_in_chain;
15904
15905       if (per_cu->cu == target_cu)
15906         {
15907           free_heap_comp_unit (per_cu->cu);
15908           *last_chain = next_cu;
15909           break;
15910         }
15911       else
15912         last_chain = &per_cu->cu->read_in_chain;
15913
15914       per_cu = next_cu;
15915     }
15916 }
15917
15918 /* Release all extra memory associated with OBJFILE.  */
15919
15920 void
15921 dwarf2_free_objfile (struct objfile *objfile)
15922 {
15923   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15924
15925   if (dwarf2_per_objfile == NULL)
15926     return;
15927
15928   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
15929   free_cached_comp_units (NULL);
15930
15931   if (dwarf2_per_objfile->quick_file_names_table)
15932     htab_delete (dwarf2_per_objfile->quick_file_names_table);
15933
15934   /* Everything else should be on the objfile obstack.  */
15935 }
15936
15937 /* A pair of DIE offset and GDB type pointer.  We store these
15938    in a hash table separate from the DIEs, and preserve them
15939    when the DIEs are flushed out of cache.  */
15940
15941 struct dwarf2_offset_and_type
15942 {
15943   unsigned int offset;
15944   struct type *type;
15945 };
15946
15947 /* Hash function for a dwarf2_offset_and_type.  */
15948
15949 static hashval_t
15950 offset_and_type_hash (const void *item)
15951 {
15952   const struct dwarf2_offset_and_type *ofs = item;
15953
15954   return ofs->offset;
15955 }
15956
15957 /* Equality function for a dwarf2_offset_and_type.  */
15958
15959 static int
15960 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15961 {
15962   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15963   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15964
15965   return ofs_lhs->offset == ofs_rhs->offset;
15966 }
15967
15968 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
15969    table if necessary.  For convenience, return TYPE.
15970
15971    The DIEs reading must have careful ordering to:
15972     * Not cause infite loops trying to read in DIEs as a prerequisite for
15973       reading current DIE.
15974     * Not trying to dereference contents of still incompletely read in types
15975       while reading in other DIEs.
15976     * Enable referencing still incompletely read in types just by a pointer to
15977       the type without accessing its fields.
15978
15979    Therefore caller should follow these rules:
15980      * Try to fetch any prerequisite types we may need to build this DIE type
15981        before building the type and calling set_die_type.
15982      * After building type call set_die_type for current DIE as soon as
15983        possible before fetching more types to complete the current type.
15984      * Make the type as complete as possible before fetching more types.  */
15985
15986 static struct type *
15987 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15988 {
15989   struct dwarf2_offset_and_type **slot, ofs;
15990   struct objfile *objfile = cu->objfile;
15991   htab_t *type_hash_ptr;
15992
15993   /* For Ada types, make sure that the gnat-specific data is always
15994      initialized (if not already set).  There are a few types where
15995      we should not be doing so, because the type-specific area is
15996      already used to hold some other piece of info (eg: TYPE_CODE_FLT
15997      where the type-specific area is used to store the floatformat).
15998      But this is not a problem, because the gnat-specific information
15999      is actually not needed for these types.  */
16000   if (need_gnat_info (cu)
16001       && TYPE_CODE (type) != TYPE_CODE_FUNC
16002       && TYPE_CODE (type) != TYPE_CODE_FLT
16003       && !HAVE_GNAT_AUX_INFO (type))
16004     INIT_GNAT_SPECIFIC (type);
16005
16006   if (cu->per_cu->debug_types_section)
16007     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16008   else
16009     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16010
16011   if (*type_hash_ptr == NULL)
16012     {
16013       *type_hash_ptr
16014         = htab_create_alloc_ex (127,
16015                                 offset_and_type_hash,
16016                                 offset_and_type_eq,
16017                                 NULL,
16018                                 &objfile->objfile_obstack,
16019                                 hashtab_obstack_allocate,
16020                                 dummy_obstack_deallocate);
16021     }
16022
16023   ofs.offset = die->offset;
16024   ofs.type = type;
16025   slot = (struct dwarf2_offset_and_type **)
16026     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16027   if (*slot)
16028     complaint (&symfile_complaints,
16029                _("A problem internal to GDB: DIE 0x%x has type already set"),
16030                die->offset);
16031   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16032   **slot = ofs;
16033   return type;
16034 }
16035
16036 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16037    table, or return NULL if the die does not have a saved type.  */
16038
16039 static struct type *
16040 get_die_type_at_offset (unsigned int offset,
16041                         struct dwarf2_per_cu_data *per_cu)
16042 {
16043   struct dwarf2_offset_and_type *slot, ofs;
16044   htab_t type_hash;
16045
16046   if (per_cu->debug_types_section)
16047     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16048   else
16049     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16050   if (type_hash == NULL)
16051     return NULL;
16052
16053   ofs.offset = offset;
16054   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16055   if (slot)
16056     return slot->type;
16057   else
16058     return NULL;
16059 }
16060
16061 /* Look up the type for DIE in the appropriate type_hash table,
16062    or return NULL if DIE does not have a saved type.  */
16063
16064 static struct type *
16065 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16066 {
16067   return get_die_type_at_offset (die->offset, cu->per_cu);
16068 }
16069
16070 /* Add a dependence relationship from CU to REF_PER_CU.  */
16071
16072 static void
16073 dwarf2_add_dependence (struct dwarf2_cu *cu,
16074                        struct dwarf2_per_cu_data *ref_per_cu)
16075 {
16076   void **slot;
16077
16078   if (cu->dependencies == NULL)
16079     cu->dependencies
16080       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16081                               NULL, &cu->comp_unit_obstack,
16082                               hashtab_obstack_allocate,
16083                               dummy_obstack_deallocate);
16084
16085   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16086   if (*slot == NULL)
16087     *slot = ref_per_cu;
16088 }
16089
16090 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16091    Set the mark field in every compilation unit in the
16092    cache that we must keep because we are keeping CU.  */
16093
16094 static int
16095 dwarf2_mark_helper (void **slot, void *data)
16096 {
16097   struct dwarf2_per_cu_data *per_cu;
16098
16099   per_cu = (struct dwarf2_per_cu_data *) *slot;
16100
16101   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16102      reading of the chain.  As such dependencies remain valid it is not much
16103      useful to track and undo them during QUIT cleanups.  */
16104   if (per_cu->cu == NULL)
16105     return 1;
16106
16107   if (per_cu->cu->mark)
16108     return 1;
16109   per_cu->cu->mark = 1;
16110
16111   if (per_cu->cu->dependencies != NULL)
16112     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16113
16114   return 1;
16115 }
16116
16117 /* Set the mark field in CU and in every other compilation unit in the
16118    cache that we must keep because we are keeping CU.  */
16119
16120 static void
16121 dwarf2_mark (struct dwarf2_cu *cu)
16122 {
16123   if (cu->mark)
16124     return;
16125   cu->mark = 1;
16126   if (cu->dependencies != NULL)
16127     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16128 }
16129
16130 static void
16131 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16132 {
16133   while (per_cu)
16134     {
16135       per_cu->cu->mark = 0;
16136       per_cu = per_cu->cu->read_in_chain;
16137     }
16138 }
16139
16140 /* Trivial hash function for partial_die_info: the hash value of a DIE
16141    is its offset in .debug_info for this objfile.  */
16142
16143 static hashval_t
16144 partial_die_hash (const void *item)
16145 {
16146   const struct partial_die_info *part_die = item;
16147
16148   return part_die->offset;
16149 }
16150
16151 /* Trivial comparison function for partial_die_info structures: two DIEs
16152    are equal if they have the same offset.  */
16153
16154 static int
16155 partial_die_eq (const void *item_lhs, const void *item_rhs)
16156 {
16157   const struct partial_die_info *part_die_lhs = item_lhs;
16158   const struct partial_die_info *part_die_rhs = item_rhs;
16159
16160   return part_die_lhs->offset == part_die_rhs->offset;
16161 }
16162
16163 static struct cmd_list_element *set_dwarf2_cmdlist;
16164 static struct cmd_list_element *show_dwarf2_cmdlist;
16165
16166 static void
16167 set_dwarf2_cmd (char *args, int from_tty)
16168 {
16169   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16170 }
16171
16172 static void
16173 show_dwarf2_cmd (char *args, int from_tty)
16174 {
16175   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16176 }
16177
16178 /* If section described by INFO was mmapped, munmap it now.  */
16179
16180 static void
16181 munmap_section_buffer (struct dwarf2_section_info *info)
16182 {
16183   if (info->map_addr != NULL)
16184     {
16185 #ifdef HAVE_MMAP
16186       int res;
16187
16188       res = munmap (info->map_addr, info->map_len);
16189       gdb_assert (res == 0);
16190 #else
16191       /* Without HAVE_MMAP, we should never be here to begin with.  */
16192       gdb_assert_not_reached ("no mmap support");
16193 #endif
16194     }
16195 }
16196
16197 /* munmap debug sections for OBJFILE, if necessary.  */
16198
16199 static void
16200 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16201 {
16202   struct dwarf2_per_objfile *data = d;
16203   int ix;
16204   struct dwarf2_section_info *section;
16205
16206   /* This is sorted according to the order they're defined in to make it easier
16207      to keep in sync.  */
16208   munmap_section_buffer (&data->info);
16209   munmap_section_buffer (&data->abbrev);
16210   munmap_section_buffer (&data->line);
16211   munmap_section_buffer (&data->loc);
16212   munmap_section_buffer (&data->macinfo);
16213   munmap_section_buffer (&data->macro);
16214   munmap_section_buffer (&data->str);
16215   munmap_section_buffer (&data->ranges);
16216   munmap_section_buffer (&data->frame);
16217   munmap_section_buffer (&data->eh_frame);
16218   munmap_section_buffer (&data->gdb_index);
16219
16220   for (ix = 0;
16221        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16222        ++ix)
16223     munmap_section_buffer (section);
16224
16225   VEC_free (dwarf2_section_info_def, data->types);
16226 }
16227
16228 \f
16229 /* The "save gdb-index" command.  */
16230
16231 /* The contents of the hash table we create when building the string
16232    table.  */
16233 struct strtab_entry
16234 {
16235   offset_type offset;
16236   const char *str;
16237 };
16238
16239 /* Hash function for a strtab_entry.
16240
16241    Function is used only during write_hash_table so no index format backward
16242    compatibility is needed.  */
16243
16244 static hashval_t
16245 hash_strtab_entry (const void *e)
16246 {
16247   const struct strtab_entry *entry = e;
16248   return mapped_index_string_hash (INT_MAX, entry->str);
16249 }
16250
16251 /* Equality function for a strtab_entry.  */
16252
16253 static int
16254 eq_strtab_entry (const void *a, const void *b)
16255 {
16256   const struct strtab_entry *ea = a;
16257   const struct strtab_entry *eb = b;
16258   return !strcmp (ea->str, eb->str);
16259 }
16260
16261 /* Create a strtab_entry hash table.  */
16262
16263 static htab_t
16264 create_strtab (void)
16265 {
16266   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16267                             xfree, xcalloc, xfree);
16268 }
16269
16270 /* Add a string to the constant pool.  Return the string's offset in
16271    host order.  */
16272
16273 static offset_type
16274 add_string (htab_t table, struct obstack *cpool, const char *str)
16275 {
16276   void **slot;
16277   struct strtab_entry entry;
16278   struct strtab_entry *result;
16279
16280   entry.str = str;
16281   slot = htab_find_slot (table, &entry, INSERT);
16282   if (*slot)
16283     result = *slot;
16284   else
16285     {
16286       result = XNEW (struct strtab_entry);
16287       result->offset = obstack_object_size (cpool);
16288       result->str = str;
16289       obstack_grow_str0 (cpool, str);
16290       *slot = result;
16291     }
16292   return result->offset;
16293 }
16294
16295 /* An entry in the symbol table.  */
16296 struct symtab_index_entry
16297 {
16298   /* The name of the symbol.  */
16299   const char *name;
16300   /* The offset of the name in the constant pool.  */
16301   offset_type index_offset;
16302   /* A sorted vector of the indices of all the CUs that hold an object
16303      of this name.  */
16304   VEC (offset_type) *cu_indices;
16305 };
16306
16307 /* The symbol table.  This is a power-of-2-sized hash table.  */
16308 struct mapped_symtab
16309 {
16310   offset_type n_elements;
16311   offset_type size;
16312   struct symtab_index_entry **data;
16313 };
16314
16315 /* Hash function for a symtab_index_entry.  */
16316
16317 static hashval_t
16318 hash_symtab_entry (const void *e)
16319 {
16320   const struct symtab_index_entry *entry = e;
16321   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16322                          sizeof (offset_type) * VEC_length (offset_type,
16323                                                             entry->cu_indices),
16324                          0);
16325 }
16326
16327 /* Equality function for a symtab_index_entry.  */
16328
16329 static int
16330 eq_symtab_entry (const void *a, const void *b)
16331 {
16332   const struct symtab_index_entry *ea = a;
16333   const struct symtab_index_entry *eb = b;
16334   int len = VEC_length (offset_type, ea->cu_indices);
16335   if (len != VEC_length (offset_type, eb->cu_indices))
16336     return 0;
16337   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16338                   VEC_address (offset_type, eb->cu_indices),
16339                   sizeof (offset_type) * len);
16340 }
16341
16342 /* Destroy a symtab_index_entry.  */
16343
16344 static void
16345 delete_symtab_entry (void *p)
16346 {
16347   struct symtab_index_entry *entry = p;
16348   VEC_free (offset_type, entry->cu_indices);
16349   xfree (entry);
16350 }
16351
16352 /* Create a hash table holding symtab_index_entry objects.  */
16353
16354 static htab_t
16355 create_symbol_hash_table (void)
16356 {
16357   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16358                             delete_symtab_entry, xcalloc, xfree);
16359 }
16360
16361 /* Create a new mapped symtab object.  */
16362
16363 static struct mapped_symtab *
16364 create_mapped_symtab (void)
16365 {
16366   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16367   symtab->n_elements = 0;
16368   symtab->size = 1024;
16369   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16370   return symtab;
16371 }
16372
16373 /* Destroy a mapped_symtab.  */
16374
16375 static void
16376 cleanup_mapped_symtab (void *p)
16377 {
16378   struct mapped_symtab *symtab = p;
16379   /* The contents of the array are freed when the other hash table is
16380      destroyed.  */
16381   xfree (symtab->data);
16382   xfree (symtab);
16383 }
16384
16385 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16386    the slot.
16387    
16388    Function is used only during write_hash_table so no index format backward
16389    compatibility is needed.  */
16390
16391 static struct symtab_index_entry **
16392 find_slot (struct mapped_symtab *symtab, const char *name)
16393 {
16394   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16395
16396   index = hash & (symtab->size - 1);
16397   step = ((hash * 17) & (symtab->size - 1)) | 1;
16398
16399   for (;;)
16400     {
16401       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16402         return &symtab->data[index];
16403       index = (index + step) & (symtab->size - 1);
16404     }
16405 }
16406
16407 /* Expand SYMTAB's hash table.  */
16408
16409 static void
16410 hash_expand (struct mapped_symtab *symtab)
16411 {
16412   offset_type old_size = symtab->size;
16413   offset_type i;
16414   struct symtab_index_entry **old_entries = symtab->data;
16415
16416   symtab->size *= 2;
16417   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16418
16419   for (i = 0; i < old_size; ++i)
16420     {
16421       if (old_entries[i])
16422         {
16423           struct symtab_index_entry **slot = find_slot (symtab,
16424                                                         old_entries[i]->name);
16425           *slot = old_entries[i];
16426         }
16427     }
16428
16429   xfree (old_entries);
16430 }
16431
16432 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16433    is the index of the CU in which the symbol appears.  */
16434
16435 static void
16436 add_index_entry (struct mapped_symtab *symtab, const char *name,
16437                  offset_type cu_index)
16438 {
16439   struct symtab_index_entry **slot;
16440
16441   ++symtab->n_elements;
16442   if (4 * symtab->n_elements / 3 >= symtab->size)
16443     hash_expand (symtab);
16444
16445   slot = find_slot (symtab, name);
16446   if (!*slot)
16447     {
16448       *slot = XNEW (struct symtab_index_entry);
16449       (*slot)->name = name;
16450       (*slot)->cu_indices = NULL;
16451     }
16452   /* Don't push an index twice.  Due to how we add entries we only
16453      have to check the last one.  */ 
16454   if (VEC_empty (offset_type, (*slot)->cu_indices)
16455       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16456     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16457 }
16458
16459 /* Add a vector of indices to the constant pool.  */
16460
16461 static offset_type
16462 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16463                       struct symtab_index_entry *entry)
16464 {
16465   void **slot;
16466
16467   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16468   if (!*slot)
16469     {
16470       offset_type len = VEC_length (offset_type, entry->cu_indices);
16471       offset_type val = MAYBE_SWAP (len);
16472       offset_type iter;
16473       int i;
16474
16475       *slot = entry;
16476       entry->index_offset = obstack_object_size (cpool);
16477
16478       obstack_grow (cpool, &val, sizeof (val));
16479       for (i = 0;
16480            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16481            ++i)
16482         {
16483           val = MAYBE_SWAP (iter);
16484           obstack_grow (cpool, &val, sizeof (val));
16485         }
16486     }
16487   else
16488     {
16489       struct symtab_index_entry *old_entry = *slot;
16490       entry->index_offset = old_entry->index_offset;
16491       entry = old_entry;
16492     }
16493   return entry->index_offset;
16494 }
16495
16496 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16497    constant pool entries going into the obstack CPOOL.  */
16498
16499 static void
16500 write_hash_table (struct mapped_symtab *symtab,
16501                   struct obstack *output, struct obstack *cpool)
16502 {
16503   offset_type i;
16504   htab_t symbol_hash_table;
16505   htab_t str_table;
16506
16507   symbol_hash_table = create_symbol_hash_table ();
16508   str_table = create_strtab ();
16509
16510   /* We add all the index vectors to the constant pool first, to
16511      ensure alignment is ok.  */
16512   for (i = 0; i < symtab->size; ++i)
16513     {
16514       if (symtab->data[i])
16515         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16516     }
16517
16518   /* Now write out the hash table.  */
16519   for (i = 0; i < symtab->size; ++i)
16520     {
16521       offset_type str_off, vec_off;
16522
16523       if (symtab->data[i])
16524         {
16525           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16526           vec_off = symtab->data[i]->index_offset;
16527         }
16528       else
16529         {
16530           /* While 0 is a valid constant pool index, it is not valid
16531              to have 0 for both offsets.  */
16532           str_off = 0;
16533           vec_off = 0;
16534         }
16535
16536       str_off = MAYBE_SWAP (str_off);
16537       vec_off = MAYBE_SWAP (vec_off);
16538
16539       obstack_grow (output, &str_off, sizeof (str_off));
16540       obstack_grow (output, &vec_off, sizeof (vec_off));
16541     }
16542
16543   htab_delete (str_table);
16544   htab_delete (symbol_hash_table);
16545 }
16546
16547 /* Struct to map psymtab to CU index in the index file.  */
16548 struct psymtab_cu_index_map
16549 {
16550   struct partial_symtab *psymtab;
16551   unsigned int cu_index;
16552 };
16553
16554 static hashval_t
16555 hash_psymtab_cu_index (const void *item)
16556 {
16557   const struct psymtab_cu_index_map *map = item;
16558
16559   return htab_hash_pointer (map->psymtab);
16560 }
16561
16562 static int
16563 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16564 {
16565   const struct psymtab_cu_index_map *lhs = item_lhs;
16566   const struct psymtab_cu_index_map *rhs = item_rhs;
16567
16568   return lhs->psymtab == rhs->psymtab;
16569 }
16570
16571 /* Helper struct for building the address table.  */
16572 struct addrmap_index_data
16573 {
16574   struct objfile *objfile;
16575   struct obstack *addr_obstack;
16576   htab_t cu_index_htab;
16577
16578   /* Non-zero if the previous_* fields are valid.
16579      We can't write an entry until we see the next entry (since it is only then
16580      that we know the end of the entry).  */
16581   int previous_valid;
16582   /* Index of the CU in the table of all CUs in the index file.  */
16583   unsigned int previous_cu_index;
16584   /* Start address of the CU.  */
16585   CORE_ADDR previous_cu_start;
16586 };
16587
16588 /* Write an address entry to OBSTACK.  */
16589
16590 static void
16591 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16592                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16593 {
16594   offset_type cu_index_to_write;
16595   char addr[8];
16596   CORE_ADDR baseaddr;
16597
16598   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16599
16600   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16601   obstack_grow (obstack, addr, 8);
16602   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16603   obstack_grow (obstack, addr, 8);
16604   cu_index_to_write = MAYBE_SWAP (cu_index);
16605   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16606 }
16607
16608 /* Worker function for traversing an addrmap to build the address table.  */
16609
16610 static int
16611 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16612 {
16613   struct addrmap_index_data *data = datap;
16614   struct partial_symtab *pst = obj;
16615   offset_type cu_index;
16616   void **slot;
16617
16618   if (data->previous_valid)
16619     add_address_entry (data->objfile, data->addr_obstack,
16620                        data->previous_cu_start, start_addr,
16621                        data->previous_cu_index);
16622
16623   data->previous_cu_start = start_addr;
16624   if (pst != NULL)
16625     {
16626       struct psymtab_cu_index_map find_map, *map;
16627       find_map.psymtab = pst;
16628       map = htab_find (data->cu_index_htab, &find_map);
16629       gdb_assert (map != NULL);
16630       data->previous_cu_index = map->cu_index;
16631       data->previous_valid = 1;
16632     }
16633   else
16634       data->previous_valid = 0;
16635
16636   return 0;
16637 }
16638
16639 /* Write OBJFILE's address map to OBSTACK.
16640    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16641    in the index file.  */
16642
16643 static void
16644 write_address_map (struct objfile *objfile, struct obstack *obstack,
16645                    htab_t cu_index_htab)
16646 {
16647   struct addrmap_index_data addrmap_index_data;
16648
16649   /* When writing the address table, we have to cope with the fact that
16650      the addrmap iterator only provides the start of a region; we have to
16651      wait until the next invocation to get the start of the next region.  */
16652
16653   addrmap_index_data.objfile = objfile;
16654   addrmap_index_data.addr_obstack = obstack;
16655   addrmap_index_data.cu_index_htab = cu_index_htab;
16656   addrmap_index_data.previous_valid = 0;
16657
16658   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16659                    &addrmap_index_data);
16660
16661   /* It's highly unlikely the last entry (end address = 0xff...ff)
16662      is valid, but we should still handle it.
16663      The end address is recorded as the start of the next region, but that
16664      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16665      anyway.  */
16666   if (addrmap_index_data.previous_valid)
16667     add_address_entry (objfile, obstack,
16668                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16669                        addrmap_index_data.previous_cu_index);
16670 }
16671
16672 /* Add a list of partial symbols to SYMTAB.  */
16673
16674 static void
16675 write_psymbols (struct mapped_symtab *symtab,
16676                 htab_t psyms_seen,
16677                 struct partial_symbol **psymp,
16678                 int count,
16679                 offset_type cu_index,
16680                 int is_static)
16681 {
16682   for (; count-- > 0; ++psymp)
16683     {
16684       void **slot, *lookup;
16685
16686       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16687         error (_("Ada is not currently supported by the index"));
16688
16689       /* We only want to add a given psymbol once.  However, we also
16690          want to account for whether it is global or static.  So, we
16691          may add it twice, using slightly different values.  */
16692       if (is_static)
16693         {
16694           uintptr_t val = 1 | (uintptr_t) *psymp;
16695
16696           lookup = (void *) val;
16697         }
16698       else
16699         lookup = *psymp;
16700
16701       /* Only add a given psymbol once.  */
16702       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16703       if (!*slot)
16704         {
16705           *slot = lookup;
16706           add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16707         }
16708     }
16709 }
16710
16711 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16712    exception if there is an error.  */
16713
16714 static void
16715 write_obstack (FILE *file, struct obstack *obstack)
16716 {
16717   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16718               file)
16719       != obstack_object_size (obstack))
16720     error (_("couldn't data write to file"));
16721 }
16722
16723 /* Unlink a file if the argument is not NULL.  */
16724
16725 static void
16726 unlink_if_set (void *p)
16727 {
16728   char **filename = p;
16729   if (*filename)
16730     unlink (*filename);
16731 }
16732
16733 /* A helper struct used when iterating over debug_types.  */
16734 struct signatured_type_index_data
16735 {
16736   struct objfile *objfile;
16737   struct mapped_symtab *symtab;
16738   struct obstack *types_list;
16739   htab_t psyms_seen;
16740   int cu_index;
16741 };
16742
16743 /* A helper function that writes a single signatured_type to an
16744    obstack.  */
16745
16746 static int
16747 write_one_signatured_type (void **slot, void *d)
16748 {
16749   struct signatured_type_index_data *info = d;
16750   struct signatured_type *entry = (struct signatured_type *) *slot;
16751   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16752   struct partial_symtab *psymtab = per_cu->v.psymtab;
16753   gdb_byte val[8];
16754
16755   write_psymbols (info->symtab,
16756                   info->psyms_seen,
16757                   info->objfile->global_psymbols.list
16758                   + psymtab->globals_offset,
16759                   psymtab->n_global_syms, info->cu_index,
16760                   0);
16761   write_psymbols (info->symtab,
16762                   info->psyms_seen,
16763                   info->objfile->static_psymbols.list
16764                   + psymtab->statics_offset,
16765                   psymtab->n_static_syms, info->cu_index,
16766                   1);
16767
16768   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16769   obstack_grow (info->types_list, val, 8);
16770   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16771   obstack_grow (info->types_list, val, 8);
16772   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16773   obstack_grow (info->types_list, val, 8);
16774
16775   ++info->cu_index;
16776
16777   return 1;
16778 }
16779
16780 /* Create an index file for OBJFILE in the directory DIR.  */
16781
16782 static void
16783 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16784 {
16785   struct cleanup *cleanup;
16786   char *filename, *cleanup_filename;
16787   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16788   struct obstack cu_list, types_cu_list;
16789   int i;
16790   FILE *out_file;
16791   struct mapped_symtab *symtab;
16792   offset_type val, size_of_contents, total_len;
16793   struct stat st;
16794   char buf[8];
16795   htab_t psyms_seen;
16796   htab_t cu_index_htab;
16797   struct psymtab_cu_index_map *psymtab_cu_index_map;
16798
16799   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16800     return;
16801
16802   if (dwarf2_per_objfile->using_index)
16803     error (_("Cannot use an index to create the index"));
16804
16805   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16806     error (_("Cannot make an index when the file has multiple .debug_types sections"));
16807
16808   if (stat (objfile->name, &st) < 0)
16809     perror_with_name (objfile->name);
16810
16811   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16812                      INDEX_SUFFIX, (char *) NULL);
16813   cleanup = make_cleanup (xfree, filename);
16814
16815   out_file = fopen (filename, "wb");
16816   if (!out_file)
16817     error (_("Can't open `%s' for writing"), filename);
16818
16819   cleanup_filename = filename;
16820   make_cleanup (unlink_if_set, &cleanup_filename);
16821
16822   symtab = create_mapped_symtab ();
16823   make_cleanup (cleanup_mapped_symtab, symtab);
16824
16825   obstack_init (&addr_obstack);
16826   make_cleanup_obstack_free (&addr_obstack);
16827
16828   obstack_init (&cu_list);
16829   make_cleanup_obstack_free (&cu_list);
16830
16831   obstack_init (&types_cu_list);
16832   make_cleanup_obstack_free (&types_cu_list);
16833
16834   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16835                                   NULL, xcalloc, xfree);
16836   make_cleanup_htab_delete (psyms_seen);
16837
16838   /* While we're scanning CU's create a table that maps a psymtab pointer
16839      (which is what addrmap records) to its index (which is what is recorded
16840      in the index file).  This will later be needed to write the address
16841      table.  */
16842   cu_index_htab = htab_create_alloc (100,
16843                                      hash_psymtab_cu_index,
16844                                      eq_psymtab_cu_index,
16845                                      NULL, xcalloc, xfree);
16846   make_cleanup_htab_delete (cu_index_htab);
16847   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16848     xmalloc (sizeof (struct psymtab_cu_index_map)
16849              * dwarf2_per_objfile->n_comp_units);
16850   make_cleanup (xfree, psymtab_cu_index_map);
16851
16852   /* The CU list is already sorted, so we don't need to do additional
16853      work here.  Also, the debug_types entries do not appear in
16854      all_comp_units, but only in their own hash table.  */
16855   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16856     {
16857       struct dwarf2_per_cu_data *per_cu
16858         = dwarf2_per_objfile->all_comp_units[i];
16859       struct partial_symtab *psymtab = per_cu->v.psymtab;
16860       gdb_byte val[8];
16861       struct psymtab_cu_index_map *map;
16862       void **slot;
16863
16864       write_psymbols (symtab,
16865                       psyms_seen,
16866                       objfile->global_psymbols.list + psymtab->globals_offset,
16867                       psymtab->n_global_syms, i,
16868                       0);
16869       write_psymbols (symtab,
16870                       psyms_seen,
16871                       objfile->static_psymbols.list + psymtab->statics_offset,
16872                       psymtab->n_static_syms, i,
16873                       1);
16874
16875       map = &psymtab_cu_index_map[i];
16876       map->psymtab = psymtab;
16877       map->cu_index = i;
16878       slot = htab_find_slot (cu_index_htab, map, INSERT);
16879       gdb_assert (slot != NULL);
16880       gdb_assert (*slot == NULL);
16881       *slot = map;
16882
16883       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16884       obstack_grow (&cu_list, val, 8);
16885       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16886       obstack_grow (&cu_list, val, 8);
16887     }
16888
16889   /* Dump the address map.  */
16890   write_address_map (objfile, &addr_obstack, cu_index_htab);
16891
16892   /* Write out the .debug_type entries, if any.  */
16893   if (dwarf2_per_objfile->signatured_types)
16894     {
16895       struct signatured_type_index_data sig_data;
16896
16897       sig_data.objfile = objfile;
16898       sig_data.symtab = symtab;
16899       sig_data.types_list = &types_cu_list;
16900       sig_data.psyms_seen = psyms_seen;
16901       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16902       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16903                               write_one_signatured_type, &sig_data);
16904     }
16905
16906   obstack_init (&constant_pool);
16907   make_cleanup_obstack_free (&constant_pool);
16908   obstack_init (&symtab_obstack);
16909   make_cleanup_obstack_free (&symtab_obstack);
16910   write_hash_table (symtab, &symtab_obstack, &constant_pool);
16911
16912   obstack_init (&contents);
16913   make_cleanup_obstack_free (&contents);
16914   size_of_contents = 6 * sizeof (offset_type);
16915   total_len = size_of_contents;
16916
16917   /* The version number.  */
16918   val = MAYBE_SWAP (5);
16919   obstack_grow (&contents, &val, sizeof (val));
16920
16921   /* The offset of the CU list from the start of the file.  */
16922   val = MAYBE_SWAP (total_len);
16923   obstack_grow (&contents, &val, sizeof (val));
16924   total_len += obstack_object_size (&cu_list);
16925
16926   /* The offset of the types CU list from the start of the file.  */
16927   val = MAYBE_SWAP (total_len);
16928   obstack_grow (&contents, &val, sizeof (val));
16929   total_len += obstack_object_size (&types_cu_list);
16930
16931   /* The offset of the address table from the start of the file.  */
16932   val = MAYBE_SWAP (total_len);
16933   obstack_grow (&contents, &val, sizeof (val));
16934   total_len += obstack_object_size (&addr_obstack);
16935
16936   /* The offset of the symbol table from the start of the file.  */
16937   val = MAYBE_SWAP (total_len);
16938   obstack_grow (&contents, &val, sizeof (val));
16939   total_len += obstack_object_size (&symtab_obstack);
16940
16941   /* The offset of the constant pool from the start of the file.  */
16942   val = MAYBE_SWAP (total_len);
16943   obstack_grow (&contents, &val, sizeof (val));
16944   total_len += obstack_object_size (&constant_pool);
16945
16946   gdb_assert (obstack_object_size (&contents) == size_of_contents);
16947
16948   write_obstack (out_file, &contents);
16949   write_obstack (out_file, &cu_list);
16950   write_obstack (out_file, &types_cu_list);
16951   write_obstack (out_file, &addr_obstack);
16952   write_obstack (out_file, &symtab_obstack);
16953   write_obstack (out_file, &constant_pool);
16954
16955   fclose (out_file);
16956
16957   /* We want to keep the file, so we set cleanup_filename to NULL
16958      here.  See unlink_if_set.  */
16959   cleanup_filename = NULL;
16960
16961   do_cleanups (cleanup);
16962 }
16963
16964 /* Implementation of the `save gdb-index' command.
16965    
16966    Note that the file format used by this command is documented in the
16967    GDB manual.  Any changes here must be documented there.  */
16968
16969 static void
16970 save_gdb_index_command (char *arg, int from_tty)
16971 {
16972   struct objfile *objfile;
16973
16974   if (!arg || !*arg)
16975     error (_("usage: save gdb-index DIRECTORY"));
16976
16977   ALL_OBJFILES (objfile)
16978   {
16979     struct stat st;
16980
16981     /* If the objfile does not correspond to an actual file, skip it.  */
16982     if (stat (objfile->name, &st) < 0)
16983       continue;
16984
16985     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16986     if (dwarf2_per_objfile)
16987       {
16988         volatile struct gdb_exception except;
16989
16990         TRY_CATCH (except, RETURN_MASK_ERROR)
16991           {
16992             write_psymtabs_to_index (objfile, arg);
16993           }
16994         if (except.reason < 0)
16995           exception_fprintf (gdb_stderr, except,
16996                              _("Error while writing index for `%s': "),
16997                              objfile->name);
16998       }
16999   }
17000 }
17001
17002 \f
17003
17004 int dwarf2_always_disassemble;
17005
17006 static void
17007 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17008                                 struct cmd_list_element *c, const char *value)
17009 {
17010   fprintf_filtered (file,
17011                     _("Whether to always disassemble "
17012                       "DWARF expressions is %s.\n"),
17013                     value);
17014 }
17015
17016 static void
17017 show_check_physname (struct ui_file *file, int from_tty,
17018                      struct cmd_list_element *c, const char *value)
17019 {
17020   fprintf_filtered (file,
17021                     _("Whether to check \"physname\" is %s.\n"),
17022                     value);
17023 }
17024
17025 void _initialize_dwarf2_read (void);
17026
17027 void
17028 _initialize_dwarf2_read (void)
17029 {
17030   struct cmd_list_element *c;
17031
17032   dwarf2_objfile_data_key
17033     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17034
17035   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17036 Set DWARF 2 specific variables.\n\
17037 Configure DWARF 2 variables such as the cache size"),
17038                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17039                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17040
17041   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17042 Show DWARF 2 specific variables\n\
17043 Show DWARF 2 variables such as the cache size"),
17044                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17045                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17046
17047   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17048                             &dwarf2_max_cache_age, _("\
17049 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17050 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17051 A higher limit means that cached compilation units will be stored\n\
17052 in memory longer, and more total memory will be used.  Zero disables\n\
17053 caching, which can slow down startup."),
17054                             NULL,
17055                             show_dwarf2_max_cache_age,
17056                             &set_dwarf2_cmdlist,
17057                             &show_dwarf2_cmdlist);
17058
17059   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17060                            &dwarf2_always_disassemble, _("\
17061 Set whether `info address' always disassembles DWARF expressions."), _("\
17062 Show whether `info address' always disassembles DWARF expressions."), _("\
17063 When enabled, DWARF expressions are always printed in an assembly-like\n\
17064 syntax.  When disabled, expressions will be printed in a more\n\
17065 conversational style, when possible."),
17066                            NULL,
17067                            show_dwarf2_always_disassemble,
17068                            &set_dwarf2_cmdlist,
17069                            &show_dwarf2_cmdlist);
17070
17071   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17072 Set debugging of the dwarf2 DIE reader."), _("\
17073 Show debugging of the dwarf2 DIE reader."), _("\
17074 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17075 The value is the maximum depth to print."),
17076                             NULL,
17077                             NULL,
17078                             &setdebuglist, &showdebuglist);
17079
17080   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17081 Set cross-checking of \"physname\" code against demangler."), _("\
17082 Show cross-checking of \"physname\" code against demangler."), _("\
17083 When enabled, GDB's internal \"physname\" code is checked against\n\
17084 the demangler."),
17085                            NULL, show_check_physname,
17086                            &setdebuglist, &showdebuglist);
17087
17088   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17089                _("\
17090 Save a gdb-index file.\n\
17091 Usage: save gdb-index DIRECTORY"),
17092                &save_cmdlist);
17093   set_cmd_completer (c, filename_completer);
17094 }