* defs.h (is_cplus_marker, set_demangling_style): Moved to ...
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "gdb-demangle.h"
38 #include "expression.h"
39 #include "filenames.h"  /* for DOSish file names */
40 #include "macrotab.h"
41 #include "language.h"
42 #include "complaints.h"
43 #include "bcache.h"
44 #include "dwarf2expr.h"
45 #include "dwarf2loc.h"
46 #include "cp-support.h"
47 #include "hashtab.h"
48 #include "command.h"
49 #include "gdbcmd.h"
50 #include "block.h"
51 #include "addrmap.h"
52 #include "typeprint.h"
53 #include "jv-lang.h"
54 #include "psympriv.h"
55 #include "exceptions.h"
56 #include "gdb_stat.h"
57 #include "completer.h"
58 #include "vec.h"
59 #include "c-lang.h"
60 #include "valprint.h"
61 #include <ctype.h>
62
63 #include <fcntl.h>
64 #include "gdb_string.h"
65 #include "gdb_assert.h"
66 #include <sys/types.h>
67 #ifdef HAVE_ZLIB_H
68 #include <zlib.h>
69 #endif
70 #ifdef HAVE_MMAP
71 #include <sys/mman.h>
72 #ifndef MAP_FAILED
73 #define MAP_FAILED ((void *) -1)
74 #endif
75 #endif
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 #if 0
81 /* .debug_info header for a compilation unit
82    Because of alignment constraints, this structure has padding and cannot
83    be mapped directly onto the beginning of the .debug_info section.  */
84 typedef struct comp_unit_header
85   {
86     unsigned int length;        /* length of the .debug_info
87                                    contribution */
88     unsigned short version;     /* version number -- 2 for DWARF
89                                    version 2 */
90     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
91     unsigned char addr_size;    /* byte size of an address -- 4 */
92   }
93 _COMP_UNIT_HEADER;
94 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
95 #endif
96
97 /* .debug_line statement program prologue
98    Because of alignment constraints, this structure has padding and cannot
99    be mapped directly onto the beginning of the .debug_info section.  */
100 typedef struct statement_prologue
101   {
102     unsigned int total_length;  /* byte length of the statement
103                                    information */
104     unsigned short version;     /* version number -- 2 for DWARF
105                                    version 2 */
106     unsigned int prologue_length;       /* # bytes between prologue &
107                                            stmt program */
108     unsigned char minimum_instruction_length;   /* byte size of
109                                                    smallest instr */
110     unsigned char default_is_stmt;      /* initial value of is_stmt
111                                            register */
112     char line_base;
113     unsigned char line_range;
114     unsigned char opcode_base;  /* number assigned to first special
115                                    opcode */
116     unsigned char *standard_opcode_lengths;
117   }
118 _STATEMENT_PROLOGUE;
119
120 /* When non-zero, dump DIEs after they are read in.  */
121 static int dwarf2_die_debug = 0;
122
123 /* When non-zero, cross-check physname against demangler.  */
124 static int check_physname = 0;
125
126 static int pagesize;
127
128 /* When set, the file that we're processing is known to have debugging
129    info for C++ namespaces.  GCC 3.3.x did not produce this information,
130    but later versions do.  */
131
132 static int processing_has_namespace_info;
133
134 static const struct objfile_data *dwarf2_objfile_data_key;
135
136 struct dwarf2_section_info
137 {
138   asection *asection;
139   gdb_byte *buffer;
140   bfd_size_type size;
141   /* Not NULL if the section was actually mmapped.  */
142   void *map_addr;
143   /* Page aligned size of mmapped area.  */
144   bfd_size_type map_len;
145   /* True if we have tried to read this section.  */
146   int readin;
147 };
148
149 typedef struct dwarf2_section_info dwarf2_section_info_def;
150 DEF_VEC_O (dwarf2_section_info_def);
151
152 /* All offsets in the index are of this type.  It must be
153    architecture-independent.  */
154 typedef uint32_t offset_type;
155
156 DEF_VEC_I (offset_type);
157
158 /* A description of the mapped index.  The file format is described in
159    a comment by the code that writes the index.  */
160 struct mapped_index
161 {
162   /* Index data format version.  */
163   int version;
164
165   /* The total length of the buffer.  */
166   off_t total_size;
167
168   /* A pointer to the address table data.  */
169   const gdb_byte *address_table;
170
171   /* Size of the address table data in bytes.  */
172   offset_type address_table_size;
173
174   /* The symbol table, implemented as a hash table.  */
175   const offset_type *symbol_table;
176
177   /* Size in slots, each slot is 2 offset_types.  */
178   offset_type symbol_table_slots;
179
180   /* A pointer to the constant pool.  */
181   const char *constant_pool;
182 };
183
184 struct dwarf2_per_objfile
185 {
186   struct dwarf2_section_info info;
187   struct dwarf2_section_info abbrev;
188   struct dwarf2_section_info line;
189   struct dwarf2_section_info loc;
190   struct dwarf2_section_info macinfo;
191   struct dwarf2_section_info macro;
192   struct dwarf2_section_info str;
193   struct dwarf2_section_info ranges;
194   struct dwarf2_section_info frame;
195   struct dwarf2_section_info eh_frame;
196   struct dwarf2_section_info gdb_index;
197
198   VEC (dwarf2_section_info_def) *types;
199
200   /* Back link.  */
201   struct objfile *objfile;
202
203   /* A list of all the compilation units.  This is used to locate
204      the target compilation unit of a particular reference.  */
205   struct dwarf2_per_cu_data **all_comp_units;
206
207   /* The number of compilation units in ALL_COMP_UNITS.  */
208   int n_comp_units;
209
210   /* The number of .debug_types-related CUs.  */
211   int n_type_comp_units;
212
213   /* The .debug_types-related CUs.  */
214   struct dwarf2_per_cu_data **type_comp_units;
215
216   /* A chain of compilation units that are currently read in, so that
217      they can be freed later.  */
218   struct dwarf2_per_cu_data *read_in_chain;
219
220   /* A table mapping .debug_types signatures to its signatured_type entry.
221      This is NULL if the .debug_types section hasn't been read in yet.  */
222   htab_t signatured_types;
223
224   /* A flag indicating wether this objfile has a section loaded at a
225      VMA of 0.  */
226   int has_section_at_zero;
227
228   /* True if we are using the mapped index,
229      or we are faking it for OBJF_READNOW's sake.  */
230   unsigned char using_index;
231
232   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
233   struct mapped_index *index_table;
234
235   /* When using index_table, this keeps track of all quick_file_names entries.
236      TUs can share line table entries with CUs or other TUs, and there can be
237      a lot more TUs than unique line tables, so we maintain a separate table
238      of all line table entries to support the sharing.  */
239   htab_t quick_file_names_table;
240
241   /* Set during partial symbol reading, to prevent queueing of full
242      symbols.  */
243   int reading_partial_symbols;
244
245   /* Table mapping type .debug_info DIE offsets to types.
246      This is NULL if not allocated yet.
247      It (currently) makes sense to allocate debug_types_type_hash lazily.
248      To keep things simple we allocate both lazily.  */
249   htab_t debug_info_type_hash;
250
251   /* Table mapping type .debug_types DIE offsets to types.
252      This is NULL if not allocated yet.  */
253   htab_t debug_types_type_hash;
254 };
255
256 static struct dwarf2_per_objfile *dwarf2_per_objfile;
257
258 /* Default names of the debugging sections.  */
259
260 /* Note that if the debugging section has been compressed, it might
261    have a name like .zdebug_info.  */
262
263 static const struct dwarf2_debug_sections dwarf2_elf_names = {
264   { ".debug_info", ".zdebug_info" },
265   { ".debug_abbrev", ".zdebug_abbrev" },
266   { ".debug_line", ".zdebug_line" },
267   { ".debug_loc", ".zdebug_loc" },
268   { ".debug_macinfo", ".zdebug_macinfo" },
269   { ".debug_macro", ".zdebug_macro" },
270   { ".debug_str", ".zdebug_str" },
271   { ".debug_ranges", ".zdebug_ranges" },
272   { ".debug_types", ".zdebug_types" },
273   { ".debug_frame", ".zdebug_frame" },
274   { ".eh_frame", NULL },
275   { ".gdb_index", ".zgdb_index" },
276   23
277 };
278
279 /* local data types */
280
281 /* We hold several abbreviation tables in memory at the same time.  */
282 #ifndef ABBREV_HASH_SIZE
283 #define ABBREV_HASH_SIZE 121
284 #endif
285
286 /* The data in a compilation unit header, after target2host
287    translation, looks like this.  */
288 struct comp_unit_head
289 {
290   unsigned int length;
291   short version;
292   unsigned char addr_size;
293   unsigned char signed_addr_p;
294   unsigned int abbrev_offset;
295
296   /* Size of file offsets; either 4 or 8.  */
297   unsigned int offset_size;
298
299   /* Size of the length field; either 4 or 12.  */
300   unsigned int initial_length_size;
301
302   /* Offset to the first byte of this compilation unit header in the
303      .debug_info section, for resolving relative reference dies.  */
304   unsigned int offset;
305
306   /* Offset to first die in this cu from the start of the cu.
307      This will be the first byte following the compilation unit header.  */
308   unsigned int first_die_offset;
309 };
310
311 /* Type used for delaying computation of method physnames.
312    See comments for compute_delayed_physnames.  */
313 struct delayed_method_info
314 {
315   /* The type to which the method is attached, i.e., its parent class.  */
316   struct type *type;
317
318   /* The index of the method in the type's function fieldlists.  */
319   int fnfield_index;
320
321   /* The index of the method in the fieldlist.  */
322   int index;
323
324   /* The name of the DIE.  */
325   const char *name;
326
327   /*  The DIE associated with this method.  */
328   struct die_info *die;
329 };
330
331 typedef struct delayed_method_info delayed_method_info;
332 DEF_VEC_O (delayed_method_info);
333
334 /* Internal state when decoding a particular compilation unit.  */
335 struct dwarf2_cu
336 {
337   /* The objfile containing this compilation unit.  */
338   struct objfile *objfile;
339
340   /* The header of the compilation unit.  */
341   struct comp_unit_head header;
342
343   /* Base address of this compilation unit.  */
344   CORE_ADDR base_address;
345
346   /* Non-zero if base_address has been set.  */
347   int base_known;
348
349   struct function_range *first_fn, *last_fn, *cached_fn;
350
351   /* The language we are debugging.  */
352   enum language language;
353   const struct language_defn *language_defn;
354
355   const char *producer;
356
357   /* The generic symbol table building routines have separate lists for
358      file scope symbols and all all other scopes (local scopes).  So
359      we need to select the right one to pass to add_symbol_to_list().
360      We do it by keeping a pointer to the correct list in list_in_scope.
361
362      FIXME: The original dwarf code just treated the file scope as the
363      first local scope, and all other local scopes as nested local
364      scopes, and worked fine.  Check to see if we really need to
365      distinguish these in buildsym.c.  */
366   struct pending **list_in_scope;
367
368   /* DWARF abbreviation table associated with this compilation unit.  */
369   struct abbrev_info **dwarf2_abbrevs;
370
371   /* Storage for the abbrev table.  */
372   struct obstack abbrev_obstack;
373
374   /* Hash table holding all the loaded partial DIEs.  */
375   htab_t partial_dies;
376
377   /* Storage for things with the same lifetime as this read-in compilation
378      unit, including partial DIEs.  */
379   struct obstack comp_unit_obstack;
380
381   /* When multiple dwarf2_cu structures are living in memory, this field
382      chains them all together, so that they can be released efficiently.
383      We will probably also want a generation counter so that most-recently-used
384      compilation units are cached...  */
385   struct dwarf2_per_cu_data *read_in_chain;
386
387   /* Backchain to our per_cu entry if the tree has been built.  */
388   struct dwarf2_per_cu_data *per_cu;
389
390   /* How many compilation units ago was this CU last referenced?  */
391   int last_used;
392
393   /* A hash table of die offsets for following references.  */
394   htab_t die_hash;
395
396   /* Full DIEs if read in.  */
397   struct die_info *dies;
398
399   /* A set of pointers to dwarf2_per_cu_data objects for compilation
400      units referenced by this one.  Only set during full symbol processing;
401      partial symbol tables do not have dependencies.  */
402   htab_t dependencies;
403
404   /* Header data from the line table, during full symbol processing.  */
405   struct line_header *line_header;
406
407   /* A list of methods which need to have physnames computed
408      after all type information has been read.  */
409   VEC (delayed_method_info) *method_list;
410
411   /* To be copied to symtab->call_site_htab.  */
412   htab_t call_site_htab;
413
414   /* Mark used when releasing cached dies.  */
415   unsigned int mark : 1;
416
417   /* This flag will be set if this compilation unit might include
418      inter-compilation-unit references.  */
419   unsigned int has_form_ref_addr : 1;
420
421   /* This flag will be set if this compilation unit includes any
422      DW_TAG_namespace DIEs.  If we know that there are explicit
423      DIEs for namespaces, we don't need to try to infer them
424      from mangled names.  */
425   unsigned int has_namespace_info : 1;
426
427   /* This CU references .debug_loc.  See the symtab->locations_valid field.
428      This test is imperfect as there may exist optimized debug code not using
429      any location list and still facing inlining issues if handled as
430      unoptimized code.  For a future better test see GCC PR other/32998.  */
431
432   unsigned int has_loclist : 1;
433 };
434
435 /* Persistent data held for a compilation unit, even when not
436    processing it.  We put a pointer to this structure in the
437    read_symtab_private field of the psymtab.  If we encounter
438    inter-compilation-unit references, we also maintain a sorted
439    list of all compilation units.  */
440
441 struct dwarf2_per_cu_data
442 {
443   /* The start offset and length of this compilation unit.  2**29-1
444      bytes should suffice to store the length of any compilation unit
445      - if it doesn't, GDB will fall over anyway.
446      NOTE: Unlike comp_unit_head.length, this length includes
447      initial_length_size.  */
448   unsigned int offset;
449   unsigned int length : 29;
450
451   /* Flag indicating this compilation unit will be read in before
452      any of the current compilation units are processed.  */
453   unsigned int queued : 1;
454
455   /* This flag will be set if we need to load absolutely all DIEs
456      for this compilation unit, instead of just the ones we think
457      are interesting.  It gets set if we look for a DIE in the
458      hash table and don't find it.  */
459   unsigned int load_all_dies : 1;
460
461   /* Non-null if this CU is from .debug_types; in which case it points
462      to the section.  Otherwise it's from .debug_info.  */
463   struct dwarf2_section_info *debug_type_section;
464
465   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
466      of the CU cache it gets reset to NULL again.  */
467   struct dwarf2_cu *cu;
468
469   /* The corresponding objfile.  */
470   struct objfile *objfile;
471
472   /* When using partial symbol tables, the 'psymtab' field is active.
473      Otherwise the 'quick' field is active.  */
474   union
475   {
476     /* The partial symbol table associated with this compilation unit,
477        or NULL for partial units (which do not have an associated
478        symtab).  */
479     struct partial_symtab *psymtab;
480
481     /* Data needed by the "quick" functions.  */
482     struct dwarf2_per_cu_quick_data *quick;
483   } v;
484 };
485
486 /* Entry in the signatured_types hash table.  */
487
488 struct signatured_type
489 {
490   ULONGEST signature;
491
492   /* Offset in .debug_types of the type defined by this TU.  */
493   unsigned int type_offset;
494
495   /* The CU(/TU) of this type.  */
496   struct dwarf2_per_cu_data per_cu;
497 };
498
499 /* Struct used to pass misc. parameters to read_die_and_children, et
500    al.  which are used for both .debug_info and .debug_types dies.
501    All parameters here are unchanging for the life of the call.  This
502    struct exists to abstract away the constant parameters of die
503    reading.  */
504
505 struct die_reader_specs
506 {
507   /* The bfd of this objfile.  */
508   bfd* abfd;
509
510   /* The CU of the DIE we are parsing.  */
511   struct dwarf2_cu *cu;
512
513   /* Pointer to start of section buffer.
514      This is either the start of .debug_info or .debug_types.  */
515   const gdb_byte *buffer;
516 };
517
518 /* The line number information for a compilation unit (found in the
519    .debug_line section) begins with a "statement program header",
520    which contains the following information.  */
521 struct line_header
522 {
523   unsigned int total_length;
524   unsigned short version;
525   unsigned int header_length;
526   unsigned char minimum_instruction_length;
527   unsigned char maximum_ops_per_instruction;
528   unsigned char default_is_stmt;
529   int line_base;
530   unsigned char line_range;
531   unsigned char opcode_base;
532
533   /* standard_opcode_lengths[i] is the number of operands for the
534      standard opcode whose value is i.  This means that
535      standard_opcode_lengths[0] is unused, and the last meaningful
536      element is standard_opcode_lengths[opcode_base - 1].  */
537   unsigned char *standard_opcode_lengths;
538
539   /* The include_directories table.  NOTE!  These strings are not
540      allocated with xmalloc; instead, they are pointers into
541      debug_line_buffer.  If you try to free them, `free' will get
542      indigestion.  */
543   unsigned int num_include_dirs, include_dirs_size;
544   char **include_dirs;
545
546   /* The file_names table.  NOTE!  These strings are not allocated
547      with xmalloc; instead, they are pointers into debug_line_buffer.
548      Don't try to free them directly.  */
549   unsigned int num_file_names, file_names_size;
550   struct file_entry
551   {
552     char *name;
553     unsigned int dir_index;
554     unsigned int mod_time;
555     unsigned int length;
556     int included_p; /* Non-zero if referenced by the Line Number Program.  */
557     struct symtab *symtab; /* The associated symbol table, if any.  */
558   } *file_names;
559
560   /* The start and end of the statement program following this
561      header.  These point into dwarf2_per_objfile->line_buffer.  */
562   gdb_byte *statement_program_start, *statement_program_end;
563 };
564
565 /* When we construct a partial symbol table entry we only
566    need this much information.  */
567 struct partial_die_info
568   {
569     /* Offset of this DIE.  */
570     unsigned int offset;
571
572     /* DWARF-2 tag for this DIE.  */
573     ENUM_BITFIELD(dwarf_tag) tag : 16;
574
575     /* Assorted flags describing the data found in this DIE.  */
576     unsigned int has_children : 1;
577     unsigned int is_external : 1;
578     unsigned int is_declaration : 1;
579     unsigned int has_type : 1;
580     unsigned int has_specification : 1;
581     unsigned int has_pc_info : 1;
582
583     /* Flag set if the SCOPE field of this structure has been
584        computed.  */
585     unsigned int scope_set : 1;
586
587     /* Flag set if the DIE has a byte_size attribute.  */
588     unsigned int has_byte_size : 1;
589
590     /* Flag set if any of the DIE's children are template arguments.  */
591     unsigned int has_template_arguments : 1;
592
593     /* Flag set if fixup_partial_die has been called on this die.  */
594     unsigned int fixup_called : 1;
595
596     /* The name of this DIE.  Normally the value of DW_AT_name, but
597        sometimes a default name for unnamed DIEs.  */
598     char *name;
599
600     /* The linkage name, if present.  */
601     const char *linkage_name;
602
603     /* The scope to prepend to our children.  This is generally
604        allocated on the comp_unit_obstack, so will disappear
605        when this compilation unit leaves the cache.  */
606     char *scope;
607
608     /* The location description associated with this DIE, if any.  */
609     struct dwarf_block *locdesc;
610
611     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
612     CORE_ADDR lowpc;
613     CORE_ADDR highpc;
614
615     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
616        DW_AT_sibling, if any.  */
617     /* NOTE: This member isn't strictly necessary, read_partial_die could
618        return DW_AT_sibling values to its caller load_partial_dies.  */
619     gdb_byte *sibling;
620
621     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
622        DW_AT_specification (or DW_AT_abstract_origin or
623        DW_AT_extension).  */
624     unsigned int spec_offset;
625
626     /* Pointers to this DIE's parent, first child, and next sibling,
627        if any.  */
628     struct partial_die_info *die_parent, *die_child, *die_sibling;
629   };
630
631 /* This data structure holds the information of an abbrev.  */
632 struct abbrev_info
633   {
634     unsigned int number;        /* number identifying abbrev */
635     enum dwarf_tag tag;         /* dwarf tag */
636     unsigned short has_children;                /* boolean */
637     unsigned short num_attrs;   /* number of attributes */
638     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
639     struct abbrev_info *next;   /* next in chain */
640   };
641
642 struct attr_abbrev
643   {
644     ENUM_BITFIELD(dwarf_attribute) name : 16;
645     ENUM_BITFIELD(dwarf_form) form : 16;
646   };
647
648 /* Attributes have a name and a value.  */
649 struct attribute
650   {
651     ENUM_BITFIELD(dwarf_attribute) name : 16;
652     ENUM_BITFIELD(dwarf_form) form : 15;
653
654     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
655        field should be in u.str (existing only for DW_STRING) but it is kept
656        here for better struct attribute alignment.  */
657     unsigned int string_is_canonical : 1;
658
659     union
660       {
661         char *str;
662         struct dwarf_block *blk;
663         ULONGEST unsnd;
664         LONGEST snd;
665         CORE_ADDR addr;
666         struct signatured_type *signatured_type;
667       }
668     u;
669   };
670
671 /* This data structure holds a complete die structure.  */
672 struct die_info
673   {
674     /* DWARF-2 tag for this DIE.  */
675     ENUM_BITFIELD(dwarf_tag) tag : 16;
676
677     /* Number of attributes */
678     unsigned char num_attrs;
679
680     /* True if we're presently building the full type name for the
681        type derived from this DIE.  */
682     unsigned char building_fullname : 1;
683
684     /* Abbrev number */
685     unsigned int abbrev;
686
687     /* Offset in .debug_info or .debug_types section.  */
688     unsigned int offset;
689
690     /* The dies in a compilation unit form an n-ary tree.  PARENT
691        points to this die's parent; CHILD points to the first child of
692        this node; and all the children of a given node are chained
693        together via their SIBLING fields.  */
694     struct die_info *child;     /* Its first child, if any.  */
695     struct die_info *sibling;   /* Its next sibling, if any.  */
696     struct die_info *parent;    /* Its parent, if any.  */
697
698     /* An array of attributes, with NUM_ATTRS elements.  There may be
699        zero, but it's not common and zero-sized arrays are not
700        sufficiently portable C.  */
701     struct attribute attrs[1];
702   };
703
704 struct function_range
705 {
706   const char *name;
707   CORE_ADDR lowpc, highpc;
708   int seen_line;
709   struct function_range *next;
710 };
711
712 /* Get at parts of an attribute structure.  */
713
714 #define DW_STRING(attr)    ((attr)->u.str)
715 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
716 #define DW_UNSND(attr)     ((attr)->u.unsnd)
717 #define DW_BLOCK(attr)     ((attr)->u.blk)
718 #define DW_SND(attr)       ((attr)->u.snd)
719 #define DW_ADDR(attr)      ((attr)->u.addr)
720 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
721
722 /* Blocks are a bunch of untyped bytes.  */
723 struct dwarf_block
724   {
725     unsigned int size;
726
727     /* Valid only if SIZE is not zero.  */
728     gdb_byte *data;
729   };
730
731 #ifndef ATTR_ALLOC_CHUNK
732 #define ATTR_ALLOC_CHUNK 4
733 #endif
734
735 /* Allocate fields for structs, unions and enums in this size.  */
736 #ifndef DW_FIELD_ALLOC_CHUNK
737 #define DW_FIELD_ALLOC_CHUNK 4
738 #endif
739
740 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
741    but this would require a corresponding change in unpack_field_as_long
742    and friends.  */
743 static int bits_per_byte = 8;
744
745 /* The routines that read and process dies for a C struct or C++ class
746    pass lists of data member fields and lists of member function fields
747    in an instance of a field_info structure, as defined below.  */
748 struct field_info
749   {
750     /* List of data member and baseclasses fields.  */
751     struct nextfield
752       {
753         struct nextfield *next;
754         int accessibility;
755         int virtuality;
756         struct field field;
757       }
758      *fields, *baseclasses;
759
760     /* Number of fields (including baseclasses).  */
761     int nfields;
762
763     /* Number of baseclasses.  */
764     int nbaseclasses;
765
766     /* Set if the accesibility of one of the fields is not public.  */
767     int non_public_fields;
768
769     /* Member function fields array, entries are allocated in the order they
770        are encountered in the object file.  */
771     struct nextfnfield
772       {
773         struct nextfnfield *next;
774         struct fn_field fnfield;
775       }
776      *fnfields;
777
778     /* Member function fieldlist array, contains name of possibly overloaded
779        member function, number of overloaded member functions and a pointer
780        to the head of the member function field chain.  */
781     struct fnfieldlist
782       {
783         char *name;
784         int length;
785         struct nextfnfield *head;
786       }
787      *fnfieldlists;
788
789     /* Number of entries in the fnfieldlists array.  */
790     int nfnfields;
791
792     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
793        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
794     struct typedef_field_list
795       {
796         struct typedef_field field;
797         struct typedef_field_list *next;
798       }
799     *typedef_field_list;
800     unsigned typedef_field_list_count;
801   };
802
803 /* One item on the queue of compilation units to read in full symbols
804    for.  */
805 struct dwarf2_queue_item
806 {
807   struct dwarf2_per_cu_data *per_cu;
808   struct dwarf2_queue_item *next;
809 };
810
811 /* The current queue.  */
812 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
813
814 /* Loaded secondary compilation units are kept in memory until they
815    have not been referenced for the processing of this many
816    compilation units.  Set this to zero to disable caching.  Cache
817    sizes of up to at least twenty will improve startup time for
818    typical inter-CU-reference binaries, at an obvious memory cost.  */
819 static int dwarf2_max_cache_age = 5;
820 static void
821 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
822                            struct cmd_list_element *c, const char *value)
823 {
824   fprintf_filtered (file, _("The upper bound on the age of cached "
825                             "dwarf2 compilation units is %s.\n"),
826                     value);
827 }
828
829
830 /* Various complaints about symbol reading that don't abort the process.  */
831
832 static void
833 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
834 {
835   complaint (&symfile_complaints,
836              _("statement list doesn't fit in .debug_line section"));
837 }
838
839 static void
840 dwarf2_debug_line_missing_file_complaint (void)
841 {
842   complaint (&symfile_complaints,
843              _(".debug_line section has line data without a file"));
844 }
845
846 static void
847 dwarf2_debug_line_missing_end_sequence_complaint (void)
848 {
849   complaint (&symfile_complaints,
850              _(".debug_line section has line "
851                "program sequence without an end"));
852 }
853
854 static void
855 dwarf2_complex_location_expr_complaint (void)
856 {
857   complaint (&symfile_complaints, _("location expression too complex"));
858 }
859
860 static void
861 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
862                                               int arg3)
863 {
864   complaint (&symfile_complaints,
865              _("const value length mismatch for '%s', got %d, expected %d"),
866              arg1, arg2, arg3);
867 }
868
869 static void
870 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
871 {
872   complaint (&symfile_complaints,
873              _("macro info runs off end of `%s' section"),
874              section->asection->name);
875 }
876
877 static void
878 dwarf2_macro_malformed_definition_complaint (const char *arg1)
879 {
880   complaint (&symfile_complaints,
881              _("macro debug info contains a "
882                "malformed macro definition:\n`%s'"),
883              arg1);
884 }
885
886 static void
887 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
888 {
889   complaint (&symfile_complaints,
890              _("invalid attribute class or form for '%s' in '%s'"),
891              arg1, arg2);
892 }
893
894 /* local function prototypes */
895
896 static void dwarf2_locate_sections (bfd *, asection *, void *);
897
898 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
899                                            struct objfile *);
900
901 static void dwarf2_find_base_address (struct die_info *die,
902                                       struct dwarf2_cu *cu);
903
904 static void dwarf2_build_psymtabs_hard (struct objfile *);
905
906 static void scan_partial_symbols (struct partial_die_info *,
907                                   CORE_ADDR *, CORE_ADDR *,
908                                   int, struct dwarf2_cu *);
909
910 static void add_partial_symbol (struct partial_die_info *,
911                                 struct dwarf2_cu *);
912
913 static void add_partial_namespace (struct partial_die_info *pdi,
914                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
915                                    int need_pc, struct dwarf2_cu *cu);
916
917 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
918                                 CORE_ADDR *highpc, int need_pc,
919                                 struct dwarf2_cu *cu);
920
921 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
922                                      struct dwarf2_cu *cu);
923
924 static void add_partial_subprogram (struct partial_die_info *pdi,
925                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
926                                     int need_pc, struct dwarf2_cu *cu);
927
928 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
929                                      gdb_byte *buffer, gdb_byte *info_ptr,
930                                      bfd *abfd, struct dwarf2_cu *cu);
931
932 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
933
934 static void psymtab_to_symtab_1 (struct partial_symtab *);
935
936 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
937
938 static void dwarf2_free_abbrev_table (void *);
939
940 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
941
942 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
943                                             struct dwarf2_cu *);
944
945 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
946                                                  struct dwarf2_cu *);
947
948 static struct partial_die_info *load_partial_dies (bfd *,
949                                                    gdb_byte *, gdb_byte *,
950                                                    int, struct dwarf2_cu *);
951
952 static gdb_byte *read_partial_die (struct partial_die_info *,
953                                    struct abbrev_info *abbrev,
954                                    unsigned int, bfd *,
955                                    gdb_byte *, gdb_byte *,
956                                    struct dwarf2_cu *);
957
958 static struct partial_die_info *find_partial_die (unsigned int,
959                                                   struct dwarf2_cu *);
960
961 static void fixup_partial_die (struct partial_die_info *,
962                                struct dwarf2_cu *);
963
964 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
965                                  bfd *, gdb_byte *, struct dwarf2_cu *);
966
967 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
968                                        bfd *, gdb_byte *, struct dwarf2_cu *);
969
970 static unsigned int read_1_byte (bfd *, gdb_byte *);
971
972 static int read_1_signed_byte (bfd *, gdb_byte *);
973
974 static unsigned int read_2_bytes (bfd *, gdb_byte *);
975
976 static unsigned int read_4_bytes (bfd *, gdb_byte *);
977
978 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
979
980 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
981                                unsigned int *);
982
983 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
984
985 static LONGEST read_checked_initial_length_and_offset
986   (bfd *, gdb_byte *, const struct comp_unit_head *,
987    unsigned int *, unsigned int *);
988
989 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
990                             unsigned int *);
991
992 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
993
994 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
995
996 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
997
998 static char *read_indirect_string (bfd *, gdb_byte *,
999                                    const struct comp_unit_head *,
1000                                    unsigned int *);
1001
1002 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1003
1004 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1005
1006 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1007
1008 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1009
1010 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1011                                       struct dwarf2_cu *);
1012
1013 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1014                                                 unsigned int,
1015                                                 struct dwarf2_cu *);
1016
1017 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1018                                struct dwarf2_cu *cu);
1019
1020 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1021
1022 static struct die_info *die_specification (struct die_info *die,
1023                                            struct dwarf2_cu **);
1024
1025 static void free_line_header (struct line_header *lh);
1026
1027 static void add_file_name (struct line_header *, char *, unsigned int,
1028                            unsigned int, unsigned int);
1029
1030 static struct line_header *(dwarf_decode_line_header
1031                             (unsigned int offset,
1032                              bfd *abfd, struct dwarf2_cu *cu));
1033
1034 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1035                                 struct dwarf2_cu *, struct partial_symtab *);
1036
1037 static void dwarf2_start_subfile (char *, const char *, const char *);
1038
1039 static struct symbol *new_symbol (struct die_info *, struct type *,
1040                                   struct dwarf2_cu *);
1041
1042 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1043                                        struct dwarf2_cu *, struct symbol *);
1044
1045 static void dwarf2_const_value (struct attribute *, struct symbol *,
1046                                 struct dwarf2_cu *);
1047
1048 static void dwarf2_const_value_attr (struct attribute *attr,
1049                                      struct type *type,
1050                                      const char *name,
1051                                      struct obstack *obstack,
1052                                      struct dwarf2_cu *cu, long *value,
1053                                      gdb_byte **bytes,
1054                                      struct dwarf2_locexpr_baton **baton);
1055
1056 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1057
1058 static int need_gnat_info (struct dwarf2_cu *);
1059
1060 static struct type *die_descriptive_type (struct die_info *,
1061                                           struct dwarf2_cu *);
1062
1063 static void set_descriptive_type (struct type *, struct die_info *,
1064                                   struct dwarf2_cu *);
1065
1066 static struct type *die_containing_type (struct die_info *,
1067                                          struct dwarf2_cu *);
1068
1069 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1070                                      struct dwarf2_cu *);
1071
1072 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1073
1074 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1075
1076 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1077
1078 static char *typename_concat (struct obstack *obs, const char *prefix,
1079                               const char *suffix, int physname,
1080                               struct dwarf2_cu *cu);
1081
1082 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1083
1084 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1085
1086 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1087
1088 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1089
1090 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1091
1092 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1093                                struct dwarf2_cu *, struct partial_symtab *);
1094
1095 static int dwarf2_get_pc_bounds (struct die_info *,
1096                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1097                                  struct partial_symtab *);
1098
1099 static void get_scope_pc_bounds (struct die_info *,
1100                                  CORE_ADDR *, CORE_ADDR *,
1101                                  struct dwarf2_cu *);
1102
1103 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1104                                         CORE_ADDR, struct dwarf2_cu *);
1105
1106 static void dwarf2_add_field (struct field_info *, struct die_info *,
1107                               struct dwarf2_cu *);
1108
1109 static void dwarf2_attach_fields_to_type (struct field_info *,
1110                                           struct type *, struct dwarf2_cu *);
1111
1112 static void dwarf2_add_member_fn (struct field_info *,
1113                                   struct die_info *, struct type *,
1114                                   struct dwarf2_cu *);
1115
1116 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1117                                              struct type *,
1118                                              struct dwarf2_cu *);
1119
1120 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1121
1122 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1123
1124 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1125
1126 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1127
1128 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1129
1130 static struct type *read_module_type (struct die_info *die,
1131                                       struct dwarf2_cu *cu);
1132
1133 static const char *namespace_name (struct die_info *die,
1134                                    int *is_anonymous, struct dwarf2_cu *);
1135
1136 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1137
1138 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1139
1140 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1141                                                        struct dwarf2_cu *);
1142
1143 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1144
1145 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1146                                                  gdb_byte *info_ptr,
1147                                                  gdb_byte **new_info_ptr,
1148                                                  struct die_info *parent);
1149
1150 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1151                                                gdb_byte *info_ptr,
1152                                                gdb_byte **new_info_ptr,
1153                                                struct die_info *parent);
1154
1155 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1156                                                gdb_byte *info_ptr,
1157                                                gdb_byte **new_info_ptr,
1158                                                struct die_info *parent);
1159
1160 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1161                                 struct die_info **, gdb_byte *,
1162                                 int *);
1163
1164 static void process_die (struct die_info *, struct dwarf2_cu *);
1165
1166 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1167                                        struct obstack *);
1168
1169 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1170
1171 static const char *dwarf2_full_name (char *name,
1172                                      struct die_info *die,
1173                                      struct dwarf2_cu *cu);
1174
1175 static struct die_info *dwarf2_extension (struct die_info *die,
1176                                           struct dwarf2_cu **);
1177
1178 static char *dwarf_tag_name (unsigned int);
1179
1180 static char *dwarf_attr_name (unsigned int);
1181
1182 static char *dwarf_form_name (unsigned int);
1183
1184 static char *dwarf_bool_name (unsigned int);
1185
1186 static char *dwarf_type_encoding_name (unsigned int);
1187
1188 #if 0
1189 static char *dwarf_cfi_name (unsigned int);
1190 #endif
1191
1192 static struct die_info *sibling_die (struct die_info *);
1193
1194 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1195
1196 static void dump_die_for_error (struct die_info *);
1197
1198 static void dump_die_1 (struct ui_file *, int level, int max_level,
1199                         struct die_info *);
1200
1201 /*static*/ void dump_die (struct die_info *, int max_level);
1202
1203 static void store_in_ref_table (struct die_info *,
1204                                 struct dwarf2_cu *);
1205
1206 static int is_ref_attr (struct attribute *);
1207
1208 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1209
1210 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1211
1212 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1213                                                struct attribute *,
1214                                                struct dwarf2_cu **);
1215
1216 static struct die_info *follow_die_ref (struct die_info *,
1217                                         struct attribute *,
1218                                         struct dwarf2_cu **);
1219
1220 static struct die_info *follow_die_sig (struct die_info *,
1221                                         struct attribute *,
1222                                         struct dwarf2_cu **);
1223
1224 static struct signatured_type *lookup_signatured_type_at_offset
1225     (struct objfile *objfile,
1226      struct dwarf2_section_info *section,
1227      unsigned int offset);
1228
1229 static void read_signatured_type_at_offset (struct objfile *objfile,
1230                                             struct dwarf2_section_info *sect,
1231                                             unsigned int offset);
1232
1233 static void read_signatured_type (struct objfile *,
1234                                   struct signatured_type *type_sig);
1235
1236 /* memory allocation interface */
1237
1238 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1239
1240 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1241
1242 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1243
1244 static void initialize_cu_func_list (struct dwarf2_cu *);
1245
1246 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1247                                  struct dwarf2_cu *);
1248
1249 static void dwarf_decode_macros (struct line_header *, unsigned int,
1250                                  char *, bfd *, struct dwarf2_cu *,
1251                                  struct dwarf2_section_info *,
1252                                  int);
1253
1254 static int attr_form_is_block (struct attribute *);
1255
1256 static int attr_form_is_section_offset (struct attribute *);
1257
1258 static int attr_form_is_constant (struct attribute *);
1259
1260 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1261                                    struct dwarf2_loclist_baton *baton,
1262                                    struct attribute *attr);
1263
1264 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1265                                          struct symbol *sym,
1266                                          struct dwarf2_cu *cu);
1267
1268 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1269                                struct abbrev_info *abbrev,
1270                                struct dwarf2_cu *cu);
1271
1272 static void free_stack_comp_unit (void *);
1273
1274 static hashval_t partial_die_hash (const void *item);
1275
1276 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1277
1278 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1279   (unsigned int offset, struct objfile *objfile);
1280
1281 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1282   (unsigned int offset, struct objfile *objfile);
1283
1284 static void init_one_comp_unit (struct dwarf2_cu *cu,
1285                                 struct objfile *objfile);
1286
1287 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1288                                    struct die_info *comp_unit_die);
1289
1290 static void free_one_comp_unit (void *);
1291
1292 static void free_cached_comp_units (void *);
1293
1294 static void age_cached_comp_units (void);
1295
1296 static void free_one_cached_comp_unit (void *);
1297
1298 static struct type *set_die_type (struct die_info *, struct type *,
1299                                   struct dwarf2_cu *);
1300
1301 static void create_all_comp_units (struct objfile *);
1302
1303 static int create_debug_types_hash_table (struct objfile *objfile);
1304
1305 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1306                                  struct objfile *);
1307
1308 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1309
1310 static void dwarf2_add_dependence (struct dwarf2_cu *,
1311                                    struct dwarf2_per_cu_data *);
1312
1313 static void dwarf2_mark (struct dwarf2_cu *);
1314
1315 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1316
1317 static struct type *get_die_type_at_offset (unsigned int,
1318                                             struct dwarf2_per_cu_data *per_cu);
1319
1320 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1321
1322 static void dwarf2_release_queue (void *dummy);
1323
1324 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1325                              struct objfile *objfile);
1326
1327 static void process_queue (struct objfile *objfile);
1328
1329 static void find_file_and_directory (struct die_info *die,
1330                                      struct dwarf2_cu *cu,
1331                                      char **name, char **comp_dir);
1332
1333 static char *file_full_name (int file, struct line_header *lh,
1334                              const char *comp_dir);
1335
1336 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1337                                               gdb_byte *info_ptr,
1338                                               gdb_byte *buffer,
1339                                               unsigned int buffer_size,
1340                                               bfd *abfd,
1341                                               int is_debug_type_section);
1342
1343 static void init_cu_die_reader (struct die_reader_specs *reader,
1344                                 struct dwarf2_cu *cu);
1345
1346 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1347
1348 #if WORDS_BIGENDIAN
1349
1350 /* Convert VALUE between big- and little-endian.  */
1351 static offset_type
1352 byte_swap (offset_type value)
1353 {
1354   offset_type result;
1355
1356   result = (value & 0xff) << 24;
1357   result |= (value & 0xff00) << 8;
1358   result |= (value & 0xff0000) >> 8;
1359   result |= (value & 0xff000000) >> 24;
1360   return result;
1361 }
1362
1363 #define MAYBE_SWAP(V)  byte_swap (V)
1364
1365 #else
1366 #define MAYBE_SWAP(V) (V)
1367 #endif /* WORDS_BIGENDIAN */
1368
1369 /* The suffix for an index file.  */
1370 #define INDEX_SUFFIX ".gdb-index"
1371
1372 static const char *dwarf2_physname (char *name, struct die_info *die,
1373                                     struct dwarf2_cu *cu);
1374
1375 /* Try to locate the sections we need for DWARF 2 debugging
1376    information and return true if we have enough to do something.
1377    NAMES points to the dwarf2 section names, or is NULL if the standard
1378    ELF names are used.  */
1379
1380 int
1381 dwarf2_has_info (struct objfile *objfile,
1382                  const struct dwarf2_debug_sections *names)
1383 {
1384   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1385   if (!dwarf2_per_objfile)
1386     {
1387       /* Initialize per-objfile state.  */
1388       struct dwarf2_per_objfile *data
1389         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1390
1391       memset (data, 0, sizeof (*data));
1392       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1393       dwarf2_per_objfile = data;
1394
1395       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1396                              (void *) names);
1397       dwarf2_per_objfile->objfile = objfile;
1398     }
1399   return (dwarf2_per_objfile->info.asection != NULL
1400           && dwarf2_per_objfile->abbrev.asection != NULL);
1401 }
1402
1403 /* When loading sections, we look either for uncompressed section or for
1404    compressed section names.  */
1405
1406 static int
1407 section_is_p (const char *section_name,
1408               const struct dwarf2_section_names *names)
1409 {
1410   if (names->normal != NULL
1411       && strcmp (section_name, names->normal) == 0)
1412     return 1;
1413   if (names->compressed != NULL
1414       && strcmp (section_name, names->compressed) == 0)
1415     return 1;
1416   return 0;
1417 }
1418
1419 /* This function is mapped across the sections and remembers the
1420    offset and size of each of the debugging sections we are interested
1421    in.  */
1422
1423 static void
1424 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1425 {
1426   const struct dwarf2_debug_sections *names;
1427
1428   if (vnames == NULL)
1429     names = &dwarf2_elf_names;
1430   else
1431     names = (const struct dwarf2_debug_sections *) vnames;
1432
1433   if (section_is_p (sectp->name, &names->info))
1434     {
1435       dwarf2_per_objfile->info.asection = sectp;
1436       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1437     }
1438   else if (section_is_p (sectp->name, &names->abbrev))
1439     {
1440       dwarf2_per_objfile->abbrev.asection = sectp;
1441       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1442     }
1443   else if (section_is_p (sectp->name, &names->line))
1444     {
1445       dwarf2_per_objfile->line.asection = sectp;
1446       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1447     }
1448   else if (section_is_p (sectp->name, &names->loc))
1449     {
1450       dwarf2_per_objfile->loc.asection = sectp;
1451       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1452     }
1453   else if (section_is_p (sectp->name, &names->macinfo))
1454     {
1455       dwarf2_per_objfile->macinfo.asection = sectp;
1456       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1457     }
1458   else if (section_is_p (sectp->name, &names->macro))
1459     {
1460       dwarf2_per_objfile->macro.asection = sectp;
1461       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1462     }
1463   else if (section_is_p (sectp->name, &names->str))
1464     {
1465       dwarf2_per_objfile->str.asection = sectp;
1466       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1467     }
1468   else if (section_is_p (sectp->name, &names->frame))
1469     {
1470       dwarf2_per_objfile->frame.asection = sectp;
1471       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1472     }
1473   else if (section_is_p (sectp->name, &names->eh_frame))
1474     {
1475       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1476
1477       if (aflag & SEC_HAS_CONTENTS)
1478         {
1479           dwarf2_per_objfile->eh_frame.asection = sectp;
1480           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1481         }
1482     }
1483   else if (section_is_p (sectp->name, &names->ranges))
1484     {
1485       dwarf2_per_objfile->ranges.asection = sectp;
1486       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1487     }
1488   else if (section_is_p (sectp->name, &names->types))
1489     {
1490       struct dwarf2_section_info type_section;
1491
1492       memset (&type_section, 0, sizeof (type_section));
1493       type_section.asection = sectp;
1494       type_section.size = bfd_get_section_size (sectp);
1495
1496       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1497                      &type_section);
1498     }
1499   else if (section_is_p (sectp->name, &names->gdb_index))
1500     {
1501       dwarf2_per_objfile->gdb_index.asection = sectp;
1502       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1503     }
1504
1505   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1506       && bfd_section_vma (abfd, sectp) == 0)
1507     dwarf2_per_objfile->has_section_at_zero = 1;
1508 }
1509
1510 /* Decompress a section that was compressed using zlib.  Store the
1511    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1512
1513 static void
1514 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1515                          gdb_byte **outbuf, bfd_size_type *outsize)
1516 {
1517   bfd *abfd = objfile->obfd;
1518 #ifndef HAVE_ZLIB_H
1519   error (_("Support for zlib-compressed DWARF data (from '%s') "
1520            "is disabled in this copy of GDB"),
1521          bfd_get_filename (abfd));
1522 #else
1523   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1524   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1525   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1526   bfd_size_type uncompressed_size;
1527   gdb_byte *uncompressed_buffer;
1528   z_stream strm;
1529   int rc;
1530   int header_size = 12;
1531
1532   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1533       || bfd_bread (compressed_buffer,
1534                     compressed_size, abfd) != compressed_size)
1535     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1536            bfd_get_filename (abfd));
1537
1538   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1539      by the uncompressed section size, 8 bytes in big-endian order.  */
1540   if (compressed_size < header_size
1541       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1542     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1543            bfd_get_filename (abfd));
1544   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1545   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1546   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1547   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1548   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1549   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1550   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1551   uncompressed_size += compressed_buffer[11];
1552
1553   /* It is possible the section consists of several compressed
1554      buffers concatenated together, so we uncompress in a loop.  */
1555   strm.zalloc = NULL;
1556   strm.zfree = NULL;
1557   strm.opaque = NULL;
1558   strm.avail_in = compressed_size - header_size;
1559   strm.next_in = (Bytef*) compressed_buffer + header_size;
1560   strm.avail_out = uncompressed_size;
1561   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1562                                        uncompressed_size);
1563   rc = inflateInit (&strm);
1564   while (strm.avail_in > 0)
1565     {
1566       if (rc != Z_OK)
1567         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1568                bfd_get_filename (abfd), rc);
1569       strm.next_out = ((Bytef*) uncompressed_buffer
1570                        + (uncompressed_size - strm.avail_out));
1571       rc = inflate (&strm, Z_FINISH);
1572       if (rc != Z_STREAM_END)
1573         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1574                bfd_get_filename (abfd), rc);
1575       rc = inflateReset (&strm);
1576     }
1577   rc = inflateEnd (&strm);
1578   if (rc != Z_OK
1579       || strm.avail_out != 0)
1580     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1581            bfd_get_filename (abfd), rc);
1582
1583   do_cleanups (cleanup);
1584   *outbuf = uncompressed_buffer;
1585   *outsize = uncompressed_size;
1586 #endif
1587 }
1588
1589 /* A helper function that decides whether a section is empty.  */
1590
1591 static int
1592 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1593 {
1594   return info->asection == NULL || info->size == 0;
1595 }
1596
1597 /* Read the contents of the section SECTP from object file specified by
1598    OBJFILE, store info about the section into INFO.
1599    If the section is compressed, uncompress it before returning.  */
1600
1601 static void
1602 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1603 {
1604   bfd *abfd = objfile->obfd;
1605   asection *sectp = info->asection;
1606   gdb_byte *buf, *retbuf;
1607   unsigned char header[4];
1608
1609   if (info->readin)
1610     return;
1611   info->buffer = NULL;
1612   info->map_addr = NULL;
1613   info->readin = 1;
1614
1615   if (dwarf2_section_empty_p (info))
1616     return;
1617
1618   /* Check if the file has a 4-byte header indicating compression.  */
1619   if (info->size > sizeof (header)
1620       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1621       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1622     {
1623       /* Upon decompression, update the buffer and its size.  */
1624       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1625         {
1626           zlib_decompress_section (objfile, sectp, &info->buffer,
1627                                    &info->size);
1628           return;
1629         }
1630     }
1631
1632 #ifdef HAVE_MMAP
1633   if (pagesize == 0)
1634     pagesize = getpagesize ();
1635
1636   /* Only try to mmap sections which are large enough: we don't want to
1637      waste space due to fragmentation.  Also, only try mmap for sections
1638      without relocations.  */
1639
1640   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1641     {
1642       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1643                          MAP_PRIVATE, sectp->filepos,
1644                          &info->map_addr, &info->map_len);
1645
1646       if ((caddr_t)info->buffer != MAP_FAILED)
1647         {
1648 #if HAVE_POSIX_MADVISE
1649           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1650 #endif
1651           return;
1652         }
1653     }
1654 #endif
1655
1656   /* If we get here, we are a normal, not-compressed section.  */
1657   info->buffer = buf
1658     = obstack_alloc (&objfile->objfile_obstack, info->size);
1659
1660   /* When debugging .o files, we may need to apply relocations; see
1661      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1662      We never compress sections in .o files, so we only need to
1663      try this when the section is not compressed.  */
1664   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1665   if (retbuf != NULL)
1666     {
1667       info->buffer = retbuf;
1668       return;
1669     }
1670
1671   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1672       || bfd_bread (buf, info->size, abfd) != info->size)
1673     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1674            bfd_get_filename (abfd));
1675 }
1676
1677 /* A helper function that returns the size of a section in a safe way.
1678    If you are positive that the section has been read before using the
1679    size, then it is safe to refer to the dwarf2_section_info object's
1680    "size" field directly.  In other cases, you must call this
1681    function, because for compressed sections the size field is not set
1682    correctly until the section has been read.  */
1683
1684 static bfd_size_type
1685 dwarf2_section_size (struct objfile *objfile,
1686                      struct dwarf2_section_info *info)
1687 {
1688   if (!info->readin)
1689     dwarf2_read_section (objfile, info);
1690   return info->size;
1691 }
1692
1693 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1694    SECTION_NAME.  */
1695
1696 void
1697 dwarf2_get_section_info (struct objfile *objfile,
1698                          enum dwarf2_section_enum sect,
1699                          asection **sectp, gdb_byte **bufp,
1700                          bfd_size_type *sizep)
1701 {
1702   struct dwarf2_per_objfile *data
1703     = objfile_data (objfile, dwarf2_objfile_data_key);
1704   struct dwarf2_section_info *info;
1705
1706   /* We may see an objfile without any DWARF, in which case we just
1707      return nothing.  */
1708   if (data == NULL)
1709     {
1710       *sectp = NULL;
1711       *bufp = NULL;
1712       *sizep = 0;
1713       return;
1714     }
1715   switch (sect)
1716     {
1717     case DWARF2_DEBUG_FRAME:
1718       info = &data->frame;
1719       break;
1720     case DWARF2_EH_FRAME:
1721       info = &data->eh_frame;
1722       break;
1723     default:
1724       gdb_assert_not_reached ("unexpected section");
1725     }
1726
1727   dwarf2_read_section (objfile, info);
1728
1729   *sectp = info->asection;
1730   *bufp = info->buffer;
1731   *sizep = info->size;
1732 }
1733
1734 \f
1735 /* DWARF quick_symbols_functions support.  */
1736
1737 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1738    unique line tables, so we maintain a separate table of all .debug_line
1739    derived entries to support the sharing.
1740    All the quick functions need is the list of file names.  We discard the
1741    line_header when we're done and don't need to record it here.  */
1742 struct quick_file_names
1743 {
1744   /* The offset in .debug_line of the line table.  We hash on this.  */
1745   unsigned int offset;
1746
1747   /* The number of entries in file_names, real_names.  */
1748   unsigned int num_file_names;
1749
1750   /* The file names from the line table, after being run through
1751      file_full_name.  */
1752   const char **file_names;
1753
1754   /* The file names from the line table after being run through
1755      gdb_realpath.  These are computed lazily.  */
1756   const char **real_names;
1757 };
1758
1759 /* When using the index (and thus not using psymtabs), each CU has an
1760    object of this type.  This is used to hold information needed by
1761    the various "quick" methods.  */
1762 struct dwarf2_per_cu_quick_data
1763 {
1764   /* The file table.  This can be NULL if there was no file table
1765      or it's currently not read in.
1766      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1767   struct quick_file_names *file_names;
1768
1769   /* The corresponding symbol table.  This is NULL if symbols for this
1770      CU have not yet been read.  */
1771   struct symtab *symtab;
1772
1773   /* A temporary mark bit used when iterating over all CUs in
1774      expand_symtabs_matching.  */
1775   unsigned int mark : 1;
1776
1777   /* True if we've tried to read the file table and found there isn't one.
1778      There will be no point in trying to read it again next time.  */
1779   unsigned int no_file_data : 1;
1780 };
1781
1782 /* Hash function for a quick_file_names.  */
1783
1784 static hashval_t
1785 hash_file_name_entry (const void *e)
1786 {
1787   const struct quick_file_names *file_data = e;
1788
1789   return file_data->offset;
1790 }
1791
1792 /* Equality function for a quick_file_names.  */
1793
1794 static int
1795 eq_file_name_entry (const void *a, const void *b)
1796 {
1797   const struct quick_file_names *ea = a;
1798   const struct quick_file_names *eb = b;
1799
1800   return ea->offset == eb->offset;
1801 }
1802
1803 /* Delete function for a quick_file_names.  */
1804
1805 static void
1806 delete_file_name_entry (void *e)
1807 {
1808   struct quick_file_names *file_data = e;
1809   int i;
1810
1811   for (i = 0; i < file_data->num_file_names; ++i)
1812     {
1813       xfree ((void*) file_data->file_names[i]);
1814       if (file_data->real_names)
1815         xfree ((void*) file_data->real_names[i]);
1816     }
1817
1818   /* The space for the struct itself lives on objfile_obstack,
1819      so we don't free it here.  */
1820 }
1821
1822 /* Create a quick_file_names hash table.  */
1823
1824 static htab_t
1825 create_quick_file_names_table (unsigned int nr_initial_entries)
1826 {
1827   return htab_create_alloc (nr_initial_entries,
1828                             hash_file_name_entry, eq_file_name_entry,
1829                             delete_file_name_entry, xcalloc, xfree);
1830 }
1831
1832 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1833    have to be created afterwards.  You should call age_cached_comp_units after
1834    processing PER_CU->CU.  dw2_setup must have been already called.  */
1835
1836 static void
1837 load_cu (struct dwarf2_per_cu_data *per_cu)
1838 {
1839   if (per_cu->debug_type_section)
1840     read_signatured_type_at_offset (per_cu->objfile,
1841                                     per_cu->debug_type_section,
1842                                     per_cu->offset);
1843   else
1844     load_full_comp_unit (per_cu, per_cu->objfile);
1845
1846   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1847
1848   gdb_assert (per_cu->cu != NULL);
1849 }
1850
1851 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1852    this CU came.  */
1853
1854 static void
1855 dw2_do_instantiate_symtab (struct objfile *objfile,
1856                            struct dwarf2_per_cu_data *per_cu)
1857 {
1858   struct cleanup *back_to;
1859
1860   back_to = make_cleanup (dwarf2_release_queue, NULL);
1861
1862   queue_comp_unit (per_cu, objfile);
1863
1864   load_cu (per_cu);
1865
1866   process_queue (objfile);
1867
1868   /* Age the cache, releasing compilation units that have not
1869      been used recently.  */
1870   age_cached_comp_units ();
1871
1872   do_cleanups (back_to);
1873 }
1874
1875 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1876    the objfile from which this CU came.  Returns the resulting symbol
1877    table.  */
1878
1879 static struct symtab *
1880 dw2_instantiate_symtab (struct objfile *objfile,
1881                         struct dwarf2_per_cu_data *per_cu)
1882 {
1883   if (!per_cu->v.quick->symtab)
1884     {
1885       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1886       increment_reading_symtab ();
1887       dw2_do_instantiate_symtab (objfile, per_cu);
1888       do_cleanups (back_to);
1889     }
1890   return per_cu->v.quick->symtab;
1891 }
1892
1893 /* Return the CU given its index.  */
1894
1895 static struct dwarf2_per_cu_data *
1896 dw2_get_cu (int index)
1897 {
1898   if (index >= dwarf2_per_objfile->n_comp_units)
1899     {
1900       index -= dwarf2_per_objfile->n_comp_units;
1901       return dwarf2_per_objfile->type_comp_units[index];
1902     }
1903   return dwarf2_per_objfile->all_comp_units[index];
1904 }
1905
1906 /* A helper function that knows how to read a 64-bit value in a way
1907    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1908    otherwise.  */
1909
1910 static int
1911 extract_cu_value (const char *bytes, ULONGEST *result)
1912 {
1913   if (sizeof (ULONGEST) < 8)
1914     {
1915       int i;
1916
1917       /* Ignore the upper 4 bytes if they are all zero.  */
1918       for (i = 0; i < 4; ++i)
1919         if (bytes[i + 4] != 0)
1920           return 0;
1921
1922       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1923     }
1924   else
1925     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1926   return 1;
1927 }
1928
1929 /* Read the CU list from the mapped index, and use it to create all
1930    the CU objects for this objfile.  Return 0 if something went wrong,
1931    1 if everything went ok.  */
1932
1933 static int
1934 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1935                        offset_type cu_list_elements)
1936 {
1937   offset_type i;
1938
1939   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1940   dwarf2_per_objfile->all_comp_units
1941     = obstack_alloc (&objfile->objfile_obstack,
1942                      dwarf2_per_objfile->n_comp_units
1943                      * sizeof (struct dwarf2_per_cu_data *));
1944
1945   for (i = 0; i < cu_list_elements; i += 2)
1946     {
1947       struct dwarf2_per_cu_data *the_cu;
1948       ULONGEST offset, length;
1949
1950       if (!extract_cu_value (cu_list, &offset)
1951           || !extract_cu_value (cu_list + 8, &length))
1952         return 0;
1953       cu_list += 2 * 8;
1954
1955       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1956                                struct dwarf2_per_cu_data);
1957       the_cu->offset = offset;
1958       the_cu->length = length;
1959       the_cu->objfile = objfile;
1960       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1961                                         struct dwarf2_per_cu_quick_data);
1962       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1963     }
1964
1965   return 1;
1966 }
1967
1968 /* Create the signatured type hash table from the index.  */
1969
1970 static int
1971 create_signatured_type_table_from_index (struct objfile *objfile,
1972                                          struct dwarf2_section_info *section,
1973                                          const gdb_byte *bytes,
1974                                          offset_type elements)
1975 {
1976   offset_type i;
1977   htab_t sig_types_hash;
1978
1979   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1980   dwarf2_per_objfile->type_comp_units
1981     = obstack_alloc (&objfile->objfile_obstack,
1982                      dwarf2_per_objfile->n_type_comp_units
1983                      * sizeof (struct dwarf2_per_cu_data *));
1984
1985   sig_types_hash = allocate_signatured_type_table (objfile);
1986
1987   for (i = 0; i < elements; i += 3)
1988     {
1989       struct signatured_type *type_sig;
1990       ULONGEST offset, type_offset, signature;
1991       void **slot;
1992
1993       if (!extract_cu_value (bytes, &offset)
1994           || !extract_cu_value (bytes + 8, &type_offset))
1995         return 0;
1996       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1997       bytes += 3 * 8;
1998
1999       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2000                                  struct signatured_type);
2001       type_sig->signature = signature;
2002       type_sig->type_offset = type_offset;
2003       type_sig->per_cu.debug_type_section = section;
2004       type_sig->per_cu.offset = offset;
2005       type_sig->per_cu.objfile = objfile;
2006       type_sig->per_cu.v.quick
2007         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2008                           struct dwarf2_per_cu_quick_data);
2009
2010       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
2011       *slot = type_sig;
2012
2013       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2014     }
2015
2016   dwarf2_per_objfile->signatured_types = sig_types_hash;
2017
2018   return 1;
2019 }
2020
2021 /* Read the address map data from the mapped index, and use it to
2022    populate the objfile's psymtabs_addrmap.  */
2023
2024 static void
2025 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2026 {
2027   const gdb_byte *iter, *end;
2028   struct obstack temp_obstack;
2029   struct addrmap *mutable_map;
2030   struct cleanup *cleanup;
2031   CORE_ADDR baseaddr;
2032
2033   obstack_init (&temp_obstack);
2034   cleanup = make_cleanup_obstack_free (&temp_obstack);
2035   mutable_map = addrmap_create_mutable (&temp_obstack);
2036
2037   iter = index->address_table;
2038   end = iter + index->address_table_size;
2039
2040   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2041
2042   while (iter < end)
2043     {
2044       ULONGEST hi, lo, cu_index;
2045       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2046       iter += 8;
2047       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2048       iter += 8;
2049       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2050       iter += 4;
2051       
2052       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2053                          dw2_get_cu (cu_index));
2054     }
2055
2056   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2057                                                     &objfile->objfile_obstack);
2058   do_cleanups (cleanup);
2059 }
2060
2061 /* The hash function for strings in the mapped index.  This is the same as
2062    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2063    implementation.  This is necessary because the hash function is tied to the
2064    format of the mapped index file.  The hash values do not have to match with
2065    SYMBOL_HASH_NEXT.
2066    
2067    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2068
2069 static hashval_t
2070 mapped_index_string_hash (int index_version, const void *p)
2071 {
2072   const unsigned char *str = (const unsigned char *) p;
2073   hashval_t r = 0;
2074   unsigned char c;
2075
2076   while ((c = *str++) != 0)
2077     {
2078       if (index_version >= 5)
2079         c = tolower (c);
2080       r = r * 67 + c - 113;
2081     }
2082
2083   return r;
2084 }
2085
2086 /* Find a slot in the mapped index INDEX for the object named NAME.
2087    If NAME is found, set *VEC_OUT to point to the CU vector in the
2088    constant pool and return 1.  If NAME cannot be found, return 0.  */
2089
2090 static int
2091 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2092                           offset_type **vec_out)
2093 {
2094   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2095   offset_type hash;
2096   offset_type slot, step;
2097   int (*cmp) (const char *, const char *);
2098
2099   if (current_language->la_language == language_cplus
2100       || current_language->la_language == language_java
2101       || current_language->la_language == language_fortran)
2102     {
2103       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2104          not contain any.  */
2105       const char *paren = strchr (name, '(');
2106
2107       if (paren)
2108         {
2109           char *dup;
2110
2111           dup = xmalloc (paren - name + 1);
2112           memcpy (dup, name, paren - name);
2113           dup[paren - name] = 0;
2114
2115           make_cleanup (xfree, dup);
2116           name = dup;
2117         }
2118     }
2119
2120   /* Index version 4 did not support case insensitive searches.  But the
2121      indexes for case insensitive languages are built in lowercase, therefore
2122      simulate our NAME being searched is also lowercased.  */
2123   hash = mapped_index_string_hash ((index->version == 4
2124                                     && case_sensitivity == case_sensitive_off
2125                                     ? 5 : index->version),
2126                                    name);
2127
2128   slot = hash & (index->symbol_table_slots - 1);
2129   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2130   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2131
2132   for (;;)
2133     {
2134       /* Convert a slot number to an offset into the table.  */
2135       offset_type i = 2 * slot;
2136       const char *str;
2137       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2138         {
2139           do_cleanups (back_to);
2140           return 0;
2141         }
2142
2143       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2144       if (!cmp (name, str))
2145         {
2146           *vec_out = (offset_type *) (index->constant_pool
2147                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2148           do_cleanups (back_to);
2149           return 1;
2150         }
2151
2152       slot = (slot + step) & (index->symbol_table_slots - 1);
2153     }
2154 }
2155
2156 /* Read the index file.  If everything went ok, initialize the "quick"
2157    elements of all the CUs and return 1.  Otherwise, return 0.  */
2158
2159 static int
2160 dwarf2_read_index (struct objfile *objfile)
2161 {
2162   char *addr;
2163   struct mapped_index *map;
2164   offset_type *metadata;
2165   const gdb_byte *cu_list;
2166   const gdb_byte *types_list = NULL;
2167   offset_type version, cu_list_elements;
2168   offset_type types_list_elements = 0;
2169   int i;
2170
2171   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2172     return 0;
2173
2174   /* Older elfutils strip versions could keep the section in the main
2175      executable while splitting it for the separate debug info file.  */
2176   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2177        & SEC_HAS_CONTENTS) == 0)
2178     return 0;
2179
2180   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2181
2182   addr = dwarf2_per_objfile->gdb_index.buffer;
2183   /* Version check.  */
2184   version = MAYBE_SWAP (*(offset_type *) addr);
2185   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2186      causes the index to behave very poorly for certain requests.  Version 3
2187      contained incomplete addrmap.  So, it seems better to just ignore such
2188      indices.  Index version 4 uses a different hash function than index
2189      version 5 and later.  */
2190   if (version < 4)
2191     return 0;
2192   /* Indexes with higher version than the one supported by GDB may be no
2193      longer backward compatible.  */
2194   if (version > 5)
2195     return 0;
2196
2197   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2198   map->version = version;
2199   map->total_size = dwarf2_per_objfile->gdb_index.size;
2200
2201   metadata = (offset_type *) (addr + sizeof (offset_type));
2202
2203   i = 0;
2204   cu_list = addr + MAYBE_SWAP (metadata[i]);
2205   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2206                       / 8);
2207   ++i;
2208
2209   types_list = addr + MAYBE_SWAP (metadata[i]);
2210   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2211                           - MAYBE_SWAP (metadata[i]))
2212                          / 8);
2213   ++i;
2214
2215   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2216   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2217                              - MAYBE_SWAP (metadata[i]));
2218   ++i;
2219
2220   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2221   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2222                               - MAYBE_SWAP (metadata[i]))
2223                              / (2 * sizeof (offset_type)));
2224   ++i;
2225
2226   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2227
2228   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2229     return 0;
2230
2231   if (types_list_elements)
2232     {
2233       struct dwarf2_section_info *section;
2234
2235       /* We can only handle a single .debug_types when we have an
2236          index.  */
2237       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2238         return 0;
2239
2240       section = VEC_index (dwarf2_section_info_def,
2241                            dwarf2_per_objfile->types, 0);
2242
2243       if (!create_signatured_type_table_from_index (objfile, section,
2244                                                     types_list,
2245                                                     types_list_elements))
2246         return 0;
2247     }
2248
2249   create_addrmap_from_index (objfile, map);
2250
2251   dwarf2_per_objfile->index_table = map;
2252   dwarf2_per_objfile->using_index = 1;
2253   dwarf2_per_objfile->quick_file_names_table =
2254     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2255
2256   return 1;
2257 }
2258
2259 /* A helper for the "quick" functions which sets the global
2260    dwarf2_per_objfile according to OBJFILE.  */
2261
2262 static void
2263 dw2_setup (struct objfile *objfile)
2264 {
2265   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2266   gdb_assert (dwarf2_per_objfile);
2267 }
2268
2269 /* A helper for the "quick" functions which attempts to read the line
2270    table for THIS_CU.  */
2271
2272 static struct quick_file_names *
2273 dw2_get_file_names (struct objfile *objfile,
2274                     struct dwarf2_per_cu_data *this_cu)
2275 {
2276   bfd *abfd = objfile->obfd;
2277   struct line_header *lh;
2278   struct attribute *attr;
2279   struct cleanup *cleanups;
2280   struct die_info *comp_unit_die;
2281   struct dwarf2_section_info* sec;
2282   gdb_byte *info_ptr, *buffer;
2283   int has_children, i;
2284   struct dwarf2_cu cu;
2285   unsigned int bytes_read, buffer_size;
2286   struct die_reader_specs reader_specs;
2287   char *name, *comp_dir;
2288   void **slot;
2289   struct quick_file_names *qfn;
2290   unsigned int line_offset;
2291
2292   if (this_cu->v.quick->file_names != NULL)
2293     return this_cu->v.quick->file_names;
2294   /* If we know there is no line data, no point in looking again.  */
2295   if (this_cu->v.quick->no_file_data)
2296     return NULL;
2297
2298   init_one_comp_unit (&cu, objfile);
2299   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2300
2301   if (this_cu->debug_type_section)
2302     sec = this_cu->debug_type_section;
2303   else
2304     sec = &dwarf2_per_objfile->info;
2305   dwarf2_read_section (objfile, sec);
2306   buffer_size = sec->size;
2307   buffer = sec->buffer;
2308   info_ptr = buffer + this_cu->offset;
2309
2310   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2311                                           buffer, buffer_size,
2312                                           abfd,
2313                                           this_cu->debug_type_section != NULL);
2314
2315   /* Skip dummy compilation units.  */
2316   if (info_ptr >= buffer + buffer_size
2317       || peek_abbrev_code (abfd, info_ptr) == 0)
2318     {
2319       do_cleanups (cleanups);
2320       return NULL;
2321     }
2322
2323   this_cu->cu = &cu;
2324   cu.per_cu = this_cu;
2325
2326   dwarf2_read_abbrevs (abfd, &cu);
2327   make_cleanup (dwarf2_free_abbrev_table, &cu);
2328
2329   init_cu_die_reader (&reader_specs, &cu);
2330   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2331                  &has_children);
2332
2333   lh = NULL;
2334   slot = NULL;
2335   line_offset = 0;
2336   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2337   if (attr)
2338     {
2339       struct quick_file_names find_entry;
2340
2341       line_offset = DW_UNSND (attr);
2342
2343       /* We may have already read in this line header (TU line header sharing).
2344          If we have we're done.  */
2345       find_entry.offset = line_offset;
2346       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2347                              &find_entry, INSERT);
2348       if (*slot != NULL)
2349         {
2350           do_cleanups (cleanups);
2351           this_cu->v.quick->file_names = *slot;
2352           return *slot;
2353         }
2354
2355       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2356     }
2357   if (lh == NULL)
2358     {
2359       do_cleanups (cleanups);
2360       this_cu->v.quick->no_file_data = 1;
2361       return NULL;
2362     }
2363
2364   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2365   qfn->offset = line_offset;
2366   gdb_assert (slot != NULL);
2367   *slot = qfn;
2368
2369   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2370
2371   qfn->num_file_names = lh->num_file_names;
2372   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2373                                    lh->num_file_names * sizeof (char *));
2374   for (i = 0; i < lh->num_file_names; ++i)
2375     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2376   qfn->real_names = NULL;
2377
2378   free_line_header (lh);
2379   do_cleanups (cleanups);
2380
2381   this_cu->v.quick->file_names = qfn;
2382   return qfn;
2383 }
2384
2385 /* A helper for the "quick" functions which computes and caches the
2386    real path for a given file name from the line table.  */
2387
2388 static const char *
2389 dw2_get_real_path (struct objfile *objfile,
2390                    struct quick_file_names *qfn, int index)
2391 {
2392   if (qfn->real_names == NULL)
2393     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2394                                       qfn->num_file_names, sizeof (char *));
2395
2396   if (qfn->real_names[index] == NULL)
2397     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2398
2399   return qfn->real_names[index];
2400 }
2401
2402 static struct symtab *
2403 dw2_find_last_source_symtab (struct objfile *objfile)
2404 {
2405   int index;
2406
2407   dw2_setup (objfile);
2408   index = dwarf2_per_objfile->n_comp_units - 1;
2409   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2410 }
2411
2412 /* Traversal function for dw2_forget_cached_source_info.  */
2413
2414 static int
2415 dw2_free_cached_file_names (void **slot, void *info)
2416 {
2417   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2418
2419   if (file_data->real_names)
2420     {
2421       int i;
2422
2423       for (i = 0; i < file_data->num_file_names; ++i)
2424         {
2425           xfree ((void*) file_data->real_names[i]);
2426           file_data->real_names[i] = NULL;
2427         }
2428     }
2429
2430   return 1;
2431 }
2432
2433 static void
2434 dw2_forget_cached_source_info (struct objfile *objfile)
2435 {
2436   dw2_setup (objfile);
2437
2438   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2439                           dw2_free_cached_file_names, NULL);
2440 }
2441
2442 static int
2443 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2444                    const char *full_path, const char *real_path,
2445                    struct symtab **result)
2446 {
2447   int i;
2448   int check_basename = lbasename (name) == name;
2449   struct dwarf2_per_cu_data *base_cu = NULL;
2450
2451   dw2_setup (objfile);
2452
2453   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2454                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2455     {
2456       int j;
2457       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2458       struct quick_file_names *file_data;
2459
2460       if (per_cu->v.quick->symtab)
2461         continue;
2462
2463       file_data = dw2_get_file_names (objfile, per_cu);
2464       if (file_data == NULL)
2465         continue;
2466
2467       for (j = 0; j < file_data->num_file_names; ++j)
2468         {
2469           const char *this_name = file_data->file_names[j];
2470
2471           if (FILENAME_CMP (name, this_name) == 0)
2472             {
2473               *result = dw2_instantiate_symtab (objfile, per_cu);
2474               return 1;
2475             }
2476
2477           if (check_basename && ! base_cu
2478               && FILENAME_CMP (lbasename (this_name), name) == 0)
2479             base_cu = per_cu;
2480
2481           if (full_path != NULL)
2482             {
2483               const char *this_real_name = dw2_get_real_path (objfile,
2484                                                               file_data, j);
2485
2486               if (this_real_name != NULL
2487                   && FILENAME_CMP (full_path, this_real_name) == 0)
2488                 {
2489                   *result = dw2_instantiate_symtab (objfile, per_cu);
2490                   return 1;
2491                 }
2492             }
2493
2494           if (real_path != NULL)
2495             {
2496               const char *this_real_name = dw2_get_real_path (objfile,
2497                                                               file_data, j);
2498
2499               if (this_real_name != NULL
2500                   && FILENAME_CMP (real_path, this_real_name) == 0)
2501                 {
2502                   *result = dw2_instantiate_symtab (objfile, per_cu);
2503                   return 1;
2504                 }
2505             }
2506         }
2507     }
2508
2509   if (base_cu)
2510     {
2511       *result = dw2_instantiate_symtab (objfile, base_cu);
2512       return 1;
2513     }
2514
2515   return 0;
2516 }
2517
2518 static struct symtab *
2519 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2520                    const char *name, domain_enum domain)
2521 {
2522   /* We do all the work in the pre_expand_symtabs_matching hook
2523      instead.  */
2524   return NULL;
2525 }
2526
2527 /* A helper function that expands all symtabs that hold an object
2528    named NAME.  */
2529
2530 static void
2531 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2532 {
2533   dw2_setup (objfile);
2534
2535   /* index_table is NULL if OBJF_READNOW.  */
2536   if (dwarf2_per_objfile->index_table)
2537     {
2538       offset_type *vec;
2539
2540       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2541                                     name, &vec))
2542         {
2543           offset_type i, len = MAYBE_SWAP (*vec);
2544           for (i = 0; i < len; ++i)
2545             {
2546               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2547               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2548
2549               dw2_instantiate_symtab (objfile, per_cu);
2550             }
2551         }
2552     }
2553 }
2554
2555 static void
2556 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2557                                  enum block_enum block_kind, const char *name,
2558                                  domain_enum domain)
2559 {
2560   dw2_do_expand_symtabs_matching (objfile, name);
2561 }
2562
2563 static void
2564 dw2_print_stats (struct objfile *objfile)
2565 {
2566   int i, count;
2567
2568   dw2_setup (objfile);
2569   count = 0;
2570   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2571                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2572     {
2573       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2574
2575       if (!per_cu->v.quick->symtab)
2576         ++count;
2577     }
2578   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2579 }
2580
2581 static void
2582 dw2_dump (struct objfile *objfile)
2583 {
2584   /* Nothing worth printing.  */
2585 }
2586
2587 static void
2588 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2589               struct section_offsets *delta)
2590 {
2591   /* There's nothing to relocate here.  */
2592 }
2593
2594 static void
2595 dw2_expand_symtabs_for_function (struct objfile *objfile,
2596                                  const char *func_name)
2597 {
2598   dw2_do_expand_symtabs_matching (objfile, func_name);
2599 }
2600
2601 static void
2602 dw2_expand_all_symtabs (struct objfile *objfile)
2603 {
2604   int i;
2605
2606   dw2_setup (objfile);
2607
2608   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2609                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2610     {
2611       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2612
2613       dw2_instantiate_symtab (objfile, per_cu);
2614     }
2615 }
2616
2617 static void
2618 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2619                                   const char *filename)
2620 {
2621   int i;
2622
2623   dw2_setup (objfile);
2624
2625   /* We don't need to consider type units here.
2626      This is only called for examining code, e.g. expand_line_sal.
2627      There can be an order of magnitude (or more) more type units
2628      than comp units, and we avoid them if we can.  */
2629
2630   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2631     {
2632       int j;
2633       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2634       struct quick_file_names *file_data;
2635
2636       if (per_cu->v.quick->symtab)
2637         continue;
2638
2639       file_data = dw2_get_file_names (objfile, per_cu);
2640       if (file_data == NULL)
2641         continue;
2642
2643       for (j = 0; j < file_data->num_file_names; ++j)
2644         {
2645           const char *this_name = file_data->file_names[j];
2646           if (FILENAME_CMP (this_name, filename) == 0)
2647             {
2648               dw2_instantiate_symtab (objfile, per_cu);
2649               break;
2650             }
2651         }
2652     }
2653 }
2654
2655 static const char *
2656 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2657 {
2658   struct dwarf2_per_cu_data *per_cu;
2659   offset_type *vec;
2660   struct quick_file_names *file_data;
2661
2662   dw2_setup (objfile);
2663
2664   /* index_table is NULL if OBJF_READNOW.  */
2665   if (!dwarf2_per_objfile->index_table)
2666     {
2667       struct symtab *s;
2668
2669       ALL_OBJFILE_SYMTABS (objfile, s)
2670         if (s->primary)
2671           {
2672             struct blockvector *bv = BLOCKVECTOR (s);
2673             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2674             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2675
2676             if (sym)
2677               return sym->symtab->filename;
2678           }
2679       return NULL;
2680     }
2681
2682   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2683                                  name, &vec))
2684     return NULL;
2685
2686   /* Note that this just looks at the very first one named NAME -- but
2687      actually we are looking for a function.  find_main_filename
2688      should be rewritten so that it doesn't require a custom hook.  It
2689      could just use the ordinary symbol tables.  */
2690   /* vec[0] is the length, which must always be >0.  */
2691   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2692
2693   file_data = dw2_get_file_names (objfile, per_cu);
2694   if (file_data == NULL)
2695     return NULL;
2696
2697   return file_data->file_names[file_data->num_file_names - 1];
2698 }
2699
2700 static void
2701 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2702                           struct objfile *objfile, int global,
2703                           int (*callback) (struct block *,
2704                                            struct symbol *, void *),
2705                           void *data, symbol_compare_ftype *match,
2706                           symbol_compare_ftype *ordered_compare)
2707 {
2708   /* Currently unimplemented; used for Ada.  The function can be called if the
2709      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2710      does not look for non-Ada symbols this function should just return.  */
2711 }
2712
2713 static void
2714 dw2_expand_symtabs_matching (struct objfile *objfile,
2715                              int (*file_matcher) (const char *, void *),
2716                              int (*name_matcher) (const char *, void *),
2717                              enum search_domain kind,
2718                              void *data)
2719 {
2720   int i;
2721   offset_type iter;
2722   struct mapped_index *index;
2723
2724   dw2_setup (objfile);
2725
2726   /* index_table is NULL if OBJF_READNOW.  */
2727   if (!dwarf2_per_objfile->index_table)
2728     return;
2729   index = dwarf2_per_objfile->index_table;
2730
2731   if (file_matcher != NULL)
2732     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2733                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2734       {
2735         int j;
2736         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2737         struct quick_file_names *file_data;
2738
2739         per_cu->v.quick->mark = 0;
2740         if (per_cu->v.quick->symtab)
2741           continue;
2742
2743         file_data = dw2_get_file_names (objfile, per_cu);
2744         if (file_data == NULL)
2745           continue;
2746
2747         for (j = 0; j < file_data->num_file_names; ++j)
2748           {
2749             if (file_matcher (file_data->file_names[j], data))
2750               {
2751                 per_cu->v.quick->mark = 1;
2752                 break;
2753               }
2754           }
2755       }
2756
2757   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2758     {
2759       offset_type idx = 2 * iter;
2760       const char *name;
2761       offset_type *vec, vec_len, vec_idx;
2762
2763       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2764         continue;
2765
2766       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2767
2768       if (! (*name_matcher) (name, data))
2769         continue;
2770
2771       /* The name was matched, now expand corresponding CUs that were
2772          marked.  */
2773       vec = (offset_type *) (index->constant_pool
2774                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2775       vec_len = MAYBE_SWAP (vec[0]);
2776       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2777         {
2778           struct dwarf2_per_cu_data *per_cu;
2779
2780           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2781           if (file_matcher == NULL || per_cu->v.quick->mark)
2782             dw2_instantiate_symtab (objfile, per_cu);
2783         }
2784     }
2785 }
2786
2787 static struct symtab *
2788 dw2_find_pc_sect_symtab (struct objfile *objfile,
2789                          struct minimal_symbol *msymbol,
2790                          CORE_ADDR pc,
2791                          struct obj_section *section,
2792                          int warn_if_readin)
2793 {
2794   struct dwarf2_per_cu_data *data;
2795
2796   dw2_setup (objfile);
2797
2798   if (!objfile->psymtabs_addrmap)
2799     return NULL;
2800
2801   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2802   if (!data)
2803     return NULL;
2804
2805   if (warn_if_readin && data->v.quick->symtab)
2806     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2807              paddress (get_objfile_arch (objfile), pc));
2808
2809   return dw2_instantiate_symtab (objfile, data);
2810 }
2811
2812 static void
2813 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2814                           void *data)
2815 {
2816   int i;
2817
2818   dw2_setup (objfile);
2819
2820   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2821                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2822     {
2823       int j;
2824       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2825       struct quick_file_names *file_data;
2826
2827       if (per_cu->v.quick->symtab)
2828         continue;
2829
2830       file_data = dw2_get_file_names (objfile, per_cu);
2831       if (file_data == NULL)
2832         continue;
2833
2834       for (j = 0; j < file_data->num_file_names; ++j)
2835         {
2836           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2837                                                           j);
2838           (*fun) (file_data->file_names[j], this_real_name, data);
2839         }
2840     }
2841 }
2842
2843 static int
2844 dw2_has_symbols (struct objfile *objfile)
2845 {
2846   return 1;
2847 }
2848
2849 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2850 {
2851   dw2_has_symbols,
2852   dw2_find_last_source_symtab,
2853   dw2_forget_cached_source_info,
2854   dw2_lookup_symtab,
2855   dw2_lookup_symbol,
2856   dw2_pre_expand_symtabs_matching,
2857   dw2_print_stats,
2858   dw2_dump,
2859   dw2_relocate,
2860   dw2_expand_symtabs_for_function,
2861   dw2_expand_all_symtabs,
2862   dw2_expand_symtabs_with_filename,
2863   dw2_find_symbol_file,
2864   dw2_map_matching_symbols,
2865   dw2_expand_symtabs_matching,
2866   dw2_find_pc_sect_symtab,
2867   dw2_map_symbol_filenames
2868 };
2869
2870 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2871    file will use psymtabs, or 1 if using the GNU index.  */
2872
2873 int
2874 dwarf2_initialize_objfile (struct objfile *objfile)
2875 {
2876   /* If we're about to read full symbols, don't bother with the
2877      indices.  In this case we also don't care if some other debug
2878      format is making psymtabs, because they are all about to be
2879      expanded anyway.  */
2880   if ((objfile->flags & OBJF_READNOW))
2881     {
2882       int i;
2883
2884       dwarf2_per_objfile->using_index = 1;
2885       create_all_comp_units (objfile);
2886       create_debug_types_hash_table (objfile);
2887       dwarf2_per_objfile->quick_file_names_table =
2888         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2889
2890       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2891                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2892         {
2893           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2894
2895           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2896                                             struct dwarf2_per_cu_quick_data);
2897         }
2898
2899       /* Return 1 so that gdb sees the "quick" functions.  However,
2900          these functions will be no-ops because we will have expanded
2901          all symtabs.  */
2902       return 1;
2903     }
2904
2905   if (dwarf2_read_index (objfile))
2906     return 1;
2907
2908   return 0;
2909 }
2910
2911 \f
2912
2913 /* Build a partial symbol table.  */
2914
2915 void
2916 dwarf2_build_psymtabs (struct objfile *objfile)
2917 {
2918   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2919     {
2920       init_psymbol_list (objfile, 1024);
2921     }
2922
2923   dwarf2_build_psymtabs_hard (objfile);
2924 }
2925
2926 /* Return TRUE if OFFSET is within CU_HEADER.  */
2927
2928 static inline int
2929 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2930 {
2931   unsigned int bottom = cu_header->offset;
2932   unsigned int top = (cu_header->offset
2933                       + cu_header->length
2934                       + cu_header->initial_length_size);
2935
2936   return (offset >= bottom && offset < top);
2937 }
2938
2939 /* Read in the comp unit header information from the debug_info at info_ptr.
2940    NOTE: This leaves members offset, first_die_offset to be filled in
2941    by the caller.  */
2942
2943 static gdb_byte *
2944 read_comp_unit_head (struct comp_unit_head *cu_header,
2945                      gdb_byte *info_ptr, bfd *abfd)
2946 {
2947   int signed_addr;
2948   unsigned int bytes_read;
2949
2950   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2951   cu_header->initial_length_size = bytes_read;
2952   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2953   info_ptr += bytes_read;
2954   cu_header->version = read_2_bytes (abfd, info_ptr);
2955   info_ptr += 2;
2956   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2957                                           &bytes_read);
2958   info_ptr += bytes_read;
2959   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2960   info_ptr += 1;
2961   signed_addr = bfd_get_sign_extend_vma (abfd);
2962   if (signed_addr < 0)
2963     internal_error (__FILE__, __LINE__,
2964                     _("read_comp_unit_head: dwarf from non elf file"));
2965   cu_header->signed_addr_p = signed_addr;
2966
2967   return info_ptr;
2968 }
2969
2970 /* Read in a CU header and perform some basic error checking.  */
2971
2972 static gdb_byte *
2973 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2974                              gdb_byte *buffer, unsigned int buffer_size,
2975                              bfd *abfd, int is_debug_type_section)
2976 {
2977   gdb_byte *beg_of_comp_unit = info_ptr;
2978
2979   header->offset = beg_of_comp_unit - buffer;
2980
2981   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2982
2983   /* If we're reading a type unit, skip over the signature and
2984      type_offset fields.  */
2985   if (is_debug_type_section)
2986     info_ptr += 8 /*signature*/ + header->offset_size;
2987
2988   header->first_die_offset = info_ptr - beg_of_comp_unit;
2989
2990   if (header->version != 2 && header->version != 3 && header->version != 4)
2991     error (_("Dwarf Error: wrong version in compilation unit header "
2992            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2993            bfd_get_filename (abfd));
2994
2995   if (header->abbrev_offset
2996       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2997                               &dwarf2_per_objfile->abbrev))
2998     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2999            "(offset 0x%lx + 6) [in module %s]"),
3000            (long) header->abbrev_offset,
3001            (long) (beg_of_comp_unit - buffer),
3002            bfd_get_filename (abfd));
3003
3004   if (beg_of_comp_unit + header->length + header->initial_length_size
3005       > buffer + buffer_size)
3006     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3007            "(offset 0x%lx + 0) [in module %s]"),
3008            (long) header->length,
3009            (long) (beg_of_comp_unit - buffer),
3010            bfd_get_filename (abfd));
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_type_comp_unit_head (struct comp_unit_head *cu_header,
3020                           struct dwarf2_section_info *section,
3021                           ULONGEST *signature,
3022                           gdb_byte *types_ptr, bfd *abfd)
3023 {
3024   gdb_byte *initial_types_ptr = types_ptr;
3025
3026   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3027   cu_header->offset = types_ptr - section->buffer;
3028
3029   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3030
3031   *signature = read_8_bytes (abfd, types_ptr);
3032   types_ptr += 8;
3033   types_ptr += cu_header->offset_size;
3034   cu_header->first_die_offset = types_ptr - initial_types_ptr;
3035
3036   return types_ptr;
3037 }
3038
3039 /* Allocate a new partial symtab for file named NAME and mark this new
3040    partial symtab as being an include of PST.  */
3041
3042 static void
3043 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3044                                struct objfile *objfile)
3045 {
3046   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3047
3048   subpst->section_offsets = pst->section_offsets;
3049   subpst->textlow = 0;
3050   subpst->texthigh = 0;
3051
3052   subpst->dependencies = (struct partial_symtab **)
3053     obstack_alloc (&objfile->objfile_obstack,
3054                    sizeof (struct partial_symtab *));
3055   subpst->dependencies[0] = pst;
3056   subpst->number_of_dependencies = 1;
3057
3058   subpst->globals_offset = 0;
3059   subpst->n_global_syms = 0;
3060   subpst->statics_offset = 0;
3061   subpst->n_static_syms = 0;
3062   subpst->symtab = NULL;
3063   subpst->read_symtab = pst->read_symtab;
3064   subpst->readin = 0;
3065
3066   /* No private part is necessary for include psymtabs.  This property
3067      can be used to differentiate between such include psymtabs and
3068      the regular ones.  */
3069   subpst->read_symtab_private = NULL;
3070 }
3071
3072 /* Read the Line Number Program data and extract the list of files
3073    included by the source file represented by PST.  Build an include
3074    partial symtab for each of these included files.  */
3075
3076 static void
3077 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3078                                struct die_info *die,
3079                                struct partial_symtab *pst)
3080 {
3081   struct objfile *objfile = cu->objfile;
3082   bfd *abfd = objfile->obfd;
3083   struct line_header *lh = NULL;
3084   struct attribute *attr;
3085
3086   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3087   if (attr)
3088     {
3089       unsigned int line_offset = DW_UNSND (attr);
3090
3091       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3092     }
3093   if (lh == NULL)
3094     return;  /* No linetable, so no includes.  */
3095
3096   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3097   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3098
3099   free_line_header (lh);
3100 }
3101
3102 static hashval_t
3103 hash_type_signature (const void *item)
3104 {
3105   const struct signatured_type *type_sig = item;
3106
3107   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3108   return type_sig->signature;
3109 }
3110
3111 static int
3112 eq_type_signature (const void *item_lhs, const void *item_rhs)
3113 {
3114   const struct signatured_type *lhs = item_lhs;
3115   const struct signatured_type *rhs = item_rhs;
3116
3117   return lhs->signature == rhs->signature;
3118 }
3119
3120 /* Allocate a hash table for signatured types.  */
3121
3122 static htab_t
3123 allocate_signatured_type_table (struct objfile *objfile)
3124 {
3125   return htab_create_alloc_ex (41,
3126                                hash_type_signature,
3127                                eq_type_signature,
3128                                NULL,
3129                                &objfile->objfile_obstack,
3130                                hashtab_obstack_allocate,
3131                                dummy_obstack_deallocate);
3132 }
3133
3134 /* A helper function to add a signatured type CU to a list.  */
3135
3136 static int
3137 add_signatured_type_cu_to_list (void **slot, void *datum)
3138 {
3139   struct signatured_type *sigt = *slot;
3140   struct dwarf2_per_cu_data ***datap = datum;
3141
3142   **datap = &sigt->per_cu;
3143   ++*datap;
3144
3145   return 1;
3146 }
3147
3148 /* Create the hash table of all entries in the .debug_types section.
3149    The result is zero if there is an error (e.g. missing .debug_types section),
3150    otherwise non-zero.  */
3151
3152 static int
3153 create_debug_types_hash_table (struct objfile *objfile)
3154 {
3155   htab_t types_htab = NULL;
3156   struct dwarf2_per_cu_data **iter;
3157   int ix;
3158   struct dwarf2_section_info *section;
3159
3160   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3161     {
3162       dwarf2_per_objfile->signatured_types = NULL;
3163       return 0;
3164     }
3165
3166   for (ix = 0;
3167        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3168                     ix, section);
3169        ++ix)
3170     {
3171       gdb_byte *info_ptr, *end_ptr;
3172
3173       dwarf2_read_section (objfile, section);
3174       info_ptr = section->buffer;
3175
3176       if (info_ptr == NULL)
3177         continue;
3178
3179       if (types_htab == NULL)
3180         types_htab = allocate_signatured_type_table (objfile);
3181
3182       if (dwarf2_die_debug)
3183         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3184
3185       end_ptr = info_ptr + section->size;
3186       while (info_ptr < end_ptr)
3187         {
3188           unsigned int offset;
3189           unsigned int offset_size;
3190           unsigned int type_offset;
3191           unsigned int length, initial_length_size;
3192           unsigned short version;
3193           ULONGEST signature;
3194           struct signatured_type *type_sig;
3195           void **slot;
3196           gdb_byte *ptr = info_ptr;
3197
3198           offset = ptr - section->buffer;
3199
3200           /* We need to read the type's signature in order to build the hash
3201              table, but we don't need to read anything else just yet.  */
3202
3203           /* Sanity check to ensure entire cu is present.  */
3204           length = read_initial_length (objfile->obfd, ptr,
3205                                         &initial_length_size);
3206           if (ptr + length + initial_length_size > end_ptr)
3207             {
3208               complaint (&symfile_complaints,
3209                          _("debug type entry runs off end "
3210                            "of `.debug_types' section, ignored"));
3211               break;
3212             }
3213
3214           offset_size = initial_length_size == 4 ? 4 : 8;
3215           ptr += initial_length_size;
3216           version = bfd_get_16 (objfile->obfd, ptr);
3217           ptr += 2;
3218           ptr += offset_size; /* abbrev offset */
3219           ptr += 1; /* address size */
3220           signature = bfd_get_64 (objfile->obfd, ptr);
3221           ptr += 8;
3222           type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3223           ptr += offset_size;
3224
3225           /* Skip dummy type units.  */
3226           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3227             {
3228               info_ptr = info_ptr + initial_length_size + length;
3229               continue;
3230             }
3231
3232           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3233           memset (type_sig, 0, sizeof (*type_sig));
3234           type_sig->signature = signature;
3235           type_sig->type_offset = type_offset;
3236           type_sig->per_cu.objfile = objfile;
3237           type_sig->per_cu.debug_type_section = section;
3238           type_sig->per_cu.offset = offset;
3239
3240           slot = htab_find_slot (types_htab, type_sig, INSERT);
3241           gdb_assert (slot != NULL);
3242           if (*slot != NULL)
3243             {
3244               const struct signatured_type *dup_sig = *slot;
3245
3246               complaint (&symfile_complaints,
3247                          _("debug type entry at offset 0x%x is duplicate to the "
3248                            "entry at offset 0x%x, signature 0x%s"),
3249                          offset, dup_sig->per_cu.offset,
3250                          phex (signature, sizeof (signature)));
3251               gdb_assert (signature == dup_sig->signature);
3252             }
3253           *slot = type_sig;
3254
3255           if (dwarf2_die_debug)
3256             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3257                                 offset, phex (signature, sizeof (signature)));
3258
3259           info_ptr = info_ptr + initial_length_size + length;
3260         }
3261     }
3262
3263   dwarf2_per_objfile->signatured_types = types_htab;
3264
3265   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3266   dwarf2_per_objfile->type_comp_units
3267     = obstack_alloc (&objfile->objfile_obstack,
3268                      dwarf2_per_objfile->n_type_comp_units
3269                      * sizeof (struct dwarf2_per_cu_data *));
3270   iter = &dwarf2_per_objfile->type_comp_units[0];
3271   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3272   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3273               == dwarf2_per_objfile->n_type_comp_units);
3274
3275   return 1;
3276 }
3277
3278 /* Lookup a signature based type.
3279    Returns NULL if SIG is not present in the table.  */
3280
3281 static struct signatured_type *
3282 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3283 {
3284   struct signatured_type find_entry, *entry;
3285
3286   if (dwarf2_per_objfile->signatured_types == NULL)
3287     {
3288       complaint (&symfile_complaints,
3289                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3290       return 0;
3291     }
3292
3293   find_entry.signature = sig;
3294   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3295   return entry;
3296 }
3297
3298 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3299
3300 static void
3301 init_cu_die_reader (struct die_reader_specs *reader,
3302                     struct dwarf2_cu *cu)
3303 {
3304   reader->abfd = cu->objfile->obfd;
3305   reader->cu = cu;
3306   if (cu->per_cu->debug_type_section)
3307     {
3308       gdb_assert (cu->per_cu->debug_type_section->readin);
3309       reader->buffer = cu->per_cu->debug_type_section->buffer;
3310     }
3311   else
3312     {
3313       gdb_assert (dwarf2_per_objfile->info.readin);
3314       reader->buffer = dwarf2_per_objfile->info.buffer;
3315     }
3316 }
3317
3318 /* Find the base address of the compilation unit for range lists and
3319    location lists.  It will normally be specified by DW_AT_low_pc.
3320    In DWARF-3 draft 4, the base address could be overridden by
3321    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3322    compilation units with discontinuous ranges.  */
3323
3324 static void
3325 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3326 {
3327   struct attribute *attr;
3328
3329   cu->base_known = 0;
3330   cu->base_address = 0;
3331
3332   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3333   if (attr)
3334     {
3335       cu->base_address = DW_ADDR (attr);
3336       cu->base_known = 1;
3337     }
3338   else
3339     {
3340       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3341       if (attr)
3342         {
3343           cu->base_address = DW_ADDR (attr);
3344           cu->base_known = 1;
3345         }
3346     }
3347 }
3348
3349 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3350    to combine the common parts.
3351    Process a compilation unit for a psymtab.
3352    BUFFER is a pointer to the beginning of the dwarf section buffer,
3353    either .debug_info or debug_types.
3354    INFO_PTR is a pointer to the start of the CU.
3355    Returns a pointer to the next CU.  */
3356
3357 static gdb_byte *
3358 process_psymtab_comp_unit (struct objfile *objfile,
3359                            struct dwarf2_per_cu_data *this_cu,
3360                            gdb_byte *buffer, gdb_byte *info_ptr,
3361                            unsigned int buffer_size)
3362 {
3363   bfd *abfd = objfile->obfd;
3364   gdb_byte *beg_of_comp_unit = info_ptr;
3365   struct die_info *comp_unit_die;
3366   struct partial_symtab *pst;
3367   CORE_ADDR baseaddr;
3368   struct cleanup *back_to_inner;
3369   struct dwarf2_cu cu;
3370   int has_children, has_pc_info;
3371   struct attribute *attr;
3372   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3373   struct die_reader_specs reader_specs;
3374   const char *filename;
3375
3376   init_one_comp_unit (&cu, objfile);
3377   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3378
3379   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3380                                           buffer, buffer_size,
3381                                           abfd,
3382                                           this_cu->debug_type_section != NULL);
3383
3384   /* Skip dummy compilation units.  */
3385   if (info_ptr >= buffer + buffer_size
3386       || peek_abbrev_code (abfd, info_ptr) == 0)
3387     {
3388       info_ptr = (beg_of_comp_unit + cu.header.length
3389                   + cu.header.initial_length_size);
3390       do_cleanups (back_to_inner);
3391       return info_ptr;
3392     }
3393
3394   cu.list_in_scope = &file_symbols;
3395
3396   /* If this compilation unit was already read in, free the
3397      cached copy in order to read it in again.  This is
3398      necessary because we skipped some symbols when we first
3399      read in the compilation unit (see load_partial_dies).
3400      This problem could be avoided, but the benefit is
3401      unclear.  */
3402   if (this_cu->cu != NULL)
3403     free_one_cached_comp_unit (this_cu->cu);
3404
3405   /* Note that this is a pointer to our stack frame, being
3406      added to a global data structure.  It will be cleaned up
3407      in free_stack_comp_unit when we finish with this
3408      compilation unit.  */
3409   this_cu->cu = &cu;
3410   cu.per_cu = this_cu;
3411
3412   /* Read the abbrevs for this compilation unit into a table.  */
3413   dwarf2_read_abbrevs (abfd, &cu);
3414   make_cleanup (dwarf2_free_abbrev_table, &cu);
3415
3416   /* Read the compilation unit die.  */
3417   init_cu_die_reader (&reader_specs, &cu);
3418   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3419                             &has_children);
3420
3421   if (this_cu->debug_type_section)
3422     {
3423       /* LENGTH has not been set yet for type units.  */
3424       gdb_assert (this_cu->offset == cu.header.offset);
3425       this_cu->length = cu.header.length + cu.header.initial_length_size;
3426     }
3427   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3428     {
3429       info_ptr = (beg_of_comp_unit + cu.header.length
3430                   + cu.header.initial_length_size);
3431       do_cleanups (back_to_inner);
3432       return info_ptr;
3433     }
3434
3435   prepare_one_comp_unit (&cu, comp_unit_die);
3436
3437   /* Allocate a new partial symbol table structure.  */
3438   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3439   if (attr == NULL || !DW_STRING (attr))
3440     filename = "";
3441   else
3442     filename = DW_STRING (attr);
3443   pst = start_psymtab_common (objfile, objfile->section_offsets,
3444                               filename,
3445                               /* TEXTLOW and TEXTHIGH are set below.  */
3446                               0,
3447                               objfile->global_psymbols.next,
3448                               objfile->static_psymbols.next);
3449
3450   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3451   if (attr != NULL)
3452     pst->dirname = DW_STRING (attr);
3453
3454   pst->read_symtab_private = this_cu;
3455
3456   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3457
3458   /* Store the function that reads in the rest of the symbol table.  */
3459   pst->read_symtab = dwarf2_psymtab_to_symtab;
3460
3461   this_cu->v.psymtab = pst;
3462
3463   dwarf2_find_base_address (comp_unit_die, &cu);
3464
3465   /* Possibly set the default values of LOWPC and HIGHPC from
3466      `DW_AT_ranges'.  */
3467   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3468                                       &best_highpc, &cu, pst);
3469   if (has_pc_info == 1 && best_lowpc < best_highpc)
3470     /* Store the contiguous range if it is not empty; it can be empty for
3471        CUs with no code.  */
3472     addrmap_set_empty (objfile->psymtabs_addrmap,
3473                        best_lowpc + baseaddr,
3474                        best_highpc + baseaddr - 1, pst);
3475
3476   /* Check if comp unit has_children.
3477      If so, read the rest of the partial symbols from this comp unit.
3478      If not, there's no more debug_info for this comp unit.  */
3479   if (has_children)
3480     {
3481       struct partial_die_info *first_die;
3482       CORE_ADDR lowpc, highpc;
3483
3484       lowpc = ((CORE_ADDR) -1);
3485       highpc = ((CORE_ADDR) 0);
3486
3487       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3488
3489       scan_partial_symbols (first_die, &lowpc, &highpc,
3490                             ! has_pc_info, &cu);
3491
3492       /* If we didn't find a lowpc, set it to highpc to avoid
3493          complaints from `maint check'.  */
3494       if (lowpc == ((CORE_ADDR) -1))
3495         lowpc = highpc;
3496
3497       /* If the compilation unit didn't have an explicit address range,
3498          then use the information extracted from its child dies.  */
3499       if (! has_pc_info)
3500         {
3501           best_lowpc = lowpc;
3502           best_highpc = highpc;
3503         }
3504     }
3505   pst->textlow = best_lowpc + baseaddr;
3506   pst->texthigh = best_highpc + baseaddr;
3507
3508   pst->n_global_syms = objfile->global_psymbols.next -
3509     (objfile->global_psymbols.list + pst->globals_offset);
3510   pst->n_static_syms = objfile->static_psymbols.next -
3511     (objfile->static_psymbols.list + pst->statics_offset);
3512   sort_pst_symbols (pst);
3513
3514   info_ptr = (beg_of_comp_unit + cu.header.length
3515               + cu.header.initial_length_size);
3516
3517   if (this_cu->debug_type_section)
3518     {
3519       /* It's not clear we want to do anything with stmt lists here.
3520          Waiting to see what gcc ultimately does.  */
3521     }
3522   else
3523     {
3524       /* Get the list of files included in the current compilation unit,
3525          and build a psymtab for each of them.  */
3526       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3527     }
3528
3529   do_cleanups (back_to_inner);
3530
3531   return info_ptr;
3532 }
3533
3534 /* Traversal function for htab_traverse_noresize.
3535    Process one .debug_types comp-unit.  */
3536
3537 static int
3538 process_type_comp_unit (void **slot, void *info)
3539 {
3540   struct signatured_type *entry = (struct signatured_type *) *slot;
3541   struct objfile *objfile = (struct objfile *) info;
3542   struct dwarf2_per_cu_data *this_cu;
3543
3544   this_cu = &entry->per_cu;
3545
3546   gdb_assert (this_cu->debug_type_section->readin);
3547   process_psymtab_comp_unit (objfile, this_cu,
3548                              this_cu->debug_type_section->buffer,
3549                              (this_cu->debug_type_section->buffer
3550                               + this_cu->offset),
3551                              this_cu->debug_type_section->size);
3552
3553   return 1;
3554 }
3555
3556 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3557    Build partial symbol tables for the .debug_types comp-units.  */
3558
3559 static void
3560 build_type_psymtabs (struct objfile *objfile)
3561 {
3562   if (! create_debug_types_hash_table (objfile))
3563     return;
3564
3565   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3566                           process_type_comp_unit, objfile);
3567 }
3568
3569 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3570
3571 static void
3572 psymtabs_addrmap_cleanup (void *o)
3573 {
3574   struct objfile *objfile = o;
3575
3576   objfile->psymtabs_addrmap = NULL;
3577 }
3578
3579 /* Build the partial symbol table by doing a quick pass through the
3580    .debug_info and .debug_abbrev sections.  */
3581
3582 static void
3583 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3584 {
3585   gdb_byte *info_ptr;
3586   struct cleanup *back_to, *addrmap_cleanup;
3587   struct obstack temp_obstack;
3588
3589   dwarf2_per_objfile->reading_partial_symbols = 1;
3590
3591   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3592   info_ptr = dwarf2_per_objfile->info.buffer;
3593
3594   /* Any cached compilation units will be linked by the per-objfile
3595      read_in_chain.  Make sure to free them when we're done.  */
3596   back_to = make_cleanup (free_cached_comp_units, NULL);
3597
3598   build_type_psymtabs (objfile);
3599
3600   create_all_comp_units (objfile);
3601
3602   /* Create a temporary address map on a temporary obstack.  We later
3603      copy this to the final obstack.  */
3604   obstack_init (&temp_obstack);
3605   make_cleanup_obstack_free (&temp_obstack);
3606   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3607   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3608
3609   /* Since the objects we're extracting from .debug_info vary in
3610      length, only the individual functions to extract them (like
3611      read_comp_unit_head and load_partial_die) can really know whether
3612      the buffer is large enough to hold another complete object.
3613
3614      At the moment, they don't actually check that.  If .debug_info
3615      holds just one extra byte after the last compilation unit's dies,
3616      then read_comp_unit_head will happily read off the end of the
3617      buffer.  read_partial_die is similarly casual.  Those functions
3618      should be fixed.
3619
3620      For this loop condition, simply checking whether there's any data
3621      left at all should be sufficient.  */
3622
3623   while (info_ptr < (dwarf2_per_objfile->info.buffer
3624                      + dwarf2_per_objfile->info.size))
3625     {
3626       struct dwarf2_per_cu_data *this_cu;
3627
3628       this_cu = dwarf2_find_comp_unit (info_ptr
3629                                        - dwarf2_per_objfile->info.buffer,
3630                                        objfile);
3631
3632       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3633                                             dwarf2_per_objfile->info.buffer,
3634                                             info_ptr,
3635                                             dwarf2_per_objfile->info.size);
3636     }
3637
3638   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3639                                                     &objfile->objfile_obstack);
3640   discard_cleanups (addrmap_cleanup);
3641
3642   do_cleanups (back_to);
3643 }
3644
3645 /* Load the partial DIEs for a secondary CU into memory.  */
3646
3647 static void
3648 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3649                         struct objfile *objfile)
3650 {
3651   bfd *abfd = objfile->obfd;
3652   gdb_byte *info_ptr;
3653   struct die_info *comp_unit_die;
3654   struct dwarf2_cu *cu;
3655   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3656   int has_children;
3657   struct die_reader_specs reader_specs;
3658   int read_cu = 0;
3659
3660   gdb_assert (! this_cu->debug_type_section);
3661
3662   gdb_assert (dwarf2_per_objfile->info.readin);
3663   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3664
3665   if (this_cu->cu == NULL)
3666     {
3667       cu = xmalloc (sizeof (*cu));
3668       init_one_comp_unit (cu, objfile);
3669
3670       read_cu = 1;
3671
3672       /* If an error occurs while loading, release our storage.  */
3673       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3674
3675       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3676                                               dwarf2_per_objfile->info.buffer,
3677                                               dwarf2_per_objfile->info.size,
3678                                               abfd, 0);
3679
3680       /* Skip dummy compilation units.  */
3681       if (info_ptr >= (dwarf2_per_objfile->info.buffer
3682                        + dwarf2_per_objfile->info.size)
3683           || peek_abbrev_code (abfd, info_ptr) == 0)
3684         {
3685           do_cleanups (free_cu_cleanup);
3686           return;
3687         }
3688
3689       /* Link this compilation unit into the compilation unit tree.  */
3690       this_cu->cu = cu;
3691       cu->per_cu = this_cu;
3692
3693       /* Link this CU into read_in_chain.  */
3694       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3695       dwarf2_per_objfile->read_in_chain = this_cu;
3696     }
3697   else
3698     {
3699       cu = this_cu->cu;
3700       info_ptr += cu->header.first_die_offset;
3701     }
3702
3703   /* Read the abbrevs for this compilation unit into a table.  */
3704   gdb_assert (cu->dwarf2_abbrevs == NULL);
3705   dwarf2_read_abbrevs (abfd, cu);
3706   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3707
3708   /* Read the compilation unit die.  */
3709   init_cu_die_reader (&reader_specs, cu);
3710   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3711                             &has_children);
3712
3713   prepare_one_comp_unit (cu, comp_unit_die);
3714
3715   /* Check if comp unit has_children.
3716      If so, read the rest of the partial symbols from this comp unit.
3717      If not, there's no more debug_info for this comp unit.  */
3718   if (has_children)
3719     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3720
3721   do_cleanups (free_abbrevs_cleanup);
3722
3723   if (read_cu)
3724     {
3725       /* We've successfully allocated this compilation unit.  Let our
3726          caller clean it up when finished with it.  */
3727       discard_cleanups (free_cu_cleanup);
3728     }
3729 }
3730
3731 /* Create a list of all compilation units in OBJFILE.  We do this only
3732    if an inter-comp-unit reference is found; presumably if there is one,
3733    there will be many, and one will occur early in the .debug_info section.
3734    So there's no point in building this list incrementally.  */
3735
3736 static void
3737 create_all_comp_units (struct objfile *objfile)
3738 {
3739   int n_allocated;
3740   int n_comp_units;
3741   struct dwarf2_per_cu_data **all_comp_units;
3742   gdb_byte *info_ptr;
3743
3744   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3745   info_ptr = dwarf2_per_objfile->info.buffer;
3746
3747   n_comp_units = 0;
3748   n_allocated = 10;
3749   all_comp_units = xmalloc (n_allocated
3750                             * sizeof (struct dwarf2_per_cu_data *));
3751
3752   while (info_ptr < dwarf2_per_objfile->info.buffer
3753          + dwarf2_per_objfile->info.size)
3754     {
3755       unsigned int length, initial_length_size;
3756       struct dwarf2_per_cu_data *this_cu;
3757       unsigned int offset;
3758
3759       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3760
3761       /* Read just enough information to find out where the next
3762          compilation unit is.  */
3763       length = read_initial_length (objfile->obfd, info_ptr,
3764                                     &initial_length_size);
3765
3766       /* Save the compilation unit for later lookup.  */
3767       this_cu = obstack_alloc (&objfile->objfile_obstack,
3768                                sizeof (struct dwarf2_per_cu_data));
3769       memset (this_cu, 0, sizeof (*this_cu));
3770       this_cu->offset = offset;
3771       this_cu->length = length + initial_length_size;
3772       this_cu->objfile = objfile;
3773
3774       if (n_comp_units == n_allocated)
3775         {
3776           n_allocated *= 2;
3777           all_comp_units = xrealloc (all_comp_units,
3778                                      n_allocated
3779                                      * sizeof (struct dwarf2_per_cu_data *));
3780         }
3781       all_comp_units[n_comp_units++] = this_cu;
3782
3783       info_ptr = info_ptr + this_cu->length;
3784     }
3785
3786   dwarf2_per_objfile->all_comp_units
3787     = obstack_alloc (&objfile->objfile_obstack,
3788                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3789   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3790           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3791   xfree (all_comp_units);
3792   dwarf2_per_objfile->n_comp_units = n_comp_units;
3793 }
3794
3795 /* Process all loaded DIEs for compilation unit CU, starting at
3796    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3797    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3798    DW_AT_ranges).  If NEED_PC is set, then this function will set
3799    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3800    and record the covered ranges in the addrmap.  */
3801
3802 static void
3803 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3804                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3805 {
3806   struct partial_die_info *pdi;
3807
3808   /* Now, march along the PDI's, descending into ones which have
3809      interesting children but skipping the children of the other ones,
3810      until we reach the end of the compilation unit.  */
3811
3812   pdi = first_die;
3813
3814   while (pdi != NULL)
3815     {
3816       fixup_partial_die (pdi, cu);
3817
3818       /* Anonymous namespaces or modules have no name but have interesting
3819          children, so we need to look at them.  Ditto for anonymous
3820          enums.  */
3821
3822       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3823           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3824         {
3825           switch (pdi->tag)
3826             {
3827             case DW_TAG_subprogram:
3828               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3829               break;
3830             case DW_TAG_constant:
3831             case DW_TAG_variable:
3832             case DW_TAG_typedef:
3833             case DW_TAG_union_type:
3834               if (!pdi->is_declaration)
3835                 {
3836                   add_partial_symbol (pdi, cu);
3837                 }
3838               break;
3839             case DW_TAG_class_type:
3840             case DW_TAG_interface_type:
3841             case DW_TAG_structure_type:
3842               if (!pdi->is_declaration)
3843                 {
3844                   add_partial_symbol (pdi, cu);
3845                 }
3846               break;
3847             case DW_TAG_enumeration_type:
3848               if (!pdi->is_declaration)
3849                 add_partial_enumeration (pdi, cu);
3850               break;
3851             case DW_TAG_base_type:
3852             case DW_TAG_subrange_type:
3853               /* File scope base type definitions are added to the partial
3854                  symbol table.  */
3855               add_partial_symbol (pdi, cu);
3856               break;
3857             case DW_TAG_namespace:
3858               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3859               break;
3860             case DW_TAG_module:
3861               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3862               break;
3863             default:
3864               break;
3865             }
3866         }
3867
3868       /* If the die has a sibling, skip to the sibling.  */
3869
3870       pdi = pdi->die_sibling;
3871     }
3872 }
3873
3874 /* Functions used to compute the fully scoped name of a partial DIE.
3875
3876    Normally, this is simple.  For C++, the parent DIE's fully scoped
3877    name is concatenated with "::" and the partial DIE's name.  For
3878    Java, the same thing occurs except that "." is used instead of "::".
3879    Enumerators are an exception; they use the scope of their parent
3880    enumeration type, i.e. the name of the enumeration type is not
3881    prepended to the enumerator.
3882
3883    There are two complexities.  One is DW_AT_specification; in this
3884    case "parent" means the parent of the target of the specification,
3885    instead of the direct parent of the DIE.  The other is compilers
3886    which do not emit DW_TAG_namespace; in this case we try to guess
3887    the fully qualified name of structure types from their members'
3888    linkage names.  This must be done using the DIE's children rather
3889    than the children of any DW_AT_specification target.  We only need
3890    to do this for structures at the top level, i.e. if the target of
3891    any DW_AT_specification (if any; otherwise the DIE itself) does not
3892    have a parent.  */
3893
3894 /* Compute the scope prefix associated with PDI's parent, in
3895    compilation unit CU.  The result will be allocated on CU's
3896    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3897    field.  NULL is returned if no prefix is necessary.  */
3898 static char *
3899 partial_die_parent_scope (struct partial_die_info *pdi,
3900                           struct dwarf2_cu *cu)
3901 {
3902   char *grandparent_scope;
3903   struct partial_die_info *parent, *real_pdi;
3904
3905   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3906      then this means the parent of the specification DIE.  */
3907
3908   real_pdi = pdi;
3909   while (real_pdi->has_specification)
3910     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3911
3912   parent = real_pdi->die_parent;
3913   if (parent == NULL)
3914     return NULL;
3915
3916   if (parent->scope_set)
3917     return parent->scope;
3918
3919   fixup_partial_die (parent, cu);
3920
3921   grandparent_scope = partial_die_parent_scope (parent, cu);
3922
3923   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3924      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3925      Work around this problem here.  */
3926   if (cu->language == language_cplus
3927       && parent->tag == DW_TAG_namespace
3928       && strcmp (parent->name, "::") == 0
3929       && grandparent_scope == NULL)
3930     {
3931       parent->scope = NULL;
3932       parent->scope_set = 1;
3933       return NULL;
3934     }
3935
3936   if (pdi->tag == DW_TAG_enumerator)
3937     /* Enumerators should not get the name of the enumeration as a prefix.  */
3938     parent->scope = grandparent_scope;
3939   else if (parent->tag == DW_TAG_namespace
3940       || parent->tag == DW_TAG_module
3941       || parent->tag == DW_TAG_structure_type
3942       || parent->tag == DW_TAG_class_type
3943       || parent->tag == DW_TAG_interface_type
3944       || parent->tag == DW_TAG_union_type
3945       || parent->tag == DW_TAG_enumeration_type)
3946     {
3947       if (grandparent_scope == NULL)
3948         parent->scope = parent->name;
3949       else
3950         parent->scope = typename_concat (&cu->comp_unit_obstack,
3951                                          grandparent_scope,
3952                                          parent->name, 0, cu);
3953     }
3954   else
3955     {
3956       /* FIXME drow/2004-04-01: What should we be doing with
3957          function-local names?  For partial symbols, we should probably be
3958          ignoring them.  */
3959       complaint (&symfile_complaints,
3960                  _("unhandled containing DIE tag %d for DIE at %d"),
3961                  parent->tag, pdi->offset);
3962       parent->scope = grandparent_scope;
3963     }
3964
3965   parent->scope_set = 1;
3966   return parent->scope;
3967 }
3968
3969 /* Return the fully scoped name associated with PDI, from compilation unit
3970    CU.  The result will be allocated with malloc.  */
3971 static char *
3972 partial_die_full_name (struct partial_die_info *pdi,
3973                        struct dwarf2_cu *cu)
3974 {
3975   char *parent_scope;
3976
3977   /* If this is a template instantiation, we can not work out the
3978      template arguments from partial DIEs.  So, unfortunately, we have
3979      to go through the full DIEs.  At least any work we do building
3980      types here will be reused if full symbols are loaded later.  */
3981   if (pdi->has_template_arguments)
3982     {
3983       fixup_partial_die (pdi, cu);
3984
3985       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3986         {
3987           struct die_info *die;
3988           struct attribute attr;
3989           struct dwarf2_cu *ref_cu = cu;
3990
3991           attr.name = 0;
3992           attr.form = DW_FORM_ref_addr;
3993           attr.u.addr = pdi->offset;
3994           die = follow_die_ref (NULL, &attr, &ref_cu);
3995
3996           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3997         }
3998     }
3999
4000   parent_scope = partial_die_parent_scope (pdi, cu);
4001   if (parent_scope == NULL)
4002     return NULL;
4003   else
4004     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4005 }
4006
4007 static void
4008 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4009 {
4010   struct objfile *objfile = cu->objfile;
4011   CORE_ADDR addr = 0;
4012   char *actual_name = NULL;
4013   const struct partial_symbol *psym = NULL;
4014   CORE_ADDR baseaddr;
4015   int built_actual_name = 0;
4016
4017   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4018
4019   actual_name = partial_die_full_name (pdi, cu);
4020   if (actual_name)
4021     built_actual_name = 1;
4022
4023   if (actual_name == NULL)
4024     actual_name = pdi->name;
4025
4026   switch (pdi->tag)
4027     {
4028     case DW_TAG_subprogram:
4029       if (pdi->is_external || cu->language == language_ada)
4030         {
4031           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4032              of the global scope.  But in Ada, we want to be able to access
4033              nested procedures globally.  So all Ada subprograms are stored
4034              in the global scope.  */
4035           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4036              mst_text, objfile); */
4037           add_psymbol_to_list (actual_name, strlen (actual_name),
4038                                built_actual_name,
4039                                VAR_DOMAIN, LOC_BLOCK,
4040                                &objfile->global_psymbols,
4041                                0, pdi->lowpc + baseaddr,
4042                                cu->language, objfile);
4043         }
4044       else
4045         {
4046           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4047              mst_file_text, objfile); */
4048           add_psymbol_to_list (actual_name, strlen (actual_name),
4049                                built_actual_name,
4050                                VAR_DOMAIN, LOC_BLOCK,
4051                                &objfile->static_psymbols,
4052                                0, pdi->lowpc + baseaddr,
4053                                cu->language, objfile);
4054         }
4055       break;
4056     case DW_TAG_constant:
4057       {
4058         struct psymbol_allocation_list *list;
4059
4060         if (pdi->is_external)
4061           list = &objfile->global_psymbols;
4062         else
4063           list = &objfile->static_psymbols;
4064         add_psymbol_to_list (actual_name, strlen (actual_name),
4065                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4066                              list, 0, 0, cu->language, objfile);
4067       }
4068       break;
4069     case DW_TAG_variable:
4070       if (pdi->locdesc)
4071         addr = decode_locdesc (pdi->locdesc, cu);
4072
4073       if (pdi->locdesc
4074           && addr == 0
4075           && !dwarf2_per_objfile->has_section_at_zero)
4076         {
4077           /* A global or static variable may also have been stripped
4078              out by the linker if unused, in which case its address
4079              will be nullified; do not add such variables into partial
4080              symbol table then.  */
4081         }
4082       else if (pdi->is_external)
4083         {
4084           /* Global Variable.
4085              Don't enter into the minimal symbol tables as there is
4086              a minimal symbol table entry from the ELF symbols already.
4087              Enter into partial symbol table if it has a location
4088              descriptor or a type.
4089              If the location descriptor is missing, new_symbol will create
4090              a LOC_UNRESOLVED symbol, the address of the variable will then
4091              be determined from the minimal symbol table whenever the variable
4092              is referenced.
4093              The address for the partial symbol table entry is not
4094              used by GDB, but it comes in handy for debugging partial symbol
4095              table building.  */
4096
4097           if (pdi->locdesc || pdi->has_type)
4098             add_psymbol_to_list (actual_name, strlen (actual_name),
4099                                  built_actual_name,
4100                                  VAR_DOMAIN, LOC_STATIC,
4101                                  &objfile->global_psymbols,
4102                                  0, addr + baseaddr,
4103                                  cu->language, objfile);
4104         }
4105       else
4106         {
4107           /* Static Variable.  Skip symbols without location descriptors.  */
4108           if (pdi->locdesc == NULL)
4109             {
4110               if (built_actual_name)
4111                 xfree (actual_name);
4112               return;
4113             }
4114           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4115              mst_file_data, objfile); */
4116           add_psymbol_to_list (actual_name, strlen (actual_name),
4117                                built_actual_name,
4118                                VAR_DOMAIN, LOC_STATIC,
4119                                &objfile->static_psymbols,
4120                                0, addr + baseaddr,
4121                                cu->language, objfile);
4122         }
4123       break;
4124     case DW_TAG_typedef:
4125     case DW_TAG_base_type:
4126     case DW_TAG_subrange_type:
4127       add_psymbol_to_list (actual_name, strlen (actual_name),
4128                            built_actual_name,
4129                            VAR_DOMAIN, LOC_TYPEDEF,
4130                            &objfile->static_psymbols,
4131                            0, (CORE_ADDR) 0, cu->language, objfile);
4132       break;
4133     case DW_TAG_namespace:
4134       add_psymbol_to_list (actual_name, strlen (actual_name),
4135                            built_actual_name,
4136                            VAR_DOMAIN, LOC_TYPEDEF,
4137                            &objfile->global_psymbols,
4138                            0, (CORE_ADDR) 0, cu->language, objfile);
4139       break;
4140     case DW_TAG_class_type:
4141     case DW_TAG_interface_type:
4142     case DW_TAG_structure_type:
4143     case DW_TAG_union_type:
4144     case DW_TAG_enumeration_type:
4145       /* Skip external references.  The DWARF standard says in the section
4146          about "Structure, Union, and Class Type Entries": "An incomplete
4147          structure, union or class type is represented by a structure,
4148          union or class entry that does not have a byte size attribute
4149          and that has a DW_AT_declaration attribute."  */
4150       if (!pdi->has_byte_size && pdi->is_declaration)
4151         {
4152           if (built_actual_name)
4153             xfree (actual_name);
4154           return;
4155         }
4156
4157       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4158          static vs. global.  */
4159       add_psymbol_to_list (actual_name, strlen (actual_name),
4160                            built_actual_name,
4161                            STRUCT_DOMAIN, LOC_TYPEDEF,
4162                            (cu->language == language_cplus
4163                             || cu->language == language_java)
4164                            ? &objfile->global_psymbols
4165                            : &objfile->static_psymbols,
4166                            0, (CORE_ADDR) 0, cu->language, objfile);
4167
4168       break;
4169     case DW_TAG_enumerator:
4170       add_psymbol_to_list (actual_name, strlen (actual_name),
4171                            built_actual_name,
4172                            VAR_DOMAIN, LOC_CONST,
4173                            (cu->language == language_cplus
4174                             || cu->language == language_java)
4175                            ? &objfile->global_psymbols
4176                            : &objfile->static_psymbols,
4177                            0, (CORE_ADDR) 0, cu->language, objfile);
4178       break;
4179     default:
4180       break;
4181     }
4182
4183   if (built_actual_name)
4184     xfree (actual_name);
4185 }
4186
4187 /* Read a partial die corresponding to a namespace; also, add a symbol
4188    corresponding to that namespace to the symbol table.  NAMESPACE is
4189    the name of the enclosing namespace.  */
4190
4191 static void
4192 add_partial_namespace (struct partial_die_info *pdi,
4193                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4194                        int need_pc, struct dwarf2_cu *cu)
4195 {
4196   /* Add a symbol for the namespace.  */
4197
4198   add_partial_symbol (pdi, cu);
4199
4200   /* Now scan partial symbols in that namespace.  */
4201
4202   if (pdi->has_children)
4203     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4204 }
4205
4206 /* Read a partial die corresponding to a Fortran module.  */
4207
4208 static void
4209 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4210                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4211 {
4212   /* Now scan partial symbols in that module.  */
4213
4214   if (pdi->has_children)
4215     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4216 }
4217
4218 /* Read a partial die corresponding to a subprogram and create a partial
4219    symbol for that subprogram.  When the CU language allows it, this
4220    routine also defines a partial symbol for each nested subprogram
4221    that this subprogram contains.
4222
4223    DIE my also be a lexical block, in which case we simply search
4224    recursively for suprograms defined inside that lexical block.
4225    Again, this is only performed when the CU language allows this
4226    type of definitions.  */
4227
4228 static void
4229 add_partial_subprogram (struct partial_die_info *pdi,
4230                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4231                         int need_pc, struct dwarf2_cu *cu)
4232 {
4233   if (pdi->tag == DW_TAG_subprogram)
4234     {
4235       if (pdi->has_pc_info)
4236         {
4237           if (pdi->lowpc < *lowpc)
4238             *lowpc = pdi->lowpc;
4239           if (pdi->highpc > *highpc)
4240             *highpc = pdi->highpc;
4241           if (need_pc)
4242             {
4243               CORE_ADDR baseaddr;
4244               struct objfile *objfile = cu->objfile;
4245
4246               baseaddr = ANOFFSET (objfile->section_offsets,
4247                                    SECT_OFF_TEXT (objfile));
4248               addrmap_set_empty (objfile->psymtabs_addrmap,
4249                                  pdi->lowpc + baseaddr,
4250                                  pdi->highpc - 1 + baseaddr,
4251                                  cu->per_cu->v.psymtab);
4252             }
4253           if (!pdi->is_declaration)
4254             /* Ignore subprogram DIEs that do not have a name, they are
4255                illegal.  Do not emit a complaint at this point, we will
4256                do so when we convert this psymtab into a symtab.  */
4257             if (pdi->name)
4258               add_partial_symbol (pdi, cu);
4259         }
4260     }
4261
4262   if (! pdi->has_children)
4263     return;
4264
4265   if (cu->language == language_ada)
4266     {
4267       pdi = pdi->die_child;
4268       while (pdi != NULL)
4269         {
4270           fixup_partial_die (pdi, cu);
4271           if (pdi->tag == DW_TAG_subprogram
4272               || pdi->tag == DW_TAG_lexical_block)
4273             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4274           pdi = pdi->die_sibling;
4275         }
4276     }
4277 }
4278
4279 /* Read a partial die corresponding to an enumeration type.  */
4280
4281 static void
4282 add_partial_enumeration (struct partial_die_info *enum_pdi,
4283                          struct dwarf2_cu *cu)
4284 {
4285   struct partial_die_info *pdi;
4286
4287   if (enum_pdi->name != NULL)
4288     add_partial_symbol (enum_pdi, cu);
4289
4290   pdi = enum_pdi->die_child;
4291   while (pdi)
4292     {
4293       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4294         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4295       else
4296         add_partial_symbol (pdi, cu);
4297       pdi = pdi->die_sibling;
4298     }
4299 }
4300
4301 /* Return the initial uleb128 in the die at INFO_PTR.  */
4302
4303 static unsigned int
4304 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4305 {
4306   unsigned int bytes_read;
4307
4308   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4309 }
4310
4311 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4312    Return the corresponding abbrev, or NULL if the number is zero (indicating
4313    an empty DIE).  In either case *BYTES_READ will be set to the length of
4314    the initial number.  */
4315
4316 static struct abbrev_info *
4317 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4318                  struct dwarf2_cu *cu)
4319 {
4320   bfd *abfd = cu->objfile->obfd;
4321   unsigned int abbrev_number;
4322   struct abbrev_info *abbrev;
4323
4324   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4325
4326   if (abbrev_number == 0)
4327     return NULL;
4328
4329   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4330   if (!abbrev)
4331     {
4332       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4333              abbrev_number, bfd_get_filename (abfd));
4334     }
4335
4336   return abbrev;
4337 }
4338
4339 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4340    Returns a pointer to the end of a series of DIEs, terminated by an empty
4341    DIE.  Any children of the skipped DIEs will also be skipped.  */
4342
4343 static gdb_byte *
4344 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4345 {
4346   struct abbrev_info *abbrev;
4347   unsigned int bytes_read;
4348
4349   while (1)
4350     {
4351       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4352       if (abbrev == NULL)
4353         return info_ptr + bytes_read;
4354       else
4355         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4356     }
4357 }
4358
4359 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4360    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4361    abbrev corresponding to that skipped uleb128 should be passed in
4362    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4363    children.  */
4364
4365 static gdb_byte *
4366 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4367               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4368 {
4369   unsigned int bytes_read;
4370   struct attribute attr;
4371   bfd *abfd = cu->objfile->obfd;
4372   unsigned int form, i;
4373
4374   for (i = 0; i < abbrev->num_attrs; i++)
4375     {
4376       /* The only abbrev we care about is DW_AT_sibling.  */
4377       if (abbrev->attrs[i].name == DW_AT_sibling)
4378         {
4379           read_attribute (&attr, &abbrev->attrs[i],
4380                           abfd, info_ptr, cu);
4381           if (attr.form == DW_FORM_ref_addr)
4382             complaint (&symfile_complaints,
4383                        _("ignoring absolute DW_AT_sibling"));
4384           else
4385             return buffer + dwarf2_get_ref_die_offset (&attr);
4386         }
4387
4388       /* If it isn't DW_AT_sibling, skip this attribute.  */
4389       form = abbrev->attrs[i].form;
4390     skip_attribute:
4391       switch (form)
4392         {
4393         case DW_FORM_ref_addr:
4394           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4395              and later it is offset sized.  */
4396           if (cu->header.version == 2)
4397             info_ptr += cu->header.addr_size;
4398           else
4399             info_ptr += cu->header.offset_size;
4400           break;
4401         case DW_FORM_addr:
4402           info_ptr += cu->header.addr_size;
4403           break;
4404         case DW_FORM_data1:
4405         case DW_FORM_ref1:
4406         case DW_FORM_flag:
4407           info_ptr += 1;
4408           break;
4409         case DW_FORM_flag_present:
4410           break;
4411         case DW_FORM_data2:
4412         case DW_FORM_ref2:
4413           info_ptr += 2;
4414           break;
4415         case DW_FORM_data4:
4416         case DW_FORM_ref4:
4417           info_ptr += 4;
4418           break;
4419         case DW_FORM_data8:
4420         case DW_FORM_ref8:
4421         case DW_FORM_ref_sig8:
4422           info_ptr += 8;
4423           break;
4424         case DW_FORM_string:
4425           read_direct_string (abfd, info_ptr, &bytes_read);
4426           info_ptr += bytes_read;
4427           break;
4428         case DW_FORM_sec_offset:
4429         case DW_FORM_strp:
4430           info_ptr += cu->header.offset_size;
4431           break;
4432         case DW_FORM_exprloc:
4433         case DW_FORM_block:
4434           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4435           info_ptr += bytes_read;
4436           break;
4437         case DW_FORM_block1:
4438           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4439           break;
4440         case DW_FORM_block2:
4441           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4442           break;
4443         case DW_FORM_block4:
4444           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4445           break;
4446         case DW_FORM_sdata:
4447         case DW_FORM_udata:
4448         case DW_FORM_ref_udata:
4449           info_ptr = skip_leb128 (abfd, info_ptr);
4450           break;
4451         case DW_FORM_indirect:
4452           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4453           info_ptr += bytes_read;
4454           /* We need to continue parsing from here, so just go back to
4455              the top.  */
4456           goto skip_attribute;
4457
4458         default:
4459           error (_("Dwarf Error: Cannot handle %s "
4460                    "in DWARF reader [in module %s]"),
4461                  dwarf_form_name (form),
4462                  bfd_get_filename (abfd));
4463         }
4464     }
4465
4466   if (abbrev->has_children)
4467     return skip_children (buffer, info_ptr, cu);
4468   else
4469     return info_ptr;
4470 }
4471
4472 /* Locate ORIG_PDI's sibling.
4473    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4474    in BUFFER.  */
4475
4476 static gdb_byte *
4477 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4478                     gdb_byte *buffer, gdb_byte *info_ptr,
4479                     bfd *abfd, struct dwarf2_cu *cu)
4480 {
4481   /* Do we know the sibling already?  */
4482
4483   if (orig_pdi->sibling)
4484     return orig_pdi->sibling;
4485
4486   /* Are there any children to deal with?  */
4487
4488   if (!orig_pdi->has_children)
4489     return info_ptr;
4490
4491   /* Skip the children the long way.  */
4492
4493   return skip_children (buffer, info_ptr, cu);
4494 }
4495
4496 /* Expand this partial symbol table into a full symbol table.  */
4497
4498 static void
4499 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4500 {
4501   if (pst != NULL)
4502     {
4503       if (pst->readin)
4504         {
4505           warning (_("bug: psymtab for %s is already read in."),
4506                    pst->filename);
4507         }
4508       else
4509         {
4510           if (info_verbose)
4511             {
4512               printf_filtered (_("Reading in symbols for %s..."),
4513                                pst->filename);
4514               gdb_flush (gdb_stdout);
4515             }
4516
4517           /* Restore our global data.  */
4518           dwarf2_per_objfile = objfile_data (pst->objfile,
4519                                              dwarf2_objfile_data_key);
4520
4521           /* If this psymtab is constructed from a debug-only objfile, the
4522              has_section_at_zero flag will not necessarily be correct.  We
4523              can get the correct value for this flag by looking at the data
4524              associated with the (presumably stripped) associated objfile.  */
4525           if (pst->objfile->separate_debug_objfile_backlink)
4526             {
4527               struct dwarf2_per_objfile *dpo_backlink
4528                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4529                                 dwarf2_objfile_data_key);
4530
4531               dwarf2_per_objfile->has_section_at_zero
4532                 = dpo_backlink->has_section_at_zero;
4533             }
4534
4535           dwarf2_per_objfile->reading_partial_symbols = 0;
4536
4537           psymtab_to_symtab_1 (pst);
4538
4539           /* Finish up the debug error message.  */
4540           if (info_verbose)
4541             printf_filtered (_("done.\n"));
4542         }
4543     }
4544 }
4545
4546 /* Add PER_CU to the queue.  */
4547
4548 static void
4549 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4550 {
4551   struct dwarf2_queue_item *item;
4552
4553   per_cu->queued = 1;
4554   item = xmalloc (sizeof (*item));
4555   item->per_cu = per_cu;
4556   item->next = NULL;
4557
4558   if (dwarf2_queue == NULL)
4559     dwarf2_queue = item;
4560   else
4561     dwarf2_queue_tail->next = item;
4562
4563   dwarf2_queue_tail = item;
4564 }
4565
4566 /* Process the queue.  */
4567
4568 static void
4569 process_queue (struct objfile *objfile)
4570 {
4571   struct dwarf2_queue_item *item, *next_item;
4572
4573   /* The queue starts out with one item, but following a DIE reference
4574      may load a new CU, adding it to the end of the queue.  */
4575   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4576     {
4577       if (dwarf2_per_objfile->using_index
4578           ? !item->per_cu->v.quick->symtab
4579           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4580         process_full_comp_unit (item->per_cu);
4581
4582       item->per_cu->queued = 0;
4583       next_item = item->next;
4584       xfree (item);
4585     }
4586
4587   dwarf2_queue_tail = NULL;
4588 }
4589
4590 /* Free all allocated queue entries.  This function only releases anything if
4591    an error was thrown; if the queue was processed then it would have been
4592    freed as we went along.  */
4593
4594 static void
4595 dwarf2_release_queue (void *dummy)
4596 {
4597   struct dwarf2_queue_item *item, *last;
4598
4599   item = dwarf2_queue;
4600   while (item)
4601     {
4602       /* Anything still marked queued is likely to be in an
4603          inconsistent state, so discard it.  */
4604       if (item->per_cu->queued)
4605         {
4606           if (item->per_cu->cu != NULL)
4607             free_one_cached_comp_unit (item->per_cu->cu);
4608           item->per_cu->queued = 0;
4609         }
4610
4611       last = item;
4612       item = item->next;
4613       xfree (last);
4614     }
4615
4616   dwarf2_queue = dwarf2_queue_tail = NULL;
4617 }
4618
4619 /* Read in full symbols for PST, and anything it depends on.  */
4620
4621 static void
4622 psymtab_to_symtab_1 (struct partial_symtab *pst)
4623 {
4624   struct dwarf2_per_cu_data *per_cu;
4625   struct cleanup *back_to;
4626   int i;
4627
4628   for (i = 0; i < pst->number_of_dependencies; i++)
4629     if (!pst->dependencies[i]->readin)
4630       {
4631         /* Inform about additional files that need to be read in.  */
4632         if (info_verbose)
4633           {
4634             /* FIXME: i18n: Need to make this a single string.  */
4635             fputs_filtered (" ", gdb_stdout);
4636             wrap_here ("");
4637             fputs_filtered ("and ", gdb_stdout);
4638             wrap_here ("");
4639             printf_filtered ("%s...", pst->dependencies[i]->filename);
4640             wrap_here ("");     /* Flush output.  */
4641             gdb_flush (gdb_stdout);
4642           }
4643         psymtab_to_symtab_1 (pst->dependencies[i]);
4644       }
4645
4646   per_cu = pst->read_symtab_private;
4647
4648   if (per_cu == NULL)
4649     {
4650       /* It's an include file, no symbols to read for it.
4651          Everything is in the parent symtab.  */
4652       pst->readin = 1;
4653       return;
4654     }
4655
4656   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4657 }
4658
4659 /* Load the DIEs associated with PER_CU into memory.  */
4660
4661 static void
4662 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4663                      struct objfile *objfile)
4664 {
4665   bfd *abfd = objfile->obfd;
4666   struct dwarf2_cu *cu;
4667   unsigned int offset;
4668   gdb_byte *info_ptr, *beg_of_comp_unit;
4669   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4670   struct attribute *attr;
4671   int read_cu = 0;
4672
4673   gdb_assert (! per_cu->debug_type_section);
4674
4675   /* Set local variables from the partial symbol table info.  */
4676   offset = per_cu->offset;
4677
4678   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4679   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4680   beg_of_comp_unit = info_ptr;
4681
4682   if (per_cu->cu == NULL)
4683     {
4684       cu = xmalloc (sizeof (*cu));
4685       init_one_comp_unit (cu, objfile);
4686
4687       read_cu = 1;
4688
4689       /* If an error occurs while loading, release our storage.  */
4690       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4691
4692       /* Read in the comp_unit header.  */
4693       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4694
4695       /* Skip dummy compilation units.  */
4696       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4697                        + dwarf2_per_objfile->info.size)
4698           || peek_abbrev_code (abfd, info_ptr) == 0)
4699         {
4700           do_cleanups (free_cu_cleanup);
4701           return;
4702         }
4703
4704       /* Complete the cu_header.  */
4705       cu->header.offset = offset;
4706       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4707
4708       /* Read the abbrevs for this compilation unit.  */
4709       dwarf2_read_abbrevs (abfd, cu);
4710       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4711
4712       /* Link this compilation unit into the compilation unit tree.  */
4713       per_cu->cu = cu;
4714       cu->per_cu = per_cu;
4715
4716       /* Link this CU into read_in_chain.  */
4717       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4718       dwarf2_per_objfile->read_in_chain = per_cu;
4719     }
4720   else
4721     {
4722       cu = per_cu->cu;
4723       info_ptr += cu->header.first_die_offset;
4724     }
4725
4726   cu->dies = read_comp_unit (info_ptr, cu);
4727
4728   /* We try not to read any attributes in this function, because not
4729      all objfiles needed for references have been loaded yet, and symbol
4730      table processing isn't initialized.  But we have to set the CU language,
4731      or we won't be able to build types correctly.  */
4732   prepare_one_comp_unit (cu, cu->dies);
4733
4734   /* Similarly, if we do not read the producer, we can not apply
4735      producer-specific interpretation.  */
4736   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4737   if (attr)
4738     cu->producer = DW_STRING (attr);
4739
4740   if (read_cu)
4741     {
4742       do_cleanups (free_abbrevs_cleanup);
4743
4744       /* We've successfully allocated this compilation unit.  Let our
4745          caller clean it up when finished with it.  */
4746       discard_cleanups (free_cu_cleanup);
4747     }
4748 }
4749
4750 /* Add a DIE to the delayed physname list.  */
4751
4752 static void
4753 add_to_method_list (struct type *type, int fnfield_index, int index,
4754                     const char *name, struct die_info *die,
4755                     struct dwarf2_cu *cu)
4756 {
4757   struct delayed_method_info mi;
4758   mi.type = type;
4759   mi.fnfield_index = fnfield_index;
4760   mi.index = index;
4761   mi.name = name;
4762   mi.die = die;
4763   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4764 }
4765
4766 /* A cleanup for freeing the delayed method list.  */
4767
4768 static void
4769 free_delayed_list (void *ptr)
4770 {
4771   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4772   if (cu->method_list != NULL)
4773     {
4774       VEC_free (delayed_method_info, cu->method_list);
4775       cu->method_list = NULL;
4776     }
4777 }
4778
4779 /* Compute the physnames of any methods on the CU's method list.
4780
4781    The computation of method physnames is delayed in order to avoid the
4782    (bad) condition that one of the method's formal parameters is of an as yet
4783    incomplete type.  */
4784
4785 static void
4786 compute_delayed_physnames (struct dwarf2_cu *cu)
4787 {
4788   int i;
4789   struct delayed_method_info *mi;
4790   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4791     {
4792       const char *physname;
4793       struct fn_fieldlist *fn_flp
4794         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4795       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4796       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4797     }
4798 }
4799
4800 /* Generate full symbol information for PST and CU, whose DIEs have
4801    already been loaded into memory.  */
4802
4803 static void
4804 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4805 {
4806   struct dwarf2_cu *cu = per_cu->cu;
4807   struct objfile *objfile = per_cu->objfile;
4808   CORE_ADDR lowpc, highpc;
4809   struct symtab *symtab;
4810   struct cleanup *back_to, *delayed_list_cleanup;
4811   CORE_ADDR baseaddr;
4812
4813   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4814
4815   buildsym_init ();
4816   back_to = make_cleanup (really_free_pendings, NULL);
4817   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4818
4819   cu->list_in_scope = &file_symbols;
4820
4821   /* Do line number decoding in read_file_scope () */
4822   process_die (cu->dies, cu);
4823
4824   /* Now that we have processed all the DIEs in the CU, all the types 
4825      should be complete, and it should now be safe to compute all of the
4826      physnames.  */
4827   compute_delayed_physnames (cu);
4828   do_cleanups (delayed_list_cleanup);
4829
4830   /* Some compilers don't define a DW_AT_high_pc attribute for the
4831      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4832      it, by scanning the DIE's below the compilation unit.  */
4833   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4834
4835   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4836
4837   if (symtab != NULL)
4838     {
4839       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4840
4841       /* Set symtab language to language from DW_AT_language.  If the
4842          compilation is from a C file generated by language preprocessors, do
4843          not set the language if it was already deduced by start_subfile.  */
4844       if (!(cu->language == language_c && symtab->language != language_c))
4845         symtab->language = cu->language;
4846
4847       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4848          produce DW_AT_location with location lists but it can be possibly
4849          invalid without -fvar-tracking.
4850
4851          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4852          needed, it would be wrong due to missing DW_AT_producer there.
4853
4854          Still one can confuse GDB by using non-standard GCC compilation
4855          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4856          */ 
4857       if (cu->has_loclist && gcc_4_minor >= 0)
4858         symtab->locations_valid = 1;
4859
4860       if (gcc_4_minor >= 5)
4861         symtab->epilogue_unwind_valid = 1;
4862
4863       symtab->call_site_htab = cu->call_site_htab;
4864     }
4865
4866   if (dwarf2_per_objfile->using_index)
4867     per_cu->v.quick->symtab = symtab;
4868   else
4869     {
4870       struct partial_symtab *pst = per_cu->v.psymtab;
4871       pst->symtab = symtab;
4872       pst->readin = 1;
4873     }
4874
4875   do_cleanups (back_to);
4876 }
4877
4878 /* Process a die and its children.  */
4879
4880 static void
4881 process_die (struct die_info *die, struct dwarf2_cu *cu)
4882 {
4883   switch (die->tag)
4884     {
4885     case DW_TAG_padding:
4886       break;
4887     case DW_TAG_compile_unit:
4888       read_file_scope (die, cu);
4889       break;
4890     case DW_TAG_type_unit:
4891       read_type_unit_scope (die, cu);
4892       break;
4893     case DW_TAG_subprogram:
4894     case DW_TAG_inlined_subroutine:
4895       read_func_scope (die, cu);
4896       break;
4897     case DW_TAG_lexical_block:
4898     case DW_TAG_try_block:
4899     case DW_TAG_catch_block:
4900       read_lexical_block_scope (die, cu);
4901       break;
4902     case DW_TAG_GNU_call_site:
4903       read_call_site_scope (die, cu);
4904       break;
4905     case DW_TAG_class_type:
4906     case DW_TAG_interface_type:
4907     case DW_TAG_structure_type:
4908     case DW_TAG_union_type:
4909       process_structure_scope (die, cu);
4910       break;
4911     case DW_TAG_enumeration_type:
4912       process_enumeration_scope (die, cu);
4913       break;
4914
4915     /* These dies have a type, but processing them does not create
4916        a symbol or recurse to process the children.  Therefore we can
4917        read them on-demand through read_type_die.  */
4918     case DW_TAG_subroutine_type:
4919     case DW_TAG_set_type:
4920     case DW_TAG_array_type:
4921     case DW_TAG_pointer_type:
4922     case DW_TAG_ptr_to_member_type:
4923     case DW_TAG_reference_type:
4924     case DW_TAG_string_type:
4925       break;
4926
4927     case DW_TAG_base_type:
4928     case DW_TAG_subrange_type:
4929     case DW_TAG_typedef:
4930       /* Add a typedef symbol for the type definition, if it has a
4931          DW_AT_name.  */
4932       new_symbol (die, read_type_die (die, cu), cu);
4933       break;
4934     case DW_TAG_common_block:
4935       read_common_block (die, cu);
4936       break;
4937     case DW_TAG_common_inclusion:
4938       break;
4939     case DW_TAG_namespace:
4940       processing_has_namespace_info = 1;
4941       read_namespace (die, cu);
4942       break;
4943     case DW_TAG_module:
4944       processing_has_namespace_info = 1;
4945       read_module (die, cu);
4946       break;
4947     case DW_TAG_imported_declaration:
4948     case DW_TAG_imported_module:
4949       processing_has_namespace_info = 1;
4950       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4951                                  || cu->language != language_fortran))
4952         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4953                    dwarf_tag_name (die->tag));
4954       read_import_statement (die, cu);
4955       break;
4956     default:
4957       new_symbol (die, NULL, cu);
4958       break;
4959     }
4960 }
4961
4962 /* A helper function for dwarf2_compute_name which determines whether DIE
4963    needs to have the name of the scope prepended to the name listed in the
4964    die.  */
4965
4966 static int
4967 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4968 {
4969   struct attribute *attr;
4970
4971   switch (die->tag)
4972     {
4973     case DW_TAG_namespace:
4974     case DW_TAG_typedef:
4975     case DW_TAG_class_type:
4976     case DW_TAG_interface_type:
4977     case DW_TAG_structure_type:
4978     case DW_TAG_union_type:
4979     case DW_TAG_enumeration_type:
4980     case DW_TAG_enumerator:
4981     case DW_TAG_subprogram:
4982     case DW_TAG_member:
4983       return 1;
4984
4985     case DW_TAG_variable:
4986     case DW_TAG_constant:
4987       /* We only need to prefix "globally" visible variables.  These include
4988          any variable marked with DW_AT_external or any variable that
4989          lives in a namespace.  [Variables in anonymous namespaces
4990          require prefixing, but they are not DW_AT_external.]  */
4991
4992       if (dwarf2_attr (die, DW_AT_specification, cu))
4993         {
4994           struct dwarf2_cu *spec_cu = cu;
4995
4996           return die_needs_namespace (die_specification (die, &spec_cu),
4997                                       spec_cu);
4998         }
4999
5000       attr = dwarf2_attr (die, DW_AT_external, cu);
5001       if (attr == NULL && die->parent->tag != DW_TAG_namespace
5002           && die->parent->tag != DW_TAG_module)
5003         return 0;
5004       /* A variable in a lexical block of some kind does not need a
5005          namespace, even though in C++ such variables may be external
5006          and have a mangled name.  */
5007       if (die->parent->tag ==  DW_TAG_lexical_block
5008           || die->parent->tag ==  DW_TAG_try_block
5009           || die->parent->tag ==  DW_TAG_catch_block
5010           || die->parent->tag == DW_TAG_subprogram)
5011         return 0;
5012       return 1;
5013
5014     default:
5015       return 0;
5016     }
5017 }
5018
5019 /* Retrieve the last character from a mem_file.  */
5020
5021 static void
5022 do_ui_file_peek_last (void *object, const char *buffer, long length)
5023 {
5024   char *last_char_p = (char *) object;
5025
5026   if (length > 0)
5027     *last_char_p = buffer[length - 1];
5028 }
5029
5030 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
5031    compute the physname for the object, which include a method's
5032    formal parameters (C++/Java) and return type (Java).
5033
5034    For Ada, return the DIE's linkage name rather than the fully qualified
5035    name.  PHYSNAME is ignored..
5036
5037    The result is allocated on the objfile_obstack and canonicalized.  */
5038
5039 static const char *
5040 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5041                      int physname)
5042 {
5043   if (name == NULL)
5044     name = dwarf2_name (die, cu);
5045
5046   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5047      compute it by typename_concat inside GDB.  */
5048   if (cu->language == language_ada
5049       || (cu->language == language_fortran && physname))
5050     {
5051       /* For Ada unit, we prefer the linkage name over the name, as
5052          the former contains the exported name, which the user expects
5053          to be able to reference.  Ideally, we want the user to be able
5054          to reference this entity using either natural or linkage name,
5055          but we haven't started looking at this enhancement yet.  */
5056       struct attribute *attr;
5057
5058       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5059       if (attr == NULL)
5060         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5061       if (attr && DW_STRING (attr))
5062         return DW_STRING (attr);
5063     }
5064
5065   /* These are the only languages we know how to qualify names in.  */
5066   if (name != NULL
5067       && (cu->language == language_cplus || cu->language == language_java
5068           || cu->language == language_fortran))
5069     {
5070       if (die_needs_namespace (die, cu))
5071         {
5072           long length;
5073           char *prefix;
5074           struct ui_file *buf;
5075
5076           prefix = determine_prefix (die, cu);
5077           buf = mem_fileopen ();
5078           if (*prefix != '\0')
5079             {
5080               char *prefixed_name = typename_concat (NULL, prefix, name,
5081                                                      physname, cu);
5082
5083               fputs_unfiltered (prefixed_name, buf);
5084               xfree (prefixed_name);
5085             }
5086           else
5087             fputs_unfiltered (name, buf);
5088
5089           /* Template parameters may be specified in the DIE's DW_AT_name, or
5090              as children with DW_TAG_template_type_param or
5091              DW_TAG_value_type_param.  If the latter, add them to the name
5092              here.  If the name already has template parameters, then
5093              skip this step; some versions of GCC emit both, and
5094              it is more efficient to use the pre-computed name.
5095
5096              Something to keep in mind about this process: it is very
5097              unlikely, or in some cases downright impossible, to produce
5098              something that will match the mangled name of a function.
5099              If the definition of the function has the same debug info,
5100              we should be able to match up with it anyway.  But fallbacks
5101              using the minimal symbol, for instance to find a method
5102              implemented in a stripped copy of libstdc++, will not work.
5103              If we do not have debug info for the definition, we will have to
5104              match them up some other way.
5105
5106              When we do name matching there is a related problem with function
5107              templates; two instantiated function templates are allowed to
5108              differ only by their return types, which we do not add here.  */
5109
5110           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5111             {
5112               struct attribute *attr;
5113               struct die_info *child;
5114               int first = 1;
5115
5116               die->building_fullname = 1;
5117
5118               for (child = die->child; child != NULL; child = child->sibling)
5119                 {
5120                   struct type *type;
5121                   long value;
5122                   gdb_byte *bytes;
5123                   struct dwarf2_locexpr_baton *baton;
5124                   struct value *v;
5125
5126                   if (child->tag != DW_TAG_template_type_param
5127                       && child->tag != DW_TAG_template_value_param)
5128                     continue;
5129
5130                   if (first)
5131                     {
5132                       fputs_unfiltered ("<", buf);
5133                       first = 0;
5134                     }
5135                   else
5136                     fputs_unfiltered (", ", buf);
5137
5138                   attr = dwarf2_attr (child, DW_AT_type, cu);
5139                   if (attr == NULL)
5140                     {
5141                       complaint (&symfile_complaints,
5142                                  _("template parameter missing DW_AT_type"));
5143                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5144                       continue;
5145                     }
5146                   type = die_type (child, cu);
5147
5148                   if (child->tag == DW_TAG_template_type_param)
5149                     {
5150                       c_print_type (type, "", buf, -1, 0);
5151                       continue;
5152                     }
5153
5154                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5155                   if (attr == NULL)
5156                     {
5157                       complaint (&symfile_complaints,
5158                                  _("template parameter missing "
5159                                    "DW_AT_const_value"));
5160                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5161                       continue;
5162                     }
5163
5164                   dwarf2_const_value_attr (attr, type, name,
5165                                            &cu->comp_unit_obstack, cu,
5166                                            &value, &bytes, &baton);
5167
5168                   if (TYPE_NOSIGN (type))
5169                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5170                        changed, this can use value_print instead.  */
5171                     c_printchar (value, type, buf);
5172                   else
5173                     {
5174                       struct value_print_options opts;
5175
5176                       if (baton != NULL)
5177                         v = dwarf2_evaluate_loc_desc (type, NULL,
5178                                                       baton->data,
5179                                                       baton->size,
5180                                                       baton->per_cu);
5181                       else if (bytes != NULL)
5182                         {
5183                           v = allocate_value (type);
5184                           memcpy (value_contents_writeable (v), bytes,
5185                                   TYPE_LENGTH (type));
5186                         }
5187                       else
5188                         v = value_from_longest (type, value);
5189
5190                       /* Specify decimal so that we do not depend on
5191                          the radix.  */
5192                       get_formatted_print_options (&opts, 'd');
5193                       opts.raw = 1;
5194                       value_print (v, buf, &opts);
5195                       release_value (v);
5196                       value_free (v);
5197                     }
5198                 }
5199
5200               die->building_fullname = 0;
5201
5202               if (!first)
5203                 {
5204                   /* Close the argument list, with a space if necessary
5205                      (nested templates).  */
5206                   char last_char = '\0';
5207                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5208                   if (last_char == '>')
5209                     fputs_unfiltered (" >", buf);
5210                   else
5211                     fputs_unfiltered (">", buf);
5212                 }
5213             }
5214
5215           /* For Java and C++ methods, append formal parameter type
5216              information, if PHYSNAME.  */
5217
5218           if (physname && die->tag == DW_TAG_subprogram
5219               && (cu->language == language_cplus
5220                   || cu->language == language_java))
5221             {
5222               struct type *type = read_type_die (die, cu);
5223
5224               c_type_print_args (type, buf, 1, cu->language);
5225
5226               if (cu->language == language_java)
5227                 {
5228                   /* For java, we must append the return type to method
5229                      names.  */
5230                   if (die->tag == DW_TAG_subprogram)
5231                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5232                                      0, 0);
5233                 }
5234               else if (cu->language == language_cplus)
5235                 {
5236                   /* Assume that an artificial first parameter is
5237                      "this", but do not crash if it is not.  RealView
5238                      marks unnamed (and thus unused) parameters as
5239                      artificial; there is no way to differentiate
5240                      the two cases.  */
5241                   if (TYPE_NFIELDS (type) > 0
5242                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5243                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5244                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5245                                                                         0))))
5246                     fputs_unfiltered (" const", buf);
5247                 }
5248             }
5249
5250           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5251                                        &length);
5252           ui_file_delete (buf);
5253
5254           if (cu->language == language_cplus)
5255             {
5256               char *cname
5257                 = dwarf2_canonicalize_name (name, cu,
5258                                             &cu->objfile->objfile_obstack);
5259
5260               if (cname != NULL)
5261                 name = cname;
5262             }
5263         }
5264     }
5265
5266   return name;
5267 }
5268
5269 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5270    If scope qualifiers are appropriate they will be added.  The result
5271    will be allocated on the objfile_obstack, or NULL if the DIE does
5272    not have a name.  NAME may either be from a previous call to
5273    dwarf2_name or NULL.
5274
5275    The output string will be canonicalized (if C++/Java).  */
5276
5277 static const char *
5278 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5279 {
5280   return dwarf2_compute_name (name, die, cu, 0);
5281 }
5282
5283 /* Construct a physname for the given DIE in CU.  NAME may either be
5284    from a previous call to dwarf2_name or NULL.  The result will be
5285    allocated on the objfile_objstack or NULL if the DIE does not have a
5286    name.
5287
5288    The output string will be canonicalized (if C++/Java).  */
5289
5290 static const char *
5291 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5292 {
5293   struct attribute *attr;
5294   const char *retval, *mangled = NULL, *canon = NULL;
5295   struct cleanup *back_to;
5296   int need_copy = 1;
5297
5298   /* In this case dwarf2_compute_name is just a shortcut not building anything
5299      on its own.  */
5300   if (!die_needs_namespace (die, cu))
5301     return dwarf2_compute_name (name, die, cu, 1);
5302
5303   back_to = make_cleanup (null_cleanup, NULL);
5304
5305   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5306   if (!attr)
5307     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5308
5309   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5310      has computed.  */
5311   if (attr && DW_STRING (attr))
5312     {
5313       char *demangled;
5314
5315       mangled = DW_STRING (attr);
5316
5317       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5318          type.  It is easier for GDB users to search for such functions as
5319          `name(params)' than `long name(params)'.  In such case the minimal
5320          symbol names do not match the full symbol names but for template
5321          functions there is never a need to look up their definition from their
5322          declaration so the only disadvantage remains the minimal symbol
5323          variant `long name(params)' does not have the proper inferior type.
5324          */
5325
5326       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5327                                             | (cu->language == language_java
5328                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5329                                                : DMGL_RET_DROP)));
5330       if (demangled)
5331         {
5332           make_cleanup (xfree, demangled);
5333           canon = demangled;
5334         }
5335       else
5336         {
5337           canon = mangled;
5338           need_copy = 0;
5339         }
5340     }
5341
5342   if (canon == NULL || check_physname)
5343     {
5344       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5345
5346       if (canon != NULL && strcmp (physname, canon) != 0)
5347         {
5348           /* It may not mean a bug in GDB.  The compiler could also
5349              compute DW_AT_linkage_name incorrectly.  But in such case
5350              GDB would need to be bug-to-bug compatible.  */
5351
5352           complaint (&symfile_complaints,
5353                      _("Computed physname <%s> does not match demangled <%s> "
5354                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5355                      physname, canon, mangled, die->offset, cu->objfile->name);
5356
5357           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5358              is available here - over computed PHYSNAME.  It is safer
5359              against both buggy GDB and buggy compilers.  */
5360
5361           retval = canon;
5362         }
5363       else
5364         {
5365           retval = physname;
5366           need_copy = 0;
5367         }
5368     }
5369   else
5370     retval = canon;
5371
5372   if (need_copy)
5373     retval = obsavestring (retval, strlen (retval),
5374                            &cu->objfile->objfile_obstack);
5375
5376   do_cleanups (back_to);
5377   return retval;
5378 }
5379
5380 /* Read the import statement specified by the given die and record it.  */
5381
5382 static void
5383 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5384 {
5385   struct attribute *import_attr;
5386   struct die_info *imported_die, *child_die;
5387   struct dwarf2_cu *imported_cu;
5388   const char *imported_name;
5389   const char *imported_name_prefix;
5390   const char *canonical_name;
5391   const char *import_alias;
5392   const char *imported_declaration = NULL;
5393   const char *import_prefix;
5394   VEC (const_char_ptr) *excludes = NULL;
5395   struct cleanup *cleanups;
5396
5397   char *temp;
5398
5399   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5400   if (import_attr == NULL)
5401     {
5402       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5403                  dwarf_tag_name (die->tag));
5404       return;
5405     }
5406
5407   imported_cu = cu;
5408   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5409   imported_name = dwarf2_name (imported_die, imported_cu);
5410   if (imported_name == NULL)
5411     {
5412       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5413
5414         The import in the following code:
5415         namespace A
5416           {
5417             typedef int B;
5418           }
5419
5420         int main ()
5421           {
5422             using A::B;
5423             B b;
5424             return b;
5425           }
5426
5427         ...
5428          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5429             <52>   DW_AT_decl_file   : 1
5430             <53>   DW_AT_decl_line   : 6
5431             <54>   DW_AT_import      : <0x75>
5432          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5433             <59>   DW_AT_name        : B
5434             <5b>   DW_AT_decl_file   : 1
5435             <5c>   DW_AT_decl_line   : 2
5436             <5d>   DW_AT_type        : <0x6e>
5437         ...
5438          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5439             <76>   DW_AT_byte_size   : 4
5440             <77>   DW_AT_encoding    : 5        (signed)
5441
5442         imports the wrong die ( 0x75 instead of 0x58 ).
5443         This case will be ignored until the gcc bug is fixed.  */
5444       return;
5445     }
5446
5447   /* Figure out the local name after import.  */
5448   import_alias = dwarf2_name (die, cu);
5449
5450   /* Figure out where the statement is being imported to.  */
5451   import_prefix = determine_prefix (die, cu);
5452
5453   /* Figure out what the scope of the imported die is and prepend it
5454      to the name of the imported die.  */
5455   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5456
5457   if (imported_die->tag != DW_TAG_namespace
5458       && imported_die->tag != DW_TAG_module)
5459     {
5460       imported_declaration = imported_name;
5461       canonical_name = imported_name_prefix;
5462     }
5463   else if (strlen (imported_name_prefix) > 0)
5464     {
5465       temp = alloca (strlen (imported_name_prefix)
5466                      + 2 + strlen (imported_name) + 1);
5467       strcpy (temp, imported_name_prefix);
5468       strcat (temp, "::");
5469       strcat (temp, imported_name);
5470       canonical_name = temp;
5471     }
5472   else
5473     canonical_name = imported_name;
5474
5475   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5476
5477   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5478     for (child_die = die->child; child_die && child_die->tag;
5479          child_die = sibling_die (child_die))
5480       {
5481         /* DWARF-4: A Fortran use statement with a “rename list” may be
5482            represented by an imported module entry with an import attribute
5483            referring to the module and owned entries corresponding to those
5484            entities that are renamed as part of being imported.  */
5485
5486         if (child_die->tag != DW_TAG_imported_declaration)
5487           {
5488             complaint (&symfile_complaints,
5489                        _("child DW_TAG_imported_declaration expected "
5490                          "- DIE at 0x%x [in module %s]"),
5491                        child_die->offset, cu->objfile->name);
5492             continue;
5493           }
5494
5495         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5496         if (import_attr == NULL)
5497           {
5498             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5499                        dwarf_tag_name (child_die->tag));
5500             continue;
5501           }
5502
5503         imported_cu = cu;
5504         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5505                                               &imported_cu);
5506         imported_name = dwarf2_name (imported_die, imported_cu);
5507         if (imported_name == NULL)
5508           {
5509             complaint (&symfile_complaints,
5510                        _("child DW_TAG_imported_declaration has unknown "
5511                          "imported name - DIE at 0x%x [in module %s]"),
5512                        child_die->offset, cu->objfile->name);
5513             continue;
5514           }
5515
5516         VEC_safe_push (const_char_ptr, excludes, imported_name);
5517
5518         process_die (child_die, cu);
5519       }
5520
5521   cp_add_using_directive (import_prefix,
5522                           canonical_name,
5523                           import_alias,
5524                           imported_declaration,
5525                           excludes,
5526                           &cu->objfile->objfile_obstack);
5527
5528   do_cleanups (cleanups);
5529 }
5530
5531 static void
5532 initialize_cu_func_list (struct dwarf2_cu *cu)
5533 {
5534   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5535 }
5536
5537 /* Cleanup function for read_file_scope.  */
5538
5539 static void
5540 free_cu_line_header (void *arg)
5541 {
5542   struct dwarf2_cu *cu = arg;
5543
5544   free_line_header (cu->line_header);
5545   cu->line_header = NULL;
5546 }
5547
5548 static void
5549 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5550                          char **name, char **comp_dir)
5551 {
5552   struct attribute *attr;
5553
5554   *name = NULL;
5555   *comp_dir = NULL;
5556
5557   /* Find the filename.  Do not use dwarf2_name here, since the filename
5558      is not a source language identifier.  */
5559   attr = dwarf2_attr (die, DW_AT_name, cu);
5560   if (attr)
5561     {
5562       *name = DW_STRING (attr);
5563     }
5564
5565   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5566   if (attr)
5567     *comp_dir = DW_STRING (attr);
5568   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5569     {
5570       *comp_dir = ldirname (*name);
5571       if (*comp_dir != NULL)
5572         make_cleanup (xfree, *comp_dir);
5573     }
5574   if (*comp_dir != NULL)
5575     {
5576       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5577          directory, get rid of it.  */
5578       char *cp = strchr (*comp_dir, ':');
5579
5580       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5581         *comp_dir = cp + 1;
5582     }
5583
5584   if (*name == NULL)
5585     *name = "<unknown>";
5586 }
5587
5588 /* Handle DW_AT_stmt_list for a compilation unit.  */
5589
5590 static void
5591 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5592                         const char *comp_dir)
5593 {
5594   struct attribute *attr;
5595   struct objfile *objfile = cu->objfile;
5596   bfd *abfd = objfile->obfd;
5597
5598   /* Decode line number information if present.  We do this before
5599      processing child DIEs, so that the line header table is available
5600      for DW_AT_decl_file.  */
5601   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5602   if (attr)
5603     {
5604       unsigned int line_offset = DW_UNSND (attr);
5605       struct line_header *line_header
5606         = dwarf_decode_line_header (line_offset, abfd, cu);
5607
5608       if (line_header)
5609         {
5610           cu->line_header = line_header;
5611           make_cleanup (free_cu_line_header, cu);
5612           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5613         }
5614     }
5615 }
5616
5617 /* Process DW_TAG_compile_unit.  */
5618
5619 static void
5620 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5621 {
5622   struct objfile *objfile = cu->objfile;
5623   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5624   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5625   CORE_ADDR highpc = ((CORE_ADDR) 0);
5626   struct attribute *attr;
5627   char *name = NULL;
5628   char *comp_dir = NULL;
5629   struct die_info *child_die;
5630   bfd *abfd = objfile->obfd;
5631   CORE_ADDR baseaddr;
5632
5633   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5634
5635   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5636
5637   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5638      from finish_block.  */
5639   if (lowpc == ((CORE_ADDR) -1))
5640     lowpc = highpc;
5641   lowpc += baseaddr;
5642   highpc += baseaddr;
5643
5644   find_file_and_directory (die, cu, &name, &comp_dir);
5645
5646   attr = dwarf2_attr (die, DW_AT_language, cu);
5647   if (attr)
5648     {
5649       set_cu_language (DW_UNSND (attr), cu);
5650     }
5651
5652   attr = dwarf2_attr (die, DW_AT_producer, cu);
5653   if (attr)
5654     cu->producer = DW_STRING (attr);
5655
5656   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5657      standardised yet.  As a workaround for the language detection we fall
5658      back to the DW_AT_producer string.  */
5659   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5660     cu->language = language_opencl;
5661
5662   /* We assume that we're processing GCC output.  */
5663   processing_gcc_compilation = 2;
5664
5665   processing_has_namespace_info = 0;
5666
5667   start_symtab (name, comp_dir, lowpc);
5668   record_debugformat ("DWARF 2");
5669   record_producer (cu->producer);
5670
5671   initialize_cu_func_list (cu);
5672
5673   handle_DW_AT_stmt_list (die, cu, comp_dir);
5674
5675   /* Process all dies in compilation unit.  */
5676   if (die->child != NULL)
5677     {
5678       child_die = die->child;
5679       while (child_die && child_die->tag)
5680         {
5681           process_die (child_die, cu);
5682           child_die = sibling_die (child_die);
5683         }
5684     }
5685
5686   /* Decode macro information, if present.  Dwarf 2 macro information
5687      refers to information in the line number info statement program
5688      header, so we can only read it if we've read the header
5689      successfully.  */
5690   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5691   if (attr && cu->line_header)
5692     {
5693       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5694         complaint (&symfile_complaints,
5695                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5696
5697       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5698                            comp_dir, abfd, cu,
5699                            &dwarf2_per_objfile->macro, 1);
5700     }
5701   else
5702     {
5703       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5704       if (attr && cu->line_header)
5705         {
5706           unsigned int macro_offset = DW_UNSND (attr);
5707
5708           dwarf_decode_macros (cu->line_header, macro_offset,
5709                                comp_dir, abfd, cu,
5710                                &dwarf2_per_objfile->macinfo, 0);
5711         }
5712     }
5713   do_cleanups (back_to);
5714 }
5715
5716 /* Process DW_TAG_type_unit.
5717    For TUs we want to skip the first top level sibling if it's not the
5718    actual type being defined by this TU.  In this case the first top
5719    level sibling is there to provide context only.  */
5720
5721 static void
5722 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5723 {
5724   struct objfile *objfile = cu->objfile;
5725   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5726   CORE_ADDR lowpc;
5727   struct attribute *attr;
5728   char *name = NULL;
5729   char *comp_dir = NULL;
5730   struct die_info *child_die;
5731   bfd *abfd = objfile->obfd;
5732
5733   /* start_symtab needs a low pc, but we don't really have one.
5734      Do what read_file_scope would do in the absence of such info.  */
5735   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5736
5737   /* Find the filename.  Do not use dwarf2_name here, since the filename
5738      is not a source language identifier.  */
5739   attr = dwarf2_attr (die, DW_AT_name, cu);
5740   if (attr)
5741     name = DW_STRING (attr);
5742
5743   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5744   if (attr)
5745     comp_dir = DW_STRING (attr);
5746   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5747     {
5748       comp_dir = ldirname (name);
5749       if (comp_dir != NULL)
5750         make_cleanup (xfree, comp_dir);
5751     }
5752
5753   if (name == NULL)
5754     name = "<unknown>";
5755
5756   attr = dwarf2_attr (die, DW_AT_language, cu);
5757   if (attr)
5758     set_cu_language (DW_UNSND (attr), cu);
5759
5760   /* This isn't technically needed today.  It is done for symmetry
5761      with read_file_scope.  */
5762   attr = dwarf2_attr (die, DW_AT_producer, cu);
5763   if (attr)
5764     cu->producer = DW_STRING (attr);
5765
5766   /* We assume that we're processing GCC output.  */
5767   processing_gcc_compilation = 2;
5768
5769   processing_has_namespace_info = 0;
5770
5771   start_symtab (name, comp_dir, lowpc);
5772   record_debugformat ("DWARF 2");
5773   record_producer (cu->producer);
5774
5775   handle_DW_AT_stmt_list (die, cu, comp_dir);
5776
5777   /* Process the dies in the type unit.  */
5778   if (die->child == NULL)
5779     {
5780       dump_die_for_error (die);
5781       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5782              bfd_get_filename (abfd));
5783     }
5784
5785   child_die = die->child;
5786
5787   while (child_die && child_die->tag)
5788     {
5789       process_die (child_die, cu);
5790
5791       child_die = sibling_die (child_die);
5792     }
5793
5794   do_cleanups (back_to);
5795 }
5796
5797 static void
5798 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5799                      struct dwarf2_cu *cu)
5800 {
5801   struct function_range *thisfn;
5802
5803   thisfn = (struct function_range *)
5804     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5805   thisfn->name = name;
5806   thisfn->lowpc = lowpc;
5807   thisfn->highpc = highpc;
5808   thisfn->seen_line = 0;
5809   thisfn->next = NULL;
5810
5811   if (cu->last_fn == NULL)
5812       cu->first_fn = thisfn;
5813   else
5814       cu->last_fn->next = thisfn;
5815
5816   cu->last_fn = thisfn;
5817 }
5818
5819 /* qsort helper for inherit_abstract_dies.  */
5820
5821 static int
5822 unsigned_int_compar (const void *ap, const void *bp)
5823 {
5824   unsigned int a = *(unsigned int *) ap;
5825   unsigned int b = *(unsigned int *) bp;
5826
5827   return (a > b) - (b > a);
5828 }
5829
5830 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5831    Inherit only the children of the DW_AT_abstract_origin DIE not being
5832    already referenced by DW_AT_abstract_origin from the children of the
5833    current DIE.  */
5834
5835 static void
5836 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5837 {
5838   struct die_info *child_die;
5839   unsigned die_children_count;
5840   /* CU offsets which were referenced by children of the current DIE.  */
5841   unsigned *offsets;
5842   unsigned *offsets_end, *offsetp;
5843   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5844   struct die_info *origin_die;
5845   /* Iterator of the ORIGIN_DIE children.  */
5846   struct die_info *origin_child_die;
5847   struct cleanup *cleanups;
5848   struct attribute *attr;
5849   struct dwarf2_cu *origin_cu;
5850   struct pending **origin_previous_list_in_scope;
5851
5852   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5853   if (!attr)
5854     return;
5855
5856   /* Note that following die references may follow to a die in a
5857      different cu.  */
5858
5859   origin_cu = cu;
5860   origin_die = follow_die_ref (die, attr, &origin_cu);
5861
5862   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5863      symbols in.  */
5864   origin_previous_list_in_scope = origin_cu->list_in_scope;
5865   origin_cu->list_in_scope = cu->list_in_scope;
5866
5867   if (die->tag != origin_die->tag
5868       && !(die->tag == DW_TAG_inlined_subroutine
5869            && origin_die->tag == DW_TAG_subprogram))
5870     complaint (&symfile_complaints,
5871                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5872                die->offset, origin_die->offset);
5873
5874   child_die = die->child;
5875   die_children_count = 0;
5876   while (child_die && child_die->tag)
5877     {
5878       child_die = sibling_die (child_die);
5879       die_children_count++;
5880     }
5881   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5882   cleanups = make_cleanup (xfree, offsets);
5883
5884   offsets_end = offsets;
5885   child_die = die->child;
5886   while (child_die && child_die->tag)
5887     {
5888       /* For each CHILD_DIE, find the corresponding child of
5889          ORIGIN_DIE.  If there is more than one layer of
5890          DW_AT_abstract_origin, follow them all; there shouldn't be,
5891          but GCC versions at least through 4.4 generate this (GCC PR
5892          40573).  */
5893       struct die_info *child_origin_die = child_die;
5894       struct dwarf2_cu *child_origin_cu = cu;
5895
5896       while (1)
5897         {
5898           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5899                               child_origin_cu);
5900           if (attr == NULL)
5901             break;
5902           child_origin_die = follow_die_ref (child_origin_die, attr,
5903                                              &child_origin_cu);
5904         }
5905
5906       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5907          counterpart may exist.  */
5908       if (child_origin_die != child_die)
5909         {
5910           if (child_die->tag != child_origin_die->tag
5911               && !(child_die->tag == DW_TAG_inlined_subroutine
5912                    && child_origin_die->tag == DW_TAG_subprogram))
5913             complaint (&symfile_complaints,
5914                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5915                          "different tags"), child_die->offset,
5916                        child_origin_die->offset);
5917           if (child_origin_die->parent != origin_die)
5918             complaint (&symfile_complaints,
5919                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5920                          "different parents"), child_die->offset,
5921                        child_origin_die->offset);
5922           else
5923             *offsets_end++ = child_origin_die->offset;
5924         }
5925       child_die = sibling_die (child_die);
5926     }
5927   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5928          unsigned_int_compar);
5929   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5930     if (offsetp[-1] == *offsetp)
5931       complaint (&symfile_complaints,
5932                  _("Multiple children of DIE 0x%x refer "
5933                    "to DIE 0x%x as their abstract origin"),
5934                  die->offset, *offsetp);
5935
5936   offsetp = offsets;
5937   origin_child_die = origin_die->child;
5938   while (origin_child_die && origin_child_die->tag)
5939     {
5940       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5941       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5942         offsetp++;
5943       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5944         {
5945           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5946           process_die (origin_child_die, origin_cu);
5947         }
5948       origin_child_die = sibling_die (origin_child_die);
5949     }
5950   origin_cu->list_in_scope = origin_previous_list_in_scope;
5951
5952   do_cleanups (cleanups);
5953 }
5954
5955 static void
5956 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5957 {
5958   struct objfile *objfile = cu->objfile;
5959   struct context_stack *new;
5960   CORE_ADDR lowpc;
5961   CORE_ADDR highpc;
5962   struct die_info *child_die;
5963   struct attribute *attr, *call_line, *call_file;
5964   char *name;
5965   CORE_ADDR baseaddr;
5966   struct block *block;
5967   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5968   VEC (symbolp) *template_args = NULL;
5969   struct template_symbol *templ_func = NULL;
5970
5971   if (inlined_func)
5972     {
5973       /* If we do not have call site information, we can't show the
5974          caller of this inlined function.  That's too confusing, so
5975          only use the scope for local variables.  */
5976       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5977       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5978       if (call_line == NULL || call_file == NULL)
5979         {
5980           read_lexical_block_scope (die, cu);
5981           return;
5982         }
5983     }
5984
5985   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5986
5987   name = dwarf2_name (die, cu);
5988
5989   /* Ignore functions with missing or empty names.  These are actually
5990      illegal according to the DWARF standard.  */
5991   if (name == NULL)
5992     {
5993       complaint (&symfile_complaints,
5994                  _("missing name for subprogram DIE at %d"), die->offset);
5995       return;
5996     }
5997
5998   /* Ignore functions with missing or invalid low and high pc attributes.  */
5999   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6000     {
6001       attr = dwarf2_attr (die, DW_AT_external, cu);
6002       if (!attr || !DW_UNSND (attr))
6003         complaint (&symfile_complaints,
6004                    _("cannot get low and high bounds "
6005                      "for subprogram DIE at %d"),
6006                    die->offset);
6007       return;
6008     }
6009
6010   lowpc += baseaddr;
6011   highpc += baseaddr;
6012
6013   /* Record the function range for dwarf_decode_lines.  */
6014   add_to_cu_func_list (name, lowpc, highpc, cu);
6015
6016   /* If we have any template arguments, then we must allocate a
6017      different sort of symbol.  */
6018   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6019     {
6020       if (child_die->tag == DW_TAG_template_type_param
6021           || child_die->tag == DW_TAG_template_value_param)
6022         {
6023           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6024                                        struct template_symbol);
6025           templ_func->base.is_cplus_template_function = 1;
6026           break;
6027         }
6028     }
6029
6030   new = push_context (0, lowpc);
6031   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6032                                (struct symbol *) templ_func);
6033
6034   /* If there is a location expression for DW_AT_frame_base, record
6035      it.  */
6036   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6037   if (attr)
6038     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6039        expression is being recorded directly in the function's symbol
6040        and not in a separate frame-base object.  I guess this hack is
6041        to avoid adding some sort of frame-base adjunct/annex to the
6042        function's symbol :-(.  The problem with doing this is that it
6043        results in a function symbol with a location expression that
6044        has nothing to do with the location of the function, ouch!  The
6045        relationship should be: a function's symbol has-a frame base; a
6046        frame-base has-a location expression.  */
6047     dwarf2_symbol_mark_computed (attr, new->name, cu);
6048
6049   cu->list_in_scope = &local_symbols;
6050
6051   if (die->child != NULL)
6052     {
6053       child_die = die->child;
6054       while (child_die && child_die->tag)
6055         {
6056           if (child_die->tag == DW_TAG_template_type_param
6057               || child_die->tag == DW_TAG_template_value_param)
6058             {
6059               struct symbol *arg = new_symbol (child_die, NULL, cu);
6060
6061               if (arg != NULL)
6062                 VEC_safe_push (symbolp, template_args, arg);
6063             }
6064           else
6065             process_die (child_die, cu);
6066           child_die = sibling_die (child_die);
6067         }
6068     }
6069
6070   inherit_abstract_dies (die, cu);
6071
6072   /* If we have a DW_AT_specification, we might need to import using
6073      directives from the context of the specification DIE.  See the
6074      comment in determine_prefix.  */
6075   if (cu->language == language_cplus
6076       && dwarf2_attr (die, DW_AT_specification, cu))
6077     {
6078       struct dwarf2_cu *spec_cu = cu;
6079       struct die_info *spec_die = die_specification (die, &spec_cu);
6080
6081       while (spec_die)
6082         {
6083           child_die = spec_die->child;
6084           while (child_die && child_die->tag)
6085             {
6086               if (child_die->tag == DW_TAG_imported_module)
6087                 process_die (child_die, spec_cu);
6088               child_die = sibling_die (child_die);
6089             }
6090
6091           /* In some cases, GCC generates specification DIEs that
6092              themselves contain DW_AT_specification attributes.  */
6093           spec_die = die_specification (spec_die, &spec_cu);
6094         }
6095     }
6096
6097   new = pop_context ();
6098   /* Make a block for the local symbols within.  */
6099   block = finish_block (new->name, &local_symbols, new->old_blocks,
6100                         lowpc, highpc, objfile);
6101
6102   /* For C++, set the block's scope.  */
6103   if (cu->language == language_cplus || cu->language == language_fortran)
6104     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6105                         determine_prefix (die, cu),
6106                         processing_has_namespace_info);
6107
6108   /* If we have address ranges, record them.  */
6109   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6110
6111   /* Attach template arguments to function.  */
6112   if (! VEC_empty (symbolp, template_args))
6113     {
6114       gdb_assert (templ_func != NULL);
6115
6116       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6117       templ_func->template_arguments
6118         = obstack_alloc (&objfile->objfile_obstack,
6119                          (templ_func->n_template_arguments
6120                           * sizeof (struct symbol *)));
6121       memcpy (templ_func->template_arguments,
6122               VEC_address (symbolp, template_args),
6123               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6124       VEC_free (symbolp, template_args);
6125     }
6126
6127   /* In C++, we can have functions nested inside functions (e.g., when
6128      a function declares a class that has methods).  This means that
6129      when we finish processing a function scope, we may need to go
6130      back to building a containing block's symbol lists.  */
6131   local_symbols = new->locals;
6132   param_symbols = new->params;
6133   using_directives = new->using_directives;
6134
6135   /* If we've finished processing a top-level function, subsequent
6136      symbols go in the file symbol list.  */
6137   if (outermost_context_p ())
6138     cu->list_in_scope = &file_symbols;
6139 }
6140
6141 /* Process all the DIES contained within a lexical block scope.  Start
6142    a new scope, process the dies, and then close the scope.  */
6143
6144 static void
6145 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6146 {
6147   struct objfile *objfile = cu->objfile;
6148   struct context_stack *new;
6149   CORE_ADDR lowpc, highpc;
6150   struct die_info *child_die;
6151   CORE_ADDR baseaddr;
6152
6153   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6154
6155   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6156   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6157      as multiple lexical blocks?  Handling children in a sane way would
6158      be nasty.  Might be easier to properly extend generic blocks to
6159      describe ranges.  */
6160   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6161     return;
6162   lowpc += baseaddr;
6163   highpc += baseaddr;
6164
6165   push_context (0, lowpc);
6166   if (die->child != NULL)
6167     {
6168       child_die = die->child;
6169       while (child_die && child_die->tag)
6170         {
6171           process_die (child_die, cu);
6172           child_die = sibling_die (child_die);
6173         }
6174     }
6175   new = pop_context ();
6176
6177   if (local_symbols != NULL || using_directives != NULL)
6178     {
6179       struct block *block
6180         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6181                         highpc, objfile);
6182
6183       /* Note that recording ranges after traversing children, as we
6184          do here, means that recording a parent's ranges entails
6185          walking across all its children's ranges as they appear in
6186          the address map, which is quadratic behavior.
6187
6188          It would be nicer to record the parent's ranges before
6189          traversing its children, simply overriding whatever you find
6190          there.  But since we don't even decide whether to create a
6191          block until after we've traversed its children, that's hard
6192          to do.  */
6193       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6194     }
6195   local_symbols = new->locals;
6196   using_directives = new->using_directives;
6197 }
6198
6199 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6200
6201 static void
6202 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6203 {
6204   struct objfile *objfile = cu->objfile;
6205   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6206   CORE_ADDR pc, baseaddr;
6207   struct attribute *attr;
6208   struct call_site *call_site, call_site_local;
6209   void **slot;
6210   int nparams;
6211   struct die_info *child_die;
6212
6213   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6214
6215   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6216   if (!attr)
6217     {
6218       complaint (&symfile_complaints,
6219                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6220                    "DIE 0x%x [in module %s]"),
6221                  die->offset, cu->objfile->name);
6222       return;
6223     }
6224   pc = DW_ADDR (attr) + baseaddr;
6225
6226   if (cu->call_site_htab == NULL)
6227     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6228                                                NULL, &objfile->objfile_obstack,
6229                                                hashtab_obstack_allocate, NULL);
6230   call_site_local.pc = pc;
6231   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6232   if (*slot != NULL)
6233     {
6234       complaint (&symfile_complaints,
6235                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6236                    "DIE 0x%x [in module %s]"),
6237                  paddress (gdbarch, pc), die->offset, cu->objfile->name);
6238       return;
6239     }
6240
6241   /* Count parameters at the caller.  */
6242
6243   nparams = 0;
6244   for (child_die = die->child; child_die && child_die->tag;
6245        child_die = sibling_die (child_die))
6246     {
6247       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6248         {
6249           complaint (&symfile_complaints,
6250                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6251                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6252                      child_die->tag, child_die->offset, cu->objfile->name);
6253           continue;
6254         }
6255
6256       nparams++;
6257     }
6258
6259   call_site = obstack_alloc (&objfile->objfile_obstack,
6260                              (sizeof (*call_site)
6261                               + (sizeof (*call_site->parameter)
6262                                  * (nparams - 1))));
6263   *slot = call_site;
6264   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6265   call_site->pc = pc;
6266
6267   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6268     {
6269       struct die_info *func_die;
6270
6271       /* Skip also over DW_TAG_inlined_subroutine.  */
6272       for (func_die = die->parent;
6273            func_die && func_die->tag != DW_TAG_subprogram
6274            && func_die->tag != DW_TAG_subroutine_type;
6275            func_die = func_die->parent);
6276
6277       /* DW_AT_GNU_all_call_sites is a superset
6278          of DW_AT_GNU_all_tail_call_sites.  */
6279       if (func_die
6280           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6281           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6282         {
6283           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6284              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6285              both the initial caller containing the real return address PC and
6286              the final callee containing the current PC of a chain of tail
6287              calls do not need to have the tail call list complete.  But any
6288              function candidate for a virtual tail call frame searched via
6289              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6290              determined unambiguously.  */
6291         }
6292       else
6293         {
6294           struct type *func_type = NULL;
6295
6296           if (func_die)
6297             func_type = get_die_type (func_die, cu);
6298           if (func_type != NULL)
6299             {
6300               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6301
6302               /* Enlist this call site to the function.  */
6303               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6304               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6305             }
6306           else
6307             complaint (&symfile_complaints,
6308                        _("Cannot find function owning DW_TAG_GNU_call_site "
6309                          "DIE 0x%x [in module %s]"),
6310                        die->offset, cu->objfile->name);
6311         }
6312     }
6313
6314   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6315   if (attr == NULL)
6316     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6317   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6318   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6319     /* Keep NULL DWARF_BLOCK.  */;
6320   else if (attr_form_is_block (attr))
6321     {
6322       struct dwarf2_locexpr_baton *dlbaton;
6323
6324       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6325       dlbaton->data = DW_BLOCK (attr)->data;
6326       dlbaton->size = DW_BLOCK (attr)->size;
6327       dlbaton->per_cu = cu->per_cu;
6328
6329       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6330     }
6331   else if (is_ref_attr (attr))
6332     {
6333       struct objfile *objfile = cu->objfile;
6334       struct dwarf2_cu *target_cu = cu;
6335       struct die_info *target_die;
6336
6337       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6338       gdb_assert (target_cu->objfile == objfile);
6339       if (die_is_declaration (target_die, target_cu))
6340         {
6341           const char *target_physname;
6342
6343           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6344           if (target_physname == NULL)
6345             complaint (&symfile_complaints,
6346                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6347                          "physname, for referencing DIE 0x%x [in module %s]"),
6348                        die->offset, cu->objfile->name);
6349           else
6350             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6351         }
6352       else
6353         {
6354           CORE_ADDR lowpc;
6355
6356           /* DW_AT_entry_pc should be preferred.  */
6357           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6358             complaint (&symfile_complaints,
6359                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6360                          "low pc, for referencing DIE 0x%x [in module %s]"),
6361                        die->offset, cu->objfile->name);
6362           else
6363             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6364         }
6365     }
6366   else
6367     complaint (&symfile_complaints,
6368                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6369                  "block nor reference, for DIE 0x%x [in module %s]"),
6370                die->offset, cu->objfile->name);
6371
6372   call_site->per_cu = cu->per_cu;
6373
6374   for (child_die = die->child;
6375        child_die && child_die->tag;
6376        child_die = sibling_die (child_die))
6377     {
6378       struct dwarf2_locexpr_baton *dlbaton;
6379       struct call_site_parameter *parameter;
6380
6381       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6382         {
6383           /* Already printed the complaint above.  */
6384           continue;
6385         }
6386
6387       gdb_assert (call_site->parameter_count < nparams);
6388       parameter = &call_site->parameter[call_site->parameter_count];
6389
6390       /* DW_AT_location specifies the register number.  Value of the data
6391          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6392
6393       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6394       if (!attr || !attr_form_is_block (attr))
6395         {
6396           complaint (&symfile_complaints,
6397                      _("No DW_FORM_block* DW_AT_location for "
6398                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6399                      child_die->offset, cu->objfile->name);
6400           continue;
6401         }
6402       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6403                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6404       if (parameter->dwarf_reg == -1
6405           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6406                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6407                                         &parameter->fb_offset))
6408         {
6409           complaint (&symfile_complaints,
6410                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6411                        "for DW_FORM_block* DW_AT_location for "
6412                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6413                      child_die->offset, cu->objfile->name);
6414           continue;
6415         }
6416
6417       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6418       if (!attr_form_is_block (attr))
6419         {
6420           complaint (&symfile_complaints,
6421                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6422                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6423                      child_die->offset, cu->objfile->name);
6424           continue;
6425         }
6426       parameter->value = DW_BLOCK (attr)->data;
6427       parameter->value_size = DW_BLOCK (attr)->size;
6428
6429       /* Parameters are not pre-cleared by memset above.  */
6430       parameter->data_value = NULL;
6431       parameter->data_value_size = 0;
6432       call_site->parameter_count++;
6433
6434       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6435       if (attr)
6436         {
6437           if (!attr_form_is_block (attr))
6438             complaint (&symfile_complaints,
6439                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6440                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6441                        child_die->offset, cu->objfile->name);
6442           else
6443             {
6444               parameter->data_value = DW_BLOCK (attr)->data;
6445               parameter->data_value_size = DW_BLOCK (attr)->size;
6446             }
6447         }
6448     }
6449 }
6450
6451 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6452    Return 1 if the attributes are present and valid, otherwise, return 0.
6453    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6454
6455 static int
6456 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6457                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6458                     struct partial_symtab *ranges_pst)
6459 {
6460   struct objfile *objfile = cu->objfile;
6461   struct comp_unit_head *cu_header = &cu->header;
6462   bfd *obfd = objfile->obfd;
6463   unsigned int addr_size = cu_header->addr_size;
6464   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6465   /* Base address selection entry.  */
6466   CORE_ADDR base;
6467   int found_base;
6468   unsigned int dummy;
6469   gdb_byte *buffer;
6470   CORE_ADDR marker;
6471   int low_set;
6472   CORE_ADDR low = 0;
6473   CORE_ADDR high = 0;
6474   CORE_ADDR baseaddr;
6475
6476   found_base = cu->base_known;
6477   base = cu->base_address;
6478
6479   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6480   if (offset >= dwarf2_per_objfile->ranges.size)
6481     {
6482       complaint (&symfile_complaints,
6483                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6484                  offset);
6485       return 0;
6486     }
6487   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6488
6489   /* Read in the largest possible address.  */
6490   marker = read_address (obfd, buffer, cu, &dummy);
6491   if ((marker & mask) == mask)
6492     {
6493       /* If we found the largest possible address, then
6494          read the base address.  */
6495       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6496       buffer += 2 * addr_size;
6497       offset += 2 * addr_size;
6498       found_base = 1;
6499     }
6500
6501   low_set = 0;
6502
6503   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6504
6505   while (1)
6506     {
6507       CORE_ADDR range_beginning, range_end;
6508
6509       range_beginning = read_address (obfd, buffer, cu, &dummy);
6510       buffer += addr_size;
6511       range_end = read_address (obfd, buffer, cu, &dummy);
6512       buffer += addr_size;
6513       offset += 2 * addr_size;
6514
6515       /* An end of list marker is a pair of zero addresses.  */
6516       if (range_beginning == 0 && range_end == 0)
6517         /* Found the end of list entry.  */
6518         break;
6519
6520       /* Each base address selection entry is a pair of 2 values.
6521          The first is the largest possible address, the second is
6522          the base address.  Check for a base address here.  */
6523       if ((range_beginning & mask) == mask)
6524         {
6525           /* If we found the largest possible address, then
6526              read the base address.  */
6527           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6528           found_base = 1;
6529           continue;
6530         }
6531
6532       if (!found_base)
6533         {
6534           /* We have no valid base address for the ranges
6535              data.  */
6536           complaint (&symfile_complaints,
6537                      _("Invalid .debug_ranges data (no base address)"));
6538           return 0;
6539         }
6540
6541       if (range_beginning > range_end)
6542         {
6543           /* Inverted range entries are invalid.  */
6544           complaint (&symfile_complaints,
6545                      _("Invalid .debug_ranges data (inverted range)"));
6546           return 0;
6547         }
6548
6549       /* Empty range entries have no effect.  */
6550       if (range_beginning == range_end)
6551         continue;
6552
6553       range_beginning += base;
6554       range_end += base;
6555
6556       if (ranges_pst != NULL)
6557         addrmap_set_empty (objfile->psymtabs_addrmap,
6558                            range_beginning + baseaddr,
6559                            range_end - 1 + baseaddr,
6560                            ranges_pst);
6561
6562       /* FIXME: This is recording everything as a low-high
6563          segment of consecutive addresses.  We should have a
6564          data structure for discontiguous block ranges
6565          instead.  */
6566       if (! low_set)
6567         {
6568           low = range_beginning;
6569           high = range_end;
6570           low_set = 1;
6571         }
6572       else
6573         {
6574           if (range_beginning < low)
6575             low = range_beginning;
6576           if (range_end > high)
6577             high = range_end;
6578         }
6579     }
6580
6581   if (! low_set)
6582     /* If the first entry is an end-of-list marker, the range
6583        describes an empty scope, i.e. no instructions.  */
6584     return 0;
6585
6586   if (low_return)
6587     *low_return = low;
6588   if (high_return)
6589     *high_return = high;
6590   return 1;
6591 }
6592
6593 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6594    are present and valid, otherwise, return 0.  Return -1 if the range is
6595    discontinuous, i.e. derived from DW_AT_ranges information.  */
6596 static int
6597 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6598                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6599                       struct partial_symtab *pst)
6600 {
6601   struct attribute *attr;
6602   CORE_ADDR low = 0;
6603   CORE_ADDR high = 0;
6604   int ret = 0;
6605
6606   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6607   if (attr)
6608     {
6609       high = DW_ADDR (attr);
6610       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6611       if (attr)
6612         low = DW_ADDR (attr);
6613       else
6614         /* Found high w/o low attribute.  */
6615         return 0;
6616
6617       /* Found consecutive range of addresses.  */
6618       ret = 1;
6619     }
6620   else
6621     {
6622       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6623       if (attr != NULL)
6624         {
6625           /* Value of the DW_AT_ranges attribute is the offset in the
6626              .debug_ranges section.  */
6627           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6628             return 0;
6629           /* Found discontinuous range of addresses.  */
6630           ret = -1;
6631         }
6632     }
6633
6634   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6635   if (high <= low)
6636     return 0;
6637
6638   /* When using the GNU linker, .gnu.linkonce. sections are used to
6639      eliminate duplicate copies of functions and vtables and such.
6640      The linker will arbitrarily choose one and discard the others.
6641      The AT_*_pc values for such functions refer to local labels in
6642      these sections.  If the section from that file was discarded, the
6643      labels are not in the output, so the relocs get a value of 0.
6644      If this is a discarded function, mark the pc bounds as invalid,
6645      so that GDB will ignore it.  */
6646   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6647     return 0;
6648
6649   *lowpc = low;
6650   if (highpc)
6651     *highpc = high;
6652   return ret;
6653 }
6654
6655 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6656    its low and high PC addresses.  Do nothing if these addresses could not
6657    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6658    and HIGHPC to the high address if greater than HIGHPC.  */
6659
6660 static void
6661 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6662                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6663                                  struct dwarf2_cu *cu)
6664 {
6665   CORE_ADDR low, high;
6666   struct die_info *child = die->child;
6667
6668   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6669     {
6670       *lowpc = min (*lowpc, low);
6671       *highpc = max (*highpc, high);
6672     }
6673
6674   /* If the language does not allow nested subprograms (either inside
6675      subprograms or lexical blocks), we're done.  */
6676   if (cu->language != language_ada)
6677     return;
6678
6679   /* Check all the children of the given DIE.  If it contains nested
6680      subprograms, then check their pc bounds.  Likewise, we need to
6681      check lexical blocks as well, as they may also contain subprogram
6682      definitions.  */
6683   while (child && child->tag)
6684     {
6685       if (child->tag == DW_TAG_subprogram
6686           || child->tag == DW_TAG_lexical_block)
6687         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6688       child = sibling_die (child);
6689     }
6690 }
6691
6692 /* Get the low and high pc's represented by the scope DIE, and store
6693    them in *LOWPC and *HIGHPC.  If the correct values can't be
6694    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6695
6696 static void
6697 get_scope_pc_bounds (struct die_info *die,
6698                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6699                      struct dwarf2_cu *cu)
6700 {
6701   CORE_ADDR best_low = (CORE_ADDR) -1;
6702   CORE_ADDR best_high = (CORE_ADDR) 0;
6703   CORE_ADDR current_low, current_high;
6704
6705   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6706     {
6707       best_low = current_low;
6708       best_high = current_high;
6709     }
6710   else
6711     {
6712       struct die_info *child = die->child;
6713
6714       while (child && child->tag)
6715         {
6716           switch (child->tag) {
6717           case DW_TAG_subprogram:
6718             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6719             break;
6720           case DW_TAG_namespace:
6721           case DW_TAG_module:
6722             /* FIXME: carlton/2004-01-16: Should we do this for
6723                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6724                that current GCC's always emit the DIEs corresponding
6725                to definitions of methods of classes as children of a
6726                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6727                the DIEs giving the declarations, which could be
6728                anywhere).  But I don't see any reason why the
6729                standards says that they have to be there.  */
6730             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6731
6732             if (current_low != ((CORE_ADDR) -1))
6733               {
6734                 best_low = min (best_low, current_low);
6735                 best_high = max (best_high, current_high);
6736               }
6737             break;
6738           default:
6739             /* Ignore.  */
6740             break;
6741           }
6742
6743           child = sibling_die (child);
6744         }
6745     }
6746
6747   *lowpc = best_low;
6748   *highpc = best_high;
6749 }
6750
6751 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6752    in DIE.  */
6753 static void
6754 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6755                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6756 {
6757   struct attribute *attr;
6758
6759   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6760   if (attr)
6761     {
6762       CORE_ADDR high = DW_ADDR (attr);
6763
6764       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6765       if (attr)
6766         {
6767           CORE_ADDR low = DW_ADDR (attr);
6768
6769           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6770         }
6771     }
6772
6773   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6774   if (attr)
6775     {
6776       bfd *obfd = cu->objfile->obfd;
6777
6778       /* The value of the DW_AT_ranges attribute is the offset of the
6779          address range list in the .debug_ranges section.  */
6780       unsigned long offset = DW_UNSND (attr);
6781       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6782
6783       /* For some target architectures, but not others, the
6784          read_address function sign-extends the addresses it returns.
6785          To recognize base address selection entries, we need a
6786          mask.  */
6787       unsigned int addr_size = cu->header.addr_size;
6788       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6789
6790       /* The base address, to which the next pair is relative.  Note
6791          that this 'base' is a DWARF concept: most entries in a range
6792          list are relative, to reduce the number of relocs against the
6793          debugging information.  This is separate from this function's
6794          'baseaddr' argument, which GDB uses to relocate debugging
6795          information from a shared library based on the address at
6796          which the library was loaded.  */
6797       CORE_ADDR base = cu->base_address;
6798       int base_known = cu->base_known;
6799
6800       gdb_assert (dwarf2_per_objfile->ranges.readin);
6801       if (offset >= dwarf2_per_objfile->ranges.size)
6802         {
6803           complaint (&symfile_complaints,
6804                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6805                      offset);
6806           return;
6807         }
6808
6809       for (;;)
6810         {
6811           unsigned int bytes_read;
6812           CORE_ADDR start, end;
6813
6814           start = read_address (obfd, buffer, cu, &bytes_read);
6815           buffer += bytes_read;
6816           end = read_address (obfd, buffer, cu, &bytes_read);
6817           buffer += bytes_read;
6818
6819           /* Did we find the end of the range list?  */
6820           if (start == 0 && end == 0)
6821             break;
6822
6823           /* Did we find a base address selection entry?  */
6824           else if ((start & base_select_mask) == base_select_mask)
6825             {
6826               base = end;
6827               base_known = 1;
6828             }
6829
6830           /* We found an ordinary address range.  */
6831           else
6832             {
6833               if (!base_known)
6834                 {
6835                   complaint (&symfile_complaints,
6836                              _("Invalid .debug_ranges data "
6837                                "(no base address)"));
6838                   return;
6839                 }
6840
6841               if (start > end)
6842                 {
6843                   /* Inverted range entries are invalid.  */
6844                   complaint (&symfile_complaints,
6845                              _("Invalid .debug_ranges data "
6846                                "(inverted range)"));
6847                   return;
6848                 }
6849
6850               /* Empty range entries have no effect.  */
6851               if (start == end)
6852                 continue;
6853
6854               record_block_range (block,
6855                                   baseaddr + base + start,
6856                                   baseaddr + base + end - 1);
6857             }
6858         }
6859     }
6860 }
6861
6862 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6863    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6864    during 4.6.0 experimental.  */
6865
6866 static int
6867 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6868 {
6869   const char *cs;
6870   int major, minor, release;
6871
6872   if (cu->producer == NULL)
6873     {
6874       /* For unknown compilers expect their behavior is DWARF version
6875          compliant.
6876
6877          GCC started to support .debug_types sections by -gdwarf-4 since
6878          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6879          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6880          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6881          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6882
6883       return 0;
6884     }
6885
6886   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6887
6888   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6889     {
6890       /* For non-GCC compilers expect their behavior is DWARF version
6891          compliant.  */
6892
6893       return 0;
6894     }
6895   cs = &cu->producer[strlen ("GNU ")];
6896   while (*cs && !isdigit (*cs))
6897     cs++;
6898   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6899     {
6900       /* Not recognized as GCC.  */
6901
6902       return 0;
6903     }
6904
6905   return major < 4 || (major == 4 && minor < 6);
6906 }
6907
6908 /* Return the default accessibility type if it is not overriden by
6909    DW_AT_accessibility.  */
6910
6911 static enum dwarf_access_attribute
6912 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6913 {
6914   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6915     {
6916       /* The default DWARF 2 accessibility for members is public, the default
6917          accessibility for inheritance is private.  */
6918
6919       if (die->tag != DW_TAG_inheritance)
6920         return DW_ACCESS_public;
6921       else
6922         return DW_ACCESS_private;
6923     }
6924   else
6925     {
6926       /* DWARF 3+ defines the default accessibility a different way.  The same
6927          rules apply now for DW_TAG_inheritance as for the members and it only
6928          depends on the container kind.  */
6929
6930       if (die->parent->tag == DW_TAG_class_type)
6931         return DW_ACCESS_private;
6932       else
6933         return DW_ACCESS_public;
6934     }
6935 }
6936
6937 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6938    offset.  If the attribute was not found return 0, otherwise return
6939    1.  If it was found but could not properly be handled, set *OFFSET
6940    to 0.  */
6941
6942 static int
6943 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6944                              LONGEST *offset)
6945 {
6946   struct attribute *attr;
6947
6948   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6949   if (attr != NULL)
6950     {
6951       *offset = 0;
6952
6953       /* Note that we do not check for a section offset first here.
6954          This is because DW_AT_data_member_location is new in DWARF 4,
6955          so if we see it, we can assume that a constant form is really
6956          a constant and not a section offset.  */
6957       if (attr_form_is_constant (attr))
6958         *offset = dwarf2_get_attr_constant_value (attr, 0);
6959       else if (attr_form_is_section_offset (attr))
6960         dwarf2_complex_location_expr_complaint ();
6961       else if (attr_form_is_block (attr))
6962         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6963       else
6964         dwarf2_complex_location_expr_complaint ();
6965
6966       return 1;
6967     }
6968
6969   return 0;
6970 }
6971
6972 /* Add an aggregate field to the field list.  */
6973
6974 static void
6975 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6976                   struct dwarf2_cu *cu)
6977 {
6978   struct objfile *objfile = cu->objfile;
6979   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6980   struct nextfield *new_field;
6981   struct attribute *attr;
6982   struct field *fp;
6983   char *fieldname = "";
6984
6985   /* Allocate a new field list entry and link it in.  */
6986   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6987   make_cleanup (xfree, new_field);
6988   memset (new_field, 0, sizeof (struct nextfield));
6989
6990   if (die->tag == DW_TAG_inheritance)
6991     {
6992       new_field->next = fip->baseclasses;
6993       fip->baseclasses = new_field;
6994     }
6995   else
6996     {
6997       new_field->next = fip->fields;
6998       fip->fields = new_field;
6999     }
7000   fip->nfields++;
7001
7002   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7003   if (attr)
7004     new_field->accessibility = DW_UNSND (attr);
7005   else
7006     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7007   if (new_field->accessibility != DW_ACCESS_public)
7008     fip->non_public_fields = 1;
7009
7010   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7011   if (attr)
7012     new_field->virtuality = DW_UNSND (attr);
7013   else
7014     new_field->virtuality = DW_VIRTUALITY_none;
7015
7016   fp = &new_field->field;
7017
7018   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7019     {
7020       LONGEST offset;
7021
7022       /* Data member other than a C++ static data member.  */
7023
7024       /* Get type of field.  */
7025       fp->type = die_type (die, cu);
7026
7027       SET_FIELD_BITPOS (*fp, 0);
7028
7029       /* Get bit size of field (zero if none).  */
7030       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7031       if (attr)
7032         {
7033           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7034         }
7035       else
7036         {
7037           FIELD_BITSIZE (*fp) = 0;
7038         }
7039
7040       /* Get bit offset of field.  */
7041       if (handle_data_member_location (die, cu, &offset))
7042         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7043       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7044       if (attr)
7045         {
7046           if (gdbarch_bits_big_endian (gdbarch))
7047             {
7048               /* For big endian bits, the DW_AT_bit_offset gives the
7049                  additional bit offset from the MSB of the containing
7050                  anonymous object to the MSB of the field.  We don't
7051                  have to do anything special since we don't need to
7052                  know the size of the anonymous object.  */
7053               FIELD_BITPOS (*fp) += DW_UNSND (attr);
7054             }
7055           else
7056             {
7057               /* For little endian bits, compute the bit offset to the
7058                  MSB of the anonymous object, subtract off the number of
7059                  bits from the MSB of the field to the MSB of the
7060                  object, and then subtract off the number of bits of
7061                  the field itself.  The result is the bit offset of
7062                  the LSB of the field.  */
7063               int anonymous_size;
7064               int bit_offset = DW_UNSND (attr);
7065
7066               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7067               if (attr)
7068                 {
7069                   /* The size of the anonymous object containing
7070                      the bit field is explicit, so use the
7071                      indicated size (in bytes).  */
7072                   anonymous_size = DW_UNSND (attr);
7073                 }
7074               else
7075                 {
7076                   /* The size of the anonymous object containing
7077                      the bit field must be inferred from the type
7078                      attribute of the data member containing the
7079                      bit field.  */
7080                   anonymous_size = TYPE_LENGTH (fp->type);
7081                 }
7082               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7083                 - bit_offset - FIELD_BITSIZE (*fp);
7084             }
7085         }
7086
7087       /* Get name of field.  */
7088       fieldname = dwarf2_name (die, cu);
7089       if (fieldname == NULL)
7090         fieldname = "";
7091
7092       /* The name is already allocated along with this objfile, so we don't
7093          need to duplicate it for the type.  */
7094       fp->name = fieldname;
7095
7096       /* Change accessibility for artificial fields (e.g. virtual table
7097          pointer or virtual base class pointer) to private.  */
7098       if (dwarf2_attr (die, DW_AT_artificial, cu))
7099         {
7100           FIELD_ARTIFICIAL (*fp) = 1;
7101           new_field->accessibility = DW_ACCESS_private;
7102           fip->non_public_fields = 1;
7103         }
7104     }
7105   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7106     {
7107       /* C++ static member.  */
7108
7109       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7110          is a declaration, but all versions of G++ as of this writing
7111          (so through at least 3.2.1) incorrectly generate
7112          DW_TAG_variable tags.  */
7113
7114       const char *physname;
7115
7116       /* Get name of field.  */
7117       fieldname = dwarf2_name (die, cu);
7118       if (fieldname == NULL)
7119         return;
7120
7121       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7122       if (attr
7123           /* Only create a symbol if this is an external value.
7124              new_symbol checks this and puts the value in the global symbol
7125              table, which we want.  If it is not external, new_symbol
7126              will try to put the value in cu->list_in_scope which is wrong.  */
7127           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7128         {
7129           /* A static const member, not much different than an enum as far as
7130              we're concerned, except that we can support more types.  */
7131           new_symbol (die, NULL, cu);
7132         }
7133
7134       /* Get physical name.  */
7135       physname = dwarf2_physname (fieldname, die, cu);
7136
7137       /* The name is already allocated along with this objfile, so we don't
7138          need to duplicate it for the type.  */
7139       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7140       FIELD_TYPE (*fp) = die_type (die, cu);
7141       FIELD_NAME (*fp) = fieldname;
7142     }
7143   else if (die->tag == DW_TAG_inheritance)
7144     {
7145       LONGEST offset;
7146
7147       /* C++ base class field.  */
7148       if (handle_data_member_location (die, cu, &offset))
7149         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7150       FIELD_BITSIZE (*fp) = 0;
7151       FIELD_TYPE (*fp) = die_type (die, cu);
7152       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7153       fip->nbaseclasses++;
7154     }
7155 }
7156
7157 /* Add a typedef defined in the scope of the FIP's class.  */
7158
7159 static void
7160 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7161                     struct dwarf2_cu *cu)
7162 {
7163   struct objfile *objfile = cu->objfile;
7164   struct typedef_field_list *new_field;
7165   struct attribute *attr;
7166   struct typedef_field *fp;
7167   char *fieldname = "";
7168
7169   /* Allocate a new field list entry and link it in.  */
7170   new_field = xzalloc (sizeof (*new_field));
7171   make_cleanup (xfree, new_field);
7172
7173   gdb_assert (die->tag == DW_TAG_typedef);
7174
7175   fp = &new_field->field;
7176
7177   /* Get name of field.  */
7178   fp->name = dwarf2_name (die, cu);
7179   if (fp->name == NULL)
7180     return;
7181
7182   fp->type = read_type_die (die, cu);
7183
7184   new_field->next = fip->typedef_field_list;
7185   fip->typedef_field_list = new_field;
7186   fip->typedef_field_list_count++;
7187 }
7188
7189 /* Create the vector of fields, and attach it to the type.  */
7190
7191 static void
7192 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7193                               struct dwarf2_cu *cu)
7194 {
7195   int nfields = fip->nfields;
7196
7197   /* Record the field count, allocate space for the array of fields,
7198      and create blank accessibility bitfields if necessary.  */
7199   TYPE_NFIELDS (type) = nfields;
7200   TYPE_FIELDS (type) = (struct field *)
7201     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7202   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7203
7204   if (fip->non_public_fields && cu->language != language_ada)
7205     {
7206       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7207
7208       TYPE_FIELD_PRIVATE_BITS (type) =
7209         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7210       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7211
7212       TYPE_FIELD_PROTECTED_BITS (type) =
7213         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7214       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7215
7216       TYPE_FIELD_IGNORE_BITS (type) =
7217         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7218       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7219     }
7220
7221   /* If the type has baseclasses, allocate and clear a bit vector for
7222      TYPE_FIELD_VIRTUAL_BITS.  */
7223   if (fip->nbaseclasses && cu->language != language_ada)
7224     {
7225       int num_bytes = B_BYTES (fip->nbaseclasses);
7226       unsigned char *pointer;
7227
7228       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7229       pointer = TYPE_ALLOC (type, num_bytes);
7230       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7231       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7232       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7233     }
7234
7235   /* Copy the saved-up fields into the field vector.  Start from the head of
7236      the list, adding to the tail of the field array, so that they end up in
7237      the same order in the array in which they were added to the list.  */
7238   while (nfields-- > 0)
7239     {
7240       struct nextfield *fieldp;
7241
7242       if (fip->fields)
7243         {
7244           fieldp = fip->fields;
7245           fip->fields = fieldp->next;
7246         }
7247       else
7248         {
7249           fieldp = fip->baseclasses;
7250           fip->baseclasses = fieldp->next;
7251         }
7252
7253       TYPE_FIELD (type, nfields) = fieldp->field;
7254       switch (fieldp->accessibility)
7255         {
7256         case DW_ACCESS_private:
7257           if (cu->language != language_ada)
7258             SET_TYPE_FIELD_PRIVATE (type, nfields);
7259           break;
7260
7261         case DW_ACCESS_protected:
7262           if (cu->language != language_ada)
7263             SET_TYPE_FIELD_PROTECTED (type, nfields);
7264           break;
7265
7266         case DW_ACCESS_public:
7267           break;
7268
7269         default:
7270           /* Unknown accessibility.  Complain and treat it as public.  */
7271           {
7272             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7273                        fieldp->accessibility);
7274           }
7275           break;
7276         }
7277       if (nfields < fip->nbaseclasses)
7278         {
7279           switch (fieldp->virtuality)
7280             {
7281             case DW_VIRTUALITY_virtual:
7282             case DW_VIRTUALITY_pure_virtual:
7283               if (cu->language == language_ada)
7284                 error (_("unexpected virtuality in component of Ada type"));
7285               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7286               break;
7287             }
7288         }
7289     }
7290 }
7291
7292 /* Add a member function to the proper fieldlist.  */
7293
7294 static void
7295 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7296                       struct type *type, struct dwarf2_cu *cu)
7297 {
7298   struct objfile *objfile = cu->objfile;
7299   struct attribute *attr;
7300   struct fnfieldlist *flp;
7301   int i;
7302   struct fn_field *fnp;
7303   char *fieldname;
7304   struct nextfnfield *new_fnfield;
7305   struct type *this_type;
7306   enum dwarf_access_attribute accessibility;
7307
7308   if (cu->language == language_ada)
7309     error (_("unexpected member function in Ada type"));
7310
7311   /* Get name of member function.  */
7312   fieldname = dwarf2_name (die, cu);
7313   if (fieldname == NULL)
7314     return;
7315
7316   /* Look up member function name in fieldlist.  */
7317   for (i = 0; i < fip->nfnfields; i++)
7318     {
7319       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7320         break;
7321     }
7322
7323   /* Create new list element if necessary.  */
7324   if (i < fip->nfnfields)
7325     flp = &fip->fnfieldlists[i];
7326   else
7327     {
7328       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7329         {
7330           fip->fnfieldlists = (struct fnfieldlist *)
7331             xrealloc (fip->fnfieldlists,
7332                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7333                       * sizeof (struct fnfieldlist));
7334           if (fip->nfnfields == 0)
7335             make_cleanup (free_current_contents, &fip->fnfieldlists);
7336         }
7337       flp = &fip->fnfieldlists[fip->nfnfields];
7338       flp->name = fieldname;
7339       flp->length = 0;
7340       flp->head = NULL;
7341       i = fip->nfnfields++;
7342     }
7343
7344   /* Create a new member function field and chain it to the field list
7345      entry.  */
7346   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7347   make_cleanup (xfree, new_fnfield);
7348   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7349   new_fnfield->next = flp->head;
7350   flp->head = new_fnfield;
7351   flp->length++;
7352
7353   /* Fill in the member function field info.  */
7354   fnp = &new_fnfield->fnfield;
7355
7356   /* Delay processing of the physname until later.  */
7357   if (cu->language == language_cplus || cu->language == language_java)
7358     {
7359       add_to_method_list (type, i, flp->length - 1, fieldname,
7360                           die, cu);
7361     }
7362   else
7363     {
7364       const char *physname = dwarf2_physname (fieldname, die, cu);
7365       fnp->physname = physname ? physname : "";
7366     }
7367
7368   fnp->type = alloc_type (objfile);
7369   this_type = read_type_die (die, cu);
7370   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7371     {
7372       int nparams = TYPE_NFIELDS (this_type);
7373
7374       /* TYPE is the domain of this method, and THIS_TYPE is the type
7375            of the method itself (TYPE_CODE_METHOD).  */
7376       smash_to_method_type (fnp->type, type,
7377                             TYPE_TARGET_TYPE (this_type),
7378                             TYPE_FIELDS (this_type),
7379                             TYPE_NFIELDS (this_type),
7380                             TYPE_VARARGS (this_type));
7381
7382       /* Handle static member functions.
7383          Dwarf2 has no clean way to discern C++ static and non-static
7384          member functions.  G++ helps GDB by marking the first
7385          parameter for non-static member functions (which is the this
7386          pointer) as artificial.  We obtain this information from
7387          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7388       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7389         fnp->voffset = VOFFSET_STATIC;
7390     }
7391   else
7392     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7393                dwarf2_full_name (fieldname, die, cu));
7394
7395   /* Get fcontext from DW_AT_containing_type if present.  */
7396   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7397     fnp->fcontext = die_containing_type (die, cu);
7398
7399   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7400      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7401
7402   /* Get accessibility.  */
7403   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7404   if (attr)
7405     accessibility = DW_UNSND (attr);
7406   else
7407     accessibility = dwarf2_default_access_attribute (die, cu);
7408   switch (accessibility)
7409     {
7410     case DW_ACCESS_private:
7411       fnp->is_private = 1;
7412       break;
7413     case DW_ACCESS_protected:
7414       fnp->is_protected = 1;
7415       break;
7416     }
7417
7418   /* Check for artificial methods.  */
7419   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7420   if (attr && DW_UNSND (attr) != 0)
7421     fnp->is_artificial = 1;
7422
7423   /* Get index in virtual function table if it is a virtual member
7424      function.  For older versions of GCC, this is an offset in the
7425      appropriate virtual table, as specified by DW_AT_containing_type.
7426      For everyone else, it is an expression to be evaluated relative
7427      to the object address.  */
7428
7429   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7430   if (attr)
7431     {
7432       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7433         {
7434           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7435             {
7436               /* Old-style GCC.  */
7437               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7438             }
7439           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7440                    || (DW_BLOCK (attr)->size > 1
7441                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7442                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7443             {
7444               struct dwarf_block blk;
7445               int offset;
7446
7447               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7448                         ? 1 : 2);
7449               blk.size = DW_BLOCK (attr)->size - offset;
7450               blk.data = DW_BLOCK (attr)->data + offset;
7451               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7452               if ((fnp->voffset % cu->header.addr_size) != 0)
7453                 dwarf2_complex_location_expr_complaint ();
7454               else
7455                 fnp->voffset /= cu->header.addr_size;
7456               fnp->voffset += 2;
7457             }
7458           else
7459             dwarf2_complex_location_expr_complaint ();
7460
7461           if (!fnp->fcontext)
7462             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7463         }
7464       else if (attr_form_is_section_offset (attr))
7465         {
7466           dwarf2_complex_location_expr_complaint ();
7467         }
7468       else
7469         {
7470           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7471                                                  fieldname);
7472         }
7473     }
7474   else
7475     {
7476       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7477       if (attr && DW_UNSND (attr))
7478         {
7479           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7480           complaint (&symfile_complaints,
7481                      _("Member function \"%s\" (offset %d) is virtual "
7482                        "but the vtable offset is not specified"),
7483                      fieldname, die->offset);
7484           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7485           TYPE_CPLUS_DYNAMIC (type) = 1;
7486         }
7487     }
7488 }
7489
7490 /* Create the vector of member function fields, and attach it to the type.  */
7491
7492 static void
7493 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7494                                  struct dwarf2_cu *cu)
7495 {
7496   struct fnfieldlist *flp;
7497   int total_length = 0;
7498   int i;
7499
7500   if (cu->language == language_ada)
7501     error (_("unexpected member functions in Ada type"));
7502
7503   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7504   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7505     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7506
7507   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7508     {
7509       struct nextfnfield *nfp = flp->head;
7510       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7511       int k;
7512
7513       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7514       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7515       fn_flp->fn_fields = (struct fn_field *)
7516         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7517       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7518         fn_flp->fn_fields[k] = nfp->fnfield;
7519
7520       total_length += flp->length;
7521     }
7522
7523   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7524   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7525 }
7526
7527 /* Returns non-zero if NAME is the name of a vtable member in CU's
7528    language, zero otherwise.  */
7529 static int
7530 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7531 {
7532   static const char vptr[] = "_vptr";
7533   static const char vtable[] = "vtable";
7534
7535   /* Look for the C++ and Java forms of the vtable.  */
7536   if ((cu->language == language_java
7537        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7538        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7539        && is_cplus_marker (name[sizeof (vptr) - 1])))
7540     return 1;
7541
7542   return 0;
7543 }
7544
7545 /* GCC outputs unnamed structures that are really pointers to member
7546    functions, with the ABI-specified layout.  If TYPE describes
7547    such a structure, smash it into a member function type.
7548
7549    GCC shouldn't do this; it should just output pointer to member DIEs.
7550    This is GCC PR debug/28767.  */
7551
7552 static void
7553 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7554 {
7555   struct type *pfn_type, *domain_type, *new_type;
7556
7557   /* Check for a structure with no name and two children.  */
7558   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7559     return;
7560
7561   /* Check for __pfn and __delta members.  */
7562   if (TYPE_FIELD_NAME (type, 0) == NULL
7563       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7564       || TYPE_FIELD_NAME (type, 1) == NULL
7565       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7566     return;
7567
7568   /* Find the type of the method.  */
7569   pfn_type = TYPE_FIELD_TYPE (type, 0);
7570   if (pfn_type == NULL
7571       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7572       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7573     return;
7574
7575   /* Look for the "this" argument.  */
7576   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7577   if (TYPE_NFIELDS (pfn_type) == 0
7578       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7579       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7580     return;
7581
7582   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7583   new_type = alloc_type (objfile);
7584   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7585                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7586                         TYPE_VARARGS (pfn_type));
7587   smash_to_methodptr_type (type, new_type);
7588 }
7589
7590 /* Called when we find the DIE that starts a structure or union scope
7591    (definition) to create a type for the structure or union.  Fill in
7592    the type's name and general properties; the members will not be
7593    processed until process_structure_type.
7594
7595    NOTE: we need to call these functions regardless of whether or not the
7596    DIE has a DW_AT_name attribute, since it might be an anonymous
7597    structure or union.  This gets the type entered into our set of
7598    user defined types.
7599
7600    However, if the structure is incomplete (an opaque struct/union)
7601    then suppress creating a symbol table entry for it since gdb only
7602    wants to find the one with the complete definition.  Note that if
7603    it is complete, we just call new_symbol, which does it's own
7604    checking about whether the struct/union is anonymous or not (and
7605    suppresses creating a symbol table entry itself).  */
7606
7607 static struct type *
7608 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7609 {
7610   struct objfile *objfile = cu->objfile;
7611   struct type *type;
7612   struct attribute *attr;
7613   char *name;
7614
7615   /* If the definition of this type lives in .debug_types, read that type.
7616      Don't follow DW_AT_specification though, that will take us back up
7617      the chain and we want to go down.  */
7618   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7619   if (attr)
7620     {
7621       struct dwarf2_cu *type_cu = cu;
7622       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7623
7624       /* We could just recurse on read_structure_type, but we need to call
7625          get_die_type to ensure only one type for this DIE is created.
7626          This is important, for example, because for c++ classes we need
7627          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7628       type = read_type_die (type_die, type_cu);
7629
7630       /* TYPE_CU may not be the same as CU.
7631          Ensure TYPE is recorded in CU's type_hash table.  */
7632       return set_die_type (die, type, cu);
7633     }
7634
7635   type = alloc_type (objfile);
7636   INIT_CPLUS_SPECIFIC (type);
7637
7638   name = dwarf2_name (die, cu);
7639   if (name != NULL)
7640     {
7641       if (cu->language == language_cplus
7642           || cu->language == language_java)
7643         {
7644           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7645
7646           /* dwarf2_full_name might have already finished building the DIE's
7647              type.  If so, there is no need to continue.  */
7648           if (get_die_type (die, cu) != NULL)
7649             return get_die_type (die, cu);
7650
7651           TYPE_TAG_NAME (type) = full_name;
7652           if (die->tag == DW_TAG_structure_type
7653               || die->tag == DW_TAG_class_type)
7654             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7655         }
7656       else
7657         {
7658           /* The name is already allocated along with this objfile, so
7659              we don't need to duplicate it for the type.  */
7660           TYPE_TAG_NAME (type) = (char *) name;
7661           if (die->tag == DW_TAG_class_type)
7662             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7663         }
7664     }
7665
7666   if (die->tag == DW_TAG_structure_type)
7667     {
7668       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7669     }
7670   else if (die->tag == DW_TAG_union_type)
7671     {
7672       TYPE_CODE (type) = TYPE_CODE_UNION;
7673     }
7674   else
7675     {
7676       TYPE_CODE (type) = TYPE_CODE_CLASS;
7677     }
7678
7679   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7680     TYPE_DECLARED_CLASS (type) = 1;
7681
7682   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7683   if (attr)
7684     {
7685       TYPE_LENGTH (type) = DW_UNSND (attr);
7686     }
7687   else
7688     {
7689       TYPE_LENGTH (type) = 0;
7690     }
7691
7692   TYPE_STUB_SUPPORTED (type) = 1;
7693   if (die_is_declaration (die, cu))
7694     TYPE_STUB (type) = 1;
7695   else if (attr == NULL && die->child == NULL
7696            && producer_is_realview (cu->producer))
7697     /* RealView does not output the required DW_AT_declaration
7698        on incomplete types.  */
7699     TYPE_STUB (type) = 1;
7700
7701   /* We need to add the type field to the die immediately so we don't
7702      infinitely recurse when dealing with pointers to the structure
7703      type within the structure itself.  */
7704   set_die_type (die, type, cu);
7705
7706   /* set_die_type should be already done.  */
7707   set_descriptive_type (type, die, cu);
7708
7709   return type;
7710 }
7711
7712 /* Finish creating a structure or union type, including filling in
7713    its members and creating a symbol for it.  */
7714
7715 static void
7716 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7717 {
7718   struct objfile *objfile = cu->objfile;
7719   struct die_info *child_die = die->child;
7720   struct type *type;
7721
7722   type = get_die_type (die, cu);
7723   if (type == NULL)
7724     type = read_structure_type (die, cu);
7725
7726   if (die->child != NULL && ! die_is_declaration (die, cu))
7727     {
7728       struct field_info fi;
7729       struct die_info *child_die;
7730       VEC (symbolp) *template_args = NULL;
7731       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7732
7733       memset (&fi, 0, sizeof (struct field_info));
7734
7735       child_die = die->child;
7736
7737       while (child_die && child_die->tag)
7738         {
7739           if (child_die->tag == DW_TAG_member
7740               || child_die->tag == DW_TAG_variable)
7741             {
7742               /* NOTE: carlton/2002-11-05: A C++ static data member
7743                  should be a DW_TAG_member that is a declaration, but
7744                  all versions of G++ as of this writing (so through at
7745                  least 3.2.1) incorrectly generate DW_TAG_variable
7746                  tags for them instead.  */
7747               dwarf2_add_field (&fi, child_die, cu);
7748             }
7749           else if (child_die->tag == DW_TAG_subprogram)
7750             {
7751               /* C++ member function.  */
7752               dwarf2_add_member_fn (&fi, child_die, type, cu);
7753             }
7754           else if (child_die->tag == DW_TAG_inheritance)
7755             {
7756               /* C++ base class field.  */
7757               dwarf2_add_field (&fi, child_die, cu);
7758             }
7759           else if (child_die->tag == DW_TAG_typedef)
7760             dwarf2_add_typedef (&fi, child_die, cu);
7761           else if (child_die->tag == DW_TAG_template_type_param
7762                    || child_die->tag == DW_TAG_template_value_param)
7763             {
7764               struct symbol *arg = new_symbol (child_die, NULL, cu);
7765
7766               if (arg != NULL)
7767                 VEC_safe_push (symbolp, template_args, arg);
7768             }
7769
7770           child_die = sibling_die (child_die);
7771         }
7772
7773       /* Attach template arguments to type.  */
7774       if (! VEC_empty (symbolp, template_args))
7775         {
7776           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7777           TYPE_N_TEMPLATE_ARGUMENTS (type)
7778             = VEC_length (symbolp, template_args);
7779           TYPE_TEMPLATE_ARGUMENTS (type)
7780             = obstack_alloc (&objfile->objfile_obstack,
7781                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7782                               * sizeof (struct symbol *)));
7783           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7784                   VEC_address (symbolp, template_args),
7785                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7786                    * sizeof (struct symbol *)));
7787           VEC_free (symbolp, template_args);
7788         }
7789
7790       /* Attach fields and member functions to the type.  */
7791       if (fi.nfields)
7792         dwarf2_attach_fields_to_type (&fi, type, cu);
7793       if (fi.nfnfields)
7794         {
7795           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7796
7797           /* Get the type which refers to the base class (possibly this
7798              class itself) which contains the vtable pointer for the current
7799              class from the DW_AT_containing_type attribute.  This use of
7800              DW_AT_containing_type is a GNU extension.  */
7801
7802           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7803             {
7804               struct type *t = die_containing_type (die, cu);
7805
7806               TYPE_VPTR_BASETYPE (type) = t;
7807               if (type == t)
7808                 {
7809                   int i;
7810
7811                   /* Our own class provides vtbl ptr.  */
7812                   for (i = TYPE_NFIELDS (t) - 1;
7813                        i >= TYPE_N_BASECLASSES (t);
7814                        --i)
7815                     {
7816                       char *fieldname = TYPE_FIELD_NAME (t, i);
7817
7818                       if (is_vtable_name (fieldname, cu))
7819                         {
7820                           TYPE_VPTR_FIELDNO (type) = i;
7821                           break;
7822                         }
7823                     }
7824
7825                   /* Complain if virtual function table field not found.  */
7826                   if (i < TYPE_N_BASECLASSES (t))
7827                     complaint (&symfile_complaints,
7828                                _("virtual function table pointer "
7829                                  "not found when defining class '%s'"),
7830                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7831                                "");
7832                 }
7833               else
7834                 {
7835                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7836                 }
7837             }
7838           else if (cu->producer
7839                    && strncmp (cu->producer,
7840                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7841             {
7842               /* The IBM XLC compiler does not provide direct indication
7843                  of the containing type, but the vtable pointer is
7844                  always named __vfp.  */
7845
7846               int i;
7847
7848               for (i = TYPE_NFIELDS (type) - 1;
7849                    i >= TYPE_N_BASECLASSES (type);
7850                    --i)
7851                 {
7852                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7853                     {
7854                       TYPE_VPTR_FIELDNO (type) = i;
7855                       TYPE_VPTR_BASETYPE (type) = type;
7856                       break;
7857                     }
7858                 }
7859             }
7860         }
7861
7862       /* Copy fi.typedef_field_list linked list elements content into the
7863          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7864       if (fi.typedef_field_list)
7865         {
7866           int i = fi.typedef_field_list_count;
7867
7868           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7869           TYPE_TYPEDEF_FIELD_ARRAY (type)
7870             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7871           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7872
7873           /* Reverse the list order to keep the debug info elements order.  */
7874           while (--i >= 0)
7875             {
7876               struct typedef_field *dest, *src;
7877
7878               dest = &TYPE_TYPEDEF_FIELD (type, i);
7879               src = &fi.typedef_field_list->field;
7880               fi.typedef_field_list = fi.typedef_field_list->next;
7881               *dest = *src;
7882             }
7883         }
7884
7885       do_cleanups (back_to);
7886
7887       if (HAVE_CPLUS_STRUCT (type))
7888         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7889     }
7890
7891   quirk_gcc_member_function_pointer (type, cu->objfile);
7892
7893   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7894      snapshots) has been known to create a die giving a declaration
7895      for a class that has, as a child, a die giving a definition for a
7896      nested class.  So we have to process our children even if the
7897      current die is a declaration.  Normally, of course, a declaration
7898      won't have any children at all.  */
7899
7900   while (child_die != NULL && child_die->tag)
7901     {
7902       if (child_die->tag == DW_TAG_member
7903           || child_die->tag == DW_TAG_variable
7904           || child_die->tag == DW_TAG_inheritance
7905           || child_die->tag == DW_TAG_template_value_param
7906           || child_die->tag == DW_TAG_template_type_param)
7907         {
7908           /* Do nothing.  */
7909         }
7910       else
7911         process_die (child_die, cu);
7912
7913       child_die = sibling_die (child_die);
7914     }
7915
7916   /* Do not consider external references.  According to the DWARF standard,
7917      these DIEs are identified by the fact that they have no byte_size
7918      attribute, and a declaration attribute.  */
7919   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7920       || !die_is_declaration (die, cu))
7921     new_symbol (die, type, cu);
7922 }
7923
7924 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7925    complete the type's fields yet, or create any symbols.  */
7926
7927 static struct type *
7928 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7929 {
7930   struct objfile *objfile = cu->objfile;
7931   struct type *type;
7932   struct attribute *attr;
7933   const char *name;
7934
7935   /* If the definition of this type lives in .debug_types, read that type.
7936      Don't follow DW_AT_specification though, that will take us back up
7937      the chain and we want to go down.  */
7938   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7939   if (attr)
7940     {
7941       struct dwarf2_cu *type_cu = cu;
7942       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7943
7944       type = read_type_die (type_die, type_cu);
7945
7946       /* TYPE_CU may not be the same as CU.
7947          Ensure TYPE is recorded in CU's type_hash table.  */
7948       return set_die_type (die, type, cu);
7949     }
7950
7951   type = alloc_type (objfile);
7952
7953   TYPE_CODE (type) = TYPE_CODE_ENUM;
7954   name = dwarf2_full_name (NULL, die, cu);
7955   if (name != NULL)
7956     TYPE_TAG_NAME (type) = (char *) name;
7957
7958   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7959   if (attr)
7960     {
7961       TYPE_LENGTH (type) = DW_UNSND (attr);
7962     }
7963   else
7964     {
7965       TYPE_LENGTH (type) = 0;
7966     }
7967
7968   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7969      declared as private in the package spec, and then defined only
7970      inside the package body.  Such types are known as Taft Amendment
7971      Types.  When another package uses such a type, an incomplete DIE
7972      may be generated by the compiler.  */
7973   if (die_is_declaration (die, cu))
7974     TYPE_STUB (type) = 1;
7975
7976   return set_die_type (die, type, cu);
7977 }
7978
7979 /* Given a pointer to a die which begins an enumeration, process all
7980    the dies that define the members of the enumeration, and create the
7981    symbol for the enumeration type.
7982
7983    NOTE: We reverse the order of the element list.  */
7984
7985 static void
7986 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7987 {
7988   struct type *this_type;
7989
7990   this_type = get_die_type (die, cu);
7991   if (this_type == NULL)
7992     this_type = read_enumeration_type (die, cu);
7993
7994   if (die->child != NULL)
7995     {
7996       struct die_info *child_die;
7997       struct symbol *sym;
7998       struct field *fields = NULL;
7999       int num_fields = 0;
8000       int unsigned_enum = 1;
8001       char *name;
8002
8003       child_die = die->child;
8004       while (child_die && child_die->tag)
8005         {
8006           if (child_die->tag != DW_TAG_enumerator)
8007             {
8008               process_die (child_die, cu);
8009             }
8010           else
8011             {
8012               name = dwarf2_name (child_die, cu);
8013               if (name)
8014                 {
8015                   sym = new_symbol (child_die, this_type, cu);
8016                   if (SYMBOL_VALUE (sym) < 0)
8017                     unsigned_enum = 0;
8018
8019                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8020                     {
8021                       fields = (struct field *)
8022                         xrealloc (fields,
8023                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
8024                                   * sizeof (struct field));
8025                     }
8026
8027                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8028                   FIELD_TYPE (fields[num_fields]) = NULL;
8029                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8030                   FIELD_BITSIZE (fields[num_fields]) = 0;
8031
8032                   num_fields++;
8033                 }
8034             }
8035
8036           child_die = sibling_die (child_die);
8037         }
8038
8039       if (num_fields)
8040         {
8041           TYPE_NFIELDS (this_type) = num_fields;
8042           TYPE_FIELDS (this_type) = (struct field *)
8043             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8044           memcpy (TYPE_FIELDS (this_type), fields,
8045                   sizeof (struct field) * num_fields);
8046           xfree (fields);
8047         }
8048       if (unsigned_enum)
8049         TYPE_UNSIGNED (this_type) = 1;
8050     }
8051
8052   /* If we are reading an enum from a .debug_types unit, and the enum
8053      is a declaration, and the enum is not the signatured type in the
8054      unit, then we do not want to add a symbol for it.  Adding a
8055      symbol would in some cases obscure the true definition of the
8056      enum, giving users an incomplete type when the definition is
8057      actually available.  Note that we do not want to do this for all
8058      enums which are just declarations, because C++0x allows forward
8059      enum declarations.  */
8060   if (cu->per_cu->debug_type_section
8061       && die_is_declaration (die, cu))
8062     {
8063       struct signatured_type *type_sig;
8064
8065       type_sig
8066         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8067                                             cu->per_cu->debug_type_section,
8068                                             cu->per_cu->offset);
8069       if (type_sig->type_offset != die->offset)
8070         return;
8071     }
8072
8073   new_symbol (die, this_type, cu);
8074 }
8075
8076 /* Extract all information from a DW_TAG_array_type DIE and put it in
8077    the DIE's type field.  For now, this only handles one dimensional
8078    arrays.  */
8079
8080 static struct type *
8081 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8082 {
8083   struct objfile *objfile = cu->objfile;
8084   struct die_info *child_die;
8085   struct type *type;
8086   struct type *element_type, *range_type, *index_type;
8087   struct type **range_types = NULL;
8088   struct attribute *attr;
8089   int ndim = 0;
8090   struct cleanup *back_to;
8091   char *name;
8092
8093   element_type = die_type (die, cu);
8094
8095   /* The die_type call above may have already set the type for this DIE.  */
8096   type = get_die_type (die, cu);
8097   if (type)
8098     return type;
8099
8100   /* Irix 6.2 native cc creates array types without children for
8101      arrays with unspecified length.  */
8102   if (die->child == NULL)
8103     {
8104       index_type = objfile_type (objfile)->builtin_int;
8105       range_type = create_range_type (NULL, index_type, 0, -1);
8106       type = create_array_type (NULL, element_type, range_type);
8107       return set_die_type (die, type, cu);
8108     }
8109
8110   back_to = make_cleanup (null_cleanup, NULL);
8111   child_die = die->child;
8112   while (child_die && child_die->tag)
8113     {
8114       if (child_die->tag == DW_TAG_subrange_type)
8115         {
8116           struct type *child_type = read_type_die (child_die, cu);
8117
8118           if (child_type != NULL)
8119             {
8120               /* The range type was succesfully read.  Save it for the
8121                  array type creation.  */
8122               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8123                 {
8124                   range_types = (struct type **)
8125                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8126                               * sizeof (struct type *));
8127                   if (ndim == 0)
8128                     make_cleanup (free_current_contents, &range_types);
8129                 }
8130               range_types[ndim++] = child_type;
8131             }
8132         }
8133       child_die = sibling_die (child_die);
8134     }
8135
8136   /* Dwarf2 dimensions are output from left to right, create the
8137      necessary array types in backwards order.  */
8138
8139   type = element_type;
8140
8141   if (read_array_order (die, cu) == DW_ORD_col_major)
8142     {
8143       int i = 0;
8144
8145       while (i < ndim)
8146         type = create_array_type (NULL, type, range_types[i++]);
8147     }
8148   else
8149     {
8150       while (ndim-- > 0)
8151         type = create_array_type (NULL, type, range_types[ndim]);
8152     }
8153
8154   /* Understand Dwarf2 support for vector types (like they occur on
8155      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8156      array type.  This is not part of the Dwarf2/3 standard yet, but a
8157      custom vendor extension.  The main difference between a regular
8158      array and the vector variant is that vectors are passed by value
8159      to functions.  */
8160   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8161   if (attr)
8162     make_vector_type (type);
8163
8164   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8165      implementation may choose to implement triple vectors using this
8166      attribute.  */
8167   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8168   if (attr)
8169     {
8170       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8171         TYPE_LENGTH (type) = DW_UNSND (attr);
8172       else
8173         complaint (&symfile_complaints,
8174                    _("DW_AT_byte_size for array type smaller "
8175                      "than the total size of elements"));
8176     }
8177
8178   name = dwarf2_name (die, cu);
8179   if (name)
8180     TYPE_NAME (type) = name;
8181
8182   /* Install the type in the die.  */
8183   set_die_type (die, type, cu);
8184
8185   /* set_die_type should be already done.  */
8186   set_descriptive_type (type, die, cu);
8187
8188   do_cleanups (back_to);
8189
8190   return type;
8191 }
8192
8193 static enum dwarf_array_dim_ordering
8194 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8195 {
8196   struct attribute *attr;
8197
8198   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8199
8200   if (attr) return DW_SND (attr);
8201
8202   /* GNU F77 is a special case, as at 08/2004 array type info is the
8203      opposite order to the dwarf2 specification, but data is still
8204      laid out as per normal fortran.
8205
8206      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8207      version checking.  */
8208
8209   if (cu->language == language_fortran
8210       && cu->producer && strstr (cu->producer, "GNU F77"))
8211     {
8212       return DW_ORD_row_major;
8213     }
8214
8215   switch (cu->language_defn->la_array_ordering)
8216     {
8217     case array_column_major:
8218       return DW_ORD_col_major;
8219     case array_row_major:
8220     default:
8221       return DW_ORD_row_major;
8222     };
8223 }
8224
8225 /* Extract all information from a DW_TAG_set_type DIE and put it in
8226    the DIE's type field.  */
8227
8228 static struct type *
8229 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8230 {
8231   struct type *domain_type, *set_type;
8232   struct attribute *attr;
8233
8234   domain_type = die_type (die, cu);
8235
8236   /* The die_type call above may have already set the type for this DIE.  */
8237   set_type = get_die_type (die, cu);
8238   if (set_type)
8239     return set_type;
8240
8241   set_type = create_set_type (NULL, domain_type);
8242
8243   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8244   if (attr)
8245     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8246
8247   return set_die_type (die, set_type, cu);
8248 }
8249
8250 /* First cut: install each common block member as a global variable.  */
8251
8252 static void
8253 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8254 {
8255   struct die_info *child_die;
8256   struct attribute *attr;
8257   struct symbol *sym;
8258   CORE_ADDR base = (CORE_ADDR) 0;
8259
8260   attr = dwarf2_attr (die, DW_AT_location, cu);
8261   if (attr)
8262     {
8263       /* Support the .debug_loc offsets.  */
8264       if (attr_form_is_block (attr))
8265         {
8266           base = decode_locdesc (DW_BLOCK (attr), cu);
8267         }
8268       else if (attr_form_is_section_offset (attr))
8269         {
8270           dwarf2_complex_location_expr_complaint ();
8271         }
8272       else
8273         {
8274           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8275                                                  "common block member");
8276         }
8277     }
8278   if (die->child != NULL)
8279     {
8280       child_die = die->child;
8281       while (child_die && child_die->tag)
8282         {
8283           LONGEST offset;
8284
8285           sym = new_symbol (child_die, NULL, cu);
8286           if (sym != NULL
8287               && handle_data_member_location (child_die, cu, &offset))
8288             {
8289               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8290               add_symbol_to_list (sym, &global_symbols);
8291             }
8292           child_die = sibling_die (child_die);
8293         }
8294     }
8295 }
8296
8297 /* Create a type for a C++ namespace.  */
8298
8299 static struct type *
8300 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8301 {
8302   struct objfile *objfile = cu->objfile;
8303   const char *previous_prefix, *name;
8304   int is_anonymous;
8305   struct type *type;
8306
8307   /* For extensions, reuse the type of the original namespace.  */
8308   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8309     {
8310       struct die_info *ext_die;
8311       struct dwarf2_cu *ext_cu = cu;
8312
8313       ext_die = dwarf2_extension (die, &ext_cu);
8314       type = read_type_die (ext_die, ext_cu);
8315
8316       /* EXT_CU may not be the same as CU.
8317          Ensure TYPE is recorded in CU's type_hash table.  */
8318       return set_die_type (die, type, cu);
8319     }
8320
8321   name = namespace_name (die, &is_anonymous, cu);
8322
8323   /* Now build the name of the current namespace.  */
8324
8325   previous_prefix = determine_prefix (die, cu);
8326   if (previous_prefix[0] != '\0')
8327     name = typename_concat (&objfile->objfile_obstack,
8328                             previous_prefix, name, 0, cu);
8329
8330   /* Create the type.  */
8331   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8332                     objfile);
8333   TYPE_NAME (type) = (char *) name;
8334   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8335
8336   return set_die_type (die, type, cu);
8337 }
8338
8339 /* Read a C++ namespace.  */
8340
8341 static void
8342 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8343 {
8344   struct objfile *objfile = cu->objfile;
8345   int is_anonymous;
8346
8347   /* Add a symbol associated to this if we haven't seen the namespace
8348      before.  Also, add a using directive if it's an anonymous
8349      namespace.  */
8350
8351   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8352     {
8353       struct type *type;
8354
8355       type = read_type_die (die, cu);
8356       new_symbol (die, type, cu);
8357
8358       namespace_name (die, &is_anonymous, cu);
8359       if (is_anonymous)
8360         {
8361           const char *previous_prefix = determine_prefix (die, cu);
8362
8363           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8364                                   NULL, NULL, &objfile->objfile_obstack);
8365         }
8366     }
8367
8368   if (die->child != NULL)
8369     {
8370       struct die_info *child_die = die->child;
8371
8372       while (child_die && child_die->tag)
8373         {
8374           process_die (child_die, cu);
8375           child_die = sibling_die (child_die);
8376         }
8377     }
8378 }
8379
8380 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8381    imported module.  Still we need that type as local Fortran "use ... only"
8382    declaration imports depend on the created type in determine_prefix.  */
8383
8384 static struct type *
8385 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8386 {
8387   struct objfile *objfile = cu->objfile;
8388   char *module_name;
8389   struct type *type;
8390
8391   module_name = dwarf2_name (die, cu);
8392   if (!module_name)
8393     complaint (&symfile_complaints,
8394                _("DW_TAG_module has no name, offset 0x%x"),
8395                die->offset);
8396   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8397
8398   /* determine_prefix uses TYPE_TAG_NAME.  */
8399   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8400
8401   return set_die_type (die, type, cu);
8402 }
8403
8404 /* Read a Fortran module.  */
8405
8406 static void
8407 read_module (struct die_info *die, struct dwarf2_cu *cu)
8408 {
8409   struct die_info *child_die = die->child;
8410
8411   while (child_die && child_die->tag)
8412     {
8413       process_die (child_die, cu);
8414       child_die = sibling_die (child_die);
8415     }
8416 }
8417
8418 /* Return the name of the namespace represented by DIE.  Set
8419    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8420    namespace.  */
8421
8422 static const char *
8423 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8424 {
8425   struct die_info *current_die;
8426   const char *name = NULL;
8427
8428   /* Loop through the extensions until we find a name.  */
8429
8430   for (current_die = die;
8431        current_die != NULL;
8432        current_die = dwarf2_extension (die, &cu))
8433     {
8434       name = dwarf2_name (current_die, cu);
8435       if (name != NULL)
8436         break;
8437     }
8438
8439   /* Is it an anonymous namespace?  */
8440
8441   *is_anonymous = (name == NULL);
8442   if (*is_anonymous)
8443     name = CP_ANONYMOUS_NAMESPACE_STR;
8444
8445   return name;
8446 }
8447
8448 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8449    the user defined type vector.  */
8450
8451 static struct type *
8452 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8453 {
8454   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8455   struct comp_unit_head *cu_header = &cu->header;
8456   struct type *type;
8457   struct attribute *attr_byte_size;
8458   struct attribute *attr_address_class;
8459   int byte_size, addr_class;
8460   struct type *target_type;
8461
8462   target_type = die_type (die, cu);
8463
8464   /* The die_type call above may have already set the type for this DIE.  */
8465   type = get_die_type (die, cu);
8466   if (type)
8467     return type;
8468
8469   type = lookup_pointer_type (target_type);
8470
8471   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8472   if (attr_byte_size)
8473     byte_size = DW_UNSND (attr_byte_size);
8474   else
8475     byte_size = cu_header->addr_size;
8476
8477   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8478   if (attr_address_class)
8479     addr_class = DW_UNSND (attr_address_class);
8480   else
8481     addr_class = DW_ADDR_none;
8482
8483   /* If the pointer size or address class is different than the
8484      default, create a type variant marked as such and set the
8485      length accordingly.  */
8486   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8487     {
8488       if (gdbarch_address_class_type_flags_p (gdbarch))
8489         {
8490           int type_flags;
8491
8492           type_flags = gdbarch_address_class_type_flags
8493                          (gdbarch, byte_size, addr_class);
8494           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8495                       == 0);
8496           type = make_type_with_address_space (type, type_flags);
8497         }
8498       else if (TYPE_LENGTH (type) != byte_size)
8499         {
8500           complaint (&symfile_complaints,
8501                      _("invalid pointer size %d"), byte_size);
8502         }
8503       else
8504         {
8505           /* Should we also complain about unhandled address classes?  */
8506         }
8507     }
8508
8509   TYPE_LENGTH (type) = byte_size;
8510   return set_die_type (die, type, cu);
8511 }
8512
8513 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8514    the user defined type vector.  */
8515
8516 static struct type *
8517 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8518 {
8519   struct type *type;
8520   struct type *to_type;
8521   struct type *domain;
8522
8523   to_type = die_type (die, cu);
8524   domain = die_containing_type (die, cu);
8525
8526   /* The calls above may have already set the type for this DIE.  */
8527   type = get_die_type (die, cu);
8528   if (type)
8529     return type;
8530
8531   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8532     type = lookup_methodptr_type (to_type);
8533   else
8534     type = lookup_memberptr_type (to_type, domain);
8535
8536   return set_die_type (die, type, cu);
8537 }
8538
8539 /* Extract all information from a DW_TAG_reference_type DIE and add to
8540    the user defined type vector.  */
8541
8542 static struct type *
8543 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8544 {
8545   struct comp_unit_head *cu_header = &cu->header;
8546   struct type *type, *target_type;
8547   struct attribute *attr;
8548
8549   target_type = die_type (die, cu);
8550
8551   /* The die_type call above may have already set the type for this DIE.  */
8552   type = get_die_type (die, cu);
8553   if (type)
8554     return type;
8555
8556   type = lookup_reference_type (target_type);
8557   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8558   if (attr)
8559     {
8560       TYPE_LENGTH (type) = DW_UNSND (attr);
8561     }
8562   else
8563     {
8564       TYPE_LENGTH (type) = cu_header->addr_size;
8565     }
8566   return set_die_type (die, type, cu);
8567 }
8568
8569 static struct type *
8570 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8571 {
8572   struct type *base_type, *cv_type;
8573
8574   base_type = die_type (die, cu);
8575
8576   /* The die_type call above may have already set the type for this DIE.  */
8577   cv_type = get_die_type (die, cu);
8578   if (cv_type)
8579     return cv_type;
8580
8581   /* In case the const qualifier is applied to an array type, the element type
8582      is so qualified, not the array type (section 6.7.3 of C99).  */
8583   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8584     {
8585       struct type *el_type, *inner_array;
8586
8587       base_type = copy_type (base_type);
8588       inner_array = base_type;
8589
8590       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8591         {
8592           TYPE_TARGET_TYPE (inner_array) =
8593             copy_type (TYPE_TARGET_TYPE (inner_array));
8594           inner_array = TYPE_TARGET_TYPE (inner_array);
8595         }
8596
8597       el_type = TYPE_TARGET_TYPE (inner_array);
8598       TYPE_TARGET_TYPE (inner_array) =
8599         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8600
8601       return set_die_type (die, base_type, cu);
8602     }
8603
8604   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8605   return set_die_type (die, cv_type, cu);
8606 }
8607
8608 static struct type *
8609 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8610 {
8611   struct type *base_type, *cv_type;
8612
8613   base_type = die_type (die, cu);
8614
8615   /* The die_type call above may have already set the type for this DIE.  */
8616   cv_type = get_die_type (die, cu);
8617   if (cv_type)
8618     return cv_type;
8619
8620   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8621   return set_die_type (die, cv_type, cu);
8622 }
8623
8624 /* Extract all information from a DW_TAG_string_type DIE and add to
8625    the user defined type vector.  It isn't really a user defined type,
8626    but it behaves like one, with other DIE's using an AT_user_def_type
8627    attribute to reference it.  */
8628
8629 static struct type *
8630 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8631 {
8632   struct objfile *objfile = cu->objfile;
8633   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8634   struct type *type, *range_type, *index_type, *char_type;
8635   struct attribute *attr;
8636   unsigned int length;
8637
8638   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8639   if (attr)
8640     {
8641       length = DW_UNSND (attr);
8642     }
8643   else
8644     {
8645       /* Check for the DW_AT_byte_size attribute.  */
8646       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8647       if (attr)
8648         {
8649           length = DW_UNSND (attr);
8650         }
8651       else
8652         {
8653           length = 1;
8654         }
8655     }
8656
8657   index_type = objfile_type (objfile)->builtin_int;
8658   range_type = create_range_type (NULL, index_type, 1, length);
8659   char_type = language_string_char_type (cu->language_defn, gdbarch);
8660   type = create_string_type (NULL, char_type, range_type);
8661
8662   return set_die_type (die, type, cu);
8663 }
8664
8665 /* Handle DIES due to C code like:
8666
8667    struct foo
8668    {
8669    int (*funcp)(int a, long l);
8670    int b;
8671    };
8672
8673    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8674
8675 static struct type *
8676 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8677 {
8678   struct type *type;            /* Type that this function returns.  */
8679   struct type *ftype;           /* Function that returns above type.  */
8680   struct attribute *attr;
8681
8682   type = die_type (die, cu);
8683
8684   /* The die_type call above may have already set the type for this DIE.  */
8685   ftype = get_die_type (die, cu);
8686   if (ftype)
8687     return ftype;
8688
8689   ftype = lookup_function_type (type);
8690
8691   /* All functions in C++, Pascal and Java have prototypes.  */
8692   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8693   if ((attr && (DW_UNSND (attr) != 0))
8694       || cu->language == language_cplus
8695       || cu->language == language_java
8696       || cu->language == language_pascal)
8697     TYPE_PROTOTYPED (ftype) = 1;
8698   else if (producer_is_realview (cu->producer))
8699     /* RealView does not emit DW_AT_prototyped.  We can not
8700        distinguish prototyped and unprototyped functions; default to
8701        prototyped, since that is more common in modern code (and
8702        RealView warns about unprototyped functions).  */
8703     TYPE_PROTOTYPED (ftype) = 1;
8704
8705   /* Store the calling convention in the type if it's available in
8706      the subroutine die.  Otherwise set the calling convention to
8707      the default value DW_CC_normal.  */
8708   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8709   if (attr)
8710     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8711   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8712     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8713   else
8714     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8715
8716   /* We need to add the subroutine type to the die immediately so
8717      we don't infinitely recurse when dealing with parameters
8718      declared as the same subroutine type.  */
8719   set_die_type (die, ftype, cu);
8720
8721   if (die->child != NULL)
8722     {
8723       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8724       struct die_info *child_die;
8725       int nparams, iparams;
8726
8727       /* Count the number of parameters.
8728          FIXME: GDB currently ignores vararg functions, but knows about
8729          vararg member functions.  */
8730       nparams = 0;
8731       child_die = die->child;
8732       while (child_die && child_die->tag)
8733         {
8734           if (child_die->tag == DW_TAG_formal_parameter)
8735             nparams++;
8736           else if (child_die->tag == DW_TAG_unspecified_parameters)
8737             TYPE_VARARGS (ftype) = 1;
8738           child_die = sibling_die (child_die);
8739         }
8740
8741       /* Allocate storage for parameters and fill them in.  */
8742       TYPE_NFIELDS (ftype) = nparams;
8743       TYPE_FIELDS (ftype) = (struct field *)
8744         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8745
8746       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8747          even if we error out during the parameters reading below.  */
8748       for (iparams = 0; iparams < nparams; iparams++)
8749         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8750
8751       iparams = 0;
8752       child_die = die->child;
8753       while (child_die && child_die->tag)
8754         {
8755           if (child_die->tag == DW_TAG_formal_parameter)
8756             {
8757               struct type *arg_type;
8758
8759               /* DWARF version 2 has no clean way to discern C++
8760                  static and non-static member functions.  G++ helps
8761                  GDB by marking the first parameter for non-static
8762                  member functions (which is the this pointer) as
8763                  artificial.  We pass this information to
8764                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8765
8766                  DWARF version 3 added DW_AT_object_pointer, which GCC
8767                  4.5 does not yet generate.  */
8768               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8769               if (attr)
8770                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8771               else
8772                 {
8773                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8774
8775                   /* GCC/43521: In java, the formal parameter
8776                      "this" is sometimes not marked with DW_AT_artificial.  */
8777                   if (cu->language == language_java)
8778                     {
8779                       const char *name = dwarf2_name (child_die, cu);
8780
8781                       if (name && !strcmp (name, "this"))
8782                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8783                     }
8784                 }
8785               arg_type = die_type (child_die, cu);
8786
8787               /* RealView does not mark THIS as const, which the testsuite
8788                  expects.  GCC marks THIS as const in method definitions,
8789                  but not in the class specifications (GCC PR 43053).  */
8790               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8791                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8792                 {
8793                   int is_this = 0;
8794                   struct dwarf2_cu *arg_cu = cu;
8795                   const char *name = dwarf2_name (child_die, cu);
8796
8797                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8798                   if (attr)
8799                     {
8800                       /* If the compiler emits this, use it.  */
8801                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8802                         is_this = 1;
8803                     }
8804                   else if (name && strcmp (name, "this") == 0)
8805                     /* Function definitions will have the argument names.  */
8806                     is_this = 1;
8807                   else if (name == NULL && iparams == 0)
8808                     /* Declarations may not have the names, so like
8809                        elsewhere in GDB, assume an artificial first
8810                        argument is "this".  */
8811                     is_this = 1;
8812
8813                   if (is_this)
8814                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8815                                              arg_type, 0);
8816                 }
8817
8818               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8819               iparams++;
8820             }
8821           child_die = sibling_die (child_die);
8822         }
8823     }
8824
8825   return ftype;
8826 }
8827
8828 static struct type *
8829 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8830 {
8831   struct objfile *objfile = cu->objfile;
8832   const char *name = NULL;
8833   struct type *this_type;
8834
8835   name = dwarf2_full_name (NULL, die, cu);
8836   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8837                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8838   TYPE_NAME (this_type) = (char *) name;
8839   set_die_type (die, this_type, cu);
8840   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8841   return this_type;
8842 }
8843
8844 /* Find a representation of a given base type and install
8845    it in the TYPE field of the die.  */
8846
8847 static struct type *
8848 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8849 {
8850   struct objfile *objfile = cu->objfile;
8851   struct type *type;
8852   struct attribute *attr;
8853   int encoding = 0, size = 0;
8854   char *name;
8855   enum type_code code = TYPE_CODE_INT;
8856   int type_flags = 0;
8857   struct type *target_type = NULL;
8858
8859   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8860   if (attr)
8861     {
8862       encoding = DW_UNSND (attr);
8863     }
8864   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8865   if (attr)
8866     {
8867       size = DW_UNSND (attr);
8868     }
8869   name = dwarf2_name (die, cu);
8870   if (!name)
8871     {
8872       complaint (&symfile_complaints,
8873                  _("DW_AT_name missing from DW_TAG_base_type"));
8874     }
8875
8876   switch (encoding)
8877     {
8878       case DW_ATE_address:
8879         /* Turn DW_ATE_address into a void * pointer.  */
8880         code = TYPE_CODE_PTR;
8881         type_flags |= TYPE_FLAG_UNSIGNED;
8882         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8883         break;
8884       case DW_ATE_boolean:
8885         code = TYPE_CODE_BOOL;
8886         type_flags |= TYPE_FLAG_UNSIGNED;
8887         break;
8888       case DW_ATE_complex_float:
8889         code = TYPE_CODE_COMPLEX;
8890         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8891         break;
8892       case DW_ATE_decimal_float:
8893         code = TYPE_CODE_DECFLOAT;
8894         break;
8895       case DW_ATE_float:
8896         code = TYPE_CODE_FLT;
8897         break;
8898       case DW_ATE_signed:
8899         break;
8900       case DW_ATE_unsigned:
8901         type_flags |= TYPE_FLAG_UNSIGNED;
8902         if (cu->language == language_fortran
8903             && name
8904             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8905           code = TYPE_CODE_CHAR;
8906         break;
8907       case DW_ATE_signed_char:
8908         if (cu->language == language_ada || cu->language == language_m2
8909             || cu->language == language_pascal
8910             || cu->language == language_fortran)
8911           code = TYPE_CODE_CHAR;
8912         break;
8913       case DW_ATE_unsigned_char:
8914         if (cu->language == language_ada || cu->language == language_m2
8915             || cu->language == language_pascal
8916             || cu->language == language_fortran)
8917           code = TYPE_CODE_CHAR;
8918         type_flags |= TYPE_FLAG_UNSIGNED;
8919         break;
8920       case DW_ATE_UTF:
8921         /* We just treat this as an integer and then recognize the
8922            type by name elsewhere.  */
8923         break;
8924
8925       default:
8926         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8927                    dwarf_type_encoding_name (encoding));
8928         break;
8929     }
8930
8931   type = init_type (code, size, type_flags, NULL, objfile);
8932   TYPE_NAME (type) = name;
8933   TYPE_TARGET_TYPE (type) = target_type;
8934
8935   if (name && strcmp (name, "char") == 0)
8936     TYPE_NOSIGN (type) = 1;
8937
8938   return set_die_type (die, type, cu);
8939 }
8940
8941 /* Read the given DW_AT_subrange DIE.  */
8942
8943 static struct type *
8944 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8945 {
8946   struct type *base_type;
8947   struct type *range_type;
8948   struct attribute *attr;
8949   LONGEST low = 0;
8950   LONGEST high = -1;
8951   char *name;
8952   LONGEST negative_mask;
8953
8954   base_type = die_type (die, cu);
8955   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8956   check_typedef (base_type);
8957
8958   /* The die_type call above may have already set the type for this DIE.  */
8959   range_type = get_die_type (die, cu);
8960   if (range_type)
8961     return range_type;
8962
8963   if (cu->language == language_fortran)
8964     {
8965       /* FORTRAN implies a lower bound of 1, if not given.  */
8966       low = 1;
8967     }
8968
8969   /* FIXME: For variable sized arrays either of these could be
8970      a variable rather than a constant value.  We'll allow it,
8971      but we don't know how to handle it.  */
8972   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8973   if (attr)
8974     low = dwarf2_get_attr_constant_value (attr, 0);
8975
8976   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8977   if (attr)
8978     {
8979       if (attr_form_is_block (attr) || is_ref_attr (attr))
8980         {
8981           /* GCC encodes arrays with unspecified or dynamic length
8982              with a DW_FORM_block1 attribute or a reference attribute.
8983              FIXME: GDB does not yet know how to handle dynamic
8984              arrays properly, treat them as arrays with unspecified
8985              length for now.
8986
8987              FIXME: jimb/2003-09-22: GDB does not really know
8988              how to handle arrays of unspecified length
8989              either; we just represent them as zero-length
8990              arrays.  Choose an appropriate upper bound given
8991              the lower bound we've computed above.  */
8992           high = low - 1;
8993         }
8994       else
8995         high = dwarf2_get_attr_constant_value (attr, 1);
8996     }
8997   else
8998     {
8999       attr = dwarf2_attr (die, DW_AT_count, cu);
9000       if (attr)
9001         {
9002           int count = dwarf2_get_attr_constant_value (attr, 1);
9003           high = low + count - 1;
9004         }
9005       else
9006         {
9007           /* Unspecified array length.  */
9008           high = low - 1;
9009         }
9010     }
9011
9012   /* Dwarf-2 specifications explicitly allows to create subrange types
9013      without specifying a base type.
9014      In that case, the base type must be set to the type of
9015      the lower bound, upper bound or count, in that order, if any of these
9016      three attributes references an object that has a type.
9017      If no base type is found, the Dwarf-2 specifications say that
9018      a signed integer type of size equal to the size of an address should
9019      be used.
9020      For the following C code: `extern char gdb_int [];'
9021      GCC produces an empty range DIE.
9022      FIXME: muller/2010-05-28: Possible references to object for low bound,
9023      high bound or count are not yet handled by this code.  */
9024   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9025     {
9026       struct objfile *objfile = cu->objfile;
9027       struct gdbarch *gdbarch = get_objfile_arch (objfile);
9028       int addr_size = gdbarch_addr_bit (gdbarch) /8;
9029       struct type *int_type = objfile_type (objfile)->builtin_int;
9030
9031       /* Test "int", "long int", and "long long int" objfile types,
9032          and select the first one having a size above or equal to the
9033          architecture address size.  */
9034       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9035         base_type = int_type;
9036       else
9037         {
9038           int_type = objfile_type (objfile)->builtin_long;
9039           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9040             base_type = int_type;
9041           else
9042             {
9043               int_type = objfile_type (objfile)->builtin_long_long;
9044               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9045                 base_type = int_type;
9046             }
9047         }
9048     }
9049
9050   negative_mask =
9051     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9052   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9053     low |= negative_mask;
9054   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9055     high |= negative_mask;
9056
9057   range_type = create_range_type (NULL, base_type, low, high);
9058
9059   /* Mark arrays with dynamic length at least as an array of unspecified
9060      length.  GDB could check the boundary but before it gets implemented at
9061      least allow accessing the array elements.  */
9062   if (attr && attr_form_is_block (attr))
9063     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9064
9065   /* Ada expects an empty array on no boundary attributes.  */
9066   if (attr == NULL && cu->language != language_ada)
9067     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9068
9069   name = dwarf2_name (die, cu);
9070   if (name)
9071     TYPE_NAME (range_type) = name;
9072
9073   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9074   if (attr)
9075     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9076
9077   set_die_type (die, range_type, cu);
9078
9079   /* set_die_type should be already done.  */
9080   set_descriptive_type (range_type, die, cu);
9081
9082   return range_type;
9083 }
9084
9085 static struct type *
9086 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9087 {
9088   struct type *type;
9089
9090   /* For now, we only support the C meaning of an unspecified type: void.  */
9091
9092   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9093   TYPE_NAME (type) = dwarf2_name (die, cu);
9094
9095   return set_die_type (die, type, cu);
9096 }
9097
9098 /* Trivial hash function for die_info: the hash value of a DIE
9099    is its offset in .debug_info for this objfile.  */
9100
9101 static hashval_t
9102 die_hash (const void *item)
9103 {
9104   const struct die_info *die = item;
9105
9106   return die->offset;
9107 }
9108
9109 /* Trivial comparison function for die_info structures: two DIEs
9110    are equal if they have the same offset.  */
9111
9112 static int
9113 die_eq (const void *item_lhs, const void *item_rhs)
9114 {
9115   const struct die_info *die_lhs = item_lhs;
9116   const struct die_info *die_rhs = item_rhs;
9117
9118   return die_lhs->offset == die_rhs->offset;
9119 }
9120
9121 /* Read a whole compilation unit into a linked list of dies.  */
9122
9123 static struct die_info *
9124 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9125 {
9126   struct die_reader_specs reader_specs;
9127   int read_abbrevs = 0;
9128   struct cleanup *back_to = NULL;
9129   struct die_info *die;
9130
9131   if (cu->dwarf2_abbrevs == NULL)
9132     {
9133       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9134       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9135       read_abbrevs = 1;
9136     }
9137
9138   gdb_assert (cu->die_hash == NULL);
9139   cu->die_hash
9140     = htab_create_alloc_ex (cu->header.length / 12,
9141                             die_hash,
9142                             die_eq,
9143                             NULL,
9144                             &cu->comp_unit_obstack,
9145                             hashtab_obstack_allocate,
9146                             dummy_obstack_deallocate);
9147
9148   init_cu_die_reader (&reader_specs, cu);
9149
9150   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9151
9152   if (read_abbrevs)
9153     do_cleanups (back_to);
9154
9155   return die;
9156 }
9157
9158 /* Main entry point for reading a DIE and all children.
9159    Read the DIE and dump it if requested.  */
9160
9161 static struct die_info *
9162 read_die_and_children (const struct die_reader_specs *reader,
9163                        gdb_byte *info_ptr,
9164                        gdb_byte **new_info_ptr,
9165                        struct die_info *parent)
9166 {
9167   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9168                                                      new_info_ptr, parent);
9169
9170   if (dwarf2_die_debug)
9171     {
9172       fprintf_unfiltered (gdb_stdlog,
9173                           "\nRead die from %s of %s:\n",
9174                           (reader->cu->per_cu->debug_type_section
9175                            ? ".debug_types"
9176                            : ".debug_info"),
9177                           reader->abfd->filename);
9178       dump_die (result, dwarf2_die_debug);
9179     }
9180
9181   return result;
9182 }
9183
9184 /* Read a single die and all its descendents.  Set the die's sibling
9185    field to NULL; set other fields in the die correctly, and set all
9186    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9187    location of the info_ptr after reading all of those dies.  PARENT
9188    is the parent of the die in question.  */
9189
9190 static struct die_info *
9191 read_die_and_children_1 (const struct die_reader_specs *reader,
9192                          gdb_byte *info_ptr,
9193                          gdb_byte **new_info_ptr,
9194                          struct die_info *parent)
9195 {
9196   struct die_info *die;
9197   gdb_byte *cur_ptr;
9198   int has_children;
9199
9200   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9201   if (die == NULL)
9202     {
9203       *new_info_ptr = cur_ptr;
9204       return NULL;
9205     }
9206   store_in_ref_table (die, reader->cu);
9207
9208   if (has_children)
9209     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9210   else
9211     {
9212       die->child = NULL;
9213       *new_info_ptr = cur_ptr;
9214     }
9215
9216   die->sibling = NULL;
9217   die->parent = parent;
9218   return die;
9219 }
9220
9221 /* Read a die, all of its descendents, and all of its siblings; set
9222    all of the fields of all of the dies correctly.  Arguments are as
9223    in read_die_and_children.  */
9224
9225 static struct die_info *
9226 read_die_and_siblings (const struct die_reader_specs *reader,
9227                        gdb_byte *info_ptr,
9228                        gdb_byte **new_info_ptr,
9229                        struct die_info *parent)
9230 {
9231   struct die_info *first_die, *last_sibling;
9232   gdb_byte *cur_ptr;
9233
9234   cur_ptr = info_ptr;
9235   first_die = last_sibling = NULL;
9236
9237   while (1)
9238     {
9239       struct die_info *die
9240         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9241
9242       if (die == NULL)
9243         {
9244           *new_info_ptr = cur_ptr;
9245           return first_die;
9246         }
9247
9248       if (!first_die)
9249         first_die = die;
9250       else
9251         last_sibling->sibling = die;
9252
9253       last_sibling = die;
9254     }
9255 }
9256
9257 /* Read the die from the .debug_info section buffer.  Set DIEP to
9258    point to a newly allocated die with its information, except for its
9259    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9260    whether the die has children or not.  */
9261
9262 static gdb_byte *
9263 read_full_die (const struct die_reader_specs *reader,
9264                struct die_info **diep, gdb_byte *info_ptr,
9265                int *has_children)
9266 {
9267   unsigned int abbrev_number, bytes_read, i, offset;
9268   struct abbrev_info *abbrev;
9269   struct die_info *die;
9270   struct dwarf2_cu *cu = reader->cu;
9271   bfd *abfd = reader->abfd;
9272
9273   offset = info_ptr - reader->buffer;
9274   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9275   info_ptr += bytes_read;
9276   if (!abbrev_number)
9277     {
9278       *diep = NULL;
9279       *has_children = 0;
9280       return info_ptr;
9281     }
9282
9283   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9284   if (!abbrev)
9285     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9286            abbrev_number,
9287            bfd_get_filename (abfd));
9288
9289   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9290   die->offset = offset;
9291   die->tag = abbrev->tag;
9292   die->abbrev = abbrev_number;
9293
9294   die->num_attrs = abbrev->num_attrs;
9295
9296   for (i = 0; i < abbrev->num_attrs; ++i)
9297     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9298                                abfd, info_ptr, cu);
9299
9300   *diep = die;
9301   *has_children = abbrev->has_children;
9302   return info_ptr;
9303 }
9304
9305 /* In DWARF version 2, the description of the debugging information is
9306    stored in a separate .debug_abbrev section.  Before we read any
9307    dies from a section we read in all abbreviations and install them
9308    in a hash table.  This function also sets flags in CU describing
9309    the data found in the abbrev table.  */
9310
9311 static void
9312 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9313 {
9314   struct comp_unit_head *cu_header = &cu->header;
9315   gdb_byte *abbrev_ptr;
9316   struct abbrev_info *cur_abbrev;
9317   unsigned int abbrev_number, bytes_read, abbrev_name;
9318   unsigned int abbrev_form, hash_number;
9319   struct attr_abbrev *cur_attrs;
9320   unsigned int allocated_attrs;
9321
9322   /* Initialize dwarf2 abbrevs.  */
9323   obstack_init (&cu->abbrev_obstack);
9324   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9325                                       (ABBREV_HASH_SIZE
9326                                        * sizeof (struct abbrev_info *)));
9327   memset (cu->dwarf2_abbrevs, 0,
9328           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9329
9330   dwarf2_read_section (dwarf2_per_objfile->objfile,
9331                        &dwarf2_per_objfile->abbrev);
9332   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9333   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9334   abbrev_ptr += bytes_read;
9335
9336   allocated_attrs = ATTR_ALLOC_CHUNK;
9337   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9338
9339   /* Loop until we reach an abbrev number of 0.  */
9340   while (abbrev_number)
9341     {
9342       cur_abbrev = dwarf_alloc_abbrev (cu);
9343
9344       /* read in abbrev header */
9345       cur_abbrev->number = abbrev_number;
9346       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9347       abbrev_ptr += bytes_read;
9348       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9349       abbrev_ptr += 1;
9350
9351       if (cur_abbrev->tag == DW_TAG_namespace)
9352         cu->has_namespace_info = 1;
9353
9354       /* now read in declarations */
9355       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9356       abbrev_ptr += bytes_read;
9357       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9358       abbrev_ptr += bytes_read;
9359       while (abbrev_name)
9360         {
9361           if (cur_abbrev->num_attrs == allocated_attrs)
9362             {
9363               allocated_attrs += ATTR_ALLOC_CHUNK;
9364               cur_attrs
9365                 = xrealloc (cur_attrs, (allocated_attrs
9366                                         * sizeof (struct attr_abbrev)));
9367             }
9368
9369           /* Record whether this compilation unit might have
9370              inter-compilation-unit references.  If we don't know what form
9371              this attribute will have, then it might potentially be a
9372              DW_FORM_ref_addr, so we conservatively expect inter-CU
9373              references.  */
9374
9375           if (abbrev_form == DW_FORM_ref_addr
9376               || abbrev_form == DW_FORM_indirect)
9377             cu->has_form_ref_addr = 1;
9378
9379           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9380           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9381           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9382           abbrev_ptr += bytes_read;
9383           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9384           abbrev_ptr += bytes_read;
9385         }
9386
9387       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9388                                          (cur_abbrev->num_attrs
9389                                           * sizeof (struct attr_abbrev)));
9390       memcpy (cur_abbrev->attrs, cur_attrs,
9391               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9392
9393       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9394       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9395       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9396
9397       /* Get next abbreviation.
9398          Under Irix6 the abbreviations for a compilation unit are not
9399          always properly terminated with an abbrev number of 0.
9400          Exit loop if we encounter an abbreviation which we have
9401          already read (which means we are about to read the abbreviations
9402          for the next compile unit) or if the end of the abbreviation
9403          table is reached.  */
9404       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9405           >= dwarf2_per_objfile->abbrev.size)
9406         break;
9407       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9408       abbrev_ptr += bytes_read;
9409       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9410         break;
9411     }
9412
9413   xfree (cur_attrs);
9414 }
9415
9416 /* Release the memory used by the abbrev table for a compilation unit.  */
9417
9418 static void
9419 dwarf2_free_abbrev_table (void *ptr_to_cu)
9420 {
9421   struct dwarf2_cu *cu = ptr_to_cu;
9422
9423   obstack_free (&cu->abbrev_obstack, NULL);
9424   cu->dwarf2_abbrevs = NULL;
9425 }
9426
9427 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9428
9429 static struct abbrev_info *
9430 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9431 {
9432   unsigned int hash_number;
9433   struct abbrev_info *abbrev;
9434
9435   hash_number = number % ABBREV_HASH_SIZE;
9436   abbrev = cu->dwarf2_abbrevs[hash_number];
9437
9438   while (abbrev)
9439     {
9440       if (abbrev->number == number)
9441         return abbrev;
9442       else
9443         abbrev = abbrev->next;
9444     }
9445   return NULL;
9446 }
9447
9448 /* Returns nonzero if TAG represents a type that we might generate a partial
9449    symbol for.  */
9450
9451 static int
9452 is_type_tag_for_partial (int tag)
9453 {
9454   switch (tag)
9455     {
9456 #if 0
9457     /* Some types that would be reasonable to generate partial symbols for,
9458        that we don't at present.  */
9459     case DW_TAG_array_type:
9460     case DW_TAG_file_type:
9461     case DW_TAG_ptr_to_member_type:
9462     case DW_TAG_set_type:
9463     case DW_TAG_string_type:
9464     case DW_TAG_subroutine_type:
9465 #endif
9466     case DW_TAG_base_type:
9467     case DW_TAG_class_type:
9468     case DW_TAG_interface_type:
9469     case DW_TAG_enumeration_type:
9470     case DW_TAG_structure_type:
9471     case DW_TAG_subrange_type:
9472     case DW_TAG_typedef:
9473     case DW_TAG_union_type:
9474       return 1;
9475     default:
9476       return 0;
9477     }
9478 }
9479
9480 /* Load all DIEs that are interesting for partial symbols into memory.  */
9481
9482 static struct partial_die_info *
9483 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9484                    int building_psymtab, struct dwarf2_cu *cu)
9485 {
9486   struct partial_die_info *part_die;
9487   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9488   struct abbrev_info *abbrev;
9489   unsigned int bytes_read;
9490   unsigned int load_all = 0;
9491
9492   int nesting_level = 1;
9493
9494   parent_die = NULL;
9495   last_die = NULL;
9496
9497   if (cu->per_cu && cu->per_cu->load_all_dies)
9498     load_all = 1;
9499
9500   cu->partial_dies
9501     = htab_create_alloc_ex (cu->header.length / 12,
9502                             partial_die_hash,
9503                             partial_die_eq,
9504                             NULL,
9505                             &cu->comp_unit_obstack,
9506                             hashtab_obstack_allocate,
9507                             dummy_obstack_deallocate);
9508
9509   part_die = obstack_alloc (&cu->comp_unit_obstack,
9510                             sizeof (struct partial_die_info));
9511
9512   while (1)
9513     {
9514       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9515
9516       /* A NULL abbrev means the end of a series of children.  */
9517       if (abbrev == NULL)
9518         {
9519           if (--nesting_level == 0)
9520             {
9521               /* PART_DIE was probably the last thing allocated on the
9522                  comp_unit_obstack, so we could call obstack_free
9523                  here.  We don't do that because the waste is small,
9524                  and will be cleaned up when we're done with this
9525                  compilation unit.  This way, we're also more robust
9526                  against other users of the comp_unit_obstack.  */
9527               return first_die;
9528             }
9529           info_ptr += bytes_read;
9530           last_die = parent_die;
9531           parent_die = parent_die->die_parent;
9532           continue;
9533         }
9534
9535       /* Check for template arguments.  We never save these; if
9536          they're seen, we just mark the parent, and go on our way.  */
9537       if (parent_die != NULL
9538           && cu->language == language_cplus
9539           && (abbrev->tag == DW_TAG_template_type_param
9540               || abbrev->tag == DW_TAG_template_value_param))
9541         {
9542           parent_die->has_template_arguments = 1;
9543
9544           if (!load_all)
9545             {
9546               /* We don't need a partial DIE for the template argument.  */
9547               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9548                                        cu);
9549               continue;
9550             }
9551         }
9552
9553       /* We only recurse into subprograms looking for template arguments.
9554          Skip their other children.  */
9555       if (!load_all
9556           && cu->language == language_cplus
9557           && parent_die != NULL
9558           && parent_die->tag == DW_TAG_subprogram)
9559         {
9560           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9561           continue;
9562         }
9563
9564       /* Check whether this DIE is interesting enough to save.  Normally
9565          we would not be interested in members here, but there may be
9566          later variables referencing them via DW_AT_specification (for
9567          static members).  */
9568       if (!load_all
9569           && !is_type_tag_for_partial (abbrev->tag)
9570           && abbrev->tag != DW_TAG_constant
9571           && abbrev->tag != DW_TAG_enumerator
9572           && abbrev->tag != DW_TAG_subprogram
9573           && abbrev->tag != DW_TAG_lexical_block
9574           && abbrev->tag != DW_TAG_variable
9575           && abbrev->tag != DW_TAG_namespace
9576           && abbrev->tag != DW_TAG_module
9577           && abbrev->tag != DW_TAG_member)
9578         {
9579           /* Otherwise we skip to the next sibling, if any.  */
9580           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9581           continue;
9582         }
9583
9584       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9585                                    buffer, info_ptr, cu);
9586
9587       /* This two-pass algorithm for processing partial symbols has a
9588          high cost in cache pressure.  Thus, handle some simple cases
9589          here which cover the majority of C partial symbols.  DIEs
9590          which neither have specification tags in them, nor could have
9591          specification tags elsewhere pointing at them, can simply be
9592          processed and discarded.
9593
9594          This segment is also optional; scan_partial_symbols and
9595          add_partial_symbol will handle these DIEs if we chain
9596          them in normally.  When compilers which do not emit large
9597          quantities of duplicate debug information are more common,
9598          this code can probably be removed.  */
9599
9600       /* Any complete simple types at the top level (pretty much all
9601          of them, for a language without namespaces), can be processed
9602          directly.  */
9603       if (parent_die == NULL
9604           && part_die->has_specification == 0
9605           && part_die->is_declaration == 0
9606           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9607               || part_die->tag == DW_TAG_base_type
9608               || part_die->tag == DW_TAG_subrange_type))
9609         {
9610           if (building_psymtab && part_die->name != NULL)
9611             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9612                                  VAR_DOMAIN, LOC_TYPEDEF,
9613                                  &cu->objfile->static_psymbols,
9614                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9615           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9616           continue;
9617         }
9618
9619       /* The exception for DW_TAG_typedef with has_children above is
9620          a workaround of GCC PR debug/47510.  In the case of this complaint
9621          type_name_no_tag_or_error will error on such types later.
9622
9623          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9624          it could not find the child DIEs referenced later, this is checked
9625          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9626
9627       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9628         complaint (&symfile_complaints,
9629                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9630                      "- DIE at 0x%x [in module %s]"),
9631                    part_die->offset, cu->objfile->name);
9632
9633       /* If we're at the second level, and we're an enumerator, and
9634          our parent has no specification (meaning possibly lives in a
9635          namespace elsewhere), then we can add the partial symbol now
9636          instead of queueing it.  */
9637       if (part_die->tag == DW_TAG_enumerator
9638           && parent_die != NULL
9639           && parent_die->die_parent == NULL
9640           && parent_die->tag == DW_TAG_enumeration_type
9641           && parent_die->has_specification == 0)
9642         {
9643           if (part_die->name == NULL)
9644             complaint (&symfile_complaints,
9645                        _("malformed enumerator DIE ignored"));
9646           else if (building_psymtab)
9647             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9648                                  VAR_DOMAIN, LOC_CONST,
9649                                  (cu->language == language_cplus
9650                                   || cu->language == language_java)
9651                                  ? &cu->objfile->global_psymbols
9652                                  : &cu->objfile->static_psymbols,
9653                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9654
9655           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9656           continue;
9657         }
9658
9659       /* We'll save this DIE so link it in.  */
9660       part_die->die_parent = parent_die;
9661       part_die->die_sibling = NULL;
9662       part_die->die_child = NULL;
9663
9664       if (last_die && last_die == parent_die)
9665         last_die->die_child = part_die;
9666       else if (last_die)
9667         last_die->die_sibling = part_die;
9668
9669       last_die = part_die;
9670
9671       if (first_die == NULL)
9672         first_die = part_die;
9673
9674       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9675          find interesting need to be in the hash table, because we
9676          also have the parent/sibling/child chains; only those that we
9677          might refer to by offset later during partial symbol reading.
9678
9679          For now this means things that might have be the target of a
9680          DW_AT_specification, DW_AT_abstract_origin, or
9681          DW_AT_extension.  DW_AT_extension will refer only to
9682          namespaces; DW_AT_abstract_origin refers to functions (and
9683          many things under the function DIE, but we do not recurse
9684          into function DIEs during partial symbol reading) and
9685          possibly variables as well; DW_AT_specification refers to
9686          declarations.  Declarations ought to have the DW_AT_declaration
9687          flag.  It happens that GCC forgets to put it in sometimes, but
9688          only for functions, not for types.
9689
9690          Adding more things than necessary to the hash table is harmless
9691          except for the performance cost.  Adding too few will result in
9692          wasted time in find_partial_die, when we reread the compilation
9693          unit with load_all_dies set.  */
9694
9695       if (load_all
9696           || abbrev->tag == DW_TAG_constant
9697           || abbrev->tag == DW_TAG_subprogram
9698           || abbrev->tag == DW_TAG_variable
9699           || abbrev->tag == DW_TAG_namespace
9700           || part_die->is_declaration)
9701         {
9702           void **slot;
9703
9704           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9705                                            part_die->offset, INSERT);
9706           *slot = part_die;
9707         }
9708
9709       part_die = obstack_alloc (&cu->comp_unit_obstack,
9710                                 sizeof (struct partial_die_info));
9711
9712       /* For some DIEs we want to follow their children (if any).  For C
9713          we have no reason to follow the children of structures; for other
9714          languages we have to, so that we can get at method physnames
9715          to infer fully qualified class names, for DW_AT_specification,
9716          and for C++ template arguments.  For C++, we also look one level
9717          inside functions to find template arguments (if the name of the
9718          function does not already contain the template arguments).
9719
9720          For Ada, we need to scan the children of subprograms and lexical
9721          blocks as well because Ada allows the definition of nested
9722          entities that could be interesting for the debugger, such as
9723          nested subprograms for instance.  */
9724       if (last_die->has_children
9725           && (load_all
9726               || last_die->tag == DW_TAG_namespace
9727               || last_die->tag == DW_TAG_module
9728               || last_die->tag == DW_TAG_enumeration_type
9729               || (cu->language == language_cplus
9730                   && last_die->tag == DW_TAG_subprogram
9731                   && (last_die->name == NULL
9732                       || strchr (last_die->name, '<') == NULL))
9733               || (cu->language != language_c
9734                   && (last_die->tag == DW_TAG_class_type
9735                       || last_die->tag == DW_TAG_interface_type
9736                       || last_die->tag == DW_TAG_structure_type
9737                       || last_die->tag == DW_TAG_union_type))
9738               || (cu->language == language_ada
9739                   && (last_die->tag == DW_TAG_subprogram
9740                       || last_die->tag == DW_TAG_lexical_block))))
9741         {
9742           nesting_level++;
9743           parent_die = last_die;
9744           continue;
9745         }
9746
9747       /* Otherwise we skip to the next sibling, if any.  */
9748       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9749
9750       /* Back to the top, do it again.  */
9751     }
9752 }
9753
9754 /* Read a minimal amount of information into the minimal die structure.  */
9755
9756 static gdb_byte *
9757 read_partial_die (struct partial_die_info *part_die,
9758                   struct abbrev_info *abbrev,
9759                   unsigned int abbrev_len, bfd *abfd,
9760                   gdb_byte *buffer, gdb_byte *info_ptr,
9761                   struct dwarf2_cu *cu)
9762 {
9763   unsigned int i;
9764   struct attribute attr;
9765   int has_low_pc_attr = 0;
9766   int has_high_pc_attr = 0;
9767
9768   memset (part_die, 0, sizeof (struct partial_die_info));
9769
9770   part_die->offset = info_ptr - buffer;
9771
9772   info_ptr += abbrev_len;
9773
9774   if (abbrev == NULL)
9775     return info_ptr;
9776
9777   part_die->tag = abbrev->tag;
9778   part_die->has_children = abbrev->has_children;
9779
9780   for (i = 0; i < abbrev->num_attrs; ++i)
9781     {
9782       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9783
9784       /* Store the data if it is of an attribute we want to keep in a
9785          partial symbol table.  */
9786       switch (attr.name)
9787         {
9788         case DW_AT_name:
9789           switch (part_die->tag)
9790             {
9791             case DW_TAG_compile_unit:
9792             case DW_TAG_type_unit:
9793               /* Compilation units have a DW_AT_name that is a filename, not
9794                  a source language identifier.  */
9795             case DW_TAG_enumeration_type:
9796             case DW_TAG_enumerator:
9797               /* These tags always have simple identifiers already; no need
9798                  to canonicalize them.  */
9799               part_die->name = DW_STRING (&attr);
9800               break;
9801             default:
9802               part_die->name
9803                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9804                                             &cu->objfile->objfile_obstack);
9805               break;
9806             }
9807           break;
9808         case DW_AT_linkage_name:
9809         case DW_AT_MIPS_linkage_name:
9810           /* Note that both forms of linkage name might appear.  We
9811              assume they will be the same, and we only store the last
9812              one we see.  */
9813           if (cu->language == language_ada)
9814             part_die->name = DW_STRING (&attr);
9815           part_die->linkage_name = DW_STRING (&attr);
9816           break;
9817         case DW_AT_low_pc:
9818           has_low_pc_attr = 1;
9819           part_die->lowpc = DW_ADDR (&attr);
9820           break;
9821         case DW_AT_high_pc:
9822           has_high_pc_attr = 1;
9823           part_die->highpc = DW_ADDR (&attr);
9824           break;
9825         case DW_AT_location:
9826           /* Support the .debug_loc offsets.  */
9827           if (attr_form_is_block (&attr))
9828             {
9829                part_die->locdesc = DW_BLOCK (&attr);
9830             }
9831           else if (attr_form_is_section_offset (&attr))
9832             {
9833               dwarf2_complex_location_expr_complaint ();
9834             }
9835           else
9836             {
9837               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9838                                                      "partial symbol information");
9839             }
9840           break;
9841         case DW_AT_external:
9842           part_die->is_external = DW_UNSND (&attr);
9843           break;
9844         case DW_AT_declaration:
9845           part_die->is_declaration = DW_UNSND (&attr);
9846           break;
9847         case DW_AT_type:
9848           part_die->has_type = 1;
9849           break;
9850         case DW_AT_abstract_origin:
9851         case DW_AT_specification:
9852         case DW_AT_extension:
9853           part_die->has_specification = 1;
9854           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9855           break;
9856         case DW_AT_sibling:
9857           /* Ignore absolute siblings, they might point outside of
9858              the current compile unit.  */
9859           if (attr.form == DW_FORM_ref_addr)
9860             complaint (&symfile_complaints,
9861                        _("ignoring absolute DW_AT_sibling"));
9862           else
9863             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9864           break;
9865         case DW_AT_byte_size:
9866           part_die->has_byte_size = 1;
9867           break;
9868         case DW_AT_calling_convention:
9869           /* DWARF doesn't provide a way to identify a program's source-level
9870              entry point.  DW_AT_calling_convention attributes are only meant
9871              to describe functions' calling conventions.
9872
9873              However, because it's a necessary piece of information in
9874              Fortran, and because DW_CC_program is the only piece of debugging
9875              information whose definition refers to a 'main program' at all,
9876              several compilers have begun marking Fortran main programs with
9877              DW_CC_program --- even when those functions use the standard
9878              calling conventions.
9879
9880              So until DWARF specifies a way to provide this information and
9881              compilers pick up the new representation, we'll support this
9882              practice.  */
9883           if (DW_UNSND (&attr) == DW_CC_program
9884               && cu->language == language_fortran)
9885             {
9886               set_main_name (part_die->name);
9887
9888               /* As this DIE has a static linkage the name would be difficult
9889                  to look up later.  */
9890               language_of_main = language_fortran;
9891             }
9892           break;
9893         default:
9894           break;
9895         }
9896     }
9897
9898   if (has_low_pc_attr && has_high_pc_attr)
9899     {
9900       /* When using the GNU linker, .gnu.linkonce. sections are used to
9901          eliminate duplicate copies of functions and vtables and such.
9902          The linker will arbitrarily choose one and discard the others.
9903          The AT_*_pc values for such functions refer to local labels in
9904          these sections.  If the section from that file was discarded, the
9905          labels are not in the output, so the relocs get a value of 0.
9906          If this is a discarded function, mark the pc bounds as invalid,
9907          so that GDB will ignore it.  */
9908       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9909         {
9910           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9911
9912           complaint (&symfile_complaints,
9913                      _("DW_AT_low_pc %s is zero "
9914                        "for DIE at 0x%x [in module %s]"),
9915                      paddress (gdbarch, part_die->lowpc),
9916                      part_die->offset, cu->objfile->name);
9917         }
9918       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9919       else if (part_die->lowpc >= part_die->highpc)
9920         {
9921           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9922
9923           complaint (&symfile_complaints,
9924                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9925                        "for DIE at 0x%x [in module %s]"),
9926                      paddress (gdbarch, part_die->lowpc),
9927                      paddress (gdbarch, part_die->highpc),
9928                      part_die->offset, cu->objfile->name);
9929         }
9930       else
9931         part_die->has_pc_info = 1;
9932     }
9933
9934   return info_ptr;
9935 }
9936
9937 /* Find a cached partial DIE at OFFSET in CU.  */
9938
9939 static struct partial_die_info *
9940 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9941 {
9942   struct partial_die_info *lookup_die = NULL;
9943   struct partial_die_info part_die;
9944
9945   part_die.offset = offset;
9946   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9947
9948   return lookup_die;
9949 }
9950
9951 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9952    except in the case of .debug_types DIEs which do not reference
9953    outside their CU (they do however referencing other types via
9954    DW_FORM_ref_sig8).  */
9955
9956 static struct partial_die_info *
9957 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9958 {
9959   struct dwarf2_per_cu_data *per_cu = NULL;
9960   struct partial_die_info *pd = NULL;
9961
9962   if (cu->per_cu->debug_type_section)
9963     {
9964       pd = find_partial_die_in_comp_unit (offset, cu);
9965       if (pd != NULL)
9966         return pd;
9967       goto not_found;
9968     }
9969
9970   if (offset_in_cu_p (&cu->header, offset))
9971     {
9972       pd = find_partial_die_in_comp_unit (offset, cu);
9973       if (pd != NULL)
9974         return pd;
9975     }
9976
9977   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9978
9979   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9980     load_partial_comp_unit (per_cu, cu->objfile);
9981
9982   per_cu->cu->last_used = 0;
9983   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9984
9985   if (pd == NULL && per_cu->load_all_dies == 0)
9986     {
9987       struct cleanup *back_to;
9988       struct partial_die_info comp_unit_die;
9989       struct abbrev_info *abbrev;
9990       unsigned int bytes_read;
9991       char *info_ptr;
9992
9993       per_cu->load_all_dies = 1;
9994
9995       /* Re-read the DIEs.  */
9996       back_to = make_cleanup (null_cleanup, 0);
9997       if (per_cu->cu->dwarf2_abbrevs == NULL)
9998         {
9999           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
10000           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10001         }
10002       info_ptr = (dwarf2_per_objfile->info.buffer
10003                   + per_cu->cu->header.offset
10004                   + per_cu->cu->header.first_die_offset);
10005       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10006       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10007                                    per_cu->cu->objfile->obfd,
10008                                    dwarf2_per_objfile->info.buffer, info_ptr,
10009                                    per_cu->cu);
10010       if (comp_unit_die.has_children)
10011         load_partial_dies (per_cu->cu->objfile->obfd,
10012                            dwarf2_per_objfile->info.buffer, info_ptr,
10013                            0, per_cu->cu);
10014       do_cleanups (back_to);
10015
10016       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10017     }
10018
10019  not_found:
10020
10021   if (pd == NULL)
10022     internal_error (__FILE__, __LINE__,
10023                     _("could not find partial DIE 0x%x "
10024                       "in cache [from module %s]\n"),
10025                     offset, bfd_get_filename (cu->objfile->obfd));
10026   return pd;
10027 }
10028
10029 /* See if we can figure out if the class lives in a namespace.  We do
10030    this by looking for a member function; its demangled name will
10031    contain namespace info, if there is any.  */
10032
10033 static void
10034 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10035                                   struct dwarf2_cu *cu)
10036 {
10037   /* NOTE: carlton/2003-10-07: Getting the info this way changes
10038      what template types look like, because the demangler
10039      frequently doesn't give the same name as the debug info.  We
10040      could fix this by only using the demangled name to get the
10041      prefix (but see comment in read_structure_type).  */
10042
10043   struct partial_die_info *real_pdi;
10044   struct partial_die_info *child_pdi;
10045
10046   /* If this DIE (this DIE's specification, if any) has a parent, then
10047      we should not do this.  We'll prepend the parent's fully qualified
10048      name when we create the partial symbol.  */
10049
10050   real_pdi = struct_pdi;
10051   while (real_pdi->has_specification)
10052     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10053
10054   if (real_pdi->die_parent != NULL)
10055     return;
10056
10057   for (child_pdi = struct_pdi->die_child;
10058        child_pdi != NULL;
10059        child_pdi = child_pdi->die_sibling)
10060     {
10061       if (child_pdi->tag == DW_TAG_subprogram
10062           && child_pdi->linkage_name != NULL)
10063         {
10064           char *actual_class_name
10065             = language_class_name_from_physname (cu->language_defn,
10066                                                  child_pdi->linkage_name);
10067           if (actual_class_name != NULL)
10068             {
10069               struct_pdi->name
10070                 = obsavestring (actual_class_name,
10071                                 strlen (actual_class_name),
10072                                 &cu->objfile->objfile_obstack);
10073               xfree (actual_class_name);
10074             }
10075           break;
10076         }
10077     }
10078 }
10079
10080 /* Adjust PART_DIE before generating a symbol for it.  This function
10081    may set the is_external flag or change the DIE's name.  */
10082
10083 static void
10084 fixup_partial_die (struct partial_die_info *part_die,
10085                    struct dwarf2_cu *cu)
10086 {
10087   /* Once we've fixed up a die, there's no point in doing so again.
10088      This also avoids a memory leak if we were to call
10089      guess_partial_die_structure_name multiple times.  */
10090   if (part_die->fixup_called)
10091     return;
10092
10093   /* If we found a reference attribute and the DIE has no name, try
10094      to find a name in the referred to DIE.  */
10095
10096   if (part_die->name == NULL && part_die->has_specification)
10097     {
10098       struct partial_die_info *spec_die;
10099
10100       spec_die = find_partial_die (part_die->spec_offset, cu);
10101
10102       fixup_partial_die (spec_die, cu);
10103
10104       if (spec_die->name)
10105         {
10106           part_die->name = spec_die->name;
10107
10108           /* Copy DW_AT_external attribute if it is set.  */
10109           if (spec_die->is_external)
10110             part_die->is_external = spec_die->is_external;
10111         }
10112     }
10113
10114   /* Set default names for some unnamed DIEs.  */
10115
10116   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10117     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10118
10119   /* If there is no parent die to provide a namespace, and there are
10120      children, see if we can determine the namespace from their linkage
10121      name.
10122      NOTE: We need to do this even if cu->has_namespace_info != 0.
10123      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
10124   if (cu->language == language_cplus
10125       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10126       && part_die->die_parent == NULL
10127       && part_die->has_children
10128       && (part_die->tag == DW_TAG_class_type
10129           || part_die->tag == DW_TAG_structure_type
10130           || part_die->tag == DW_TAG_union_type))
10131     guess_partial_die_structure_name (part_die, cu);
10132
10133   /* GCC might emit a nameless struct or union that has a linkage
10134      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10135   if (part_die->name == NULL
10136       && (part_die->tag == DW_TAG_class_type
10137           || part_die->tag == DW_TAG_interface_type
10138           || part_die->tag == DW_TAG_structure_type
10139           || part_die->tag == DW_TAG_union_type)
10140       && part_die->linkage_name != NULL)
10141     {
10142       char *demangled;
10143
10144       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10145       if (demangled)
10146         {
10147           const char *base;
10148
10149           /* Strip any leading namespaces/classes, keep only the base name.
10150              DW_AT_name for named DIEs does not contain the prefixes.  */
10151           base = strrchr (demangled, ':');
10152           if (base && base > demangled && base[-1] == ':')
10153             base++;
10154           else
10155             base = demangled;
10156
10157           part_die->name = obsavestring (base, strlen (base),
10158                                          &cu->objfile->objfile_obstack);
10159           xfree (demangled);
10160         }
10161     }
10162
10163   part_die->fixup_called = 1;
10164 }
10165
10166 /* Read an attribute value described by an attribute form.  */
10167
10168 static gdb_byte *
10169 read_attribute_value (struct attribute *attr, unsigned form,
10170                       bfd *abfd, gdb_byte *info_ptr,
10171                       struct dwarf2_cu *cu)
10172 {
10173   struct comp_unit_head *cu_header = &cu->header;
10174   unsigned int bytes_read;
10175   struct dwarf_block *blk;
10176
10177   attr->form = form;
10178   switch (form)
10179     {
10180     case DW_FORM_ref_addr:
10181       if (cu->header.version == 2)
10182         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10183       else
10184         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10185                                       &cu->header, &bytes_read);
10186       info_ptr += bytes_read;
10187       break;
10188     case DW_FORM_addr:
10189       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10190       info_ptr += bytes_read;
10191       break;
10192     case DW_FORM_block2:
10193       blk = dwarf_alloc_block (cu);
10194       blk->size = read_2_bytes (abfd, info_ptr);
10195       info_ptr += 2;
10196       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10197       info_ptr += blk->size;
10198       DW_BLOCK (attr) = blk;
10199       break;
10200     case DW_FORM_block4:
10201       blk = dwarf_alloc_block (cu);
10202       blk->size = read_4_bytes (abfd, info_ptr);
10203       info_ptr += 4;
10204       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10205       info_ptr += blk->size;
10206       DW_BLOCK (attr) = blk;
10207       break;
10208     case DW_FORM_data2:
10209       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10210       info_ptr += 2;
10211       break;
10212     case DW_FORM_data4:
10213       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10214       info_ptr += 4;
10215       break;
10216     case DW_FORM_data8:
10217       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10218       info_ptr += 8;
10219       break;
10220     case DW_FORM_sec_offset:
10221       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10222       info_ptr += bytes_read;
10223       break;
10224     case DW_FORM_string:
10225       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10226       DW_STRING_IS_CANONICAL (attr) = 0;
10227       info_ptr += bytes_read;
10228       break;
10229     case DW_FORM_strp:
10230       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10231                                                &bytes_read);
10232       DW_STRING_IS_CANONICAL (attr) = 0;
10233       info_ptr += bytes_read;
10234       break;
10235     case DW_FORM_exprloc:
10236     case DW_FORM_block:
10237       blk = dwarf_alloc_block (cu);
10238       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10239       info_ptr += bytes_read;
10240       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10241       info_ptr += blk->size;
10242       DW_BLOCK (attr) = blk;
10243       break;
10244     case DW_FORM_block1:
10245       blk = dwarf_alloc_block (cu);
10246       blk->size = read_1_byte (abfd, info_ptr);
10247       info_ptr += 1;
10248       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10249       info_ptr += blk->size;
10250       DW_BLOCK (attr) = blk;
10251       break;
10252     case DW_FORM_data1:
10253       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10254       info_ptr += 1;
10255       break;
10256     case DW_FORM_flag:
10257       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10258       info_ptr += 1;
10259       break;
10260     case DW_FORM_flag_present:
10261       DW_UNSND (attr) = 1;
10262       break;
10263     case DW_FORM_sdata:
10264       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10265       info_ptr += bytes_read;
10266       break;
10267     case DW_FORM_udata:
10268       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10269       info_ptr += bytes_read;
10270       break;
10271     case DW_FORM_ref1:
10272       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10273       info_ptr += 1;
10274       break;
10275     case DW_FORM_ref2:
10276       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10277       info_ptr += 2;
10278       break;
10279     case DW_FORM_ref4:
10280       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10281       info_ptr += 4;
10282       break;
10283     case DW_FORM_ref8:
10284       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10285       info_ptr += 8;
10286       break;
10287     case DW_FORM_ref_sig8:
10288       /* Convert the signature to something we can record in DW_UNSND
10289          for later lookup.
10290          NOTE: This is NULL if the type wasn't found.  */
10291       DW_SIGNATURED_TYPE (attr) =
10292         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10293       info_ptr += 8;
10294       break;
10295     case DW_FORM_ref_udata:
10296       DW_ADDR (attr) = (cu->header.offset
10297                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10298       info_ptr += bytes_read;
10299       break;
10300     case DW_FORM_indirect:
10301       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10302       info_ptr += bytes_read;
10303       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10304       break;
10305     default:
10306       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10307              dwarf_form_name (form),
10308              bfd_get_filename (abfd));
10309     }
10310
10311   /* We have seen instances where the compiler tried to emit a byte
10312      size attribute of -1 which ended up being encoded as an unsigned
10313      0xffffffff.  Although 0xffffffff is technically a valid size value,
10314      an object of this size seems pretty unlikely so we can relatively
10315      safely treat these cases as if the size attribute was invalid and
10316      treat them as zero by default.  */
10317   if (attr->name == DW_AT_byte_size
10318       && form == DW_FORM_data4
10319       && DW_UNSND (attr) >= 0xffffffff)
10320     {
10321       complaint
10322         (&symfile_complaints,
10323          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10324          hex_string (DW_UNSND (attr)));
10325       DW_UNSND (attr) = 0;
10326     }
10327
10328   return info_ptr;
10329 }
10330
10331 /* Read an attribute described by an abbreviated attribute.  */
10332
10333 static gdb_byte *
10334 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10335                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10336 {
10337   attr->name = abbrev->name;
10338   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10339 }
10340
10341 /* Read dwarf information from a buffer.  */
10342
10343 static unsigned int
10344 read_1_byte (bfd *abfd, gdb_byte *buf)
10345 {
10346   return bfd_get_8 (abfd, buf);
10347 }
10348
10349 static int
10350 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10351 {
10352   return bfd_get_signed_8 (abfd, buf);
10353 }
10354
10355 static unsigned int
10356 read_2_bytes (bfd *abfd, gdb_byte *buf)
10357 {
10358   return bfd_get_16 (abfd, buf);
10359 }
10360
10361 static int
10362 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10363 {
10364   return bfd_get_signed_16 (abfd, buf);
10365 }
10366
10367 static unsigned int
10368 read_4_bytes (bfd *abfd, gdb_byte *buf)
10369 {
10370   return bfd_get_32 (abfd, buf);
10371 }
10372
10373 static int
10374 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10375 {
10376   return bfd_get_signed_32 (abfd, buf);
10377 }
10378
10379 static ULONGEST
10380 read_8_bytes (bfd *abfd, gdb_byte *buf)
10381 {
10382   return bfd_get_64 (abfd, buf);
10383 }
10384
10385 static CORE_ADDR
10386 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10387               unsigned int *bytes_read)
10388 {
10389   struct comp_unit_head *cu_header = &cu->header;
10390   CORE_ADDR retval = 0;
10391
10392   if (cu_header->signed_addr_p)
10393     {
10394       switch (cu_header->addr_size)
10395         {
10396         case 2:
10397           retval = bfd_get_signed_16 (abfd, buf);
10398           break;
10399         case 4:
10400           retval = bfd_get_signed_32 (abfd, buf);
10401           break;
10402         case 8:
10403           retval = bfd_get_signed_64 (abfd, buf);
10404           break;
10405         default:
10406           internal_error (__FILE__, __LINE__,
10407                           _("read_address: bad switch, signed [in module %s]"),
10408                           bfd_get_filename (abfd));
10409         }
10410     }
10411   else
10412     {
10413       switch (cu_header->addr_size)
10414         {
10415         case 2:
10416           retval = bfd_get_16 (abfd, buf);
10417           break;
10418         case 4:
10419           retval = bfd_get_32 (abfd, buf);
10420           break;
10421         case 8:
10422           retval = bfd_get_64 (abfd, buf);
10423           break;
10424         default:
10425           internal_error (__FILE__, __LINE__,
10426                           _("read_address: bad switch, "
10427                             "unsigned [in module %s]"),
10428                           bfd_get_filename (abfd));
10429         }
10430     }
10431
10432   *bytes_read = cu_header->addr_size;
10433   return retval;
10434 }
10435
10436 /* Read the initial length from a section.  The (draft) DWARF 3
10437    specification allows the initial length to take up either 4 bytes
10438    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10439    bytes describe the length and all offsets will be 8 bytes in length
10440    instead of 4.
10441
10442    An older, non-standard 64-bit format is also handled by this
10443    function.  The older format in question stores the initial length
10444    as an 8-byte quantity without an escape value.  Lengths greater
10445    than 2^32 aren't very common which means that the initial 4 bytes
10446    is almost always zero.  Since a length value of zero doesn't make
10447    sense for the 32-bit format, this initial zero can be considered to
10448    be an escape value which indicates the presence of the older 64-bit
10449    format.  As written, the code can't detect (old format) lengths
10450    greater than 4GB.  If it becomes necessary to handle lengths
10451    somewhat larger than 4GB, we could allow other small values (such
10452    as the non-sensical values of 1, 2, and 3) to also be used as
10453    escape values indicating the presence of the old format.
10454
10455    The value returned via bytes_read should be used to increment the
10456    relevant pointer after calling read_initial_length().
10457
10458    [ Note:  read_initial_length() and read_offset() are based on the
10459      document entitled "DWARF Debugging Information Format", revision
10460      3, draft 8, dated November 19, 2001.  This document was obtained
10461      from:
10462
10463         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10464
10465      This document is only a draft and is subject to change.  (So beware.)
10466
10467      Details regarding the older, non-standard 64-bit format were
10468      determined empirically by examining 64-bit ELF files produced by
10469      the SGI toolchain on an IRIX 6.5 machine.
10470
10471      - Kevin, July 16, 2002
10472    ] */
10473
10474 static LONGEST
10475 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10476 {
10477   LONGEST length = bfd_get_32 (abfd, buf);
10478
10479   if (length == 0xffffffff)
10480     {
10481       length = bfd_get_64 (abfd, buf + 4);
10482       *bytes_read = 12;
10483     }
10484   else if (length == 0)
10485     {
10486       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10487       length = bfd_get_64 (abfd, buf);
10488       *bytes_read = 8;
10489     }
10490   else
10491     {
10492       *bytes_read = 4;
10493     }
10494
10495   return length;
10496 }
10497
10498 /* Cover function for read_initial_length.
10499    Returns the length of the object at BUF, and stores the size of the
10500    initial length in *BYTES_READ and stores the size that offsets will be in
10501    *OFFSET_SIZE.
10502    If the initial length size is not equivalent to that specified in
10503    CU_HEADER then issue a complaint.
10504    This is useful when reading non-comp-unit headers.  */
10505
10506 static LONGEST
10507 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10508                                         const struct comp_unit_head *cu_header,
10509                                         unsigned int *bytes_read,
10510                                         unsigned int *offset_size)
10511 {
10512   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10513
10514   gdb_assert (cu_header->initial_length_size == 4
10515               || cu_header->initial_length_size == 8
10516               || cu_header->initial_length_size == 12);
10517
10518   if (cu_header->initial_length_size != *bytes_read)
10519     complaint (&symfile_complaints,
10520                _("intermixed 32-bit and 64-bit DWARF sections"));
10521
10522   *offset_size = (*bytes_read == 4) ? 4 : 8;
10523   return length;
10524 }
10525
10526 /* Read an offset from the data stream.  The size of the offset is
10527    given by cu_header->offset_size.  */
10528
10529 static LONGEST
10530 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10531              unsigned int *bytes_read)
10532 {
10533   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10534
10535   *bytes_read = cu_header->offset_size;
10536   return offset;
10537 }
10538
10539 /* Read an offset from the data stream.  */
10540
10541 static LONGEST
10542 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10543 {
10544   LONGEST retval = 0;
10545
10546   switch (offset_size)
10547     {
10548     case 4:
10549       retval = bfd_get_32 (abfd, buf);
10550       break;
10551     case 8:
10552       retval = bfd_get_64 (abfd, buf);
10553       break;
10554     default:
10555       internal_error (__FILE__, __LINE__,
10556                       _("read_offset_1: bad switch [in module %s]"),
10557                       bfd_get_filename (abfd));
10558     }
10559
10560   return retval;
10561 }
10562
10563 static gdb_byte *
10564 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10565 {
10566   /* If the size of a host char is 8 bits, we can return a pointer
10567      to the buffer, otherwise we have to copy the data to a buffer
10568      allocated on the temporary obstack.  */
10569   gdb_assert (HOST_CHAR_BIT == 8);
10570   return buf;
10571 }
10572
10573 static char *
10574 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10575 {
10576   /* If the size of a host char is 8 bits, we can return a pointer
10577      to the string, otherwise we have to copy the string to a buffer
10578      allocated on the temporary obstack.  */
10579   gdb_assert (HOST_CHAR_BIT == 8);
10580   if (*buf == '\0')
10581     {
10582       *bytes_read_ptr = 1;
10583       return NULL;
10584     }
10585   *bytes_read_ptr = strlen ((char *) buf) + 1;
10586   return (char *) buf;
10587 }
10588
10589 static char *
10590 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10591 {
10592   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10593   if (dwarf2_per_objfile->str.buffer == NULL)
10594     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10595            bfd_get_filename (abfd));
10596   if (str_offset >= dwarf2_per_objfile->str.size)
10597     error (_("DW_FORM_strp pointing outside of "
10598              ".debug_str section [in module %s]"),
10599            bfd_get_filename (abfd));
10600   gdb_assert (HOST_CHAR_BIT == 8);
10601   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10602     return NULL;
10603   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10604 }
10605
10606 static char *
10607 read_indirect_string (bfd *abfd, gdb_byte *buf,
10608                       const struct comp_unit_head *cu_header,
10609                       unsigned int *bytes_read_ptr)
10610 {
10611   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10612
10613   return read_indirect_string_at_offset (abfd, str_offset);
10614 }
10615
10616 static unsigned long
10617 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10618 {
10619   unsigned long result;
10620   unsigned int num_read;
10621   int i, shift;
10622   unsigned char byte;
10623
10624   result = 0;
10625   shift = 0;
10626   num_read = 0;
10627   i = 0;
10628   while (1)
10629     {
10630       byte = bfd_get_8 (abfd, buf);
10631       buf++;
10632       num_read++;
10633       result |= ((unsigned long)(byte & 127) << shift);
10634       if ((byte & 128) == 0)
10635         {
10636           break;
10637         }
10638       shift += 7;
10639     }
10640   *bytes_read_ptr = num_read;
10641   return result;
10642 }
10643
10644 static long
10645 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10646 {
10647   long result;
10648   int i, shift, num_read;
10649   unsigned char byte;
10650
10651   result = 0;
10652   shift = 0;
10653   num_read = 0;
10654   i = 0;
10655   while (1)
10656     {
10657       byte = bfd_get_8 (abfd, buf);
10658       buf++;
10659       num_read++;
10660       result |= ((long)(byte & 127) << shift);
10661       shift += 7;
10662       if ((byte & 128) == 0)
10663         {
10664           break;
10665         }
10666     }
10667   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10668     result |= -(((long)1) << shift);
10669   *bytes_read_ptr = num_read;
10670   return result;
10671 }
10672
10673 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10674
10675 static gdb_byte *
10676 skip_leb128 (bfd *abfd, gdb_byte *buf)
10677 {
10678   int byte;
10679
10680   while (1)
10681     {
10682       byte = bfd_get_8 (abfd, buf);
10683       buf++;
10684       if ((byte & 128) == 0)
10685         return buf;
10686     }
10687 }
10688
10689 static void
10690 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10691 {
10692   switch (lang)
10693     {
10694     case DW_LANG_C89:
10695     case DW_LANG_C99:
10696     case DW_LANG_C:
10697       cu->language = language_c;
10698       break;
10699     case DW_LANG_C_plus_plus:
10700       cu->language = language_cplus;
10701       break;
10702     case DW_LANG_D:
10703       cu->language = language_d;
10704       break;
10705     case DW_LANG_Fortran77:
10706     case DW_LANG_Fortran90:
10707     case DW_LANG_Fortran95:
10708       cu->language = language_fortran;
10709       break;
10710     case DW_LANG_Mips_Assembler:
10711       cu->language = language_asm;
10712       break;
10713     case DW_LANG_Java:
10714       cu->language = language_java;
10715       break;
10716     case DW_LANG_Ada83:
10717     case DW_LANG_Ada95:
10718       cu->language = language_ada;
10719       break;
10720     case DW_LANG_Modula2:
10721       cu->language = language_m2;
10722       break;
10723     case DW_LANG_Pascal83:
10724       cu->language = language_pascal;
10725       break;
10726     case DW_LANG_ObjC:
10727       cu->language = language_objc;
10728       break;
10729     case DW_LANG_Cobol74:
10730     case DW_LANG_Cobol85:
10731     default:
10732       cu->language = language_minimal;
10733       break;
10734     }
10735   cu->language_defn = language_def (cu->language);
10736 }
10737
10738 /* Return the named attribute or NULL if not there.  */
10739
10740 static struct attribute *
10741 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10742 {
10743   unsigned int i;
10744   struct attribute *spec = NULL;
10745
10746   for (i = 0; i < die->num_attrs; ++i)
10747     {
10748       if (die->attrs[i].name == name)
10749         return &die->attrs[i];
10750       if (die->attrs[i].name == DW_AT_specification
10751           || die->attrs[i].name == DW_AT_abstract_origin)
10752         spec = &die->attrs[i];
10753     }
10754
10755   if (spec)
10756     {
10757       die = follow_die_ref (die, spec, &cu);
10758       return dwarf2_attr (die, name, cu);
10759     }
10760
10761   return NULL;
10762 }
10763
10764 /* Return the named attribute or NULL if not there,
10765    but do not follow DW_AT_specification, etc.
10766    This is for use in contexts where we're reading .debug_types dies.
10767    Following DW_AT_specification, DW_AT_abstract_origin will take us
10768    back up the chain, and we want to go down.  */
10769
10770 static struct attribute *
10771 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10772                        struct dwarf2_cu *cu)
10773 {
10774   unsigned int i;
10775
10776   for (i = 0; i < die->num_attrs; ++i)
10777     if (die->attrs[i].name == name)
10778       return &die->attrs[i];
10779
10780   return NULL;
10781 }
10782
10783 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10784    and holds a non-zero value.  This function should only be used for
10785    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10786
10787 static int
10788 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10789 {
10790   struct attribute *attr = dwarf2_attr (die, name, cu);
10791
10792   return (attr && DW_UNSND (attr));
10793 }
10794
10795 static int
10796 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10797 {
10798   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10799      which value is non-zero.  However, we have to be careful with
10800      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10801      (via dwarf2_flag_true_p) follows this attribute.  So we may
10802      end up accidently finding a declaration attribute that belongs
10803      to a different DIE referenced by the specification attribute,
10804      even though the given DIE does not have a declaration attribute.  */
10805   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10806           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10807 }
10808
10809 /* Return the die giving the specification for DIE, if there is
10810    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10811    containing the return value on output.  If there is no
10812    specification, but there is an abstract origin, that is
10813    returned.  */
10814
10815 static struct die_info *
10816 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10817 {
10818   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10819                                              *spec_cu);
10820
10821   if (spec_attr == NULL)
10822     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10823
10824   if (spec_attr == NULL)
10825     return NULL;
10826   else
10827     return follow_die_ref (die, spec_attr, spec_cu);
10828 }
10829
10830 /* Free the line_header structure *LH, and any arrays and strings it
10831    refers to.
10832    NOTE: This is also used as a "cleanup" function.  */
10833
10834 static void
10835 free_line_header (struct line_header *lh)
10836 {
10837   if (lh->standard_opcode_lengths)
10838     xfree (lh->standard_opcode_lengths);
10839
10840   /* Remember that all the lh->file_names[i].name pointers are
10841      pointers into debug_line_buffer, and don't need to be freed.  */
10842   if (lh->file_names)
10843     xfree (lh->file_names);
10844
10845   /* Similarly for the include directory names.  */
10846   if (lh->include_dirs)
10847     xfree (lh->include_dirs);
10848
10849   xfree (lh);
10850 }
10851
10852 /* Add an entry to LH's include directory table.  */
10853
10854 static void
10855 add_include_dir (struct line_header *lh, char *include_dir)
10856 {
10857   /* Grow the array if necessary.  */
10858   if (lh->include_dirs_size == 0)
10859     {
10860       lh->include_dirs_size = 1; /* for testing */
10861       lh->include_dirs = xmalloc (lh->include_dirs_size
10862                                   * sizeof (*lh->include_dirs));
10863     }
10864   else if (lh->num_include_dirs >= lh->include_dirs_size)
10865     {
10866       lh->include_dirs_size *= 2;
10867       lh->include_dirs = xrealloc (lh->include_dirs,
10868                                    (lh->include_dirs_size
10869                                     * sizeof (*lh->include_dirs)));
10870     }
10871
10872   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10873 }
10874
10875 /* Add an entry to LH's file name table.  */
10876
10877 static void
10878 add_file_name (struct line_header *lh,
10879                char *name,
10880                unsigned int dir_index,
10881                unsigned int mod_time,
10882                unsigned int length)
10883 {
10884   struct file_entry *fe;
10885
10886   /* Grow the array if necessary.  */
10887   if (lh->file_names_size == 0)
10888     {
10889       lh->file_names_size = 1; /* for testing */
10890       lh->file_names = xmalloc (lh->file_names_size
10891                                 * sizeof (*lh->file_names));
10892     }
10893   else if (lh->num_file_names >= lh->file_names_size)
10894     {
10895       lh->file_names_size *= 2;
10896       lh->file_names = xrealloc (lh->file_names,
10897                                  (lh->file_names_size
10898                                   * sizeof (*lh->file_names)));
10899     }
10900
10901   fe = &lh->file_names[lh->num_file_names++];
10902   fe->name = name;
10903   fe->dir_index = dir_index;
10904   fe->mod_time = mod_time;
10905   fe->length = length;
10906   fe->included_p = 0;
10907   fe->symtab = NULL;
10908 }
10909
10910 /* Read the statement program header starting at OFFSET in
10911    .debug_line, according to the endianness of ABFD.  Return a pointer
10912    to a struct line_header, allocated using xmalloc.
10913
10914    NOTE: the strings in the include directory and file name tables of
10915    the returned object point into debug_line_buffer, and must not be
10916    freed.  */
10917
10918 static struct line_header *
10919 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10920                           struct dwarf2_cu *cu)
10921 {
10922   struct cleanup *back_to;
10923   struct line_header *lh;
10924   gdb_byte *line_ptr;
10925   unsigned int bytes_read, offset_size;
10926   int i;
10927   char *cur_dir, *cur_file;
10928
10929   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10930   if (dwarf2_per_objfile->line.buffer == NULL)
10931     {
10932       complaint (&symfile_complaints, _("missing .debug_line section"));
10933       return 0;
10934     }
10935
10936   /* Make sure that at least there's room for the total_length field.
10937      That could be 12 bytes long, but we're just going to fudge that.  */
10938   if (offset + 4 >= dwarf2_per_objfile->line.size)
10939     {
10940       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10941       return 0;
10942     }
10943
10944   lh = xmalloc (sizeof (*lh));
10945   memset (lh, 0, sizeof (*lh));
10946   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10947                           (void *) lh);
10948
10949   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10950
10951   /* Read in the header.  */
10952   lh->total_length =
10953     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10954                                             &bytes_read, &offset_size);
10955   line_ptr += bytes_read;
10956   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10957                                      + dwarf2_per_objfile->line.size))
10958     {
10959       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10960       return 0;
10961     }
10962   lh->statement_program_end = line_ptr + lh->total_length;
10963   lh->version = read_2_bytes (abfd, line_ptr);
10964   line_ptr += 2;
10965   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10966   line_ptr += offset_size;
10967   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10968   line_ptr += 1;
10969   if (lh->version >= 4)
10970     {
10971       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10972       line_ptr += 1;
10973     }
10974   else
10975     lh->maximum_ops_per_instruction = 1;
10976
10977   if (lh->maximum_ops_per_instruction == 0)
10978     {
10979       lh->maximum_ops_per_instruction = 1;
10980       complaint (&symfile_complaints,
10981                  _("invalid maximum_ops_per_instruction "
10982                    "in `.debug_line' section"));
10983     }
10984
10985   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10986   line_ptr += 1;
10987   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10988   line_ptr += 1;
10989   lh->line_range = read_1_byte (abfd, line_ptr);
10990   line_ptr += 1;
10991   lh->opcode_base = read_1_byte (abfd, line_ptr);
10992   line_ptr += 1;
10993   lh->standard_opcode_lengths
10994     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10995
10996   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10997   for (i = 1; i < lh->opcode_base; ++i)
10998     {
10999       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11000       line_ptr += 1;
11001     }
11002
11003   /* Read directory table.  */
11004   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11005     {
11006       line_ptr += bytes_read;
11007       add_include_dir (lh, cur_dir);
11008     }
11009   line_ptr += bytes_read;
11010
11011   /* Read file name table.  */
11012   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11013     {
11014       unsigned int dir_index, mod_time, length;
11015
11016       line_ptr += bytes_read;
11017       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11018       line_ptr += bytes_read;
11019       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11020       line_ptr += bytes_read;
11021       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11022       line_ptr += bytes_read;
11023
11024       add_file_name (lh, cur_file, dir_index, mod_time, length);
11025     }
11026   line_ptr += bytes_read;
11027   lh->statement_program_start = line_ptr;
11028
11029   if (line_ptr > (dwarf2_per_objfile->line.buffer
11030                   + dwarf2_per_objfile->line.size))
11031     complaint (&symfile_complaints,
11032                _("line number info header doesn't "
11033                  "fit in `.debug_line' section"));
11034
11035   discard_cleanups (back_to);
11036   return lh;
11037 }
11038
11039 /* This function exists to work around a bug in certain compilers
11040    (particularly GCC 2.95), in which the first line number marker of a
11041    function does not show up until after the prologue, right before
11042    the second line number marker.  This function shifts ADDRESS down
11043    to the beginning of the function if necessary, and is called on
11044    addresses passed to record_line.  */
11045
11046 static CORE_ADDR
11047 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
11048 {
11049   struct function_range *fn;
11050
11051   /* Find the function_range containing address.  */
11052   if (!cu->first_fn)
11053     return address;
11054
11055   if (!cu->cached_fn)
11056     cu->cached_fn = cu->first_fn;
11057
11058   fn = cu->cached_fn;
11059   while (fn)
11060     if (fn->lowpc <= address && fn->highpc > address)
11061       goto found;
11062     else
11063       fn = fn->next;
11064
11065   fn = cu->first_fn;
11066   while (fn && fn != cu->cached_fn)
11067     if (fn->lowpc <= address && fn->highpc > address)
11068       goto found;
11069     else
11070       fn = fn->next;
11071
11072   return address;
11073
11074  found:
11075   if (fn->seen_line)
11076     return address;
11077   if (address != fn->lowpc)
11078     complaint (&symfile_complaints,
11079                _("misplaced first line number at 0x%lx for '%s'"),
11080                (unsigned long) address, fn->name);
11081   fn->seen_line = 1;
11082   return fn->lowpc;
11083 }
11084
11085 /* Subroutine of dwarf_decode_lines to simplify it.
11086    Return the file name of the psymtab for included file FILE_INDEX
11087    in line header LH of PST.
11088    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11089    If space for the result is malloc'd, it will be freed by a cleanup.
11090    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
11091
11092 static char *
11093 psymtab_include_file_name (const struct line_header *lh, int file_index,
11094                            const struct partial_symtab *pst,
11095                            const char *comp_dir)
11096 {
11097   const struct file_entry fe = lh->file_names [file_index];
11098   char *include_name = fe.name;
11099   char *include_name_to_compare = include_name;
11100   char *dir_name = NULL;
11101   const char *pst_filename;
11102   char *copied_name = NULL;
11103   int file_is_pst;
11104
11105   if (fe.dir_index)
11106     dir_name = lh->include_dirs[fe.dir_index - 1];
11107
11108   if (!IS_ABSOLUTE_PATH (include_name)
11109       && (dir_name != NULL || comp_dir != NULL))
11110     {
11111       /* Avoid creating a duplicate psymtab for PST.
11112          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11113          Before we do the comparison, however, we need to account
11114          for DIR_NAME and COMP_DIR.
11115          First prepend dir_name (if non-NULL).  If we still don't
11116          have an absolute path prepend comp_dir (if non-NULL).
11117          However, the directory we record in the include-file's
11118          psymtab does not contain COMP_DIR (to match the
11119          corresponding symtab(s)).
11120
11121          Example:
11122
11123          bash$ cd /tmp
11124          bash$ gcc -g ./hello.c
11125          include_name = "hello.c"
11126          dir_name = "."
11127          DW_AT_comp_dir = comp_dir = "/tmp"
11128          DW_AT_name = "./hello.c"  */
11129
11130       if (dir_name != NULL)
11131         {
11132           include_name = concat (dir_name, SLASH_STRING,
11133                                  include_name, (char *)NULL);
11134           include_name_to_compare = include_name;
11135           make_cleanup (xfree, include_name);
11136         }
11137       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11138         {
11139           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11140                                             include_name, (char *)NULL);
11141         }
11142     }
11143
11144   pst_filename = pst->filename;
11145   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11146     {
11147       copied_name = concat (pst->dirname, SLASH_STRING,
11148                             pst_filename, (char *)NULL);
11149       pst_filename = copied_name;
11150     }
11151
11152   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11153
11154   if (include_name_to_compare != include_name)
11155     xfree (include_name_to_compare);
11156   if (copied_name != NULL)
11157     xfree (copied_name);
11158
11159   if (file_is_pst)
11160     return NULL;
11161   return include_name;
11162 }
11163
11164 /* Ignore this record_line request.  */
11165
11166 static void
11167 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11168 {
11169   return;
11170 }
11171
11172 /* Decode the Line Number Program (LNP) for the given line_header
11173    structure and CU.  The actual information extracted and the type
11174    of structures created from the LNP depends on the value of PST.
11175
11176    1. If PST is NULL, then this procedure uses the data from the program
11177       to create all necessary symbol tables, and their linetables.
11178
11179    2. If PST is not NULL, this procedure reads the program to determine
11180       the list of files included by the unit represented by PST, and
11181       builds all the associated partial symbol tables.
11182
11183    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11184    It is used for relative paths in the line table.
11185    NOTE: When processing partial symtabs (pst != NULL),
11186    comp_dir == pst->dirname.
11187
11188    NOTE: It is important that psymtabs have the same file name (via strcmp)
11189    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11190    symtab we don't use it in the name of the psymtabs we create.
11191    E.g. expand_line_sal requires this when finding psymtabs to expand.
11192    A good testcase for this is mb-inline.exp.  */
11193
11194 static void
11195 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11196                     struct dwarf2_cu *cu, struct partial_symtab *pst)
11197 {
11198   gdb_byte *line_ptr, *extended_end;
11199   gdb_byte *line_end;
11200   unsigned int bytes_read, extended_len;
11201   unsigned char op_code, extended_op, adj_opcode;
11202   CORE_ADDR baseaddr;
11203   struct objfile *objfile = cu->objfile;
11204   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11205   const int decode_for_pst_p = (pst != NULL);
11206   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11207   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11208     = record_line;
11209
11210   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11211
11212   line_ptr = lh->statement_program_start;
11213   line_end = lh->statement_program_end;
11214
11215   /* Read the statement sequences until there's nothing left.  */
11216   while (line_ptr < line_end)
11217     {
11218       /* state machine registers  */
11219       CORE_ADDR address = 0;
11220       unsigned int file = 1;
11221       unsigned int line = 1;
11222       unsigned int column = 0;
11223       int is_stmt = lh->default_is_stmt;
11224       int basic_block = 0;
11225       int end_sequence = 0;
11226       CORE_ADDR addr;
11227       unsigned char op_index = 0;
11228
11229       if (!decode_for_pst_p && lh->num_file_names >= file)
11230         {
11231           /* Start a subfile for the current file of the state machine.  */
11232           /* lh->include_dirs and lh->file_names are 0-based, but the
11233              directory and file name numbers in the statement program
11234              are 1-based.  */
11235           struct file_entry *fe = &lh->file_names[file - 1];
11236           char *dir = NULL;
11237
11238           if (fe->dir_index)
11239             dir = lh->include_dirs[fe->dir_index - 1];
11240
11241           dwarf2_start_subfile (fe->name, dir, comp_dir);
11242         }
11243
11244       /* Decode the table.  */
11245       while (!end_sequence)
11246         {
11247           op_code = read_1_byte (abfd, line_ptr);
11248           line_ptr += 1;
11249           if (line_ptr > line_end)
11250             {
11251               dwarf2_debug_line_missing_end_sequence_complaint ();
11252               break;
11253             }
11254
11255           if (op_code >= lh->opcode_base)
11256             {
11257               /* Special operand.  */
11258               adj_opcode = op_code - lh->opcode_base;
11259               address += (((op_index + (adj_opcode / lh->line_range))
11260                            / lh->maximum_ops_per_instruction)
11261                           * lh->minimum_instruction_length);
11262               op_index = ((op_index + (adj_opcode / lh->line_range))
11263                           % lh->maximum_ops_per_instruction);
11264               line += lh->line_base + (adj_opcode % lh->line_range);
11265               if (lh->num_file_names < file || file == 0)
11266                 dwarf2_debug_line_missing_file_complaint ();
11267               /* For now we ignore lines not starting on an
11268                  instruction boundary.  */
11269               else if (op_index == 0)
11270                 {
11271                   lh->file_names[file - 1].included_p = 1;
11272                   if (!decode_for_pst_p && is_stmt)
11273                     {
11274                       if (last_subfile != current_subfile)
11275                         {
11276                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11277                           if (last_subfile)
11278                             (*p_record_line) (last_subfile, 0, addr);
11279                           last_subfile = current_subfile;
11280                         }
11281                       /* Append row to matrix using current values.  */
11282                       addr = check_cu_functions (address, cu);
11283                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
11284                       (*p_record_line) (current_subfile, line, addr);
11285                     }
11286                 }
11287               basic_block = 0;
11288             }
11289           else switch (op_code)
11290             {
11291             case DW_LNS_extended_op:
11292               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11293                                                    &bytes_read);
11294               line_ptr += bytes_read;
11295               extended_end = line_ptr + extended_len;
11296               extended_op = read_1_byte (abfd, line_ptr);
11297               line_ptr += 1;
11298               switch (extended_op)
11299                 {
11300                 case DW_LNE_end_sequence:
11301                   p_record_line = record_line;
11302                   end_sequence = 1;
11303                   break;
11304                 case DW_LNE_set_address:
11305                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11306
11307                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11308                     {
11309                       /* This line table is for a function which has been
11310                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11311
11312                       long line_offset
11313                         = line_ptr - dwarf2_per_objfile->line.buffer;
11314
11315                       complaint (&symfile_complaints,
11316                                  _(".debug_line address at offset 0x%lx is 0 "
11317                                    "[in module %s]"),
11318                                  line_offset, cu->objfile->name);
11319                       p_record_line = noop_record_line;
11320                     }
11321
11322                   op_index = 0;
11323                   line_ptr += bytes_read;
11324                   address += baseaddr;
11325                   break;
11326                 case DW_LNE_define_file:
11327                   {
11328                     char *cur_file;
11329                     unsigned int dir_index, mod_time, length;
11330
11331                     cur_file = read_direct_string (abfd, line_ptr,
11332                                                    &bytes_read);
11333                     line_ptr += bytes_read;
11334                     dir_index =
11335                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11336                     line_ptr += bytes_read;
11337                     mod_time =
11338                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11339                     line_ptr += bytes_read;
11340                     length =
11341                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11342                     line_ptr += bytes_read;
11343                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11344                   }
11345                   break;
11346                 case DW_LNE_set_discriminator:
11347                   /* The discriminator is not interesting to the debugger;
11348                      just ignore it.  */
11349                   line_ptr = extended_end;
11350                   break;
11351                 default:
11352                   complaint (&symfile_complaints,
11353                              _("mangled .debug_line section"));
11354                   return;
11355                 }
11356               /* Make sure that we parsed the extended op correctly.  If e.g.
11357                  we expected a different address size than the producer used,
11358                  we may have read the wrong number of bytes.  */
11359               if (line_ptr != extended_end)
11360                 {
11361                   complaint (&symfile_complaints,
11362                              _("mangled .debug_line section"));
11363                   return;
11364                 }
11365               break;
11366             case DW_LNS_copy:
11367               if (lh->num_file_names < file || file == 0)
11368                 dwarf2_debug_line_missing_file_complaint ();
11369               else
11370                 {
11371                   lh->file_names[file - 1].included_p = 1;
11372                   if (!decode_for_pst_p && is_stmt)
11373                     {
11374                       if (last_subfile != current_subfile)
11375                         {
11376                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11377                           if (last_subfile)
11378                             (*p_record_line) (last_subfile, 0, addr);
11379                           last_subfile = current_subfile;
11380                         }
11381                       addr = check_cu_functions (address, cu);
11382                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
11383                       (*p_record_line) (current_subfile, line, addr);
11384                     }
11385                 }
11386               basic_block = 0;
11387               break;
11388             case DW_LNS_advance_pc:
11389               {
11390                 CORE_ADDR adjust
11391                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11392
11393                 address += (((op_index + adjust)
11394                              / lh->maximum_ops_per_instruction)
11395                             * lh->minimum_instruction_length);
11396                 op_index = ((op_index + adjust)
11397                             % lh->maximum_ops_per_instruction);
11398                 line_ptr += bytes_read;
11399               }
11400               break;
11401             case DW_LNS_advance_line:
11402               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11403               line_ptr += bytes_read;
11404               break;
11405             case DW_LNS_set_file:
11406               {
11407                 /* The arrays lh->include_dirs and lh->file_names are
11408                    0-based, but the directory and file name numbers in
11409                    the statement program are 1-based.  */
11410                 struct file_entry *fe;
11411                 char *dir = NULL;
11412
11413                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11414                 line_ptr += bytes_read;
11415                 if (lh->num_file_names < file || file == 0)
11416                   dwarf2_debug_line_missing_file_complaint ();
11417                 else
11418                   {
11419                     fe = &lh->file_names[file - 1];
11420                     if (fe->dir_index)
11421                       dir = lh->include_dirs[fe->dir_index - 1];
11422                     if (!decode_for_pst_p)
11423                       {
11424                         last_subfile = current_subfile;
11425                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11426                       }
11427                   }
11428               }
11429               break;
11430             case DW_LNS_set_column:
11431               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11432               line_ptr += bytes_read;
11433               break;
11434             case DW_LNS_negate_stmt:
11435               is_stmt = (!is_stmt);
11436               break;
11437             case DW_LNS_set_basic_block:
11438               basic_block = 1;
11439               break;
11440             /* Add to the address register of the state machine the
11441                address increment value corresponding to special opcode
11442                255.  I.e., this value is scaled by the minimum
11443                instruction length since special opcode 255 would have
11444                scaled the increment.  */
11445             case DW_LNS_const_add_pc:
11446               {
11447                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11448
11449                 address += (((op_index + adjust)
11450                              / lh->maximum_ops_per_instruction)
11451                             * lh->minimum_instruction_length);
11452                 op_index = ((op_index + adjust)
11453                             % lh->maximum_ops_per_instruction);
11454               }
11455               break;
11456             case DW_LNS_fixed_advance_pc:
11457               address += read_2_bytes (abfd, line_ptr);
11458               op_index = 0;
11459               line_ptr += 2;
11460               break;
11461             default:
11462               {
11463                 /* Unknown standard opcode, ignore it.  */
11464                 int i;
11465
11466                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11467                   {
11468                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11469                     line_ptr += bytes_read;
11470                   }
11471               }
11472             }
11473         }
11474       if (lh->num_file_names < file || file == 0)
11475         dwarf2_debug_line_missing_file_complaint ();
11476       else
11477         {
11478           lh->file_names[file - 1].included_p = 1;
11479           if (!decode_for_pst_p)
11480             {
11481               addr = gdbarch_addr_bits_remove (gdbarch, address);
11482               (*p_record_line) (current_subfile, 0, addr);
11483             }
11484         }
11485     }
11486
11487   if (decode_for_pst_p)
11488     {
11489       int file_index;
11490
11491       /* Now that we're done scanning the Line Header Program, we can
11492          create the psymtab of each included file.  */
11493       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11494         if (lh->file_names[file_index].included_p == 1)
11495           {
11496             char *include_name =
11497               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11498             if (include_name != NULL)
11499               dwarf2_create_include_psymtab (include_name, pst, objfile);
11500           }
11501     }
11502   else
11503     {
11504       /* Make sure a symtab is created for every file, even files
11505          which contain only variables (i.e. no code with associated
11506          line numbers).  */
11507
11508       int i;
11509       struct file_entry *fe;
11510
11511       for (i = 0; i < lh->num_file_names; i++)
11512         {
11513           char *dir = NULL;
11514
11515           fe = &lh->file_names[i];
11516           if (fe->dir_index)
11517             dir = lh->include_dirs[fe->dir_index - 1];
11518           dwarf2_start_subfile (fe->name, dir, comp_dir);
11519
11520           /* Skip the main file; we don't need it, and it must be
11521              allocated last, so that it will show up before the
11522              non-primary symtabs in the objfile's symtab list.  */
11523           if (current_subfile == first_subfile)
11524             continue;
11525
11526           if (current_subfile->symtab == NULL)
11527             current_subfile->symtab = allocate_symtab (current_subfile->name,
11528                                                        cu->objfile);
11529           fe->symtab = current_subfile->symtab;
11530         }
11531     }
11532 }
11533
11534 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11535    DIRNAME the name of the source directory which contains FILENAME
11536    or NULL if not known.  COMP_DIR is the compilation directory for the
11537    linetable's compilation unit or NULL if not known.
11538    This routine tries to keep line numbers from identical absolute and
11539    relative file names in a common subfile.
11540
11541    Using the `list' example from the GDB testsuite, which resides in
11542    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11543    of /srcdir/list0.c yields the following debugging information for list0.c:
11544
11545    DW_AT_name:          /srcdir/list0.c
11546    DW_AT_comp_dir:              /compdir
11547    files.files[0].name: list0.h
11548    files.files[0].dir:  /srcdir
11549    files.files[1].name: list0.c
11550    files.files[1].dir:  /srcdir
11551
11552    The line number information for list0.c has to end up in a single
11553    subfile, so that `break /srcdir/list0.c:1' works as expected.
11554    start_subfile will ensure that this happens provided that we pass the
11555    concatenation of files.files[1].dir and files.files[1].name as the
11556    subfile's name.  */
11557
11558 static void
11559 dwarf2_start_subfile (char *filename, const char *dirname,
11560                       const char *comp_dir)
11561 {
11562   char *fullname;
11563
11564   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11565      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11566      second argument to start_subfile.  To be consistent, we do the
11567      same here.  In order not to lose the line information directory,
11568      we concatenate it to the filename when it makes sense.
11569      Note that the Dwarf3 standard says (speaking of filenames in line
11570      information): ``The directory index is ignored for file names
11571      that represent full path names''.  Thus ignoring dirname in the
11572      `else' branch below isn't an issue.  */
11573
11574   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11575     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11576   else
11577     fullname = filename;
11578
11579   start_subfile (fullname, comp_dir);
11580
11581   if (fullname != filename)
11582     xfree (fullname);
11583 }
11584
11585 static void
11586 var_decode_location (struct attribute *attr, struct symbol *sym,
11587                      struct dwarf2_cu *cu)
11588 {
11589   struct objfile *objfile = cu->objfile;
11590   struct comp_unit_head *cu_header = &cu->header;
11591
11592   /* NOTE drow/2003-01-30: There used to be a comment and some special
11593      code here to turn a symbol with DW_AT_external and a
11594      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11595      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11596      with some versions of binutils) where shared libraries could have
11597      relocations against symbols in their debug information - the
11598      minimal symbol would have the right address, but the debug info
11599      would not.  It's no longer necessary, because we will explicitly
11600      apply relocations when we read in the debug information now.  */
11601
11602   /* A DW_AT_location attribute with no contents indicates that a
11603      variable has been optimized away.  */
11604   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11605     {
11606       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11607       return;
11608     }
11609
11610   /* Handle one degenerate form of location expression specially, to
11611      preserve GDB's previous behavior when section offsets are
11612      specified.  If this is just a DW_OP_addr then mark this symbol
11613      as LOC_STATIC.  */
11614
11615   if (attr_form_is_block (attr)
11616       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11617       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11618     {
11619       unsigned int dummy;
11620
11621       SYMBOL_VALUE_ADDRESS (sym) =
11622         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11623       SYMBOL_CLASS (sym) = LOC_STATIC;
11624       fixup_symbol_section (sym, objfile);
11625       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11626                                               SYMBOL_SECTION (sym));
11627       return;
11628     }
11629
11630   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11631      expression evaluator, and use LOC_COMPUTED only when necessary
11632      (i.e. when the value of a register or memory location is
11633      referenced, or a thread-local block, etc.).  Then again, it might
11634      not be worthwhile.  I'm assuming that it isn't unless performance
11635      or memory numbers show me otherwise.  */
11636
11637   dwarf2_symbol_mark_computed (attr, sym, cu);
11638   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11639
11640   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11641     cu->has_loclist = 1;
11642 }
11643
11644 /* Given a pointer to a DWARF information entry, figure out if we need
11645    to make a symbol table entry for it, and if so, create a new entry
11646    and return a pointer to it.
11647    If TYPE is NULL, determine symbol type from the die, otherwise
11648    used the passed type.
11649    If SPACE is not NULL, use it to hold the new symbol.  If it is
11650    NULL, allocate a new symbol on the objfile's obstack.  */
11651
11652 static struct symbol *
11653 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11654                  struct symbol *space)
11655 {
11656   struct objfile *objfile = cu->objfile;
11657   struct symbol *sym = NULL;
11658   char *name;
11659   struct attribute *attr = NULL;
11660   struct attribute *attr2 = NULL;
11661   CORE_ADDR baseaddr;
11662   struct pending **list_to_add = NULL;
11663
11664   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11665
11666   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11667
11668   name = dwarf2_name (die, cu);
11669   if (name)
11670     {
11671       const char *linkagename;
11672       int suppress_add = 0;
11673
11674       if (space)
11675         sym = space;
11676       else
11677         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11678       OBJSTAT (objfile, n_syms++);
11679
11680       /* Cache this symbol's name and the name's demangled form (if any).  */
11681       SYMBOL_SET_LANGUAGE (sym, cu->language);
11682       linkagename = dwarf2_physname (name, die, cu);
11683       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11684
11685       /* Fortran does not have mangling standard and the mangling does differ
11686          between gfortran, iFort etc.  */
11687       if (cu->language == language_fortran
11688           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11689         symbol_set_demangled_name (&(sym->ginfo),
11690                                    (char *) dwarf2_full_name (name, die, cu),
11691                                    NULL);
11692
11693       /* Default assumptions.
11694          Use the passed type or decode it from the die.  */
11695       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11696       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11697       if (type != NULL)
11698         SYMBOL_TYPE (sym) = type;
11699       else
11700         SYMBOL_TYPE (sym) = die_type (die, cu);
11701       attr = dwarf2_attr (die,
11702                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11703                           cu);
11704       if (attr)
11705         {
11706           SYMBOL_LINE (sym) = DW_UNSND (attr);
11707         }
11708
11709       attr = dwarf2_attr (die,
11710                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11711                           cu);
11712       if (attr)
11713         {
11714           int file_index = DW_UNSND (attr);
11715
11716           if (cu->line_header == NULL
11717               || file_index > cu->line_header->num_file_names)
11718             complaint (&symfile_complaints,
11719                        _("file index out of range"));
11720           else if (file_index > 0)
11721             {
11722               struct file_entry *fe;
11723
11724               fe = &cu->line_header->file_names[file_index - 1];
11725               SYMBOL_SYMTAB (sym) = fe->symtab;
11726             }
11727         }
11728
11729       switch (die->tag)
11730         {
11731         case DW_TAG_label:
11732           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11733           if (attr)
11734             {
11735               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11736             }
11737           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11738           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11739           SYMBOL_CLASS (sym) = LOC_LABEL;
11740           add_symbol_to_list (sym, cu->list_in_scope);
11741           break;
11742         case DW_TAG_subprogram:
11743           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11744              finish_block.  */
11745           SYMBOL_CLASS (sym) = LOC_BLOCK;
11746           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11747           if ((attr2 && (DW_UNSND (attr2) != 0))
11748               || cu->language == language_ada)
11749             {
11750               /* Subprograms marked external are stored as a global symbol.
11751                  Ada subprograms, whether marked external or not, are always
11752                  stored as a global symbol, because we want to be able to
11753                  access them globally.  For instance, we want to be able
11754                  to break on a nested subprogram without having to
11755                  specify the context.  */
11756               list_to_add = &global_symbols;
11757             }
11758           else
11759             {
11760               list_to_add = cu->list_in_scope;
11761             }
11762           break;
11763         case DW_TAG_inlined_subroutine:
11764           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11765              finish_block.  */
11766           SYMBOL_CLASS (sym) = LOC_BLOCK;
11767           SYMBOL_INLINED (sym) = 1;
11768           /* Do not add the symbol to any lists.  It will be found via
11769              BLOCK_FUNCTION from the blockvector.  */
11770           break;
11771         case DW_TAG_template_value_param:
11772           suppress_add = 1;
11773           /* Fall through.  */
11774         case DW_TAG_constant:
11775         case DW_TAG_variable:
11776         case DW_TAG_member:
11777           /* Compilation with minimal debug info may result in
11778              variables with missing type entries.  Change the
11779              misleading `void' type to something sensible.  */
11780           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11781             SYMBOL_TYPE (sym)
11782               = objfile_type (objfile)->nodebug_data_symbol;
11783
11784           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11785           /* In the case of DW_TAG_member, we should only be called for
11786              static const members.  */
11787           if (die->tag == DW_TAG_member)
11788             {
11789               /* dwarf2_add_field uses die_is_declaration,
11790                  so we do the same.  */
11791               gdb_assert (die_is_declaration (die, cu));
11792               gdb_assert (attr);
11793             }
11794           if (attr)
11795             {
11796               dwarf2_const_value (attr, sym, cu);
11797               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11798               if (!suppress_add)
11799                 {
11800                   if (attr2 && (DW_UNSND (attr2) != 0))
11801                     list_to_add = &global_symbols;
11802                   else
11803                     list_to_add = cu->list_in_scope;
11804                 }
11805               break;
11806             }
11807           attr = dwarf2_attr (die, DW_AT_location, cu);
11808           if (attr)
11809             {
11810               var_decode_location (attr, sym, cu);
11811               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11812               if (SYMBOL_CLASS (sym) == LOC_STATIC
11813                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11814                   && !dwarf2_per_objfile->has_section_at_zero)
11815                 {
11816                   /* When a static variable is eliminated by the linker,
11817                      the corresponding debug information is not stripped
11818                      out, but the variable address is set to null;
11819                      do not add such variables into symbol table.  */
11820                 }
11821               else if (attr2 && (DW_UNSND (attr2) != 0))
11822                 {
11823                   /* Workaround gfortran PR debug/40040 - it uses
11824                      DW_AT_location for variables in -fPIC libraries which may
11825                      get overriden by other libraries/executable and get
11826                      a different address.  Resolve it by the minimal symbol
11827                      which may come from inferior's executable using copy
11828                      relocation.  Make this workaround only for gfortran as for
11829                      other compilers GDB cannot guess the minimal symbol
11830                      Fortran mangling kind.  */
11831                   if (cu->language == language_fortran && die->parent
11832                       && die->parent->tag == DW_TAG_module
11833                       && cu->producer
11834                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11835                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11836
11837                   /* A variable with DW_AT_external is never static,
11838                      but it may be block-scoped.  */
11839                   list_to_add = (cu->list_in_scope == &file_symbols
11840                                  ? &global_symbols : cu->list_in_scope);
11841                 }
11842               else
11843                 list_to_add = cu->list_in_scope;
11844             }
11845           else
11846             {
11847               /* We do not know the address of this symbol.
11848                  If it is an external symbol and we have type information
11849                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11850                  The address of the variable will then be determined from
11851                  the minimal symbol table whenever the variable is
11852                  referenced.  */
11853               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11854               if (attr2 && (DW_UNSND (attr2) != 0)
11855                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11856                 {
11857                   /* A variable with DW_AT_external is never static, but it
11858                      may be block-scoped.  */
11859                   list_to_add = (cu->list_in_scope == &file_symbols
11860                                  ? &global_symbols : cu->list_in_scope);
11861
11862                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11863                 }
11864               else if (!die_is_declaration (die, cu))
11865                 {
11866                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11867                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11868                   if (!suppress_add)
11869                     list_to_add = cu->list_in_scope;
11870                 }
11871             }
11872           break;
11873         case DW_TAG_formal_parameter:
11874           /* If we are inside a function, mark this as an argument.  If
11875              not, we might be looking at an argument to an inlined function
11876              when we do not have enough information to show inlined frames;
11877              pretend it's a local variable in that case so that the user can
11878              still see it.  */
11879           if (context_stack_depth > 0
11880               && context_stack[context_stack_depth - 1].name != NULL)
11881             SYMBOL_IS_ARGUMENT (sym) = 1;
11882           attr = dwarf2_attr (die, DW_AT_location, cu);
11883           if (attr)
11884             {
11885               var_decode_location (attr, sym, cu);
11886             }
11887           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11888           if (attr)
11889             {
11890               dwarf2_const_value (attr, sym, cu);
11891             }
11892
11893           list_to_add = cu->list_in_scope;
11894           break;
11895         case DW_TAG_unspecified_parameters:
11896           /* From varargs functions; gdb doesn't seem to have any
11897              interest in this information, so just ignore it for now.
11898              (FIXME?) */
11899           break;
11900         case DW_TAG_template_type_param:
11901           suppress_add = 1;
11902           /* Fall through.  */
11903         case DW_TAG_class_type:
11904         case DW_TAG_interface_type:
11905         case DW_TAG_structure_type:
11906         case DW_TAG_union_type:
11907         case DW_TAG_set_type:
11908         case DW_TAG_enumeration_type:
11909           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11910           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11911
11912           {
11913             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11914                really ever be static objects: otherwise, if you try
11915                to, say, break of a class's method and you're in a file
11916                which doesn't mention that class, it won't work unless
11917                the check for all static symbols in lookup_symbol_aux
11918                saves you.  See the OtherFileClass tests in
11919                gdb.c++/namespace.exp.  */
11920
11921             if (!suppress_add)
11922               {
11923                 list_to_add = (cu->list_in_scope == &file_symbols
11924                                && (cu->language == language_cplus
11925                                    || cu->language == language_java)
11926                                ? &global_symbols : cu->list_in_scope);
11927
11928                 /* The semantics of C++ state that "struct foo {
11929                    ... }" also defines a typedef for "foo".  A Java
11930                    class declaration also defines a typedef for the
11931                    class.  */
11932                 if (cu->language == language_cplus
11933                     || cu->language == language_java
11934                     || cu->language == language_ada)
11935                   {
11936                     /* The symbol's name is already allocated along
11937                        with this objfile, so we don't need to
11938                        duplicate it for the type.  */
11939                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11940                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11941                   }
11942               }
11943           }
11944           break;
11945         case DW_TAG_typedef:
11946           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11947           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11948           list_to_add = cu->list_in_scope;
11949           break;
11950         case DW_TAG_base_type:
11951         case DW_TAG_subrange_type:
11952           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11953           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11954           list_to_add = cu->list_in_scope;
11955           break;
11956         case DW_TAG_enumerator:
11957           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11958           if (attr)
11959             {
11960               dwarf2_const_value (attr, sym, cu);
11961             }
11962           {
11963             /* NOTE: carlton/2003-11-10: See comment above in the
11964                DW_TAG_class_type, etc. block.  */
11965
11966             list_to_add = (cu->list_in_scope == &file_symbols
11967                            && (cu->language == language_cplus
11968                                || cu->language == language_java)
11969                            ? &global_symbols : cu->list_in_scope);
11970           }
11971           break;
11972         case DW_TAG_namespace:
11973           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11974           list_to_add = &global_symbols;
11975           break;
11976         default:
11977           /* Not a tag we recognize.  Hopefully we aren't processing
11978              trash data, but since we must specifically ignore things
11979              we don't recognize, there is nothing else we should do at
11980              this point.  */
11981           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11982                      dwarf_tag_name (die->tag));
11983           break;
11984         }
11985
11986       if (suppress_add)
11987         {
11988           sym->hash_next = objfile->template_symbols;
11989           objfile->template_symbols = sym;
11990           list_to_add = NULL;
11991         }
11992
11993       if (list_to_add != NULL)
11994         add_symbol_to_list (sym, list_to_add);
11995
11996       /* For the benefit of old versions of GCC, check for anonymous
11997          namespaces based on the demangled name.  */
11998       if (!processing_has_namespace_info
11999           && cu->language == language_cplus)
12000         cp_scan_for_anonymous_namespaces (sym, objfile);
12001     }
12002   return (sym);
12003 }
12004
12005 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
12006
12007 static struct symbol *
12008 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12009 {
12010   return new_symbol_full (die, type, cu, NULL);
12011 }
12012
12013 /* Given an attr with a DW_FORM_dataN value in host byte order,
12014    zero-extend it as appropriate for the symbol's type.  The DWARF
12015    standard (v4) is not entirely clear about the meaning of using
12016    DW_FORM_dataN for a constant with a signed type, where the type is
12017    wider than the data.  The conclusion of a discussion on the DWARF
12018    list was that this is unspecified.  We choose to always zero-extend
12019    because that is the interpretation long in use by GCC.  */
12020
12021 static gdb_byte *
12022 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12023                          const char *name, struct obstack *obstack,
12024                          struct dwarf2_cu *cu, long *value, int bits)
12025 {
12026   struct objfile *objfile = cu->objfile;
12027   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12028                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12029   LONGEST l = DW_UNSND (attr);
12030
12031   if (bits < sizeof (*value) * 8)
12032     {
12033       l &= ((LONGEST) 1 << bits) - 1;
12034       *value = l;
12035     }
12036   else if (bits == sizeof (*value) * 8)
12037     *value = l;
12038   else
12039     {
12040       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12041       store_unsigned_integer (bytes, bits / 8, byte_order, l);
12042       return bytes;
12043     }
12044
12045   return NULL;
12046 }
12047
12048 /* Read a constant value from an attribute.  Either set *VALUE, or if
12049    the value does not fit in *VALUE, set *BYTES - either already
12050    allocated on the objfile obstack, or newly allocated on OBSTACK,
12051    or, set *BATON, if we translated the constant to a location
12052    expression.  */
12053
12054 static void
12055 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12056                          const char *name, struct obstack *obstack,
12057                          struct dwarf2_cu *cu,
12058                          long *value, gdb_byte **bytes,
12059                          struct dwarf2_locexpr_baton **baton)
12060 {
12061   struct objfile *objfile = cu->objfile;
12062   struct comp_unit_head *cu_header = &cu->header;
12063   struct dwarf_block *blk;
12064   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12065                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12066
12067   *value = 0;
12068   *bytes = NULL;
12069   *baton = NULL;
12070
12071   switch (attr->form)
12072     {
12073     case DW_FORM_addr:
12074       {
12075         gdb_byte *data;
12076
12077         if (TYPE_LENGTH (type) != cu_header->addr_size)
12078           dwarf2_const_value_length_mismatch_complaint (name,
12079                                                         cu_header->addr_size,
12080                                                         TYPE_LENGTH (type));
12081         /* Symbols of this form are reasonably rare, so we just
12082            piggyback on the existing location code rather than writing
12083            a new implementation of symbol_computed_ops.  */
12084         *baton = obstack_alloc (&objfile->objfile_obstack,
12085                                 sizeof (struct dwarf2_locexpr_baton));
12086         (*baton)->per_cu = cu->per_cu;
12087         gdb_assert ((*baton)->per_cu);
12088
12089         (*baton)->size = 2 + cu_header->addr_size;
12090         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12091         (*baton)->data = data;
12092
12093         data[0] = DW_OP_addr;
12094         store_unsigned_integer (&data[1], cu_header->addr_size,
12095                                 byte_order, DW_ADDR (attr));
12096         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12097       }
12098       break;
12099     case DW_FORM_string:
12100     case DW_FORM_strp:
12101       /* DW_STRING is already allocated on the objfile obstack, point
12102          directly to it.  */
12103       *bytes = (gdb_byte *) DW_STRING (attr);
12104       break;
12105     case DW_FORM_block1:
12106     case DW_FORM_block2:
12107     case DW_FORM_block4:
12108     case DW_FORM_block:
12109     case DW_FORM_exprloc:
12110       blk = DW_BLOCK (attr);
12111       if (TYPE_LENGTH (type) != blk->size)
12112         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12113                                                       TYPE_LENGTH (type));
12114       *bytes = blk->data;
12115       break;
12116
12117       /* The DW_AT_const_value attributes are supposed to carry the
12118          symbol's value "represented as it would be on the target
12119          architecture."  By the time we get here, it's already been
12120          converted to host endianness, so we just need to sign- or
12121          zero-extend it as appropriate.  */
12122     case DW_FORM_data1:
12123       *bytes = dwarf2_const_value_data (attr, type, name,
12124                                         obstack, cu, value, 8);
12125       break;
12126     case DW_FORM_data2:
12127       *bytes = dwarf2_const_value_data (attr, type, name,
12128                                         obstack, cu, value, 16);
12129       break;
12130     case DW_FORM_data4:
12131       *bytes = dwarf2_const_value_data (attr, type, name,
12132                                         obstack, cu, value, 32);
12133       break;
12134     case DW_FORM_data8:
12135       *bytes = dwarf2_const_value_data (attr, type, name,
12136                                         obstack, cu, value, 64);
12137       break;
12138
12139     case DW_FORM_sdata:
12140       *value = DW_SND (attr);
12141       break;
12142
12143     case DW_FORM_udata:
12144       *value = DW_UNSND (attr);
12145       break;
12146
12147     default:
12148       complaint (&symfile_complaints,
12149                  _("unsupported const value attribute form: '%s'"),
12150                  dwarf_form_name (attr->form));
12151       *value = 0;
12152       break;
12153     }
12154 }
12155
12156
12157 /* Copy constant value from an attribute to a symbol.  */
12158
12159 static void
12160 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12161                     struct dwarf2_cu *cu)
12162 {
12163   struct objfile *objfile = cu->objfile;
12164   struct comp_unit_head *cu_header = &cu->header;
12165   long value;
12166   gdb_byte *bytes;
12167   struct dwarf2_locexpr_baton *baton;
12168
12169   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12170                            SYMBOL_PRINT_NAME (sym),
12171                            &objfile->objfile_obstack, cu,
12172                            &value, &bytes, &baton);
12173
12174   if (baton != NULL)
12175     {
12176       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12177       SYMBOL_LOCATION_BATON (sym) = baton;
12178       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12179     }
12180   else if (bytes != NULL)
12181      {
12182       SYMBOL_VALUE_BYTES (sym) = bytes;
12183       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12184     }
12185   else
12186     {
12187       SYMBOL_VALUE (sym) = value;
12188       SYMBOL_CLASS (sym) = LOC_CONST;
12189     }
12190 }
12191
12192 /* Return the type of the die in question using its DW_AT_type attribute.  */
12193
12194 static struct type *
12195 die_type (struct die_info *die, struct dwarf2_cu *cu)
12196 {
12197   struct attribute *type_attr;
12198
12199   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12200   if (!type_attr)
12201     {
12202       /* A missing DW_AT_type represents a void type.  */
12203       return objfile_type (cu->objfile)->builtin_void;
12204     }
12205
12206   return lookup_die_type (die, type_attr, cu);
12207 }
12208
12209 /* True iff CU's producer generates GNAT Ada auxiliary information
12210    that allows to find parallel types through that information instead
12211    of having to do expensive parallel lookups by type name.  */
12212
12213 static int
12214 need_gnat_info (struct dwarf2_cu *cu)
12215 {
12216   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12217      of GNAT produces this auxiliary information, without any indication
12218      that it is produced.  Part of enhancing the FSF version of GNAT
12219      to produce that information will be to put in place an indicator
12220      that we can use in order to determine whether the descriptive type
12221      info is available or not.  One suggestion that has been made is
12222      to use a new attribute, attached to the CU die.  For now, assume
12223      that the descriptive type info is not available.  */
12224   return 0;
12225 }
12226
12227 /* Return the auxiliary type of the die in question using its
12228    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12229    attribute is not present.  */
12230
12231 static struct type *
12232 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12233 {
12234   struct attribute *type_attr;
12235
12236   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12237   if (!type_attr)
12238     return NULL;
12239
12240   return lookup_die_type (die, type_attr, cu);
12241 }
12242
12243 /* If DIE has a descriptive_type attribute, then set the TYPE's
12244    descriptive type accordingly.  */
12245
12246 static void
12247 set_descriptive_type (struct type *type, struct die_info *die,
12248                       struct dwarf2_cu *cu)
12249 {
12250   struct type *descriptive_type = die_descriptive_type (die, cu);
12251
12252   if (descriptive_type)
12253     {
12254       ALLOCATE_GNAT_AUX_TYPE (type);
12255       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12256     }
12257 }
12258
12259 /* Return the containing type of the die in question using its
12260    DW_AT_containing_type attribute.  */
12261
12262 static struct type *
12263 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12264 {
12265   struct attribute *type_attr;
12266
12267   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12268   if (!type_attr)
12269     error (_("Dwarf Error: Problem turning containing type into gdb type "
12270              "[in module %s]"), cu->objfile->name);
12271
12272   return lookup_die_type (die, type_attr, cu);
12273 }
12274
12275 /* Look up the type of DIE in CU using its type attribute ATTR.
12276    If there is no type substitute an error marker.  */
12277
12278 static struct type *
12279 lookup_die_type (struct die_info *die, struct attribute *attr,
12280                  struct dwarf2_cu *cu)
12281 {
12282   struct type *this_type;
12283
12284   /* First see if we have it cached.  */
12285
12286   if (is_ref_attr (attr))
12287     {
12288       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12289
12290       this_type = get_die_type_at_offset (offset, cu->per_cu);
12291     }
12292   else if (attr->form == DW_FORM_ref_sig8)
12293     {
12294       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12295       struct dwarf2_cu *sig_cu;
12296       unsigned int offset;
12297
12298       /* sig_type will be NULL if the signatured type is missing from
12299          the debug info.  */
12300       if (sig_type == NULL)
12301         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12302                  "at 0x%x [in module %s]"),
12303                die->offset, cu->objfile->name);
12304
12305       gdb_assert (sig_type->per_cu.debug_type_section);
12306       offset = sig_type->per_cu.offset + sig_type->type_offset;
12307       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12308     }
12309   else
12310     {
12311       dump_die_for_error (die);
12312       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12313              dwarf_attr_name (attr->name), cu->objfile->name);
12314     }
12315
12316   /* If not cached we need to read it in.  */
12317
12318   if (this_type == NULL)
12319     {
12320       struct die_info *type_die;
12321       struct dwarf2_cu *type_cu = cu;
12322
12323       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12324       /* If the type is cached, we should have found it above.  */
12325       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12326       this_type = read_type_die_1 (type_die, type_cu);
12327     }
12328
12329   /* If we still don't have a type use an error marker.  */
12330
12331   if (this_type == NULL)
12332     {
12333       char *message, *saved;
12334
12335       /* read_type_die already issued a complaint.  */
12336       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12337                             cu->objfile->name,
12338                             cu->header.offset,
12339                             die->offset);
12340       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12341                              message, strlen (message));
12342       xfree (message);
12343
12344       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12345     }
12346
12347   return this_type;
12348 }
12349
12350 /* Return the type in DIE, CU.
12351    Returns NULL for invalid types.
12352
12353    This first does a lookup in the appropriate type_hash table,
12354    and only reads the die in if necessary.
12355
12356    NOTE: This can be called when reading in partial or full symbols.  */
12357
12358 static struct type *
12359 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12360 {
12361   struct type *this_type;
12362
12363   this_type = get_die_type (die, cu);
12364   if (this_type)
12365     return this_type;
12366
12367   return read_type_die_1 (die, cu);
12368 }
12369
12370 /* Read the type in DIE, CU.
12371    Returns NULL for invalid types.  */
12372
12373 static struct type *
12374 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12375 {
12376   struct type *this_type = NULL;
12377
12378   switch (die->tag)
12379     {
12380     case DW_TAG_class_type:
12381     case DW_TAG_interface_type:
12382     case DW_TAG_structure_type:
12383     case DW_TAG_union_type:
12384       this_type = read_structure_type (die, cu);
12385       break;
12386     case DW_TAG_enumeration_type:
12387       this_type = read_enumeration_type (die, cu);
12388       break;
12389     case DW_TAG_subprogram:
12390     case DW_TAG_subroutine_type:
12391     case DW_TAG_inlined_subroutine:
12392       this_type = read_subroutine_type (die, cu);
12393       break;
12394     case DW_TAG_array_type:
12395       this_type = read_array_type (die, cu);
12396       break;
12397     case DW_TAG_set_type:
12398       this_type = read_set_type (die, cu);
12399       break;
12400     case DW_TAG_pointer_type:
12401       this_type = read_tag_pointer_type (die, cu);
12402       break;
12403     case DW_TAG_ptr_to_member_type:
12404       this_type = read_tag_ptr_to_member_type (die, cu);
12405       break;
12406     case DW_TAG_reference_type:
12407       this_type = read_tag_reference_type (die, cu);
12408       break;
12409     case DW_TAG_const_type:
12410       this_type = read_tag_const_type (die, cu);
12411       break;
12412     case DW_TAG_volatile_type:
12413       this_type = read_tag_volatile_type (die, cu);
12414       break;
12415     case DW_TAG_string_type:
12416       this_type = read_tag_string_type (die, cu);
12417       break;
12418     case DW_TAG_typedef:
12419       this_type = read_typedef (die, cu);
12420       break;
12421     case DW_TAG_subrange_type:
12422       this_type = read_subrange_type (die, cu);
12423       break;
12424     case DW_TAG_base_type:
12425       this_type = read_base_type (die, cu);
12426       break;
12427     case DW_TAG_unspecified_type:
12428       this_type = read_unspecified_type (die, cu);
12429       break;
12430     case DW_TAG_namespace:
12431       this_type = read_namespace_type (die, cu);
12432       break;
12433     case DW_TAG_module:
12434       this_type = read_module_type (die, cu);
12435       break;
12436     default:
12437       complaint (&symfile_complaints,
12438                  _("unexpected tag in read_type_die: '%s'"),
12439                  dwarf_tag_name (die->tag));
12440       break;
12441     }
12442
12443   return this_type;
12444 }
12445
12446 /* See if we can figure out if the class lives in a namespace.  We do
12447    this by looking for a member function; its demangled name will
12448    contain namespace info, if there is any.
12449    Return the computed name or NULL.
12450    Space for the result is allocated on the objfile's obstack.
12451    This is the full-die version of guess_partial_die_structure_name.
12452    In this case we know DIE has no useful parent.  */
12453
12454 static char *
12455 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12456 {
12457   struct die_info *spec_die;
12458   struct dwarf2_cu *spec_cu;
12459   struct die_info *child;
12460
12461   spec_cu = cu;
12462   spec_die = die_specification (die, &spec_cu);
12463   if (spec_die != NULL)
12464     {
12465       die = spec_die;
12466       cu = spec_cu;
12467     }
12468
12469   for (child = die->child;
12470        child != NULL;
12471        child = child->sibling)
12472     {
12473       if (child->tag == DW_TAG_subprogram)
12474         {
12475           struct attribute *attr;
12476
12477           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12478           if (attr == NULL)
12479             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12480           if (attr != NULL)
12481             {
12482               char *actual_name
12483                 = language_class_name_from_physname (cu->language_defn,
12484                                                      DW_STRING (attr));
12485               char *name = NULL;
12486
12487               if (actual_name != NULL)
12488                 {
12489                   char *die_name = dwarf2_name (die, cu);
12490
12491                   if (die_name != NULL
12492                       && strcmp (die_name, actual_name) != 0)
12493                     {
12494                       /* Strip off the class name from the full name.
12495                          We want the prefix.  */
12496                       int die_name_len = strlen (die_name);
12497                       int actual_name_len = strlen (actual_name);
12498
12499                       /* Test for '::' as a sanity check.  */
12500                       if (actual_name_len > die_name_len + 2
12501                           && actual_name[actual_name_len
12502                                          - die_name_len - 1] == ':')
12503                         name =
12504                           obsavestring (actual_name,
12505                                         actual_name_len - die_name_len - 2,
12506                                         &cu->objfile->objfile_obstack);
12507                     }
12508                 }
12509               xfree (actual_name);
12510               return name;
12511             }
12512         }
12513     }
12514
12515   return NULL;
12516 }
12517
12518 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12519    prefix part in such case.  See
12520    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12521
12522 static char *
12523 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12524 {
12525   struct attribute *attr;
12526   char *base;
12527
12528   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12529       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12530     return NULL;
12531
12532   attr = dwarf2_attr (die, DW_AT_name, cu);
12533   if (attr != NULL && DW_STRING (attr) != NULL)
12534     return NULL;
12535
12536   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12537   if (attr == NULL)
12538     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12539   if (attr == NULL || DW_STRING (attr) == NULL)
12540     return NULL;
12541
12542   /* dwarf2_name had to be already called.  */
12543   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12544
12545   /* Strip the base name, keep any leading namespaces/classes.  */
12546   base = strrchr (DW_STRING (attr), ':');
12547   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12548     return "";
12549
12550   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12551                        &cu->objfile->objfile_obstack);
12552 }
12553
12554 /* Return the name of the namespace/class that DIE is defined within,
12555    or "" if we can't tell.  The caller should not xfree the result.
12556
12557    For example, if we're within the method foo() in the following
12558    code:
12559
12560    namespace N {
12561      class C {
12562        void foo () {
12563        }
12564      };
12565    }
12566
12567    then determine_prefix on foo's die will return "N::C".  */
12568
12569 static char *
12570 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12571 {
12572   struct die_info *parent, *spec_die;
12573   struct dwarf2_cu *spec_cu;
12574   struct type *parent_type;
12575   char *retval;
12576
12577   if (cu->language != language_cplus && cu->language != language_java
12578       && cu->language != language_fortran)
12579     return "";
12580
12581   retval = anonymous_struct_prefix (die, cu);
12582   if (retval)
12583     return retval;
12584
12585   /* We have to be careful in the presence of DW_AT_specification.
12586      For example, with GCC 3.4, given the code
12587
12588      namespace N {
12589        void foo() {
12590          // Definition of N::foo.
12591        }
12592      }
12593
12594      then we'll have a tree of DIEs like this:
12595
12596      1: DW_TAG_compile_unit
12597        2: DW_TAG_namespace        // N
12598          3: DW_TAG_subprogram     // declaration of N::foo
12599        4: DW_TAG_subprogram       // definition of N::foo
12600             DW_AT_specification   // refers to die #3
12601
12602      Thus, when processing die #4, we have to pretend that we're in
12603      the context of its DW_AT_specification, namely the contex of die
12604      #3.  */
12605   spec_cu = cu;
12606   spec_die = die_specification (die, &spec_cu);
12607   if (spec_die == NULL)
12608     parent = die->parent;
12609   else
12610     {
12611       parent = spec_die->parent;
12612       cu = spec_cu;
12613     }
12614
12615   if (parent == NULL)
12616     return "";
12617   else if (parent->building_fullname)
12618     {
12619       const char *name;
12620       const char *parent_name;
12621
12622       /* It has been seen on RealView 2.2 built binaries,
12623          DW_TAG_template_type_param types actually _defined_ as
12624          children of the parent class:
12625
12626          enum E {};
12627          template class <class Enum> Class{};
12628          Class<enum E> class_e;
12629
12630          1: DW_TAG_class_type (Class)
12631            2: DW_TAG_enumeration_type (E)
12632              3: DW_TAG_enumerator (enum1:0)
12633              3: DW_TAG_enumerator (enum2:1)
12634              ...
12635            2: DW_TAG_template_type_param
12636               DW_AT_type  DW_FORM_ref_udata (E)
12637
12638          Besides being broken debug info, it can put GDB into an
12639          infinite loop.  Consider:
12640
12641          When we're building the full name for Class<E>, we'll start
12642          at Class, and go look over its template type parameters,
12643          finding E.  We'll then try to build the full name of E, and
12644          reach here.  We're now trying to build the full name of E,
12645          and look over the parent DIE for containing scope.  In the
12646          broken case, if we followed the parent DIE of E, we'd again
12647          find Class, and once again go look at its template type
12648          arguments, etc., etc.  Simply don't consider such parent die
12649          as source-level parent of this die (it can't be, the language
12650          doesn't allow it), and break the loop here.  */
12651       name = dwarf2_name (die, cu);
12652       parent_name = dwarf2_name (parent, cu);
12653       complaint (&symfile_complaints,
12654                  _("template param type '%s' defined within parent '%s'"),
12655                  name ? name : "<unknown>",
12656                  parent_name ? parent_name : "<unknown>");
12657       return "";
12658     }
12659   else
12660     switch (parent->tag)
12661       {
12662       case DW_TAG_namespace:
12663         parent_type = read_type_die (parent, cu);
12664         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12665            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12666            Work around this problem here.  */
12667         if (cu->language == language_cplus
12668             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12669           return "";
12670         /* We give a name to even anonymous namespaces.  */
12671         return TYPE_TAG_NAME (parent_type);
12672       case DW_TAG_class_type:
12673       case DW_TAG_interface_type:
12674       case DW_TAG_structure_type:
12675       case DW_TAG_union_type:
12676       case DW_TAG_module:
12677         parent_type = read_type_die (parent, cu);
12678         if (TYPE_TAG_NAME (parent_type) != NULL)
12679           return TYPE_TAG_NAME (parent_type);
12680         else
12681           /* An anonymous structure is only allowed non-static data
12682              members; no typedefs, no member functions, et cetera.
12683              So it does not need a prefix.  */
12684           return "";
12685       case DW_TAG_compile_unit:
12686         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12687         if (cu->language == language_cplus
12688             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12689             && die->child != NULL
12690             && (die->tag == DW_TAG_class_type
12691                 || die->tag == DW_TAG_structure_type
12692                 || die->tag == DW_TAG_union_type))
12693           {
12694             char *name = guess_full_die_structure_name (die, cu);
12695             if (name != NULL)
12696               return name;
12697           }
12698         return "";
12699       default:
12700         return determine_prefix (parent, cu);
12701       }
12702 }
12703
12704 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12705    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12706    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12707    an obconcat, otherwise allocate storage for the result.  The CU argument is
12708    used to determine the language and hence, the appropriate separator.  */
12709
12710 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12711
12712 static char *
12713 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12714                  int physname, struct dwarf2_cu *cu)
12715 {
12716   const char *lead = "";
12717   const char *sep;
12718
12719   if (suffix == NULL || suffix[0] == '\0'
12720       || prefix == NULL || prefix[0] == '\0')
12721     sep = "";
12722   else if (cu->language == language_java)
12723     sep = ".";
12724   else if (cu->language == language_fortran && physname)
12725     {
12726       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12727          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12728
12729       lead = "__";
12730       sep = "_MOD_";
12731     }
12732   else
12733     sep = "::";
12734
12735   if (prefix == NULL)
12736     prefix = "";
12737   if (suffix == NULL)
12738     suffix = "";
12739
12740   if (obs == NULL)
12741     {
12742       char *retval
12743         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12744
12745       strcpy (retval, lead);
12746       strcat (retval, prefix);
12747       strcat (retval, sep);
12748       strcat (retval, suffix);
12749       return retval;
12750     }
12751   else
12752     {
12753       /* We have an obstack.  */
12754       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12755     }
12756 }
12757
12758 /* Return sibling of die, NULL if no sibling.  */
12759
12760 static struct die_info *
12761 sibling_die (struct die_info *die)
12762 {
12763   return die->sibling;
12764 }
12765
12766 /* Get name of a die, return NULL if not found.  */
12767
12768 static char *
12769 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12770                           struct obstack *obstack)
12771 {
12772   if (name && cu->language == language_cplus)
12773     {
12774       char *canon_name = cp_canonicalize_string (name);
12775
12776       if (canon_name != NULL)
12777         {
12778           if (strcmp (canon_name, name) != 0)
12779             name = obsavestring (canon_name, strlen (canon_name),
12780                                  obstack);
12781           xfree (canon_name);
12782         }
12783     }
12784
12785   return name;
12786 }
12787
12788 /* Get name of a die, return NULL if not found.  */
12789
12790 static char *
12791 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12792 {
12793   struct attribute *attr;
12794
12795   attr = dwarf2_attr (die, DW_AT_name, cu);
12796   if ((!attr || !DW_STRING (attr))
12797       && die->tag != DW_TAG_class_type
12798       && die->tag != DW_TAG_interface_type
12799       && die->tag != DW_TAG_structure_type
12800       && die->tag != DW_TAG_union_type)
12801     return NULL;
12802
12803   switch (die->tag)
12804     {
12805     case DW_TAG_compile_unit:
12806       /* Compilation units have a DW_AT_name that is a filename, not
12807          a source language identifier.  */
12808     case DW_TAG_enumeration_type:
12809     case DW_TAG_enumerator:
12810       /* These tags always have simple identifiers already; no need
12811          to canonicalize them.  */
12812       return DW_STRING (attr);
12813
12814     case DW_TAG_subprogram:
12815       /* Java constructors will all be named "<init>", so return
12816          the class name when we see this special case.  */
12817       if (cu->language == language_java
12818           && DW_STRING (attr) != NULL
12819           && strcmp (DW_STRING (attr), "<init>") == 0)
12820         {
12821           struct dwarf2_cu *spec_cu = cu;
12822           struct die_info *spec_die;
12823
12824           /* GCJ will output '<init>' for Java constructor names.
12825              For this special case, return the name of the parent class.  */
12826
12827           /* GCJ may output suprogram DIEs with AT_specification set.
12828              If so, use the name of the specified DIE.  */
12829           spec_die = die_specification (die, &spec_cu);
12830           if (spec_die != NULL)
12831             return dwarf2_name (spec_die, spec_cu);
12832
12833           do
12834             {
12835               die = die->parent;
12836               if (die->tag == DW_TAG_class_type)
12837                 return dwarf2_name (die, cu);
12838             }
12839           while (die->tag != DW_TAG_compile_unit);
12840         }
12841       break;
12842
12843     case DW_TAG_class_type:
12844     case DW_TAG_interface_type:
12845     case DW_TAG_structure_type:
12846     case DW_TAG_union_type:
12847       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12848          structures or unions.  These were of the form "._%d" in GCC 4.1,
12849          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12850          and GCC 4.4.  We work around this problem by ignoring these.  */
12851       if (attr && DW_STRING (attr)
12852           && (strncmp (DW_STRING (attr), "._", 2) == 0
12853               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12854         return NULL;
12855
12856       /* GCC might emit a nameless typedef that has a linkage name.  See
12857          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12858       if (!attr || DW_STRING (attr) == NULL)
12859         {
12860           char *demangled = NULL;
12861
12862           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12863           if (attr == NULL)
12864             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12865
12866           if (attr == NULL || DW_STRING (attr) == NULL)
12867             return NULL;
12868
12869           /* Avoid demangling DW_STRING (attr) the second time on a second
12870              call for the same DIE.  */
12871           if (!DW_STRING_IS_CANONICAL (attr))
12872             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12873
12874           if (demangled)
12875             {
12876               char *base;
12877
12878               /* FIXME: we already did this for the partial symbol... */
12879               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12880                                                &cu->objfile->objfile_obstack);
12881               DW_STRING_IS_CANONICAL (attr) = 1;
12882               xfree (demangled);
12883
12884               /* Strip any leading namespaces/classes, keep only the base name.
12885                  DW_AT_name for named DIEs does not contain the prefixes.  */
12886               base = strrchr (DW_STRING (attr), ':');
12887               if (base && base > DW_STRING (attr) && base[-1] == ':')
12888                 return &base[1];
12889               else
12890                 return DW_STRING (attr);
12891             }
12892         }
12893       break;
12894
12895     default:
12896       break;
12897     }
12898
12899   if (!DW_STRING_IS_CANONICAL (attr))
12900     {
12901       DW_STRING (attr)
12902         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12903                                     &cu->objfile->objfile_obstack);
12904       DW_STRING_IS_CANONICAL (attr) = 1;
12905     }
12906   return DW_STRING (attr);
12907 }
12908
12909 /* Return the die that this die in an extension of, or NULL if there
12910    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12911    containing the return value on output.  */
12912
12913 static struct die_info *
12914 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12915 {
12916   struct attribute *attr;
12917
12918   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12919   if (attr == NULL)
12920     return NULL;
12921
12922   return follow_die_ref (die, attr, ext_cu);
12923 }
12924
12925 /* Convert a DIE tag into its string name.  */
12926
12927 static char *
12928 dwarf_tag_name (unsigned tag)
12929 {
12930   switch (tag)
12931     {
12932     case DW_TAG_padding:
12933       return "DW_TAG_padding";
12934     case DW_TAG_array_type:
12935       return "DW_TAG_array_type";
12936     case DW_TAG_class_type:
12937       return "DW_TAG_class_type";
12938     case DW_TAG_entry_point:
12939       return "DW_TAG_entry_point";
12940     case DW_TAG_enumeration_type:
12941       return "DW_TAG_enumeration_type";
12942     case DW_TAG_formal_parameter:
12943       return "DW_TAG_formal_parameter";
12944     case DW_TAG_imported_declaration:
12945       return "DW_TAG_imported_declaration";
12946     case DW_TAG_label:
12947       return "DW_TAG_label";
12948     case DW_TAG_lexical_block:
12949       return "DW_TAG_lexical_block";
12950     case DW_TAG_member:
12951       return "DW_TAG_member";
12952     case DW_TAG_pointer_type:
12953       return "DW_TAG_pointer_type";
12954     case DW_TAG_reference_type:
12955       return "DW_TAG_reference_type";
12956     case DW_TAG_compile_unit:
12957       return "DW_TAG_compile_unit";
12958     case DW_TAG_string_type:
12959       return "DW_TAG_string_type";
12960     case DW_TAG_structure_type:
12961       return "DW_TAG_structure_type";
12962     case DW_TAG_subroutine_type:
12963       return "DW_TAG_subroutine_type";
12964     case DW_TAG_typedef:
12965       return "DW_TAG_typedef";
12966     case DW_TAG_union_type:
12967       return "DW_TAG_union_type";
12968     case DW_TAG_unspecified_parameters:
12969       return "DW_TAG_unspecified_parameters";
12970     case DW_TAG_variant:
12971       return "DW_TAG_variant";
12972     case DW_TAG_common_block:
12973       return "DW_TAG_common_block";
12974     case DW_TAG_common_inclusion:
12975       return "DW_TAG_common_inclusion";
12976     case DW_TAG_inheritance:
12977       return "DW_TAG_inheritance";
12978     case DW_TAG_inlined_subroutine:
12979       return "DW_TAG_inlined_subroutine";
12980     case DW_TAG_module:
12981       return "DW_TAG_module";
12982     case DW_TAG_ptr_to_member_type:
12983       return "DW_TAG_ptr_to_member_type";
12984     case DW_TAG_set_type:
12985       return "DW_TAG_set_type";
12986     case DW_TAG_subrange_type:
12987       return "DW_TAG_subrange_type";
12988     case DW_TAG_with_stmt:
12989       return "DW_TAG_with_stmt";
12990     case DW_TAG_access_declaration:
12991       return "DW_TAG_access_declaration";
12992     case DW_TAG_base_type:
12993       return "DW_TAG_base_type";
12994     case DW_TAG_catch_block:
12995       return "DW_TAG_catch_block";
12996     case DW_TAG_const_type:
12997       return "DW_TAG_const_type";
12998     case DW_TAG_constant:
12999       return "DW_TAG_constant";
13000     case DW_TAG_enumerator:
13001       return "DW_TAG_enumerator";
13002     case DW_TAG_file_type:
13003       return "DW_TAG_file_type";
13004     case DW_TAG_friend:
13005       return "DW_TAG_friend";
13006     case DW_TAG_namelist:
13007       return "DW_TAG_namelist";
13008     case DW_TAG_namelist_item:
13009       return "DW_TAG_namelist_item";
13010     case DW_TAG_packed_type:
13011       return "DW_TAG_packed_type";
13012     case DW_TAG_subprogram:
13013       return "DW_TAG_subprogram";
13014     case DW_TAG_template_type_param:
13015       return "DW_TAG_template_type_param";
13016     case DW_TAG_template_value_param:
13017       return "DW_TAG_template_value_param";
13018     case DW_TAG_thrown_type:
13019       return "DW_TAG_thrown_type";
13020     case DW_TAG_try_block:
13021       return "DW_TAG_try_block";
13022     case DW_TAG_variant_part:
13023       return "DW_TAG_variant_part";
13024     case DW_TAG_variable:
13025       return "DW_TAG_variable";
13026     case DW_TAG_volatile_type:
13027       return "DW_TAG_volatile_type";
13028     case DW_TAG_dwarf_procedure:
13029       return "DW_TAG_dwarf_procedure";
13030     case DW_TAG_restrict_type:
13031       return "DW_TAG_restrict_type";
13032     case DW_TAG_interface_type:
13033       return "DW_TAG_interface_type";
13034     case DW_TAG_namespace:
13035       return "DW_TAG_namespace";
13036     case DW_TAG_imported_module:
13037       return "DW_TAG_imported_module";
13038     case DW_TAG_unspecified_type:
13039       return "DW_TAG_unspecified_type";
13040     case DW_TAG_partial_unit:
13041       return "DW_TAG_partial_unit";
13042     case DW_TAG_imported_unit:
13043       return "DW_TAG_imported_unit";
13044     case DW_TAG_condition:
13045       return "DW_TAG_condition";
13046     case DW_TAG_shared_type:
13047       return "DW_TAG_shared_type";
13048     case DW_TAG_type_unit:
13049       return "DW_TAG_type_unit";
13050     case DW_TAG_MIPS_loop:
13051       return "DW_TAG_MIPS_loop";
13052     case DW_TAG_HP_array_descriptor:
13053       return "DW_TAG_HP_array_descriptor";
13054     case DW_TAG_format_label:
13055       return "DW_TAG_format_label";
13056     case DW_TAG_function_template:
13057       return "DW_TAG_function_template";
13058     case DW_TAG_class_template:
13059       return "DW_TAG_class_template";
13060     case DW_TAG_GNU_BINCL:
13061       return "DW_TAG_GNU_BINCL";
13062     case DW_TAG_GNU_EINCL:
13063       return "DW_TAG_GNU_EINCL";
13064     case DW_TAG_upc_shared_type:
13065       return "DW_TAG_upc_shared_type";
13066     case DW_TAG_upc_strict_type:
13067       return "DW_TAG_upc_strict_type";
13068     case DW_TAG_upc_relaxed_type:
13069       return "DW_TAG_upc_relaxed_type";
13070     case DW_TAG_PGI_kanji_type:
13071       return "DW_TAG_PGI_kanji_type";
13072     case DW_TAG_PGI_interface_block:
13073       return "DW_TAG_PGI_interface_block";
13074     case DW_TAG_GNU_call_site:
13075       return "DW_TAG_GNU_call_site";
13076     default:
13077       return "DW_TAG_<unknown>";
13078     }
13079 }
13080
13081 /* Convert a DWARF attribute code into its string name.  */
13082
13083 static char *
13084 dwarf_attr_name (unsigned attr)
13085 {
13086   switch (attr)
13087     {
13088     case DW_AT_sibling:
13089       return "DW_AT_sibling";
13090     case DW_AT_location:
13091       return "DW_AT_location";
13092     case DW_AT_name:
13093       return "DW_AT_name";
13094     case DW_AT_ordering:
13095       return "DW_AT_ordering";
13096     case DW_AT_subscr_data:
13097       return "DW_AT_subscr_data";
13098     case DW_AT_byte_size:
13099       return "DW_AT_byte_size";
13100     case DW_AT_bit_offset:
13101       return "DW_AT_bit_offset";
13102     case DW_AT_bit_size:
13103       return "DW_AT_bit_size";
13104     case DW_AT_element_list:
13105       return "DW_AT_element_list";
13106     case DW_AT_stmt_list:
13107       return "DW_AT_stmt_list";
13108     case DW_AT_low_pc:
13109       return "DW_AT_low_pc";
13110     case DW_AT_high_pc:
13111       return "DW_AT_high_pc";
13112     case DW_AT_language:
13113       return "DW_AT_language";
13114     case DW_AT_member:
13115       return "DW_AT_member";
13116     case DW_AT_discr:
13117       return "DW_AT_discr";
13118     case DW_AT_discr_value:
13119       return "DW_AT_discr_value";
13120     case DW_AT_visibility:
13121       return "DW_AT_visibility";
13122     case DW_AT_import:
13123       return "DW_AT_import";
13124     case DW_AT_string_length:
13125       return "DW_AT_string_length";
13126     case DW_AT_common_reference:
13127       return "DW_AT_common_reference";
13128     case DW_AT_comp_dir:
13129       return "DW_AT_comp_dir";
13130     case DW_AT_const_value:
13131       return "DW_AT_const_value";
13132     case DW_AT_containing_type:
13133       return "DW_AT_containing_type";
13134     case DW_AT_default_value:
13135       return "DW_AT_default_value";
13136     case DW_AT_inline:
13137       return "DW_AT_inline";
13138     case DW_AT_is_optional:
13139       return "DW_AT_is_optional";
13140     case DW_AT_lower_bound:
13141       return "DW_AT_lower_bound";
13142     case DW_AT_producer:
13143       return "DW_AT_producer";
13144     case DW_AT_prototyped:
13145       return "DW_AT_prototyped";
13146     case DW_AT_return_addr:
13147       return "DW_AT_return_addr";
13148     case DW_AT_start_scope:
13149       return "DW_AT_start_scope";
13150     case DW_AT_bit_stride:
13151       return "DW_AT_bit_stride";
13152     case DW_AT_upper_bound:
13153       return "DW_AT_upper_bound";
13154     case DW_AT_abstract_origin:
13155       return "DW_AT_abstract_origin";
13156     case DW_AT_accessibility:
13157       return "DW_AT_accessibility";
13158     case DW_AT_address_class:
13159       return "DW_AT_address_class";
13160     case DW_AT_artificial:
13161       return "DW_AT_artificial";
13162     case DW_AT_base_types:
13163       return "DW_AT_base_types";
13164     case DW_AT_calling_convention:
13165       return "DW_AT_calling_convention";
13166     case DW_AT_count:
13167       return "DW_AT_count";
13168     case DW_AT_data_member_location:
13169       return "DW_AT_data_member_location";
13170     case DW_AT_decl_column:
13171       return "DW_AT_decl_column";
13172     case DW_AT_decl_file:
13173       return "DW_AT_decl_file";
13174     case DW_AT_decl_line:
13175       return "DW_AT_decl_line";
13176     case DW_AT_declaration:
13177       return "DW_AT_declaration";
13178     case DW_AT_discr_list:
13179       return "DW_AT_discr_list";
13180     case DW_AT_encoding:
13181       return "DW_AT_encoding";
13182     case DW_AT_external:
13183       return "DW_AT_external";
13184     case DW_AT_frame_base:
13185       return "DW_AT_frame_base";
13186     case DW_AT_friend:
13187       return "DW_AT_friend";
13188     case DW_AT_identifier_case:
13189       return "DW_AT_identifier_case";
13190     case DW_AT_macro_info:
13191       return "DW_AT_macro_info";
13192     case DW_AT_namelist_items:
13193       return "DW_AT_namelist_items";
13194     case DW_AT_priority:
13195       return "DW_AT_priority";
13196     case DW_AT_segment:
13197       return "DW_AT_segment";
13198     case DW_AT_specification:
13199       return "DW_AT_specification";
13200     case DW_AT_static_link:
13201       return "DW_AT_static_link";
13202     case DW_AT_type:
13203       return "DW_AT_type";
13204     case DW_AT_use_location:
13205       return "DW_AT_use_location";
13206     case DW_AT_variable_parameter:
13207       return "DW_AT_variable_parameter";
13208     case DW_AT_virtuality:
13209       return "DW_AT_virtuality";
13210     case DW_AT_vtable_elem_location:
13211       return "DW_AT_vtable_elem_location";
13212     /* DWARF 3 values.  */
13213     case DW_AT_allocated:
13214       return "DW_AT_allocated";
13215     case DW_AT_associated:
13216       return "DW_AT_associated";
13217     case DW_AT_data_location:
13218       return "DW_AT_data_location";
13219     case DW_AT_byte_stride:
13220       return "DW_AT_byte_stride";
13221     case DW_AT_entry_pc:
13222       return "DW_AT_entry_pc";
13223     case DW_AT_use_UTF8:
13224       return "DW_AT_use_UTF8";
13225     case DW_AT_extension:
13226       return "DW_AT_extension";
13227     case DW_AT_ranges:
13228       return "DW_AT_ranges";
13229     case DW_AT_trampoline:
13230       return "DW_AT_trampoline";
13231     case DW_AT_call_column:
13232       return "DW_AT_call_column";
13233     case DW_AT_call_file:
13234       return "DW_AT_call_file";
13235     case DW_AT_call_line:
13236       return "DW_AT_call_line";
13237     case DW_AT_description:
13238       return "DW_AT_description";
13239     case DW_AT_binary_scale:
13240       return "DW_AT_binary_scale";
13241     case DW_AT_decimal_scale:
13242       return "DW_AT_decimal_scale";
13243     case DW_AT_small:
13244       return "DW_AT_small";
13245     case DW_AT_decimal_sign:
13246       return "DW_AT_decimal_sign";
13247     case DW_AT_digit_count:
13248       return "DW_AT_digit_count";
13249     case DW_AT_picture_string:
13250       return "DW_AT_picture_string";
13251     case DW_AT_mutable:
13252       return "DW_AT_mutable";
13253     case DW_AT_threads_scaled:
13254       return "DW_AT_threads_scaled";
13255     case DW_AT_explicit:
13256       return "DW_AT_explicit";
13257     case DW_AT_object_pointer:
13258       return "DW_AT_object_pointer";
13259     case DW_AT_endianity:
13260       return "DW_AT_endianity";
13261     case DW_AT_elemental:
13262       return "DW_AT_elemental";
13263     case DW_AT_pure:
13264       return "DW_AT_pure";
13265     case DW_AT_recursive:
13266       return "DW_AT_recursive";
13267     /* DWARF 4 values.  */
13268     case DW_AT_signature:
13269       return "DW_AT_signature";
13270     case DW_AT_linkage_name:
13271       return "DW_AT_linkage_name";
13272     /* SGI/MIPS extensions.  */
13273 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13274     case DW_AT_MIPS_fde:
13275       return "DW_AT_MIPS_fde";
13276 #endif
13277     case DW_AT_MIPS_loop_begin:
13278       return "DW_AT_MIPS_loop_begin";
13279     case DW_AT_MIPS_tail_loop_begin:
13280       return "DW_AT_MIPS_tail_loop_begin";
13281     case DW_AT_MIPS_epilog_begin:
13282       return "DW_AT_MIPS_epilog_begin";
13283     case DW_AT_MIPS_loop_unroll_factor:
13284       return "DW_AT_MIPS_loop_unroll_factor";
13285     case DW_AT_MIPS_software_pipeline_depth:
13286       return "DW_AT_MIPS_software_pipeline_depth";
13287     case DW_AT_MIPS_linkage_name:
13288       return "DW_AT_MIPS_linkage_name";
13289     case DW_AT_MIPS_stride:
13290       return "DW_AT_MIPS_stride";
13291     case DW_AT_MIPS_abstract_name:
13292       return "DW_AT_MIPS_abstract_name";
13293     case DW_AT_MIPS_clone_origin:
13294       return "DW_AT_MIPS_clone_origin";
13295     case DW_AT_MIPS_has_inlines:
13296       return "DW_AT_MIPS_has_inlines";
13297     /* HP extensions.  */
13298 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13299     case DW_AT_HP_block_index:
13300       return "DW_AT_HP_block_index";
13301 #endif
13302     case DW_AT_HP_unmodifiable:
13303       return "DW_AT_HP_unmodifiable";
13304     case DW_AT_HP_actuals_stmt_list:
13305       return "DW_AT_HP_actuals_stmt_list";
13306     case DW_AT_HP_proc_per_section:
13307       return "DW_AT_HP_proc_per_section";
13308     case DW_AT_HP_raw_data_ptr:
13309       return "DW_AT_HP_raw_data_ptr";
13310     case DW_AT_HP_pass_by_reference:
13311       return "DW_AT_HP_pass_by_reference";
13312     case DW_AT_HP_opt_level:
13313       return "DW_AT_HP_opt_level";
13314     case DW_AT_HP_prof_version_id:
13315       return "DW_AT_HP_prof_version_id";
13316     case DW_AT_HP_opt_flags:
13317       return "DW_AT_HP_opt_flags";
13318     case DW_AT_HP_cold_region_low_pc:
13319       return "DW_AT_HP_cold_region_low_pc";
13320     case DW_AT_HP_cold_region_high_pc:
13321       return "DW_AT_HP_cold_region_high_pc";
13322     case DW_AT_HP_all_variables_modifiable:
13323       return "DW_AT_HP_all_variables_modifiable";
13324     case DW_AT_HP_linkage_name:
13325       return "DW_AT_HP_linkage_name";
13326     case DW_AT_HP_prof_flags:
13327       return "DW_AT_HP_prof_flags";
13328     /* GNU extensions.  */
13329     case DW_AT_sf_names:
13330       return "DW_AT_sf_names";
13331     case DW_AT_src_info:
13332       return "DW_AT_src_info";
13333     case DW_AT_mac_info:
13334       return "DW_AT_mac_info";
13335     case DW_AT_src_coords:
13336       return "DW_AT_src_coords";
13337     case DW_AT_body_begin:
13338       return "DW_AT_body_begin";
13339     case DW_AT_body_end:
13340       return "DW_AT_body_end";
13341     case DW_AT_GNU_vector:
13342       return "DW_AT_GNU_vector";
13343     case DW_AT_GNU_odr_signature:
13344       return "DW_AT_GNU_odr_signature";
13345     /* VMS extensions.  */
13346     case DW_AT_VMS_rtnbeg_pd_address:
13347       return "DW_AT_VMS_rtnbeg_pd_address";
13348     /* UPC extension.  */
13349     case DW_AT_upc_threads_scaled:
13350       return "DW_AT_upc_threads_scaled";
13351     /* PGI (STMicroelectronics) extensions.  */
13352     case DW_AT_PGI_lbase:
13353       return "DW_AT_PGI_lbase";
13354     case DW_AT_PGI_soffset:
13355       return "DW_AT_PGI_soffset";
13356     case DW_AT_PGI_lstride:
13357       return "DW_AT_PGI_lstride";
13358     default:
13359       return "DW_AT_<unknown>";
13360     }
13361 }
13362
13363 /* Convert a DWARF value form code into its string name.  */
13364
13365 static char *
13366 dwarf_form_name (unsigned form)
13367 {
13368   switch (form)
13369     {
13370     case DW_FORM_addr:
13371       return "DW_FORM_addr";
13372     case DW_FORM_block2:
13373       return "DW_FORM_block2";
13374     case DW_FORM_block4:
13375       return "DW_FORM_block4";
13376     case DW_FORM_data2:
13377       return "DW_FORM_data2";
13378     case DW_FORM_data4:
13379       return "DW_FORM_data4";
13380     case DW_FORM_data8:
13381       return "DW_FORM_data8";
13382     case DW_FORM_string:
13383       return "DW_FORM_string";
13384     case DW_FORM_block:
13385       return "DW_FORM_block";
13386     case DW_FORM_block1:
13387       return "DW_FORM_block1";
13388     case DW_FORM_data1:
13389       return "DW_FORM_data1";
13390     case DW_FORM_flag:
13391       return "DW_FORM_flag";
13392     case DW_FORM_sdata:
13393       return "DW_FORM_sdata";
13394     case DW_FORM_strp:
13395       return "DW_FORM_strp";
13396     case DW_FORM_udata:
13397       return "DW_FORM_udata";
13398     case DW_FORM_ref_addr:
13399       return "DW_FORM_ref_addr";
13400     case DW_FORM_ref1:
13401       return "DW_FORM_ref1";
13402     case DW_FORM_ref2:
13403       return "DW_FORM_ref2";
13404     case DW_FORM_ref4:
13405       return "DW_FORM_ref4";
13406     case DW_FORM_ref8:
13407       return "DW_FORM_ref8";
13408     case DW_FORM_ref_udata:
13409       return "DW_FORM_ref_udata";
13410     case DW_FORM_indirect:
13411       return "DW_FORM_indirect";
13412     case DW_FORM_sec_offset:
13413       return "DW_FORM_sec_offset";
13414     case DW_FORM_exprloc:
13415       return "DW_FORM_exprloc";
13416     case DW_FORM_flag_present:
13417       return "DW_FORM_flag_present";
13418     case DW_FORM_ref_sig8:
13419       return "DW_FORM_ref_sig8";
13420     default:
13421       return "DW_FORM_<unknown>";
13422     }
13423 }
13424
13425 /* Convert a DWARF stack opcode into its string name.  */
13426
13427 const char *
13428 dwarf_stack_op_name (unsigned op)
13429 {
13430   switch (op)
13431     {
13432     case DW_OP_addr:
13433       return "DW_OP_addr";
13434     case DW_OP_deref:
13435       return "DW_OP_deref";
13436     case DW_OP_const1u:
13437       return "DW_OP_const1u";
13438     case DW_OP_const1s:
13439       return "DW_OP_const1s";
13440     case DW_OP_const2u:
13441       return "DW_OP_const2u";
13442     case DW_OP_const2s:
13443       return "DW_OP_const2s";
13444     case DW_OP_const4u:
13445       return "DW_OP_const4u";
13446     case DW_OP_const4s:
13447       return "DW_OP_const4s";
13448     case DW_OP_const8u:
13449       return "DW_OP_const8u";
13450     case DW_OP_const8s:
13451       return "DW_OP_const8s";
13452     case DW_OP_constu:
13453       return "DW_OP_constu";
13454     case DW_OP_consts:
13455       return "DW_OP_consts";
13456     case DW_OP_dup:
13457       return "DW_OP_dup";
13458     case DW_OP_drop:
13459       return "DW_OP_drop";
13460     case DW_OP_over:
13461       return "DW_OP_over";
13462     case DW_OP_pick:
13463       return "DW_OP_pick";
13464     case DW_OP_swap:
13465       return "DW_OP_swap";
13466     case DW_OP_rot:
13467       return "DW_OP_rot";
13468     case DW_OP_xderef:
13469       return "DW_OP_xderef";
13470     case DW_OP_abs:
13471       return "DW_OP_abs";
13472     case DW_OP_and:
13473       return "DW_OP_and";
13474     case DW_OP_div:
13475       return "DW_OP_div";
13476     case DW_OP_minus:
13477       return "DW_OP_minus";
13478     case DW_OP_mod:
13479       return "DW_OP_mod";
13480     case DW_OP_mul:
13481       return "DW_OP_mul";
13482     case DW_OP_neg:
13483       return "DW_OP_neg";
13484     case DW_OP_not:
13485       return "DW_OP_not";
13486     case DW_OP_or:
13487       return "DW_OP_or";
13488     case DW_OP_plus:
13489       return "DW_OP_plus";
13490     case DW_OP_plus_uconst:
13491       return "DW_OP_plus_uconst";
13492     case DW_OP_shl:
13493       return "DW_OP_shl";
13494     case DW_OP_shr:
13495       return "DW_OP_shr";
13496     case DW_OP_shra:
13497       return "DW_OP_shra";
13498     case DW_OP_xor:
13499       return "DW_OP_xor";
13500     case DW_OP_bra:
13501       return "DW_OP_bra";
13502     case DW_OP_eq:
13503       return "DW_OP_eq";
13504     case DW_OP_ge:
13505       return "DW_OP_ge";
13506     case DW_OP_gt:
13507       return "DW_OP_gt";
13508     case DW_OP_le:
13509       return "DW_OP_le";
13510     case DW_OP_lt:
13511       return "DW_OP_lt";
13512     case DW_OP_ne:
13513       return "DW_OP_ne";
13514     case DW_OP_skip:
13515       return "DW_OP_skip";
13516     case DW_OP_lit0:
13517       return "DW_OP_lit0";
13518     case DW_OP_lit1:
13519       return "DW_OP_lit1";
13520     case DW_OP_lit2:
13521       return "DW_OP_lit2";
13522     case DW_OP_lit3:
13523       return "DW_OP_lit3";
13524     case DW_OP_lit4:
13525       return "DW_OP_lit4";
13526     case DW_OP_lit5:
13527       return "DW_OP_lit5";
13528     case DW_OP_lit6:
13529       return "DW_OP_lit6";
13530     case DW_OP_lit7:
13531       return "DW_OP_lit7";
13532     case DW_OP_lit8:
13533       return "DW_OP_lit8";
13534     case DW_OP_lit9:
13535       return "DW_OP_lit9";
13536     case DW_OP_lit10:
13537       return "DW_OP_lit10";
13538     case DW_OP_lit11:
13539       return "DW_OP_lit11";
13540     case DW_OP_lit12:
13541       return "DW_OP_lit12";
13542     case DW_OP_lit13:
13543       return "DW_OP_lit13";
13544     case DW_OP_lit14:
13545       return "DW_OP_lit14";
13546     case DW_OP_lit15:
13547       return "DW_OP_lit15";
13548     case DW_OP_lit16:
13549       return "DW_OP_lit16";
13550     case DW_OP_lit17:
13551       return "DW_OP_lit17";
13552     case DW_OP_lit18:
13553       return "DW_OP_lit18";
13554     case DW_OP_lit19:
13555       return "DW_OP_lit19";
13556     case DW_OP_lit20:
13557       return "DW_OP_lit20";
13558     case DW_OP_lit21:
13559       return "DW_OP_lit21";
13560     case DW_OP_lit22:
13561       return "DW_OP_lit22";
13562     case DW_OP_lit23:
13563       return "DW_OP_lit23";
13564     case DW_OP_lit24:
13565       return "DW_OP_lit24";
13566     case DW_OP_lit25:
13567       return "DW_OP_lit25";
13568     case DW_OP_lit26:
13569       return "DW_OP_lit26";
13570     case DW_OP_lit27:
13571       return "DW_OP_lit27";
13572     case DW_OP_lit28:
13573       return "DW_OP_lit28";
13574     case DW_OP_lit29:
13575       return "DW_OP_lit29";
13576     case DW_OP_lit30:
13577       return "DW_OP_lit30";
13578     case DW_OP_lit31:
13579       return "DW_OP_lit31";
13580     case DW_OP_reg0:
13581       return "DW_OP_reg0";
13582     case DW_OP_reg1:
13583       return "DW_OP_reg1";
13584     case DW_OP_reg2:
13585       return "DW_OP_reg2";
13586     case DW_OP_reg3:
13587       return "DW_OP_reg3";
13588     case DW_OP_reg4:
13589       return "DW_OP_reg4";
13590     case DW_OP_reg5:
13591       return "DW_OP_reg5";
13592     case DW_OP_reg6:
13593       return "DW_OP_reg6";
13594     case DW_OP_reg7:
13595       return "DW_OP_reg7";
13596     case DW_OP_reg8:
13597       return "DW_OP_reg8";
13598     case DW_OP_reg9:
13599       return "DW_OP_reg9";
13600     case DW_OP_reg10:
13601       return "DW_OP_reg10";
13602     case DW_OP_reg11:
13603       return "DW_OP_reg11";
13604     case DW_OP_reg12:
13605       return "DW_OP_reg12";
13606     case DW_OP_reg13:
13607       return "DW_OP_reg13";
13608     case DW_OP_reg14:
13609       return "DW_OP_reg14";
13610     case DW_OP_reg15:
13611       return "DW_OP_reg15";
13612     case DW_OP_reg16:
13613       return "DW_OP_reg16";
13614     case DW_OP_reg17:
13615       return "DW_OP_reg17";
13616     case DW_OP_reg18:
13617       return "DW_OP_reg18";
13618     case DW_OP_reg19:
13619       return "DW_OP_reg19";
13620     case DW_OP_reg20:
13621       return "DW_OP_reg20";
13622     case DW_OP_reg21:
13623       return "DW_OP_reg21";
13624     case DW_OP_reg22:
13625       return "DW_OP_reg22";
13626     case DW_OP_reg23:
13627       return "DW_OP_reg23";
13628     case DW_OP_reg24:
13629       return "DW_OP_reg24";
13630     case DW_OP_reg25:
13631       return "DW_OP_reg25";
13632     case DW_OP_reg26:
13633       return "DW_OP_reg26";
13634     case DW_OP_reg27:
13635       return "DW_OP_reg27";
13636     case DW_OP_reg28:
13637       return "DW_OP_reg28";
13638     case DW_OP_reg29:
13639       return "DW_OP_reg29";
13640     case DW_OP_reg30:
13641       return "DW_OP_reg30";
13642     case DW_OP_reg31:
13643       return "DW_OP_reg31";
13644     case DW_OP_breg0:
13645       return "DW_OP_breg0";
13646     case DW_OP_breg1:
13647       return "DW_OP_breg1";
13648     case DW_OP_breg2:
13649       return "DW_OP_breg2";
13650     case DW_OP_breg3:
13651       return "DW_OP_breg3";
13652     case DW_OP_breg4:
13653       return "DW_OP_breg4";
13654     case DW_OP_breg5:
13655       return "DW_OP_breg5";
13656     case DW_OP_breg6:
13657       return "DW_OP_breg6";
13658     case DW_OP_breg7:
13659       return "DW_OP_breg7";
13660     case DW_OP_breg8:
13661       return "DW_OP_breg8";
13662     case DW_OP_breg9:
13663       return "DW_OP_breg9";
13664     case DW_OP_breg10:
13665       return "DW_OP_breg10";
13666     case DW_OP_breg11:
13667       return "DW_OP_breg11";
13668     case DW_OP_breg12:
13669       return "DW_OP_breg12";
13670     case DW_OP_breg13:
13671       return "DW_OP_breg13";
13672     case DW_OP_breg14:
13673       return "DW_OP_breg14";
13674     case DW_OP_breg15:
13675       return "DW_OP_breg15";
13676     case DW_OP_breg16:
13677       return "DW_OP_breg16";
13678     case DW_OP_breg17:
13679       return "DW_OP_breg17";
13680     case DW_OP_breg18:
13681       return "DW_OP_breg18";
13682     case DW_OP_breg19:
13683       return "DW_OP_breg19";
13684     case DW_OP_breg20:
13685       return "DW_OP_breg20";
13686     case DW_OP_breg21:
13687       return "DW_OP_breg21";
13688     case DW_OP_breg22:
13689       return "DW_OP_breg22";
13690     case DW_OP_breg23:
13691       return "DW_OP_breg23";
13692     case DW_OP_breg24:
13693       return "DW_OP_breg24";
13694     case DW_OP_breg25:
13695       return "DW_OP_breg25";
13696     case DW_OP_breg26:
13697       return "DW_OP_breg26";
13698     case DW_OP_breg27:
13699       return "DW_OP_breg27";
13700     case DW_OP_breg28:
13701       return "DW_OP_breg28";
13702     case DW_OP_breg29:
13703       return "DW_OP_breg29";
13704     case DW_OP_breg30:
13705       return "DW_OP_breg30";
13706     case DW_OP_breg31:
13707       return "DW_OP_breg31";
13708     case DW_OP_regx:
13709       return "DW_OP_regx";
13710     case DW_OP_fbreg:
13711       return "DW_OP_fbreg";
13712     case DW_OP_bregx:
13713       return "DW_OP_bregx";
13714     case DW_OP_piece:
13715       return "DW_OP_piece";
13716     case DW_OP_deref_size:
13717       return "DW_OP_deref_size";
13718     case DW_OP_xderef_size:
13719       return "DW_OP_xderef_size";
13720     case DW_OP_nop:
13721       return "DW_OP_nop";
13722     /* DWARF 3 extensions.  */
13723     case DW_OP_push_object_address:
13724       return "DW_OP_push_object_address";
13725     case DW_OP_call2:
13726       return "DW_OP_call2";
13727     case DW_OP_call4:
13728       return "DW_OP_call4";
13729     case DW_OP_call_ref:
13730       return "DW_OP_call_ref";
13731     case DW_OP_form_tls_address:
13732       return "DW_OP_form_tls_address";
13733     case DW_OP_call_frame_cfa:
13734       return "DW_OP_call_frame_cfa";
13735     case DW_OP_bit_piece:
13736       return "DW_OP_bit_piece";
13737     /* DWARF 4 extensions.  */
13738     case DW_OP_implicit_value:
13739       return "DW_OP_implicit_value";
13740     case DW_OP_stack_value:
13741       return "DW_OP_stack_value";
13742     /* GNU extensions.  */
13743     case DW_OP_GNU_push_tls_address:
13744       return "DW_OP_GNU_push_tls_address";
13745     case DW_OP_GNU_uninit:
13746       return "DW_OP_GNU_uninit";
13747     case DW_OP_GNU_implicit_pointer:
13748       return "DW_OP_GNU_implicit_pointer";
13749     case DW_OP_GNU_entry_value:
13750       return "DW_OP_GNU_entry_value";
13751     case DW_OP_GNU_const_type:
13752       return "DW_OP_GNU_const_type";
13753     case DW_OP_GNU_regval_type:
13754       return "DW_OP_GNU_regval_type";
13755     case DW_OP_GNU_deref_type:
13756       return "DW_OP_GNU_deref_type";
13757     case DW_OP_GNU_convert:
13758       return "DW_OP_GNU_convert";
13759     case DW_OP_GNU_reinterpret:
13760       return "DW_OP_GNU_reinterpret";
13761     default:
13762       return NULL;
13763     }
13764 }
13765
13766 static char *
13767 dwarf_bool_name (unsigned mybool)
13768 {
13769   if (mybool)
13770     return "TRUE";
13771   else
13772     return "FALSE";
13773 }
13774
13775 /* Convert a DWARF type code into its string name.  */
13776
13777 static char *
13778 dwarf_type_encoding_name (unsigned enc)
13779 {
13780   switch (enc)
13781     {
13782     case DW_ATE_void:
13783       return "DW_ATE_void";
13784     case DW_ATE_address:
13785       return "DW_ATE_address";
13786     case DW_ATE_boolean:
13787       return "DW_ATE_boolean";
13788     case DW_ATE_complex_float:
13789       return "DW_ATE_complex_float";
13790     case DW_ATE_float:
13791       return "DW_ATE_float";
13792     case DW_ATE_signed:
13793       return "DW_ATE_signed";
13794     case DW_ATE_signed_char:
13795       return "DW_ATE_signed_char";
13796     case DW_ATE_unsigned:
13797       return "DW_ATE_unsigned";
13798     case DW_ATE_unsigned_char:
13799       return "DW_ATE_unsigned_char";
13800     /* DWARF 3.  */
13801     case DW_ATE_imaginary_float:
13802       return "DW_ATE_imaginary_float";
13803     case DW_ATE_packed_decimal:
13804       return "DW_ATE_packed_decimal";
13805     case DW_ATE_numeric_string:
13806       return "DW_ATE_numeric_string";
13807     case DW_ATE_edited:
13808       return "DW_ATE_edited";
13809     case DW_ATE_signed_fixed:
13810       return "DW_ATE_signed_fixed";
13811     case DW_ATE_unsigned_fixed:
13812       return "DW_ATE_unsigned_fixed";
13813     case DW_ATE_decimal_float:
13814       return "DW_ATE_decimal_float";
13815     /* DWARF 4.  */
13816     case DW_ATE_UTF:
13817       return "DW_ATE_UTF";
13818     /* HP extensions.  */
13819     case DW_ATE_HP_float80:
13820       return "DW_ATE_HP_float80";
13821     case DW_ATE_HP_complex_float80:
13822       return "DW_ATE_HP_complex_float80";
13823     case DW_ATE_HP_float128:
13824       return "DW_ATE_HP_float128";
13825     case DW_ATE_HP_complex_float128:
13826       return "DW_ATE_HP_complex_float128";
13827     case DW_ATE_HP_floathpintel:
13828       return "DW_ATE_HP_floathpintel";
13829     case DW_ATE_HP_imaginary_float80:
13830       return "DW_ATE_HP_imaginary_float80";
13831     case DW_ATE_HP_imaginary_float128:
13832       return "DW_ATE_HP_imaginary_float128";
13833     default:
13834       return "DW_ATE_<unknown>";
13835     }
13836 }
13837
13838 /* Convert a DWARF call frame info operation to its string name.  */
13839
13840 #if 0
13841 static char *
13842 dwarf_cfi_name (unsigned cfi_opc)
13843 {
13844   switch (cfi_opc)
13845     {
13846     case DW_CFA_advance_loc:
13847       return "DW_CFA_advance_loc";
13848     case DW_CFA_offset:
13849       return "DW_CFA_offset";
13850     case DW_CFA_restore:
13851       return "DW_CFA_restore";
13852     case DW_CFA_nop:
13853       return "DW_CFA_nop";
13854     case DW_CFA_set_loc:
13855       return "DW_CFA_set_loc";
13856     case DW_CFA_advance_loc1:
13857       return "DW_CFA_advance_loc1";
13858     case DW_CFA_advance_loc2:
13859       return "DW_CFA_advance_loc2";
13860     case DW_CFA_advance_loc4:
13861       return "DW_CFA_advance_loc4";
13862     case DW_CFA_offset_extended:
13863       return "DW_CFA_offset_extended";
13864     case DW_CFA_restore_extended:
13865       return "DW_CFA_restore_extended";
13866     case DW_CFA_undefined:
13867       return "DW_CFA_undefined";
13868     case DW_CFA_same_value:
13869       return "DW_CFA_same_value";
13870     case DW_CFA_register:
13871       return "DW_CFA_register";
13872     case DW_CFA_remember_state:
13873       return "DW_CFA_remember_state";
13874     case DW_CFA_restore_state:
13875       return "DW_CFA_restore_state";
13876     case DW_CFA_def_cfa:
13877       return "DW_CFA_def_cfa";
13878     case DW_CFA_def_cfa_register:
13879       return "DW_CFA_def_cfa_register";
13880     case DW_CFA_def_cfa_offset:
13881       return "DW_CFA_def_cfa_offset";
13882     /* DWARF 3.  */
13883     case DW_CFA_def_cfa_expression:
13884       return "DW_CFA_def_cfa_expression";
13885     case DW_CFA_expression:
13886       return "DW_CFA_expression";
13887     case DW_CFA_offset_extended_sf:
13888       return "DW_CFA_offset_extended_sf";
13889     case DW_CFA_def_cfa_sf:
13890       return "DW_CFA_def_cfa_sf";
13891     case DW_CFA_def_cfa_offset_sf:
13892       return "DW_CFA_def_cfa_offset_sf";
13893     case DW_CFA_val_offset:
13894       return "DW_CFA_val_offset";
13895     case DW_CFA_val_offset_sf:
13896       return "DW_CFA_val_offset_sf";
13897     case DW_CFA_val_expression:
13898       return "DW_CFA_val_expression";
13899     /* SGI/MIPS specific.  */
13900     case DW_CFA_MIPS_advance_loc8:
13901       return "DW_CFA_MIPS_advance_loc8";
13902     /* GNU extensions.  */
13903     case DW_CFA_GNU_window_save:
13904       return "DW_CFA_GNU_window_save";
13905     case DW_CFA_GNU_args_size:
13906       return "DW_CFA_GNU_args_size";
13907     case DW_CFA_GNU_negative_offset_extended:
13908       return "DW_CFA_GNU_negative_offset_extended";
13909     default:
13910       return "DW_CFA_<unknown>";
13911     }
13912 }
13913 #endif
13914
13915 static void
13916 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13917 {
13918   unsigned int i;
13919
13920   print_spaces (indent, f);
13921   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13922            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13923
13924   if (die->parent != NULL)
13925     {
13926       print_spaces (indent, f);
13927       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13928                           die->parent->offset);
13929     }
13930
13931   print_spaces (indent, f);
13932   fprintf_unfiltered (f, "  has children: %s\n",
13933            dwarf_bool_name (die->child != NULL));
13934
13935   print_spaces (indent, f);
13936   fprintf_unfiltered (f, "  attributes:\n");
13937
13938   for (i = 0; i < die->num_attrs; ++i)
13939     {
13940       print_spaces (indent, f);
13941       fprintf_unfiltered (f, "    %s (%s) ",
13942                dwarf_attr_name (die->attrs[i].name),
13943                dwarf_form_name (die->attrs[i].form));
13944
13945       switch (die->attrs[i].form)
13946         {
13947         case DW_FORM_ref_addr:
13948         case DW_FORM_addr:
13949           fprintf_unfiltered (f, "address: ");
13950           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13951           break;
13952         case DW_FORM_block2:
13953         case DW_FORM_block4:
13954         case DW_FORM_block:
13955         case DW_FORM_block1:
13956           fprintf_unfiltered (f, "block: size %d",
13957                               DW_BLOCK (&die->attrs[i])->size);
13958           break;
13959         case DW_FORM_exprloc:
13960           fprintf_unfiltered (f, "expression: size %u",
13961                               DW_BLOCK (&die->attrs[i])->size);
13962           break;
13963         case DW_FORM_ref1:
13964         case DW_FORM_ref2:
13965         case DW_FORM_ref4:
13966           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13967                               (long) (DW_ADDR (&die->attrs[i])));
13968           break;
13969         case DW_FORM_data1:
13970         case DW_FORM_data2:
13971         case DW_FORM_data4:
13972         case DW_FORM_data8:
13973         case DW_FORM_udata:
13974         case DW_FORM_sdata:
13975           fprintf_unfiltered (f, "constant: %s",
13976                               pulongest (DW_UNSND (&die->attrs[i])));
13977           break;
13978         case DW_FORM_sec_offset:
13979           fprintf_unfiltered (f, "section offset: %s",
13980                               pulongest (DW_UNSND (&die->attrs[i])));
13981           break;
13982         case DW_FORM_ref_sig8:
13983           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13984             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13985                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13986           else
13987             fprintf_unfiltered (f, "signatured type, offset: unknown");
13988           break;
13989         case DW_FORM_string:
13990         case DW_FORM_strp:
13991           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13992                    DW_STRING (&die->attrs[i])
13993                    ? DW_STRING (&die->attrs[i]) : "",
13994                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13995           break;
13996         case DW_FORM_flag:
13997           if (DW_UNSND (&die->attrs[i]))
13998             fprintf_unfiltered (f, "flag: TRUE");
13999           else
14000             fprintf_unfiltered (f, "flag: FALSE");
14001           break;
14002         case DW_FORM_flag_present:
14003           fprintf_unfiltered (f, "flag: TRUE");
14004           break;
14005         case DW_FORM_indirect:
14006           /* The reader will have reduced the indirect form to
14007              the "base form" so this form should not occur.  */
14008           fprintf_unfiltered (f, 
14009                               "unexpected attribute form: DW_FORM_indirect");
14010           break;
14011         default:
14012           fprintf_unfiltered (f, "unsupported attribute form: %d.",
14013                    die->attrs[i].form);
14014           break;
14015         }
14016       fprintf_unfiltered (f, "\n");
14017     }
14018 }
14019
14020 static void
14021 dump_die_for_error (struct die_info *die)
14022 {
14023   dump_die_shallow (gdb_stderr, 0, die);
14024 }
14025
14026 static void
14027 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14028 {
14029   int indent = level * 4;
14030
14031   gdb_assert (die != NULL);
14032
14033   if (level >= max_level)
14034     return;
14035
14036   dump_die_shallow (f, indent, die);
14037
14038   if (die->child != NULL)
14039     {
14040       print_spaces (indent, f);
14041       fprintf_unfiltered (f, "  Children:");
14042       if (level + 1 < max_level)
14043         {
14044           fprintf_unfiltered (f, "\n");
14045           dump_die_1 (f, level + 1, max_level, die->child);
14046         }
14047       else
14048         {
14049           fprintf_unfiltered (f,
14050                               " [not printed, max nesting level reached]\n");
14051         }
14052     }
14053
14054   if (die->sibling != NULL && level > 0)
14055     {
14056       dump_die_1 (f, level, max_level, die->sibling);
14057     }
14058 }
14059
14060 /* This is called from the pdie macro in gdbinit.in.
14061    It's not static so gcc will keep a copy callable from gdb.  */
14062
14063 void
14064 dump_die (struct die_info *die, int max_level)
14065 {
14066   dump_die_1 (gdb_stdlog, 0, max_level, die);
14067 }
14068
14069 static void
14070 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14071 {
14072   void **slot;
14073
14074   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14075
14076   *slot = die;
14077 }
14078
14079 static int
14080 is_ref_attr (struct attribute *attr)
14081 {
14082   switch (attr->form)
14083     {
14084     case DW_FORM_ref_addr:
14085     case DW_FORM_ref1:
14086     case DW_FORM_ref2:
14087     case DW_FORM_ref4:
14088     case DW_FORM_ref8:
14089     case DW_FORM_ref_udata:
14090       return 1;
14091     default:
14092       return 0;
14093     }
14094 }
14095
14096 static unsigned int
14097 dwarf2_get_ref_die_offset (struct attribute *attr)
14098 {
14099   if (is_ref_attr (attr))
14100     return DW_ADDR (attr);
14101
14102   complaint (&symfile_complaints,
14103              _("unsupported die ref attribute form: '%s'"),
14104              dwarf_form_name (attr->form));
14105   return 0;
14106 }
14107
14108 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14109  * the value held by the attribute is not constant.  */
14110
14111 static LONGEST
14112 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14113 {
14114   if (attr->form == DW_FORM_sdata)
14115     return DW_SND (attr);
14116   else if (attr->form == DW_FORM_udata
14117            || attr->form == DW_FORM_data1
14118            || attr->form == DW_FORM_data2
14119            || attr->form == DW_FORM_data4
14120            || attr->form == DW_FORM_data8)
14121     return DW_UNSND (attr);
14122   else
14123     {
14124       complaint (&symfile_complaints,
14125                  _("Attribute value is not a constant (%s)"),
14126                  dwarf_form_name (attr->form));
14127       return default_value;
14128     }
14129 }
14130
14131 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14132    unit and add it to our queue.
14133    The result is non-zero if PER_CU was queued, otherwise the result is zero
14134    meaning either PER_CU is already queued or it is already loaded.  */
14135
14136 static int
14137 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14138                        struct dwarf2_per_cu_data *per_cu)
14139 {
14140   /* We may arrive here during partial symbol reading, if we need full
14141      DIEs to process an unusual case (e.g. template arguments).  Do
14142      not queue PER_CU, just tell our caller to load its DIEs.  */
14143   if (dwarf2_per_objfile->reading_partial_symbols)
14144     {
14145       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14146         return 1;
14147       return 0;
14148     }
14149
14150   /* Mark the dependence relation so that we don't flush PER_CU
14151      too early.  */
14152   dwarf2_add_dependence (this_cu, per_cu);
14153
14154   /* If it's already on the queue, we have nothing to do.  */
14155   if (per_cu->queued)
14156     return 0;
14157
14158   /* If the compilation unit is already loaded, just mark it as
14159      used.  */
14160   if (per_cu->cu != NULL)
14161     {
14162       per_cu->cu->last_used = 0;
14163       return 0;
14164     }
14165
14166   /* Add it to the queue.  */
14167   queue_comp_unit (per_cu, this_cu->objfile);
14168
14169   return 1;
14170 }
14171
14172 /* Follow reference or signature attribute ATTR of SRC_DIE.
14173    On entry *REF_CU is the CU of SRC_DIE.
14174    On exit *REF_CU is the CU of the result.  */
14175
14176 static struct die_info *
14177 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14178                        struct dwarf2_cu **ref_cu)
14179 {
14180   struct die_info *die;
14181
14182   if (is_ref_attr (attr))
14183     die = follow_die_ref (src_die, attr, ref_cu);
14184   else if (attr->form == DW_FORM_ref_sig8)
14185     die = follow_die_sig (src_die, attr, ref_cu);
14186   else
14187     {
14188       dump_die_for_error (src_die);
14189       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14190              (*ref_cu)->objfile->name);
14191     }
14192
14193   return die;
14194 }
14195
14196 /* Follow reference OFFSET.
14197    On entry *REF_CU is the CU of the source die referencing OFFSET.
14198    On exit *REF_CU is the CU of the result.
14199    Returns NULL if OFFSET is invalid.  */
14200
14201 static struct die_info *
14202 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14203 {
14204   struct die_info temp_die;
14205   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14206
14207   gdb_assert (cu->per_cu != NULL);
14208
14209   target_cu = cu;
14210
14211   if (cu->per_cu->debug_type_section)
14212     {
14213       /* .debug_types CUs cannot reference anything outside their CU.
14214          If they need to, they have to reference a signatured type via
14215          DW_FORM_ref_sig8.  */
14216       if (! offset_in_cu_p (&cu->header, offset))
14217         return NULL;
14218     }
14219   else if (! offset_in_cu_p (&cu->header, offset))
14220     {
14221       struct dwarf2_per_cu_data *per_cu;
14222
14223       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14224
14225       /* If necessary, add it to the queue and load its DIEs.  */
14226       if (maybe_queue_comp_unit (cu, per_cu))
14227         load_full_comp_unit (per_cu, cu->objfile);
14228
14229       target_cu = per_cu->cu;
14230     }
14231   else if (cu->dies == NULL)
14232     {
14233       /* We're loading full DIEs during partial symbol reading.  */
14234       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14235       load_full_comp_unit (cu->per_cu, cu->objfile);
14236     }
14237
14238   *ref_cu = target_cu;
14239   temp_die.offset = offset;
14240   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14241 }
14242
14243 /* Follow reference attribute ATTR of SRC_DIE.
14244    On entry *REF_CU is the CU of SRC_DIE.
14245    On exit *REF_CU is the CU of the result.  */
14246
14247 static struct die_info *
14248 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14249                 struct dwarf2_cu **ref_cu)
14250 {
14251   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14252   struct dwarf2_cu *cu = *ref_cu;
14253   struct die_info *die;
14254
14255   die = follow_die_offset (offset, ref_cu);
14256   if (!die)
14257     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14258            "at 0x%x [in module %s]"),
14259            offset, src_die->offset, cu->objfile->name);
14260
14261   return die;
14262 }
14263
14264 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14265    Returned value is intended for DW_OP_call*.  Returned
14266    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14267
14268 struct dwarf2_locexpr_baton
14269 dwarf2_fetch_die_location_block (unsigned int offset,
14270                                  struct dwarf2_per_cu_data *per_cu,
14271                                  CORE_ADDR (*get_frame_pc) (void *baton),
14272                                  void *baton)
14273 {
14274   struct dwarf2_cu *cu;
14275   struct die_info *die;
14276   struct attribute *attr;
14277   struct dwarf2_locexpr_baton retval;
14278
14279   dw2_setup (per_cu->objfile);
14280
14281   if (per_cu->cu == NULL)
14282     load_cu (per_cu);
14283   cu = per_cu->cu;
14284
14285   die = follow_die_offset (offset, &cu);
14286   if (!die)
14287     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14288            offset, per_cu->cu->objfile->name);
14289
14290   attr = dwarf2_attr (die, DW_AT_location, cu);
14291   if (!attr)
14292     {
14293       /* DWARF: "If there is no such attribute, then there is no effect.".
14294          DATA is ignored if SIZE is 0.  */
14295
14296       retval.data = NULL;
14297       retval.size = 0;
14298     }
14299   else if (attr_form_is_section_offset (attr))
14300     {
14301       struct dwarf2_loclist_baton loclist_baton;
14302       CORE_ADDR pc = (*get_frame_pc) (baton);
14303       size_t size;
14304
14305       fill_in_loclist_baton (cu, &loclist_baton, attr);
14306
14307       retval.data = dwarf2_find_location_expression (&loclist_baton,
14308                                                      &size, pc);
14309       retval.size = size;
14310     }
14311   else
14312     {
14313       if (!attr_form_is_block (attr))
14314         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14315                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14316                offset, per_cu->cu->objfile->name);
14317
14318       retval.data = DW_BLOCK (attr)->data;
14319       retval.size = DW_BLOCK (attr)->size;
14320     }
14321   retval.per_cu = cu->per_cu;
14322
14323   age_cached_comp_units ();
14324
14325   return retval;
14326 }
14327
14328 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14329    PER_CU.  */
14330
14331 struct type *
14332 dwarf2_get_die_type (unsigned int die_offset,
14333                      struct dwarf2_per_cu_data *per_cu)
14334 {
14335   dw2_setup (per_cu->objfile);
14336   return get_die_type_at_offset (die_offset, per_cu);
14337 }
14338
14339 /* Follow the signature attribute ATTR in SRC_DIE.
14340    On entry *REF_CU is the CU of SRC_DIE.
14341    On exit *REF_CU is the CU of the result.  */
14342
14343 static struct die_info *
14344 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14345                 struct dwarf2_cu **ref_cu)
14346 {
14347   struct objfile *objfile = (*ref_cu)->objfile;
14348   struct die_info temp_die;
14349   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14350   struct dwarf2_cu *sig_cu;
14351   struct die_info *die;
14352
14353   /* sig_type will be NULL if the signatured type is missing from
14354      the debug info.  */
14355   if (sig_type == NULL)
14356     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14357              "at 0x%x [in module %s]"),
14358            src_die->offset, objfile->name);
14359
14360   /* If necessary, add it to the queue and load its DIEs.  */
14361
14362   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14363     read_signatured_type (objfile, sig_type);
14364
14365   gdb_assert (sig_type->per_cu.cu != NULL);
14366
14367   sig_cu = sig_type->per_cu.cu;
14368   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14369   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14370   if (die)
14371     {
14372       *ref_cu = sig_cu;
14373       return die;
14374     }
14375
14376   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14377          "from DIE at 0x%x [in module %s]"),
14378          sig_type->type_offset, src_die->offset, objfile->name);
14379 }
14380
14381 /* Given an offset of a signatured type, return its signatured_type.  */
14382
14383 static struct signatured_type *
14384 lookup_signatured_type_at_offset (struct objfile *objfile,
14385                                   struct dwarf2_section_info *section,
14386                                   unsigned int offset)
14387 {
14388   gdb_byte *info_ptr = section->buffer + offset;
14389   unsigned int length, initial_length_size;
14390   unsigned int sig_offset;
14391   struct signatured_type find_entry, *type_sig;
14392
14393   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14394   sig_offset = (initial_length_size
14395                 + 2 /*version*/
14396                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14397                 + 1 /*address_size*/);
14398   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14399   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14400
14401   /* This is only used to lookup previously recorded types.
14402      If we didn't find it, it's our bug.  */
14403   gdb_assert (type_sig != NULL);
14404   gdb_assert (offset == type_sig->per_cu.offset);
14405
14406   return type_sig;
14407 }
14408
14409 /* Read in signatured type at OFFSET and build its CU and die(s).  */
14410
14411 static void
14412 read_signatured_type_at_offset (struct objfile *objfile,
14413                                 struct dwarf2_section_info *sect,
14414                                 unsigned int offset)
14415 {
14416   struct signatured_type *type_sig;
14417
14418   dwarf2_read_section (objfile, sect);
14419
14420   /* We have the section offset, but we need the signature to do the
14421      hash table lookup.  */
14422   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14423
14424   gdb_assert (type_sig->per_cu.cu == NULL);
14425
14426   read_signatured_type (objfile, type_sig);
14427
14428   gdb_assert (type_sig->per_cu.cu != NULL);
14429 }
14430
14431 /* Read in a signatured type and build its CU and DIEs.  */
14432
14433 static void
14434 read_signatured_type (struct objfile *objfile,
14435                       struct signatured_type *type_sig)
14436 {
14437   gdb_byte *types_ptr;
14438   struct die_reader_specs reader_specs;
14439   struct dwarf2_cu *cu;
14440   ULONGEST signature;
14441   struct cleanup *back_to, *free_cu_cleanup;
14442   struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14443
14444   dwarf2_read_section (objfile, section);
14445   types_ptr = section->buffer + type_sig->per_cu.offset;
14446
14447   gdb_assert (type_sig->per_cu.cu == NULL);
14448
14449   cu = xmalloc (sizeof (*cu));
14450   init_one_comp_unit (cu, objfile);
14451
14452   type_sig->per_cu.cu = cu;
14453   cu->per_cu = &type_sig->per_cu;
14454
14455   /* If an error occurs while loading, release our storage.  */
14456   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14457
14458   types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14459                                         types_ptr, objfile->obfd);
14460   gdb_assert (signature == type_sig->signature);
14461
14462   cu->die_hash
14463     = htab_create_alloc_ex (cu->header.length / 12,
14464                             die_hash,
14465                             die_eq,
14466                             NULL,
14467                             &cu->comp_unit_obstack,
14468                             hashtab_obstack_allocate,
14469                             dummy_obstack_deallocate);
14470
14471   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14472   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14473
14474   init_cu_die_reader (&reader_specs, cu);
14475
14476   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14477                                     NULL /*parent*/);
14478
14479   /* We try not to read any attributes in this function, because not
14480      all objfiles needed for references have been loaded yet, and symbol
14481      table processing isn't initialized.  But we have to set the CU language,
14482      or we won't be able to build types correctly.  */
14483   prepare_one_comp_unit (cu, cu->dies);
14484
14485   do_cleanups (back_to);
14486
14487   /* We've successfully allocated this compilation unit.  Let our caller
14488      clean it up when finished with it.  */
14489   discard_cleanups (free_cu_cleanup);
14490
14491   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14492   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14493 }
14494
14495 /* Decode simple location descriptions.
14496    Given a pointer to a dwarf block that defines a location, compute
14497    the location and return the value.
14498
14499    NOTE drow/2003-11-18: This function is called in two situations
14500    now: for the address of static or global variables (partial symbols
14501    only) and for offsets into structures which are expected to be
14502    (more or less) constant.  The partial symbol case should go away,
14503    and only the constant case should remain.  That will let this
14504    function complain more accurately.  A few special modes are allowed
14505    without complaint for global variables (for instance, global
14506    register values and thread-local values).
14507
14508    A location description containing no operations indicates that the
14509    object is optimized out.  The return value is 0 for that case.
14510    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14511    callers will only want a very basic result and this can become a
14512    complaint.
14513
14514    Note that stack[0] is unused except as a default error return.  */
14515
14516 static CORE_ADDR
14517 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14518 {
14519   struct objfile *objfile = cu->objfile;
14520   int i;
14521   int size = blk->size;
14522   gdb_byte *data = blk->data;
14523   CORE_ADDR stack[64];
14524   int stacki;
14525   unsigned int bytes_read, unsnd;
14526   gdb_byte op;
14527
14528   i = 0;
14529   stacki = 0;
14530   stack[stacki] = 0;
14531   stack[++stacki] = 0;
14532
14533   while (i < size)
14534     {
14535       op = data[i++];
14536       switch (op)
14537         {
14538         case DW_OP_lit0:
14539         case DW_OP_lit1:
14540         case DW_OP_lit2:
14541         case DW_OP_lit3:
14542         case DW_OP_lit4:
14543         case DW_OP_lit5:
14544         case DW_OP_lit6:
14545         case DW_OP_lit7:
14546         case DW_OP_lit8:
14547         case DW_OP_lit9:
14548         case DW_OP_lit10:
14549         case DW_OP_lit11:
14550         case DW_OP_lit12:
14551         case DW_OP_lit13:
14552         case DW_OP_lit14:
14553         case DW_OP_lit15:
14554         case DW_OP_lit16:
14555         case DW_OP_lit17:
14556         case DW_OP_lit18:
14557         case DW_OP_lit19:
14558         case DW_OP_lit20:
14559         case DW_OP_lit21:
14560         case DW_OP_lit22:
14561         case DW_OP_lit23:
14562         case DW_OP_lit24:
14563         case DW_OP_lit25:
14564         case DW_OP_lit26:
14565         case DW_OP_lit27:
14566         case DW_OP_lit28:
14567         case DW_OP_lit29:
14568         case DW_OP_lit30:
14569         case DW_OP_lit31:
14570           stack[++stacki] = op - DW_OP_lit0;
14571           break;
14572
14573         case DW_OP_reg0:
14574         case DW_OP_reg1:
14575         case DW_OP_reg2:
14576         case DW_OP_reg3:
14577         case DW_OP_reg4:
14578         case DW_OP_reg5:
14579         case DW_OP_reg6:
14580         case DW_OP_reg7:
14581         case DW_OP_reg8:
14582         case DW_OP_reg9:
14583         case DW_OP_reg10:
14584         case DW_OP_reg11:
14585         case DW_OP_reg12:
14586         case DW_OP_reg13:
14587         case DW_OP_reg14:
14588         case DW_OP_reg15:
14589         case DW_OP_reg16:
14590         case DW_OP_reg17:
14591         case DW_OP_reg18:
14592         case DW_OP_reg19:
14593         case DW_OP_reg20:
14594         case DW_OP_reg21:
14595         case DW_OP_reg22:
14596         case DW_OP_reg23:
14597         case DW_OP_reg24:
14598         case DW_OP_reg25:
14599         case DW_OP_reg26:
14600         case DW_OP_reg27:
14601         case DW_OP_reg28:
14602         case DW_OP_reg29:
14603         case DW_OP_reg30:
14604         case DW_OP_reg31:
14605           stack[++stacki] = op - DW_OP_reg0;
14606           if (i < size)
14607             dwarf2_complex_location_expr_complaint ();
14608           break;
14609
14610         case DW_OP_regx:
14611           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14612           i += bytes_read;
14613           stack[++stacki] = unsnd;
14614           if (i < size)
14615             dwarf2_complex_location_expr_complaint ();
14616           break;
14617
14618         case DW_OP_addr:
14619           stack[++stacki] = read_address (objfile->obfd, &data[i],
14620                                           cu, &bytes_read);
14621           i += bytes_read;
14622           break;
14623
14624         case DW_OP_const1u:
14625           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14626           i += 1;
14627           break;
14628
14629         case DW_OP_const1s:
14630           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14631           i += 1;
14632           break;
14633
14634         case DW_OP_const2u:
14635           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14636           i += 2;
14637           break;
14638
14639         case DW_OP_const2s:
14640           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14641           i += 2;
14642           break;
14643
14644         case DW_OP_const4u:
14645           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14646           i += 4;
14647           break;
14648
14649         case DW_OP_const4s:
14650           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14651           i += 4;
14652           break;
14653
14654         case DW_OP_constu:
14655           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14656                                                   &bytes_read);
14657           i += bytes_read;
14658           break;
14659
14660         case DW_OP_consts:
14661           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14662           i += bytes_read;
14663           break;
14664
14665         case DW_OP_dup:
14666           stack[stacki + 1] = stack[stacki];
14667           stacki++;
14668           break;
14669
14670         case DW_OP_plus:
14671           stack[stacki - 1] += stack[stacki];
14672           stacki--;
14673           break;
14674
14675         case DW_OP_plus_uconst:
14676           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14677                                                  &bytes_read);
14678           i += bytes_read;
14679           break;
14680
14681         case DW_OP_minus:
14682           stack[stacki - 1] -= stack[stacki];
14683           stacki--;
14684           break;
14685
14686         case DW_OP_deref:
14687           /* If we're not the last op, then we definitely can't encode
14688              this using GDB's address_class enum.  This is valid for partial
14689              global symbols, although the variable's address will be bogus
14690              in the psymtab.  */
14691           if (i < size)
14692             dwarf2_complex_location_expr_complaint ();
14693           break;
14694
14695         case DW_OP_GNU_push_tls_address:
14696           /* The top of the stack has the offset from the beginning
14697              of the thread control block at which the variable is located.  */
14698           /* Nothing should follow this operator, so the top of stack would
14699              be returned.  */
14700           /* This is valid for partial global symbols, but the variable's
14701              address will be bogus in the psymtab.  */
14702           if (i < size)
14703             dwarf2_complex_location_expr_complaint ();
14704           break;
14705
14706         case DW_OP_GNU_uninit:
14707           break;
14708
14709         default:
14710           {
14711             const char *name = dwarf_stack_op_name (op);
14712
14713             if (name)
14714               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14715                          name);
14716             else
14717               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14718                          op);
14719           }
14720
14721           return (stack[stacki]);
14722         }
14723
14724       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14725          outside of the allocated space.  Also enforce minimum>0.  */
14726       if (stacki >= ARRAY_SIZE (stack) - 1)
14727         {
14728           complaint (&symfile_complaints,
14729                      _("location description stack overflow"));
14730           return 0;
14731         }
14732
14733       if (stacki <= 0)
14734         {
14735           complaint (&symfile_complaints,
14736                      _("location description stack underflow"));
14737           return 0;
14738         }
14739     }
14740   return (stack[stacki]);
14741 }
14742
14743 /* memory allocation interface */
14744
14745 static struct dwarf_block *
14746 dwarf_alloc_block (struct dwarf2_cu *cu)
14747 {
14748   struct dwarf_block *blk;
14749
14750   blk = (struct dwarf_block *)
14751     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14752   return (blk);
14753 }
14754
14755 static struct abbrev_info *
14756 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14757 {
14758   struct abbrev_info *abbrev;
14759
14760   abbrev = (struct abbrev_info *)
14761     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14762   memset (abbrev, 0, sizeof (struct abbrev_info));
14763   return (abbrev);
14764 }
14765
14766 static struct die_info *
14767 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14768 {
14769   struct die_info *die;
14770   size_t size = sizeof (struct die_info);
14771
14772   if (num_attrs > 1)
14773     size += (num_attrs - 1) * sizeof (struct attribute);
14774
14775   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14776   memset (die, 0, sizeof (struct die_info));
14777   return (die);
14778 }
14779
14780 \f
14781 /* Macro support.  */
14782
14783 /* Return the full name of file number I in *LH's file name table.
14784    Use COMP_DIR as the name of the current directory of the
14785    compilation.  The result is allocated using xmalloc; the caller is
14786    responsible for freeing it.  */
14787 static char *
14788 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14789 {
14790   /* Is the file number a valid index into the line header's file name
14791      table?  Remember that file numbers start with one, not zero.  */
14792   if (1 <= file && file <= lh->num_file_names)
14793     {
14794       struct file_entry *fe = &lh->file_names[file - 1];
14795
14796       if (IS_ABSOLUTE_PATH (fe->name))
14797         return xstrdup (fe->name);
14798       else
14799         {
14800           const char *dir;
14801           int dir_len;
14802           char *full_name;
14803
14804           if (fe->dir_index)
14805             dir = lh->include_dirs[fe->dir_index - 1];
14806           else
14807             dir = comp_dir;
14808
14809           if (dir)
14810             {
14811               dir_len = strlen (dir);
14812               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14813               strcpy (full_name, dir);
14814               full_name[dir_len] = '/';
14815               strcpy (full_name + dir_len + 1, fe->name);
14816               return full_name;
14817             }
14818           else
14819             return xstrdup (fe->name);
14820         }
14821     }
14822   else
14823     {
14824       /* The compiler produced a bogus file number.  We can at least
14825          record the macro definitions made in the file, even if we
14826          won't be able to find the file by name.  */
14827       char fake_name[80];
14828
14829       sprintf (fake_name, "<bad macro file number %d>", file);
14830
14831       complaint (&symfile_complaints,
14832                  _("bad file number in macro information (%d)"),
14833                  file);
14834
14835       return xstrdup (fake_name);
14836     }
14837 }
14838
14839
14840 static struct macro_source_file *
14841 macro_start_file (int file, int line,
14842                   struct macro_source_file *current_file,
14843                   const char *comp_dir,
14844                   struct line_header *lh, struct objfile *objfile)
14845 {
14846   /* The full name of this source file.  */
14847   char *full_name = file_full_name (file, lh, comp_dir);
14848
14849   /* We don't create a macro table for this compilation unit
14850      at all until we actually get a filename.  */
14851   if (! pending_macros)
14852     pending_macros = new_macro_table (&objfile->objfile_obstack,
14853                                       objfile->macro_cache);
14854
14855   if (! current_file)
14856     /* If we have no current file, then this must be the start_file
14857        directive for the compilation unit's main source file.  */
14858     current_file = macro_set_main (pending_macros, full_name);
14859   else
14860     current_file = macro_include (current_file, line, full_name);
14861
14862   xfree (full_name);
14863
14864   return current_file;
14865 }
14866
14867
14868 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14869    followed by a null byte.  */
14870 static char *
14871 copy_string (const char *buf, int len)
14872 {
14873   char *s = xmalloc (len + 1);
14874
14875   memcpy (s, buf, len);
14876   s[len] = '\0';
14877   return s;
14878 }
14879
14880
14881 static const char *
14882 consume_improper_spaces (const char *p, const char *body)
14883 {
14884   if (*p == ' ')
14885     {
14886       complaint (&symfile_complaints,
14887                  _("macro definition contains spaces "
14888                    "in formal argument list:\n`%s'"),
14889                  body);
14890
14891       while (*p == ' ')
14892         p++;
14893     }
14894
14895   return p;
14896 }
14897
14898
14899 static void
14900 parse_macro_definition (struct macro_source_file *file, int line,
14901                         const char *body)
14902 {
14903   const char *p;
14904
14905   /* The body string takes one of two forms.  For object-like macro
14906      definitions, it should be:
14907
14908         <macro name> " " <definition>
14909
14910      For function-like macro definitions, it should be:
14911
14912         <macro name> "() " <definition>
14913      or
14914         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14915
14916      Spaces may appear only where explicitly indicated, and in the
14917      <definition>.
14918
14919      The Dwarf 2 spec says that an object-like macro's name is always
14920      followed by a space, but versions of GCC around March 2002 omit
14921      the space when the macro's definition is the empty string.
14922
14923      The Dwarf 2 spec says that there should be no spaces between the
14924      formal arguments in a function-like macro's formal argument list,
14925      but versions of GCC around March 2002 include spaces after the
14926      commas.  */
14927
14928
14929   /* Find the extent of the macro name.  The macro name is terminated
14930      by either a space or null character (for an object-like macro) or
14931      an opening paren (for a function-like macro).  */
14932   for (p = body; *p; p++)
14933     if (*p == ' ' || *p == '(')
14934       break;
14935
14936   if (*p == ' ' || *p == '\0')
14937     {
14938       /* It's an object-like macro.  */
14939       int name_len = p - body;
14940       char *name = copy_string (body, name_len);
14941       const char *replacement;
14942
14943       if (*p == ' ')
14944         replacement = body + name_len + 1;
14945       else
14946         {
14947           dwarf2_macro_malformed_definition_complaint (body);
14948           replacement = body + name_len;
14949         }
14950
14951       macro_define_object (file, line, name, replacement);
14952
14953       xfree (name);
14954     }
14955   else if (*p == '(')
14956     {
14957       /* It's a function-like macro.  */
14958       char *name = copy_string (body, p - body);
14959       int argc = 0;
14960       int argv_size = 1;
14961       char **argv = xmalloc (argv_size * sizeof (*argv));
14962
14963       p++;
14964
14965       p = consume_improper_spaces (p, body);
14966
14967       /* Parse the formal argument list.  */
14968       while (*p && *p != ')')
14969         {
14970           /* Find the extent of the current argument name.  */
14971           const char *arg_start = p;
14972
14973           while (*p && *p != ',' && *p != ')' && *p != ' ')
14974             p++;
14975
14976           if (! *p || p == arg_start)
14977             dwarf2_macro_malformed_definition_complaint (body);
14978           else
14979             {
14980               /* Make sure argv has room for the new argument.  */
14981               if (argc >= argv_size)
14982                 {
14983                   argv_size *= 2;
14984                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14985                 }
14986
14987               argv[argc++] = copy_string (arg_start, p - arg_start);
14988             }
14989
14990           p = consume_improper_spaces (p, body);
14991
14992           /* Consume the comma, if present.  */
14993           if (*p == ',')
14994             {
14995               p++;
14996
14997               p = consume_improper_spaces (p, body);
14998             }
14999         }
15000
15001       if (*p == ')')
15002         {
15003           p++;
15004
15005           if (*p == ' ')
15006             /* Perfectly formed definition, no complaints.  */
15007             macro_define_function (file, line, name,
15008                                    argc, (const char **) argv,
15009                                    p + 1);
15010           else if (*p == '\0')
15011             {
15012               /* Complain, but do define it.  */
15013               dwarf2_macro_malformed_definition_complaint (body);
15014               macro_define_function (file, line, name,
15015                                      argc, (const char **) argv,
15016                                      p);
15017             }
15018           else
15019             /* Just complain.  */
15020             dwarf2_macro_malformed_definition_complaint (body);
15021         }
15022       else
15023         /* Just complain.  */
15024         dwarf2_macro_malformed_definition_complaint (body);
15025
15026       xfree (name);
15027       {
15028         int i;
15029
15030         for (i = 0; i < argc; i++)
15031           xfree (argv[i]);
15032       }
15033       xfree (argv);
15034     }
15035   else
15036     dwarf2_macro_malformed_definition_complaint (body);
15037 }
15038
15039 /* Skip some bytes from BYTES according to the form given in FORM.
15040    Returns the new pointer.  */
15041
15042 static gdb_byte *
15043 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15044                  enum dwarf_form form,
15045                  unsigned int offset_size,
15046                  struct dwarf2_section_info *section)
15047 {
15048   unsigned int bytes_read;
15049
15050   switch (form)
15051     {
15052     case DW_FORM_data1:
15053     case DW_FORM_flag:
15054       ++bytes;
15055       break;
15056
15057     case DW_FORM_data2:
15058       bytes += 2;
15059       break;
15060
15061     case DW_FORM_data4:
15062       bytes += 4;
15063       break;
15064
15065     case DW_FORM_data8:
15066       bytes += 8;
15067       break;
15068
15069     case DW_FORM_string:
15070       read_direct_string (abfd, bytes, &bytes_read);
15071       bytes += bytes_read;
15072       break;
15073
15074     case DW_FORM_sec_offset:
15075     case DW_FORM_strp:
15076       bytes += offset_size;
15077       break;
15078
15079     case DW_FORM_block:
15080       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15081       bytes += bytes_read;
15082       break;
15083
15084     case DW_FORM_block1:
15085       bytes += 1 + read_1_byte (abfd, bytes);
15086       break;
15087     case DW_FORM_block2:
15088       bytes += 2 + read_2_bytes (abfd, bytes);
15089       break;
15090     case DW_FORM_block4:
15091       bytes += 4 + read_4_bytes (abfd, bytes);
15092       break;
15093
15094     case DW_FORM_sdata:
15095     case DW_FORM_udata:
15096       bytes = skip_leb128 (abfd, bytes);
15097       break;
15098
15099     default:
15100       {
15101       complain:
15102         complaint (&symfile_complaints,
15103                    _("invalid form 0x%x in `%s'"),
15104                    form,
15105                    section->asection->name);
15106         return NULL;
15107       }
15108     }
15109
15110   return bytes;
15111 }
15112
15113 /* A helper for dwarf_decode_macros that handles skipping an unknown
15114    opcode.  Returns an updated pointer to the macro data buffer; or,
15115    on error, issues a complaint and returns NULL.  */
15116
15117 static gdb_byte *
15118 skip_unknown_opcode (unsigned int opcode,
15119                      gdb_byte **opcode_definitions,
15120                      gdb_byte *mac_ptr,
15121                      bfd *abfd,
15122                      unsigned int offset_size,
15123                      struct dwarf2_section_info *section)
15124 {
15125   unsigned int bytes_read, i;
15126   unsigned long arg;
15127   gdb_byte *defn;
15128
15129   if (opcode_definitions[opcode] == NULL)
15130     {
15131       complaint (&symfile_complaints,
15132                  _("unrecognized DW_MACFINO opcode 0x%x"),
15133                  opcode);
15134       return NULL;
15135     }
15136
15137   defn = opcode_definitions[opcode];
15138   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15139   defn += bytes_read;
15140
15141   for (i = 0; i < arg; ++i)
15142     {
15143       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15144       if (mac_ptr == NULL)
15145         {
15146           /* skip_form_bytes already issued the complaint.  */
15147           return NULL;
15148         }
15149     }
15150
15151   return mac_ptr;
15152 }
15153
15154 /* A helper function which parses the header of a macro section.
15155    If the macro section is the extended (for now called "GNU") type,
15156    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15157    the header, or issues a complaint and returns NULL on error.  */
15158
15159 static gdb_byte *
15160 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15161                           bfd *abfd,
15162                           gdb_byte *mac_ptr,
15163                           unsigned int *offset_size,
15164                           int section_is_gnu)
15165 {
15166   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15167
15168   if (section_is_gnu)
15169     {
15170       unsigned int version, flags;
15171
15172       version = read_2_bytes (abfd, mac_ptr);
15173       if (version != 4)
15174         {
15175           complaint (&symfile_complaints,
15176                      _("unrecognized version `%d' in .debug_macro section"),
15177                      version);
15178           return NULL;
15179         }
15180       mac_ptr += 2;
15181
15182       flags = read_1_byte (abfd, mac_ptr);
15183       ++mac_ptr;
15184       *offset_size = (flags & 1) ? 8 : 4;
15185
15186       if ((flags & 2) != 0)
15187         /* We don't need the line table offset.  */
15188         mac_ptr += *offset_size;
15189
15190       /* Vendor opcode descriptions.  */
15191       if ((flags & 4) != 0)
15192         {
15193           unsigned int i, count;
15194
15195           count = read_1_byte (abfd, mac_ptr);
15196           ++mac_ptr;
15197           for (i = 0; i < count; ++i)
15198             {
15199               unsigned int opcode, bytes_read;
15200               unsigned long arg;
15201
15202               opcode = read_1_byte (abfd, mac_ptr);
15203               ++mac_ptr;
15204               opcode_definitions[opcode] = mac_ptr;
15205               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15206               mac_ptr += bytes_read;
15207               mac_ptr += arg;
15208             }
15209         }
15210     }
15211
15212   return mac_ptr;
15213 }
15214
15215 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15216    including DW_GNU_MACINFO_transparent_include.  */
15217
15218 static void
15219 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15220                           struct macro_source_file *current_file,
15221                           struct line_header *lh, char *comp_dir,
15222                           struct dwarf2_section_info *section,
15223                           int section_is_gnu,
15224                           unsigned int offset_size,
15225                           struct objfile *objfile)
15226 {
15227   enum dwarf_macro_record_type macinfo_type;
15228   int at_commandline;
15229   gdb_byte *opcode_definitions[256];
15230
15231   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15232                                       &offset_size, section_is_gnu);
15233   if (mac_ptr == NULL)
15234     {
15235       /* We already issued a complaint.  */
15236       return;
15237     }
15238
15239   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15240      GDB is still reading the definitions from command line.  First
15241      DW_MACINFO_start_file will need to be ignored as it was already executed
15242      to create CURRENT_FILE for the main source holding also the command line
15243      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15244      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15245
15246   at_commandline = 1;
15247
15248   do
15249     {
15250       /* Do we at least have room for a macinfo type byte?  */
15251       if (mac_ptr >= mac_end)
15252         {
15253           dwarf2_macros_too_long_complaint (section);
15254           break;
15255         }
15256
15257       macinfo_type = read_1_byte (abfd, mac_ptr);
15258       mac_ptr++;
15259
15260       /* Note that we rely on the fact that the corresponding GNU and
15261          DWARF constants are the same.  */
15262       switch (macinfo_type)
15263         {
15264           /* A zero macinfo type indicates the end of the macro
15265              information.  */
15266         case 0:
15267           break;
15268
15269         case DW_MACRO_GNU_define:
15270         case DW_MACRO_GNU_undef:
15271         case DW_MACRO_GNU_define_indirect:
15272         case DW_MACRO_GNU_undef_indirect:
15273           {
15274             unsigned int bytes_read;
15275             int line;
15276             char *body;
15277             int is_define;
15278
15279             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15280             mac_ptr += bytes_read;
15281
15282             if (macinfo_type == DW_MACRO_GNU_define
15283                 || macinfo_type == DW_MACRO_GNU_undef)
15284               {
15285                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15286                 mac_ptr += bytes_read;
15287               }
15288             else
15289               {
15290                 LONGEST str_offset;
15291
15292                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15293                 mac_ptr += offset_size;
15294
15295                 body = read_indirect_string_at_offset (abfd, str_offset);
15296               }
15297
15298             is_define = (macinfo_type == DW_MACRO_GNU_define
15299                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15300             if (! current_file)
15301               {
15302                 /* DWARF violation as no main source is present.  */
15303                 complaint (&symfile_complaints,
15304                            _("debug info with no main source gives macro %s "
15305                              "on line %d: %s"),
15306                            is_define ? _("definition") : _("undefinition"),
15307                            line, body);
15308                 break;
15309               }
15310             if ((line == 0 && !at_commandline)
15311                 || (line != 0 && at_commandline))
15312               complaint (&symfile_complaints,
15313                          _("debug info gives %s macro %s with %s line %d: %s"),
15314                          at_commandline ? _("command-line") : _("in-file"),
15315                          is_define ? _("definition") : _("undefinition"),
15316                          line == 0 ? _("zero") : _("non-zero"), line, body);
15317
15318             if (is_define)
15319               parse_macro_definition (current_file, line, body);
15320             else
15321               {
15322                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15323                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15324                 macro_undef (current_file, line, body);
15325               }
15326           }
15327           break;
15328
15329         case DW_MACRO_GNU_start_file:
15330           {
15331             unsigned int bytes_read;
15332             int line, file;
15333
15334             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15335             mac_ptr += bytes_read;
15336             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15337             mac_ptr += bytes_read;
15338
15339             if ((line == 0 && !at_commandline)
15340                 || (line != 0 && at_commandline))
15341               complaint (&symfile_complaints,
15342                          _("debug info gives source %d included "
15343                            "from %s at %s line %d"),
15344                          file, at_commandline ? _("command-line") : _("file"),
15345                          line == 0 ? _("zero") : _("non-zero"), line);
15346
15347             if (at_commandline)
15348               {
15349                 /* This DW_MACRO_GNU_start_file was executed in the
15350                    pass one.  */
15351                 at_commandline = 0;
15352               }
15353             else
15354               current_file = macro_start_file (file, line,
15355                                                current_file, comp_dir,
15356                                                lh, objfile);
15357           }
15358           break;
15359
15360         case DW_MACRO_GNU_end_file:
15361           if (! current_file)
15362             complaint (&symfile_complaints,
15363                        _("macro debug info has an unmatched "
15364                          "`close_file' directive"));
15365           else
15366             {
15367               current_file = current_file->included_by;
15368               if (! current_file)
15369                 {
15370                   enum dwarf_macro_record_type next_type;
15371
15372                   /* GCC circa March 2002 doesn't produce the zero
15373                      type byte marking the end of the compilation
15374                      unit.  Complain if it's not there, but exit no
15375                      matter what.  */
15376
15377                   /* Do we at least have room for a macinfo type byte?  */
15378                   if (mac_ptr >= mac_end)
15379                     {
15380                       dwarf2_macros_too_long_complaint (section);
15381                       return;
15382                     }
15383
15384                   /* We don't increment mac_ptr here, so this is just
15385                      a look-ahead.  */
15386                   next_type = read_1_byte (abfd, mac_ptr);
15387                   if (next_type != 0)
15388                     complaint (&symfile_complaints,
15389                                _("no terminating 0-type entry for "
15390                                  "macros in `.debug_macinfo' section"));
15391
15392                   return;
15393                 }
15394             }
15395           break;
15396
15397         case DW_MACRO_GNU_transparent_include:
15398           {
15399             LONGEST offset;
15400
15401             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15402             mac_ptr += offset_size;
15403
15404             dwarf_decode_macro_bytes (abfd,
15405                                       section->buffer + offset,
15406                                       mac_end, current_file,
15407                                       lh, comp_dir,
15408                                       section, section_is_gnu,
15409                                       offset_size, objfile);
15410           }
15411           break;
15412
15413         case DW_MACINFO_vendor_ext:
15414           if (!section_is_gnu)
15415             {
15416               unsigned int bytes_read;
15417               int constant;
15418
15419               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15420               mac_ptr += bytes_read;
15421               read_direct_string (abfd, mac_ptr, &bytes_read);
15422               mac_ptr += bytes_read;
15423
15424               /* We don't recognize any vendor extensions.  */
15425               break;
15426             }
15427           /* FALLTHROUGH */
15428
15429         default:
15430           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15431                                          mac_ptr, abfd, offset_size,
15432                                          section);
15433           if (mac_ptr == NULL)
15434             return;
15435           break;
15436         }
15437     } while (macinfo_type != 0);
15438 }
15439
15440 static void
15441 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15442                      char *comp_dir, bfd *abfd,
15443                      struct dwarf2_cu *cu,
15444                      struct dwarf2_section_info *section,
15445                      int section_is_gnu)
15446 {
15447   gdb_byte *mac_ptr, *mac_end;
15448   struct macro_source_file *current_file = 0;
15449   enum dwarf_macro_record_type macinfo_type;
15450   unsigned int offset_size = cu->header.offset_size;
15451   gdb_byte *opcode_definitions[256];
15452
15453   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15454   if (section->buffer == NULL)
15455     {
15456       complaint (&symfile_complaints, _("missing %s section"),
15457                  section->asection->name);
15458       return;
15459     }
15460
15461   /* First pass: Find the name of the base filename.
15462      This filename is needed in order to process all macros whose definition
15463      (or undefinition) comes from the command line.  These macros are defined
15464      before the first DW_MACINFO_start_file entry, and yet still need to be
15465      associated to the base file.
15466
15467      To determine the base file name, we scan the macro definitions until we
15468      reach the first DW_MACINFO_start_file entry.  We then initialize
15469      CURRENT_FILE accordingly so that any macro definition found before the
15470      first DW_MACINFO_start_file can still be associated to the base file.  */
15471
15472   mac_ptr = section->buffer + offset;
15473   mac_end = section->buffer + section->size;
15474
15475   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15476                                       &offset_size, section_is_gnu);
15477   if (mac_ptr == NULL)
15478     {
15479       /* We already issued a complaint.  */
15480       return;
15481     }
15482
15483   do
15484     {
15485       /* Do we at least have room for a macinfo type byte?  */
15486       if (mac_ptr >= mac_end)
15487         {
15488           /* Complaint is printed during the second pass as GDB will probably
15489              stop the first pass earlier upon finding
15490              DW_MACINFO_start_file.  */
15491           break;
15492         }
15493
15494       macinfo_type = read_1_byte (abfd, mac_ptr);
15495       mac_ptr++;
15496
15497       /* Note that we rely on the fact that the corresponding GNU and
15498          DWARF constants are the same.  */
15499       switch (macinfo_type)
15500         {
15501           /* A zero macinfo type indicates the end of the macro
15502              information.  */
15503         case 0:
15504           break;
15505
15506         case DW_MACRO_GNU_define:
15507         case DW_MACRO_GNU_undef:
15508           /* Only skip the data by MAC_PTR.  */
15509           {
15510             unsigned int bytes_read;
15511
15512             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15513             mac_ptr += bytes_read;
15514             read_direct_string (abfd, mac_ptr, &bytes_read);
15515             mac_ptr += bytes_read;
15516           }
15517           break;
15518
15519         case DW_MACRO_GNU_start_file:
15520           {
15521             unsigned int bytes_read;
15522             int line, file;
15523
15524             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15525             mac_ptr += bytes_read;
15526             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15527             mac_ptr += bytes_read;
15528
15529             current_file = macro_start_file (file, line, current_file,
15530                                              comp_dir, lh, cu->objfile);
15531           }
15532           break;
15533
15534         case DW_MACRO_GNU_end_file:
15535           /* No data to skip by MAC_PTR.  */
15536           break;
15537
15538         case DW_MACRO_GNU_define_indirect:
15539         case DW_MACRO_GNU_undef_indirect:
15540           {
15541             unsigned int bytes_read;
15542
15543             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15544             mac_ptr += bytes_read;
15545             mac_ptr += offset_size;
15546           }
15547           break;
15548
15549         case DW_MACRO_GNU_transparent_include:
15550           /* Note that, according to the spec, a transparent include
15551              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15552              skip this opcode.  */
15553           mac_ptr += offset_size;
15554           break;
15555
15556         case DW_MACINFO_vendor_ext:
15557           /* Only skip the data by MAC_PTR.  */
15558           if (!section_is_gnu)
15559             {
15560               unsigned int bytes_read;
15561
15562               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15563               mac_ptr += bytes_read;
15564               read_direct_string (abfd, mac_ptr, &bytes_read);
15565               mac_ptr += bytes_read;
15566             }
15567           /* FALLTHROUGH */
15568
15569         default:
15570           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15571                                          mac_ptr, abfd, offset_size,
15572                                          section);
15573           if (mac_ptr == NULL)
15574             return;
15575           break;
15576         }
15577     } while (macinfo_type != 0 && current_file == NULL);
15578
15579   /* Second pass: Process all entries.
15580
15581      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15582      command-line macro definitions/undefinitions.  This flag is unset when we
15583      reach the first DW_MACINFO_start_file entry.  */
15584
15585   dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15586                             current_file, lh, comp_dir, section, section_is_gnu,
15587                             offset_size, cu->objfile);
15588 }
15589
15590 /* Check if the attribute's form is a DW_FORM_block*
15591    if so return true else false.  */
15592 static int
15593 attr_form_is_block (struct attribute *attr)
15594 {
15595   return (attr == NULL ? 0 :
15596       attr->form == DW_FORM_block1
15597       || attr->form == DW_FORM_block2
15598       || attr->form == DW_FORM_block4
15599       || attr->form == DW_FORM_block
15600       || attr->form == DW_FORM_exprloc);
15601 }
15602
15603 /* Return non-zero if ATTR's value is a section offset --- classes
15604    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15605    You may use DW_UNSND (attr) to retrieve such offsets.
15606
15607    Section 7.5.4, "Attribute Encodings", explains that no attribute
15608    may have a value that belongs to more than one of these classes; it
15609    would be ambiguous if we did, because we use the same forms for all
15610    of them.  */
15611 static int
15612 attr_form_is_section_offset (struct attribute *attr)
15613 {
15614   return (attr->form == DW_FORM_data4
15615           || attr->form == DW_FORM_data8
15616           || attr->form == DW_FORM_sec_offset);
15617 }
15618
15619
15620 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15621    zero otherwise.  When this function returns true, you can apply
15622    dwarf2_get_attr_constant_value to it.
15623
15624    However, note that for some attributes you must check
15625    attr_form_is_section_offset before using this test.  DW_FORM_data4
15626    and DW_FORM_data8 are members of both the constant class, and of
15627    the classes that contain offsets into other debug sections
15628    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15629    that, if an attribute's can be either a constant or one of the
15630    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15631    taken as section offsets, not constants.  */
15632 static int
15633 attr_form_is_constant (struct attribute *attr)
15634 {
15635   switch (attr->form)
15636     {
15637     case DW_FORM_sdata:
15638     case DW_FORM_udata:
15639     case DW_FORM_data1:
15640     case DW_FORM_data2:
15641     case DW_FORM_data4:
15642     case DW_FORM_data8:
15643       return 1;
15644     default:
15645       return 0;
15646     }
15647 }
15648
15649 /* A helper function that fills in a dwarf2_loclist_baton.  */
15650
15651 static void
15652 fill_in_loclist_baton (struct dwarf2_cu *cu,
15653                        struct dwarf2_loclist_baton *baton,
15654                        struct attribute *attr)
15655 {
15656   dwarf2_read_section (dwarf2_per_objfile->objfile,
15657                        &dwarf2_per_objfile->loc);
15658
15659   baton->per_cu = cu->per_cu;
15660   gdb_assert (baton->per_cu);
15661   /* We don't know how long the location list is, but make sure we
15662      don't run off the edge of the section.  */
15663   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15664   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15665   baton->base_address = cu->base_address;
15666 }
15667
15668 static void
15669 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15670                              struct dwarf2_cu *cu)
15671 {
15672   if (attr_form_is_section_offset (attr)
15673       /* ".debug_loc" may not exist at all, or the offset may be outside
15674          the section.  If so, fall through to the complaint in the
15675          other branch.  */
15676       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15677                                                 &dwarf2_per_objfile->loc))
15678     {
15679       struct dwarf2_loclist_baton *baton;
15680
15681       baton = obstack_alloc (&cu->objfile->objfile_obstack,
15682                              sizeof (struct dwarf2_loclist_baton));
15683
15684       fill_in_loclist_baton (cu, baton, attr);
15685
15686       if (cu->base_known == 0)
15687         complaint (&symfile_complaints,
15688                    _("Location list used without "
15689                      "specifying the CU base address."));
15690
15691       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15692       SYMBOL_LOCATION_BATON (sym) = baton;
15693     }
15694   else
15695     {
15696       struct dwarf2_locexpr_baton *baton;
15697
15698       baton = obstack_alloc (&cu->objfile->objfile_obstack,
15699                              sizeof (struct dwarf2_locexpr_baton));
15700       baton->per_cu = cu->per_cu;
15701       gdb_assert (baton->per_cu);
15702
15703       if (attr_form_is_block (attr))
15704         {
15705           /* Note that we're just copying the block's data pointer
15706              here, not the actual data.  We're still pointing into the
15707              info_buffer for SYM's objfile; right now we never release
15708              that buffer, but when we do clean up properly this may
15709              need to change.  */
15710           baton->size = DW_BLOCK (attr)->size;
15711           baton->data = DW_BLOCK (attr)->data;
15712         }
15713       else
15714         {
15715           dwarf2_invalid_attrib_class_complaint ("location description",
15716                                                  SYMBOL_NATURAL_NAME (sym));
15717           baton->size = 0;
15718         }
15719
15720       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15721       SYMBOL_LOCATION_BATON (sym) = baton;
15722     }
15723 }
15724
15725 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15726    came from a separate debuginfo file, then the master objfile is
15727    returned.  */
15728
15729 struct objfile *
15730 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15731 {
15732   struct objfile *objfile = per_cu->objfile;
15733
15734   /* Return the master objfile, so that we can report and look up the
15735      correct file containing this variable.  */
15736   if (objfile->separate_debug_objfile_backlink)
15737     objfile = objfile->separate_debug_objfile_backlink;
15738
15739   return objfile;
15740 }
15741
15742 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15743    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15744    CU_HEADERP first.  */
15745
15746 static const struct comp_unit_head *
15747 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15748                        struct dwarf2_per_cu_data *per_cu)
15749 {
15750   struct objfile *objfile;
15751   struct dwarf2_per_objfile *per_objfile;
15752   gdb_byte *info_ptr;
15753
15754   if (per_cu->cu)
15755     return &per_cu->cu->header;
15756
15757   objfile = per_cu->objfile;
15758   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15759   info_ptr = per_objfile->info.buffer + per_cu->offset;
15760
15761   memset (cu_headerp, 0, sizeof (*cu_headerp));
15762   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15763
15764   return cu_headerp;
15765 }
15766
15767 /* Return the address size given in the compilation unit header for CU.  */
15768
15769 CORE_ADDR
15770 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15771 {
15772   struct comp_unit_head cu_header_local;
15773   const struct comp_unit_head *cu_headerp;
15774
15775   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15776
15777   return cu_headerp->addr_size;
15778 }
15779
15780 /* Return the offset size given in the compilation unit header for CU.  */
15781
15782 int
15783 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15784 {
15785   struct comp_unit_head cu_header_local;
15786   const struct comp_unit_head *cu_headerp;
15787
15788   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15789
15790   return cu_headerp->offset_size;
15791 }
15792
15793 /* See its dwarf2loc.h declaration.  */
15794
15795 int
15796 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15797 {
15798   struct comp_unit_head cu_header_local;
15799   const struct comp_unit_head *cu_headerp;
15800
15801   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15802
15803   if (cu_headerp->version == 2)
15804     return cu_headerp->addr_size;
15805   else
15806     return cu_headerp->offset_size;
15807 }
15808
15809 /* Return the text offset of the CU.  The returned offset comes from
15810    this CU's objfile.  If this objfile came from a separate debuginfo
15811    file, then the offset may be different from the corresponding
15812    offset in the parent objfile.  */
15813
15814 CORE_ADDR
15815 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15816 {
15817   struct objfile *objfile = per_cu->objfile;
15818
15819   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15820 }
15821
15822 /* Locate the .debug_info compilation unit from CU's objfile which contains
15823    the DIE at OFFSET.  Raises an error on failure.  */
15824
15825 static struct dwarf2_per_cu_data *
15826 dwarf2_find_containing_comp_unit (unsigned int offset,
15827                                   struct objfile *objfile)
15828 {
15829   struct dwarf2_per_cu_data *this_cu;
15830   int low, high;
15831
15832   low = 0;
15833   high = dwarf2_per_objfile->n_comp_units - 1;
15834   while (high > low)
15835     {
15836       int mid = low + (high - low) / 2;
15837
15838       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15839         high = mid;
15840       else
15841         low = mid + 1;
15842     }
15843   gdb_assert (low == high);
15844   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15845     {
15846       if (low == 0)
15847         error (_("Dwarf Error: could not find partial DIE containing "
15848                "offset 0x%lx [in module %s]"),
15849                (long) offset, bfd_get_filename (objfile->obfd));
15850
15851       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15852       return dwarf2_per_objfile->all_comp_units[low-1];
15853     }
15854   else
15855     {
15856       this_cu = dwarf2_per_objfile->all_comp_units[low];
15857       if (low == dwarf2_per_objfile->n_comp_units - 1
15858           && offset >= this_cu->offset + this_cu->length)
15859         error (_("invalid dwarf2 offset %u"), offset);
15860       gdb_assert (offset < this_cu->offset + this_cu->length);
15861       return this_cu;
15862     }
15863 }
15864
15865 /* Locate the compilation unit from OBJFILE which is located at exactly
15866    OFFSET.  Raises an error on failure.  */
15867
15868 static struct dwarf2_per_cu_data *
15869 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15870 {
15871   struct dwarf2_per_cu_data *this_cu;
15872
15873   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15874   if (this_cu->offset != offset)
15875     error (_("no compilation unit with offset %u."), offset);
15876   return this_cu;
15877 }
15878
15879 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
15880
15881 static void
15882 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15883 {
15884   memset (cu, 0, sizeof (*cu));
15885   cu->objfile = objfile;
15886   obstack_init (&cu->comp_unit_obstack);
15887 }
15888
15889 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15890
15891 static void
15892 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15893 {
15894   struct attribute *attr;
15895
15896   /* Set the language we're debugging.  */
15897   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15898   if (attr)
15899     set_cu_language (DW_UNSND (attr), cu);
15900   else
15901     {
15902       cu->language = language_minimal;
15903       cu->language_defn = language_def (cu->language);
15904     }
15905 }
15906
15907 /* Release one cached compilation unit, CU.  We unlink it from the tree
15908    of compilation units, but we don't remove it from the read_in_chain;
15909    the caller is responsible for that.
15910    NOTE: DATA is a void * because this function is also used as a
15911    cleanup routine.  */
15912
15913 static void
15914 free_one_comp_unit (void *data)
15915 {
15916   struct dwarf2_cu *cu = data;
15917
15918   if (cu->per_cu != NULL)
15919     cu->per_cu->cu = NULL;
15920   cu->per_cu = NULL;
15921
15922   obstack_free (&cu->comp_unit_obstack, NULL);
15923
15924   xfree (cu);
15925 }
15926
15927 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15928    when we're finished with it.  We can't free the pointer itself, but be
15929    sure to unlink it from the cache.  Also release any associated storage
15930    and perform cache maintenance.
15931
15932    Only used during partial symbol parsing.  */
15933
15934 static void
15935 free_stack_comp_unit (void *data)
15936 {
15937   struct dwarf2_cu *cu = data;
15938
15939   obstack_free (&cu->comp_unit_obstack, NULL);
15940   cu->partial_dies = NULL;
15941
15942   if (cu->per_cu != NULL)
15943     {
15944       /* This compilation unit is on the stack in our caller, so we
15945          should not xfree it.  Just unlink it.  */
15946       cu->per_cu->cu = NULL;
15947       cu->per_cu = NULL;
15948
15949       /* If we had a per-cu pointer, then we may have other compilation
15950          units loaded, so age them now.  */
15951       age_cached_comp_units ();
15952     }
15953 }
15954
15955 /* Free all cached compilation units.  */
15956
15957 static void
15958 free_cached_comp_units (void *data)
15959 {
15960   struct dwarf2_per_cu_data *per_cu, **last_chain;
15961
15962   per_cu = dwarf2_per_objfile->read_in_chain;
15963   last_chain = &dwarf2_per_objfile->read_in_chain;
15964   while (per_cu != NULL)
15965     {
15966       struct dwarf2_per_cu_data *next_cu;
15967
15968       next_cu = per_cu->cu->read_in_chain;
15969
15970       free_one_comp_unit (per_cu->cu);
15971       *last_chain = next_cu;
15972
15973       per_cu = next_cu;
15974     }
15975 }
15976
15977 /* Increase the age counter on each cached compilation unit, and free
15978    any that are too old.  */
15979
15980 static void
15981 age_cached_comp_units (void)
15982 {
15983   struct dwarf2_per_cu_data *per_cu, **last_chain;
15984
15985   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15986   per_cu = dwarf2_per_objfile->read_in_chain;
15987   while (per_cu != NULL)
15988     {
15989       per_cu->cu->last_used ++;
15990       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15991         dwarf2_mark (per_cu->cu);
15992       per_cu = per_cu->cu->read_in_chain;
15993     }
15994
15995   per_cu = dwarf2_per_objfile->read_in_chain;
15996   last_chain = &dwarf2_per_objfile->read_in_chain;
15997   while (per_cu != NULL)
15998     {
15999       struct dwarf2_per_cu_data *next_cu;
16000
16001       next_cu = per_cu->cu->read_in_chain;
16002
16003       if (!per_cu->cu->mark)
16004         {
16005           free_one_comp_unit (per_cu->cu);
16006           *last_chain = next_cu;
16007         }
16008       else
16009         last_chain = &per_cu->cu->read_in_chain;
16010
16011       per_cu = next_cu;
16012     }
16013 }
16014
16015 /* Remove a single compilation unit from the cache.  */
16016
16017 static void
16018 free_one_cached_comp_unit (void *target_cu)
16019 {
16020   struct dwarf2_per_cu_data *per_cu, **last_chain;
16021
16022   per_cu = dwarf2_per_objfile->read_in_chain;
16023   last_chain = &dwarf2_per_objfile->read_in_chain;
16024   while (per_cu != NULL)
16025     {
16026       struct dwarf2_per_cu_data *next_cu;
16027
16028       next_cu = per_cu->cu->read_in_chain;
16029
16030       if (per_cu->cu == target_cu)
16031         {
16032           free_one_comp_unit (per_cu->cu);
16033           *last_chain = next_cu;
16034           break;
16035         }
16036       else
16037         last_chain = &per_cu->cu->read_in_chain;
16038
16039       per_cu = next_cu;
16040     }
16041 }
16042
16043 /* Release all extra memory associated with OBJFILE.  */
16044
16045 void
16046 dwarf2_free_objfile (struct objfile *objfile)
16047 {
16048   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16049
16050   if (dwarf2_per_objfile == NULL)
16051     return;
16052
16053   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
16054   free_cached_comp_units (NULL);
16055
16056   if (dwarf2_per_objfile->quick_file_names_table)
16057     htab_delete (dwarf2_per_objfile->quick_file_names_table);
16058
16059   /* Everything else should be on the objfile obstack.  */
16060 }
16061
16062 /* A pair of DIE offset and GDB type pointer.  We store these
16063    in a hash table separate from the DIEs, and preserve them
16064    when the DIEs are flushed out of cache.  */
16065
16066 struct dwarf2_offset_and_type
16067 {
16068   unsigned int offset;
16069   struct type *type;
16070 };
16071
16072 /* Hash function for a dwarf2_offset_and_type.  */
16073
16074 static hashval_t
16075 offset_and_type_hash (const void *item)
16076 {
16077   const struct dwarf2_offset_and_type *ofs = item;
16078
16079   return ofs->offset;
16080 }
16081
16082 /* Equality function for a dwarf2_offset_and_type.  */
16083
16084 static int
16085 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16086 {
16087   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16088   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16089
16090   return ofs_lhs->offset == ofs_rhs->offset;
16091 }
16092
16093 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16094    table if necessary.  For convenience, return TYPE.
16095
16096    The DIEs reading must have careful ordering to:
16097     * Not cause infite loops trying to read in DIEs as a prerequisite for
16098       reading current DIE.
16099     * Not trying to dereference contents of still incompletely read in types
16100       while reading in other DIEs.
16101     * Enable referencing still incompletely read in types just by a pointer to
16102       the type without accessing its fields.
16103
16104    Therefore caller should follow these rules:
16105      * Try to fetch any prerequisite types we may need to build this DIE type
16106        before building the type and calling set_die_type.
16107      * After building type call set_die_type for current DIE as soon as
16108        possible before fetching more types to complete the current type.
16109      * Make the type as complete as possible before fetching more types.  */
16110
16111 static struct type *
16112 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16113 {
16114   struct dwarf2_offset_and_type **slot, ofs;
16115   struct objfile *objfile = cu->objfile;
16116   htab_t *type_hash_ptr;
16117
16118   /* For Ada types, make sure that the gnat-specific data is always
16119      initialized (if not already set).  There are a few types where
16120      we should not be doing so, because the type-specific area is
16121      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16122      where the type-specific area is used to store the floatformat).
16123      But this is not a problem, because the gnat-specific information
16124      is actually not needed for these types.  */
16125   if (need_gnat_info (cu)
16126       && TYPE_CODE (type) != TYPE_CODE_FUNC
16127       && TYPE_CODE (type) != TYPE_CODE_FLT
16128       && !HAVE_GNAT_AUX_INFO (type))
16129     INIT_GNAT_SPECIFIC (type);
16130
16131   if (cu->per_cu->debug_type_section)
16132     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16133   else
16134     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16135
16136   if (*type_hash_ptr == NULL)
16137     {
16138       *type_hash_ptr
16139         = htab_create_alloc_ex (127,
16140                                 offset_and_type_hash,
16141                                 offset_and_type_eq,
16142                                 NULL,
16143                                 &objfile->objfile_obstack,
16144                                 hashtab_obstack_allocate,
16145                                 dummy_obstack_deallocate);
16146     }
16147
16148   ofs.offset = die->offset;
16149   ofs.type = type;
16150   slot = (struct dwarf2_offset_and_type **)
16151     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16152   if (*slot)
16153     complaint (&symfile_complaints,
16154                _("A problem internal to GDB: DIE 0x%x has type already set"),
16155                die->offset);
16156   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16157   **slot = ofs;
16158   return type;
16159 }
16160
16161 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16162    table, or return NULL if the die does not have a saved type.  */
16163
16164 static struct type *
16165 get_die_type_at_offset (unsigned int offset,
16166                         struct dwarf2_per_cu_data *per_cu)
16167 {
16168   struct dwarf2_offset_and_type *slot, ofs;
16169   htab_t type_hash;
16170
16171   if (per_cu->debug_type_section)
16172     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16173   else
16174     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16175   if (type_hash == NULL)
16176     return NULL;
16177
16178   ofs.offset = offset;
16179   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16180   if (slot)
16181     return slot->type;
16182   else
16183     return NULL;
16184 }
16185
16186 /* Look up the type for DIE in the appropriate type_hash table,
16187    or return NULL if DIE does not have a saved type.  */
16188
16189 static struct type *
16190 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16191 {
16192   return get_die_type_at_offset (die->offset, cu->per_cu);
16193 }
16194
16195 /* Add a dependence relationship from CU to REF_PER_CU.  */
16196
16197 static void
16198 dwarf2_add_dependence (struct dwarf2_cu *cu,
16199                        struct dwarf2_per_cu_data *ref_per_cu)
16200 {
16201   void **slot;
16202
16203   if (cu->dependencies == NULL)
16204     cu->dependencies
16205       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16206                               NULL, &cu->comp_unit_obstack,
16207                               hashtab_obstack_allocate,
16208                               dummy_obstack_deallocate);
16209
16210   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16211   if (*slot == NULL)
16212     *slot = ref_per_cu;
16213 }
16214
16215 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16216    Set the mark field in every compilation unit in the
16217    cache that we must keep because we are keeping CU.  */
16218
16219 static int
16220 dwarf2_mark_helper (void **slot, void *data)
16221 {
16222   struct dwarf2_per_cu_data *per_cu;
16223
16224   per_cu = (struct dwarf2_per_cu_data *) *slot;
16225
16226   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16227      reading of the chain.  As such dependencies remain valid it is not much
16228      useful to track and undo them during QUIT cleanups.  */
16229   if (per_cu->cu == NULL)
16230     return 1;
16231
16232   if (per_cu->cu->mark)
16233     return 1;
16234   per_cu->cu->mark = 1;
16235
16236   if (per_cu->cu->dependencies != NULL)
16237     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16238
16239   return 1;
16240 }
16241
16242 /* Set the mark field in CU and in every other compilation unit in the
16243    cache that we must keep because we are keeping CU.  */
16244
16245 static void
16246 dwarf2_mark (struct dwarf2_cu *cu)
16247 {
16248   if (cu->mark)
16249     return;
16250   cu->mark = 1;
16251   if (cu->dependencies != NULL)
16252     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16253 }
16254
16255 static void
16256 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16257 {
16258   while (per_cu)
16259     {
16260       per_cu->cu->mark = 0;
16261       per_cu = per_cu->cu->read_in_chain;
16262     }
16263 }
16264
16265 /* Trivial hash function for partial_die_info: the hash value of a DIE
16266    is its offset in .debug_info for this objfile.  */
16267
16268 static hashval_t
16269 partial_die_hash (const void *item)
16270 {
16271   const struct partial_die_info *part_die = item;
16272
16273   return part_die->offset;
16274 }
16275
16276 /* Trivial comparison function for partial_die_info structures: two DIEs
16277    are equal if they have the same offset.  */
16278
16279 static int
16280 partial_die_eq (const void *item_lhs, const void *item_rhs)
16281 {
16282   const struct partial_die_info *part_die_lhs = item_lhs;
16283   const struct partial_die_info *part_die_rhs = item_rhs;
16284
16285   return part_die_lhs->offset == part_die_rhs->offset;
16286 }
16287
16288 static struct cmd_list_element *set_dwarf2_cmdlist;
16289 static struct cmd_list_element *show_dwarf2_cmdlist;
16290
16291 static void
16292 set_dwarf2_cmd (char *args, int from_tty)
16293 {
16294   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16295 }
16296
16297 static void
16298 show_dwarf2_cmd (char *args, int from_tty)
16299 {
16300   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16301 }
16302
16303 /* If section described by INFO was mmapped, munmap it now.  */
16304
16305 static void
16306 munmap_section_buffer (struct dwarf2_section_info *info)
16307 {
16308   if (info->map_addr != NULL)
16309     {
16310 #ifdef HAVE_MMAP
16311       int res;
16312
16313       res = munmap (info->map_addr, info->map_len);
16314       gdb_assert (res == 0);
16315 #else
16316       /* Without HAVE_MMAP, we should never be here to begin with.  */
16317       gdb_assert_not_reached ("no mmap support");
16318 #endif
16319     }
16320 }
16321
16322 /* munmap debug sections for OBJFILE, if necessary.  */
16323
16324 static void
16325 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16326 {
16327   struct dwarf2_per_objfile *data = d;
16328   int ix;
16329   struct dwarf2_section_info *section;
16330
16331   /* This is sorted according to the order they're defined in to make it easier
16332      to keep in sync.  */
16333   munmap_section_buffer (&data->info);
16334   munmap_section_buffer (&data->abbrev);
16335   munmap_section_buffer (&data->line);
16336   munmap_section_buffer (&data->loc);
16337   munmap_section_buffer (&data->macinfo);
16338   munmap_section_buffer (&data->macro);
16339   munmap_section_buffer (&data->str);
16340   munmap_section_buffer (&data->ranges);
16341   munmap_section_buffer (&data->frame);
16342   munmap_section_buffer (&data->eh_frame);
16343   munmap_section_buffer (&data->gdb_index);
16344
16345   for (ix = 0;
16346        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16347        ++ix)
16348     munmap_section_buffer (section);
16349
16350   VEC_free (dwarf2_section_info_def, data->types);
16351 }
16352
16353 \f
16354 /* The "save gdb-index" command.  */
16355
16356 /* The contents of the hash table we create when building the string
16357    table.  */
16358 struct strtab_entry
16359 {
16360   offset_type offset;
16361   const char *str;
16362 };
16363
16364 /* Hash function for a strtab_entry.
16365
16366    Function is used only during write_hash_table so no index format backward
16367    compatibility is needed.  */
16368
16369 static hashval_t
16370 hash_strtab_entry (const void *e)
16371 {
16372   const struct strtab_entry *entry = e;
16373   return mapped_index_string_hash (INT_MAX, entry->str);
16374 }
16375
16376 /* Equality function for a strtab_entry.  */
16377
16378 static int
16379 eq_strtab_entry (const void *a, const void *b)
16380 {
16381   const struct strtab_entry *ea = a;
16382   const struct strtab_entry *eb = b;
16383   return !strcmp (ea->str, eb->str);
16384 }
16385
16386 /* Create a strtab_entry hash table.  */
16387
16388 static htab_t
16389 create_strtab (void)
16390 {
16391   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16392                             xfree, xcalloc, xfree);
16393 }
16394
16395 /* Add a string to the constant pool.  Return the string's offset in
16396    host order.  */
16397
16398 static offset_type
16399 add_string (htab_t table, struct obstack *cpool, const char *str)
16400 {
16401   void **slot;
16402   struct strtab_entry entry;
16403   struct strtab_entry *result;
16404
16405   entry.str = str;
16406   slot = htab_find_slot (table, &entry, INSERT);
16407   if (*slot)
16408     result = *slot;
16409   else
16410     {
16411       result = XNEW (struct strtab_entry);
16412       result->offset = obstack_object_size (cpool);
16413       result->str = str;
16414       obstack_grow_str0 (cpool, str);
16415       *slot = result;
16416     }
16417   return result->offset;
16418 }
16419
16420 /* An entry in the symbol table.  */
16421 struct symtab_index_entry
16422 {
16423   /* The name of the symbol.  */
16424   const char *name;
16425   /* The offset of the name in the constant pool.  */
16426   offset_type index_offset;
16427   /* A sorted vector of the indices of all the CUs that hold an object
16428      of this name.  */
16429   VEC (offset_type) *cu_indices;
16430 };
16431
16432 /* The symbol table.  This is a power-of-2-sized hash table.  */
16433 struct mapped_symtab
16434 {
16435   offset_type n_elements;
16436   offset_type size;
16437   struct symtab_index_entry **data;
16438 };
16439
16440 /* Hash function for a symtab_index_entry.  */
16441
16442 static hashval_t
16443 hash_symtab_entry (const void *e)
16444 {
16445   const struct symtab_index_entry *entry = e;
16446   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16447                          sizeof (offset_type) * VEC_length (offset_type,
16448                                                             entry->cu_indices),
16449                          0);
16450 }
16451
16452 /* Equality function for a symtab_index_entry.  */
16453
16454 static int
16455 eq_symtab_entry (const void *a, const void *b)
16456 {
16457   const struct symtab_index_entry *ea = a;
16458   const struct symtab_index_entry *eb = b;
16459   int len = VEC_length (offset_type, ea->cu_indices);
16460   if (len != VEC_length (offset_type, eb->cu_indices))
16461     return 0;
16462   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16463                   VEC_address (offset_type, eb->cu_indices),
16464                   sizeof (offset_type) * len);
16465 }
16466
16467 /* Destroy a symtab_index_entry.  */
16468
16469 static void
16470 delete_symtab_entry (void *p)
16471 {
16472   struct symtab_index_entry *entry = p;
16473   VEC_free (offset_type, entry->cu_indices);
16474   xfree (entry);
16475 }
16476
16477 /* Create a hash table holding symtab_index_entry objects.  */
16478
16479 static htab_t
16480 create_symbol_hash_table (void)
16481 {
16482   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16483                             delete_symtab_entry, xcalloc, xfree);
16484 }
16485
16486 /* Create a new mapped symtab object.  */
16487
16488 static struct mapped_symtab *
16489 create_mapped_symtab (void)
16490 {
16491   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16492   symtab->n_elements = 0;
16493   symtab->size = 1024;
16494   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16495   return symtab;
16496 }
16497
16498 /* Destroy a mapped_symtab.  */
16499
16500 static void
16501 cleanup_mapped_symtab (void *p)
16502 {
16503   struct mapped_symtab *symtab = p;
16504   /* The contents of the array are freed when the other hash table is
16505      destroyed.  */
16506   xfree (symtab->data);
16507   xfree (symtab);
16508 }
16509
16510 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16511    the slot.
16512    
16513    Function is used only during write_hash_table so no index format backward
16514    compatibility is needed.  */
16515
16516 static struct symtab_index_entry **
16517 find_slot (struct mapped_symtab *symtab, const char *name)
16518 {
16519   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16520
16521   index = hash & (symtab->size - 1);
16522   step = ((hash * 17) & (symtab->size - 1)) | 1;
16523
16524   for (;;)
16525     {
16526       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16527         return &symtab->data[index];
16528       index = (index + step) & (symtab->size - 1);
16529     }
16530 }
16531
16532 /* Expand SYMTAB's hash table.  */
16533
16534 static void
16535 hash_expand (struct mapped_symtab *symtab)
16536 {
16537   offset_type old_size = symtab->size;
16538   offset_type i;
16539   struct symtab_index_entry **old_entries = symtab->data;
16540
16541   symtab->size *= 2;
16542   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16543
16544   for (i = 0; i < old_size; ++i)
16545     {
16546       if (old_entries[i])
16547         {
16548           struct symtab_index_entry **slot = find_slot (symtab,
16549                                                         old_entries[i]->name);
16550           *slot = old_entries[i];
16551         }
16552     }
16553
16554   xfree (old_entries);
16555 }
16556
16557 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16558    is the index of the CU in which the symbol appears.  */
16559
16560 static void
16561 add_index_entry (struct mapped_symtab *symtab, const char *name,
16562                  offset_type cu_index)
16563 {
16564   struct symtab_index_entry **slot;
16565
16566   ++symtab->n_elements;
16567   if (4 * symtab->n_elements / 3 >= symtab->size)
16568     hash_expand (symtab);
16569
16570   slot = find_slot (symtab, name);
16571   if (!*slot)
16572     {
16573       *slot = XNEW (struct symtab_index_entry);
16574       (*slot)->name = name;
16575       (*slot)->cu_indices = NULL;
16576     }
16577   /* Don't push an index twice.  Due to how we add entries we only
16578      have to check the last one.  */ 
16579   if (VEC_empty (offset_type, (*slot)->cu_indices)
16580       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16581     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16582 }
16583
16584 /* Add a vector of indices to the constant pool.  */
16585
16586 static offset_type
16587 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16588                       struct symtab_index_entry *entry)
16589 {
16590   void **slot;
16591
16592   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16593   if (!*slot)
16594     {
16595       offset_type len = VEC_length (offset_type, entry->cu_indices);
16596       offset_type val = MAYBE_SWAP (len);
16597       offset_type iter;
16598       int i;
16599
16600       *slot = entry;
16601       entry->index_offset = obstack_object_size (cpool);
16602
16603       obstack_grow (cpool, &val, sizeof (val));
16604       for (i = 0;
16605            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16606            ++i)
16607         {
16608           val = MAYBE_SWAP (iter);
16609           obstack_grow (cpool, &val, sizeof (val));
16610         }
16611     }
16612   else
16613     {
16614       struct symtab_index_entry *old_entry = *slot;
16615       entry->index_offset = old_entry->index_offset;
16616       entry = old_entry;
16617     }
16618   return entry->index_offset;
16619 }
16620
16621 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16622    constant pool entries going into the obstack CPOOL.  */
16623
16624 static void
16625 write_hash_table (struct mapped_symtab *symtab,
16626                   struct obstack *output, struct obstack *cpool)
16627 {
16628   offset_type i;
16629   htab_t symbol_hash_table;
16630   htab_t str_table;
16631
16632   symbol_hash_table = create_symbol_hash_table ();
16633   str_table = create_strtab ();
16634
16635   /* We add all the index vectors to the constant pool first, to
16636      ensure alignment is ok.  */
16637   for (i = 0; i < symtab->size; ++i)
16638     {
16639       if (symtab->data[i])
16640         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16641     }
16642
16643   /* Now write out the hash table.  */
16644   for (i = 0; i < symtab->size; ++i)
16645     {
16646       offset_type str_off, vec_off;
16647
16648       if (symtab->data[i])
16649         {
16650           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16651           vec_off = symtab->data[i]->index_offset;
16652         }
16653       else
16654         {
16655           /* While 0 is a valid constant pool index, it is not valid
16656              to have 0 for both offsets.  */
16657           str_off = 0;
16658           vec_off = 0;
16659         }
16660
16661       str_off = MAYBE_SWAP (str_off);
16662       vec_off = MAYBE_SWAP (vec_off);
16663
16664       obstack_grow (output, &str_off, sizeof (str_off));
16665       obstack_grow (output, &vec_off, sizeof (vec_off));
16666     }
16667
16668   htab_delete (str_table);
16669   htab_delete (symbol_hash_table);
16670 }
16671
16672 /* Struct to map psymtab to CU index in the index file.  */
16673 struct psymtab_cu_index_map
16674 {
16675   struct partial_symtab *psymtab;
16676   unsigned int cu_index;
16677 };
16678
16679 static hashval_t
16680 hash_psymtab_cu_index (const void *item)
16681 {
16682   const struct psymtab_cu_index_map *map = item;
16683
16684   return htab_hash_pointer (map->psymtab);
16685 }
16686
16687 static int
16688 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16689 {
16690   const struct psymtab_cu_index_map *lhs = item_lhs;
16691   const struct psymtab_cu_index_map *rhs = item_rhs;
16692
16693   return lhs->psymtab == rhs->psymtab;
16694 }
16695
16696 /* Helper struct for building the address table.  */
16697 struct addrmap_index_data
16698 {
16699   struct objfile *objfile;
16700   struct obstack *addr_obstack;
16701   htab_t cu_index_htab;
16702
16703   /* Non-zero if the previous_* fields are valid.
16704      We can't write an entry until we see the next entry (since it is only then
16705      that we know the end of the entry).  */
16706   int previous_valid;
16707   /* Index of the CU in the table of all CUs in the index file.  */
16708   unsigned int previous_cu_index;
16709   /* Start address of the CU.  */
16710   CORE_ADDR previous_cu_start;
16711 };
16712
16713 /* Write an address entry to OBSTACK.  */
16714
16715 static void
16716 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16717                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16718 {
16719   offset_type cu_index_to_write;
16720   char addr[8];
16721   CORE_ADDR baseaddr;
16722
16723   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16724
16725   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16726   obstack_grow (obstack, addr, 8);
16727   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16728   obstack_grow (obstack, addr, 8);
16729   cu_index_to_write = MAYBE_SWAP (cu_index);
16730   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16731 }
16732
16733 /* Worker function for traversing an addrmap to build the address table.  */
16734
16735 static int
16736 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16737 {
16738   struct addrmap_index_data *data = datap;
16739   struct partial_symtab *pst = obj;
16740   offset_type cu_index;
16741   void **slot;
16742
16743   if (data->previous_valid)
16744     add_address_entry (data->objfile, data->addr_obstack,
16745                        data->previous_cu_start, start_addr,
16746                        data->previous_cu_index);
16747
16748   data->previous_cu_start = start_addr;
16749   if (pst != NULL)
16750     {
16751       struct psymtab_cu_index_map find_map, *map;
16752       find_map.psymtab = pst;
16753       map = htab_find (data->cu_index_htab, &find_map);
16754       gdb_assert (map != NULL);
16755       data->previous_cu_index = map->cu_index;
16756       data->previous_valid = 1;
16757     }
16758   else
16759       data->previous_valid = 0;
16760
16761   return 0;
16762 }
16763
16764 /* Write OBJFILE's address map to OBSTACK.
16765    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16766    in the index file.  */
16767
16768 static void
16769 write_address_map (struct objfile *objfile, struct obstack *obstack,
16770                    htab_t cu_index_htab)
16771 {
16772   struct addrmap_index_data addrmap_index_data;
16773
16774   /* When writing the address table, we have to cope with the fact that
16775      the addrmap iterator only provides the start of a region; we have to
16776      wait until the next invocation to get the start of the next region.  */
16777
16778   addrmap_index_data.objfile = objfile;
16779   addrmap_index_data.addr_obstack = obstack;
16780   addrmap_index_data.cu_index_htab = cu_index_htab;
16781   addrmap_index_data.previous_valid = 0;
16782
16783   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16784                    &addrmap_index_data);
16785
16786   /* It's highly unlikely the last entry (end address = 0xff...ff)
16787      is valid, but we should still handle it.
16788      The end address is recorded as the start of the next region, but that
16789      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16790      anyway.  */
16791   if (addrmap_index_data.previous_valid)
16792     add_address_entry (objfile, obstack,
16793                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16794                        addrmap_index_data.previous_cu_index);
16795 }
16796
16797 /* Add a list of partial symbols to SYMTAB.  */
16798
16799 static void
16800 write_psymbols (struct mapped_symtab *symtab,
16801                 htab_t psyms_seen,
16802                 struct partial_symbol **psymp,
16803                 int count,
16804                 offset_type cu_index,
16805                 int is_static)
16806 {
16807   for (; count-- > 0; ++psymp)
16808     {
16809       void **slot, *lookup;
16810
16811       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16812         error (_("Ada is not currently supported by the index"));
16813
16814       /* We only want to add a given psymbol once.  However, we also
16815          want to account for whether it is global or static.  So, we
16816          may add it twice, using slightly different values.  */
16817       if (is_static)
16818         {
16819           uintptr_t val = 1 | (uintptr_t) *psymp;
16820
16821           lookup = (void *) val;
16822         }
16823       else
16824         lookup = *psymp;
16825
16826       /* Only add a given psymbol once.  */
16827       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16828       if (!*slot)
16829         {
16830           *slot = lookup;
16831           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16832         }
16833     }
16834 }
16835
16836 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16837    exception if there is an error.  */
16838
16839 static void
16840 write_obstack (FILE *file, struct obstack *obstack)
16841 {
16842   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16843               file)
16844       != obstack_object_size (obstack))
16845     error (_("couldn't data write to file"));
16846 }
16847
16848 /* Unlink a file if the argument is not NULL.  */
16849
16850 static void
16851 unlink_if_set (void *p)
16852 {
16853   char **filename = p;
16854   if (*filename)
16855     unlink (*filename);
16856 }
16857
16858 /* A helper struct used when iterating over debug_types.  */
16859 struct signatured_type_index_data
16860 {
16861   struct objfile *objfile;
16862   struct mapped_symtab *symtab;
16863   struct obstack *types_list;
16864   htab_t psyms_seen;
16865   int cu_index;
16866 };
16867
16868 /* A helper function that writes a single signatured_type to an
16869    obstack.  */
16870
16871 static int
16872 write_one_signatured_type (void **slot, void *d)
16873 {
16874   struct signatured_type_index_data *info = d;
16875   struct signatured_type *entry = (struct signatured_type *) *slot;
16876   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16877   struct partial_symtab *psymtab = per_cu->v.psymtab;
16878   gdb_byte val[8];
16879
16880   write_psymbols (info->symtab,
16881                   info->psyms_seen,
16882                   info->objfile->global_psymbols.list
16883                   + psymtab->globals_offset,
16884                   psymtab->n_global_syms, info->cu_index,
16885                   0);
16886   write_psymbols (info->symtab,
16887                   info->psyms_seen,
16888                   info->objfile->static_psymbols.list
16889                   + psymtab->statics_offset,
16890                   psymtab->n_static_syms, info->cu_index,
16891                   1);
16892
16893   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16894   obstack_grow (info->types_list, val, 8);
16895   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16896   obstack_grow (info->types_list, val, 8);
16897   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16898   obstack_grow (info->types_list, val, 8);
16899
16900   ++info->cu_index;
16901
16902   return 1;
16903 }
16904
16905 /* Create an index file for OBJFILE in the directory DIR.  */
16906
16907 static void
16908 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16909 {
16910   struct cleanup *cleanup;
16911   char *filename, *cleanup_filename;
16912   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16913   struct obstack cu_list, types_cu_list;
16914   int i;
16915   FILE *out_file;
16916   struct mapped_symtab *symtab;
16917   offset_type val, size_of_contents, total_len;
16918   struct stat st;
16919   char buf[8];
16920   htab_t psyms_seen;
16921   htab_t cu_index_htab;
16922   struct psymtab_cu_index_map *psymtab_cu_index_map;
16923
16924   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16925     return;
16926
16927   if (dwarf2_per_objfile->using_index)
16928     error (_("Cannot use an index to create the index"));
16929
16930   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16931     error (_("Cannot make an index when the file has multiple .debug_types sections"));
16932
16933   if (stat (objfile->name, &st) < 0)
16934     perror_with_name (objfile->name);
16935
16936   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16937                      INDEX_SUFFIX, (char *) NULL);
16938   cleanup = make_cleanup (xfree, filename);
16939
16940   out_file = fopen (filename, "wb");
16941   if (!out_file)
16942     error (_("Can't open `%s' for writing"), filename);
16943
16944   cleanup_filename = filename;
16945   make_cleanup (unlink_if_set, &cleanup_filename);
16946
16947   symtab = create_mapped_symtab ();
16948   make_cleanup (cleanup_mapped_symtab, symtab);
16949
16950   obstack_init (&addr_obstack);
16951   make_cleanup_obstack_free (&addr_obstack);
16952
16953   obstack_init (&cu_list);
16954   make_cleanup_obstack_free (&cu_list);
16955
16956   obstack_init (&types_cu_list);
16957   make_cleanup_obstack_free (&types_cu_list);
16958
16959   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16960                                   NULL, xcalloc, xfree);
16961   make_cleanup_htab_delete (psyms_seen);
16962
16963   /* While we're scanning CU's create a table that maps a psymtab pointer
16964      (which is what addrmap records) to its index (which is what is recorded
16965      in the index file).  This will later be needed to write the address
16966      table.  */
16967   cu_index_htab = htab_create_alloc (100,
16968                                      hash_psymtab_cu_index,
16969                                      eq_psymtab_cu_index,
16970                                      NULL, xcalloc, xfree);
16971   make_cleanup_htab_delete (cu_index_htab);
16972   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16973     xmalloc (sizeof (struct psymtab_cu_index_map)
16974              * dwarf2_per_objfile->n_comp_units);
16975   make_cleanup (xfree, psymtab_cu_index_map);
16976
16977   /* The CU list is already sorted, so we don't need to do additional
16978      work here.  Also, the debug_types entries do not appear in
16979      all_comp_units, but only in their own hash table.  */
16980   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16981     {
16982       struct dwarf2_per_cu_data *per_cu
16983         = dwarf2_per_objfile->all_comp_units[i];
16984       struct partial_symtab *psymtab = per_cu->v.psymtab;
16985       gdb_byte val[8];
16986       struct psymtab_cu_index_map *map;
16987       void **slot;
16988
16989       write_psymbols (symtab,
16990                       psyms_seen,
16991                       objfile->global_psymbols.list + psymtab->globals_offset,
16992                       psymtab->n_global_syms, i,
16993                       0);
16994       write_psymbols (symtab,
16995                       psyms_seen,
16996                       objfile->static_psymbols.list + psymtab->statics_offset,
16997                       psymtab->n_static_syms, i,
16998                       1);
16999
17000       map = &psymtab_cu_index_map[i];
17001       map->psymtab = psymtab;
17002       map->cu_index = i;
17003       slot = htab_find_slot (cu_index_htab, map, INSERT);
17004       gdb_assert (slot != NULL);
17005       gdb_assert (*slot == NULL);
17006       *slot = map;
17007
17008       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
17009       obstack_grow (&cu_list, val, 8);
17010       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17011       obstack_grow (&cu_list, val, 8);
17012     }
17013
17014   /* Dump the address map.  */
17015   write_address_map (objfile, &addr_obstack, cu_index_htab);
17016
17017   /* Write out the .debug_type entries, if any.  */
17018   if (dwarf2_per_objfile->signatured_types)
17019     {
17020       struct signatured_type_index_data sig_data;
17021
17022       sig_data.objfile = objfile;
17023       sig_data.symtab = symtab;
17024       sig_data.types_list = &types_cu_list;
17025       sig_data.psyms_seen = psyms_seen;
17026       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17027       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17028                               write_one_signatured_type, &sig_data);
17029     }
17030
17031   obstack_init (&constant_pool);
17032   make_cleanup_obstack_free (&constant_pool);
17033   obstack_init (&symtab_obstack);
17034   make_cleanup_obstack_free (&symtab_obstack);
17035   write_hash_table (symtab, &symtab_obstack, &constant_pool);
17036
17037   obstack_init (&contents);
17038   make_cleanup_obstack_free (&contents);
17039   size_of_contents = 6 * sizeof (offset_type);
17040   total_len = size_of_contents;
17041
17042   /* The version number.  */
17043   val = MAYBE_SWAP (5);
17044   obstack_grow (&contents, &val, sizeof (val));
17045
17046   /* The offset of the CU list from the start of the file.  */
17047   val = MAYBE_SWAP (total_len);
17048   obstack_grow (&contents, &val, sizeof (val));
17049   total_len += obstack_object_size (&cu_list);
17050
17051   /* The offset of the types CU list from the start of the file.  */
17052   val = MAYBE_SWAP (total_len);
17053   obstack_grow (&contents, &val, sizeof (val));
17054   total_len += obstack_object_size (&types_cu_list);
17055
17056   /* The offset of the address table from the start of the file.  */
17057   val = MAYBE_SWAP (total_len);
17058   obstack_grow (&contents, &val, sizeof (val));
17059   total_len += obstack_object_size (&addr_obstack);
17060
17061   /* The offset of the symbol table from the start of the file.  */
17062   val = MAYBE_SWAP (total_len);
17063   obstack_grow (&contents, &val, sizeof (val));
17064   total_len += obstack_object_size (&symtab_obstack);
17065
17066   /* The offset of the constant pool from the start of the file.  */
17067   val = MAYBE_SWAP (total_len);
17068   obstack_grow (&contents, &val, sizeof (val));
17069   total_len += obstack_object_size (&constant_pool);
17070
17071   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17072
17073   write_obstack (out_file, &contents);
17074   write_obstack (out_file, &cu_list);
17075   write_obstack (out_file, &types_cu_list);
17076   write_obstack (out_file, &addr_obstack);
17077   write_obstack (out_file, &symtab_obstack);
17078   write_obstack (out_file, &constant_pool);
17079
17080   fclose (out_file);
17081
17082   /* We want to keep the file, so we set cleanup_filename to NULL
17083      here.  See unlink_if_set.  */
17084   cleanup_filename = NULL;
17085
17086   do_cleanups (cleanup);
17087 }
17088
17089 /* Implementation of the `save gdb-index' command.
17090    
17091    Note that the file format used by this command is documented in the
17092    GDB manual.  Any changes here must be documented there.  */
17093
17094 static void
17095 save_gdb_index_command (char *arg, int from_tty)
17096 {
17097   struct objfile *objfile;
17098
17099   if (!arg || !*arg)
17100     error (_("usage: save gdb-index DIRECTORY"));
17101
17102   ALL_OBJFILES (objfile)
17103   {
17104     struct stat st;
17105
17106     /* If the objfile does not correspond to an actual file, skip it.  */
17107     if (stat (objfile->name, &st) < 0)
17108       continue;
17109
17110     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17111     if (dwarf2_per_objfile)
17112       {
17113         volatile struct gdb_exception except;
17114
17115         TRY_CATCH (except, RETURN_MASK_ERROR)
17116           {
17117             write_psymtabs_to_index (objfile, arg);
17118           }
17119         if (except.reason < 0)
17120           exception_fprintf (gdb_stderr, except,
17121                              _("Error while writing index for `%s': "),
17122                              objfile->name);
17123       }
17124   }
17125 }
17126
17127 \f
17128
17129 int dwarf2_always_disassemble;
17130
17131 static void
17132 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17133                                 struct cmd_list_element *c, const char *value)
17134 {
17135   fprintf_filtered (file,
17136                     _("Whether to always disassemble "
17137                       "DWARF expressions is %s.\n"),
17138                     value);
17139 }
17140
17141 static void
17142 show_check_physname (struct ui_file *file, int from_tty,
17143                      struct cmd_list_element *c, const char *value)
17144 {
17145   fprintf_filtered (file,
17146                     _("Whether to check \"physname\" is %s.\n"),
17147                     value);
17148 }
17149
17150 void _initialize_dwarf2_read (void);
17151
17152 void
17153 _initialize_dwarf2_read (void)
17154 {
17155   struct cmd_list_element *c;
17156
17157   dwarf2_objfile_data_key
17158     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17159
17160   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17161 Set DWARF 2 specific variables.\n\
17162 Configure DWARF 2 variables such as the cache size"),
17163                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17164                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17165
17166   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17167 Show DWARF 2 specific variables\n\
17168 Show DWARF 2 variables such as the cache size"),
17169                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17170                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17171
17172   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17173                             &dwarf2_max_cache_age, _("\
17174 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17175 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17176 A higher limit means that cached compilation units will be stored\n\
17177 in memory longer, and more total memory will be used.  Zero disables\n\
17178 caching, which can slow down startup."),
17179                             NULL,
17180                             show_dwarf2_max_cache_age,
17181                             &set_dwarf2_cmdlist,
17182                             &show_dwarf2_cmdlist);
17183
17184   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17185                            &dwarf2_always_disassemble, _("\
17186 Set whether `info address' always disassembles DWARF expressions."), _("\
17187 Show whether `info address' always disassembles DWARF expressions."), _("\
17188 When enabled, DWARF expressions are always printed in an assembly-like\n\
17189 syntax.  When disabled, expressions will be printed in a more\n\
17190 conversational style, when possible."),
17191                            NULL,
17192                            show_dwarf2_always_disassemble,
17193                            &set_dwarf2_cmdlist,
17194                            &show_dwarf2_cmdlist);
17195
17196   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17197 Set debugging of the dwarf2 DIE reader."), _("\
17198 Show debugging of the dwarf2 DIE reader."), _("\
17199 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17200 The value is the maximum depth to print."),
17201                             NULL,
17202                             NULL,
17203                             &setdebuglist, &showdebuglist);
17204
17205   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17206 Set cross-checking of \"physname\" code against demangler."), _("\
17207 Show cross-checking of \"physname\" code against demangler."), _("\
17208 When enabled, GDB's internal \"physname\" code is checked against\n\
17209 the demangler."),
17210                            NULL, show_check_physname,
17211                            &setdebuglist, &showdebuglist);
17212
17213   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17214                _("\
17215 Save a gdb-index file.\n\
17216 Usage: save gdb-index DIRECTORY"),
17217                &save_cmdlist);
17218   set_cmd_completer (c, filename_completer);
17219 }