* gdbtypes.c (check_typedef): Document that this function can
[platform/upstream/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81    Because of alignment constraints, this structure has padding and cannot
82    be mapped directly onto the beginning of the .debug_info section.  */
83 typedef struct comp_unit_header
84   {
85     unsigned int length;        /* length of the .debug_info
86                                    contribution */
87     unsigned short version;     /* version number -- 2 for DWARF
88                                    version 2 */
89     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90     unsigned char addr_size;    /* byte size of an address -- 4 */
91   }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97    Because of alignment constraints, this structure has padding and cannot
98    be mapped directly onto the beginning of the .debug_info section.  */
99 typedef struct statement_prologue
100   {
101     unsigned int total_length;  /* byte length of the statement
102                                    information */
103     unsigned short version;     /* version number -- 2 for DWARF
104                                    version 2 */
105     unsigned int prologue_length;       /* # bytes between prologue &
106                                            stmt program */
107     unsigned char minimum_instruction_length;   /* byte size of
108                                                    smallest instr */
109     unsigned char default_is_stmt;      /* initial value of is_stmt
110                                            register */
111     char line_base;
112     unsigned char line_range;
113     unsigned char opcode_base;  /* number assigned to first special
114                                    opcode */
115     unsigned char *standard_opcode_lengths;
116   }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in.  */
120 static int dwarf2_die_debug = 0;
121
122 /* When non-zero, cross-check physname against demangler.  */
123 static int check_physname = 0;
124
125 static int pagesize;
126
127 /* When set, the file that we're processing is known to have debugging
128    info for C++ namespaces.  GCC 3.3.x did not produce this information,
129    but later versions do.  */
130
131 static int processing_has_namespace_info;
132
133 static const struct objfile_data *dwarf2_objfile_data_key;
134
135 struct dwarf2_section_info
136 {
137   asection *asection;
138   gdb_byte *buffer;
139   bfd_size_type size;
140   /* Not NULL if the section was actually mmapped.  */
141   void *map_addr;
142   /* Page aligned size of mmapped area.  */
143   bfd_size_type map_len;
144   /* True if we have tried to read this section.  */
145   int readin;
146 };
147
148 typedef struct dwarf2_section_info dwarf2_section_info_def;
149 DEF_VEC_O (dwarf2_section_info_def);
150
151 /* All offsets in the index are of this type.  It must be
152    architecture-independent.  */
153 typedef uint32_t offset_type;
154
155 DEF_VEC_I (offset_type);
156
157 /* A description of the mapped index.  The file format is described in
158    a comment by the code that writes the index.  */
159 struct mapped_index
160 {
161   /* Index data format version.  */
162   int version;
163
164   /* The total length of the buffer.  */
165   off_t total_size;
166
167   /* A pointer to the address table data.  */
168   const gdb_byte *address_table;
169
170   /* Size of the address table data in bytes.  */
171   offset_type address_table_size;
172
173   /* The symbol table, implemented as a hash table.  */
174   const offset_type *symbol_table;
175
176   /* Size in slots, each slot is 2 offset_types.  */
177   offset_type symbol_table_slots;
178
179   /* A pointer to the constant pool.  */
180   const char *constant_pool;
181 };
182
183 struct dwarf2_per_objfile
184 {
185   struct dwarf2_section_info info;
186   struct dwarf2_section_info abbrev;
187   struct dwarf2_section_info line;
188   struct dwarf2_section_info loc;
189   struct dwarf2_section_info macinfo;
190   struct dwarf2_section_info macro;
191   struct dwarf2_section_info str;
192   struct dwarf2_section_info ranges;
193   struct dwarf2_section_info frame;
194   struct dwarf2_section_info eh_frame;
195   struct dwarf2_section_info gdb_index;
196
197   VEC (dwarf2_section_info_def) *types;
198
199   /* Back link.  */
200   struct objfile *objfile;
201
202   /* A list of all the compilation units.  This is used to locate
203      the target compilation unit of a particular reference.  */
204   struct dwarf2_per_cu_data **all_comp_units;
205
206   /* The number of compilation units in ALL_COMP_UNITS.  */
207   int n_comp_units;
208
209   /* The number of .debug_types-related CUs.  */
210   int n_type_comp_units;
211
212   /* The .debug_types-related CUs.  */
213   struct dwarf2_per_cu_data **type_comp_units;
214
215   /* A chain of compilation units that are currently read in, so that
216      they can be freed later.  */
217   struct dwarf2_per_cu_data *read_in_chain;
218
219   /* A table mapping .debug_types signatures to its signatured_type entry.
220      This is NULL if the .debug_types section hasn't been read in yet.  */
221   htab_t signatured_types;
222
223   /* A flag indicating wether this objfile has a section loaded at a
224      VMA of 0.  */
225   int has_section_at_zero;
226
227   /* True if we are using the mapped index,
228      or we are faking it for OBJF_READNOW's sake.  */
229   unsigned char using_index;
230
231   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
232   struct mapped_index *index_table;
233
234   /* When using index_table, this keeps track of all quick_file_names entries.
235      TUs can share line table entries with CUs or other TUs, and there can be
236      a lot more TUs than unique line tables, so we maintain a separate table
237      of all line table entries to support the sharing.  */
238   htab_t quick_file_names_table;
239
240   /* Set during partial symbol reading, to prevent queueing of full
241      symbols.  */
242   int reading_partial_symbols;
243
244   /* Table mapping type .debug_info DIE offsets to types.
245      This is NULL if not allocated yet.
246      It (currently) makes sense to allocate debug_types_type_hash lazily.
247      To keep things simple we allocate both lazily.  */
248   htab_t debug_info_type_hash;
249
250   /* Table mapping type .debug_types DIE offsets to types.
251      This is NULL if not allocated yet.  */
252   htab_t debug_types_type_hash;
253 };
254
255 static struct dwarf2_per_objfile *dwarf2_per_objfile;
256
257 /* Default names of the debugging sections.  */
258
259 /* Note that if the debugging section has been compressed, it might
260    have a name like .zdebug_info.  */
261
262 static const struct dwarf2_debug_sections dwarf2_elf_names = {
263   { ".debug_info", ".zdebug_info" },
264   { ".debug_abbrev", ".zdebug_abbrev" },
265   { ".debug_line", ".zdebug_line" },
266   { ".debug_loc", ".zdebug_loc" },
267   { ".debug_macinfo", ".zdebug_macinfo" },
268   { ".debug_macro", ".zdebug_macro" },
269   { ".debug_str", ".zdebug_str" },
270   { ".debug_ranges", ".zdebug_ranges" },
271   { ".debug_types", ".zdebug_types" },
272   { ".debug_frame", ".zdebug_frame" },
273   { ".eh_frame", NULL },
274   { ".gdb_index", ".zgdb_index" },
275   23
276 };
277
278 /* local data types */
279
280 /* We hold several abbreviation tables in memory at the same time.  */
281 #ifndef ABBREV_HASH_SIZE
282 #define ABBREV_HASH_SIZE 121
283 #endif
284
285 /* The data in a compilation unit header, after target2host
286    translation, looks like this.  */
287 struct comp_unit_head
288 {
289   unsigned int length;
290   short version;
291   unsigned char addr_size;
292   unsigned char signed_addr_p;
293   unsigned int abbrev_offset;
294
295   /* Size of file offsets; either 4 or 8.  */
296   unsigned int offset_size;
297
298   /* Size of the length field; either 4 or 12.  */
299   unsigned int initial_length_size;
300
301   /* Offset to the first byte of this compilation unit header in the
302      .debug_info section, for resolving relative reference dies.  */
303   unsigned int offset;
304
305   /* Offset to first die in this cu from the start of the cu.
306      This will be the first byte following the compilation unit header.  */
307   unsigned int first_die_offset;
308 };
309
310 /* Type used for delaying computation of method physnames.
311    See comments for compute_delayed_physnames.  */
312 struct delayed_method_info
313 {
314   /* The type to which the method is attached, i.e., its parent class.  */
315   struct type *type;
316
317   /* The index of the method in the type's function fieldlists.  */
318   int fnfield_index;
319
320   /* The index of the method in the fieldlist.  */
321   int index;
322
323   /* The name of the DIE.  */
324   const char *name;
325
326   /*  The DIE associated with this method.  */
327   struct die_info *die;
328 };
329
330 typedef struct delayed_method_info delayed_method_info;
331 DEF_VEC_O (delayed_method_info);
332
333 /* Internal state when decoding a particular compilation unit.  */
334 struct dwarf2_cu
335 {
336   /* The objfile containing this compilation unit.  */
337   struct objfile *objfile;
338
339   /* The header of the compilation unit.  */
340   struct comp_unit_head header;
341
342   /* Base address of this compilation unit.  */
343   CORE_ADDR base_address;
344
345   /* Non-zero if base_address has been set.  */
346   int base_known;
347
348   struct function_range *first_fn, *last_fn, *cached_fn;
349
350   /* The language we are debugging.  */
351   enum language language;
352   const struct language_defn *language_defn;
353
354   const char *producer;
355
356   /* The generic symbol table building routines have separate lists for
357      file scope symbols and all all other scopes (local scopes).  So
358      we need to select the right one to pass to add_symbol_to_list().
359      We do it by keeping a pointer to the correct list in list_in_scope.
360
361      FIXME: The original dwarf code just treated the file scope as the
362      first local scope, and all other local scopes as nested local
363      scopes, and worked fine.  Check to see if we really need to
364      distinguish these in buildsym.c.  */
365   struct pending **list_in_scope;
366
367   /* DWARF abbreviation table associated with this compilation unit.  */
368   struct abbrev_info **dwarf2_abbrevs;
369
370   /* Storage for the abbrev table.  */
371   struct obstack abbrev_obstack;
372
373   /* Hash table holding all the loaded partial DIEs.  */
374   htab_t partial_dies;
375
376   /* Storage for things with the same lifetime as this read-in compilation
377      unit, including partial DIEs.  */
378   struct obstack comp_unit_obstack;
379
380   /* When multiple dwarf2_cu structures are living in memory, this field
381      chains them all together, so that they can be released efficiently.
382      We will probably also want a generation counter so that most-recently-used
383      compilation units are cached...  */
384   struct dwarf2_per_cu_data *read_in_chain;
385
386   /* Backchain to our per_cu entry if the tree has been built.  */
387   struct dwarf2_per_cu_data *per_cu;
388
389   /* How many compilation units ago was this CU last referenced?  */
390   int last_used;
391
392   /* A hash table of die offsets for following references.  */
393   htab_t die_hash;
394
395   /* Full DIEs if read in.  */
396   struct die_info *dies;
397
398   /* A set of pointers to dwarf2_per_cu_data objects for compilation
399      units referenced by this one.  Only set during full symbol processing;
400      partial symbol tables do not have dependencies.  */
401   htab_t dependencies;
402
403   /* Header data from the line table, during full symbol processing.  */
404   struct line_header *line_header;
405
406   /* A list of methods which need to have physnames computed
407      after all type information has been read.  */
408   VEC (delayed_method_info) *method_list;
409
410   /* To be copied to symtab->call_site_htab.  */
411   htab_t call_site_htab;
412
413   /* Mark used when releasing cached dies.  */
414   unsigned int mark : 1;
415
416   /* This flag will be set if this compilation unit might include
417      inter-compilation-unit references.  */
418   unsigned int has_form_ref_addr : 1;
419
420   /* This flag will be set if this compilation unit includes any
421      DW_TAG_namespace DIEs.  If we know that there are explicit
422      DIEs for namespaces, we don't need to try to infer them
423      from mangled names.  */
424   unsigned int has_namespace_info : 1;
425
426   /* This CU references .debug_loc.  See the symtab->locations_valid field.
427      This test is imperfect as there may exist optimized debug code not using
428      any location list and still facing inlining issues if handled as
429      unoptimized code.  For a future better test see GCC PR other/32998.  */
430
431   unsigned int has_loclist : 1;
432 };
433
434 /* Persistent data held for a compilation unit, even when not
435    processing it.  We put a pointer to this structure in the
436    read_symtab_private field of the psymtab.  If we encounter
437    inter-compilation-unit references, we also maintain a sorted
438    list of all compilation units.  */
439
440 struct dwarf2_per_cu_data
441 {
442   /* The start offset and length of this compilation unit.  2**29-1
443      bytes should suffice to store the length of any compilation unit
444      - if it doesn't, GDB will fall over anyway.
445      NOTE: Unlike comp_unit_head.length, this length includes
446      initial_length_size.  */
447   unsigned int offset;
448   unsigned int length : 29;
449
450   /* Flag indicating this compilation unit will be read in before
451      any of the current compilation units are processed.  */
452   unsigned int queued : 1;
453
454   /* This flag will be set if we need to load absolutely all DIEs
455      for this compilation unit, instead of just the ones we think
456      are interesting.  It gets set if we look for a DIE in the
457      hash table and don't find it.  */
458   unsigned int load_all_dies : 1;
459
460   /* Non-null if this CU is from .debug_types; in which case it points
461      to the section.  Otherwise it's from .debug_info.  */
462   struct dwarf2_section_info *debug_type_section;
463
464   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
465      of the CU cache it gets reset to NULL again.  */
466   struct dwarf2_cu *cu;
467
468   /* The corresponding objfile.  */
469   struct objfile *objfile;
470
471   /* When using partial symbol tables, the 'psymtab' field is active.
472      Otherwise the 'quick' field is active.  */
473   union
474   {
475     /* The partial symbol table associated with this compilation unit,
476        or NULL for partial units (which do not have an associated
477        symtab).  */
478     struct partial_symtab *psymtab;
479
480     /* Data needed by the "quick" functions.  */
481     struct dwarf2_per_cu_quick_data *quick;
482   } v;
483 };
484
485 /* Entry in the signatured_types hash table.  */
486
487 struct signatured_type
488 {
489   ULONGEST signature;
490
491   /* Offset in .debug_types of the type defined by this TU.  */
492   unsigned int type_offset;
493
494   /* The CU(/TU) of this type.  */
495   struct dwarf2_per_cu_data per_cu;
496 };
497
498 /* Struct used to pass misc. parameters to read_die_and_children, et
499    al.  which are used for both .debug_info and .debug_types dies.
500    All parameters here are unchanging for the life of the call.  This
501    struct exists to abstract away the constant parameters of die
502    reading.  */
503
504 struct die_reader_specs
505 {
506   /* The bfd of this objfile.  */
507   bfd* abfd;
508
509   /* The CU of the DIE we are parsing.  */
510   struct dwarf2_cu *cu;
511
512   /* Pointer to start of section buffer.
513      This is either the start of .debug_info or .debug_types.  */
514   const gdb_byte *buffer;
515 };
516
517 /* The line number information for a compilation unit (found in the
518    .debug_line section) begins with a "statement program header",
519    which contains the following information.  */
520 struct line_header
521 {
522   unsigned int total_length;
523   unsigned short version;
524   unsigned int header_length;
525   unsigned char minimum_instruction_length;
526   unsigned char maximum_ops_per_instruction;
527   unsigned char default_is_stmt;
528   int line_base;
529   unsigned char line_range;
530   unsigned char opcode_base;
531
532   /* standard_opcode_lengths[i] is the number of operands for the
533      standard opcode whose value is i.  This means that
534      standard_opcode_lengths[0] is unused, and the last meaningful
535      element is standard_opcode_lengths[opcode_base - 1].  */
536   unsigned char *standard_opcode_lengths;
537
538   /* The include_directories table.  NOTE!  These strings are not
539      allocated with xmalloc; instead, they are pointers into
540      debug_line_buffer.  If you try to free them, `free' will get
541      indigestion.  */
542   unsigned int num_include_dirs, include_dirs_size;
543   char **include_dirs;
544
545   /* The file_names table.  NOTE!  These strings are not allocated
546      with xmalloc; instead, they are pointers into debug_line_buffer.
547      Don't try to free them directly.  */
548   unsigned int num_file_names, file_names_size;
549   struct file_entry
550   {
551     char *name;
552     unsigned int dir_index;
553     unsigned int mod_time;
554     unsigned int length;
555     int included_p; /* Non-zero if referenced by the Line Number Program.  */
556     struct symtab *symtab; /* The associated symbol table, if any.  */
557   } *file_names;
558
559   /* The start and end of the statement program following this
560      header.  These point into dwarf2_per_objfile->line_buffer.  */
561   gdb_byte *statement_program_start, *statement_program_end;
562 };
563
564 /* When we construct a partial symbol table entry we only
565    need this much information.  */
566 struct partial_die_info
567   {
568     /* Offset of this DIE.  */
569     unsigned int offset;
570
571     /* DWARF-2 tag for this DIE.  */
572     ENUM_BITFIELD(dwarf_tag) tag : 16;
573
574     /* Assorted flags describing the data found in this DIE.  */
575     unsigned int has_children : 1;
576     unsigned int is_external : 1;
577     unsigned int is_declaration : 1;
578     unsigned int has_type : 1;
579     unsigned int has_specification : 1;
580     unsigned int has_pc_info : 1;
581
582     /* Flag set if the SCOPE field of this structure has been
583        computed.  */
584     unsigned int scope_set : 1;
585
586     /* Flag set if the DIE has a byte_size attribute.  */
587     unsigned int has_byte_size : 1;
588
589     /* Flag set if any of the DIE's children are template arguments.  */
590     unsigned int has_template_arguments : 1;
591
592     /* Flag set if fixup_partial_die has been called on this die.  */
593     unsigned int fixup_called : 1;
594
595     /* The name of this DIE.  Normally the value of DW_AT_name, but
596        sometimes a default name for unnamed DIEs.  */
597     char *name;
598
599     /* The linkage name, if present.  */
600     const char *linkage_name;
601
602     /* The scope to prepend to our children.  This is generally
603        allocated on the comp_unit_obstack, so will disappear
604        when this compilation unit leaves the cache.  */
605     char *scope;
606
607     /* The location description associated with this DIE, if any.  */
608     struct dwarf_block *locdesc;
609
610     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
611     CORE_ADDR lowpc;
612     CORE_ADDR highpc;
613
614     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
615        DW_AT_sibling, if any.  */
616     /* NOTE: This member isn't strictly necessary, read_partial_die could
617        return DW_AT_sibling values to its caller load_partial_dies.  */
618     gdb_byte *sibling;
619
620     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
621        DW_AT_specification (or DW_AT_abstract_origin or
622        DW_AT_extension).  */
623     unsigned int spec_offset;
624
625     /* Pointers to this DIE's parent, first child, and next sibling,
626        if any.  */
627     struct partial_die_info *die_parent, *die_child, *die_sibling;
628   };
629
630 /* This data structure holds the information of an abbrev.  */
631 struct abbrev_info
632   {
633     unsigned int number;        /* number identifying abbrev */
634     enum dwarf_tag tag;         /* dwarf tag */
635     unsigned short has_children;                /* boolean */
636     unsigned short num_attrs;   /* number of attributes */
637     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
638     struct abbrev_info *next;   /* next in chain */
639   };
640
641 struct attr_abbrev
642   {
643     ENUM_BITFIELD(dwarf_attribute) name : 16;
644     ENUM_BITFIELD(dwarf_form) form : 16;
645   };
646
647 /* Attributes have a name and a value.  */
648 struct attribute
649   {
650     ENUM_BITFIELD(dwarf_attribute) name : 16;
651     ENUM_BITFIELD(dwarf_form) form : 15;
652
653     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
654        field should be in u.str (existing only for DW_STRING) but it is kept
655        here for better struct attribute alignment.  */
656     unsigned int string_is_canonical : 1;
657
658     union
659       {
660         char *str;
661         struct dwarf_block *blk;
662         ULONGEST unsnd;
663         LONGEST snd;
664         CORE_ADDR addr;
665         struct signatured_type *signatured_type;
666       }
667     u;
668   };
669
670 /* This data structure holds a complete die structure.  */
671 struct die_info
672   {
673     /* DWARF-2 tag for this DIE.  */
674     ENUM_BITFIELD(dwarf_tag) tag : 16;
675
676     /* Number of attributes */
677     unsigned char num_attrs;
678
679     /* True if we're presently building the full type name for the
680        type derived from this DIE.  */
681     unsigned char building_fullname : 1;
682
683     /* Abbrev number */
684     unsigned int abbrev;
685
686     /* Offset in .debug_info or .debug_types section.  */
687     unsigned int offset;
688
689     /* The dies in a compilation unit form an n-ary tree.  PARENT
690        points to this die's parent; CHILD points to the first child of
691        this node; and all the children of a given node are chained
692        together via their SIBLING fields.  */
693     struct die_info *child;     /* Its first child, if any.  */
694     struct die_info *sibling;   /* Its next sibling, if any.  */
695     struct die_info *parent;    /* Its parent, if any.  */
696
697     /* An array of attributes, with NUM_ATTRS elements.  There may be
698        zero, but it's not common and zero-sized arrays are not
699        sufficiently portable C.  */
700     struct attribute attrs[1];
701   };
702
703 struct function_range
704 {
705   const char *name;
706   CORE_ADDR lowpc, highpc;
707   int seen_line;
708   struct function_range *next;
709 };
710
711 /* Get at parts of an attribute structure.  */
712
713 #define DW_STRING(attr)    ((attr)->u.str)
714 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
715 #define DW_UNSND(attr)     ((attr)->u.unsnd)
716 #define DW_BLOCK(attr)     ((attr)->u.blk)
717 #define DW_SND(attr)       ((attr)->u.snd)
718 #define DW_ADDR(attr)      ((attr)->u.addr)
719 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
720
721 /* Blocks are a bunch of untyped bytes.  */
722 struct dwarf_block
723   {
724     unsigned int size;
725
726     /* Valid only if SIZE is not zero.  */
727     gdb_byte *data;
728   };
729
730 #ifndef ATTR_ALLOC_CHUNK
731 #define ATTR_ALLOC_CHUNK 4
732 #endif
733
734 /* Allocate fields for structs, unions and enums in this size.  */
735 #ifndef DW_FIELD_ALLOC_CHUNK
736 #define DW_FIELD_ALLOC_CHUNK 4
737 #endif
738
739 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
740    but this would require a corresponding change in unpack_field_as_long
741    and friends.  */
742 static int bits_per_byte = 8;
743
744 /* The routines that read and process dies for a C struct or C++ class
745    pass lists of data member fields and lists of member function fields
746    in an instance of a field_info structure, as defined below.  */
747 struct field_info
748   {
749     /* List of data member and baseclasses fields.  */
750     struct nextfield
751       {
752         struct nextfield *next;
753         int accessibility;
754         int virtuality;
755         struct field field;
756       }
757      *fields, *baseclasses;
758
759     /* Number of fields (including baseclasses).  */
760     int nfields;
761
762     /* Number of baseclasses.  */
763     int nbaseclasses;
764
765     /* Set if the accesibility of one of the fields is not public.  */
766     int non_public_fields;
767
768     /* Member function fields array, entries are allocated in the order they
769        are encountered in the object file.  */
770     struct nextfnfield
771       {
772         struct nextfnfield *next;
773         struct fn_field fnfield;
774       }
775      *fnfields;
776
777     /* Member function fieldlist array, contains name of possibly overloaded
778        member function, number of overloaded member functions and a pointer
779        to the head of the member function field chain.  */
780     struct fnfieldlist
781       {
782         char *name;
783         int length;
784         struct nextfnfield *head;
785       }
786      *fnfieldlists;
787
788     /* Number of entries in the fnfieldlists array.  */
789     int nfnfields;
790
791     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
792        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
793     struct typedef_field_list
794       {
795         struct typedef_field field;
796         struct typedef_field_list *next;
797       }
798     *typedef_field_list;
799     unsigned typedef_field_list_count;
800   };
801
802 /* One item on the queue of compilation units to read in full symbols
803    for.  */
804 struct dwarf2_queue_item
805 {
806   struct dwarf2_per_cu_data *per_cu;
807   struct dwarf2_queue_item *next;
808 };
809
810 /* The current queue.  */
811 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
812
813 /* Loaded secondary compilation units are kept in memory until they
814    have not been referenced for the processing of this many
815    compilation units.  Set this to zero to disable caching.  Cache
816    sizes of up to at least twenty will improve startup time for
817    typical inter-CU-reference binaries, at an obvious memory cost.  */
818 static int dwarf2_max_cache_age = 5;
819 static void
820 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
821                            struct cmd_list_element *c, const char *value)
822 {
823   fprintf_filtered (file, _("The upper bound on the age of cached "
824                             "dwarf2 compilation units is %s.\n"),
825                     value);
826 }
827
828
829 /* Various complaints about symbol reading that don't abort the process.  */
830
831 static void
832 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
833 {
834   complaint (&symfile_complaints,
835              _("statement list doesn't fit in .debug_line section"));
836 }
837
838 static void
839 dwarf2_debug_line_missing_file_complaint (void)
840 {
841   complaint (&symfile_complaints,
842              _(".debug_line section has line data without a file"));
843 }
844
845 static void
846 dwarf2_debug_line_missing_end_sequence_complaint (void)
847 {
848   complaint (&symfile_complaints,
849              _(".debug_line section has line "
850                "program sequence without an end"));
851 }
852
853 static void
854 dwarf2_complex_location_expr_complaint (void)
855 {
856   complaint (&symfile_complaints, _("location expression too complex"));
857 }
858
859 static void
860 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
861                                               int arg3)
862 {
863   complaint (&symfile_complaints,
864              _("const value length mismatch for '%s', got %d, expected %d"),
865              arg1, arg2, arg3);
866 }
867
868 static void
869 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
870 {
871   complaint (&symfile_complaints,
872              _("macro info runs off end of `%s' section"),
873              section->asection->name);
874 }
875
876 static void
877 dwarf2_macro_malformed_definition_complaint (const char *arg1)
878 {
879   complaint (&symfile_complaints,
880              _("macro debug info contains a "
881                "malformed macro definition:\n`%s'"),
882              arg1);
883 }
884
885 static void
886 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
887 {
888   complaint (&symfile_complaints,
889              _("invalid attribute class or form for '%s' in '%s'"),
890              arg1, arg2);
891 }
892
893 /* local function prototypes */
894
895 static void dwarf2_locate_sections (bfd *, asection *, void *);
896
897 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
898                                            struct objfile *);
899
900 static void dwarf2_find_base_address (struct die_info *die,
901                                       struct dwarf2_cu *cu);
902
903 static void dwarf2_build_psymtabs_hard (struct objfile *);
904
905 static void scan_partial_symbols (struct partial_die_info *,
906                                   CORE_ADDR *, CORE_ADDR *,
907                                   int, struct dwarf2_cu *);
908
909 static void add_partial_symbol (struct partial_die_info *,
910                                 struct dwarf2_cu *);
911
912 static void add_partial_namespace (struct partial_die_info *pdi,
913                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
914                                    int need_pc, struct dwarf2_cu *cu);
915
916 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
917                                 CORE_ADDR *highpc, int need_pc,
918                                 struct dwarf2_cu *cu);
919
920 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
921                                      struct dwarf2_cu *cu);
922
923 static void add_partial_subprogram (struct partial_die_info *pdi,
924                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
925                                     int need_pc, struct dwarf2_cu *cu);
926
927 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
928                                      gdb_byte *buffer, gdb_byte *info_ptr,
929                                      bfd *abfd, struct dwarf2_cu *cu);
930
931 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
932
933 static void psymtab_to_symtab_1 (struct partial_symtab *);
934
935 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
936
937 static void dwarf2_free_abbrev_table (void *);
938
939 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
940
941 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
942                                             struct dwarf2_cu *);
943
944 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
945                                                  struct dwarf2_cu *);
946
947 static struct partial_die_info *load_partial_dies (bfd *,
948                                                    gdb_byte *, gdb_byte *,
949                                                    int, struct dwarf2_cu *);
950
951 static gdb_byte *read_partial_die (struct partial_die_info *,
952                                    struct abbrev_info *abbrev,
953                                    unsigned int, bfd *,
954                                    gdb_byte *, gdb_byte *,
955                                    struct dwarf2_cu *);
956
957 static struct partial_die_info *find_partial_die (unsigned int,
958                                                   struct dwarf2_cu *);
959
960 static void fixup_partial_die (struct partial_die_info *,
961                                struct dwarf2_cu *);
962
963 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
964                                  bfd *, gdb_byte *, struct dwarf2_cu *);
965
966 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
967                                        bfd *, gdb_byte *, struct dwarf2_cu *);
968
969 static unsigned int read_1_byte (bfd *, gdb_byte *);
970
971 static int read_1_signed_byte (bfd *, gdb_byte *);
972
973 static unsigned int read_2_bytes (bfd *, gdb_byte *);
974
975 static unsigned int read_4_bytes (bfd *, gdb_byte *);
976
977 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
978
979 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
980                                unsigned int *);
981
982 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
983
984 static LONGEST read_checked_initial_length_and_offset
985   (bfd *, gdb_byte *, const struct comp_unit_head *,
986    unsigned int *, unsigned int *);
987
988 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
989                             unsigned int *);
990
991 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
992
993 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
994
995 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
996
997 static char *read_indirect_string (bfd *, gdb_byte *,
998                                    const struct comp_unit_head *,
999                                    unsigned int *);
1000
1001 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1002
1003 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1004
1005 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1006
1007 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1008
1009 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1010                                       struct dwarf2_cu *);
1011
1012 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1013                                                 unsigned int,
1014                                                 struct dwarf2_cu *);
1015
1016 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1017                                struct dwarf2_cu *cu);
1018
1019 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1020
1021 static struct die_info *die_specification (struct die_info *die,
1022                                            struct dwarf2_cu **);
1023
1024 static void free_line_header (struct line_header *lh);
1025
1026 static void add_file_name (struct line_header *, char *, unsigned int,
1027                            unsigned int, unsigned int);
1028
1029 static struct line_header *(dwarf_decode_line_header
1030                             (unsigned int offset,
1031                              bfd *abfd, struct dwarf2_cu *cu));
1032
1033 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1034                                 struct dwarf2_cu *, struct partial_symtab *);
1035
1036 static void dwarf2_start_subfile (char *, const char *, const char *);
1037
1038 static struct symbol *new_symbol (struct die_info *, struct type *,
1039                                   struct dwarf2_cu *);
1040
1041 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1042                                        struct dwarf2_cu *, struct symbol *);
1043
1044 static void dwarf2_const_value (struct attribute *, struct symbol *,
1045                                 struct dwarf2_cu *);
1046
1047 static void dwarf2_const_value_attr (struct attribute *attr,
1048                                      struct type *type,
1049                                      const char *name,
1050                                      struct obstack *obstack,
1051                                      struct dwarf2_cu *cu, long *value,
1052                                      gdb_byte **bytes,
1053                                      struct dwarf2_locexpr_baton **baton);
1054
1055 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1056
1057 static int need_gnat_info (struct dwarf2_cu *);
1058
1059 static struct type *die_descriptive_type (struct die_info *,
1060                                           struct dwarf2_cu *);
1061
1062 static void set_descriptive_type (struct type *, struct die_info *,
1063                                   struct dwarf2_cu *);
1064
1065 static struct type *die_containing_type (struct die_info *,
1066                                          struct dwarf2_cu *);
1067
1068 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1069                                      struct dwarf2_cu *);
1070
1071 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1072
1073 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1074
1075 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1076
1077 static char *typename_concat (struct obstack *obs, const char *prefix,
1078                               const char *suffix, int physname,
1079                               struct dwarf2_cu *cu);
1080
1081 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1082
1083 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1084
1085 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1086
1087 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1090
1091 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1092                                struct dwarf2_cu *, struct partial_symtab *);
1093
1094 static int dwarf2_get_pc_bounds (struct die_info *,
1095                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1096                                  struct partial_symtab *);
1097
1098 static void get_scope_pc_bounds (struct die_info *,
1099                                  CORE_ADDR *, CORE_ADDR *,
1100                                  struct dwarf2_cu *);
1101
1102 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1103                                         CORE_ADDR, struct dwarf2_cu *);
1104
1105 static void dwarf2_add_field (struct field_info *, struct die_info *,
1106                               struct dwarf2_cu *);
1107
1108 static void dwarf2_attach_fields_to_type (struct field_info *,
1109                                           struct type *, struct dwarf2_cu *);
1110
1111 static void dwarf2_add_member_fn (struct field_info *,
1112                                   struct die_info *, struct type *,
1113                                   struct dwarf2_cu *);
1114
1115 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1116                                              struct type *,
1117                                              struct dwarf2_cu *);
1118
1119 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1120
1121 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1122
1123 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1124
1125 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1126
1127 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1128
1129 static struct type *read_module_type (struct die_info *die,
1130                                       struct dwarf2_cu *cu);
1131
1132 static const char *namespace_name (struct die_info *die,
1133                                    int *is_anonymous, struct dwarf2_cu *);
1134
1135 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1136
1137 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1138
1139 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1140                                                        struct dwarf2_cu *);
1141
1142 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1143
1144 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1145                                                  gdb_byte *info_ptr,
1146                                                  gdb_byte **new_info_ptr,
1147                                                  struct die_info *parent);
1148
1149 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1150                                                gdb_byte *info_ptr,
1151                                                gdb_byte **new_info_ptr,
1152                                                struct die_info *parent);
1153
1154 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1155                                                gdb_byte *info_ptr,
1156                                                gdb_byte **new_info_ptr,
1157                                                struct die_info *parent);
1158
1159 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1160                                 struct die_info **, gdb_byte *,
1161                                 int *);
1162
1163 static void process_die (struct die_info *, struct dwarf2_cu *);
1164
1165 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1166                                        struct obstack *);
1167
1168 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1169
1170 static const char *dwarf2_full_name (char *name,
1171                                      struct die_info *die,
1172                                      struct dwarf2_cu *cu);
1173
1174 static struct die_info *dwarf2_extension (struct die_info *die,
1175                                           struct dwarf2_cu **);
1176
1177 static char *dwarf_tag_name (unsigned int);
1178
1179 static char *dwarf_attr_name (unsigned int);
1180
1181 static char *dwarf_form_name (unsigned int);
1182
1183 static char *dwarf_bool_name (unsigned int);
1184
1185 static char *dwarf_type_encoding_name (unsigned int);
1186
1187 #if 0
1188 static char *dwarf_cfi_name (unsigned int);
1189 #endif
1190
1191 static struct die_info *sibling_die (struct die_info *);
1192
1193 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1194
1195 static void dump_die_for_error (struct die_info *);
1196
1197 static void dump_die_1 (struct ui_file *, int level, int max_level,
1198                         struct die_info *);
1199
1200 /*static*/ void dump_die (struct die_info *, int max_level);
1201
1202 static void store_in_ref_table (struct die_info *,
1203                                 struct dwarf2_cu *);
1204
1205 static int is_ref_attr (struct attribute *);
1206
1207 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1208
1209 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1210
1211 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1212                                                struct attribute *,
1213                                                struct dwarf2_cu **);
1214
1215 static struct die_info *follow_die_ref (struct die_info *,
1216                                         struct attribute *,
1217                                         struct dwarf2_cu **);
1218
1219 static struct die_info *follow_die_sig (struct die_info *,
1220                                         struct attribute *,
1221                                         struct dwarf2_cu **);
1222
1223 static struct signatured_type *lookup_signatured_type_at_offset
1224     (struct objfile *objfile,
1225      struct dwarf2_section_info *section,
1226      unsigned int offset);
1227
1228 static void read_signatured_type_at_offset (struct objfile *objfile,
1229                                             struct dwarf2_section_info *sect,
1230                                             unsigned int offset);
1231
1232 static void read_signatured_type (struct objfile *,
1233                                   struct signatured_type *type_sig);
1234
1235 /* memory allocation interface */
1236
1237 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1238
1239 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1240
1241 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1242
1243 static void initialize_cu_func_list (struct dwarf2_cu *);
1244
1245 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1246                                  struct dwarf2_cu *);
1247
1248 static void dwarf_decode_macros (struct line_header *, unsigned int,
1249                                  char *, bfd *, struct dwarf2_cu *,
1250                                  struct dwarf2_section_info *,
1251                                  int);
1252
1253 static int attr_form_is_block (struct attribute *);
1254
1255 static int attr_form_is_section_offset (struct attribute *);
1256
1257 static int attr_form_is_constant (struct attribute *);
1258
1259 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1260                                    struct dwarf2_loclist_baton *baton,
1261                                    struct attribute *attr);
1262
1263 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1264                                          struct symbol *sym,
1265                                          struct dwarf2_cu *cu);
1266
1267 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1268                                struct abbrev_info *abbrev,
1269                                struct dwarf2_cu *cu);
1270
1271 static void free_stack_comp_unit (void *);
1272
1273 static hashval_t partial_die_hash (const void *item);
1274
1275 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1276
1277 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1278   (unsigned int offset, struct objfile *objfile);
1279
1280 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1281   (unsigned int offset, struct objfile *objfile);
1282
1283 static void init_one_comp_unit (struct dwarf2_cu *cu,
1284                                 struct objfile *objfile);
1285
1286 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1287                                    struct die_info *comp_unit_die);
1288
1289 static void free_one_comp_unit (void *);
1290
1291 static void free_cached_comp_units (void *);
1292
1293 static void age_cached_comp_units (void);
1294
1295 static void free_one_cached_comp_unit (void *);
1296
1297 static struct type *set_die_type (struct die_info *, struct type *,
1298                                   struct dwarf2_cu *);
1299
1300 static void create_all_comp_units (struct objfile *);
1301
1302 static int create_debug_types_hash_table (struct objfile *objfile);
1303
1304 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1305                                  struct objfile *);
1306
1307 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1308
1309 static void dwarf2_add_dependence (struct dwarf2_cu *,
1310                                    struct dwarf2_per_cu_data *);
1311
1312 static void dwarf2_mark (struct dwarf2_cu *);
1313
1314 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1315
1316 static struct type *get_die_type_at_offset (unsigned int,
1317                                             struct dwarf2_per_cu_data *per_cu);
1318
1319 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1320
1321 static void dwarf2_release_queue (void *dummy);
1322
1323 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1324                              struct objfile *objfile);
1325
1326 static void process_queue (struct objfile *objfile);
1327
1328 static void find_file_and_directory (struct die_info *die,
1329                                      struct dwarf2_cu *cu,
1330                                      char **name, char **comp_dir);
1331
1332 static char *file_full_name (int file, struct line_header *lh,
1333                              const char *comp_dir);
1334
1335 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1336                                               gdb_byte *info_ptr,
1337                                               gdb_byte *buffer,
1338                                               unsigned int buffer_size,
1339                                               bfd *abfd,
1340                                               int is_debug_type_section);
1341
1342 static void init_cu_die_reader (struct die_reader_specs *reader,
1343                                 struct dwarf2_cu *cu);
1344
1345 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1346
1347 #if WORDS_BIGENDIAN
1348
1349 /* Convert VALUE between big- and little-endian.  */
1350 static offset_type
1351 byte_swap (offset_type value)
1352 {
1353   offset_type result;
1354
1355   result = (value & 0xff) << 24;
1356   result |= (value & 0xff00) << 8;
1357   result |= (value & 0xff0000) >> 8;
1358   result |= (value & 0xff000000) >> 24;
1359   return result;
1360 }
1361
1362 #define MAYBE_SWAP(V)  byte_swap (V)
1363
1364 #else
1365 #define MAYBE_SWAP(V) (V)
1366 #endif /* WORDS_BIGENDIAN */
1367
1368 /* The suffix for an index file.  */
1369 #define INDEX_SUFFIX ".gdb-index"
1370
1371 static const char *dwarf2_physname (char *name, struct die_info *die,
1372                                     struct dwarf2_cu *cu);
1373
1374 /* Try to locate the sections we need for DWARF 2 debugging
1375    information and return true if we have enough to do something.
1376    NAMES points to the dwarf2 section names, or is NULL if the standard
1377    ELF names are used.  */
1378
1379 int
1380 dwarf2_has_info (struct objfile *objfile,
1381                  const struct dwarf2_debug_sections *names)
1382 {
1383   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1384   if (!dwarf2_per_objfile)
1385     {
1386       /* Initialize per-objfile state.  */
1387       struct dwarf2_per_objfile *data
1388         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1389
1390       memset (data, 0, sizeof (*data));
1391       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1392       dwarf2_per_objfile = data;
1393
1394       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1395                              (void *) names);
1396       dwarf2_per_objfile->objfile = objfile;
1397     }
1398   return (dwarf2_per_objfile->info.asection != NULL
1399           && dwarf2_per_objfile->abbrev.asection != NULL);
1400 }
1401
1402 /* When loading sections, we look either for uncompressed section or for
1403    compressed section names.  */
1404
1405 static int
1406 section_is_p (const char *section_name,
1407               const struct dwarf2_section_names *names)
1408 {
1409   if (names->normal != NULL
1410       && strcmp (section_name, names->normal) == 0)
1411     return 1;
1412   if (names->compressed != NULL
1413       && strcmp (section_name, names->compressed) == 0)
1414     return 1;
1415   return 0;
1416 }
1417
1418 /* This function is mapped across the sections and remembers the
1419    offset and size of each of the debugging sections we are interested
1420    in.  */
1421
1422 static void
1423 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1424 {
1425   const struct dwarf2_debug_sections *names;
1426
1427   if (vnames == NULL)
1428     names = &dwarf2_elf_names;
1429   else
1430     names = (const struct dwarf2_debug_sections *) vnames;
1431
1432   if (section_is_p (sectp->name, &names->info))
1433     {
1434       dwarf2_per_objfile->info.asection = sectp;
1435       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1436     }
1437   else if (section_is_p (sectp->name, &names->abbrev))
1438     {
1439       dwarf2_per_objfile->abbrev.asection = sectp;
1440       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1441     }
1442   else if (section_is_p (sectp->name, &names->line))
1443     {
1444       dwarf2_per_objfile->line.asection = sectp;
1445       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1446     }
1447   else if (section_is_p (sectp->name, &names->loc))
1448     {
1449       dwarf2_per_objfile->loc.asection = sectp;
1450       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1451     }
1452   else if (section_is_p (sectp->name, &names->macinfo))
1453     {
1454       dwarf2_per_objfile->macinfo.asection = sectp;
1455       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1456     }
1457   else if (section_is_p (sectp->name, &names->macro))
1458     {
1459       dwarf2_per_objfile->macro.asection = sectp;
1460       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1461     }
1462   else if (section_is_p (sectp->name, &names->str))
1463     {
1464       dwarf2_per_objfile->str.asection = sectp;
1465       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1466     }
1467   else if (section_is_p (sectp->name, &names->frame))
1468     {
1469       dwarf2_per_objfile->frame.asection = sectp;
1470       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1471     }
1472   else if (section_is_p (sectp->name, &names->eh_frame))
1473     {
1474       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1475
1476       if (aflag & SEC_HAS_CONTENTS)
1477         {
1478           dwarf2_per_objfile->eh_frame.asection = sectp;
1479           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1480         }
1481     }
1482   else if (section_is_p (sectp->name, &names->ranges))
1483     {
1484       dwarf2_per_objfile->ranges.asection = sectp;
1485       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1486     }
1487   else if (section_is_p (sectp->name, &names->types))
1488     {
1489       struct dwarf2_section_info type_section;
1490
1491       memset (&type_section, 0, sizeof (type_section));
1492       type_section.asection = sectp;
1493       type_section.size = bfd_get_section_size (sectp);
1494
1495       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1496                      &type_section);
1497     }
1498   else if (section_is_p (sectp->name, &names->gdb_index))
1499     {
1500       dwarf2_per_objfile->gdb_index.asection = sectp;
1501       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1502     }
1503
1504   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1505       && bfd_section_vma (abfd, sectp) == 0)
1506     dwarf2_per_objfile->has_section_at_zero = 1;
1507 }
1508
1509 /* Decompress a section that was compressed using zlib.  Store the
1510    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1511
1512 static void
1513 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1514                          gdb_byte **outbuf, bfd_size_type *outsize)
1515 {
1516   bfd *abfd = objfile->obfd;
1517 #ifndef HAVE_ZLIB_H
1518   error (_("Support for zlib-compressed DWARF data (from '%s') "
1519            "is disabled in this copy of GDB"),
1520          bfd_get_filename (abfd));
1521 #else
1522   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1523   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1524   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1525   bfd_size_type uncompressed_size;
1526   gdb_byte *uncompressed_buffer;
1527   z_stream strm;
1528   int rc;
1529   int header_size = 12;
1530
1531   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1532       || bfd_bread (compressed_buffer,
1533                     compressed_size, abfd) != compressed_size)
1534     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1535            bfd_get_filename (abfd));
1536
1537   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1538      by the uncompressed section size, 8 bytes in big-endian order.  */
1539   if (compressed_size < header_size
1540       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1541     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1542            bfd_get_filename (abfd));
1543   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1544   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1545   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1546   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1547   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1548   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1549   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1550   uncompressed_size += compressed_buffer[11];
1551
1552   /* It is possible the section consists of several compressed
1553      buffers concatenated together, so we uncompress in a loop.  */
1554   strm.zalloc = NULL;
1555   strm.zfree = NULL;
1556   strm.opaque = NULL;
1557   strm.avail_in = compressed_size - header_size;
1558   strm.next_in = (Bytef*) compressed_buffer + header_size;
1559   strm.avail_out = uncompressed_size;
1560   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1561                                        uncompressed_size);
1562   rc = inflateInit (&strm);
1563   while (strm.avail_in > 0)
1564     {
1565       if (rc != Z_OK)
1566         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1567                bfd_get_filename (abfd), rc);
1568       strm.next_out = ((Bytef*) uncompressed_buffer
1569                        + (uncompressed_size - strm.avail_out));
1570       rc = inflate (&strm, Z_FINISH);
1571       if (rc != Z_STREAM_END)
1572         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1573                bfd_get_filename (abfd), rc);
1574       rc = inflateReset (&strm);
1575     }
1576   rc = inflateEnd (&strm);
1577   if (rc != Z_OK
1578       || strm.avail_out != 0)
1579     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1580            bfd_get_filename (abfd), rc);
1581
1582   do_cleanups (cleanup);
1583   *outbuf = uncompressed_buffer;
1584   *outsize = uncompressed_size;
1585 #endif
1586 }
1587
1588 /* A helper function that decides whether a section is empty.  */
1589
1590 static int
1591 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1592 {
1593   return info->asection == NULL || info->size == 0;
1594 }
1595
1596 /* Read the contents of the section SECTP from object file specified by
1597    OBJFILE, store info about the section into INFO.
1598    If the section is compressed, uncompress it before returning.  */
1599
1600 static void
1601 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1602 {
1603   bfd *abfd = objfile->obfd;
1604   asection *sectp = info->asection;
1605   gdb_byte *buf, *retbuf;
1606   unsigned char header[4];
1607
1608   if (info->readin)
1609     return;
1610   info->buffer = NULL;
1611   info->map_addr = NULL;
1612   info->readin = 1;
1613
1614   if (dwarf2_section_empty_p (info))
1615     return;
1616
1617   /* Check if the file has a 4-byte header indicating compression.  */
1618   if (info->size > sizeof (header)
1619       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1620       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1621     {
1622       /* Upon decompression, update the buffer and its size.  */
1623       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1624         {
1625           zlib_decompress_section (objfile, sectp, &info->buffer,
1626                                    &info->size);
1627           return;
1628         }
1629     }
1630
1631 #ifdef HAVE_MMAP
1632   if (pagesize == 0)
1633     pagesize = getpagesize ();
1634
1635   /* Only try to mmap sections which are large enough: we don't want to
1636      waste space due to fragmentation.  Also, only try mmap for sections
1637      without relocations.  */
1638
1639   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1640     {
1641       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1642                          MAP_PRIVATE, sectp->filepos,
1643                          &info->map_addr, &info->map_len);
1644
1645       if ((caddr_t)info->buffer != MAP_FAILED)
1646         {
1647 #if HAVE_POSIX_MADVISE
1648           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1649 #endif
1650           return;
1651         }
1652     }
1653 #endif
1654
1655   /* If we get here, we are a normal, not-compressed section.  */
1656   info->buffer = buf
1657     = obstack_alloc (&objfile->objfile_obstack, info->size);
1658
1659   /* When debugging .o files, we may need to apply relocations; see
1660      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1661      We never compress sections in .o files, so we only need to
1662      try this when the section is not compressed.  */
1663   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1664   if (retbuf != NULL)
1665     {
1666       info->buffer = retbuf;
1667       return;
1668     }
1669
1670   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1671       || bfd_bread (buf, info->size, abfd) != info->size)
1672     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1673            bfd_get_filename (abfd));
1674 }
1675
1676 /* A helper function that returns the size of a section in a safe way.
1677    If you are positive that the section has been read before using the
1678    size, then it is safe to refer to the dwarf2_section_info object's
1679    "size" field directly.  In other cases, you must call this
1680    function, because for compressed sections the size field is not set
1681    correctly until the section has been read.  */
1682
1683 static bfd_size_type
1684 dwarf2_section_size (struct objfile *objfile,
1685                      struct dwarf2_section_info *info)
1686 {
1687   if (!info->readin)
1688     dwarf2_read_section (objfile, info);
1689   return info->size;
1690 }
1691
1692 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1693    SECTION_NAME.  */
1694
1695 void
1696 dwarf2_get_section_info (struct objfile *objfile,
1697                          enum dwarf2_section_enum sect,
1698                          asection **sectp, gdb_byte **bufp,
1699                          bfd_size_type *sizep)
1700 {
1701   struct dwarf2_per_objfile *data
1702     = objfile_data (objfile, dwarf2_objfile_data_key);
1703   struct dwarf2_section_info *info;
1704
1705   /* We may see an objfile without any DWARF, in which case we just
1706      return nothing.  */
1707   if (data == NULL)
1708     {
1709       *sectp = NULL;
1710       *bufp = NULL;
1711       *sizep = 0;
1712       return;
1713     }
1714   switch (sect)
1715     {
1716     case DWARF2_DEBUG_FRAME:
1717       info = &data->frame;
1718       break;
1719     case DWARF2_EH_FRAME:
1720       info = &data->eh_frame;
1721       break;
1722     default:
1723       gdb_assert_not_reached ("unexpected section");
1724     }
1725
1726   dwarf2_read_section (objfile, info);
1727
1728   *sectp = info->asection;
1729   *bufp = info->buffer;
1730   *sizep = info->size;
1731 }
1732
1733 \f
1734 /* DWARF quick_symbols_functions support.  */
1735
1736 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1737    unique line tables, so we maintain a separate table of all .debug_line
1738    derived entries to support the sharing.
1739    All the quick functions need is the list of file names.  We discard the
1740    line_header when we're done and don't need to record it here.  */
1741 struct quick_file_names
1742 {
1743   /* The offset in .debug_line of the line table.  We hash on this.  */
1744   unsigned int offset;
1745
1746   /* The number of entries in file_names, real_names.  */
1747   unsigned int num_file_names;
1748
1749   /* The file names from the line table, after being run through
1750      file_full_name.  */
1751   const char **file_names;
1752
1753   /* The file names from the line table after being run through
1754      gdb_realpath.  These are computed lazily.  */
1755   const char **real_names;
1756 };
1757
1758 /* When using the index (and thus not using psymtabs), each CU has an
1759    object of this type.  This is used to hold information needed by
1760    the various "quick" methods.  */
1761 struct dwarf2_per_cu_quick_data
1762 {
1763   /* The file table.  This can be NULL if there was no file table
1764      or it's currently not read in.
1765      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1766   struct quick_file_names *file_names;
1767
1768   /* The corresponding symbol table.  This is NULL if symbols for this
1769      CU have not yet been read.  */
1770   struct symtab *symtab;
1771
1772   /* A temporary mark bit used when iterating over all CUs in
1773      expand_symtabs_matching.  */
1774   unsigned int mark : 1;
1775
1776   /* True if we've tried to read the file table and found there isn't one.
1777      There will be no point in trying to read it again next time.  */
1778   unsigned int no_file_data : 1;
1779 };
1780
1781 /* Hash function for a quick_file_names.  */
1782
1783 static hashval_t
1784 hash_file_name_entry (const void *e)
1785 {
1786   const struct quick_file_names *file_data = e;
1787
1788   return file_data->offset;
1789 }
1790
1791 /* Equality function for a quick_file_names.  */
1792
1793 static int
1794 eq_file_name_entry (const void *a, const void *b)
1795 {
1796   const struct quick_file_names *ea = a;
1797   const struct quick_file_names *eb = b;
1798
1799   return ea->offset == eb->offset;
1800 }
1801
1802 /* Delete function for a quick_file_names.  */
1803
1804 static void
1805 delete_file_name_entry (void *e)
1806 {
1807   struct quick_file_names *file_data = e;
1808   int i;
1809
1810   for (i = 0; i < file_data->num_file_names; ++i)
1811     {
1812       xfree ((void*) file_data->file_names[i]);
1813       if (file_data->real_names)
1814         xfree ((void*) file_data->real_names[i]);
1815     }
1816
1817   /* The space for the struct itself lives on objfile_obstack,
1818      so we don't free it here.  */
1819 }
1820
1821 /* Create a quick_file_names hash table.  */
1822
1823 static htab_t
1824 create_quick_file_names_table (unsigned int nr_initial_entries)
1825 {
1826   return htab_create_alloc (nr_initial_entries,
1827                             hash_file_name_entry, eq_file_name_entry,
1828                             delete_file_name_entry, xcalloc, xfree);
1829 }
1830
1831 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1832    have to be created afterwards.  You should call age_cached_comp_units after
1833    processing PER_CU->CU.  dw2_setup must have been already called.  */
1834
1835 static void
1836 load_cu (struct dwarf2_per_cu_data *per_cu)
1837 {
1838   if (per_cu->debug_type_section)
1839     read_signatured_type_at_offset (per_cu->objfile,
1840                                     per_cu->debug_type_section,
1841                                     per_cu->offset);
1842   else
1843     load_full_comp_unit (per_cu, per_cu->objfile);
1844
1845   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1846
1847   gdb_assert (per_cu->cu != NULL);
1848 }
1849
1850 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1851    this CU came.  */
1852
1853 static void
1854 dw2_do_instantiate_symtab (struct objfile *objfile,
1855                            struct dwarf2_per_cu_data *per_cu)
1856 {
1857   struct cleanup *back_to;
1858
1859   back_to = make_cleanup (dwarf2_release_queue, NULL);
1860
1861   queue_comp_unit (per_cu, objfile);
1862
1863   load_cu (per_cu);
1864
1865   process_queue (objfile);
1866
1867   /* Age the cache, releasing compilation units that have not
1868      been used recently.  */
1869   age_cached_comp_units ();
1870
1871   do_cleanups (back_to);
1872 }
1873
1874 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1875    the objfile from which this CU came.  Returns the resulting symbol
1876    table.  */
1877
1878 static struct symtab *
1879 dw2_instantiate_symtab (struct objfile *objfile,
1880                         struct dwarf2_per_cu_data *per_cu)
1881 {
1882   if (!per_cu->v.quick->symtab)
1883     {
1884       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1885       increment_reading_symtab ();
1886       dw2_do_instantiate_symtab (objfile, per_cu);
1887       do_cleanups (back_to);
1888     }
1889   return per_cu->v.quick->symtab;
1890 }
1891
1892 /* Return the CU given its index.  */
1893
1894 static struct dwarf2_per_cu_data *
1895 dw2_get_cu (int index)
1896 {
1897   if (index >= dwarf2_per_objfile->n_comp_units)
1898     {
1899       index -= dwarf2_per_objfile->n_comp_units;
1900       return dwarf2_per_objfile->type_comp_units[index];
1901     }
1902   return dwarf2_per_objfile->all_comp_units[index];
1903 }
1904
1905 /* A helper function that knows how to read a 64-bit value in a way
1906    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1907    otherwise.  */
1908
1909 static int
1910 extract_cu_value (const char *bytes, ULONGEST *result)
1911 {
1912   if (sizeof (ULONGEST) < 8)
1913     {
1914       int i;
1915
1916       /* Ignore the upper 4 bytes if they are all zero.  */
1917       for (i = 0; i < 4; ++i)
1918         if (bytes[i + 4] != 0)
1919           return 0;
1920
1921       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1922     }
1923   else
1924     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1925   return 1;
1926 }
1927
1928 /* Read the CU list from the mapped index, and use it to create all
1929    the CU objects for this objfile.  Return 0 if something went wrong,
1930    1 if everything went ok.  */
1931
1932 static int
1933 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1934                        offset_type cu_list_elements)
1935 {
1936   offset_type i;
1937
1938   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1939   dwarf2_per_objfile->all_comp_units
1940     = obstack_alloc (&objfile->objfile_obstack,
1941                      dwarf2_per_objfile->n_comp_units
1942                      * sizeof (struct dwarf2_per_cu_data *));
1943
1944   for (i = 0; i < cu_list_elements; i += 2)
1945     {
1946       struct dwarf2_per_cu_data *the_cu;
1947       ULONGEST offset, length;
1948
1949       if (!extract_cu_value (cu_list, &offset)
1950           || !extract_cu_value (cu_list + 8, &length))
1951         return 0;
1952       cu_list += 2 * 8;
1953
1954       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1955                                struct dwarf2_per_cu_data);
1956       the_cu->offset = offset;
1957       the_cu->length = length;
1958       the_cu->objfile = objfile;
1959       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1960                                         struct dwarf2_per_cu_quick_data);
1961       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1962     }
1963
1964   return 1;
1965 }
1966
1967 /* Create the signatured type hash table from the index.  */
1968
1969 static int
1970 create_signatured_type_table_from_index (struct objfile *objfile,
1971                                          struct dwarf2_section_info *section,
1972                                          const gdb_byte *bytes,
1973                                          offset_type elements)
1974 {
1975   offset_type i;
1976   htab_t sig_types_hash;
1977
1978   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1979   dwarf2_per_objfile->type_comp_units
1980     = obstack_alloc (&objfile->objfile_obstack,
1981                      dwarf2_per_objfile->n_type_comp_units
1982                      * sizeof (struct dwarf2_per_cu_data *));
1983
1984   sig_types_hash = allocate_signatured_type_table (objfile);
1985
1986   for (i = 0; i < elements; i += 3)
1987     {
1988       struct signatured_type *type_sig;
1989       ULONGEST offset, type_offset, signature;
1990       void **slot;
1991
1992       if (!extract_cu_value (bytes, &offset)
1993           || !extract_cu_value (bytes + 8, &type_offset))
1994         return 0;
1995       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1996       bytes += 3 * 8;
1997
1998       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1999                                  struct signatured_type);
2000       type_sig->signature = signature;
2001       type_sig->type_offset = type_offset;
2002       type_sig->per_cu.debug_type_section = section;
2003       type_sig->per_cu.offset = offset;
2004       type_sig->per_cu.objfile = objfile;
2005       type_sig->per_cu.v.quick
2006         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2007                           struct dwarf2_per_cu_quick_data);
2008
2009       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
2010       *slot = type_sig;
2011
2012       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2013     }
2014
2015   dwarf2_per_objfile->signatured_types = sig_types_hash;
2016
2017   return 1;
2018 }
2019
2020 /* Read the address map data from the mapped index, and use it to
2021    populate the objfile's psymtabs_addrmap.  */
2022
2023 static void
2024 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2025 {
2026   const gdb_byte *iter, *end;
2027   struct obstack temp_obstack;
2028   struct addrmap *mutable_map;
2029   struct cleanup *cleanup;
2030   CORE_ADDR baseaddr;
2031
2032   obstack_init (&temp_obstack);
2033   cleanup = make_cleanup_obstack_free (&temp_obstack);
2034   mutable_map = addrmap_create_mutable (&temp_obstack);
2035
2036   iter = index->address_table;
2037   end = iter + index->address_table_size;
2038
2039   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2040
2041   while (iter < end)
2042     {
2043       ULONGEST hi, lo, cu_index;
2044       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2045       iter += 8;
2046       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2047       iter += 8;
2048       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2049       iter += 4;
2050       
2051       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2052                          dw2_get_cu (cu_index));
2053     }
2054
2055   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2056                                                     &objfile->objfile_obstack);
2057   do_cleanups (cleanup);
2058 }
2059
2060 /* The hash function for strings in the mapped index.  This is the same as
2061    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2062    implementation.  This is necessary because the hash function is tied to the
2063    format of the mapped index file.  The hash values do not have to match with
2064    SYMBOL_HASH_NEXT.
2065    
2066    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2067
2068 static hashval_t
2069 mapped_index_string_hash (int index_version, const void *p)
2070 {
2071   const unsigned char *str = (const unsigned char *) p;
2072   hashval_t r = 0;
2073   unsigned char c;
2074
2075   while ((c = *str++) != 0)
2076     {
2077       if (index_version >= 5)
2078         c = tolower (c);
2079       r = r * 67 + c - 113;
2080     }
2081
2082   return r;
2083 }
2084
2085 /* Find a slot in the mapped index INDEX for the object named NAME.
2086    If NAME is found, set *VEC_OUT to point to the CU vector in the
2087    constant pool and return 1.  If NAME cannot be found, return 0.  */
2088
2089 static int
2090 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2091                           offset_type **vec_out)
2092 {
2093   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2094   offset_type hash;
2095   offset_type slot, step;
2096   int (*cmp) (const char *, const char *);
2097
2098   if (current_language->la_language == language_cplus
2099       || current_language->la_language == language_java
2100       || current_language->la_language == language_fortran)
2101     {
2102       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2103          not contain any.  */
2104       const char *paren = strchr (name, '(');
2105
2106       if (paren)
2107         {
2108           char *dup;
2109
2110           dup = xmalloc (paren - name + 1);
2111           memcpy (dup, name, paren - name);
2112           dup[paren - name] = 0;
2113
2114           make_cleanup (xfree, dup);
2115           name = dup;
2116         }
2117     }
2118
2119   /* Index version 4 did not support case insensitive searches.  But the
2120      indexes for case insensitive languages are built in lowercase, therefore
2121      simulate our NAME being searched is also lowercased.  */
2122   hash = mapped_index_string_hash ((index->version == 4
2123                                     && case_sensitivity == case_sensitive_off
2124                                     ? 5 : index->version),
2125                                    name);
2126
2127   slot = hash & (index->symbol_table_slots - 1);
2128   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2129   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2130
2131   for (;;)
2132     {
2133       /* Convert a slot number to an offset into the table.  */
2134       offset_type i = 2 * slot;
2135       const char *str;
2136       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2137         {
2138           do_cleanups (back_to);
2139           return 0;
2140         }
2141
2142       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2143       if (!cmp (name, str))
2144         {
2145           *vec_out = (offset_type *) (index->constant_pool
2146                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2147           do_cleanups (back_to);
2148           return 1;
2149         }
2150
2151       slot = (slot + step) & (index->symbol_table_slots - 1);
2152     }
2153 }
2154
2155 /* Read the index file.  If everything went ok, initialize the "quick"
2156    elements of all the CUs and return 1.  Otherwise, return 0.  */
2157
2158 static int
2159 dwarf2_read_index (struct objfile *objfile)
2160 {
2161   char *addr;
2162   struct mapped_index *map;
2163   offset_type *metadata;
2164   const gdb_byte *cu_list;
2165   const gdb_byte *types_list = NULL;
2166   offset_type version, cu_list_elements;
2167   offset_type types_list_elements = 0;
2168   int i;
2169
2170   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2171     return 0;
2172
2173   /* Older elfutils strip versions could keep the section in the main
2174      executable while splitting it for the separate debug info file.  */
2175   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2176        & SEC_HAS_CONTENTS) == 0)
2177     return 0;
2178
2179   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2180
2181   addr = dwarf2_per_objfile->gdb_index.buffer;
2182   /* Version check.  */
2183   version = MAYBE_SWAP (*(offset_type *) addr);
2184   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2185      causes the index to behave very poorly for certain requests.  Version 3
2186      contained incomplete addrmap.  So, it seems better to just ignore such
2187      indices.  Index version 4 uses a different hash function than index
2188      version 5 and later.  */
2189   if (version < 4)
2190     return 0;
2191   /* Indexes with higher version than the one supported by GDB may be no
2192      longer backward compatible.  */
2193   if (version > 5)
2194     return 0;
2195
2196   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2197   map->version = version;
2198   map->total_size = dwarf2_per_objfile->gdb_index.size;
2199
2200   metadata = (offset_type *) (addr + sizeof (offset_type));
2201
2202   i = 0;
2203   cu_list = addr + MAYBE_SWAP (metadata[i]);
2204   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2205                       / 8);
2206   ++i;
2207
2208   types_list = addr + MAYBE_SWAP (metadata[i]);
2209   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2210                           - MAYBE_SWAP (metadata[i]))
2211                          / 8);
2212   ++i;
2213
2214   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2215   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2216                              - MAYBE_SWAP (metadata[i]));
2217   ++i;
2218
2219   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2220   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2221                               - MAYBE_SWAP (metadata[i]))
2222                              / (2 * sizeof (offset_type)));
2223   ++i;
2224
2225   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2226
2227   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2228     return 0;
2229
2230   if (types_list_elements)
2231     {
2232       struct dwarf2_section_info *section;
2233
2234       /* We can only handle a single .debug_types when we have an
2235          index.  */
2236       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2237         return 0;
2238
2239       section = VEC_index (dwarf2_section_info_def,
2240                            dwarf2_per_objfile->types, 0);
2241
2242       if (!create_signatured_type_table_from_index (objfile, section,
2243                                                     types_list,
2244                                                     types_list_elements))
2245         return 0;
2246     }
2247
2248   create_addrmap_from_index (objfile, map);
2249
2250   dwarf2_per_objfile->index_table = map;
2251   dwarf2_per_objfile->using_index = 1;
2252   dwarf2_per_objfile->quick_file_names_table =
2253     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2254
2255   return 1;
2256 }
2257
2258 /* A helper for the "quick" functions which sets the global
2259    dwarf2_per_objfile according to OBJFILE.  */
2260
2261 static void
2262 dw2_setup (struct objfile *objfile)
2263 {
2264   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2265   gdb_assert (dwarf2_per_objfile);
2266 }
2267
2268 /* A helper for the "quick" functions which attempts to read the line
2269    table for THIS_CU.  */
2270
2271 static struct quick_file_names *
2272 dw2_get_file_names (struct objfile *objfile,
2273                     struct dwarf2_per_cu_data *this_cu)
2274 {
2275   bfd *abfd = objfile->obfd;
2276   struct line_header *lh;
2277   struct attribute *attr;
2278   struct cleanup *cleanups;
2279   struct die_info *comp_unit_die;
2280   struct dwarf2_section_info* sec;
2281   gdb_byte *info_ptr, *buffer;
2282   int has_children, i;
2283   struct dwarf2_cu cu;
2284   unsigned int bytes_read, buffer_size;
2285   struct die_reader_specs reader_specs;
2286   char *name, *comp_dir;
2287   void **slot;
2288   struct quick_file_names *qfn;
2289   unsigned int line_offset;
2290
2291   if (this_cu->v.quick->file_names != NULL)
2292     return this_cu->v.quick->file_names;
2293   /* If we know there is no line data, no point in looking again.  */
2294   if (this_cu->v.quick->no_file_data)
2295     return NULL;
2296
2297   init_one_comp_unit (&cu, objfile);
2298   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2299
2300   if (this_cu->debug_type_section)
2301     sec = this_cu->debug_type_section;
2302   else
2303     sec = &dwarf2_per_objfile->info;
2304   dwarf2_read_section (objfile, sec);
2305   buffer_size = sec->size;
2306   buffer = sec->buffer;
2307   info_ptr = buffer + this_cu->offset;
2308
2309   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2310                                           buffer, buffer_size,
2311                                           abfd,
2312                                           this_cu->debug_type_section != NULL);
2313
2314   /* Skip dummy compilation units.  */
2315   if (info_ptr >= buffer + buffer_size
2316       || peek_abbrev_code (abfd, info_ptr) == 0)
2317     {
2318       do_cleanups (cleanups);
2319       return NULL;
2320     }
2321
2322   this_cu->cu = &cu;
2323   cu.per_cu = this_cu;
2324
2325   dwarf2_read_abbrevs (abfd, &cu);
2326   make_cleanup (dwarf2_free_abbrev_table, &cu);
2327
2328   init_cu_die_reader (&reader_specs, &cu);
2329   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2330                  &has_children);
2331
2332   lh = NULL;
2333   slot = NULL;
2334   line_offset = 0;
2335   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2336   if (attr)
2337     {
2338       struct quick_file_names find_entry;
2339
2340       line_offset = DW_UNSND (attr);
2341
2342       /* We may have already read in this line header (TU line header sharing).
2343          If we have we're done.  */
2344       find_entry.offset = line_offset;
2345       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2346                              &find_entry, INSERT);
2347       if (*slot != NULL)
2348         {
2349           do_cleanups (cleanups);
2350           this_cu->v.quick->file_names = *slot;
2351           return *slot;
2352         }
2353
2354       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2355     }
2356   if (lh == NULL)
2357     {
2358       do_cleanups (cleanups);
2359       this_cu->v.quick->no_file_data = 1;
2360       return NULL;
2361     }
2362
2363   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2364   qfn->offset = line_offset;
2365   gdb_assert (slot != NULL);
2366   *slot = qfn;
2367
2368   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2369
2370   qfn->num_file_names = lh->num_file_names;
2371   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2372                                    lh->num_file_names * sizeof (char *));
2373   for (i = 0; i < lh->num_file_names; ++i)
2374     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2375   qfn->real_names = NULL;
2376
2377   free_line_header (lh);
2378   do_cleanups (cleanups);
2379
2380   this_cu->v.quick->file_names = qfn;
2381   return qfn;
2382 }
2383
2384 /* A helper for the "quick" functions which computes and caches the
2385    real path for a given file name from the line table.  */
2386
2387 static const char *
2388 dw2_get_real_path (struct objfile *objfile,
2389                    struct quick_file_names *qfn, int index)
2390 {
2391   if (qfn->real_names == NULL)
2392     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2393                                       qfn->num_file_names, sizeof (char *));
2394
2395   if (qfn->real_names[index] == NULL)
2396     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2397
2398   return qfn->real_names[index];
2399 }
2400
2401 static struct symtab *
2402 dw2_find_last_source_symtab (struct objfile *objfile)
2403 {
2404   int index;
2405
2406   dw2_setup (objfile);
2407   index = dwarf2_per_objfile->n_comp_units - 1;
2408   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2409 }
2410
2411 /* Traversal function for dw2_forget_cached_source_info.  */
2412
2413 static int
2414 dw2_free_cached_file_names (void **slot, void *info)
2415 {
2416   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2417
2418   if (file_data->real_names)
2419     {
2420       int i;
2421
2422       for (i = 0; i < file_data->num_file_names; ++i)
2423         {
2424           xfree ((void*) file_data->real_names[i]);
2425           file_data->real_names[i] = NULL;
2426         }
2427     }
2428
2429   return 1;
2430 }
2431
2432 static void
2433 dw2_forget_cached_source_info (struct objfile *objfile)
2434 {
2435   dw2_setup (objfile);
2436
2437   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2438                           dw2_free_cached_file_names, NULL);
2439 }
2440
2441 static int
2442 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2443                    const char *full_path, const char *real_path,
2444                    struct symtab **result)
2445 {
2446   int i;
2447   int check_basename = lbasename (name) == name;
2448   struct dwarf2_per_cu_data *base_cu = NULL;
2449
2450   dw2_setup (objfile);
2451
2452   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2453                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2454     {
2455       int j;
2456       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2457       struct quick_file_names *file_data;
2458
2459       if (per_cu->v.quick->symtab)
2460         continue;
2461
2462       file_data = dw2_get_file_names (objfile, per_cu);
2463       if (file_data == NULL)
2464         continue;
2465
2466       for (j = 0; j < file_data->num_file_names; ++j)
2467         {
2468           const char *this_name = file_data->file_names[j];
2469
2470           if (FILENAME_CMP (name, this_name) == 0)
2471             {
2472               *result = dw2_instantiate_symtab (objfile, per_cu);
2473               return 1;
2474             }
2475
2476           if (check_basename && ! base_cu
2477               && FILENAME_CMP (lbasename (this_name), name) == 0)
2478             base_cu = per_cu;
2479
2480           if (full_path != NULL)
2481             {
2482               const char *this_real_name = dw2_get_real_path (objfile,
2483                                                               file_data, j);
2484
2485               if (this_real_name != NULL
2486                   && FILENAME_CMP (full_path, this_real_name) == 0)
2487                 {
2488                   *result = dw2_instantiate_symtab (objfile, per_cu);
2489                   return 1;
2490                 }
2491             }
2492
2493           if (real_path != NULL)
2494             {
2495               const char *this_real_name = dw2_get_real_path (objfile,
2496                                                               file_data, j);
2497
2498               if (this_real_name != NULL
2499                   && FILENAME_CMP (real_path, this_real_name) == 0)
2500                 {
2501                   *result = dw2_instantiate_symtab (objfile, per_cu);
2502                   return 1;
2503                 }
2504             }
2505         }
2506     }
2507
2508   if (base_cu)
2509     {
2510       *result = dw2_instantiate_symtab (objfile, base_cu);
2511       return 1;
2512     }
2513
2514   return 0;
2515 }
2516
2517 static struct symtab *
2518 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2519                    const char *name, domain_enum domain)
2520 {
2521   /* We do all the work in the pre_expand_symtabs_matching hook
2522      instead.  */
2523   return NULL;
2524 }
2525
2526 /* A helper function that expands all symtabs that hold an object
2527    named NAME.  */
2528
2529 static void
2530 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2531 {
2532   dw2_setup (objfile);
2533
2534   /* index_table is NULL if OBJF_READNOW.  */
2535   if (dwarf2_per_objfile->index_table)
2536     {
2537       offset_type *vec;
2538
2539       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2540                                     name, &vec))
2541         {
2542           offset_type i, len = MAYBE_SWAP (*vec);
2543           for (i = 0; i < len; ++i)
2544             {
2545               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2546               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2547
2548               dw2_instantiate_symtab (objfile, per_cu);
2549             }
2550         }
2551     }
2552 }
2553
2554 static void
2555 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2556                                  enum block_enum block_kind, const char *name,
2557                                  domain_enum domain)
2558 {
2559   dw2_do_expand_symtabs_matching (objfile, name);
2560 }
2561
2562 static void
2563 dw2_print_stats (struct objfile *objfile)
2564 {
2565   int i, count;
2566
2567   dw2_setup (objfile);
2568   count = 0;
2569   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2570                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2571     {
2572       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2573
2574       if (!per_cu->v.quick->symtab)
2575         ++count;
2576     }
2577   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2578 }
2579
2580 static void
2581 dw2_dump (struct objfile *objfile)
2582 {
2583   /* Nothing worth printing.  */
2584 }
2585
2586 static void
2587 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2588               struct section_offsets *delta)
2589 {
2590   /* There's nothing to relocate here.  */
2591 }
2592
2593 static void
2594 dw2_expand_symtabs_for_function (struct objfile *objfile,
2595                                  const char *func_name)
2596 {
2597   dw2_do_expand_symtabs_matching (objfile, func_name);
2598 }
2599
2600 static void
2601 dw2_expand_all_symtabs (struct objfile *objfile)
2602 {
2603   int i;
2604
2605   dw2_setup (objfile);
2606
2607   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2608                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2609     {
2610       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2611
2612       dw2_instantiate_symtab (objfile, per_cu);
2613     }
2614 }
2615
2616 static void
2617 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2618                                   const char *filename)
2619 {
2620   int i;
2621
2622   dw2_setup (objfile);
2623
2624   /* We don't need to consider type units here.
2625      This is only called for examining code, e.g. expand_line_sal.
2626      There can be an order of magnitude (or more) more type units
2627      than comp units, and we avoid them if we can.  */
2628
2629   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2630     {
2631       int j;
2632       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2633       struct quick_file_names *file_data;
2634
2635       if (per_cu->v.quick->symtab)
2636         continue;
2637
2638       file_data = dw2_get_file_names (objfile, per_cu);
2639       if (file_data == NULL)
2640         continue;
2641
2642       for (j = 0; j < file_data->num_file_names; ++j)
2643         {
2644           const char *this_name = file_data->file_names[j];
2645           if (FILENAME_CMP (this_name, filename) == 0)
2646             {
2647               dw2_instantiate_symtab (objfile, per_cu);
2648               break;
2649             }
2650         }
2651     }
2652 }
2653
2654 static const char *
2655 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2656 {
2657   struct dwarf2_per_cu_data *per_cu;
2658   offset_type *vec;
2659   struct quick_file_names *file_data;
2660
2661   dw2_setup (objfile);
2662
2663   /* index_table is NULL if OBJF_READNOW.  */
2664   if (!dwarf2_per_objfile->index_table)
2665     {
2666       struct symtab *s;
2667
2668       ALL_OBJFILE_SYMTABS (objfile, s)
2669         if (s->primary)
2670           {
2671             struct blockvector *bv = BLOCKVECTOR (s);
2672             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2673             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2674
2675             if (sym)
2676               return sym->symtab->filename;
2677           }
2678       return NULL;
2679     }
2680
2681   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2682                                  name, &vec))
2683     return NULL;
2684
2685   /* Note that this just looks at the very first one named NAME -- but
2686      actually we are looking for a function.  find_main_filename
2687      should be rewritten so that it doesn't require a custom hook.  It
2688      could just use the ordinary symbol tables.  */
2689   /* vec[0] is the length, which must always be >0.  */
2690   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2691
2692   file_data = dw2_get_file_names (objfile, per_cu);
2693   if (file_data == NULL)
2694     return NULL;
2695
2696   return file_data->file_names[file_data->num_file_names - 1];
2697 }
2698
2699 static void
2700 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2701                           struct objfile *objfile, int global,
2702                           int (*callback) (struct block *,
2703                                            struct symbol *, void *),
2704                           void *data, symbol_compare_ftype *match,
2705                           symbol_compare_ftype *ordered_compare)
2706 {
2707   /* Currently unimplemented; used for Ada.  The function can be called if the
2708      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2709      does not look for non-Ada symbols this function should just return.  */
2710 }
2711
2712 static void
2713 dw2_expand_symtabs_matching (struct objfile *objfile,
2714                              int (*file_matcher) (const char *, void *),
2715                              int (*name_matcher) (const char *, void *),
2716                              enum search_domain kind,
2717                              void *data)
2718 {
2719   int i;
2720   offset_type iter;
2721   struct mapped_index *index;
2722
2723   dw2_setup (objfile);
2724
2725   /* index_table is NULL if OBJF_READNOW.  */
2726   if (!dwarf2_per_objfile->index_table)
2727     return;
2728   index = dwarf2_per_objfile->index_table;
2729
2730   if (file_matcher != NULL)
2731     for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2732                      + dwarf2_per_objfile->n_type_comp_units); ++i)
2733       {
2734         int j;
2735         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2736         struct quick_file_names *file_data;
2737
2738         per_cu->v.quick->mark = 0;
2739         if (per_cu->v.quick->symtab)
2740           continue;
2741
2742         file_data = dw2_get_file_names (objfile, per_cu);
2743         if (file_data == NULL)
2744           continue;
2745
2746         for (j = 0; j < file_data->num_file_names; ++j)
2747           {
2748             if (file_matcher (file_data->file_names[j], data))
2749               {
2750                 per_cu->v.quick->mark = 1;
2751                 break;
2752               }
2753           }
2754       }
2755
2756   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2757     {
2758       offset_type idx = 2 * iter;
2759       const char *name;
2760       offset_type *vec, vec_len, vec_idx;
2761
2762       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2763         continue;
2764
2765       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2766
2767       if (! (*name_matcher) (name, data))
2768         continue;
2769
2770       /* The name was matched, now expand corresponding CUs that were
2771          marked.  */
2772       vec = (offset_type *) (index->constant_pool
2773                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2774       vec_len = MAYBE_SWAP (vec[0]);
2775       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2776         {
2777           struct dwarf2_per_cu_data *per_cu;
2778
2779           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2780           if (file_matcher == NULL || per_cu->v.quick->mark)
2781             dw2_instantiate_symtab (objfile, per_cu);
2782         }
2783     }
2784 }
2785
2786 static struct symtab *
2787 dw2_find_pc_sect_symtab (struct objfile *objfile,
2788                          struct minimal_symbol *msymbol,
2789                          CORE_ADDR pc,
2790                          struct obj_section *section,
2791                          int warn_if_readin)
2792 {
2793   struct dwarf2_per_cu_data *data;
2794
2795   dw2_setup (objfile);
2796
2797   if (!objfile->psymtabs_addrmap)
2798     return NULL;
2799
2800   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2801   if (!data)
2802     return NULL;
2803
2804   if (warn_if_readin && data->v.quick->symtab)
2805     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2806              paddress (get_objfile_arch (objfile), pc));
2807
2808   return dw2_instantiate_symtab (objfile, data);
2809 }
2810
2811 static void
2812 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2813                           void *data)
2814 {
2815   int i;
2816
2817   dw2_setup (objfile);
2818
2819   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2820                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2821     {
2822       int j;
2823       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2824       struct quick_file_names *file_data;
2825
2826       if (per_cu->v.quick->symtab)
2827         continue;
2828
2829       file_data = dw2_get_file_names (objfile, per_cu);
2830       if (file_data == NULL)
2831         continue;
2832
2833       for (j = 0; j < file_data->num_file_names; ++j)
2834         {
2835           const char *this_real_name = dw2_get_real_path (objfile, file_data,
2836                                                           j);
2837           (*fun) (file_data->file_names[j], this_real_name, data);
2838         }
2839     }
2840 }
2841
2842 static int
2843 dw2_has_symbols (struct objfile *objfile)
2844 {
2845   return 1;
2846 }
2847
2848 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2849 {
2850   dw2_has_symbols,
2851   dw2_find_last_source_symtab,
2852   dw2_forget_cached_source_info,
2853   dw2_lookup_symtab,
2854   dw2_lookup_symbol,
2855   dw2_pre_expand_symtabs_matching,
2856   dw2_print_stats,
2857   dw2_dump,
2858   dw2_relocate,
2859   dw2_expand_symtabs_for_function,
2860   dw2_expand_all_symtabs,
2861   dw2_expand_symtabs_with_filename,
2862   dw2_find_symbol_file,
2863   dw2_map_matching_symbols,
2864   dw2_expand_symtabs_matching,
2865   dw2_find_pc_sect_symtab,
2866   dw2_map_symbol_filenames
2867 };
2868
2869 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2870    file will use psymtabs, or 1 if using the GNU index.  */
2871
2872 int
2873 dwarf2_initialize_objfile (struct objfile *objfile)
2874 {
2875   /* If we're about to read full symbols, don't bother with the
2876      indices.  In this case we also don't care if some other debug
2877      format is making psymtabs, because they are all about to be
2878      expanded anyway.  */
2879   if ((objfile->flags & OBJF_READNOW))
2880     {
2881       int i;
2882
2883       dwarf2_per_objfile->using_index = 1;
2884       create_all_comp_units (objfile);
2885       create_debug_types_hash_table (objfile);
2886       dwarf2_per_objfile->quick_file_names_table =
2887         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2888
2889       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2890                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2891         {
2892           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2893
2894           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2895                                             struct dwarf2_per_cu_quick_data);
2896         }
2897
2898       /* Return 1 so that gdb sees the "quick" functions.  However,
2899          these functions will be no-ops because we will have expanded
2900          all symtabs.  */
2901       return 1;
2902     }
2903
2904   if (dwarf2_read_index (objfile))
2905     return 1;
2906
2907   return 0;
2908 }
2909
2910 \f
2911
2912 /* Build a partial symbol table.  */
2913
2914 void
2915 dwarf2_build_psymtabs (struct objfile *objfile)
2916 {
2917   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2918     {
2919       init_psymbol_list (objfile, 1024);
2920     }
2921
2922   dwarf2_build_psymtabs_hard (objfile);
2923 }
2924
2925 /* Return TRUE if OFFSET is within CU_HEADER.  */
2926
2927 static inline int
2928 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2929 {
2930   unsigned int bottom = cu_header->offset;
2931   unsigned int top = (cu_header->offset
2932                       + cu_header->length
2933                       + cu_header->initial_length_size);
2934
2935   return (offset >= bottom && offset < top);
2936 }
2937
2938 /* Read in the comp unit header information from the debug_info at info_ptr.
2939    NOTE: This leaves members offset, first_die_offset to be filled in
2940    by the caller.  */
2941
2942 static gdb_byte *
2943 read_comp_unit_head (struct comp_unit_head *cu_header,
2944                      gdb_byte *info_ptr, bfd *abfd)
2945 {
2946   int signed_addr;
2947   unsigned int bytes_read;
2948
2949   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2950   cu_header->initial_length_size = bytes_read;
2951   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2952   info_ptr += bytes_read;
2953   cu_header->version = read_2_bytes (abfd, info_ptr);
2954   info_ptr += 2;
2955   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2956                                           &bytes_read);
2957   info_ptr += bytes_read;
2958   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2959   info_ptr += 1;
2960   signed_addr = bfd_get_sign_extend_vma (abfd);
2961   if (signed_addr < 0)
2962     internal_error (__FILE__, __LINE__,
2963                     _("read_comp_unit_head: dwarf from non elf file"));
2964   cu_header->signed_addr_p = signed_addr;
2965
2966   return info_ptr;
2967 }
2968
2969 /* Read in a CU header and perform some basic error checking.  */
2970
2971 static gdb_byte *
2972 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2973                              gdb_byte *buffer, unsigned int buffer_size,
2974                              bfd *abfd, int is_debug_type_section)
2975 {
2976   gdb_byte *beg_of_comp_unit = info_ptr;
2977
2978   header->offset = beg_of_comp_unit - buffer;
2979
2980   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2981
2982   /* If we're reading a type unit, skip over the signature and
2983      type_offset fields.  */
2984   if (is_debug_type_section)
2985     info_ptr += 8 /*signature*/ + header->offset_size;
2986
2987   header->first_die_offset = info_ptr - beg_of_comp_unit;
2988
2989   if (header->version != 2 && header->version != 3 && header->version != 4)
2990     error (_("Dwarf Error: wrong version in compilation unit header "
2991            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2992            bfd_get_filename (abfd));
2993
2994   if (header->abbrev_offset
2995       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2996                               &dwarf2_per_objfile->abbrev))
2997     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2998            "(offset 0x%lx + 6) [in module %s]"),
2999            (long) header->abbrev_offset,
3000            (long) (beg_of_comp_unit - buffer),
3001            bfd_get_filename (abfd));
3002
3003   if (beg_of_comp_unit + header->length + header->initial_length_size
3004       > buffer + buffer_size)
3005     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3006            "(offset 0x%lx + 0) [in module %s]"),
3007            (long) header->length,
3008            (long) (beg_of_comp_unit - buffer),
3009            bfd_get_filename (abfd));
3010
3011   return info_ptr;
3012 }
3013
3014 /* Read in the types comp unit header information from .debug_types entry at
3015    types_ptr.  The result is a pointer to one past the end of the header.  */
3016
3017 static gdb_byte *
3018 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3019                           struct dwarf2_section_info *section,
3020                           ULONGEST *signature,
3021                           gdb_byte *types_ptr, bfd *abfd)
3022 {
3023   gdb_byte *initial_types_ptr = types_ptr;
3024
3025   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3026   cu_header->offset = types_ptr - section->buffer;
3027
3028   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3029
3030   *signature = read_8_bytes (abfd, types_ptr);
3031   types_ptr += 8;
3032   types_ptr += cu_header->offset_size;
3033   cu_header->first_die_offset = types_ptr - initial_types_ptr;
3034
3035   return types_ptr;
3036 }
3037
3038 /* Allocate a new partial symtab for file named NAME and mark this new
3039    partial symtab as being an include of PST.  */
3040
3041 static void
3042 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3043                                struct objfile *objfile)
3044 {
3045   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3046
3047   subpst->section_offsets = pst->section_offsets;
3048   subpst->textlow = 0;
3049   subpst->texthigh = 0;
3050
3051   subpst->dependencies = (struct partial_symtab **)
3052     obstack_alloc (&objfile->objfile_obstack,
3053                    sizeof (struct partial_symtab *));
3054   subpst->dependencies[0] = pst;
3055   subpst->number_of_dependencies = 1;
3056
3057   subpst->globals_offset = 0;
3058   subpst->n_global_syms = 0;
3059   subpst->statics_offset = 0;
3060   subpst->n_static_syms = 0;
3061   subpst->symtab = NULL;
3062   subpst->read_symtab = pst->read_symtab;
3063   subpst->readin = 0;
3064
3065   /* No private part is necessary for include psymtabs.  This property
3066      can be used to differentiate between such include psymtabs and
3067      the regular ones.  */
3068   subpst->read_symtab_private = NULL;
3069 }
3070
3071 /* Read the Line Number Program data and extract the list of files
3072    included by the source file represented by PST.  Build an include
3073    partial symtab for each of these included files.  */
3074
3075 static void
3076 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3077                                struct die_info *die,
3078                                struct partial_symtab *pst)
3079 {
3080   struct objfile *objfile = cu->objfile;
3081   bfd *abfd = objfile->obfd;
3082   struct line_header *lh = NULL;
3083   struct attribute *attr;
3084
3085   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3086   if (attr)
3087     {
3088       unsigned int line_offset = DW_UNSND (attr);
3089
3090       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3091     }
3092   if (lh == NULL)
3093     return;  /* No linetable, so no includes.  */
3094
3095   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3096   dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3097
3098   free_line_header (lh);
3099 }
3100
3101 static hashval_t
3102 hash_type_signature (const void *item)
3103 {
3104   const struct signatured_type *type_sig = item;
3105
3106   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3107   return type_sig->signature;
3108 }
3109
3110 static int
3111 eq_type_signature (const void *item_lhs, const void *item_rhs)
3112 {
3113   const struct signatured_type *lhs = item_lhs;
3114   const struct signatured_type *rhs = item_rhs;
3115
3116   return lhs->signature == rhs->signature;
3117 }
3118
3119 /* Allocate a hash table for signatured types.  */
3120
3121 static htab_t
3122 allocate_signatured_type_table (struct objfile *objfile)
3123 {
3124   return htab_create_alloc_ex (41,
3125                                hash_type_signature,
3126                                eq_type_signature,
3127                                NULL,
3128                                &objfile->objfile_obstack,
3129                                hashtab_obstack_allocate,
3130                                dummy_obstack_deallocate);
3131 }
3132
3133 /* A helper function to add a signatured type CU to a list.  */
3134
3135 static int
3136 add_signatured_type_cu_to_list (void **slot, void *datum)
3137 {
3138   struct signatured_type *sigt = *slot;
3139   struct dwarf2_per_cu_data ***datap = datum;
3140
3141   **datap = &sigt->per_cu;
3142   ++*datap;
3143
3144   return 1;
3145 }
3146
3147 /* Create the hash table of all entries in the .debug_types section.
3148    The result is zero if there is an error (e.g. missing .debug_types section),
3149    otherwise non-zero.  */
3150
3151 static int
3152 create_debug_types_hash_table (struct objfile *objfile)
3153 {
3154   htab_t types_htab = NULL;
3155   struct dwarf2_per_cu_data **iter;
3156   int ix;
3157   struct dwarf2_section_info *section;
3158
3159   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3160     {
3161       dwarf2_per_objfile->signatured_types = NULL;
3162       return 0;
3163     }
3164
3165   for (ix = 0;
3166        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3167                     ix, section);
3168        ++ix)
3169     {
3170       gdb_byte *info_ptr, *end_ptr;
3171
3172       dwarf2_read_section (objfile, section);
3173       info_ptr = section->buffer;
3174
3175       if (info_ptr == NULL)
3176         continue;
3177
3178       if (types_htab == NULL)
3179         types_htab = allocate_signatured_type_table (objfile);
3180
3181       if (dwarf2_die_debug)
3182         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3183
3184       end_ptr = info_ptr + section->size;
3185       while (info_ptr < end_ptr)
3186         {
3187           unsigned int offset;
3188           unsigned int offset_size;
3189           unsigned int type_offset;
3190           unsigned int length, initial_length_size;
3191           unsigned short version;
3192           ULONGEST signature;
3193           struct signatured_type *type_sig;
3194           void **slot;
3195           gdb_byte *ptr = info_ptr;
3196
3197           offset = ptr - section->buffer;
3198
3199           /* We need to read the type's signature in order to build the hash
3200              table, but we don't need to read anything else just yet.  */
3201
3202           /* Sanity check to ensure entire cu is present.  */
3203           length = read_initial_length (objfile->obfd, ptr,
3204                                         &initial_length_size);
3205           if (ptr + length + initial_length_size > end_ptr)
3206             {
3207               complaint (&symfile_complaints,
3208                          _("debug type entry runs off end "
3209                            "of `.debug_types' section, ignored"));
3210               break;
3211             }
3212
3213           offset_size = initial_length_size == 4 ? 4 : 8;
3214           ptr += initial_length_size;
3215           version = bfd_get_16 (objfile->obfd, ptr);
3216           ptr += 2;
3217           ptr += offset_size; /* abbrev offset */
3218           ptr += 1; /* address size */
3219           signature = bfd_get_64 (objfile->obfd, ptr);
3220           ptr += 8;
3221           type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3222           ptr += offset_size;
3223
3224           /* Skip dummy type units.  */
3225           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3226             {
3227               info_ptr = info_ptr + initial_length_size + length;
3228               continue;
3229             }
3230
3231           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3232           memset (type_sig, 0, sizeof (*type_sig));
3233           type_sig->signature = signature;
3234           type_sig->type_offset = type_offset;
3235           type_sig->per_cu.objfile = objfile;
3236           type_sig->per_cu.debug_type_section = section;
3237           type_sig->per_cu.offset = offset;
3238
3239           slot = htab_find_slot (types_htab, type_sig, INSERT);
3240           gdb_assert (slot != NULL);
3241           if (*slot != NULL)
3242             {
3243               const struct signatured_type *dup_sig = *slot;
3244
3245               complaint (&symfile_complaints,
3246                          _("debug type entry at offset 0x%x is duplicate to the "
3247                            "entry at offset 0x%x, signature 0x%s"),
3248                          offset, dup_sig->per_cu.offset,
3249                          phex (signature, sizeof (signature)));
3250               gdb_assert (signature == dup_sig->signature);
3251             }
3252           *slot = type_sig;
3253
3254           if (dwarf2_die_debug)
3255             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3256                                 offset, phex (signature, sizeof (signature)));
3257
3258           info_ptr = info_ptr + initial_length_size + length;
3259         }
3260     }
3261
3262   dwarf2_per_objfile->signatured_types = types_htab;
3263
3264   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3265   dwarf2_per_objfile->type_comp_units
3266     = obstack_alloc (&objfile->objfile_obstack,
3267                      dwarf2_per_objfile->n_type_comp_units
3268                      * sizeof (struct dwarf2_per_cu_data *));
3269   iter = &dwarf2_per_objfile->type_comp_units[0];
3270   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3271   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3272               == dwarf2_per_objfile->n_type_comp_units);
3273
3274   return 1;
3275 }
3276
3277 /* Lookup a signature based type.
3278    Returns NULL if SIG is not present in the table.  */
3279
3280 static struct signatured_type *
3281 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3282 {
3283   struct signatured_type find_entry, *entry;
3284
3285   if (dwarf2_per_objfile->signatured_types == NULL)
3286     {
3287       complaint (&symfile_complaints,
3288                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3289       return 0;
3290     }
3291
3292   find_entry.signature = sig;
3293   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3294   return entry;
3295 }
3296
3297 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3298
3299 static void
3300 init_cu_die_reader (struct die_reader_specs *reader,
3301                     struct dwarf2_cu *cu)
3302 {
3303   reader->abfd = cu->objfile->obfd;
3304   reader->cu = cu;
3305   if (cu->per_cu->debug_type_section)
3306     {
3307       gdb_assert (cu->per_cu->debug_type_section->readin);
3308       reader->buffer = cu->per_cu->debug_type_section->buffer;
3309     }
3310   else
3311     {
3312       gdb_assert (dwarf2_per_objfile->info.readin);
3313       reader->buffer = dwarf2_per_objfile->info.buffer;
3314     }
3315 }
3316
3317 /* Find the base address of the compilation unit for range lists and
3318    location lists.  It will normally be specified by DW_AT_low_pc.
3319    In DWARF-3 draft 4, the base address could be overridden by
3320    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3321    compilation units with discontinuous ranges.  */
3322
3323 static void
3324 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3325 {
3326   struct attribute *attr;
3327
3328   cu->base_known = 0;
3329   cu->base_address = 0;
3330
3331   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3332   if (attr)
3333     {
3334       cu->base_address = DW_ADDR (attr);
3335       cu->base_known = 1;
3336     }
3337   else
3338     {
3339       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3340       if (attr)
3341         {
3342           cu->base_address = DW_ADDR (attr);
3343           cu->base_known = 1;
3344         }
3345     }
3346 }
3347
3348 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3349    to combine the common parts.
3350    Process a compilation unit for a psymtab.
3351    BUFFER is a pointer to the beginning of the dwarf section buffer,
3352    either .debug_info or debug_types.
3353    INFO_PTR is a pointer to the start of the CU.
3354    Returns a pointer to the next CU.  */
3355
3356 static gdb_byte *
3357 process_psymtab_comp_unit (struct objfile *objfile,
3358                            struct dwarf2_per_cu_data *this_cu,
3359                            gdb_byte *buffer, gdb_byte *info_ptr,
3360                            unsigned int buffer_size)
3361 {
3362   bfd *abfd = objfile->obfd;
3363   gdb_byte *beg_of_comp_unit = info_ptr;
3364   struct die_info *comp_unit_die;
3365   struct partial_symtab *pst;
3366   CORE_ADDR baseaddr;
3367   struct cleanup *back_to_inner;
3368   struct dwarf2_cu cu;
3369   int has_children, has_pc_info;
3370   struct attribute *attr;
3371   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3372   struct die_reader_specs reader_specs;
3373   const char *filename;
3374
3375   init_one_comp_unit (&cu, objfile);
3376   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3377
3378   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3379                                           buffer, buffer_size,
3380                                           abfd,
3381                                           this_cu->debug_type_section != NULL);
3382
3383   /* Skip dummy compilation units.  */
3384   if (info_ptr >= buffer + buffer_size
3385       || peek_abbrev_code (abfd, info_ptr) == 0)
3386     {
3387       info_ptr = (beg_of_comp_unit + cu.header.length
3388                   + cu.header.initial_length_size);
3389       do_cleanups (back_to_inner);
3390       return info_ptr;
3391     }
3392
3393   cu.list_in_scope = &file_symbols;
3394
3395   /* If this compilation unit was already read in, free the
3396      cached copy in order to read it in again.  This is
3397      necessary because we skipped some symbols when we first
3398      read in the compilation unit (see load_partial_dies).
3399      This problem could be avoided, but the benefit is
3400      unclear.  */
3401   if (this_cu->cu != NULL)
3402     free_one_cached_comp_unit (this_cu->cu);
3403
3404   /* Note that this is a pointer to our stack frame, being
3405      added to a global data structure.  It will be cleaned up
3406      in free_stack_comp_unit when we finish with this
3407      compilation unit.  */
3408   this_cu->cu = &cu;
3409   cu.per_cu = this_cu;
3410
3411   /* Read the abbrevs for this compilation unit into a table.  */
3412   dwarf2_read_abbrevs (abfd, &cu);
3413   make_cleanup (dwarf2_free_abbrev_table, &cu);
3414
3415   /* Read the compilation unit die.  */
3416   init_cu_die_reader (&reader_specs, &cu);
3417   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3418                             &has_children);
3419
3420   if (this_cu->debug_type_section)
3421     {
3422       /* LENGTH has not been set yet for type units.  */
3423       gdb_assert (this_cu->offset == cu.header.offset);
3424       this_cu->length = cu.header.length + cu.header.initial_length_size;
3425     }
3426   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3427     {
3428       info_ptr = (beg_of_comp_unit + cu.header.length
3429                   + cu.header.initial_length_size);
3430       do_cleanups (back_to_inner);
3431       return info_ptr;
3432     }
3433
3434   prepare_one_comp_unit (&cu, comp_unit_die);
3435
3436   /* Allocate a new partial symbol table structure.  */
3437   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3438   if (attr == NULL || !DW_STRING (attr))
3439     filename = "";
3440   else
3441     filename = DW_STRING (attr);
3442   pst = start_psymtab_common (objfile, objfile->section_offsets,
3443                               filename,
3444                               /* TEXTLOW and TEXTHIGH are set below.  */
3445                               0,
3446                               objfile->global_psymbols.next,
3447                               objfile->static_psymbols.next);
3448
3449   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3450   if (attr != NULL)
3451     pst->dirname = DW_STRING (attr);
3452
3453   pst->read_symtab_private = this_cu;
3454
3455   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3456
3457   /* Store the function that reads in the rest of the symbol table.  */
3458   pst->read_symtab = dwarf2_psymtab_to_symtab;
3459
3460   this_cu->v.psymtab = pst;
3461
3462   dwarf2_find_base_address (comp_unit_die, &cu);
3463
3464   /* Possibly set the default values of LOWPC and HIGHPC from
3465      `DW_AT_ranges'.  */
3466   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3467                                       &best_highpc, &cu, pst);
3468   if (has_pc_info == 1 && best_lowpc < best_highpc)
3469     /* Store the contiguous range if it is not empty; it can be empty for
3470        CUs with no code.  */
3471     addrmap_set_empty (objfile->psymtabs_addrmap,
3472                        best_lowpc + baseaddr,
3473                        best_highpc + baseaddr - 1, pst);
3474
3475   /* Check if comp unit has_children.
3476      If so, read the rest of the partial symbols from this comp unit.
3477      If not, there's no more debug_info for this comp unit.  */
3478   if (has_children)
3479     {
3480       struct partial_die_info *first_die;
3481       CORE_ADDR lowpc, highpc;
3482
3483       lowpc = ((CORE_ADDR) -1);
3484       highpc = ((CORE_ADDR) 0);
3485
3486       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3487
3488       scan_partial_symbols (first_die, &lowpc, &highpc,
3489                             ! has_pc_info, &cu);
3490
3491       /* If we didn't find a lowpc, set it to highpc to avoid
3492          complaints from `maint check'.  */
3493       if (lowpc == ((CORE_ADDR) -1))
3494         lowpc = highpc;
3495
3496       /* If the compilation unit didn't have an explicit address range,
3497          then use the information extracted from its child dies.  */
3498       if (! has_pc_info)
3499         {
3500           best_lowpc = lowpc;
3501           best_highpc = highpc;
3502         }
3503     }
3504   pst->textlow = best_lowpc + baseaddr;
3505   pst->texthigh = best_highpc + baseaddr;
3506
3507   pst->n_global_syms = objfile->global_psymbols.next -
3508     (objfile->global_psymbols.list + pst->globals_offset);
3509   pst->n_static_syms = objfile->static_psymbols.next -
3510     (objfile->static_psymbols.list + pst->statics_offset);
3511   sort_pst_symbols (pst);
3512
3513   info_ptr = (beg_of_comp_unit + cu.header.length
3514               + cu.header.initial_length_size);
3515
3516   if (this_cu->debug_type_section)
3517     {
3518       /* It's not clear we want to do anything with stmt lists here.
3519          Waiting to see what gcc ultimately does.  */
3520     }
3521   else
3522     {
3523       /* Get the list of files included in the current compilation unit,
3524          and build a psymtab for each of them.  */
3525       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3526     }
3527
3528   do_cleanups (back_to_inner);
3529
3530   return info_ptr;
3531 }
3532
3533 /* Traversal function for htab_traverse_noresize.
3534    Process one .debug_types comp-unit.  */
3535
3536 static int
3537 process_type_comp_unit (void **slot, void *info)
3538 {
3539   struct signatured_type *entry = (struct signatured_type *) *slot;
3540   struct objfile *objfile = (struct objfile *) info;
3541   struct dwarf2_per_cu_data *this_cu;
3542
3543   this_cu = &entry->per_cu;
3544
3545   gdb_assert (this_cu->debug_type_section->readin);
3546   process_psymtab_comp_unit (objfile, this_cu,
3547                              this_cu->debug_type_section->buffer,
3548                              (this_cu->debug_type_section->buffer
3549                               + this_cu->offset),
3550                              this_cu->debug_type_section->size);
3551
3552   return 1;
3553 }
3554
3555 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3556    Build partial symbol tables for the .debug_types comp-units.  */
3557
3558 static void
3559 build_type_psymtabs (struct objfile *objfile)
3560 {
3561   if (! create_debug_types_hash_table (objfile))
3562     return;
3563
3564   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3565                           process_type_comp_unit, objfile);
3566 }
3567
3568 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3569
3570 static void
3571 psymtabs_addrmap_cleanup (void *o)
3572 {
3573   struct objfile *objfile = o;
3574
3575   objfile->psymtabs_addrmap = NULL;
3576 }
3577
3578 /* Build the partial symbol table by doing a quick pass through the
3579    .debug_info and .debug_abbrev sections.  */
3580
3581 static void
3582 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3583 {
3584   gdb_byte *info_ptr;
3585   struct cleanup *back_to, *addrmap_cleanup;
3586   struct obstack temp_obstack;
3587
3588   dwarf2_per_objfile->reading_partial_symbols = 1;
3589
3590   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3591   info_ptr = dwarf2_per_objfile->info.buffer;
3592
3593   /* Any cached compilation units will be linked by the per-objfile
3594      read_in_chain.  Make sure to free them when we're done.  */
3595   back_to = make_cleanup (free_cached_comp_units, NULL);
3596
3597   build_type_psymtabs (objfile);
3598
3599   create_all_comp_units (objfile);
3600
3601   /* Create a temporary address map on a temporary obstack.  We later
3602      copy this to the final obstack.  */
3603   obstack_init (&temp_obstack);
3604   make_cleanup_obstack_free (&temp_obstack);
3605   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3606   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3607
3608   /* Since the objects we're extracting from .debug_info vary in
3609      length, only the individual functions to extract them (like
3610      read_comp_unit_head and load_partial_die) can really know whether
3611      the buffer is large enough to hold another complete object.
3612
3613      At the moment, they don't actually check that.  If .debug_info
3614      holds just one extra byte after the last compilation unit's dies,
3615      then read_comp_unit_head will happily read off the end of the
3616      buffer.  read_partial_die is similarly casual.  Those functions
3617      should be fixed.
3618
3619      For this loop condition, simply checking whether there's any data
3620      left at all should be sufficient.  */
3621
3622   while (info_ptr < (dwarf2_per_objfile->info.buffer
3623                      + dwarf2_per_objfile->info.size))
3624     {
3625       struct dwarf2_per_cu_data *this_cu;
3626
3627       this_cu = dwarf2_find_comp_unit (info_ptr
3628                                        - dwarf2_per_objfile->info.buffer,
3629                                        objfile);
3630
3631       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3632                                             dwarf2_per_objfile->info.buffer,
3633                                             info_ptr,
3634                                             dwarf2_per_objfile->info.size);
3635     }
3636
3637   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3638                                                     &objfile->objfile_obstack);
3639   discard_cleanups (addrmap_cleanup);
3640
3641   do_cleanups (back_to);
3642 }
3643
3644 /* Load the partial DIEs for a secondary CU into memory.  */
3645
3646 static void
3647 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3648                         struct objfile *objfile)
3649 {
3650   bfd *abfd = objfile->obfd;
3651   gdb_byte *info_ptr;
3652   struct die_info *comp_unit_die;
3653   struct dwarf2_cu *cu;
3654   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3655   int has_children;
3656   struct die_reader_specs reader_specs;
3657   int read_cu = 0;
3658
3659   gdb_assert (! this_cu->debug_type_section);
3660
3661   gdb_assert (dwarf2_per_objfile->info.readin);
3662   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3663
3664   if (this_cu->cu == NULL)
3665     {
3666       cu = xmalloc (sizeof (*cu));
3667       init_one_comp_unit (cu, objfile);
3668
3669       read_cu = 1;
3670
3671       /* If an error occurs while loading, release our storage.  */
3672       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3673
3674       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3675                                               dwarf2_per_objfile->info.buffer,
3676                                               dwarf2_per_objfile->info.size,
3677                                               abfd, 0);
3678
3679       /* Skip dummy compilation units.  */
3680       if (info_ptr >= (dwarf2_per_objfile->info.buffer
3681                        + dwarf2_per_objfile->info.size)
3682           || peek_abbrev_code (abfd, info_ptr) == 0)
3683         {
3684           do_cleanups (free_cu_cleanup);
3685           return;
3686         }
3687
3688       /* Link this compilation unit into the compilation unit tree.  */
3689       this_cu->cu = cu;
3690       cu->per_cu = this_cu;
3691
3692       /* Link this CU into read_in_chain.  */
3693       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3694       dwarf2_per_objfile->read_in_chain = this_cu;
3695     }
3696   else
3697     {
3698       cu = this_cu->cu;
3699       info_ptr += cu->header.first_die_offset;
3700     }
3701
3702   /* Read the abbrevs for this compilation unit into a table.  */
3703   gdb_assert (cu->dwarf2_abbrevs == NULL);
3704   dwarf2_read_abbrevs (abfd, cu);
3705   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3706
3707   /* Read the compilation unit die.  */
3708   init_cu_die_reader (&reader_specs, cu);
3709   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3710                             &has_children);
3711
3712   prepare_one_comp_unit (cu, comp_unit_die);
3713
3714   /* Check if comp unit has_children.
3715      If so, read the rest of the partial symbols from this comp unit.
3716      If not, there's no more debug_info for this comp unit.  */
3717   if (has_children)
3718     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3719
3720   do_cleanups (free_abbrevs_cleanup);
3721
3722   if (read_cu)
3723     {
3724       /* We've successfully allocated this compilation unit.  Let our
3725          caller clean it up when finished with it.  */
3726       discard_cleanups (free_cu_cleanup);
3727     }
3728 }
3729
3730 /* Create a list of all compilation units in OBJFILE.  We do this only
3731    if an inter-comp-unit reference is found; presumably if there is one,
3732    there will be many, and one will occur early in the .debug_info section.
3733    So there's no point in building this list incrementally.  */
3734
3735 static void
3736 create_all_comp_units (struct objfile *objfile)
3737 {
3738   int n_allocated;
3739   int n_comp_units;
3740   struct dwarf2_per_cu_data **all_comp_units;
3741   gdb_byte *info_ptr;
3742
3743   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3744   info_ptr = dwarf2_per_objfile->info.buffer;
3745
3746   n_comp_units = 0;
3747   n_allocated = 10;
3748   all_comp_units = xmalloc (n_allocated
3749                             * sizeof (struct dwarf2_per_cu_data *));
3750
3751   while (info_ptr < dwarf2_per_objfile->info.buffer
3752          + dwarf2_per_objfile->info.size)
3753     {
3754       unsigned int length, initial_length_size;
3755       struct dwarf2_per_cu_data *this_cu;
3756       unsigned int offset;
3757
3758       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3759
3760       /* Read just enough information to find out where the next
3761          compilation unit is.  */
3762       length = read_initial_length (objfile->obfd, info_ptr,
3763                                     &initial_length_size);
3764
3765       /* Save the compilation unit for later lookup.  */
3766       this_cu = obstack_alloc (&objfile->objfile_obstack,
3767                                sizeof (struct dwarf2_per_cu_data));
3768       memset (this_cu, 0, sizeof (*this_cu));
3769       this_cu->offset = offset;
3770       this_cu->length = length + initial_length_size;
3771       this_cu->objfile = objfile;
3772
3773       if (n_comp_units == n_allocated)
3774         {
3775           n_allocated *= 2;
3776           all_comp_units = xrealloc (all_comp_units,
3777                                      n_allocated
3778                                      * sizeof (struct dwarf2_per_cu_data *));
3779         }
3780       all_comp_units[n_comp_units++] = this_cu;
3781
3782       info_ptr = info_ptr + this_cu->length;
3783     }
3784
3785   dwarf2_per_objfile->all_comp_units
3786     = obstack_alloc (&objfile->objfile_obstack,
3787                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3788   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3789           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3790   xfree (all_comp_units);
3791   dwarf2_per_objfile->n_comp_units = n_comp_units;
3792 }
3793
3794 /* Process all loaded DIEs for compilation unit CU, starting at
3795    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3796    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3797    DW_AT_ranges).  If NEED_PC is set, then this function will set
3798    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3799    and record the covered ranges in the addrmap.  */
3800
3801 static void
3802 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3803                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3804 {
3805   struct partial_die_info *pdi;
3806
3807   /* Now, march along the PDI's, descending into ones which have
3808      interesting children but skipping the children of the other ones,
3809      until we reach the end of the compilation unit.  */
3810
3811   pdi = first_die;
3812
3813   while (pdi != NULL)
3814     {
3815       fixup_partial_die (pdi, cu);
3816
3817       /* Anonymous namespaces or modules have no name but have interesting
3818          children, so we need to look at them.  Ditto for anonymous
3819          enums.  */
3820
3821       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3822           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3823         {
3824           switch (pdi->tag)
3825             {
3826             case DW_TAG_subprogram:
3827               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3828               break;
3829             case DW_TAG_constant:
3830             case DW_TAG_variable:
3831             case DW_TAG_typedef:
3832             case DW_TAG_union_type:
3833               if (!pdi->is_declaration)
3834                 {
3835                   add_partial_symbol (pdi, cu);
3836                 }
3837               break;
3838             case DW_TAG_class_type:
3839             case DW_TAG_interface_type:
3840             case DW_TAG_structure_type:
3841               if (!pdi->is_declaration)
3842                 {
3843                   add_partial_symbol (pdi, cu);
3844                 }
3845               break;
3846             case DW_TAG_enumeration_type:
3847               if (!pdi->is_declaration)
3848                 add_partial_enumeration (pdi, cu);
3849               break;
3850             case DW_TAG_base_type:
3851             case DW_TAG_subrange_type:
3852               /* File scope base type definitions are added to the partial
3853                  symbol table.  */
3854               add_partial_symbol (pdi, cu);
3855               break;
3856             case DW_TAG_namespace:
3857               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3858               break;
3859             case DW_TAG_module:
3860               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3861               break;
3862             default:
3863               break;
3864             }
3865         }
3866
3867       /* If the die has a sibling, skip to the sibling.  */
3868
3869       pdi = pdi->die_sibling;
3870     }
3871 }
3872
3873 /* Functions used to compute the fully scoped name of a partial DIE.
3874
3875    Normally, this is simple.  For C++, the parent DIE's fully scoped
3876    name is concatenated with "::" and the partial DIE's name.  For
3877    Java, the same thing occurs except that "." is used instead of "::".
3878    Enumerators are an exception; they use the scope of their parent
3879    enumeration type, i.e. the name of the enumeration type is not
3880    prepended to the enumerator.
3881
3882    There are two complexities.  One is DW_AT_specification; in this
3883    case "parent" means the parent of the target of the specification,
3884    instead of the direct parent of the DIE.  The other is compilers
3885    which do not emit DW_TAG_namespace; in this case we try to guess
3886    the fully qualified name of structure types from their members'
3887    linkage names.  This must be done using the DIE's children rather
3888    than the children of any DW_AT_specification target.  We only need
3889    to do this for structures at the top level, i.e. if the target of
3890    any DW_AT_specification (if any; otherwise the DIE itself) does not
3891    have a parent.  */
3892
3893 /* Compute the scope prefix associated with PDI's parent, in
3894    compilation unit CU.  The result will be allocated on CU's
3895    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3896    field.  NULL is returned if no prefix is necessary.  */
3897 static char *
3898 partial_die_parent_scope (struct partial_die_info *pdi,
3899                           struct dwarf2_cu *cu)
3900 {
3901   char *grandparent_scope;
3902   struct partial_die_info *parent, *real_pdi;
3903
3904   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3905      then this means the parent of the specification DIE.  */
3906
3907   real_pdi = pdi;
3908   while (real_pdi->has_specification)
3909     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3910
3911   parent = real_pdi->die_parent;
3912   if (parent == NULL)
3913     return NULL;
3914
3915   if (parent->scope_set)
3916     return parent->scope;
3917
3918   fixup_partial_die (parent, cu);
3919
3920   grandparent_scope = partial_die_parent_scope (parent, cu);
3921
3922   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3923      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3924      Work around this problem here.  */
3925   if (cu->language == language_cplus
3926       && parent->tag == DW_TAG_namespace
3927       && strcmp (parent->name, "::") == 0
3928       && grandparent_scope == NULL)
3929     {
3930       parent->scope = NULL;
3931       parent->scope_set = 1;
3932       return NULL;
3933     }
3934
3935   if (pdi->tag == DW_TAG_enumerator)
3936     /* Enumerators should not get the name of the enumeration as a prefix.  */
3937     parent->scope = grandparent_scope;
3938   else if (parent->tag == DW_TAG_namespace
3939       || parent->tag == DW_TAG_module
3940       || parent->tag == DW_TAG_structure_type
3941       || parent->tag == DW_TAG_class_type
3942       || parent->tag == DW_TAG_interface_type
3943       || parent->tag == DW_TAG_union_type
3944       || parent->tag == DW_TAG_enumeration_type)
3945     {
3946       if (grandparent_scope == NULL)
3947         parent->scope = parent->name;
3948       else
3949         parent->scope = typename_concat (&cu->comp_unit_obstack,
3950                                          grandparent_scope,
3951                                          parent->name, 0, cu);
3952     }
3953   else
3954     {
3955       /* FIXME drow/2004-04-01: What should we be doing with
3956          function-local names?  For partial symbols, we should probably be
3957          ignoring them.  */
3958       complaint (&symfile_complaints,
3959                  _("unhandled containing DIE tag %d for DIE at %d"),
3960                  parent->tag, pdi->offset);
3961       parent->scope = grandparent_scope;
3962     }
3963
3964   parent->scope_set = 1;
3965   return parent->scope;
3966 }
3967
3968 /* Return the fully scoped name associated with PDI, from compilation unit
3969    CU.  The result will be allocated with malloc.  */
3970 static char *
3971 partial_die_full_name (struct partial_die_info *pdi,
3972                        struct dwarf2_cu *cu)
3973 {
3974   char *parent_scope;
3975
3976   /* If this is a template instantiation, we can not work out the
3977      template arguments from partial DIEs.  So, unfortunately, we have
3978      to go through the full DIEs.  At least any work we do building
3979      types here will be reused if full symbols are loaded later.  */
3980   if (pdi->has_template_arguments)
3981     {
3982       fixup_partial_die (pdi, cu);
3983
3984       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3985         {
3986           struct die_info *die;
3987           struct attribute attr;
3988           struct dwarf2_cu *ref_cu = cu;
3989
3990           attr.name = 0;
3991           attr.form = DW_FORM_ref_addr;
3992           attr.u.addr = pdi->offset;
3993           die = follow_die_ref (NULL, &attr, &ref_cu);
3994
3995           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3996         }
3997     }
3998
3999   parent_scope = partial_die_parent_scope (pdi, cu);
4000   if (parent_scope == NULL)
4001     return NULL;
4002   else
4003     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4004 }
4005
4006 static void
4007 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4008 {
4009   struct objfile *objfile = cu->objfile;
4010   CORE_ADDR addr = 0;
4011   char *actual_name = NULL;
4012   const struct partial_symbol *psym = NULL;
4013   CORE_ADDR baseaddr;
4014   int built_actual_name = 0;
4015
4016   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4017
4018   actual_name = partial_die_full_name (pdi, cu);
4019   if (actual_name)
4020     built_actual_name = 1;
4021
4022   if (actual_name == NULL)
4023     actual_name = pdi->name;
4024
4025   switch (pdi->tag)
4026     {
4027     case DW_TAG_subprogram:
4028       if (pdi->is_external || cu->language == language_ada)
4029         {
4030           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4031              of the global scope.  But in Ada, we want to be able to access
4032              nested procedures globally.  So all Ada subprograms are stored
4033              in the global scope.  */
4034           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4035              mst_text, objfile); */
4036           add_psymbol_to_list (actual_name, strlen (actual_name),
4037                                built_actual_name,
4038                                VAR_DOMAIN, LOC_BLOCK,
4039                                &objfile->global_psymbols,
4040                                0, pdi->lowpc + baseaddr,
4041                                cu->language, objfile);
4042         }
4043       else
4044         {
4045           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4046              mst_file_text, objfile); */
4047           add_psymbol_to_list (actual_name, strlen (actual_name),
4048                                built_actual_name,
4049                                VAR_DOMAIN, LOC_BLOCK,
4050                                &objfile->static_psymbols,
4051                                0, pdi->lowpc + baseaddr,
4052                                cu->language, objfile);
4053         }
4054       break;
4055     case DW_TAG_constant:
4056       {
4057         struct psymbol_allocation_list *list;
4058
4059         if (pdi->is_external)
4060           list = &objfile->global_psymbols;
4061         else
4062           list = &objfile->static_psymbols;
4063         add_psymbol_to_list (actual_name, strlen (actual_name),
4064                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4065                              list, 0, 0, cu->language, objfile);
4066       }
4067       break;
4068     case DW_TAG_variable:
4069       if (pdi->locdesc)
4070         addr = decode_locdesc (pdi->locdesc, cu);
4071
4072       if (pdi->locdesc
4073           && addr == 0
4074           && !dwarf2_per_objfile->has_section_at_zero)
4075         {
4076           /* A global or static variable may also have been stripped
4077              out by the linker if unused, in which case its address
4078              will be nullified; do not add such variables into partial
4079              symbol table then.  */
4080         }
4081       else if (pdi->is_external)
4082         {
4083           /* Global Variable.
4084              Don't enter into the minimal symbol tables as there is
4085              a minimal symbol table entry from the ELF symbols already.
4086              Enter into partial symbol table if it has a location
4087              descriptor or a type.
4088              If the location descriptor is missing, new_symbol will create
4089              a LOC_UNRESOLVED symbol, the address of the variable will then
4090              be determined from the minimal symbol table whenever the variable
4091              is referenced.
4092              The address for the partial symbol table entry is not
4093              used by GDB, but it comes in handy for debugging partial symbol
4094              table building.  */
4095
4096           if (pdi->locdesc || pdi->has_type)
4097             add_psymbol_to_list (actual_name, strlen (actual_name),
4098                                  built_actual_name,
4099                                  VAR_DOMAIN, LOC_STATIC,
4100                                  &objfile->global_psymbols,
4101                                  0, addr + baseaddr,
4102                                  cu->language, objfile);
4103         }
4104       else
4105         {
4106           /* Static Variable.  Skip symbols without location descriptors.  */
4107           if (pdi->locdesc == NULL)
4108             {
4109               if (built_actual_name)
4110                 xfree (actual_name);
4111               return;
4112             }
4113           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4114              mst_file_data, objfile); */
4115           add_psymbol_to_list (actual_name, strlen (actual_name),
4116                                built_actual_name,
4117                                VAR_DOMAIN, LOC_STATIC,
4118                                &objfile->static_psymbols,
4119                                0, addr + baseaddr,
4120                                cu->language, objfile);
4121         }
4122       break;
4123     case DW_TAG_typedef:
4124     case DW_TAG_base_type:
4125     case DW_TAG_subrange_type:
4126       add_psymbol_to_list (actual_name, strlen (actual_name),
4127                            built_actual_name,
4128                            VAR_DOMAIN, LOC_TYPEDEF,
4129                            &objfile->static_psymbols,
4130                            0, (CORE_ADDR) 0, cu->language, objfile);
4131       break;
4132     case DW_TAG_namespace:
4133       add_psymbol_to_list (actual_name, strlen (actual_name),
4134                            built_actual_name,
4135                            VAR_DOMAIN, LOC_TYPEDEF,
4136                            &objfile->global_psymbols,
4137                            0, (CORE_ADDR) 0, cu->language, objfile);
4138       break;
4139     case DW_TAG_class_type:
4140     case DW_TAG_interface_type:
4141     case DW_TAG_structure_type:
4142     case DW_TAG_union_type:
4143     case DW_TAG_enumeration_type:
4144       /* Skip external references.  The DWARF standard says in the section
4145          about "Structure, Union, and Class Type Entries": "An incomplete
4146          structure, union or class type is represented by a structure,
4147          union or class entry that does not have a byte size attribute
4148          and that has a DW_AT_declaration attribute."  */
4149       if (!pdi->has_byte_size && pdi->is_declaration)
4150         {
4151           if (built_actual_name)
4152             xfree (actual_name);
4153           return;
4154         }
4155
4156       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4157          static vs. global.  */
4158       add_psymbol_to_list (actual_name, strlen (actual_name),
4159                            built_actual_name,
4160                            STRUCT_DOMAIN, LOC_TYPEDEF,
4161                            (cu->language == language_cplus
4162                             || cu->language == language_java)
4163                            ? &objfile->global_psymbols
4164                            : &objfile->static_psymbols,
4165                            0, (CORE_ADDR) 0, cu->language, objfile);
4166
4167       break;
4168     case DW_TAG_enumerator:
4169       add_psymbol_to_list (actual_name, strlen (actual_name),
4170                            built_actual_name,
4171                            VAR_DOMAIN, LOC_CONST,
4172                            (cu->language == language_cplus
4173                             || cu->language == language_java)
4174                            ? &objfile->global_psymbols
4175                            : &objfile->static_psymbols,
4176                            0, (CORE_ADDR) 0, cu->language, objfile);
4177       break;
4178     default:
4179       break;
4180     }
4181
4182   if (built_actual_name)
4183     xfree (actual_name);
4184 }
4185
4186 /* Read a partial die corresponding to a namespace; also, add a symbol
4187    corresponding to that namespace to the symbol table.  NAMESPACE is
4188    the name of the enclosing namespace.  */
4189
4190 static void
4191 add_partial_namespace (struct partial_die_info *pdi,
4192                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4193                        int need_pc, struct dwarf2_cu *cu)
4194 {
4195   /* Add a symbol for the namespace.  */
4196
4197   add_partial_symbol (pdi, cu);
4198
4199   /* Now scan partial symbols in that namespace.  */
4200
4201   if (pdi->has_children)
4202     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4203 }
4204
4205 /* Read a partial die corresponding to a Fortran module.  */
4206
4207 static void
4208 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4209                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4210 {
4211   /* Now scan partial symbols in that module.  */
4212
4213   if (pdi->has_children)
4214     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4215 }
4216
4217 /* Read a partial die corresponding to a subprogram and create a partial
4218    symbol for that subprogram.  When the CU language allows it, this
4219    routine also defines a partial symbol for each nested subprogram
4220    that this subprogram contains.
4221
4222    DIE my also be a lexical block, in which case we simply search
4223    recursively for suprograms defined inside that lexical block.
4224    Again, this is only performed when the CU language allows this
4225    type of definitions.  */
4226
4227 static void
4228 add_partial_subprogram (struct partial_die_info *pdi,
4229                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4230                         int need_pc, struct dwarf2_cu *cu)
4231 {
4232   if (pdi->tag == DW_TAG_subprogram)
4233     {
4234       if (pdi->has_pc_info)
4235         {
4236           if (pdi->lowpc < *lowpc)
4237             *lowpc = pdi->lowpc;
4238           if (pdi->highpc > *highpc)
4239             *highpc = pdi->highpc;
4240           if (need_pc)
4241             {
4242               CORE_ADDR baseaddr;
4243               struct objfile *objfile = cu->objfile;
4244
4245               baseaddr = ANOFFSET (objfile->section_offsets,
4246                                    SECT_OFF_TEXT (objfile));
4247               addrmap_set_empty (objfile->psymtabs_addrmap,
4248                                  pdi->lowpc + baseaddr,
4249                                  pdi->highpc - 1 + baseaddr,
4250                                  cu->per_cu->v.psymtab);
4251             }
4252           if (!pdi->is_declaration)
4253             /* Ignore subprogram DIEs that do not have a name, they are
4254                illegal.  Do not emit a complaint at this point, we will
4255                do so when we convert this psymtab into a symtab.  */
4256             if (pdi->name)
4257               add_partial_symbol (pdi, cu);
4258         }
4259     }
4260
4261   if (! pdi->has_children)
4262     return;
4263
4264   if (cu->language == language_ada)
4265     {
4266       pdi = pdi->die_child;
4267       while (pdi != NULL)
4268         {
4269           fixup_partial_die (pdi, cu);
4270           if (pdi->tag == DW_TAG_subprogram
4271               || pdi->tag == DW_TAG_lexical_block)
4272             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4273           pdi = pdi->die_sibling;
4274         }
4275     }
4276 }
4277
4278 /* Read a partial die corresponding to an enumeration type.  */
4279
4280 static void
4281 add_partial_enumeration (struct partial_die_info *enum_pdi,
4282                          struct dwarf2_cu *cu)
4283 {
4284   struct partial_die_info *pdi;
4285
4286   if (enum_pdi->name != NULL)
4287     add_partial_symbol (enum_pdi, cu);
4288
4289   pdi = enum_pdi->die_child;
4290   while (pdi)
4291     {
4292       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4293         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4294       else
4295         add_partial_symbol (pdi, cu);
4296       pdi = pdi->die_sibling;
4297     }
4298 }
4299
4300 /* Return the initial uleb128 in the die at INFO_PTR.  */
4301
4302 static unsigned int
4303 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4304 {
4305   unsigned int bytes_read;
4306
4307   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4308 }
4309
4310 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4311    Return the corresponding abbrev, or NULL if the number is zero (indicating
4312    an empty DIE).  In either case *BYTES_READ will be set to the length of
4313    the initial number.  */
4314
4315 static struct abbrev_info *
4316 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4317                  struct dwarf2_cu *cu)
4318 {
4319   bfd *abfd = cu->objfile->obfd;
4320   unsigned int abbrev_number;
4321   struct abbrev_info *abbrev;
4322
4323   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4324
4325   if (abbrev_number == 0)
4326     return NULL;
4327
4328   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4329   if (!abbrev)
4330     {
4331       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4332              abbrev_number, bfd_get_filename (abfd));
4333     }
4334
4335   return abbrev;
4336 }
4337
4338 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4339    Returns a pointer to the end of a series of DIEs, terminated by an empty
4340    DIE.  Any children of the skipped DIEs will also be skipped.  */
4341
4342 static gdb_byte *
4343 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4344 {
4345   struct abbrev_info *abbrev;
4346   unsigned int bytes_read;
4347
4348   while (1)
4349     {
4350       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4351       if (abbrev == NULL)
4352         return info_ptr + bytes_read;
4353       else
4354         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4355     }
4356 }
4357
4358 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4359    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4360    abbrev corresponding to that skipped uleb128 should be passed in
4361    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4362    children.  */
4363
4364 static gdb_byte *
4365 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4366               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4367 {
4368   unsigned int bytes_read;
4369   struct attribute attr;
4370   bfd *abfd = cu->objfile->obfd;
4371   unsigned int form, i;
4372
4373   for (i = 0; i < abbrev->num_attrs; i++)
4374     {
4375       /* The only abbrev we care about is DW_AT_sibling.  */
4376       if (abbrev->attrs[i].name == DW_AT_sibling)
4377         {
4378           read_attribute (&attr, &abbrev->attrs[i],
4379                           abfd, info_ptr, cu);
4380           if (attr.form == DW_FORM_ref_addr)
4381             complaint (&symfile_complaints,
4382                        _("ignoring absolute DW_AT_sibling"));
4383           else
4384             return buffer + dwarf2_get_ref_die_offset (&attr);
4385         }
4386
4387       /* If it isn't DW_AT_sibling, skip this attribute.  */
4388       form = abbrev->attrs[i].form;
4389     skip_attribute:
4390       switch (form)
4391         {
4392         case DW_FORM_ref_addr:
4393           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4394              and later it is offset sized.  */
4395           if (cu->header.version == 2)
4396             info_ptr += cu->header.addr_size;
4397           else
4398             info_ptr += cu->header.offset_size;
4399           break;
4400         case DW_FORM_addr:
4401           info_ptr += cu->header.addr_size;
4402           break;
4403         case DW_FORM_data1:
4404         case DW_FORM_ref1:
4405         case DW_FORM_flag:
4406           info_ptr += 1;
4407           break;
4408         case DW_FORM_flag_present:
4409           break;
4410         case DW_FORM_data2:
4411         case DW_FORM_ref2:
4412           info_ptr += 2;
4413           break;
4414         case DW_FORM_data4:
4415         case DW_FORM_ref4:
4416           info_ptr += 4;
4417           break;
4418         case DW_FORM_data8:
4419         case DW_FORM_ref8:
4420         case DW_FORM_ref_sig8:
4421           info_ptr += 8;
4422           break;
4423         case DW_FORM_string:
4424           read_direct_string (abfd, info_ptr, &bytes_read);
4425           info_ptr += bytes_read;
4426           break;
4427         case DW_FORM_sec_offset:
4428         case DW_FORM_strp:
4429           info_ptr += cu->header.offset_size;
4430           break;
4431         case DW_FORM_exprloc:
4432         case DW_FORM_block:
4433           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4434           info_ptr += bytes_read;
4435           break;
4436         case DW_FORM_block1:
4437           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4438           break;
4439         case DW_FORM_block2:
4440           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4441           break;
4442         case DW_FORM_block4:
4443           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4444           break;
4445         case DW_FORM_sdata:
4446         case DW_FORM_udata:
4447         case DW_FORM_ref_udata:
4448           info_ptr = skip_leb128 (abfd, info_ptr);
4449           break;
4450         case DW_FORM_indirect:
4451           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4452           info_ptr += bytes_read;
4453           /* We need to continue parsing from here, so just go back to
4454              the top.  */
4455           goto skip_attribute;
4456
4457         default:
4458           error (_("Dwarf Error: Cannot handle %s "
4459                    "in DWARF reader [in module %s]"),
4460                  dwarf_form_name (form),
4461                  bfd_get_filename (abfd));
4462         }
4463     }
4464
4465   if (abbrev->has_children)
4466     return skip_children (buffer, info_ptr, cu);
4467   else
4468     return info_ptr;
4469 }
4470
4471 /* Locate ORIG_PDI's sibling.
4472    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4473    in BUFFER.  */
4474
4475 static gdb_byte *
4476 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4477                     gdb_byte *buffer, gdb_byte *info_ptr,
4478                     bfd *abfd, struct dwarf2_cu *cu)
4479 {
4480   /* Do we know the sibling already?  */
4481
4482   if (orig_pdi->sibling)
4483     return orig_pdi->sibling;
4484
4485   /* Are there any children to deal with?  */
4486
4487   if (!orig_pdi->has_children)
4488     return info_ptr;
4489
4490   /* Skip the children the long way.  */
4491
4492   return skip_children (buffer, info_ptr, cu);
4493 }
4494
4495 /* Expand this partial symbol table into a full symbol table.  */
4496
4497 static void
4498 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4499 {
4500   if (pst != NULL)
4501     {
4502       if (pst->readin)
4503         {
4504           warning (_("bug: psymtab for %s is already read in."),
4505                    pst->filename);
4506         }
4507       else
4508         {
4509           if (info_verbose)
4510             {
4511               printf_filtered (_("Reading in symbols for %s..."),
4512                                pst->filename);
4513               gdb_flush (gdb_stdout);
4514             }
4515
4516           /* Restore our global data.  */
4517           dwarf2_per_objfile = objfile_data (pst->objfile,
4518                                              dwarf2_objfile_data_key);
4519
4520           /* If this psymtab is constructed from a debug-only objfile, the
4521              has_section_at_zero flag will not necessarily be correct.  We
4522              can get the correct value for this flag by looking at the data
4523              associated with the (presumably stripped) associated objfile.  */
4524           if (pst->objfile->separate_debug_objfile_backlink)
4525             {
4526               struct dwarf2_per_objfile *dpo_backlink
4527                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4528                                 dwarf2_objfile_data_key);
4529
4530               dwarf2_per_objfile->has_section_at_zero
4531                 = dpo_backlink->has_section_at_zero;
4532             }
4533
4534           dwarf2_per_objfile->reading_partial_symbols = 0;
4535
4536           psymtab_to_symtab_1 (pst);
4537
4538           /* Finish up the debug error message.  */
4539           if (info_verbose)
4540             printf_filtered (_("done.\n"));
4541         }
4542     }
4543 }
4544
4545 /* Add PER_CU to the queue.  */
4546
4547 static void
4548 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4549 {
4550   struct dwarf2_queue_item *item;
4551
4552   per_cu->queued = 1;
4553   item = xmalloc (sizeof (*item));
4554   item->per_cu = per_cu;
4555   item->next = NULL;
4556
4557   if (dwarf2_queue == NULL)
4558     dwarf2_queue = item;
4559   else
4560     dwarf2_queue_tail->next = item;
4561
4562   dwarf2_queue_tail = item;
4563 }
4564
4565 /* Process the queue.  */
4566
4567 static void
4568 process_queue (struct objfile *objfile)
4569 {
4570   struct dwarf2_queue_item *item, *next_item;
4571
4572   /* The queue starts out with one item, but following a DIE reference
4573      may load a new CU, adding it to the end of the queue.  */
4574   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4575     {
4576       if (dwarf2_per_objfile->using_index
4577           ? !item->per_cu->v.quick->symtab
4578           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4579         process_full_comp_unit (item->per_cu);
4580
4581       item->per_cu->queued = 0;
4582       next_item = item->next;
4583       xfree (item);
4584     }
4585
4586   dwarf2_queue_tail = NULL;
4587 }
4588
4589 /* Free all allocated queue entries.  This function only releases anything if
4590    an error was thrown; if the queue was processed then it would have been
4591    freed as we went along.  */
4592
4593 static void
4594 dwarf2_release_queue (void *dummy)
4595 {
4596   struct dwarf2_queue_item *item, *last;
4597
4598   item = dwarf2_queue;
4599   while (item)
4600     {
4601       /* Anything still marked queued is likely to be in an
4602          inconsistent state, so discard it.  */
4603       if (item->per_cu->queued)
4604         {
4605           if (item->per_cu->cu != NULL)
4606             free_one_cached_comp_unit (item->per_cu->cu);
4607           item->per_cu->queued = 0;
4608         }
4609
4610       last = item;
4611       item = item->next;
4612       xfree (last);
4613     }
4614
4615   dwarf2_queue = dwarf2_queue_tail = NULL;
4616 }
4617
4618 /* Read in full symbols for PST, and anything it depends on.  */
4619
4620 static void
4621 psymtab_to_symtab_1 (struct partial_symtab *pst)
4622 {
4623   struct dwarf2_per_cu_data *per_cu;
4624   struct cleanup *back_to;
4625   int i;
4626
4627   for (i = 0; i < pst->number_of_dependencies; i++)
4628     if (!pst->dependencies[i]->readin)
4629       {
4630         /* Inform about additional files that need to be read in.  */
4631         if (info_verbose)
4632           {
4633             /* FIXME: i18n: Need to make this a single string.  */
4634             fputs_filtered (" ", gdb_stdout);
4635             wrap_here ("");
4636             fputs_filtered ("and ", gdb_stdout);
4637             wrap_here ("");
4638             printf_filtered ("%s...", pst->dependencies[i]->filename);
4639             wrap_here ("");     /* Flush output.  */
4640             gdb_flush (gdb_stdout);
4641           }
4642         psymtab_to_symtab_1 (pst->dependencies[i]);
4643       }
4644
4645   per_cu = pst->read_symtab_private;
4646
4647   if (per_cu == NULL)
4648     {
4649       /* It's an include file, no symbols to read for it.
4650          Everything is in the parent symtab.  */
4651       pst->readin = 1;
4652       return;
4653     }
4654
4655   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4656 }
4657
4658 /* Load the DIEs associated with PER_CU into memory.  */
4659
4660 static void
4661 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4662                      struct objfile *objfile)
4663 {
4664   bfd *abfd = objfile->obfd;
4665   struct dwarf2_cu *cu;
4666   unsigned int offset;
4667   gdb_byte *info_ptr, *beg_of_comp_unit;
4668   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4669   struct attribute *attr;
4670   int read_cu = 0;
4671
4672   gdb_assert (! per_cu->debug_type_section);
4673
4674   /* Set local variables from the partial symbol table info.  */
4675   offset = per_cu->offset;
4676
4677   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4678   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4679   beg_of_comp_unit = info_ptr;
4680
4681   if (per_cu->cu == NULL)
4682     {
4683       cu = xmalloc (sizeof (*cu));
4684       init_one_comp_unit (cu, objfile);
4685
4686       read_cu = 1;
4687
4688       /* If an error occurs while loading, release our storage.  */
4689       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4690
4691       /* Read in the comp_unit header.  */
4692       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4693
4694       /* Skip dummy compilation units.  */
4695       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4696                        + dwarf2_per_objfile->info.size)
4697           || peek_abbrev_code (abfd, info_ptr) == 0)
4698         {
4699           do_cleanups (free_cu_cleanup);
4700           return;
4701         }
4702
4703       /* Complete the cu_header.  */
4704       cu->header.offset = offset;
4705       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4706
4707       /* Read the abbrevs for this compilation unit.  */
4708       dwarf2_read_abbrevs (abfd, cu);
4709       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4710
4711       /* Link this compilation unit into the compilation unit tree.  */
4712       per_cu->cu = cu;
4713       cu->per_cu = per_cu;
4714
4715       /* Link this CU into read_in_chain.  */
4716       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4717       dwarf2_per_objfile->read_in_chain = per_cu;
4718     }
4719   else
4720     {
4721       cu = per_cu->cu;
4722       info_ptr += cu->header.first_die_offset;
4723     }
4724
4725   cu->dies = read_comp_unit (info_ptr, cu);
4726
4727   /* We try not to read any attributes in this function, because not
4728      all objfiles needed for references have been loaded yet, and symbol
4729      table processing isn't initialized.  But we have to set the CU language,
4730      or we won't be able to build types correctly.  */
4731   prepare_one_comp_unit (cu, cu->dies);
4732
4733   /* Similarly, if we do not read the producer, we can not apply
4734      producer-specific interpretation.  */
4735   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4736   if (attr)
4737     cu->producer = DW_STRING (attr);
4738
4739   if (read_cu)
4740     {
4741       do_cleanups (free_abbrevs_cleanup);
4742
4743       /* We've successfully allocated this compilation unit.  Let our
4744          caller clean it up when finished with it.  */
4745       discard_cleanups (free_cu_cleanup);
4746     }
4747 }
4748
4749 /* Add a DIE to the delayed physname list.  */
4750
4751 static void
4752 add_to_method_list (struct type *type, int fnfield_index, int index,
4753                     const char *name, struct die_info *die,
4754                     struct dwarf2_cu *cu)
4755 {
4756   struct delayed_method_info mi;
4757   mi.type = type;
4758   mi.fnfield_index = fnfield_index;
4759   mi.index = index;
4760   mi.name = name;
4761   mi.die = die;
4762   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4763 }
4764
4765 /* A cleanup for freeing the delayed method list.  */
4766
4767 static void
4768 free_delayed_list (void *ptr)
4769 {
4770   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4771   if (cu->method_list != NULL)
4772     {
4773       VEC_free (delayed_method_info, cu->method_list);
4774       cu->method_list = NULL;
4775     }
4776 }
4777
4778 /* Compute the physnames of any methods on the CU's method list.
4779
4780    The computation of method physnames is delayed in order to avoid the
4781    (bad) condition that one of the method's formal parameters is of an as yet
4782    incomplete type.  */
4783
4784 static void
4785 compute_delayed_physnames (struct dwarf2_cu *cu)
4786 {
4787   int i;
4788   struct delayed_method_info *mi;
4789   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4790     {
4791       const char *physname;
4792       struct fn_fieldlist *fn_flp
4793         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4794       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4795       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4796     }
4797 }
4798
4799 /* Generate full symbol information for PST and CU, whose DIEs have
4800    already been loaded into memory.  */
4801
4802 static void
4803 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4804 {
4805   struct dwarf2_cu *cu = per_cu->cu;
4806   struct objfile *objfile = per_cu->objfile;
4807   CORE_ADDR lowpc, highpc;
4808   struct symtab *symtab;
4809   struct cleanup *back_to, *delayed_list_cleanup;
4810   CORE_ADDR baseaddr;
4811
4812   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4813
4814   buildsym_init ();
4815   back_to = make_cleanup (really_free_pendings, NULL);
4816   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4817
4818   cu->list_in_scope = &file_symbols;
4819
4820   /* Do line number decoding in read_file_scope () */
4821   process_die (cu->dies, cu);
4822
4823   /* Now that we have processed all the DIEs in the CU, all the types 
4824      should be complete, and it should now be safe to compute all of the
4825      physnames.  */
4826   compute_delayed_physnames (cu);
4827   do_cleanups (delayed_list_cleanup);
4828
4829   /* Some compilers don't define a DW_AT_high_pc attribute for the
4830      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4831      it, by scanning the DIE's below the compilation unit.  */
4832   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4833
4834   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4835
4836   if (symtab != NULL)
4837     {
4838       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4839
4840       /* Set symtab language to language from DW_AT_language.  If the
4841          compilation is from a C file generated by language preprocessors, do
4842          not set the language if it was already deduced by start_subfile.  */
4843       if (!(cu->language == language_c && symtab->language != language_c))
4844         symtab->language = cu->language;
4845
4846       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4847          produce DW_AT_location with location lists but it can be possibly
4848          invalid without -fvar-tracking.
4849
4850          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4851          needed, it would be wrong due to missing DW_AT_producer there.
4852
4853          Still one can confuse GDB by using non-standard GCC compilation
4854          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4855          */ 
4856       if (cu->has_loclist && gcc_4_minor >= 0)
4857         symtab->locations_valid = 1;
4858
4859       if (gcc_4_minor >= 5)
4860         symtab->epilogue_unwind_valid = 1;
4861
4862       symtab->call_site_htab = cu->call_site_htab;
4863     }
4864
4865   if (dwarf2_per_objfile->using_index)
4866     per_cu->v.quick->symtab = symtab;
4867   else
4868     {
4869       struct partial_symtab *pst = per_cu->v.psymtab;
4870       pst->symtab = symtab;
4871       pst->readin = 1;
4872     }
4873
4874   do_cleanups (back_to);
4875 }
4876
4877 /* Process a die and its children.  */
4878
4879 static void
4880 process_die (struct die_info *die, struct dwarf2_cu *cu)
4881 {
4882   switch (die->tag)
4883     {
4884     case DW_TAG_padding:
4885       break;
4886     case DW_TAG_compile_unit:
4887       read_file_scope (die, cu);
4888       break;
4889     case DW_TAG_type_unit:
4890       read_type_unit_scope (die, cu);
4891       break;
4892     case DW_TAG_subprogram:
4893     case DW_TAG_inlined_subroutine:
4894       read_func_scope (die, cu);
4895       break;
4896     case DW_TAG_lexical_block:
4897     case DW_TAG_try_block:
4898     case DW_TAG_catch_block:
4899       read_lexical_block_scope (die, cu);
4900       break;
4901     case DW_TAG_GNU_call_site:
4902       read_call_site_scope (die, cu);
4903       break;
4904     case DW_TAG_class_type:
4905     case DW_TAG_interface_type:
4906     case DW_TAG_structure_type:
4907     case DW_TAG_union_type:
4908       process_structure_scope (die, cu);
4909       break;
4910     case DW_TAG_enumeration_type:
4911       process_enumeration_scope (die, cu);
4912       break;
4913
4914     /* These dies have a type, but processing them does not create
4915        a symbol or recurse to process the children.  Therefore we can
4916        read them on-demand through read_type_die.  */
4917     case DW_TAG_subroutine_type:
4918     case DW_TAG_set_type:
4919     case DW_TAG_array_type:
4920     case DW_TAG_pointer_type:
4921     case DW_TAG_ptr_to_member_type:
4922     case DW_TAG_reference_type:
4923     case DW_TAG_string_type:
4924       break;
4925
4926     case DW_TAG_base_type:
4927     case DW_TAG_subrange_type:
4928     case DW_TAG_typedef:
4929       /* Add a typedef symbol for the type definition, if it has a
4930          DW_AT_name.  */
4931       new_symbol (die, read_type_die (die, cu), cu);
4932       break;
4933     case DW_TAG_common_block:
4934       read_common_block (die, cu);
4935       break;
4936     case DW_TAG_common_inclusion:
4937       break;
4938     case DW_TAG_namespace:
4939       processing_has_namespace_info = 1;
4940       read_namespace (die, cu);
4941       break;
4942     case DW_TAG_module:
4943       processing_has_namespace_info = 1;
4944       read_module (die, cu);
4945       break;
4946     case DW_TAG_imported_declaration:
4947     case DW_TAG_imported_module:
4948       processing_has_namespace_info = 1;
4949       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4950                                  || cu->language != language_fortran))
4951         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4952                    dwarf_tag_name (die->tag));
4953       read_import_statement (die, cu);
4954       break;
4955     default:
4956       new_symbol (die, NULL, cu);
4957       break;
4958     }
4959 }
4960
4961 /* A helper function for dwarf2_compute_name which determines whether DIE
4962    needs to have the name of the scope prepended to the name listed in the
4963    die.  */
4964
4965 static int
4966 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4967 {
4968   struct attribute *attr;
4969
4970   switch (die->tag)
4971     {
4972     case DW_TAG_namespace:
4973     case DW_TAG_typedef:
4974     case DW_TAG_class_type:
4975     case DW_TAG_interface_type:
4976     case DW_TAG_structure_type:
4977     case DW_TAG_union_type:
4978     case DW_TAG_enumeration_type:
4979     case DW_TAG_enumerator:
4980     case DW_TAG_subprogram:
4981     case DW_TAG_member:
4982       return 1;
4983
4984     case DW_TAG_variable:
4985     case DW_TAG_constant:
4986       /* We only need to prefix "globally" visible variables.  These include
4987          any variable marked with DW_AT_external or any variable that
4988          lives in a namespace.  [Variables in anonymous namespaces
4989          require prefixing, but they are not DW_AT_external.]  */
4990
4991       if (dwarf2_attr (die, DW_AT_specification, cu))
4992         {
4993           struct dwarf2_cu *spec_cu = cu;
4994
4995           return die_needs_namespace (die_specification (die, &spec_cu),
4996                                       spec_cu);
4997         }
4998
4999       attr = dwarf2_attr (die, DW_AT_external, cu);
5000       if (attr == NULL && die->parent->tag != DW_TAG_namespace
5001           && die->parent->tag != DW_TAG_module)
5002         return 0;
5003       /* A variable in a lexical block of some kind does not need a
5004          namespace, even though in C++ such variables may be external
5005          and have a mangled name.  */
5006       if (die->parent->tag ==  DW_TAG_lexical_block
5007           || die->parent->tag ==  DW_TAG_try_block
5008           || die->parent->tag ==  DW_TAG_catch_block
5009           || die->parent->tag == DW_TAG_subprogram)
5010         return 0;
5011       return 1;
5012
5013     default:
5014       return 0;
5015     }
5016 }
5017
5018 /* Retrieve the last character from a mem_file.  */
5019
5020 static void
5021 do_ui_file_peek_last (void *object, const char *buffer, long length)
5022 {
5023   char *last_char_p = (char *) object;
5024
5025   if (length > 0)
5026     *last_char_p = buffer[length - 1];
5027 }
5028
5029 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
5030    compute the physname for the object, which include a method's
5031    formal parameters (C++/Java) and return type (Java).
5032
5033    For Ada, return the DIE's linkage name rather than the fully qualified
5034    name.  PHYSNAME is ignored..
5035
5036    The result is allocated on the objfile_obstack and canonicalized.  */
5037
5038 static const char *
5039 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5040                      int physname)
5041 {
5042   if (name == NULL)
5043     name = dwarf2_name (die, cu);
5044
5045   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5046      compute it by typename_concat inside GDB.  */
5047   if (cu->language == language_ada
5048       || (cu->language == language_fortran && physname))
5049     {
5050       /* For Ada unit, we prefer the linkage name over the name, as
5051          the former contains the exported name, which the user expects
5052          to be able to reference.  Ideally, we want the user to be able
5053          to reference this entity using either natural or linkage name,
5054          but we haven't started looking at this enhancement yet.  */
5055       struct attribute *attr;
5056
5057       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5058       if (attr == NULL)
5059         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5060       if (attr && DW_STRING (attr))
5061         return DW_STRING (attr);
5062     }
5063
5064   /* These are the only languages we know how to qualify names in.  */
5065   if (name != NULL
5066       && (cu->language == language_cplus || cu->language == language_java
5067           || cu->language == language_fortran))
5068     {
5069       if (die_needs_namespace (die, cu))
5070         {
5071           long length;
5072           char *prefix;
5073           struct ui_file *buf;
5074
5075           prefix = determine_prefix (die, cu);
5076           buf = mem_fileopen ();
5077           if (*prefix != '\0')
5078             {
5079               char *prefixed_name = typename_concat (NULL, prefix, name,
5080                                                      physname, cu);
5081
5082               fputs_unfiltered (prefixed_name, buf);
5083               xfree (prefixed_name);
5084             }
5085           else
5086             fputs_unfiltered (name, buf);
5087
5088           /* Template parameters may be specified in the DIE's DW_AT_name, or
5089              as children with DW_TAG_template_type_param or
5090              DW_TAG_value_type_param.  If the latter, add them to the name
5091              here.  If the name already has template parameters, then
5092              skip this step; some versions of GCC emit both, and
5093              it is more efficient to use the pre-computed name.
5094
5095              Something to keep in mind about this process: it is very
5096              unlikely, or in some cases downright impossible, to produce
5097              something that will match the mangled name of a function.
5098              If the definition of the function has the same debug info,
5099              we should be able to match up with it anyway.  But fallbacks
5100              using the minimal symbol, for instance to find a method
5101              implemented in a stripped copy of libstdc++, will not work.
5102              If we do not have debug info for the definition, we will have to
5103              match them up some other way.
5104
5105              When we do name matching there is a related problem with function
5106              templates; two instantiated function templates are allowed to
5107              differ only by their return types, which we do not add here.  */
5108
5109           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5110             {
5111               struct attribute *attr;
5112               struct die_info *child;
5113               int first = 1;
5114
5115               die->building_fullname = 1;
5116
5117               for (child = die->child; child != NULL; child = child->sibling)
5118                 {
5119                   struct type *type;
5120                   long value;
5121                   gdb_byte *bytes;
5122                   struct dwarf2_locexpr_baton *baton;
5123                   struct value *v;
5124
5125                   if (child->tag != DW_TAG_template_type_param
5126                       && child->tag != DW_TAG_template_value_param)
5127                     continue;
5128
5129                   if (first)
5130                     {
5131                       fputs_unfiltered ("<", buf);
5132                       first = 0;
5133                     }
5134                   else
5135                     fputs_unfiltered (", ", buf);
5136
5137                   attr = dwarf2_attr (child, DW_AT_type, cu);
5138                   if (attr == NULL)
5139                     {
5140                       complaint (&symfile_complaints,
5141                                  _("template parameter missing DW_AT_type"));
5142                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5143                       continue;
5144                     }
5145                   type = die_type (child, cu);
5146
5147                   if (child->tag == DW_TAG_template_type_param)
5148                     {
5149                       c_print_type (type, "", buf, -1, 0);
5150                       continue;
5151                     }
5152
5153                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5154                   if (attr == NULL)
5155                     {
5156                       complaint (&symfile_complaints,
5157                                  _("template parameter missing "
5158                                    "DW_AT_const_value"));
5159                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5160                       continue;
5161                     }
5162
5163                   dwarf2_const_value_attr (attr, type, name,
5164                                            &cu->comp_unit_obstack, cu,
5165                                            &value, &bytes, &baton);
5166
5167                   if (TYPE_NOSIGN (type))
5168                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5169                        changed, this can use value_print instead.  */
5170                     c_printchar (value, type, buf);
5171                   else
5172                     {
5173                       struct value_print_options opts;
5174
5175                       if (baton != NULL)
5176                         v = dwarf2_evaluate_loc_desc (type, NULL,
5177                                                       baton->data,
5178                                                       baton->size,
5179                                                       baton->per_cu);
5180                       else if (bytes != NULL)
5181                         {
5182                           v = allocate_value (type);
5183                           memcpy (value_contents_writeable (v), bytes,
5184                                   TYPE_LENGTH (type));
5185                         }
5186                       else
5187                         v = value_from_longest (type, value);
5188
5189                       /* Specify decimal so that we do not depend on
5190                          the radix.  */
5191                       get_formatted_print_options (&opts, 'd');
5192                       opts.raw = 1;
5193                       value_print (v, buf, &opts);
5194                       release_value (v);
5195                       value_free (v);
5196                     }
5197                 }
5198
5199               die->building_fullname = 0;
5200
5201               if (!first)
5202                 {
5203                   /* Close the argument list, with a space if necessary
5204                      (nested templates).  */
5205                   char last_char = '\0';
5206                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5207                   if (last_char == '>')
5208                     fputs_unfiltered (" >", buf);
5209                   else
5210                     fputs_unfiltered (">", buf);
5211                 }
5212             }
5213
5214           /* For Java and C++ methods, append formal parameter type
5215              information, if PHYSNAME.  */
5216
5217           if (physname && die->tag == DW_TAG_subprogram
5218               && (cu->language == language_cplus
5219                   || cu->language == language_java))
5220             {
5221               struct type *type = read_type_die (die, cu);
5222
5223               c_type_print_args (type, buf, 1, cu->language);
5224
5225               if (cu->language == language_java)
5226                 {
5227                   /* For java, we must append the return type to method
5228                      names.  */
5229                   if (die->tag == DW_TAG_subprogram)
5230                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5231                                      0, 0);
5232                 }
5233               else if (cu->language == language_cplus)
5234                 {
5235                   /* Assume that an artificial first parameter is
5236                      "this", but do not crash if it is not.  RealView
5237                      marks unnamed (and thus unused) parameters as
5238                      artificial; there is no way to differentiate
5239                      the two cases.  */
5240                   if (TYPE_NFIELDS (type) > 0
5241                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5242                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5243                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5244                                                                         0))))
5245                     fputs_unfiltered (" const", buf);
5246                 }
5247             }
5248
5249           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5250                                        &length);
5251           ui_file_delete (buf);
5252
5253           if (cu->language == language_cplus)
5254             {
5255               char *cname
5256                 = dwarf2_canonicalize_name (name, cu,
5257                                             &cu->objfile->objfile_obstack);
5258
5259               if (cname != NULL)
5260                 name = cname;
5261             }
5262         }
5263     }
5264
5265   return name;
5266 }
5267
5268 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5269    If scope qualifiers are appropriate they will be added.  The result
5270    will be allocated on the objfile_obstack, or NULL if the DIE does
5271    not have a name.  NAME may either be from a previous call to
5272    dwarf2_name or NULL.
5273
5274    The output string will be canonicalized (if C++/Java).  */
5275
5276 static const char *
5277 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5278 {
5279   return dwarf2_compute_name (name, die, cu, 0);
5280 }
5281
5282 /* Construct a physname for the given DIE in CU.  NAME may either be
5283    from a previous call to dwarf2_name or NULL.  The result will be
5284    allocated on the objfile_objstack or NULL if the DIE does not have a
5285    name.
5286
5287    The output string will be canonicalized (if C++/Java).  */
5288
5289 static const char *
5290 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5291 {
5292   struct attribute *attr;
5293   const char *retval, *mangled = NULL, *canon = NULL;
5294   struct cleanup *back_to;
5295   int need_copy = 1;
5296
5297   /* In this case dwarf2_compute_name is just a shortcut not building anything
5298      on its own.  */
5299   if (!die_needs_namespace (die, cu))
5300     return dwarf2_compute_name (name, die, cu, 1);
5301
5302   back_to = make_cleanup (null_cleanup, NULL);
5303
5304   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5305   if (!attr)
5306     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5307
5308   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5309      has computed.  */
5310   if (attr && DW_STRING (attr))
5311     {
5312       char *demangled;
5313
5314       mangled = DW_STRING (attr);
5315
5316       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5317          type.  It is easier for GDB users to search for such functions as
5318          `name(params)' than `long name(params)'.  In such case the minimal
5319          symbol names do not match the full symbol names but for template
5320          functions there is never a need to look up their definition from their
5321          declaration so the only disadvantage remains the minimal symbol
5322          variant `long name(params)' does not have the proper inferior type.
5323          */
5324
5325       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5326                                             | (cu->language == language_java
5327                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5328                                                : DMGL_RET_DROP)));
5329       if (demangled)
5330         {
5331           make_cleanup (xfree, demangled);
5332           canon = demangled;
5333         }
5334       else
5335         {
5336           canon = mangled;
5337           need_copy = 0;
5338         }
5339     }
5340
5341   if (canon == NULL || check_physname)
5342     {
5343       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5344
5345       if (canon != NULL && strcmp (physname, canon) != 0)
5346         {
5347           /* It may not mean a bug in GDB.  The compiler could also
5348              compute DW_AT_linkage_name incorrectly.  But in such case
5349              GDB would need to be bug-to-bug compatible.  */
5350
5351           complaint (&symfile_complaints,
5352                      _("Computed physname <%s> does not match demangled <%s> "
5353                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5354                      physname, canon, mangled, die->offset, cu->objfile->name);
5355
5356           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5357              is available here - over computed PHYSNAME.  It is safer
5358              against both buggy GDB and buggy compilers.  */
5359
5360           retval = canon;
5361         }
5362       else
5363         {
5364           retval = physname;
5365           need_copy = 0;
5366         }
5367     }
5368   else
5369     retval = canon;
5370
5371   if (need_copy)
5372     retval = obsavestring (retval, strlen (retval),
5373                            &cu->objfile->objfile_obstack);
5374
5375   do_cleanups (back_to);
5376   return retval;
5377 }
5378
5379 /* Read the import statement specified by the given die and record it.  */
5380
5381 static void
5382 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5383 {
5384   struct attribute *import_attr;
5385   struct die_info *imported_die, *child_die;
5386   struct dwarf2_cu *imported_cu;
5387   const char *imported_name;
5388   const char *imported_name_prefix;
5389   const char *canonical_name;
5390   const char *import_alias;
5391   const char *imported_declaration = NULL;
5392   const char *import_prefix;
5393   VEC (const_char_ptr) *excludes = NULL;
5394   struct cleanup *cleanups;
5395
5396   char *temp;
5397
5398   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5399   if (import_attr == NULL)
5400     {
5401       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5402                  dwarf_tag_name (die->tag));
5403       return;
5404     }
5405
5406   imported_cu = cu;
5407   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5408   imported_name = dwarf2_name (imported_die, imported_cu);
5409   if (imported_name == NULL)
5410     {
5411       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5412
5413         The import in the following code:
5414         namespace A
5415           {
5416             typedef int B;
5417           }
5418
5419         int main ()
5420           {
5421             using A::B;
5422             B b;
5423             return b;
5424           }
5425
5426         ...
5427          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5428             <52>   DW_AT_decl_file   : 1
5429             <53>   DW_AT_decl_line   : 6
5430             <54>   DW_AT_import      : <0x75>
5431          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5432             <59>   DW_AT_name        : B
5433             <5b>   DW_AT_decl_file   : 1
5434             <5c>   DW_AT_decl_line   : 2
5435             <5d>   DW_AT_type        : <0x6e>
5436         ...
5437          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5438             <76>   DW_AT_byte_size   : 4
5439             <77>   DW_AT_encoding    : 5        (signed)
5440
5441         imports the wrong die ( 0x75 instead of 0x58 ).
5442         This case will be ignored until the gcc bug is fixed.  */
5443       return;
5444     }
5445
5446   /* Figure out the local name after import.  */
5447   import_alias = dwarf2_name (die, cu);
5448
5449   /* Figure out where the statement is being imported to.  */
5450   import_prefix = determine_prefix (die, cu);
5451
5452   /* Figure out what the scope of the imported die is and prepend it
5453      to the name of the imported die.  */
5454   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5455
5456   if (imported_die->tag != DW_TAG_namespace
5457       && imported_die->tag != DW_TAG_module)
5458     {
5459       imported_declaration = imported_name;
5460       canonical_name = imported_name_prefix;
5461     }
5462   else if (strlen (imported_name_prefix) > 0)
5463     {
5464       temp = alloca (strlen (imported_name_prefix)
5465                      + 2 + strlen (imported_name) + 1);
5466       strcpy (temp, imported_name_prefix);
5467       strcat (temp, "::");
5468       strcat (temp, imported_name);
5469       canonical_name = temp;
5470     }
5471   else
5472     canonical_name = imported_name;
5473
5474   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5475
5476   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5477     for (child_die = die->child; child_die && child_die->tag;
5478          child_die = sibling_die (child_die))
5479       {
5480         /* DWARF-4: A Fortran use statement with a “rename list” may be
5481            represented by an imported module entry with an import attribute
5482            referring to the module and owned entries corresponding to those
5483            entities that are renamed as part of being imported.  */
5484
5485         if (child_die->tag != DW_TAG_imported_declaration)
5486           {
5487             complaint (&symfile_complaints,
5488                        _("child DW_TAG_imported_declaration expected "
5489                          "- DIE at 0x%x [in module %s]"),
5490                        child_die->offset, cu->objfile->name);
5491             continue;
5492           }
5493
5494         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5495         if (import_attr == NULL)
5496           {
5497             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5498                        dwarf_tag_name (child_die->tag));
5499             continue;
5500           }
5501
5502         imported_cu = cu;
5503         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5504                                               &imported_cu);
5505         imported_name = dwarf2_name (imported_die, imported_cu);
5506         if (imported_name == NULL)
5507           {
5508             complaint (&symfile_complaints,
5509                        _("child DW_TAG_imported_declaration has unknown "
5510                          "imported name - DIE at 0x%x [in module %s]"),
5511                        child_die->offset, cu->objfile->name);
5512             continue;
5513           }
5514
5515         VEC_safe_push (const_char_ptr, excludes, imported_name);
5516
5517         process_die (child_die, cu);
5518       }
5519
5520   cp_add_using_directive (import_prefix,
5521                           canonical_name,
5522                           import_alias,
5523                           imported_declaration,
5524                           excludes,
5525                           &cu->objfile->objfile_obstack);
5526
5527   do_cleanups (cleanups);
5528 }
5529
5530 static void
5531 initialize_cu_func_list (struct dwarf2_cu *cu)
5532 {
5533   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5534 }
5535
5536 /* Cleanup function for read_file_scope.  */
5537
5538 static void
5539 free_cu_line_header (void *arg)
5540 {
5541   struct dwarf2_cu *cu = arg;
5542
5543   free_line_header (cu->line_header);
5544   cu->line_header = NULL;
5545 }
5546
5547 static void
5548 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5549                          char **name, char **comp_dir)
5550 {
5551   struct attribute *attr;
5552
5553   *name = NULL;
5554   *comp_dir = NULL;
5555
5556   /* Find the filename.  Do not use dwarf2_name here, since the filename
5557      is not a source language identifier.  */
5558   attr = dwarf2_attr (die, DW_AT_name, cu);
5559   if (attr)
5560     {
5561       *name = DW_STRING (attr);
5562     }
5563
5564   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5565   if (attr)
5566     *comp_dir = DW_STRING (attr);
5567   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5568     {
5569       *comp_dir = ldirname (*name);
5570       if (*comp_dir != NULL)
5571         make_cleanup (xfree, *comp_dir);
5572     }
5573   if (*comp_dir != NULL)
5574     {
5575       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5576          directory, get rid of it.  */
5577       char *cp = strchr (*comp_dir, ':');
5578
5579       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5580         *comp_dir = cp + 1;
5581     }
5582
5583   if (*name == NULL)
5584     *name = "<unknown>";
5585 }
5586
5587 /* Handle DW_AT_stmt_list for a compilation unit.  */
5588
5589 static void
5590 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5591                         const char *comp_dir)
5592 {
5593   struct attribute *attr;
5594   struct objfile *objfile = cu->objfile;
5595   bfd *abfd = objfile->obfd;
5596
5597   /* Decode line number information if present.  We do this before
5598      processing child DIEs, so that the line header table is available
5599      for DW_AT_decl_file.  */
5600   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5601   if (attr)
5602     {
5603       unsigned int line_offset = DW_UNSND (attr);
5604       struct line_header *line_header
5605         = dwarf_decode_line_header (line_offset, abfd, cu);
5606
5607       if (line_header)
5608         {
5609           cu->line_header = line_header;
5610           make_cleanup (free_cu_line_header, cu);
5611           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5612         }
5613     }
5614 }
5615
5616 /* Process DW_TAG_compile_unit.  */
5617
5618 static void
5619 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5620 {
5621   struct objfile *objfile = cu->objfile;
5622   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5623   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5624   CORE_ADDR highpc = ((CORE_ADDR) 0);
5625   struct attribute *attr;
5626   char *name = NULL;
5627   char *comp_dir = NULL;
5628   struct die_info *child_die;
5629   bfd *abfd = objfile->obfd;
5630   CORE_ADDR baseaddr;
5631
5632   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5633
5634   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5635
5636   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5637      from finish_block.  */
5638   if (lowpc == ((CORE_ADDR) -1))
5639     lowpc = highpc;
5640   lowpc += baseaddr;
5641   highpc += baseaddr;
5642
5643   find_file_and_directory (die, cu, &name, &comp_dir);
5644
5645   attr = dwarf2_attr (die, DW_AT_language, cu);
5646   if (attr)
5647     {
5648       set_cu_language (DW_UNSND (attr), cu);
5649     }
5650
5651   attr = dwarf2_attr (die, DW_AT_producer, cu);
5652   if (attr)
5653     cu->producer = DW_STRING (attr);
5654
5655   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5656      standardised yet.  As a workaround for the language detection we fall
5657      back to the DW_AT_producer string.  */
5658   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5659     cu->language = language_opencl;
5660
5661   /* We assume that we're processing GCC output.  */
5662   processing_gcc_compilation = 2;
5663
5664   processing_has_namespace_info = 0;
5665
5666   start_symtab (name, comp_dir, lowpc);
5667   record_debugformat ("DWARF 2");
5668   record_producer (cu->producer);
5669
5670   initialize_cu_func_list (cu);
5671
5672   handle_DW_AT_stmt_list (die, cu, comp_dir);
5673
5674   /* Process all dies in compilation unit.  */
5675   if (die->child != NULL)
5676     {
5677       child_die = die->child;
5678       while (child_die && child_die->tag)
5679         {
5680           process_die (child_die, cu);
5681           child_die = sibling_die (child_die);
5682         }
5683     }
5684
5685   /* Decode macro information, if present.  Dwarf 2 macro information
5686      refers to information in the line number info statement program
5687      header, so we can only read it if we've read the header
5688      successfully.  */
5689   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5690   if (attr && cu->line_header)
5691     {
5692       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5693         complaint (&symfile_complaints,
5694                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5695
5696       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5697                            comp_dir, abfd, cu,
5698                            &dwarf2_per_objfile->macro, 1);
5699     }
5700   else
5701     {
5702       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5703       if (attr && cu->line_header)
5704         {
5705           unsigned int macro_offset = DW_UNSND (attr);
5706
5707           dwarf_decode_macros (cu->line_header, macro_offset,
5708                                comp_dir, abfd, cu,
5709                                &dwarf2_per_objfile->macinfo, 0);
5710         }
5711     }
5712   do_cleanups (back_to);
5713 }
5714
5715 /* Process DW_TAG_type_unit.
5716    For TUs we want to skip the first top level sibling if it's not the
5717    actual type being defined by this TU.  In this case the first top
5718    level sibling is there to provide context only.  */
5719
5720 static void
5721 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5722 {
5723   struct objfile *objfile = cu->objfile;
5724   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5725   CORE_ADDR lowpc;
5726   struct attribute *attr;
5727   char *name = NULL;
5728   char *comp_dir = NULL;
5729   struct die_info *child_die;
5730   bfd *abfd = objfile->obfd;
5731
5732   /* start_symtab needs a low pc, but we don't really have one.
5733      Do what read_file_scope would do in the absence of such info.  */
5734   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5735
5736   /* Find the filename.  Do not use dwarf2_name here, since the filename
5737      is not a source language identifier.  */
5738   attr = dwarf2_attr (die, DW_AT_name, cu);
5739   if (attr)
5740     name = DW_STRING (attr);
5741
5742   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5743   if (attr)
5744     comp_dir = DW_STRING (attr);
5745   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5746     {
5747       comp_dir = ldirname (name);
5748       if (comp_dir != NULL)
5749         make_cleanup (xfree, comp_dir);
5750     }
5751
5752   if (name == NULL)
5753     name = "<unknown>";
5754
5755   attr = dwarf2_attr (die, DW_AT_language, cu);
5756   if (attr)
5757     set_cu_language (DW_UNSND (attr), cu);
5758
5759   /* This isn't technically needed today.  It is done for symmetry
5760      with read_file_scope.  */
5761   attr = dwarf2_attr (die, DW_AT_producer, cu);
5762   if (attr)
5763     cu->producer = DW_STRING (attr);
5764
5765   /* We assume that we're processing GCC output.  */
5766   processing_gcc_compilation = 2;
5767
5768   processing_has_namespace_info = 0;
5769
5770   start_symtab (name, comp_dir, lowpc);
5771   record_debugformat ("DWARF 2");
5772   record_producer (cu->producer);
5773
5774   handle_DW_AT_stmt_list (die, cu, comp_dir);
5775
5776   /* Process the dies in the type unit.  */
5777   if (die->child == NULL)
5778     {
5779       dump_die_for_error (die);
5780       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5781              bfd_get_filename (abfd));
5782     }
5783
5784   child_die = die->child;
5785
5786   while (child_die && child_die->tag)
5787     {
5788       process_die (child_die, cu);
5789
5790       child_die = sibling_die (child_die);
5791     }
5792
5793   do_cleanups (back_to);
5794 }
5795
5796 static void
5797 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5798                      struct dwarf2_cu *cu)
5799 {
5800   struct function_range *thisfn;
5801
5802   thisfn = (struct function_range *)
5803     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5804   thisfn->name = name;
5805   thisfn->lowpc = lowpc;
5806   thisfn->highpc = highpc;
5807   thisfn->seen_line = 0;
5808   thisfn->next = NULL;
5809
5810   if (cu->last_fn == NULL)
5811       cu->first_fn = thisfn;
5812   else
5813       cu->last_fn->next = thisfn;
5814
5815   cu->last_fn = thisfn;
5816 }
5817
5818 /* qsort helper for inherit_abstract_dies.  */
5819
5820 static int
5821 unsigned_int_compar (const void *ap, const void *bp)
5822 {
5823   unsigned int a = *(unsigned int *) ap;
5824   unsigned int b = *(unsigned int *) bp;
5825
5826   return (a > b) - (b > a);
5827 }
5828
5829 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5830    Inherit only the children of the DW_AT_abstract_origin DIE not being
5831    already referenced by DW_AT_abstract_origin from the children of the
5832    current DIE.  */
5833
5834 static void
5835 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5836 {
5837   struct die_info *child_die;
5838   unsigned die_children_count;
5839   /* CU offsets which were referenced by children of the current DIE.  */
5840   unsigned *offsets;
5841   unsigned *offsets_end, *offsetp;
5842   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5843   struct die_info *origin_die;
5844   /* Iterator of the ORIGIN_DIE children.  */
5845   struct die_info *origin_child_die;
5846   struct cleanup *cleanups;
5847   struct attribute *attr;
5848   struct dwarf2_cu *origin_cu;
5849   struct pending **origin_previous_list_in_scope;
5850
5851   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5852   if (!attr)
5853     return;
5854
5855   /* Note that following die references may follow to a die in a
5856      different cu.  */
5857
5858   origin_cu = cu;
5859   origin_die = follow_die_ref (die, attr, &origin_cu);
5860
5861   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5862      symbols in.  */
5863   origin_previous_list_in_scope = origin_cu->list_in_scope;
5864   origin_cu->list_in_scope = cu->list_in_scope;
5865
5866   if (die->tag != origin_die->tag
5867       && !(die->tag == DW_TAG_inlined_subroutine
5868            && origin_die->tag == DW_TAG_subprogram))
5869     complaint (&symfile_complaints,
5870                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5871                die->offset, origin_die->offset);
5872
5873   child_die = die->child;
5874   die_children_count = 0;
5875   while (child_die && child_die->tag)
5876     {
5877       child_die = sibling_die (child_die);
5878       die_children_count++;
5879     }
5880   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5881   cleanups = make_cleanup (xfree, offsets);
5882
5883   offsets_end = offsets;
5884   child_die = die->child;
5885   while (child_die && child_die->tag)
5886     {
5887       /* For each CHILD_DIE, find the corresponding child of
5888          ORIGIN_DIE.  If there is more than one layer of
5889          DW_AT_abstract_origin, follow them all; there shouldn't be,
5890          but GCC versions at least through 4.4 generate this (GCC PR
5891          40573).  */
5892       struct die_info *child_origin_die = child_die;
5893       struct dwarf2_cu *child_origin_cu = cu;
5894
5895       while (1)
5896         {
5897           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5898                               child_origin_cu);
5899           if (attr == NULL)
5900             break;
5901           child_origin_die = follow_die_ref (child_origin_die, attr,
5902                                              &child_origin_cu);
5903         }
5904
5905       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5906          counterpart may exist.  */
5907       if (child_origin_die != child_die)
5908         {
5909           if (child_die->tag != child_origin_die->tag
5910               && !(child_die->tag == DW_TAG_inlined_subroutine
5911                    && child_origin_die->tag == DW_TAG_subprogram))
5912             complaint (&symfile_complaints,
5913                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5914                          "different tags"), child_die->offset,
5915                        child_origin_die->offset);
5916           if (child_origin_die->parent != origin_die)
5917             complaint (&symfile_complaints,
5918                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5919                          "different parents"), child_die->offset,
5920                        child_origin_die->offset);
5921           else
5922             *offsets_end++ = child_origin_die->offset;
5923         }
5924       child_die = sibling_die (child_die);
5925     }
5926   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5927          unsigned_int_compar);
5928   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5929     if (offsetp[-1] == *offsetp)
5930       complaint (&symfile_complaints,
5931                  _("Multiple children of DIE 0x%x refer "
5932                    "to DIE 0x%x as their abstract origin"),
5933                  die->offset, *offsetp);
5934
5935   offsetp = offsets;
5936   origin_child_die = origin_die->child;
5937   while (origin_child_die && origin_child_die->tag)
5938     {
5939       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5940       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5941         offsetp++;
5942       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5943         {
5944           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5945           process_die (origin_child_die, origin_cu);
5946         }
5947       origin_child_die = sibling_die (origin_child_die);
5948     }
5949   origin_cu->list_in_scope = origin_previous_list_in_scope;
5950
5951   do_cleanups (cleanups);
5952 }
5953
5954 static void
5955 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5956 {
5957   struct objfile *objfile = cu->objfile;
5958   struct context_stack *new;
5959   CORE_ADDR lowpc;
5960   CORE_ADDR highpc;
5961   struct die_info *child_die;
5962   struct attribute *attr, *call_line, *call_file;
5963   char *name;
5964   CORE_ADDR baseaddr;
5965   struct block *block;
5966   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5967   VEC (symbolp) *template_args = NULL;
5968   struct template_symbol *templ_func = NULL;
5969
5970   if (inlined_func)
5971     {
5972       /* If we do not have call site information, we can't show the
5973          caller of this inlined function.  That's too confusing, so
5974          only use the scope for local variables.  */
5975       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5976       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5977       if (call_line == NULL || call_file == NULL)
5978         {
5979           read_lexical_block_scope (die, cu);
5980           return;
5981         }
5982     }
5983
5984   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5985
5986   name = dwarf2_name (die, cu);
5987
5988   /* Ignore functions with missing or empty names.  These are actually
5989      illegal according to the DWARF standard.  */
5990   if (name == NULL)
5991     {
5992       complaint (&symfile_complaints,
5993                  _("missing name for subprogram DIE at %d"), die->offset);
5994       return;
5995     }
5996
5997   /* Ignore functions with missing or invalid low and high pc attributes.  */
5998   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5999     {
6000       attr = dwarf2_attr (die, DW_AT_external, cu);
6001       if (!attr || !DW_UNSND (attr))
6002         complaint (&symfile_complaints,
6003                    _("cannot get low and high bounds "
6004                      "for subprogram DIE at %d"),
6005                    die->offset);
6006       return;
6007     }
6008
6009   lowpc += baseaddr;
6010   highpc += baseaddr;
6011
6012   /* Record the function range for dwarf_decode_lines.  */
6013   add_to_cu_func_list (name, lowpc, highpc, cu);
6014
6015   /* If we have any template arguments, then we must allocate a
6016      different sort of symbol.  */
6017   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6018     {
6019       if (child_die->tag == DW_TAG_template_type_param
6020           || child_die->tag == DW_TAG_template_value_param)
6021         {
6022           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6023                                        struct template_symbol);
6024           templ_func->base.is_cplus_template_function = 1;
6025           break;
6026         }
6027     }
6028
6029   new = push_context (0, lowpc);
6030   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6031                                (struct symbol *) templ_func);
6032
6033   /* If there is a location expression for DW_AT_frame_base, record
6034      it.  */
6035   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6036   if (attr)
6037     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6038        expression is being recorded directly in the function's symbol
6039        and not in a separate frame-base object.  I guess this hack is
6040        to avoid adding some sort of frame-base adjunct/annex to the
6041        function's symbol :-(.  The problem with doing this is that it
6042        results in a function symbol with a location expression that
6043        has nothing to do with the location of the function, ouch!  The
6044        relationship should be: a function's symbol has-a frame base; a
6045        frame-base has-a location expression.  */
6046     dwarf2_symbol_mark_computed (attr, new->name, cu);
6047
6048   cu->list_in_scope = &local_symbols;
6049
6050   if (die->child != NULL)
6051     {
6052       child_die = die->child;
6053       while (child_die && child_die->tag)
6054         {
6055           if (child_die->tag == DW_TAG_template_type_param
6056               || child_die->tag == DW_TAG_template_value_param)
6057             {
6058               struct symbol *arg = new_symbol (child_die, NULL, cu);
6059
6060               if (arg != NULL)
6061                 VEC_safe_push (symbolp, template_args, arg);
6062             }
6063           else
6064             process_die (child_die, cu);
6065           child_die = sibling_die (child_die);
6066         }
6067     }
6068
6069   inherit_abstract_dies (die, cu);
6070
6071   /* If we have a DW_AT_specification, we might need to import using
6072      directives from the context of the specification DIE.  See the
6073      comment in determine_prefix.  */
6074   if (cu->language == language_cplus
6075       && dwarf2_attr (die, DW_AT_specification, cu))
6076     {
6077       struct dwarf2_cu *spec_cu = cu;
6078       struct die_info *spec_die = die_specification (die, &spec_cu);
6079
6080       while (spec_die)
6081         {
6082           child_die = spec_die->child;
6083           while (child_die && child_die->tag)
6084             {
6085               if (child_die->tag == DW_TAG_imported_module)
6086                 process_die (child_die, spec_cu);
6087               child_die = sibling_die (child_die);
6088             }
6089
6090           /* In some cases, GCC generates specification DIEs that
6091              themselves contain DW_AT_specification attributes.  */
6092           spec_die = die_specification (spec_die, &spec_cu);
6093         }
6094     }
6095
6096   new = pop_context ();
6097   /* Make a block for the local symbols within.  */
6098   block = finish_block (new->name, &local_symbols, new->old_blocks,
6099                         lowpc, highpc, objfile);
6100
6101   /* For C++, set the block's scope.  */
6102   if (cu->language == language_cplus || cu->language == language_fortran)
6103     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6104                         determine_prefix (die, cu),
6105                         processing_has_namespace_info);
6106
6107   /* If we have address ranges, record them.  */
6108   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6109
6110   /* Attach template arguments to function.  */
6111   if (! VEC_empty (symbolp, template_args))
6112     {
6113       gdb_assert (templ_func != NULL);
6114
6115       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6116       templ_func->template_arguments
6117         = obstack_alloc (&objfile->objfile_obstack,
6118                          (templ_func->n_template_arguments
6119                           * sizeof (struct symbol *)));
6120       memcpy (templ_func->template_arguments,
6121               VEC_address (symbolp, template_args),
6122               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6123       VEC_free (symbolp, template_args);
6124     }
6125
6126   /* In C++, we can have functions nested inside functions (e.g., when
6127      a function declares a class that has methods).  This means that
6128      when we finish processing a function scope, we may need to go
6129      back to building a containing block's symbol lists.  */
6130   local_symbols = new->locals;
6131   param_symbols = new->params;
6132   using_directives = new->using_directives;
6133
6134   /* If we've finished processing a top-level function, subsequent
6135      symbols go in the file symbol list.  */
6136   if (outermost_context_p ())
6137     cu->list_in_scope = &file_symbols;
6138 }
6139
6140 /* Process all the DIES contained within a lexical block scope.  Start
6141    a new scope, process the dies, and then close the scope.  */
6142
6143 static void
6144 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6145 {
6146   struct objfile *objfile = cu->objfile;
6147   struct context_stack *new;
6148   CORE_ADDR lowpc, highpc;
6149   struct die_info *child_die;
6150   CORE_ADDR baseaddr;
6151
6152   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6153
6154   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6155   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6156      as multiple lexical blocks?  Handling children in a sane way would
6157      be nasty.  Might be easier to properly extend generic blocks to
6158      describe ranges.  */
6159   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6160     return;
6161   lowpc += baseaddr;
6162   highpc += baseaddr;
6163
6164   push_context (0, lowpc);
6165   if (die->child != NULL)
6166     {
6167       child_die = die->child;
6168       while (child_die && child_die->tag)
6169         {
6170           process_die (child_die, cu);
6171           child_die = sibling_die (child_die);
6172         }
6173     }
6174   new = pop_context ();
6175
6176   if (local_symbols != NULL || using_directives != NULL)
6177     {
6178       struct block *block
6179         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6180                         highpc, objfile);
6181
6182       /* Note that recording ranges after traversing children, as we
6183          do here, means that recording a parent's ranges entails
6184          walking across all its children's ranges as they appear in
6185          the address map, which is quadratic behavior.
6186
6187          It would be nicer to record the parent's ranges before
6188          traversing its children, simply overriding whatever you find
6189          there.  But since we don't even decide whether to create a
6190          block until after we've traversed its children, that's hard
6191          to do.  */
6192       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6193     }
6194   local_symbols = new->locals;
6195   using_directives = new->using_directives;
6196 }
6197
6198 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6199
6200 static void
6201 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6202 {
6203   struct objfile *objfile = cu->objfile;
6204   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6205   CORE_ADDR pc, baseaddr;
6206   struct attribute *attr;
6207   struct call_site *call_site, call_site_local;
6208   void **slot;
6209   int nparams;
6210   struct die_info *child_die;
6211
6212   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6213
6214   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6215   if (!attr)
6216     {
6217       complaint (&symfile_complaints,
6218                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6219                    "DIE 0x%x [in module %s]"),
6220                  die->offset, cu->objfile->name);
6221       return;
6222     }
6223   pc = DW_ADDR (attr) + baseaddr;
6224
6225   if (cu->call_site_htab == NULL)
6226     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6227                                                NULL, &objfile->objfile_obstack,
6228                                                hashtab_obstack_allocate, NULL);
6229   call_site_local.pc = pc;
6230   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6231   if (*slot != NULL)
6232     {
6233       complaint (&symfile_complaints,
6234                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6235                    "DIE 0x%x [in module %s]"),
6236                  paddress (gdbarch, pc), die->offset, cu->objfile->name);
6237       return;
6238     }
6239
6240   /* Count parameters at the caller.  */
6241
6242   nparams = 0;
6243   for (child_die = die->child; child_die && child_die->tag;
6244        child_die = sibling_die (child_die))
6245     {
6246       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6247         {
6248           complaint (&symfile_complaints,
6249                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6250                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6251                      child_die->tag, child_die->offset, cu->objfile->name);
6252           continue;
6253         }
6254
6255       nparams++;
6256     }
6257
6258   call_site = obstack_alloc (&objfile->objfile_obstack,
6259                              (sizeof (*call_site)
6260                               + (sizeof (*call_site->parameter)
6261                                  * (nparams - 1))));
6262   *slot = call_site;
6263   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6264   call_site->pc = pc;
6265
6266   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6267     {
6268       struct die_info *func_die;
6269
6270       /* Skip also over DW_TAG_inlined_subroutine.  */
6271       for (func_die = die->parent;
6272            func_die && func_die->tag != DW_TAG_subprogram
6273            && func_die->tag != DW_TAG_subroutine_type;
6274            func_die = func_die->parent);
6275
6276       /* DW_AT_GNU_all_call_sites is a superset
6277          of DW_AT_GNU_all_tail_call_sites.  */
6278       if (func_die
6279           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6280           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6281         {
6282           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6283              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6284              both the initial caller containing the real return address PC and
6285              the final callee containing the current PC of a chain of tail
6286              calls do not need to have the tail call list complete.  But any
6287              function candidate for a virtual tail call frame searched via
6288              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6289              determined unambiguously.  */
6290         }
6291       else
6292         {
6293           struct type *func_type = NULL;
6294
6295           if (func_die)
6296             func_type = get_die_type (func_die, cu);
6297           if (func_type != NULL)
6298             {
6299               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6300
6301               /* Enlist this call site to the function.  */
6302               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6303               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6304             }
6305           else
6306             complaint (&symfile_complaints,
6307                        _("Cannot find function owning DW_TAG_GNU_call_site "
6308                          "DIE 0x%x [in module %s]"),
6309                        die->offset, cu->objfile->name);
6310         }
6311     }
6312
6313   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6314   if (attr == NULL)
6315     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6316   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6317   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6318     /* Keep NULL DWARF_BLOCK.  */;
6319   else if (attr_form_is_block (attr))
6320     {
6321       struct dwarf2_locexpr_baton *dlbaton;
6322
6323       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6324       dlbaton->data = DW_BLOCK (attr)->data;
6325       dlbaton->size = DW_BLOCK (attr)->size;
6326       dlbaton->per_cu = cu->per_cu;
6327
6328       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6329     }
6330   else if (is_ref_attr (attr))
6331     {
6332       struct objfile *objfile = cu->objfile;
6333       struct dwarf2_cu *target_cu = cu;
6334       struct die_info *target_die;
6335
6336       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6337       gdb_assert (target_cu->objfile == objfile);
6338       if (die_is_declaration (target_die, target_cu))
6339         {
6340           const char *target_physname;
6341
6342           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6343           if (target_physname == NULL)
6344             complaint (&symfile_complaints,
6345                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6346                          "physname, for referencing DIE 0x%x [in module %s]"),
6347                        die->offset, cu->objfile->name);
6348           else
6349             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6350         }
6351       else
6352         {
6353           CORE_ADDR lowpc;
6354
6355           /* DW_AT_entry_pc should be preferred.  */
6356           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6357             complaint (&symfile_complaints,
6358                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6359                          "low pc, for referencing DIE 0x%x [in module %s]"),
6360                        die->offset, cu->objfile->name);
6361           else
6362             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6363         }
6364     }
6365   else
6366     complaint (&symfile_complaints,
6367                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6368                  "block nor reference, for DIE 0x%x [in module %s]"),
6369                die->offset, cu->objfile->name);
6370
6371   call_site->per_cu = cu->per_cu;
6372
6373   for (child_die = die->child;
6374        child_die && child_die->tag;
6375        child_die = sibling_die (child_die))
6376     {
6377       struct dwarf2_locexpr_baton *dlbaton;
6378       struct call_site_parameter *parameter;
6379
6380       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6381         {
6382           /* Already printed the complaint above.  */
6383           continue;
6384         }
6385
6386       gdb_assert (call_site->parameter_count < nparams);
6387       parameter = &call_site->parameter[call_site->parameter_count];
6388
6389       /* DW_AT_location specifies the register number.  Value of the data
6390          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6391
6392       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6393       if (!attr || !attr_form_is_block (attr))
6394         {
6395           complaint (&symfile_complaints,
6396                      _("No DW_FORM_block* DW_AT_location for "
6397                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6398                      child_die->offset, cu->objfile->name);
6399           continue;
6400         }
6401       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6402                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6403       if (parameter->dwarf_reg == -1
6404           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6405                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6406                                         &parameter->fb_offset))
6407         {
6408           complaint (&symfile_complaints,
6409                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6410                        "for DW_FORM_block* DW_AT_location for "
6411                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6412                      child_die->offset, cu->objfile->name);
6413           continue;
6414         }
6415
6416       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6417       if (!attr_form_is_block (attr))
6418         {
6419           complaint (&symfile_complaints,
6420                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6421                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6422                      child_die->offset, cu->objfile->name);
6423           continue;
6424         }
6425       parameter->value = DW_BLOCK (attr)->data;
6426       parameter->value_size = DW_BLOCK (attr)->size;
6427
6428       /* Parameters are not pre-cleared by memset above.  */
6429       parameter->data_value = NULL;
6430       parameter->data_value_size = 0;
6431       call_site->parameter_count++;
6432
6433       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6434       if (attr)
6435         {
6436           if (!attr_form_is_block (attr))
6437             complaint (&symfile_complaints,
6438                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6439                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6440                        child_die->offset, cu->objfile->name);
6441           else
6442             {
6443               parameter->data_value = DW_BLOCK (attr)->data;
6444               parameter->data_value_size = DW_BLOCK (attr)->size;
6445             }
6446         }
6447     }
6448 }
6449
6450 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6451    Return 1 if the attributes are present and valid, otherwise, return 0.
6452    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6453
6454 static int
6455 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6456                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6457                     struct partial_symtab *ranges_pst)
6458 {
6459   struct objfile *objfile = cu->objfile;
6460   struct comp_unit_head *cu_header = &cu->header;
6461   bfd *obfd = objfile->obfd;
6462   unsigned int addr_size = cu_header->addr_size;
6463   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6464   /* Base address selection entry.  */
6465   CORE_ADDR base;
6466   int found_base;
6467   unsigned int dummy;
6468   gdb_byte *buffer;
6469   CORE_ADDR marker;
6470   int low_set;
6471   CORE_ADDR low = 0;
6472   CORE_ADDR high = 0;
6473   CORE_ADDR baseaddr;
6474
6475   found_base = cu->base_known;
6476   base = cu->base_address;
6477
6478   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6479   if (offset >= dwarf2_per_objfile->ranges.size)
6480     {
6481       complaint (&symfile_complaints,
6482                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6483                  offset);
6484       return 0;
6485     }
6486   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6487
6488   /* Read in the largest possible address.  */
6489   marker = read_address (obfd, buffer, cu, &dummy);
6490   if ((marker & mask) == mask)
6491     {
6492       /* If we found the largest possible address, then
6493          read the base address.  */
6494       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6495       buffer += 2 * addr_size;
6496       offset += 2 * addr_size;
6497       found_base = 1;
6498     }
6499
6500   low_set = 0;
6501
6502   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6503
6504   while (1)
6505     {
6506       CORE_ADDR range_beginning, range_end;
6507
6508       range_beginning = read_address (obfd, buffer, cu, &dummy);
6509       buffer += addr_size;
6510       range_end = read_address (obfd, buffer, cu, &dummy);
6511       buffer += addr_size;
6512       offset += 2 * addr_size;
6513
6514       /* An end of list marker is a pair of zero addresses.  */
6515       if (range_beginning == 0 && range_end == 0)
6516         /* Found the end of list entry.  */
6517         break;
6518
6519       /* Each base address selection entry is a pair of 2 values.
6520          The first is the largest possible address, the second is
6521          the base address.  Check for a base address here.  */
6522       if ((range_beginning & mask) == mask)
6523         {
6524           /* If we found the largest possible address, then
6525              read the base address.  */
6526           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6527           found_base = 1;
6528           continue;
6529         }
6530
6531       if (!found_base)
6532         {
6533           /* We have no valid base address for the ranges
6534              data.  */
6535           complaint (&symfile_complaints,
6536                      _("Invalid .debug_ranges data (no base address)"));
6537           return 0;
6538         }
6539
6540       if (range_beginning > range_end)
6541         {
6542           /* Inverted range entries are invalid.  */
6543           complaint (&symfile_complaints,
6544                      _("Invalid .debug_ranges data (inverted range)"));
6545           return 0;
6546         }
6547
6548       /* Empty range entries have no effect.  */
6549       if (range_beginning == range_end)
6550         continue;
6551
6552       range_beginning += base;
6553       range_end += base;
6554
6555       if (ranges_pst != NULL)
6556         addrmap_set_empty (objfile->psymtabs_addrmap,
6557                            range_beginning + baseaddr,
6558                            range_end - 1 + baseaddr,
6559                            ranges_pst);
6560
6561       /* FIXME: This is recording everything as a low-high
6562          segment of consecutive addresses.  We should have a
6563          data structure for discontiguous block ranges
6564          instead.  */
6565       if (! low_set)
6566         {
6567           low = range_beginning;
6568           high = range_end;
6569           low_set = 1;
6570         }
6571       else
6572         {
6573           if (range_beginning < low)
6574             low = range_beginning;
6575           if (range_end > high)
6576             high = range_end;
6577         }
6578     }
6579
6580   if (! low_set)
6581     /* If the first entry is an end-of-list marker, the range
6582        describes an empty scope, i.e. no instructions.  */
6583     return 0;
6584
6585   if (low_return)
6586     *low_return = low;
6587   if (high_return)
6588     *high_return = high;
6589   return 1;
6590 }
6591
6592 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6593    are present and valid, otherwise, return 0.  Return -1 if the range is
6594    discontinuous, i.e. derived from DW_AT_ranges information.  */
6595 static int
6596 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6597                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6598                       struct partial_symtab *pst)
6599 {
6600   struct attribute *attr;
6601   CORE_ADDR low = 0;
6602   CORE_ADDR high = 0;
6603   int ret = 0;
6604
6605   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6606   if (attr)
6607     {
6608       high = DW_ADDR (attr);
6609       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6610       if (attr)
6611         low = DW_ADDR (attr);
6612       else
6613         /* Found high w/o low attribute.  */
6614         return 0;
6615
6616       /* Found consecutive range of addresses.  */
6617       ret = 1;
6618     }
6619   else
6620     {
6621       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6622       if (attr != NULL)
6623         {
6624           /* Value of the DW_AT_ranges attribute is the offset in the
6625              .debug_ranges section.  */
6626           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6627             return 0;
6628           /* Found discontinuous range of addresses.  */
6629           ret = -1;
6630         }
6631     }
6632
6633   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6634   if (high <= low)
6635     return 0;
6636
6637   /* When using the GNU linker, .gnu.linkonce. sections are used to
6638      eliminate duplicate copies of functions and vtables and such.
6639      The linker will arbitrarily choose one and discard the others.
6640      The AT_*_pc values for such functions refer to local labels in
6641      these sections.  If the section from that file was discarded, the
6642      labels are not in the output, so the relocs get a value of 0.
6643      If this is a discarded function, mark the pc bounds as invalid,
6644      so that GDB will ignore it.  */
6645   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6646     return 0;
6647
6648   *lowpc = low;
6649   if (highpc)
6650     *highpc = high;
6651   return ret;
6652 }
6653
6654 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6655    its low and high PC addresses.  Do nothing if these addresses could not
6656    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6657    and HIGHPC to the high address if greater than HIGHPC.  */
6658
6659 static void
6660 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6661                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6662                                  struct dwarf2_cu *cu)
6663 {
6664   CORE_ADDR low, high;
6665   struct die_info *child = die->child;
6666
6667   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6668     {
6669       *lowpc = min (*lowpc, low);
6670       *highpc = max (*highpc, high);
6671     }
6672
6673   /* If the language does not allow nested subprograms (either inside
6674      subprograms or lexical blocks), we're done.  */
6675   if (cu->language != language_ada)
6676     return;
6677
6678   /* Check all the children of the given DIE.  If it contains nested
6679      subprograms, then check their pc bounds.  Likewise, we need to
6680      check lexical blocks as well, as they may also contain subprogram
6681      definitions.  */
6682   while (child && child->tag)
6683     {
6684       if (child->tag == DW_TAG_subprogram
6685           || child->tag == DW_TAG_lexical_block)
6686         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6687       child = sibling_die (child);
6688     }
6689 }
6690
6691 /* Get the low and high pc's represented by the scope DIE, and store
6692    them in *LOWPC and *HIGHPC.  If the correct values can't be
6693    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6694
6695 static void
6696 get_scope_pc_bounds (struct die_info *die,
6697                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6698                      struct dwarf2_cu *cu)
6699 {
6700   CORE_ADDR best_low = (CORE_ADDR) -1;
6701   CORE_ADDR best_high = (CORE_ADDR) 0;
6702   CORE_ADDR current_low, current_high;
6703
6704   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6705     {
6706       best_low = current_low;
6707       best_high = current_high;
6708     }
6709   else
6710     {
6711       struct die_info *child = die->child;
6712
6713       while (child && child->tag)
6714         {
6715           switch (child->tag) {
6716           case DW_TAG_subprogram:
6717             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6718             break;
6719           case DW_TAG_namespace:
6720           case DW_TAG_module:
6721             /* FIXME: carlton/2004-01-16: Should we do this for
6722                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6723                that current GCC's always emit the DIEs corresponding
6724                to definitions of methods of classes as children of a
6725                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6726                the DIEs giving the declarations, which could be
6727                anywhere).  But I don't see any reason why the
6728                standards says that they have to be there.  */
6729             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6730
6731             if (current_low != ((CORE_ADDR) -1))
6732               {
6733                 best_low = min (best_low, current_low);
6734                 best_high = max (best_high, current_high);
6735               }
6736             break;
6737           default:
6738             /* Ignore.  */
6739             break;
6740           }
6741
6742           child = sibling_die (child);
6743         }
6744     }
6745
6746   *lowpc = best_low;
6747   *highpc = best_high;
6748 }
6749
6750 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6751    in DIE.  */
6752 static void
6753 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6754                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6755 {
6756   struct attribute *attr;
6757
6758   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6759   if (attr)
6760     {
6761       CORE_ADDR high = DW_ADDR (attr);
6762
6763       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6764       if (attr)
6765         {
6766           CORE_ADDR low = DW_ADDR (attr);
6767
6768           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6769         }
6770     }
6771
6772   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6773   if (attr)
6774     {
6775       bfd *obfd = cu->objfile->obfd;
6776
6777       /* The value of the DW_AT_ranges attribute is the offset of the
6778          address range list in the .debug_ranges section.  */
6779       unsigned long offset = DW_UNSND (attr);
6780       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6781
6782       /* For some target architectures, but not others, the
6783          read_address function sign-extends the addresses it returns.
6784          To recognize base address selection entries, we need a
6785          mask.  */
6786       unsigned int addr_size = cu->header.addr_size;
6787       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6788
6789       /* The base address, to which the next pair is relative.  Note
6790          that this 'base' is a DWARF concept: most entries in a range
6791          list are relative, to reduce the number of relocs against the
6792          debugging information.  This is separate from this function's
6793          'baseaddr' argument, which GDB uses to relocate debugging
6794          information from a shared library based on the address at
6795          which the library was loaded.  */
6796       CORE_ADDR base = cu->base_address;
6797       int base_known = cu->base_known;
6798
6799       gdb_assert (dwarf2_per_objfile->ranges.readin);
6800       if (offset >= dwarf2_per_objfile->ranges.size)
6801         {
6802           complaint (&symfile_complaints,
6803                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6804                      offset);
6805           return;
6806         }
6807
6808       for (;;)
6809         {
6810           unsigned int bytes_read;
6811           CORE_ADDR start, end;
6812
6813           start = read_address (obfd, buffer, cu, &bytes_read);
6814           buffer += bytes_read;
6815           end = read_address (obfd, buffer, cu, &bytes_read);
6816           buffer += bytes_read;
6817
6818           /* Did we find the end of the range list?  */
6819           if (start == 0 && end == 0)
6820             break;
6821
6822           /* Did we find a base address selection entry?  */
6823           else if ((start & base_select_mask) == base_select_mask)
6824             {
6825               base = end;
6826               base_known = 1;
6827             }
6828
6829           /* We found an ordinary address range.  */
6830           else
6831             {
6832               if (!base_known)
6833                 {
6834                   complaint (&symfile_complaints,
6835                              _("Invalid .debug_ranges data "
6836                                "(no base address)"));
6837                   return;
6838                 }
6839
6840               if (start > end)
6841                 {
6842                   /* Inverted range entries are invalid.  */
6843                   complaint (&symfile_complaints,
6844                              _("Invalid .debug_ranges data "
6845                                "(inverted range)"));
6846                   return;
6847                 }
6848
6849               /* Empty range entries have no effect.  */
6850               if (start == end)
6851                 continue;
6852
6853               record_block_range (block,
6854                                   baseaddr + base + start,
6855                                   baseaddr + base + end - 1);
6856             }
6857         }
6858     }
6859 }
6860
6861 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6862    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6863    during 4.6.0 experimental.  */
6864
6865 static int
6866 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6867 {
6868   const char *cs;
6869   int major, minor, release;
6870
6871   if (cu->producer == NULL)
6872     {
6873       /* For unknown compilers expect their behavior is DWARF version
6874          compliant.
6875
6876          GCC started to support .debug_types sections by -gdwarf-4 since
6877          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6878          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6879          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6880          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6881
6882       return 0;
6883     }
6884
6885   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6886
6887   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6888     {
6889       /* For non-GCC compilers expect their behavior is DWARF version
6890          compliant.  */
6891
6892       return 0;
6893     }
6894   cs = &cu->producer[strlen ("GNU ")];
6895   while (*cs && !isdigit (*cs))
6896     cs++;
6897   if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6898     {
6899       /* Not recognized as GCC.  */
6900
6901       return 0;
6902     }
6903
6904   return major < 4 || (major == 4 && minor < 6);
6905 }
6906
6907 /* Return the default accessibility type if it is not overriden by
6908    DW_AT_accessibility.  */
6909
6910 static enum dwarf_access_attribute
6911 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6912 {
6913   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6914     {
6915       /* The default DWARF 2 accessibility for members is public, the default
6916          accessibility for inheritance is private.  */
6917
6918       if (die->tag != DW_TAG_inheritance)
6919         return DW_ACCESS_public;
6920       else
6921         return DW_ACCESS_private;
6922     }
6923   else
6924     {
6925       /* DWARF 3+ defines the default accessibility a different way.  The same
6926          rules apply now for DW_TAG_inheritance as for the members and it only
6927          depends on the container kind.  */
6928
6929       if (die->parent->tag == DW_TAG_class_type)
6930         return DW_ACCESS_private;
6931       else
6932         return DW_ACCESS_public;
6933     }
6934 }
6935
6936 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6937    offset.  If the attribute was not found return 0, otherwise return
6938    1.  If it was found but could not properly be handled, set *OFFSET
6939    to 0.  */
6940
6941 static int
6942 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6943                              LONGEST *offset)
6944 {
6945   struct attribute *attr;
6946
6947   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6948   if (attr != NULL)
6949     {
6950       *offset = 0;
6951
6952       /* Note that we do not check for a section offset first here.
6953          This is because DW_AT_data_member_location is new in DWARF 4,
6954          so if we see it, we can assume that a constant form is really
6955          a constant and not a section offset.  */
6956       if (attr_form_is_constant (attr))
6957         *offset = dwarf2_get_attr_constant_value (attr, 0);
6958       else if (attr_form_is_section_offset (attr))
6959         dwarf2_complex_location_expr_complaint ();
6960       else if (attr_form_is_block (attr))
6961         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6962       else
6963         dwarf2_complex_location_expr_complaint ();
6964
6965       return 1;
6966     }
6967
6968   return 0;
6969 }
6970
6971 /* Add an aggregate field to the field list.  */
6972
6973 static void
6974 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6975                   struct dwarf2_cu *cu)
6976 {
6977   struct objfile *objfile = cu->objfile;
6978   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6979   struct nextfield *new_field;
6980   struct attribute *attr;
6981   struct field *fp;
6982   char *fieldname = "";
6983
6984   /* Allocate a new field list entry and link it in.  */
6985   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6986   make_cleanup (xfree, new_field);
6987   memset (new_field, 0, sizeof (struct nextfield));
6988
6989   if (die->tag == DW_TAG_inheritance)
6990     {
6991       new_field->next = fip->baseclasses;
6992       fip->baseclasses = new_field;
6993     }
6994   else
6995     {
6996       new_field->next = fip->fields;
6997       fip->fields = new_field;
6998     }
6999   fip->nfields++;
7000
7001   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7002   if (attr)
7003     new_field->accessibility = DW_UNSND (attr);
7004   else
7005     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7006   if (new_field->accessibility != DW_ACCESS_public)
7007     fip->non_public_fields = 1;
7008
7009   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7010   if (attr)
7011     new_field->virtuality = DW_UNSND (attr);
7012   else
7013     new_field->virtuality = DW_VIRTUALITY_none;
7014
7015   fp = &new_field->field;
7016
7017   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7018     {
7019       LONGEST offset;
7020
7021       /* Data member other than a C++ static data member.  */
7022
7023       /* Get type of field.  */
7024       fp->type = die_type (die, cu);
7025
7026       SET_FIELD_BITPOS (*fp, 0);
7027
7028       /* Get bit size of field (zero if none).  */
7029       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7030       if (attr)
7031         {
7032           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7033         }
7034       else
7035         {
7036           FIELD_BITSIZE (*fp) = 0;
7037         }
7038
7039       /* Get bit offset of field.  */
7040       if (handle_data_member_location (die, cu, &offset))
7041         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7042       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7043       if (attr)
7044         {
7045           if (gdbarch_bits_big_endian (gdbarch))
7046             {
7047               /* For big endian bits, the DW_AT_bit_offset gives the
7048                  additional bit offset from the MSB of the containing
7049                  anonymous object to the MSB of the field.  We don't
7050                  have to do anything special since we don't need to
7051                  know the size of the anonymous object.  */
7052               FIELD_BITPOS (*fp) += DW_UNSND (attr);
7053             }
7054           else
7055             {
7056               /* For little endian bits, compute the bit offset to the
7057                  MSB of the anonymous object, subtract off the number of
7058                  bits from the MSB of the field to the MSB of the
7059                  object, and then subtract off the number of bits of
7060                  the field itself.  The result is the bit offset of
7061                  the LSB of the field.  */
7062               int anonymous_size;
7063               int bit_offset = DW_UNSND (attr);
7064
7065               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7066               if (attr)
7067                 {
7068                   /* The size of the anonymous object containing
7069                      the bit field is explicit, so use the
7070                      indicated size (in bytes).  */
7071                   anonymous_size = DW_UNSND (attr);
7072                 }
7073               else
7074                 {
7075                   /* The size of the anonymous object containing
7076                      the bit field must be inferred from the type
7077                      attribute of the data member containing the
7078                      bit field.  */
7079                   anonymous_size = TYPE_LENGTH (fp->type);
7080                 }
7081               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7082                 - bit_offset - FIELD_BITSIZE (*fp);
7083             }
7084         }
7085
7086       /* Get name of field.  */
7087       fieldname = dwarf2_name (die, cu);
7088       if (fieldname == NULL)
7089         fieldname = "";
7090
7091       /* The name is already allocated along with this objfile, so we don't
7092          need to duplicate it for the type.  */
7093       fp->name = fieldname;
7094
7095       /* Change accessibility for artificial fields (e.g. virtual table
7096          pointer or virtual base class pointer) to private.  */
7097       if (dwarf2_attr (die, DW_AT_artificial, cu))
7098         {
7099           FIELD_ARTIFICIAL (*fp) = 1;
7100           new_field->accessibility = DW_ACCESS_private;
7101           fip->non_public_fields = 1;
7102         }
7103     }
7104   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7105     {
7106       /* C++ static member.  */
7107
7108       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7109          is a declaration, but all versions of G++ as of this writing
7110          (so through at least 3.2.1) incorrectly generate
7111          DW_TAG_variable tags.  */
7112
7113       const char *physname;
7114
7115       /* Get name of field.  */
7116       fieldname = dwarf2_name (die, cu);
7117       if (fieldname == NULL)
7118         return;
7119
7120       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7121       if (attr
7122           /* Only create a symbol if this is an external value.
7123              new_symbol checks this and puts the value in the global symbol
7124              table, which we want.  If it is not external, new_symbol
7125              will try to put the value in cu->list_in_scope which is wrong.  */
7126           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7127         {
7128           /* A static const member, not much different than an enum as far as
7129              we're concerned, except that we can support more types.  */
7130           new_symbol (die, NULL, cu);
7131         }
7132
7133       /* Get physical name.  */
7134       physname = dwarf2_physname (fieldname, die, cu);
7135
7136       /* The name is already allocated along with this objfile, so we don't
7137          need to duplicate it for the type.  */
7138       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7139       FIELD_TYPE (*fp) = die_type (die, cu);
7140       FIELD_NAME (*fp) = fieldname;
7141     }
7142   else if (die->tag == DW_TAG_inheritance)
7143     {
7144       LONGEST offset;
7145
7146       /* C++ base class field.  */
7147       if (handle_data_member_location (die, cu, &offset))
7148         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7149       FIELD_BITSIZE (*fp) = 0;
7150       FIELD_TYPE (*fp) = die_type (die, cu);
7151       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7152       fip->nbaseclasses++;
7153     }
7154 }
7155
7156 /* Add a typedef defined in the scope of the FIP's class.  */
7157
7158 static void
7159 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7160                     struct dwarf2_cu *cu)
7161 {
7162   struct objfile *objfile = cu->objfile;
7163   struct typedef_field_list *new_field;
7164   struct attribute *attr;
7165   struct typedef_field *fp;
7166   char *fieldname = "";
7167
7168   /* Allocate a new field list entry and link it in.  */
7169   new_field = xzalloc (sizeof (*new_field));
7170   make_cleanup (xfree, new_field);
7171
7172   gdb_assert (die->tag == DW_TAG_typedef);
7173
7174   fp = &new_field->field;
7175
7176   /* Get name of field.  */
7177   fp->name = dwarf2_name (die, cu);
7178   if (fp->name == NULL)
7179     return;
7180
7181   fp->type = read_type_die (die, cu);
7182
7183   new_field->next = fip->typedef_field_list;
7184   fip->typedef_field_list = new_field;
7185   fip->typedef_field_list_count++;
7186 }
7187
7188 /* Create the vector of fields, and attach it to the type.  */
7189
7190 static void
7191 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7192                               struct dwarf2_cu *cu)
7193 {
7194   int nfields = fip->nfields;
7195
7196   /* Record the field count, allocate space for the array of fields,
7197      and create blank accessibility bitfields if necessary.  */
7198   TYPE_NFIELDS (type) = nfields;
7199   TYPE_FIELDS (type) = (struct field *)
7200     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7201   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7202
7203   if (fip->non_public_fields && cu->language != language_ada)
7204     {
7205       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7206
7207       TYPE_FIELD_PRIVATE_BITS (type) =
7208         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7209       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7210
7211       TYPE_FIELD_PROTECTED_BITS (type) =
7212         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7213       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7214
7215       TYPE_FIELD_IGNORE_BITS (type) =
7216         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7217       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7218     }
7219
7220   /* If the type has baseclasses, allocate and clear a bit vector for
7221      TYPE_FIELD_VIRTUAL_BITS.  */
7222   if (fip->nbaseclasses && cu->language != language_ada)
7223     {
7224       int num_bytes = B_BYTES (fip->nbaseclasses);
7225       unsigned char *pointer;
7226
7227       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7228       pointer = TYPE_ALLOC (type, num_bytes);
7229       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7230       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7231       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7232     }
7233
7234   /* Copy the saved-up fields into the field vector.  Start from the head of
7235      the list, adding to the tail of the field array, so that they end up in
7236      the same order in the array in which they were added to the list.  */
7237   while (nfields-- > 0)
7238     {
7239       struct nextfield *fieldp;
7240
7241       if (fip->fields)
7242         {
7243           fieldp = fip->fields;
7244           fip->fields = fieldp->next;
7245         }
7246       else
7247         {
7248           fieldp = fip->baseclasses;
7249           fip->baseclasses = fieldp->next;
7250         }
7251
7252       TYPE_FIELD (type, nfields) = fieldp->field;
7253       switch (fieldp->accessibility)
7254         {
7255         case DW_ACCESS_private:
7256           if (cu->language != language_ada)
7257             SET_TYPE_FIELD_PRIVATE (type, nfields);
7258           break;
7259
7260         case DW_ACCESS_protected:
7261           if (cu->language != language_ada)
7262             SET_TYPE_FIELD_PROTECTED (type, nfields);
7263           break;
7264
7265         case DW_ACCESS_public:
7266           break;
7267
7268         default:
7269           /* Unknown accessibility.  Complain and treat it as public.  */
7270           {
7271             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7272                        fieldp->accessibility);
7273           }
7274           break;
7275         }
7276       if (nfields < fip->nbaseclasses)
7277         {
7278           switch (fieldp->virtuality)
7279             {
7280             case DW_VIRTUALITY_virtual:
7281             case DW_VIRTUALITY_pure_virtual:
7282               if (cu->language == language_ada)
7283                 error (_("unexpected virtuality in component of Ada type"));
7284               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7285               break;
7286             }
7287         }
7288     }
7289 }
7290
7291 /* Add a member function to the proper fieldlist.  */
7292
7293 static void
7294 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7295                       struct type *type, struct dwarf2_cu *cu)
7296 {
7297   struct objfile *objfile = cu->objfile;
7298   struct attribute *attr;
7299   struct fnfieldlist *flp;
7300   int i;
7301   struct fn_field *fnp;
7302   char *fieldname;
7303   struct nextfnfield *new_fnfield;
7304   struct type *this_type;
7305   enum dwarf_access_attribute accessibility;
7306
7307   if (cu->language == language_ada)
7308     error (_("unexpected member function in Ada type"));
7309
7310   /* Get name of member function.  */
7311   fieldname = dwarf2_name (die, cu);
7312   if (fieldname == NULL)
7313     return;
7314
7315   /* Look up member function name in fieldlist.  */
7316   for (i = 0; i < fip->nfnfields; i++)
7317     {
7318       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7319         break;
7320     }
7321
7322   /* Create new list element if necessary.  */
7323   if (i < fip->nfnfields)
7324     flp = &fip->fnfieldlists[i];
7325   else
7326     {
7327       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7328         {
7329           fip->fnfieldlists = (struct fnfieldlist *)
7330             xrealloc (fip->fnfieldlists,
7331                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7332                       * sizeof (struct fnfieldlist));
7333           if (fip->nfnfields == 0)
7334             make_cleanup (free_current_contents, &fip->fnfieldlists);
7335         }
7336       flp = &fip->fnfieldlists[fip->nfnfields];
7337       flp->name = fieldname;
7338       flp->length = 0;
7339       flp->head = NULL;
7340       i = fip->nfnfields++;
7341     }
7342
7343   /* Create a new member function field and chain it to the field list
7344      entry.  */
7345   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7346   make_cleanup (xfree, new_fnfield);
7347   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7348   new_fnfield->next = flp->head;
7349   flp->head = new_fnfield;
7350   flp->length++;
7351
7352   /* Fill in the member function field info.  */
7353   fnp = &new_fnfield->fnfield;
7354
7355   /* Delay processing of the physname until later.  */
7356   if (cu->language == language_cplus || cu->language == language_java)
7357     {
7358       add_to_method_list (type, i, flp->length - 1, fieldname,
7359                           die, cu);
7360     }
7361   else
7362     {
7363       const char *physname = dwarf2_physname (fieldname, die, cu);
7364       fnp->physname = physname ? physname : "";
7365     }
7366
7367   fnp->type = alloc_type (objfile);
7368   this_type = read_type_die (die, cu);
7369   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7370     {
7371       int nparams = TYPE_NFIELDS (this_type);
7372
7373       /* TYPE is the domain of this method, and THIS_TYPE is the type
7374            of the method itself (TYPE_CODE_METHOD).  */
7375       smash_to_method_type (fnp->type, type,
7376                             TYPE_TARGET_TYPE (this_type),
7377                             TYPE_FIELDS (this_type),
7378                             TYPE_NFIELDS (this_type),
7379                             TYPE_VARARGS (this_type));
7380
7381       /* Handle static member functions.
7382          Dwarf2 has no clean way to discern C++ static and non-static
7383          member functions.  G++ helps GDB by marking the first
7384          parameter for non-static member functions (which is the this
7385          pointer) as artificial.  We obtain this information from
7386          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7387       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7388         fnp->voffset = VOFFSET_STATIC;
7389     }
7390   else
7391     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7392                dwarf2_full_name (fieldname, die, cu));
7393
7394   /* Get fcontext from DW_AT_containing_type if present.  */
7395   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7396     fnp->fcontext = die_containing_type (die, cu);
7397
7398   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7399      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7400
7401   /* Get accessibility.  */
7402   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7403   if (attr)
7404     accessibility = DW_UNSND (attr);
7405   else
7406     accessibility = dwarf2_default_access_attribute (die, cu);
7407   switch (accessibility)
7408     {
7409     case DW_ACCESS_private:
7410       fnp->is_private = 1;
7411       break;
7412     case DW_ACCESS_protected:
7413       fnp->is_protected = 1;
7414       break;
7415     }
7416
7417   /* Check for artificial methods.  */
7418   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7419   if (attr && DW_UNSND (attr) != 0)
7420     fnp->is_artificial = 1;
7421
7422   /* Get index in virtual function table if it is a virtual member
7423      function.  For older versions of GCC, this is an offset in the
7424      appropriate virtual table, as specified by DW_AT_containing_type.
7425      For everyone else, it is an expression to be evaluated relative
7426      to the object address.  */
7427
7428   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7429   if (attr)
7430     {
7431       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7432         {
7433           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7434             {
7435               /* Old-style GCC.  */
7436               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7437             }
7438           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7439                    || (DW_BLOCK (attr)->size > 1
7440                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7441                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7442             {
7443               struct dwarf_block blk;
7444               int offset;
7445
7446               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7447                         ? 1 : 2);
7448               blk.size = DW_BLOCK (attr)->size - offset;
7449               blk.data = DW_BLOCK (attr)->data + offset;
7450               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7451               if ((fnp->voffset % cu->header.addr_size) != 0)
7452                 dwarf2_complex_location_expr_complaint ();
7453               else
7454                 fnp->voffset /= cu->header.addr_size;
7455               fnp->voffset += 2;
7456             }
7457           else
7458             dwarf2_complex_location_expr_complaint ();
7459
7460           if (!fnp->fcontext)
7461             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7462         }
7463       else if (attr_form_is_section_offset (attr))
7464         {
7465           dwarf2_complex_location_expr_complaint ();
7466         }
7467       else
7468         {
7469           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7470                                                  fieldname);
7471         }
7472     }
7473   else
7474     {
7475       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7476       if (attr && DW_UNSND (attr))
7477         {
7478           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7479           complaint (&symfile_complaints,
7480                      _("Member function \"%s\" (offset %d) is virtual "
7481                        "but the vtable offset is not specified"),
7482                      fieldname, die->offset);
7483           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7484           TYPE_CPLUS_DYNAMIC (type) = 1;
7485         }
7486     }
7487 }
7488
7489 /* Create the vector of member function fields, and attach it to the type.  */
7490
7491 static void
7492 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7493                                  struct dwarf2_cu *cu)
7494 {
7495   struct fnfieldlist *flp;
7496   int total_length = 0;
7497   int i;
7498
7499   if (cu->language == language_ada)
7500     error (_("unexpected member functions in Ada type"));
7501
7502   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7503   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7504     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7505
7506   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7507     {
7508       struct nextfnfield *nfp = flp->head;
7509       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7510       int k;
7511
7512       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7513       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7514       fn_flp->fn_fields = (struct fn_field *)
7515         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7516       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7517         fn_flp->fn_fields[k] = nfp->fnfield;
7518
7519       total_length += flp->length;
7520     }
7521
7522   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7523   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7524 }
7525
7526 /* Returns non-zero if NAME is the name of a vtable member in CU's
7527    language, zero otherwise.  */
7528 static int
7529 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7530 {
7531   static const char vptr[] = "_vptr";
7532   static const char vtable[] = "vtable";
7533
7534   /* Look for the C++ and Java forms of the vtable.  */
7535   if ((cu->language == language_java
7536        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7537        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7538        && is_cplus_marker (name[sizeof (vptr) - 1])))
7539     return 1;
7540
7541   return 0;
7542 }
7543
7544 /* GCC outputs unnamed structures that are really pointers to member
7545    functions, with the ABI-specified layout.  If TYPE describes
7546    such a structure, smash it into a member function type.
7547
7548    GCC shouldn't do this; it should just output pointer to member DIEs.
7549    This is GCC PR debug/28767.  */
7550
7551 static void
7552 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7553 {
7554   struct type *pfn_type, *domain_type, *new_type;
7555
7556   /* Check for a structure with no name and two children.  */
7557   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7558     return;
7559
7560   /* Check for __pfn and __delta members.  */
7561   if (TYPE_FIELD_NAME (type, 0) == NULL
7562       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7563       || TYPE_FIELD_NAME (type, 1) == NULL
7564       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7565     return;
7566
7567   /* Find the type of the method.  */
7568   pfn_type = TYPE_FIELD_TYPE (type, 0);
7569   if (pfn_type == NULL
7570       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7571       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7572     return;
7573
7574   /* Look for the "this" argument.  */
7575   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7576   if (TYPE_NFIELDS (pfn_type) == 0
7577       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7578       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7579     return;
7580
7581   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7582   new_type = alloc_type (objfile);
7583   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7584                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7585                         TYPE_VARARGS (pfn_type));
7586   smash_to_methodptr_type (type, new_type);
7587 }
7588
7589 /* Called when we find the DIE that starts a structure or union scope
7590    (definition) to create a type for the structure or union.  Fill in
7591    the type's name and general properties; the members will not be
7592    processed until process_structure_type.
7593
7594    NOTE: we need to call these functions regardless of whether or not the
7595    DIE has a DW_AT_name attribute, since it might be an anonymous
7596    structure or union.  This gets the type entered into our set of
7597    user defined types.
7598
7599    However, if the structure is incomplete (an opaque struct/union)
7600    then suppress creating a symbol table entry for it since gdb only
7601    wants to find the one with the complete definition.  Note that if
7602    it is complete, we just call new_symbol, which does it's own
7603    checking about whether the struct/union is anonymous or not (and
7604    suppresses creating a symbol table entry itself).  */
7605
7606 static struct type *
7607 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7608 {
7609   struct objfile *objfile = cu->objfile;
7610   struct type *type;
7611   struct attribute *attr;
7612   char *name;
7613
7614   /* If the definition of this type lives in .debug_types, read that type.
7615      Don't follow DW_AT_specification though, that will take us back up
7616      the chain and we want to go down.  */
7617   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7618   if (attr)
7619     {
7620       struct dwarf2_cu *type_cu = cu;
7621       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7622
7623       /* We could just recurse on read_structure_type, but we need to call
7624          get_die_type to ensure only one type for this DIE is created.
7625          This is important, for example, because for c++ classes we need
7626          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7627       type = read_type_die (type_die, type_cu);
7628
7629       /* TYPE_CU may not be the same as CU.
7630          Ensure TYPE is recorded in CU's type_hash table.  */
7631       return set_die_type (die, type, cu);
7632     }
7633
7634   type = alloc_type (objfile);
7635   INIT_CPLUS_SPECIFIC (type);
7636
7637   name = dwarf2_name (die, cu);
7638   if (name != NULL)
7639     {
7640       if (cu->language == language_cplus
7641           || cu->language == language_java)
7642         {
7643           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7644
7645           /* dwarf2_full_name might have already finished building the DIE's
7646              type.  If so, there is no need to continue.  */
7647           if (get_die_type (die, cu) != NULL)
7648             return get_die_type (die, cu);
7649
7650           TYPE_TAG_NAME (type) = full_name;
7651           if (die->tag == DW_TAG_structure_type
7652               || die->tag == DW_TAG_class_type)
7653             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7654         }
7655       else
7656         {
7657           /* The name is already allocated along with this objfile, so
7658              we don't need to duplicate it for the type.  */
7659           TYPE_TAG_NAME (type) = (char *) name;
7660           if (die->tag == DW_TAG_class_type)
7661             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7662         }
7663     }
7664
7665   if (die->tag == DW_TAG_structure_type)
7666     {
7667       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7668     }
7669   else if (die->tag == DW_TAG_union_type)
7670     {
7671       TYPE_CODE (type) = TYPE_CODE_UNION;
7672     }
7673   else
7674     {
7675       TYPE_CODE (type) = TYPE_CODE_CLASS;
7676     }
7677
7678   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7679     TYPE_DECLARED_CLASS (type) = 1;
7680
7681   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7682   if (attr)
7683     {
7684       TYPE_LENGTH (type) = DW_UNSND (attr);
7685     }
7686   else
7687     {
7688       TYPE_LENGTH (type) = 0;
7689     }
7690
7691   TYPE_STUB_SUPPORTED (type) = 1;
7692   if (die_is_declaration (die, cu))
7693     TYPE_STUB (type) = 1;
7694   else if (attr == NULL && die->child == NULL
7695            && producer_is_realview (cu->producer))
7696     /* RealView does not output the required DW_AT_declaration
7697        on incomplete types.  */
7698     TYPE_STUB (type) = 1;
7699
7700   /* We need to add the type field to the die immediately so we don't
7701      infinitely recurse when dealing with pointers to the structure
7702      type within the structure itself.  */
7703   set_die_type (die, type, cu);
7704
7705   /* set_die_type should be already done.  */
7706   set_descriptive_type (type, die, cu);
7707
7708   return type;
7709 }
7710
7711 /* Finish creating a structure or union type, including filling in
7712    its members and creating a symbol for it.  */
7713
7714 static void
7715 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7716 {
7717   struct objfile *objfile = cu->objfile;
7718   struct die_info *child_die = die->child;
7719   struct type *type;
7720
7721   type = get_die_type (die, cu);
7722   if (type == NULL)
7723     type = read_structure_type (die, cu);
7724
7725   if (die->child != NULL && ! die_is_declaration (die, cu))
7726     {
7727       struct field_info fi;
7728       struct die_info *child_die;
7729       VEC (symbolp) *template_args = NULL;
7730       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7731
7732       memset (&fi, 0, sizeof (struct field_info));
7733
7734       child_die = die->child;
7735
7736       while (child_die && child_die->tag)
7737         {
7738           if (child_die->tag == DW_TAG_member
7739               || child_die->tag == DW_TAG_variable)
7740             {
7741               /* NOTE: carlton/2002-11-05: A C++ static data member
7742                  should be a DW_TAG_member that is a declaration, but
7743                  all versions of G++ as of this writing (so through at
7744                  least 3.2.1) incorrectly generate DW_TAG_variable
7745                  tags for them instead.  */
7746               dwarf2_add_field (&fi, child_die, cu);
7747             }
7748           else if (child_die->tag == DW_TAG_subprogram)
7749             {
7750               /* C++ member function.  */
7751               dwarf2_add_member_fn (&fi, child_die, type, cu);
7752             }
7753           else if (child_die->tag == DW_TAG_inheritance)
7754             {
7755               /* C++ base class field.  */
7756               dwarf2_add_field (&fi, child_die, cu);
7757             }
7758           else if (child_die->tag == DW_TAG_typedef)
7759             dwarf2_add_typedef (&fi, child_die, cu);
7760           else if (child_die->tag == DW_TAG_template_type_param
7761                    || child_die->tag == DW_TAG_template_value_param)
7762             {
7763               struct symbol *arg = new_symbol (child_die, NULL, cu);
7764
7765               if (arg != NULL)
7766                 VEC_safe_push (symbolp, template_args, arg);
7767             }
7768
7769           child_die = sibling_die (child_die);
7770         }
7771
7772       /* Attach template arguments to type.  */
7773       if (! VEC_empty (symbolp, template_args))
7774         {
7775           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7776           TYPE_N_TEMPLATE_ARGUMENTS (type)
7777             = VEC_length (symbolp, template_args);
7778           TYPE_TEMPLATE_ARGUMENTS (type)
7779             = obstack_alloc (&objfile->objfile_obstack,
7780                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7781                               * sizeof (struct symbol *)));
7782           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7783                   VEC_address (symbolp, template_args),
7784                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7785                    * sizeof (struct symbol *)));
7786           VEC_free (symbolp, template_args);
7787         }
7788
7789       /* Attach fields and member functions to the type.  */
7790       if (fi.nfields)
7791         dwarf2_attach_fields_to_type (&fi, type, cu);
7792       if (fi.nfnfields)
7793         {
7794           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7795
7796           /* Get the type which refers to the base class (possibly this
7797              class itself) which contains the vtable pointer for the current
7798              class from the DW_AT_containing_type attribute.  This use of
7799              DW_AT_containing_type is a GNU extension.  */
7800
7801           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7802             {
7803               struct type *t = die_containing_type (die, cu);
7804
7805               TYPE_VPTR_BASETYPE (type) = t;
7806               if (type == t)
7807                 {
7808                   int i;
7809
7810                   /* Our own class provides vtbl ptr.  */
7811                   for (i = TYPE_NFIELDS (t) - 1;
7812                        i >= TYPE_N_BASECLASSES (t);
7813                        --i)
7814                     {
7815                       char *fieldname = TYPE_FIELD_NAME (t, i);
7816
7817                       if (is_vtable_name (fieldname, cu))
7818                         {
7819                           TYPE_VPTR_FIELDNO (type) = i;
7820                           break;
7821                         }
7822                     }
7823
7824                   /* Complain if virtual function table field not found.  */
7825                   if (i < TYPE_N_BASECLASSES (t))
7826                     complaint (&symfile_complaints,
7827                                _("virtual function table pointer "
7828                                  "not found when defining class '%s'"),
7829                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7830                                "");
7831                 }
7832               else
7833                 {
7834                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7835                 }
7836             }
7837           else if (cu->producer
7838                    && strncmp (cu->producer,
7839                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7840             {
7841               /* The IBM XLC compiler does not provide direct indication
7842                  of the containing type, but the vtable pointer is
7843                  always named __vfp.  */
7844
7845               int i;
7846
7847               for (i = TYPE_NFIELDS (type) - 1;
7848                    i >= TYPE_N_BASECLASSES (type);
7849                    --i)
7850                 {
7851                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7852                     {
7853                       TYPE_VPTR_FIELDNO (type) = i;
7854                       TYPE_VPTR_BASETYPE (type) = type;
7855                       break;
7856                     }
7857                 }
7858             }
7859         }
7860
7861       /* Copy fi.typedef_field_list linked list elements content into the
7862          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7863       if (fi.typedef_field_list)
7864         {
7865           int i = fi.typedef_field_list_count;
7866
7867           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7868           TYPE_TYPEDEF_FIELD_ARRAY (type)
7869             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7870           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7871
7872           /* Reverse the list order to keep the debug info elements order.  */
7873           while (--i >= 0)
7874             {
7875               struct typedef_field *dest, *src;
7876
7877               dest = &TYPE_TYPEDEF_FIELD (type, i);
7878               src = &fi.typedef_field_list->field;
7879               fi.typedef_field_list = fi.typedef_field_list->next;
7880               *dest = *src;
7881             }
7882         }
7883
7884       do_cleanups (back_to);
7885
7886       if (HAVE_CPLUS_STRUCT (type))
7887         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7888     }
7889
7890   quirk_gcc_member_function_pointer (type, cu->objfile);
7891
7892   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7893      snapshots) has been known to create a die giving a declaration
7894      for a class that has, as a child, a die giving a definition for a
7895      nested class.  So we have to process our children even if the
7896      current die is a declaration.  Normally, of course, a declaration
7897      won't have any children at all.  */
7898
7899   while (child_die != NULL && child_die->tag)
7900     {
7901       if (child_die->tag == DW_TAG_member
7902           || child_die->tag == DW_TAG_variable
7903           || child_die->tag == DW_TAG_inheritance
7904           || child_die->tag == DW_TAG_template_value_param
7905           || child_die->tag == DW_TAG_template_type_param)
7906         {
7907           /* Do nothing.  */
7908         }
7909       else
7910         process_die (child_die, cu);
7911
7912       child_die = sibling_die (child_die);
7913     }
7914
7915   /* Do not consider external references.  According to the DWARF standard,
7916      these DIEs are identified by the fact that they have no byte_size
7917      attribute, and a declaration attribute.  */
7918   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7919       || !die_is_declaration (die, cu))
7920     new_symbol (die, type, cu);
7921 }
7922
7923 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7924    complete the type's fields yet, or create any symbols.  */
7925
7926 static struct type *
7927 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7928 {
7929   struct objfile *objfile = cu->objfile;
7930   struct type *type;
7931   struct attribute *attr;
7932   const char *name;
7933
7934   /* If the definition of this type lives in .debug_types, read that type.
7935      Don't follow DW_AT_specification though, that will take us back up
7936      the chain and we want to go down.  */
7937   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7938   if (attr)
7939     {
7940       struct dwarf2_cu *type_cu = cu;
7941       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7942
7943       type = read_type_die (type_die, type_cu);
7944
7945       /* TYPE_CU may not be the same as CU.
7946          Ensure TYPE is recorded in CU's type_hash table.  */
7947       return set_die_type (die, type, cu);
7948     }
7949
7950   type = alloc_type (objfile);
7951
7952   TYPE_CODE (type) = TYPE_CODE_ENUM;
7953   name = dwarf2_full_name (NULL, die, cu);
7954   if (name != NULL)
7955     TYPE_TAG_NAME (type) = (char *) name;
7956
7957   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7958   if (attr)
7959     {
7960       TYPE_LENGTH (type) = DW_UNSND (attr);
7961     }
7962   else
7963     {
7964       TYPE_LENGTH (type) = 0;
7965     }
7966
7967   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7968      declared as private in the package spec, and then defined only
7969      inside the package body.  Such types are known as Taft Amendment
7970      Types.  When another package uses such a type, an incomplete DIE
7971      may be generated by the compiler.  */
7972   if (die_is_declaration (die, cu))
7973     TYPE_STUB (type) = 1;
7974
7975   return set_die_type (die, type, cu);
7976 }
7977
7978 /* Given a pointer to a die which begins an enumeration, process all
7979    the dies that define the members of the enumeration, and create the
7980    symbol for the enumeration type.
7981
7982    NOTE: We reverse the order of the element list.  */
7983
7984 static void
7985 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7986 {
7987   struct type *this_type;
7988
7989   this_type = get_die_type (die, cu);
7990   if (this_type == NULL)
7991     this_type = read_enumeration_type (die, cu);
7992
7993   if (die->child != NULL)
7994     {
7995       struct die_info *child_die;
7996       struct symbol *sym;
7997       struct field *fields = NULL;
7998       int num_fields = 0;
7999       int unsigned_enum = 1;
8000       char *name;
8001
8002       child_die = die->child;
8003       while (child_die && child_die->tag)
8004         {
8005           if (child_die->tag != DW_TAG_enumerator)
8006             {
8007               process_die (child_die, cu);
8008             }
8009           else
8010             {
8011               name = dwarf2_name (child_die, cu);
8012               if (name)
8013                 {
8014                   sym = new_symbol (child_die, this_type, cu);
8015                   if (SYMBOL_VALUE (sym) < 0)
8016                     unsigned_enum = 0;
8017
8018                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8019                     {
8020                       fields = (struct field *)
8021                         xrealloc (fields,
8022                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
8023                                   * sizeof (struct field));
8024                     }
8025
8026                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8027                   FIELD_TYPE (fields[num_fields]) = NULL;
8028                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8029                   FIELD_BITSIZE (fields[num_fields]) = 0;
8030
8031                   num_fields++;
8032                 }
8033             }
8034
8035           child_die = sibling_die (child_die);
8036         }
8037
8038       if (num_fields)
8039         {
8040           TYPE_NFIELDS (this_type) = num_fields;
8041           TYPE_FIELDS (this_type) = (struct field *)
8042             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8043           memcpy (TYPE_FIELDS (this_type), fields,
8044                   sizeof (struct field) * num_fields);
8045           xfree (fields);
8046         }
8047       if (unsigned_enum)
8048         TYPE_UNSIGNED (this_type) = 1;
8049     }
8050
8051   /* If we are reading an enum from a .debug_types unit, and the enum
8052      is a declaration, and the enum is not the signatured type in the
8053      unit, then we do not want to add a symbol for it.  Adding a
8054      symbol would in some cases obscure the true definition of the
8055      enum, giving users an incomplete type when the definition is
8056      actually available.  Note that we do not want to do this for all
8057      enums which are just declarations, because C++0x allows forward
8058      enum declarations.  */
8059   if (cu->per_cu->debug_type_section
8060       && die_is_declaration (die, cu))
8061     {
8062       struct signatured_type *type_sig;
8063
8064       type_sig
8065         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8066                                             cu->per_cu->debug_type_section,
8067                                             cu->per_cu->offset);
8068       if (type_sig->type_offset != die->offset)
8069         return;
8070     }
8071
8072   new_symbol (die, this_type, cu);
8073 }
8074
8075 /* Extract all information from a DW_TAG_array_type DIE and put it in
8076    the DIE's type field.  For now, this only handles one dimensional
8077    arrays.  */
8078
8079 static struct type *
8080 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8081 {
8082   struct objfile *objfile = cu->objfile;
8083   struct die_info *child_die;
8084   struct type *type;
8085   struct type *element_type, *range_type, *index_type;
8086   struct type **range_types = NULL;
8087   struct attribute *attr;
8088   int ndim = 0;
8089   struct cleanup *back_to;
8090   char *name;
8091
8092   element_type = die_type (die, cu);
8093
8094   /* The die_type call above may have already set the type for this DIE.  */
8095   type = get_die_type (die, cu);
8096   if (type)
8097     return type;
8098
8099   /* Irix 6.2 native cc creates array types without children for
8100      arrays with unspecified length.  */
8101   if (die->child == NULL)
8102     {
8103       index_type = objfile_type (objfile)->builtin_int;
8104       range_type = create_range_type (NULL, index_type, 0, -1);
8105       type = create_array_type (NULL, element_type, range_type);
8106       return set_die_type (die, type, cu);
8107     }
8108
8109   back_to = make_cleanup (null_cleanup, NULL);
8110   child_die = die->child;
8111   while (child_die && child_die->tag)
8112     {
8113       if (child_die->tag == DW_TAG_subrange_type)
8114         {
8115           struct type *child_type = read_type_die (child_die, cu);
8116
8117           if (child_type != NULL)
8118             {
8119               /* The range type was succesfully read.  Save it for the
8120                  array type creation.  */
8121               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8122                 {
8123                   range_types = (struct type **)
8124                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8125                               * sizeof (struct type *));
8126                   if (ndim == 0)
8127                     make_cleanup (free_current_contents, &range_types);
8128                 }
8129               range_types[ndim++] = child_type;
8130             }
8131         }
8132       child_die = sibling_die (child_die);
8133     }
8134
8135   /* Dwarf2 dimensions are output from left to right, create the
8136      necessary array types in backwards order.  */
8137
8138   type = element_type;
8139
8140   if (read_array_order (die, cu) == DW_ORD_col_major)
8141     {
8142       int i = 0;
8143
8144       while (i < ndim)
8145         type = create_array_type (NULL, type, range_types[i++]);
8146     }
8147   else
8148     {
8149       while (ndim-- > 0)
8150         type = create_array_type (NULL, type, range_types[ndim]);
8151     }
8152
8153   /* Understand Dwarf2 support for vector types (like they occur on
8154      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8155      array type.  This is not part of the Dwarf2/3 standard yet, but a
8156      custom vendor extension.  The main difference between a regular
8157      array and the vector variant is that vectors are passed by value
8158      to functions.  */
8159   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8160   if (attr)
8161     make_vector_type (type);
8162
8163   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8164      implementation may choose to implement triple vectors using this
8165      attribute.  */
8166   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8167   if (attr)
8168     {
8169       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8170         TYPE_LENGTH (type) = DW_UNSND (attr);
8171       else
8172         complaint (&symfile_complaints,
8173                    _("DW_AT_byte_size for array type smaller "
8174                      "than the total size of elements"));
8175     }
8176
8177   name = dwarf2_name (die, cu);
8178   if (name)
8179     TYPE_NAME (type) = name;
8180
8181   /* Install the type in the die.  */
8182   set_die_type (die, type, cu);
8183
8184   /* set_die_type should be already done.  */
8185   set_descriptive_type (type, die, cu);
8186
8187   do_cleanups (back_to);
8188
8189   return type;
8190 }
8191
8192 static enum dwarf_array_dim_ordering
8193 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8194 {
8195   struct attribute *attr;
8196
8197   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8198
8199   if (attr) return DW_SND (attr);
8200
8201   /* GNU F77 is a special case, as at 08/2004 array type info is the
8202      opposite order to the dwarf2 specification, but data is still
8203      laid out as per normal fortran.
8204
8205      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8206      version checking.  */
8207
8208   if (cu->language == language_fortran
8209       && cu->producer && strstr (cu->producer, "GNU F77"))
8210     {
8211       return DW_ORD_row_major;
8212     }
8213
8214   switch (cu->language_defn->la_array_ordering)
8215     {
8216     case array_column_major:
8217       return DW_ORD_col_major;
8218     case array_row_major:
8219     default:
8220       return DW_ORD_row_major;
8221     };
8222 }
8223
8224 /* Extract all information from a DW_TAG_set_type DIE and put it in
8225    the DIE's type field.  */
8226
8227 static struct type *
8228 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8229 {
8230   struct type *domain_type, *set_type;
8231   struct attribute *attr;
8232
8233   domain_type = die_type (die, cu);
8234
8235   /* The die_type call above may have already set the type for this DIE.  */
8236   set_type = get_die_type (die, cu);
8237   if (set_type)
8238     return set_type;
8239
8240   set_type = create_set_type (NULL, domain_type);
8241
8242   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8243   if (attr)
8244     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8245
8246   return set_die_type (die, set_type, cu);
8247 }
8248
8249 /* First cut: install each common block member as a global variable.  */
8250
8251 static void
8252 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8253 {
8254   struct die_info *child_die;
8255   struct attribute *attr;
8256   struct symbol *sym;
8257   CORE_ADDR base = (CORE_ADDR) 0;
8258
8259   attr = dwarf2_attr (die, DW_AT_location, cu);
8260   if (attr)
8261     {
8262       /* Support the .debug_loc offsets.  */
8263       if (attr_form_is_block (attr))
8264         {
8265           base = decode_locdesc (DW_BLOCK (attr), cu);
8266         }
8267       else if (attr_form_is_section_offset (attr))
8268         {
8269           dwarf2_complex_location_expr_complaint ();
8270         }
8271       else
8272         {
8273           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8274                                                  "common block member");
8275         }
8276     }
8277   if (die->child != NULL)
8278     {
8279       child_die = die->child;
8280       while (child_die && child_die->tag)
8281         {
8282           LONGEST offset;
8283
8284           sym = new_symbol (child_die, NULL, cu);
8285           if (sym != NULL
8286               && handle_data_member_location (child_die, cu, &offset))
8287             {
8288               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8289               add_symbol_to_list (sym, &global_symbols);
8290             }
8291           child_die = sibling_die (child_die);
8292         }
8293     }
8294 }
8295
8296 /* Create a type for a C++ namespace.  */
8297
8298 static struct type *
8299 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8300 {
8301   struct objfile *objfile = cu->objfile;
8302   const char *previous_prefix, *name;
8303   int is_anonymous;
8304   struct type *type;
8305
8306   /* For extensions, reuse the type of the original namespace.  */
8307   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8308     {
8309       struct die_info *ext_die;
8310       struct dwarf2_cu *ext_cu = cu;
8311
8312       ext_die = dwarf2_extension (die, &ext_cu);
8313       type = read_type_die (ext_die, ext_cu);
8314
8315       /* EXT_CU may not be the same as CU.
8316          Ensure TYPE is recorded in CU's type_hash table.  */
8317       return set_die_type (die, type, cu);
8318     }
8319
8320   name = namespace_name (die, &is_anonymous, cu);
8321
8322   /* Now build the name of the current namespace.  */
8323
8324   previous_prefix = determine_prefix (die, cu);
8325   if (previous_prefix[0] != '\0')
8326     name = typename_concat (&objfile->objfile_obstack,
8327                             previous_prefix, name, 0, cu);
8328
8329   /* Create the type.  */
8330   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8331                     objfile);
8332   TYPE_NAME (type) = (char *) name;
8333   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8334
8335   return set_die_type (die, type, cu);
8336 }
8337
8338 /* Read a C++ namespace.  */
8339
8340 static void
8341 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8342 {
8343   struct objfile *objfile = cu->objfile;
8344   int is_anonymous;
8345
8346   /* Add a symbol associated to this if we haven't seen the namespace
8347      before.  Also, add a using directive if it's an anonymous
8348      namespace.  */
8349
8350   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8351     {
8352       struct type *type;
8353
8354       type = read_type_die (die, cu);
8355       new_symbol (die, type, cu);
8356
8357       namespace_name (die, &is_anonymous, cu);
8358       if (is_anonymous)
8359         {
8360           const char *previous_prefix = determine_prefix (die, cu);
8361
8362           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8363                                   NULL, NULL, &objfile->objfile_obstack);
8364         }
8365     }
8366
8367   if (die->child != NULL)
8368     {
8369       struct die_info *child_die = die->child;
8370
8371       while (child_die && child_die->tag)
8372         {
8373           process_die (child_die, cu);
8374           child_die = sibling_die (child_die);
8375         }
8376     }
8377 }
8378
8379 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8380    imported module.  Still we need that type as local Fortran "use ... only"
8381    declaration imports depend on the created type in determine_prefix.  */
8382
8383 static struct type *
8384 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8385 {
8386   struct objfile *objfile = cu->objfile;
8387   char *module_name;
8388   struct type *type;
8389
8390   module_name = dwarf2_name (die, cu);
8391   if (!module_name)
8392     complaint (&symfile_complaints,
8393                _("DW_TAG_module has no name, offset 0x%x"),
8394                die->offset);
8395   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8396
8397   /* determine_prefix uses TYPE_TAG_NAME.  */
8398   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8399
8400   return set_die_type (die, type, cu);
8401 }
8402
8403 /* Read a Fortran module.  */
8404
8405 static void
8406 read_module (struct die_info *die, struct dwarf2_cu *cu)
8407 {
8408   struct die_info *child_die = die->child;
8409
8410   while (child_die && child_die->tag)
8411     {
8412       process_die (child_die, cu);
8413       child_die = sibling_die (child_die);
8414     }
8415 }
8416
8417 /* Return the name of the namespace represented by DIE.  Set
8418    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8419    namespace.  */
8420
8421 static const char *
8422 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8423 {
8424   struct die_info *current_die;
8425   const char *name = NULL;
8426
8427   /* Loop through the extensions until we find a name.  */
8428
8429   for (current_die = die;
8430        current_die != NULL;
8431        current_die = dwarf2_extension (die, &cu))
8432     {
8433       name = dwarf2_name (current_die, cu);
8434       if (name != NULL)
8435         break;
8436     }
8437
8438   /* Is it an anonymous namespace?  */
8439
8440   *is_anonymous = (name == NULL);
8441   if (*is_anonymous)
8442     name = CP_ANONYMOUS_NAMESPACE_STR;
8443
8444   return name;
8445 }
8446
8447 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8448    the user defined type vector.  */
8449
8450 static struct type *
8451 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8452 {
8453   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8454   struct comp_unit_head *cu_header = &cu->header;
8455   struct type *type;
8456   struct attribute *attr_byte_size;
8457   struct attribute *attr_address_class;
8458   int byte_size, addr_class;
8459   struct type *target_type;
8460
8461   target_type = die_type (die, cu);
8462
8463   /* The die_type call above may have already set the type for this DIE.  */
8464   type = get_die_type (die, cu);
8465   if (type)
8466     return type;
8467
8468   type = lookup_pointer_type (target_type);
8469
8470   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8471   if (attr_byte_size)
8472     byte_size = DW_UNSND (attr_byte_size);
8473   else
8474     byte_size = cu_header->addr_size;
8475
8476   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8477   if (attr_address_class)
8478     addr_class = DW_UNSND (attr_address_class);
8479   else
8480     addr_class = DW_ADDR_none;
8481
8482   /* If the pointer size or address class is different than the
8483      default, create a type variant marked as such and set the
8484      length accordingly.  */
8485   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8486     {
8487       if (gdbarch_address_class_type_flags_p (gdbarch))
8488         {
8489           int type_flags;
8490
8491           type_flags = gdbarch_address_class_type_flags
8492                          (gdbarch, byte_size, addr_class);
8493           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8494                       == 0);
8495           type = make_type_with_address_space (type, type_flags);
8496         }
8497       else if (TYPE_LENGTH (type) != byte_size)
8498         {
8499           complaint (&symfile_complaints,
8500                      _("invalid pointer size %d"), byte_size);
8501         }
8502       else
8503         {
8504           /* Should we also complain about unhandled address classes?  */
8505         }
8506     }
8507
8508   TYPE_LENGTH (type) = byte_size;
8509   return set_die_type (die, type, cu);
8510 }
8511
8512 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8513    the user defined type vector.  */
8514
8515 static struct type *
8516 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8517 {
8518   struct type *type;
8519   struct type *to_type;
8520   struct type *domain;
8521
8522   to_type = die_type (die, cu);
8523   domain = die_containing_type (die, cu);
8524
8525   /* The calls above may have already set the type for this DIE.  */
8526   type = get_die_type (die, cu);
8527   if (type)
8528     return type;
8529
8530   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8531     type = lookup_methodptr_type (to_type);
8532   else
8533     type = lookup_memberptr_type (to_type, domain);
8534
8535   return set_die_type (die, type, cu);
8536 }
8537
8538 /* Extract all information from a DW_TAG_reference_type DIE and add to
8539    the user defined type vector.  */
8540
8541 static struct type *
8542 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8543 {
8544   struct comp_unit_head *cu_header = &cu->header;
8545   struct type *type, *target_type;
8546   struct attribute *attr;
8547
8548   target_type = die_type (die, cu);
8549
8550   /* The die_type call above may have already set the type for this DIE.  */
8551   type = get_die_type (die, cu);
8552   if (type)
8553     return type;
8554
8555   type = lookup_reference_type (target_type);
8556   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8557   if (attr)
8558     {
8559       TYPE_LENGTH (type) = DW_UNSND (attr);
8560     }
8561   else
8562     {
8563       TYPE_LENGTH (type) = cu_header->addr_size;
8564     }
8565   return set_die_type (die, type, cu);
8566 }
8567
8568 static struct type *
8569 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8570 {
8571   struct type *base_type, *cv_type;
8572
8573   base_type = die_type (die, cu);
8574
8575   /* The die_type call above may have already set the type for this DIE.  */
8576   cv_type = get_die_type (die, cu);
8577   if (cv_type)
8578     return cv_type;
8579
8580   /* In case the const qualifier is applied to an array type, the element type
8581      is so qualified, not the array type (section 6.7.3 of C99).  */
8582   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8583     {
8584       struct type *el_type, *inner_array;
8585
8586       base_type = copy_type (base_type);
8587       inner_array = base_type;
8588
8589       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8590         {
8591           TYPE_TARGET_TYPE (inner_array) =
8592             copy_type (TYPE_TARGET_TYPE (inner_array));
8593           inner_array = TYPE_TARGET_TYPE (inner_array);
8594         }
8595
8596       el_type = TYPE_TARGET_TYPE (inner_array);
8597       TYPE_TARGET_TYPE (inner_array) =
8598         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8599
8600       return set_die_type (die, base_type, cu);
8601     }
8602
8603   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8604   return set_die_type (die, cv_type, cu);
8605 }
8606
8607 static struct type *
8608 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8609 {
8610   struct type *base_type, *cv_type;
8611
8612   base_type = die_type (die, cu);
8613
8614   /* The die_type call above may have already set the type for this DIE.  */
8615   cv_type = get_die_type (die, cu);
8616   if (cv_type)
8617     return cv_type;
8618
8619   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8620   return set_die_type (die, cv_type, cu);
8621 }
8622
8623 /* Extract all information from a DW_TAG_string_type DIE and add to
8624    the user defined type vector.  It isn't really a user defined type,
8625    but it behaves like one, with other DIE's using an AT_user_def_type
8626    attribute to reference it.  */
8627
8628 static struct type *
8629 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8630 {
8631   struct objfile *objfile = cu->objfile;
8632   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8633   struct type *type, *range_type, *index_type, *char_type;
8634   struct attribute *attr;
8635   unsigned int length;
8636
8637   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8638   if (attr)
8639     {
8640       length = DW_UNSND (attr);
8641     }
8642   else
8643     {
8644       /* Check for the DW_AT_byte_size attribute.  */
8645       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8646       if (attr)
8647         {
8648           length = DW_UNSND (attr);
8649         }
8650       else
8651         {
8652           length = 1;
8653         }
8654     }
8655
8656   index_type = objfile_type (objfile)->builtin_int;
8657   range_type = create_range_type (NULL, index_type, 1, length);
8658   char_type = language_string_char_type (cu->language_defn, gdbarch);
8659   type = create_string_type (NULL, char_type, range_type);
8660
8661   return set_die_type (die, type, cu);
8662 }
8663
8664 /* Handle DIES due to C code like:
8665
8666    struct foo
8667    {
8668    int (*funcp)(int a, long l);
8669    int b;
8670    };
8671
8672    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8673
8674 static struct type *
8675 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8676 {
8677   struct type *type;            /* Type that this function returns.  */
8678   struct type *ftype;           /* Function that returns above type.  */
8679   struct attribute *attr;
8680
8681   type = die_type (die, cu);
8682
8683   /* The die_type call above may have already set the type for this DIE.  */
8684   ftype = get_die_type (die, cu);
8685   if (ftype)
8686     return ftype;
8687
8688   ftype = lookup_function_type (type);
8689
8690   /* All functions in C++, Pascal and Java have prototypes.  */
8691   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8692   if ((attr && (DW_UNSND (attr) != 0))
8693       || cu->language == language_cplus
8694       || cu->language == language_java
8695       || cu->language == language_pascal)
8696     TYPE_PROTOTYPED (ftype) = 1;
8697   else if (producer_is_realview (cu->producer))
8698     /* RealView does not emit DW_AT_prototyped.  We can not
8699        distinguish prototyped and unprototyped functions; default to
8700        prototyped, since that is more common in modern code (and
8701        RealView warns about unprototyped functions).  */
8702     TYPE_PROTOTYPED (ftype) = 1;
8703
8704   /* Store the calling convention in the type if it's available in
8705      the subroutine die.  Otherwise set the calling convention to
8706      the default value DW_CC_normal.  */
8707   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8708   if (attr)
8709     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8710   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8711     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8712   else
8713     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8714
8715   /* We need to add the subroutine type to the die immediately so
8716      we don't infinitely recurse when dealing with parameters
8717      declared as the same subroutine type.  */
8718   set_die_type (die, ftype, cu);
8719
8720   if (die->child != NULL)
8721     {
8722       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8723       struct die_info *child_die;
8724       int nparams, iparams;
8725
8726       /* Count the number of parameters.
8727          FIXME: GDB currently ignores vararg functions, but knows about
8728          vararg member functions.  */
8729       nparams = 0;
8730       child_die = die->child;
8731       while (child_die && child_die->tag)
8732         {
8733           if (child_die->tag == DW_TAG_formal_parameter)
8734             nparams++;
8735           else if (child_die->tag == DW_TAG_unspecified_parameters)
8736             TYPE_VARARGS (ftype) = 1;
8737           child_die = sibling_die (child_die);
8738         }
8739
8740       /* Allocate storage for parameters and fill them in.  */
8741       TYPE_NFIELDS (ftype) = nparams;
8742       TYPE_FIELDS (ftype) = (struct field *)
8743         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8744
8745       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8746          even if we error out during the parameters reading below.  */
8747       for (iparams = 0; iparams < nparams; iparams++)
8748         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8749
8750       iparams = 0;
8751       child_die = die->child;
8752       while (child_die && child_die->tag)
8753         {
8754           if (child_die->tag == DW_TAG_formal_parameter)
8755             {
8756               struct type *arg_type;
8757
8758               /* DWARF version 2 has no clean way to discern C++
8759                  static and non-static member functions.  G++ helps
8760                  GDB by marking the first parameter for non-static
8761                  member functions (which is the this pointer) as
8762                  artificial.  We pass this information to
8763                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8764
8765                  DWARF version 3 added DW_AT_object_pointer, which GCC
8766                  4.5 does not yet generate.  */
8767               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8768               if (attr)
8769                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8770               else
8771                 {
8772                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8773
8774                   /* GCC/43521: In java, the formal parameter
8775                      "this" is sometimes not marked with DW_AT_artificial.  */
8776                   if (cu->language == language_java)
8777                     {
8778                       const char *name = dwarf2_name (child_die, cu);
8779
8780                       if (name && !strcmp (name, "this"))
8781                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8782                     }
8783                 }
8784               arg_type = die_type (child_die, cu);
8785
8786               /* RealView does not mark THIS as const, which the testsuite
8787                  expects.  GCC marks THIS as const in method definitions,
8788                  but not in the class specifications (GCC PR 43053).  */
8789               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8790                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8791                 {
8792                   int is_this = 0;
8793                   struct dwarf2_cu *arg_cu = cu;
8794                   const char *name = dwarf2_name (child_die, cu);
8795
8796                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8797                   if (attr)
8798                     {
8799                       /* If the compiler emits this, use it.  */
8800                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8801                         is_this = 1;
8802                     }
8803                   else if (name && strcmp (name, "this") == 0)
8804                     /* Function definitions will have the argument names.  */
8805                     is_this = 1;
8806                   else if (name == NULL && iparams == 0)
8807                     /* Declarations may not have the names, so like
8808                        elsewhere in GDB, assume an artificial first
8809                        argument is "this".  */
8810                     is_this = 1;
8811
8812                   if (is_this)
8813                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8814                                              arg_type, 0);
8815                 }
8816
8817               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8818               iparams++;
8819             }
8820           child_die = sibling_die (child_die);
8821         }
8822     }
8823
8824   return ftype;
8825 }
8826
8827 static struct type *
8828 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8829 {
8830   struct objfile *objfile = cu->objfile;
8831   const char *name = NULL;
8832   struct type *this_type;
8833
8834   name = dwarf2_full_name (NULL, die, cu);
8835   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8836                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8837   TYPE_NAME (this_type) = (char *) name;
8838   set_die_type (die, this_type, cu);
8839   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8840   return this_type;
8841 }
8842
8843 /* Find a representation of a given base type and install
8844    it in the TYPE field of the die.  */
8845
8846 static struct type *
8847 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8848 {
8849   struct objfile *objfile = cu->objfile;
8850   struct type *type;
8851   struct attribute *attr;
8852   int encoding = 0, size = 0;
8853   char *name;
8854   enum type_code code = TYPE_CODE_INT;
8855   int type_flags = 0;
8856   struct type *target_type = NULL;
8857
8858   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8859   if (attr)
8860     {
8861       encoding = DW_UNSND (attr);
8862     }
8863   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8864   if (attr)
8865     {
8866       size = DW_UNSND (attr);
8867     }
8868   name = dwarf2_name (die, cu);
8869   if (!name)
8870     {
8871       complaint (&symfile_complaints,
8872                  _("DW_AT_name missing from DW_TAG_base_type"));
8873     }
8874
8875   switch (encoding)
8876     {
8877       case DW_ATE_address:
8878         /* Turn DW_ATE_address into a void * pointer.  */
8879         code = TYPE_CODE_PTR;
8880         type_flags |= TYPE_FLAG_UNSIGNED;
8881         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8882         break;
8883       case DW_ATE_boolean:
8884         code = TYPE_CODE_BOOL;
8885         type_flags |= TYPE_FLAG_UNSIGNED;
8886         break;
8887       case DW_ATE_complex_float:
8888         code = TYPE_CODE_COMPLEX;
8889         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8890         break;
8891       case DW_ATE_decimal_float:
8892         code = TYPE_CODE_DECFLOAT;
8893         break;
8894       case DW_ATE_float:
8895         code = TYPE_CODE_FLT;
8896         break;
8897       case DW_ATE_signed:
8898         break;
8899       case DW_ATE_unsigned:
8900         type_flags |= TYPE_FLAG_UNSIGNED;
8901         if (cu->language == language_fortran
8902             && name
8903             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8904           code = TYPE_CODE_CHAR;
8905         break;
8906       case DW_ATE_signed_char:
8907         if (cu->language == language_ada || cu->language == language_m2
8908             || cu->language == language_pascal
8909             || cu->language == language_fortran)
8910           code = TYPE_CODE_CHAR;
8911         break;
8912       case DW_ATE_unsigned_char:
8913         if (cu->language == language_ada || cu->language == language_m2
8914             || cu->language == language_pascal
8915             || cu->language == language_fortran)
8916           code = TYPE_CODE_CHAR;
8917         type_flags |= TYPE_FLAG_UNSIGNED;
8918         break;
8919       case DW_ATE_UTF:
8920         /* We just treat this as an integer and then recognize the
8921            type by name elsewhere.  */
8922         break;
8923
8924       default:
8925         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8926                    dwarf_type_encoding_name (encoding));
8927         break;
8928     }
8929
8930   type = init_type (code, size, type_flags, NULL, objfile);
8931   TYPE_NAME (type) = name;
8932   TYPE_TARGET_TYPE (type) = target_type;
8933
8934   if (name && strcmp (name, "char") == 0)
8935     TYPE_NOSIGN (type) = 1;
8936
8937   return set_die_type (die, type, cu);
8938 }
8939
8940 /* Read the given DW_AT_subrange DIE.  */
8941
8942 static struct type *
8943 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8944 {
8945   struct type *base_type;
8946   struct type *range_type;
8947   struct attribute *attr;
8948   LONGEST low = 0;
8949   LONGEST high = -1;
8950   char *name;
8951   LONGEST negative_mask;
8952
8953   base_type = die_type (die, cu);
8954   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8955   check_typedef (base_type);
8956
8957   /* The die_type call above may have already set the type for this DIE.  */
8958   range_type = get_die_type (die, cu);
8959   if (range_type)
8960     return range_type;
8961
8962   if (cu->language == language_fortran)
8963     {
8964       /* FORTRAN implies a lower bound of 1, if not given.  */
8965       low = 1;
8966     }
8967
8968   /* FIXME: For variable sized arrays either of these could be
8969      a variable rather than a constant value.  We'll allow it,
8970      but we don't know how to handle it.  */
8971   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8972   if (attr)
8973     low = dwarf2_get_attr_constant_value (attr, 0);
8974
8975   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8976   if (attr)
8977     {
8978       if (attr_form_is_block (attr) || is_ref_attr (attr))
8979         {
8980           /* GCC encodes arrays with unspecified or dynamic length
8981              with a DW_FORM_block1 attribute or a reference attribute.
8982              FIXME: GDB does not yet know how to handle dynamic
8983              arrays properly, treat them as arrays with unspecified
8984              length for now.
8985
8986              FIXME: jimb/2003-09-22: GDB does not really know
8987              how to handle arrays of unspecified length
8988              either; we just represent them as zero-length
8989              arrays.  Choose an appropriate upper bound given
8990              the lower bound we've computed above.  */
8991           high = low - 1;
8992         }
8993       else
8994         high = dwarf2_get_attr_constant_value (attr, 1);
8995     }
8996   else
8997     {
8998       attr = dwarf2_attr (die, DW_AT_count, cu);
8999       if (attr)
9000         {
9001           int count = dwarf2_get_attr_constant_value (attr, 1);
9002           high = low + count - 1;
9003         }
9004       else
9005         {
9006           /* Unspecified array length.  */
9007           high = low - 1;
9008         }
9009     }
9010
9011   /* Dwarf-2 specifications explicitly allows to create subrange types
9012      without specifying a base type.
9013      In that case, the base type must be set to the type of
9014      the lower bound, upper bound or count, in that order, if any of these
9015      three attributes references an object that has a type.
9016      If no base type is found, the Dwarf-2 specifications say that
9017      a signed integer type of size equal to the size of an address should
9018      be used.
9019      For the following C code: `extern char gdb_int [];'
9020      GCC produces an empty range DIE.
9021      FIXME: muller/2010-05-28: Possible references to object for low bound,
9022      high bound or count are not yet handled by this code.  */
9023   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9024     {
9025       struct objfile *objfile = cu->objfile;
9026       struct gdbarch *gdbarch = get_objfile_arch (objfile);
9027       int addr_size = gdbarch_addr_bit (gdbarch) /8;
9028       struct type *int_type = objfile_type (objfile)->builtin_int;
9029
9030       /* Test "int", "long int", and "long long int" objfile types,
9031          and select the first one having a size above or equal to the
9032          architecture address size.  */
9033       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9034         base_type = int_type;
9035       else
9036         {
9037           int_type = objfile_type (objfile)->builtin_long;
9038           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9039             base_type = int_type;
9040           else
9041             {
9042               int_type = objfile_type (objfile)->builtin_long_long;
9043               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9044                 base_type = int_type;
9045             }
9046         }
9047     }
9048
9049   negative_mask =
9050     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9051   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9052     low |= negative_mask;
9053   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9054     high |= negative_mask;
9055
9056   range_type = create_range_type (NULL, base_type, low, high);
9057
9058   /* Mark arrays with dynamic length at least as an array of unspecified
9059      length.  GDB could check the boundary but before it gets implemented at
9060      least allow accessing the array elements.  */
9061   if (attr && attr_form_is_block (attr))
9062     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9063
9064   /* Ada expects an empty array on no boundary attributes.  */
9065   if (attr == NULL && cu->language != language_ada)
9066     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9067
9068   name = dwarf2_name (die, cu);
9069   if (name)
9070     TYPE_NAME (range_type) = name;
9071
9072   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9073   if (attr)
9074     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9075
9076   set_die_type (die, range_type, cu);
9077
9078   /* set_die_type should be already done.  */
9079   set_descriptive_type (range_type, die, cu);
9080
9081   return range_type;
9082 }
9083
9084 static struct type *
9085 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9086 {
9087   struct type *type;
9088
9089   /* For now, we only support the C meaning of an unspecified type: void.  */
9090
9091   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9092   TYPE_NAME (type) = dwarf2_name (die, cu);
9093
9094   return set_die_type (die, type, cu);
9095 }
9096
9097 /* Trivial hash function for die_info: the hash value of a DIE
9098    is its offset in .debug_info for this objfile.  */
9099
9100 static hashval_t
9101 die_hash (const void *item)
9102 {
9103   const struct die_info *die = item;
9104
9105   return die->offset;
9106 }
9107
9108 /* Trivial comparison function for die_info structures: two DIEs
9109    are equal if they have the same offset.  */
9110
9111 static int
9112 die_eq (const void *item_lhs, const void *item_rhs)
9113 {
9114   const struct die_info *die_lhs = item_lhs;
9115   const struct die_info *die_rhs = item_rhs;
9116
9117   return die_lhs->offset == die_rhs->offset;
9118 }
9119
9120 /* Read a whole compilation unit into a linked list of dies.  */
9121
9122 static struct die_info *
9123 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9124 {
9125   struct die_reader_specs reader_specs;
9126   int read_abbrevs = 0;
9127   struct cleanup *back_to = NULL;
9128   struct die_info *die;
9129
9130   if (cu->dwarf2_abbrevs == NULL)
9131     {
9132       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9133       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9134       read_abbrevs = 1;
9135     }
9136
9137   gdb_assert (cu->die_hash == NULL);
9138   cu->die_hash
9139     = htab_create_alloc_ex (cu->header.length / 12,
9140                             die_hash,
9141                             die_eq,
9142                             NULL,
9143                             &cu->comp_unit_obstack,
9144                             hashtab_obstack_allocate,
9145                             dummy_obstack_deallocate);
9146
9147   init_cu_die_reader (&reader_specs, cu);
9148
9149   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9150
9151   if (read_abbrevs)
9152     do_cleanups (back_to);
9153
9154   return die;
9155 }
9156
9157 /* Main entry point for reading a DIE and all children.
9158    Read the DIE and dump it if requested.  */
9159
9160 static struct die_info *
9161 read_die_and_children (const struct die_reader_specs *reader,
9162                        gdb_byte *info_ptr,
9163                        gdb_byte **new_info_ptr,
9164                        struct die_info *parent)
9165 {
9166   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9167                                                      new_info_ptr, parent);
9168
9169   if (dwarf2_die_debug)
9170     {
9171       fprintf_unfiltered (gdb_stdlog,
9172                           "\nRead die from %s of %s:\n",
9173                           (reader->cu->per_cu->debug_type_section
9174                            ? ".debug_types"
9175                            : ".debug_info"),
9176                           reader->abfd->filename);
9177       dump_die (result, dwarf2_die_debug);
9178     }
9179
9180   return result;
9181 }
9182
9183 /* Read a single die and all its descendents.  Set the die's sibling
9184    field to NULL; set other fields in the die correctly, and set all
9185    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9186    location of the info_ptr after reading all of those dies.  PARENT
9187    is the parent of the die in question.  */
9188
9189 static struct die_info *
9190 read_die_and_children_1 (const struct die_reader_specs *reader,
9191                          gdb_byte *info_ptr,
9192                          gdb_byte **new_info_ptr,
9193                          struct die_info *parent)
9194 {
9195   struct die_info *die;
9196   gdb_byte *cur_ptr;
9197   int has_children;
9198
9199   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9200   if (die == NULL)
9201     {
9202       *new_info_ptr = cur_ptr;
9203       return NULL;
9204     }
9205   store_in_ref_table (die, reader->cu);
9206
9207   if (has_children)
9208     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9209   else
9210     {
9211       die->child = NULL;
9212       *new_info_ptr = cur_ptr;
9213     }
9214
9215   die->sibling = NULL;
9216   die->parent = parent;
9217   return die;
9218 }
9219
9220 /* Read a die, all of its descendents, and all of its siblings; set
9221    all of the fields of all of the dies correctly.  Arguments are as
9222    in read_die_and_children.  */
9223
9224 static struct die_info *
9225 read_die_and_siblings (const struct die_reader_specs *reader,
9226                        gdb_byte *info_ptr,
9227                        gdb_byte **new_info_ptr,
9228                        struct die_info *parent)
9229 {
9230   struct die_info *first_die, *last_sibling;
9231   gdb_byte *cur_ptr;
9232
9233   cur_ptr = info_ptr;
9234   first_die = last_sibling = NULL;
9235
9236   while (1)
9237     {
9238       struct die_info *die
9239         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9240
9241       if (die == NULL)
9242         {
9243           *new_info_ptr = cur_ptr;
9244           return first_die;
9245         }
9246
9247       if (!first_die)
9248         first_die = die;
9249       else
9250         last_sibling->sibling = die;
9251
9252       last_sibling = die;
9253     }
9254 }
9255
9256 /* Read the die from the .debug_info section buffer.  Set DIEP to
9257    point to a newly allocated die with its information, except for its
9258    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9259    whether the die has children or not.  */
9260
9261 static gdb_byte *
9262 read_full_die (const struct die_reader_specs *reader,
9263                struct die_info **diep, gdb_byte *info_ptr,
9264                int *has_children)
9265 {
9266   unsigned int abbrev_number, bytes_read, i, offset;
9267   struct abbrev_info *abbrev;
9268   struct die_info *die;
9269   struct dwarf2_cu *cu = reader->cu;
9270   bfd *abfd = reader->abfd;
9271
9272   offset = info_ptr - reader->buffer;
9273   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9274   info_ptr += bytes_read;
9275   if (!abbrev_number)
9276     {
9277       *diep = NULL;
9278       *has_children = 0;
9279       return info_ptr;
9280     }
9281
9282   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9283   if (!abbrev)
9284     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9285            abbrev_number,
9286            bfd_get_filename (abfd));
9287
9288   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9289   die->offset = offset;
9290   die->tag = abbrev->tag;
9291   die->abbrev = abbrev_number;
9292
9293   die->num_attrs = abbrev->num_attrs;
9294
9295   for (i = 0; i < abbrev->num_attrs; ++i)
9296     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9297                                abfd, info_ptr, cu);
9298
9299   *diep = die;
9300   *has_children = abbrev->has_children;
9301   return info_ptr;
9302 }
9303
9304 /* In DWARF version 2, the description of the debugging information is
9305    stored in a separate .debug_abbrev section.  Before we read any
9306    dies from a section we read in all abbreviations and install them
9307    in a hash table.  This function also sets flags in CU describing
9308    the data found in the abbrev table.  */
9309
9310 static void
9311 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9312 {
9313   struct comp_unit_head *cu_header = &cu->header;
9314   gdb_byte *abbrev_ptr;
9315   struct abbrev_info *cur_abbrev;
9316   unsigned int abbrev_number, bytes_read, abbrev_name;
9317   unsigned int abbrev_form, hash_number;
9318   struct attr_abbrev *cur_attrs;
9319   unsigned int allocated_attrs;
9320
9321   /* Initialize dwarf2 abbrevs.  */
9322   obstack_init (&cu->abbrev_obstack);
9323   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9324                                       (ABBREV_HASH_SIZE
9325                                        * sizeof (struct abbrev_info *)));
9326   memset (cu->dwarf2_abbrevs, 0,
9327           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9328
9329   dwarf2_read_section (dwarf2_per_objfile->objfile,
9330                        &dwarf2_per_objfile->abbrev);
9331   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9332   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9333   abbrev_ptr += bytes_read;
9334
9335   allocated_attrs = ATTR_ALLOC_CHUNK;
9336   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9337
9338   /* Loop until we reach an abbrev number of 0.  */
9339   while (abbrev_number)
9340     {
9341       cur_abbrev = dwarf_alloc_abbrev (cu);
9342
9343       /* read in abbrev header */
9344       cur_abbrev->number = abbrev_number;
9345       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9346       abbrev_ptr += bytes_read;
9347       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9348       abbrev_ptr += 1;
9349
9350       if (cur_abbrev->tag == DW_TAG_namespace)
9351         cu->has_namespace_info = 1;
9352
9353       /* now read in declarations */
9354       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9355       abbrev_ptr += bytes_read;
9356       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9357       abbrev_ptr += bytes_read;
9358       while (abbrev_name)
9359         {
9360           if (cur_abbrev->num_attrs == allocated_attrs)
9361             {
9362               allocated_attrs += ATTR_ALLOC_CHUNK;
9363               cur_attrs
9364                 = xrealloc (cur_attrs, (allocated_attrs
9365                                         * sizeof (struct attr_abbrev)));
9366             }
9367
9368           /* Record whether this compilation unit might have
9369              inter-compilation-unit references.  If we don't know what form
9370              this attribute will have, then it might potentially be a
9371              DW_FORM_ref_addr, so we conservatively expect inter-CU
9372              references.  */
9373
9374           if (abbrev_form == DW_FORM_ref_addr
9375               || abbrev_form == DW_FORM_indirect)
9376             cu->has_form_ref_addr = 1;
9377
9378           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9379           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9380           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9381           abbrev_ptr += bytes_read;
9382           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9383           abbrev_ptr += bytes_read;
9384         }
9385
9386       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9387                                          (cur_abbrev->num_attrs
9388                                           * sizeof (struct attr_abbrev)));
9389       memcpy (cur_abbrev->attrs, cur_attrs,
9390               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9391
9392       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9393       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9394       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9395
9396       /* Get next abbreviation.
9397          Under Irix6 the abbreviations for a compilation unit are not
9398          always properly terminated with an abbrev number of 0.
9399          Exit loop if we encounter an abbreviation which we have
9400          already read (which means we are about to read the abbreviations
9401          for the next compile unit) or if the end of the abbreviation
9402          table is reached.  */
9403       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9404           >= dwarf2_per_objfile->abbrev.size)
9405         break;
9406       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9407       abbrev_ptr += bytes_read;
9408       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9409         break;
9410     }
9411
9412   xfree (cur_attrs);
9413 }
9414
9415 /* Release the memory used by the abbrev table for a compilation unit.  */
9416
9417 static void
9418 dwarf2_free_abbrev_table (void *ptr_to_cu)
9419 {
9420   struct dwarf2_cu *cu = ptr_to_cu;
9421
9422   obstack_free (&cu->abbrev_obstack, NULL);
9423   cu->dwarf2_abbrevs = NULL;
9424 }
9425
9426 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9427
9428 static struct abbrev_info *
9429 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9430 {
9431   unsigned int hash_number;
9432   struct abbrev_info *abbrev;
9433
9434   hash_number = number % ABBREV_HASH_SIZE;
9435   abbrev = cu->dwarf2_abbrevs[hash_number];
9436
9437   while (abbrev)
9438     {
9439       if (abbrev->number == number)
9440         return abbrev;
9441       else
9442         abbrev = abbrev->next;
9443     }
9444   return NULL;
9445 }
9446
9447 /* Returns nonzero if TAG represents a type that we might generate a partial
9448    symbol for.  */
9449
9450 static int
9451 is_type_tag_for_partial (int tag)
9452 {
9453   switch (tag)
9454     {
9455 #if 0
9456     /* Some types that would be reasonable to generate partial symbols for,
9457        that we don't at present.  */
9458     case DW_TAG_array_type:
9459     case DW_TAG_file_type:
9460     case DW_TAG_ptr_to_member_type:
9461     case DW_TAG_set_type:
9462     case DW_TAG_string_type:
9463     case DW_TAG_subroutine_type:
9464 #endif
9465     case DW_TAG_base_type:
9466     case DW_TAG_class_type:
9467     case DW_TAG_interface_type:
9468     case DW_TAG_enumeration_type:
9469     case DW_TAG_structure_type:
9470     case DW_TAG_subrange_type:
9471     case DW_TAG_typedef:
9472     case DW_TAG_union_type:
9473       return 1;
9474     default:
9475       return 0;
9476     }
9477 }
9478
9479 /* Load all DIEs that are interesting for partial symbols into memory.  */
9480
9481 static struct partial_die_info *
9482 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9483                    int building_psymtab, struct dwarf2_cu *cu)
9484 {
9485   struct partial_die_info *part_die;
9486   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9487   struct abbrev_info *abbrev;
9488   unsigned int bytes_read;
9489   unsigned int load_all = 0;
9490
9491   int nesting_level = 1;
9492
9493   parent_die = NULL;
9494   last_die = NULL;
9495
9496   if (cu->per_cu && cu->per_cu->load_all_dies)
9497     load_all = 1;
9498
9499   cu->partial_dies
9500     = htab_create_alloc_ex (cu->header.length / 12,
9501                             partial_die_hash,
9502                             partial_die_eq,
9503                             NULL,
9504                             &cu->comp_unit_obstack,
9505                             hashtab_obstack_allocate,
9506                             dummy_obstack_deallocate);
9507
9508   part_die = obstack_alloc (&cu->comp_unit_obstack,
9509                             sizeof (struct partial_die_info));
9510
9511   while (1)
9512     {
9513       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9514
9515       /* A NULL abbrev means the end of a series of children.  */
9516       if (abbrev == NULL)
9517         {
9518           if (--nesting_level == 0)
9519             {
9520               /* PART_DIE was probably the last thing allocated on the
9521                  comp_unit_obstack, so we could call obstack_free
9522                  here.  We don't do that because the waste is small,
9523                  and will be cleaned up when we're done with this
9524                  compilation unit.  This way, we're also more robust
9525                  against other users of the comp_unit_obstack.  */
9526               return first_die;
9527             }
9528           info_ptr += bytes_read;
9529           last_die = parent_die;
9530           parent_die = parent_die->die_parent;
9531           continue;
9532         }
9533
9534       /* Check for template arguments.  We never save these; if
9535          they're seen, we just mark the parent, and go on our way.  */
9536       if (parent_die != NULL
9537           && cu->language == language_cplus
9538           && (abbrev->tag == DW_TAG_template_type_param
9539               || abbrev->tag == DW_TAG_template_value_param))
9540         {
9541           parent_die->has_template_arguments = 1;
9542
9543           if (!load_all)
9544             {
9545               /* We don't need a partial DIE for the template argument.  */
9546               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9547                                        cu);
9548               continue;
9549             }
9550         }
9551
9552       /* We only recurse into subprograms looking for template arguments.
9553          Skip their other children.  */
9554       if (!load_all
9555           && cu->language == language_cplus
9556           && parent_die != NULL
9557           && parent_die->tag == DW_TAG_subprogram)
9558         {
9559           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9560           continue;
9561         }
9562
9563       /* Check whether this DIE is interesting enough to save.  Normally
9564          we would not be interested in members here, but there may be
9565          later variables referencing them via DW_AT_specification (for
9566          static members).  */
9567       if (!load_all
9568           && !is_type_tag_for_partial (abbrev->tag)
9569           && abbrev->tag != DW_TAG_constant
9570           && abbrev->tag != DW_TAG_enumerator
9571           && abbrev->tag != DW_TAG_subprogram
9572           && abbrev->tag != DW_TAG_lexical_block
9573           && abbrev->tag != DW_TAG_variable
9574           && abbrev->tag != DW_TAG_namespace
9575           && abbrev->tag != DW_TAG_module
9576           && abbrev->tag != DW_TAG_member)
9577         {
9578           /* Otherwise we skip to the next sibling, if any.  */
9579           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9580           continue;
9581         }
9582
9583       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9584                                    buffer, info_ptr, cu);
9585
9586       /* This two-pass algorithm for processing partial symbols has a
9587          high cost in cache pressure.  Thus, handle some simple cases
9588          here which cover the majority of C partial symbols.  DIEs
9589          which neither have specification tags in them, nor could have
9590          specification tags elsewhere pointing at them, can simply be
9591          processed and discarded.
9592
9593          This segment is also optional; scan_partial_symbols and
9594          add_partial_symbol will handle these DIEs if we chain
9595          them in normally.  When compilers which do not emit large
9596          quantities of duplicate debug information are more common,
9597          this code can probably be removed.  */
9598
9599       /* Any complete simple types at the top level (pretty much all
9600          of them, for a language without namespaces), can be processed
9601          directly.  */
9602       if (parent_die == NULL
9603           && part_die->has_specification == 0
9604           && part_die->is_declaration == 0
9605           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9606               || part_die->tag == DW_TAG_base_type
9607               || part_die->tag == DW_TAG_subrange_type))
9608         {
9609           if (building_psymtab && part_die->name != NULL)
9610             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9611                                  VAR_DOMAIN, LOC_TYPEDEF,
9612                                  &cu->objfile->static_psymbols,
9613                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9614           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9615           continue;
9616         }
9617
9618       /* The exception for DW_TAG_typedef with has_children above is
9619          a workaround of GCC PR debug/47510.  In the case of this complaint
9620          type_name_no_tag_or_error will error on such types later.
9621
9622          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9623          it could not find the child DIEs referenced later, this is checked
9624          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9625
9626       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9627         complaint (&symfile_complaints,
9628                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9629                      "- DIE at 0x%x [in module %s]"),
9630                    part_die->offset, cu->objfile->name);
9631
9632       /* If we're at the second level, and we're an enumerator, and
9633          our parent has no specification (meaning possibly lives in a
9634          namespace elsewhere), then we can add the partial symbol now
9635          instead of queueing it.  */
9636       if (part_die->tag == DW_TAG_enumerator
9637           && parent_die != NULL
9638           && parent_die->die_parent == NULL
9639           && parent_die->tag == DW_TAG_enumeration_type
9640           && parent_die->has_specification == 0)
9641         {
9642           if (part_die->name == NULL)
9643             complaint (&symfile_complaints,
9644                        _("malformed enumerator DIE ignored"));
9645           else if (building_psymtab)
9646             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9647                                  VAR_DOMAIN, LOC_CONST,
9648                                  (cu->language == language_cplus
9649                                   || cu->language == language_java)
9650                                  ? &cu->objfile->global_psymbols
9651                                  : &cu->objfile->static_psymbols,
9652                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
9653
9654           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9655           continue;
9656         }
9657
9658       /* We'll save this DIE so link it in.  */
9659       part_die->die_parent = parent_die;
9660       part_die->die_sibling = NULL;
9661       part_die->die_child = NULL;
9662
9663       if (last_die && last_die == parent_die)
9664         last_die->die_child = part_die;
9665       else if (last_die)
9666         last_die->die_sibling = part_die;
9667
9668       last_die = part_die;
9669
9670       if (first_die == NULL)
9671         first_die = part_die;
9672
9673       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9674          find interesting need to be in the hash table, because we
9675          also have the parent/sibling/child chains; only those that we
9676          might refer to by offset later during partial symbol reading.
9677
9678          For now this means things that might have be the target of a
9679          DW_AT_specification, DW_AT_abstract_origin, or
9680          DW_AT_extension.  DW_AT_extension will refer only to
9681          namespaces; DW_AT_abstract_origin refers to functions (and
9682          many things under the function DIE, but we do not recurse
9683          into function DIEs during partial symbol reading) and
9684          possibly variables as well; DW_AT_specification refers to
9685          declarations.  Declarations ought to have the DW_AT_declaration
9686          flag.  It happens that GCC forgets to put it in sometimes, but
9687          only for functions, not for types.
9688
9689          Adding more things than necessary to the hash table is harmless
9690          except for the performance cost.  Adding too few will result in
9691          wasted time in find_partial_die, when we reread the compilation
9692          unit with load_all_dies set.  */
9693
9694       if (load_all
9695           || abbrev->tag == DW_TAG_constant
9696           || abbrev->tag == DW_TAG_subprogram
9697           || abbrev->tag == DW_TAG_variable
9698           || abbrev->tag == DW_TAG_namespace
9699           || part_die->is_declaration)
9700         {
9701           void **slot;
9702
9703           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9704                                            part_die->offset, INSERT);
9705           *slot = part_die;
9706         }
9707
9708       part_die = obstack_alloc (&cu->comp_unit_obstack,
9709                                 sizeof (struct partial_die_info));
9710
9711       /* For some DIEs we want to follow their children (if any).  For C
9712          we have no reason to follow the children of structures; for other
9713          languages we have to, so that we can get at method physnames
9714          to infer fully qualified class names, for DW_AT_specification,
9715          and for C++ template arguments.  For C++, we also look one level
9716          inside functions to find template arguments (if the name of the
9717          function does not already contain the template arguments).
9718
9719          For Ada, we need to scan the children of subprograms and lexical
9720          blocks as well because Ada allows the definition of nested
9721          entities that could be interesting for the debugger, such as
9722          nested subprograms for instance.  */
9723       if (last_die->has_children
9724           && (load_all
9725               || last_die->tag == DW_TAG_namespace
9726               || last_die->tag == DW_TAG_module
9727               || last_die->tag == DW_TAG_enumeration_type
9728               || (cu->language == language_cplus
9729                   && last_die->tag == DW_TAG_subprogram
9730                   && (last_die->name == NULL
9731                       || strchr (last_die->name, '<') == NULL))
9732               || (cu->language != language_c
9733                   && (last_die->tag == DW_TAG_class_type
9734                       || last_die->tag == DW_TAG_interface_type
9735                       || last_die->tag == DW_TAG_structure_type
9736                       || last_die->tag == DW_TAG_union_type))
9737               || (cu->language == language_ada
9738                   && (last_die->tag == DW_TAG_subprogram
9739                       || last_die->tag == DW_TAG_lexical_block))))
9740         {
9741           nesting_level++;
9742           parent_die = last_die;
9743           continue;
9744         }
9745
9746       /* Otherwise we skip to the next sibling, if any.  */
9747       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9748
9749       /* Back to the top, do it again.  */
9750     }
9751 }
9752
9753 /* Read a minimal amount of information into the minimal die structure.  */
9754
9755 static gdb_byte *
9756 read_partial_die (struct partial_die_info *part_die,
9757                   struct abbrev_info *abbrev,
9758                   unsigned int abbrev_len, bfd *abfd,
9759                   gdb_byte *buffer, gdb_byte *info_ptr,
9760                   struct dwarf2_cu *cu)
9761 {
9762   unsigned int i;
9763   struct attribute attr;
9764   int has_low_pc_attr = 0;
9765   int has_high_pc_attr = 0;
9766
9767   memset (part_die, 0, sizeof (struct partial_die_info));
9768
9769   part_die->offset = info_ptr - buffer;
9770
9771   info_ptr += abbrev_len;
9772
9773   if (abbrev == NULL)
9774     return info_ptr;
9775
9776   part_die->tag = abbrev->tag;
9777   part_die->has_children = abbrev->has_children;
9778
9779   for (i = 0; i < abbrev->num_attrs; ++i)
9780     {
9781       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9782
9783       /* Store the data if it is of an attribute we want to keep in a
9784          partial symbol table.  */
9785       switch (attr.name)
9786         {
9787         case DW_AT_name:
9788           switch (part_die->tag)
9789             {
9790             case DW_TAG_compile_unit:
9791             case DW_TAG_type_unit:
9792               /* Compilation units have a DW_AT_name that is a filename, not
9793                  a source language identifier.  */
9794             case DW_TAG_enumeration_type:
9795             case DW_TAG_enumerator:
9796               /* These tags always have simple identifiers already; no need
9797                  to canonicalize them.  */
9798               part_die->name = DW_STRING (&attr);
9799               break;
9800             default:
9801               part_die->name
9802                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9803                                             &cu->objfile->objfile_obstack);
9804               break;
9805             }
9806           break;
9807         case DW_AT_linkage_name:
9808         case DW_AT_MIPS_linkage_name:
9809           /* Note that both forms of linkage name might appear.  We
9810              assume they will be the same, and we only store the last
9811              one we see.  */
9812           if (cu->language == language_ada)
9813             part_die->name = DW_STRING (&attr);
9814           part_die->linkage_name = DW_STRING (&attr);
9815           break;
9816         case DW_AT_low_pc:
9817           has_low_pc_attr = 1;
9818           part_die->lowpc = DW_ADDR (&attr);
9819           break;
9820         case DW_AT_high_pc:
9821           has_high_pc_attr = 1;
9822           part_die->highpc = DW_ADDR (&attr);
9823           break;
9824         case DW_AT_location:
9825           /* Support the .debug_loc offsets.  */
9826           if (attr_form_is_block (&attr))
9827             {
9828                part_die->locdesc = DW_BLOCK (&attr);
9829             }
9830           else if (attr_form_is_section_offset (&attr))
9831             {
9832               dwarf2_complex_location_expr_complaint ();
9833             }
9834           else
9835             {
9836               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9837                                                      "partial symbol information");
9838             }
9839           break;
9840         case DW_AT_external:
9841           part_die->is_external = DW_UNSND (&attr);
9842           break;
9843         case DW_AT_declaration:
9844           part_die->is_declaration = DW_UNSND (&attr);
9845           break;
9846         case DW_AT_type:
9847           part_die->has_type = 1;
9848           break;
9849         case DW_AT_abstract_origin:
9850         case DW_AT_specification:
9851         case DW_AT_extension:
9852           part_die->has_specification = 1;
9853           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9854           break;
9855         case DW_AT_sibling:
9856           /* Ignore absolute siblings, they might point outside of
9857              the current compile unit.  */
9858           if (attr.form == DW_FORM_ref_addr)
9859             complaint (&symfile_complaints,
9860                        _("ignoring absolute DW_AT_sibling"));
9861           else
9862             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9863           break;
9864         case DW_AT_byte_size:
9865           part_die->has_byte_size = 1;
9866           break;
9867         case DW_AT_calling_convention:
9868           /* DWARF doesn't provide a way to identify a program's source-level
9869              entry point.  DW_AT_calling_convention attributes are only meant
9870              to describe functions' calling conventions.
9871
9872              However, because it's a necessary piece of information in
9873              Fortran, and because DW_CC_program is the only piece of debugging
9874              information whose definition refers to a 'main program' at all,
9875              several compilers have begun marking Fortran main programs with
9876              DW_CC_program --- even when those functions use the standard
9877              calling conventions.
9878
9879              So until DWARF specifies a way to provide this information and
9880              compilers pick up the new representation, we'll support this
9881              practice.  */
9882           if (DW_UNSND (&attr) == DW_CC_program
9883               && cu->language == language_fortran)
9884             {
9885               set_main_name (part_die->name);
9886
9887               /* As this DIE has a static linkage the name would be difficult
9888                  to look up later.  */
9889               language_of_main = language_fortran;
9890             }
9891           break;
9892         default:
9893           break;
9894         }
9895     }
9896
9897   if (has_low_pc_attr && has_high_pc_attr)
9898     {
9899       /* When using the GNU linker, .gnu.linkonce. sections are used to
9900          eliminate duplicate copies of functions and vtables and such.
9901          The linker will arbitrarily choose one and discard the others.
9902          The AT_*_pc values for such functions refer to local labels in
9903          these sections.  If the section from that file was discarded, the
9904          labels are not in the output, so the relocs get a value of 0.
9905          If this is a discarded function, mark the pc bounds as invalid,
9906          so that GDB will ignore it.  */
9907       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9908         {
9909           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9910
9911           complaint (&symfile_complaints,
9912                      _("DW_AT_low_pc %s is zero "
9913                        "for DIE at 0x%x [in module %s]"),
9914                      paddress (gdbarch, part_die->lowpc),
9915                      part_die->offset, cu->objfile->name);
9916         }
9917       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9918       else if (part_die->lowpc >= part_die->highpc)
9919         {
9920           struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9921
9922           complaint (&symfile_complaints,
9923                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9924                        "for DIE at 0x%x [in module %s]"),
9925                      paddress (gdbarch, part_die->lowpc),
9926                      paddress (gdbarch, part_die->highpc),
9927                      part_die->offset, cu->objfile->name);
9928         }
9929       else
9930         part_die->has_pc_info = 1;
9931     }
9932
9933   return info_ptr;
9934 }
9935
9936 /* Find a cached partial DIE at OFFSET in CU.  */
9937
9938 static struct partial_die_info *
9939 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9940 {
9941   struct partial_die_info *lookup_die = NULL;
9942   struct partial_die_info part_die;
9943
9944   part_die.offset = offset;
9945   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9946
9947   return lookup_die;
9948 }
9949
9950 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9951    except in the case of .debug_types DIEs which do not reference
9952    outside their CU (they do however referencing other types via
9953    DW_FORM_ref_sig8).  */
9954
9955 static struct partial_die_info *
9956 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9957 {
9958   struct dwarf2_per_cu_data *per_cu = NULL;
9959   struct partial_die_info *pd = NULL;
9960
9961   if (cu->per_cu->debug_type_section)
9962     {
9963       pd = find_partial_die_in_comp_unit (offset, cu);
9964       if (pd != NULL)
9965         return pd;
9966       goto not_found;
9967     }
9968
9969   if (offset_in_cu_p (&cu->header, offset))
9970     {
9971       pd = find_partial_die_in_comp_unit (offset, cu);
9972       if (pd != NULL)
9973         return pd;
9974     }
9975
9976   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9977
9978   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9979     load_partial_comp_unit (per_cu, cu->objfile);
9980
9981   per_cu->cu->last_used = 0;
9982   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9983
9984   if (pd == NULL && per_cu->load_all_dies == 0)
9985     {
9986       struct cleanup *back_to;
9987       struct partial_die_info comp_unit_die;
9988       struct abbrev_info *abbrev;
9989       unsigned int bytes_read;
9990       char *info_ptr;
9991
9992       per_cu->load_all_dies = 1;
9993
9994       /* Re-read the DIEs.  */
9995       back_to = make_cleanup (null_cleanup, 0);
9996       if (per_cu->cu->dwarf2_abbrevs == NULL)
9997         {
9998           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9999           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10000         }
10001       info_ptr = (dwarf2_per_objfile->info.buffer
10002                   + per_cu->cu->header.offset
10003                   + per_cu->cu->header.first_die_offset);
10004       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10005       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10006                                    per_cu->cu->objfile->obfd,
10007                                    dwarf2_per_objfile->info.buffer, info_ptr,
10008                                    per_cu->cu);
10009       if (comp_unit_die.has_children)
10010         load_partial_dies (per_cu->cu->objfile->obfd,
10011                            dwarf2_per_objfile->info.buffer, info_ptr,
10012                            0, per_cu->cu);
10013       do_cleanups (back_to);
10014
10015       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10016     }
10017
10018  not_found:
10019
10020   if (pd == NULL)
10021     internal_error (__FILE__, __LINE__,
10022                     _("could not find partial DIE 0x%x "
10023                       "in cache [from module %s]\n"),
10024                     offset, bfd_get_filename (cu->objfile->obfd));
10025   return pd;
10026 }
10027
10028 /* See if we can figure out if the class lives in a namespace.  We do
10029    this by looking for a member function; its demangled name will
10030    contain namespace info, if there is any.  */
10031
10032 static void
10033 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10034                                   struct dwarf2_cu *cu)
10035 {
10036   /* NOTE: carlton/2003-10-07: Getting the info this way changes
10037      what template types look like, because the demangler
10038      frequently doesn't give the same name as the debug info.  We
10039      could fix this by only using the demangled name to get the
10040      prefix (but see comment in read_structure_type).  */
10041
10042   struct partial_die_info *real_pdi;
10043   struct partial_die_info *child_pdi;
10044
10045   /* If this DIE (this DIE's specification, if any) has a parent, then
10046      we should not do this.  We'll prepend the parent's fully qualified
10047      name when we create the partial symbol.  */
10048
10049   real_pdi = struct_pdi;
10050   while (real_pdi->has_specification)
10051     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10052
10053   if (real_pdi->die_parent != NULL)
10054     return;
10055
10056   for (child_pdi = struct_pdi->die_child;
10057        child_pdi != NULL;
10058        child_pdi = child_pdi->die_sibling)
10059     {
10060       if (child_pdi->tag == DW_TAG_subprogram
10061           && child_pdi->linkage_name != NULL)
10062         {
10063           char *actual_class_name
10064             = language_class_name_from_physname (cu->language_defn,
10065                                                  child_pdi->linkage_name);
10066           if (actual_class_name != NULL)
10067             {
10068               struct_pdi->name
10069                 = obsavestring (actual_class_name,
10070                                 strlen (actual_class_name),
10071                                 &cu->objfile->objfile_obstack);
10072               xfree (actual_class_name);
10073             }
10074           break;
10075         }
10076     }
10077 }
10078
10079 /* Adjust PART_DIE before generating a symbol for it.  This function
10080    may set the is_external flag or change the DIE's name.  */
10081
10082 static void
10083 fixup_partial_die (struct partial_die_info *part_die,
10084                    struct dwarf2_cu *cu)
10085 {
10086   /* Once we've fixed up a die, there's no point in doing so again.
10087      This also avoids a memory leak if we were to call
10088      guess_partial_die_structure_name multiple times.  */
10089   if (part_die->fixup_called)
10090     return;
10091
10092   /* If we found a reference attribute and the DIE has no name, try
10093      to find a name in the referred to DIE.  */
10094
10095   if (part_die->name == NULL && part_die->has_specification)
10096     {
10097       struct partial_die_info *spec_die;
10098
10099       spec_die = find_partial_die (part_die->spec_offset, cu);
10100
10101       fixup_partial_die (spec_die, cu);
10102
10103       if (spec_die->name)
10104         {
10105           part_die->name = spec_die->name;
10106
10107           /* Copy DW_AT_external attribute if it is set.  */
10108           if (spec_die->is_external)
10109             part_die->is_external = spec_die->is_external;
10110         }
10111     }
10112
10113   /* Set default names for some unnamed DIEs.  */
10114
10115   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10116     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10117
10118   /* If there is no parent die to provide a namespace, and there are
10119      children, see if we can determine the namespace from their linkage
10120      name.
10121      NOTE: We need to do this even if cu->has_namespace_info != 0.
10122      gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
10123   if (cu->language == language_cplus
10124       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10125       && part_die->die_parent == NULL
10126       && part_die->has_children
10127       && (part_die->tag == DW_TAG_class_type
10128           || part_die->tag == DW_TAG_structure_type
10129           || part_die->tag == DW_TAG_union_type))
10130     guess_partial_die_structure_name (part_die, cu);
10131
10132   /* GCC might emit a nameless struct or union that has a linkage
10133      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10134   if (part_die->name == NULL
10135       && (part_die->tag == DW_TAG_class_type
10136           || part_die->tag == DW_TAG_interface_type
10137           || part_die->tag == DW_TAG_structure_type
10138           || part_die->tag == DW_TAG_union_type)
10139       && part_die->linkage_name != NULL)
10140     {
10141       char *demangled;
10142
10143       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10144       if (demangled)
10145         {
10146           const char *base;
10147
10148           /* Strip any leading namespaces/classes, keep only the base name.
10149              DW_AT_name for named DIEs does not contain the prefixes.  */
10150           base = strrchr (demangled, ':');
10151           if (base && base > demangled && base[-1] == ':')
10152             base++;
10153           else
10154             base = demangled;
10155
10156           part_die->name = obsavestring (base, strlen (base),
10157                                          &cu->objfile->objfile_obstack);
10158           xfree (demangled);
10159         }
10160     }
10161
10162   part_die->fixup_called = 1;
10163 }
10164
10165 /* Read an attribute value described by an attribute form.  */
10166
10167 static gdb_byte *
10168 read_attribute_value (struct attribute *attr, unsigned form,
10169                       bfd *abfd, gdb_byte *info_ptr,
10170                       struct dwarf2_cu *cu)
10171 {
10172   struct comp_unit_head *cu_header = &cu->header;
10173   unsigned int bytes_read;
10174   struct dwarf_block *blk;
10175
10176   attr->form = form;
10177   switch (form)
10178     {
10179     case DW_FORM_ref_addr:
10180       if (cu->header.version == 2)
10181         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10182       else
10183         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10184                                       &cu->header, &bytes_read);
10185       info_ptr += bytes_read;
10186       break;
10187     case DW_FORM_addr:
10188       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10189       info_ptr += bytes_read;
10190       break;
10191     case DW_FORM_block2:
10192       blk = dwarf_alloc_block (cu);
10193       blk->size = read_2_bytes (abfd, info_ptr);
10194       info_ptr += 2;
10195       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10196       info_ptr += blk->size;
10197       DW_BLOCK (attr) = blk;
10198       break;
10199     case DW_FORM_block4:
10200       blk = dwarf_alloc_block (cu);
10201       blk->size = read_4_bytes (abfd, info_ptr);
10202       info_ptr += 4;
10203       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10204       info_ptr += blk->size;
10205       DW_BLOCK (attr) = blk;
10206       break;
10207     case DW_FORM_data2:
10208       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10209       info_ptr += 2;
10210       break;
10211     case DW_FORM_data4:
10212       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10213       info_ptr += 4;
10214       break;
10215     case DW_FORM_data8:
10216       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10217       info_ptr += 8;
10218       break;
10219     case DW_FORM_sec_offset:
10220       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10221       info_ptr += bytes_read;
10222       break;
10223     case DW_FORM_string:
10224       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10225       DW_STRING_IS_CANONICAL (attr) = 0;
10226       info_ptr += bytes_read;
10227       break;
10228     case DW_FORM_strp:
10229       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10230                                                &bytes_read);
10231       DW_STRING_IS_CANONICAL (attr) = 0;
10232       info_ptr += bytes_read;
10233       break;
10234     case DW_FORM_exprloc:
10235     case DW_FORM_block:
10236       blk = dwarf_alloc_block (cu);
10237       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10238       info_ptr += bytes_read;
10239       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10240       info_ptr += blk->size;
10241       DW_BLOCK (attr) = blk;
10242       break;
10243     case DW_FORM_block1:
10244       blk = dwarf_alloc_block (cu);
10245       blk->size = read_1_byte (abfd, info_ptr);
10246       info_ptr += 1;
10247       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10248       info_ptr += blk->size;
10249       DW_BLOCK (attr) = blk;
10250       break;
10251     case DW_FORM_data1:
10252       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10253       info_ptr += 1;
10254       break;
10255     case DW_FORM_flag:
10256       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10257       info_ptr += 1;
10258       break;
10259     case DW_FORM_flag_present:
10260       DW_UNSND (attr) = 1;
10261       break;
10262     case DW_FORM_sdata:
10263       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10264       info_ptr += bytes_read;
10265       break;
10266     case DW_FORM_udata:
10267       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10268       info_ptr += bytes_read;
10269       break;
10270     case DW_FORM_ref1:
10271       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10272       info_ptr += 1;
10273       break;
10274     case DW_FORM_ref2:
10275       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10276       info_ptr += 2;
10277       break;
10278     case DW_FORM_ref4:
10279       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10280       info_ptr += 4;
10281       break;
10282     case DW_FORM_ref8:
10283       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10284       info_ptr += 8;
10285       break;
10286     case DW_FORM_ref_sig8:
10287       /* Convert the signature to something we can record in DW_UNSND
10288          for later lookup.
10289          NOTE: This is NULL if the type wasn't found.  */
10290       DW_SIGNATURED_TYPE (attr) =
10291         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10292       info_ptr += 8;
10293       break;
10294     case DW_FORM_ref_udata:
10295       DW_ADDR (attr) = (cu->header.offset
10296                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10297       info_ptr += bytes_read;
10298       break;
10299     case DW_FORM_indirect:
10300       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10301       info_ptr += bytes_read;
10302       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10303       break;
10304     default:
10305       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10306              dwarf_form_name (form),
10307              bfd_get_filename (abfd));
10308     }
10309
10310   /* We have seen instances where the compiler tried to emit a byte
10311      size attribute of -1 which ended up being encoded as an unsigned
10312      0xffffffff.  Although 0xffffffff is technically a valid size value,
10313      an object of this size seems pretty unlikely so we can relatively
10314      safely treat these cases as if the size attribute was invalid and
10315      treat them as zero by default.  */
10316   if (attr->name == DW_AT_byte_size
10317       && form == DW_FORM_data4
10318       && DW_UNSND (attr) >= 0xffffffff)
10319     {
10320       complaint
10321         (&symfile_complaints,
10322          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10323          hex_string (DW_UNSND (attr)));
10324       DW_UNSND (attr) = 0;
10325     }
10326
10327   return info_ptr;
10328 }
10329
10330 /* Read an attribute described by an abbreviated attribute.  */
10331
10332 static gdb_byte *
10333 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10334                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10335 {
10336   attr->name = abbrev->name;
10337   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10338 }
10339
10340 /* Read dwarf information from a buffer.  */
10341
10342 static unsigned int
10343 read_1_byte (bfd *abfd, gdb_byte *buf)
10344 {
10345   return bfd_get_8 (abfd, buf);
10346 }
10347
10348 static int
10349 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10350 {
10351   return bfd_get_signed_8 (abfd, buf);
10352 }
10353
10354 static unsigned int
10355 read_2_bytes (bfd *abfd, gdb_byte *buf)
10356 {
10357   return bfd_get_16 (abfd, buf);
10358 }
10359
10360 static int
10361 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10362 {
10363   return bfd_get_signed_16 (abfd, buf);
10364 }
10365
10366 static unsigned int
10367 read_4_bytes (bfd *abfd, gdb_byte *buf)
10368 {
10369   return bfd_get_32 (abfd, buf);
10370 }
10371
10372 static int
10373 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10374 {
10375   return bfd_get_signed_32 (abfd, buf);
10376 }
10377
10378 static ULONGEST
10379 read_8_bytes (bfd *abfd, gdb_byte *buf)
10380 {
10381   return bfd_get_64 (abfd, buf);
10382 }
10383
10384 static CORE_ADDR
10385 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10386               unsigned int *bytes_read)
10387 {
10388   struct comp_unit_head *cu_header = &cu->header;
10389   CORE_ADDR retval = 0;
10390
10391   if (cu_header->signed_addr_p)
10392     {
10393       switch (cu_header->addr_size)
10394         {
10395         case 2:
10396           retval = bfd_get_signed_16 (abfd, buf);
10397           break;
10398         case 4:
10399           retval = bfd_get_signed_32 (abfd, buf);
10400           break;
10401         case 8:
10402           retval = bfd_get_signed_64 (abfd, buf);
10403           break;
10404         default:
10405           internal_error (__FILE__, __LINE__,
10406                           _("read_address: bad switch, signed [in module %s]"),
10407                           bfd_get_filename (abfd));
10408         }
10409     }
10410   else
10411     {
10412       switch (cu_header->addr_size)
10413         {
10414         case 2:
10415           retval = bfd_get_16 (abfd, buf);
10416           break;
10417         case 4:
10418           retval = bfd_get_32 (abfd, buf);
10419           break;
10420         case 8:
10421           retval = bfd_get_64 (abfd, buf);
10422           break;
10423         default:
10424           internal_error (__FILE__, __LINE__,
10425                           _("read_address: bad switch, "
10426                             "unsigned [in module %s]"),
10427                           bfd_get_filename (abfd));
10428         }
10429     }
10430
10431   *bytes_read = cu_header->addr_size;
10432   return retval;
10433 }
10434
10435 /* Read the initial length from a section.  The (draft) DWARF 3
10436    specification allows the initial length to take up either 4 bytes
10437    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10438    bytes describe the length and all offsets will be 8 bytes in length
10439    instead of 4.
10440
10441    An older, non-standard 64-bit format is also handled by this
10442    function.  The older format in question stores the initial length
10443    as an 8-byte quantity without an escape value.  Lengths greater
10444    than 2^32 aren't very common which means that the initial 4 bytes
10445    is almost always zero.  Since a length value of zero doesn't make
10446    sense for the 32-bit format, this initial zero can be considered to
10447    be an escape value which indicates the presence of the older 64-bit
10448    format.  As written, the code can't detect (old format) lengths
10449    greater than 4GB.  If it becomes necessary to handle lengths
10450    somewhat larger than 4GB, we could allow other small values (such
10451    as the non-sensical values of 1, 2, and 3) to also be used as
10452    escape values indicating the presence of the old format.
10453
10454    The value returned via bytes_read should be used to increment the
10455    relevant pointer after calling read_initial_length().
10456
10457    [ Note:  read_initial_length() and read_offset() are based on the
10458      document entitled "DWARF Debugging Information Format", revision
10459      3, draft 8, dated November 19, 2001.  This document was obtained
10460      from:
10461
10462         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10463
10464      This document is only a draft and is subject to change.  (So beware.)
10465
10466      Details regarding the older, non-standard 64-bit format were
10467      determined empirically by examining 64-bit ELF files produced by
10468      the SGI toolchain on an IRIX 6.5 machine.
10469
10470      - Kevin, July 16, 2002
10471    ] */
10472
10473 static LONGEST
10474 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10475 {
10476   LONGEST length = bfd_get_32 (abfd, buf);
10477
10478   if (length == 0xffffffff)
10479     {
10480       length = bfd_get_64 (abfd, buf + 4);
10481       *bytes_read = 12;
10482     }
10483   else if (length == 0)
10484     {
10485       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10486       length = bfd_get_64 (abfd, buf);
10487       *bytes_read = 8;
10488     }
10489   else
10490     {
10491       *bytes_read = 4;
10492     }
10493
10494   return length;
10495 }
10496
10497 /* Cover function for read_initial_length.
10498    Returns the length of the object at BUF, and stores the size of the
10499    initial length in *BYTES_READ and stores the size that offsets will be in
10500    *OFFSET_SIZE.
10501    If the initial length size is not equivalent to that specified in
10502    CU_HEADER then issue a complaint.
10503    This is useful when reading non-comp-unit headers.  */
10504
10505 static LONGEST
10506 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10507                                         const struct comp_unit_head *cu_header,
10508                                         unsigned int *bytes_read,
10509                                         unsigned int *offset_size)
10510 {
10511   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10512
10513   gdb_assert (cu_header->initial_length_size == 4
10514               || cu_header->initial_length_size == 8
10515               || cu_header->initial_length_size == 12);
10516
10517   if (cu_header->initial_length_size != *bytes_read)
10518     complaint (&symfile_complaints,
10519                _("intermixed 32-bit and 64-bit DWARF sections"));
10520
10521   *offset_size = (*bytes_read == 4) ? 4 : 8;
10522   return length;
10523 }
10524
10525 /* Read an offset from the data stream.  The size of the offset is
10526    given by cu_header->offset_size.  */
10527
10528 static LONGEST
10529 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10530              unsigned int *bytes_read)
10531 {
10532   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10533
10534   *bytes_read = cu_header->offset_size;
10535   return offset;
10536 }
10537
10538 /* Read an offset from the data stream.  */
10539
10540 static LONGEST
10541 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10542 {
10543   LONGEST retval = 0;
10544
10545   switch (offset_size)
10546     {
10547     case 4:
10548       retval = bfd_get_32 (abfd, buf);
10549       break;
10550     case 8:
10551       retval = bfd_get_64 (abfd, buf);
10552       break;
10553     default:
10554       internal_error (__FILE__, __LINE__,
10555                       _("read_offset_1: bad switch [in module %s]"),
10556                       bfd_get_filename (abfd));
10557     }
10558
10559   return retval;
10560 }
10561
10562 static gdb_byte *
10563 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10564 {
10565   /* If the size of a host char is 8 bits, we can return a pointer
10566      to the buffer, otherwise we have to copy the data to a buffer
10567      allocated on the temporary obstack.  */
10568   gdb_assert (HOST_CHAR_BIT == 8);
10569   return buf;
10570 }
10571
10572 static char *
10573 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10574 {
10575   /* If the size of a host char is 8 bits, we can return a pointer
10576      to the string, otherwise we have to copy the string to a buffer
10577      allocated on the temporary obstack.  */
10578   gdb_assert (HOST_CHAR_BIT == 8);
10579   if (*buf == '\0')
10580     {
10581       *bytes_read_ptr = 1;
10582       return NULL;
10583     }
10584   *bytes_read_ptr = strlen ((char *) buf) + 1;
10585   return (char *) buf;
10586 }
10587
10588 static char *
10589 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10590 {
10591   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10592   if (dwarf2_per_objfile->str.buffer == NULL)
10593     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10594            bfd_get_filename (abfd));
10595   if (str_offset >= dwarf2_per_objfile->str.size)
10596     error (_("DW_FORM_strp pointing outside of "
10597              ".debug_str section [in module %s]"),
10598            bfd_get_filename (abfd));
10599   gdb_assert (HOST_CHAR_BIT == 8);
10600   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10601     return NULL;
10602   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10603 }
10604
10605 static char *
10606 read_indirect_string (bfd *abfd, gdb_byte *buf,
10607                       const struct comp_unit_head *cu_header,
10608                       unsigned int *bytes_read_ptr)
10609 {
10610   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10611
10612   return read_indirect_string_at_offset (abfd, str_offset);
10613 }
10614
10615 static unsigned long
10616 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10617 {
10618   unsigned long result;
10619   unsigned int num_read;
10620   int i, shift;
10621   unsigned char byte;
10622
10623   result = 0;
10624   shift = 0;
10625   num_read = 0;
10626   i = 0;
10627   while (1)
10628     {
10629       byte = bfd_get_8 (abfd, buf);
10630       buf++;
10631       num_read++;
10632       result |= ((unsigned long)(byte & 127) << shift);
10633       if ((byte & 128) == 0)
10634         {
10635           break;
10636         }
10637       shift += 7;
10638     }
10639   *bytes_read_ptr = num_read;
10640   return result;
10641 }
10642
10643 static long
10644 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10645 {
10646   long result;
10647   int i, shift, num_read;
10648   unsigned char byte;
10649
10650   result = 0;
10651   shift = 0;
10652   num_read = 0;
10653   i = 0;
10654   while (1)
10655     {
10656       byte = bfd_get_8 (abfd, buf);
10657       buf++;
10658       num_read++;
10659       result |= ((long)(byte & 127) << shift);
10660       shift += 7;
10661       if ((byte & 128) == 0)
10662         {
10663           break;
10664         }
10665     }
10666   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10667     result |= -(((long)1) << shift);
10668   *bytes_read_ptr = num_read;
10669   return result;
10670 }
10671
10672 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10673
10674 static gdb_byte *
10675 skip_leb128 (bfd *abfd, gdb_byte *buf)
10676 {
10677   int byte;
10678
10679   while (1)
10680     {
10681       byte = bfd_get_8 (abfd, buf);
10682       buf++;
10683       if ((byte & 128) == 0)
10684         return buf;
10685     }
10686 }
10687
10688 static void
10689 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10690 {
10691   switch (lang)
10692     {
10693     case DW_LANG_C89:
10694     case DW_LANG_C99:
10695     case DW_LANG_C:
10696       cu->language = language_c;
10697       break;
10698     case DW_LANG_C_plus_plus:
10699       cu->language = language_cplus;
10700       break;
10701     case DW_LANG_D:
10702       cu->language = language_d;
10703       break;
10704     case DW_LANG_Fortran77:
10705     case DW_LANG_Fortran90:
10706     case DW_LANG_Fortran95:
10707       cu->language = language_fortran;
10708       break;
10709     case DW_LANG_Mips_Assembler:
10710       cu->language = language_asm;
10711       break;
10712     case DW_LANG_Java:
10713       cu->language = language_java;
10714       break;
10715     case DW_LANG_Ada83:
10716     case DW_LANG_Ada95:
10717       cu->language = language_ada;
10718       break;
10719     case DW_LANG_Modula2:
10720       cu->language = language_m2;
10721       break;
10722     case DW_LANG_Pascal83:
10723       cu->language = language_pascal;
10724       break;
10725     case DW_LANG_ObjC:
10726       cu->language = language_objc;
10727       break;
10728     case DW_LANG_Cobol74:
10729     case DW_LANG_Cobol85:
10730     default:
10731       cu->language = language_minimal;
10732       break;
10733     }
10734   cu->language_defn = language_def (cu->language);
10735 }
10736
10737 /* Return the named attribute or NULL if not there.  */
10738
10739 static struct attribute *
10740 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10741 {
10742   unsigned int i;
10743   struct attribute *spec = NULL;
10744
10745   for (i = 0; i < die->num_attrs; ++i)
10746     {
10747       if (die->attrs[i].name == name)
10748         return &die->attrs[i];
10749       if (die->attrs[i].name == DW_AT_specification
10750           || die->attrs[i].name == DW_AT_abstract_origin)
10751         spec = &die->attrs[i];
10752     }
10753
10754   if (spec)
10755     {
10756       die = follow_die_ref (die, spec, &cu);
10757       return dwarf2_attr (die, name, cu);
10758     }
10759
10760   return NULL;
10761 }
10762
10763 /* Return the named attribute or NULL if not there,
10764    but do not follow DW_AT_specification, etc.
10765    This is for use in contexts where we're reading .debug_types dies.
10766    Following DW_AT_specification, DW_AT_abstract_origin will take us
10767    back up the chain, and we want to go down.  */
10768
10769 static struct attribute *
10770 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10771                        struct dwarf2_cu *cu)
10772 {
10773   unsigned int i;
10774
10775   for (i = 0; i < die->num_attrs; ++i)
10776     if (die->attrs[i].name == name)
10777       return &die->attrs[i];
10778
10779   return NULL;
10780 }
10781
10782 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10783    and holds a non-zero value.  This function should only be used for
10784    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10785
10786 static int
10787 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10788 {
10789   struct attribute *attr = dwarf2_attr (die, name, cu);
10790
10791   return (attr && DW_UNSND (attr));
10792 }
10793
10794 static int
10795 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10796 {
10797   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10798      which value is non-zero.  However, we have to be careful with
10799      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10800      (via dwarf2_flag_true_p) follows this attribute.  So we may
10801      end up accidently finding a declaration attribute that belongs
10802      to a different DIE referenced by the specification attribute,
10803      even though the given DIE does not have a declaration attribute.  */
10804   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10805           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10806 }
10807
10808 /* Return the die giving the specification for DIE, if there is
10809    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10810    containing the return value on output.  If there is no
10811    specification, but there is an abstract origin, that is
10812    returned.  */
10813
10814 static struct die_info *
10815 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10816 {
10817   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10818                                              *spec_cu);
10819
10820   if (spec_attr == NULL)
10821     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10822
10823   if (spec_attr == NULL)
10824     return NULL;
10825   else
10826     return follow_die_ref (die, spec_attr, spec_cu);
10827 }
10828
10829 /* Free the line_header structure *LH, and any arrays and strings it
10830    refers to.
10831    NOTE: This is also used as a "cleanup" function.  */
10832
10833 static void
10834 free_line_header (struct line_header *lh)
10835 {
10836   if (lh->standard_opcode_lengths)
10837     xfree (lh->standard_opcode_lengths);
10838
10839   /* Remember that all the lh->file_names[i].name pointers are
10840      pointers into debug_line_buffer, and don't need to be freed.  */
10841   if (lh->file_names)
10842     xfree (lh->file_names);
10843
10844   /* Similarly for the include directory names.  */
10845   if (lh->include_dirs)
10846     xfree (lh->include_dirs);
10847
10848   xfree (lh);
10849 }
10850
10851 /* Add an entry to LH's include directory table.  */
10852
10853 static void
10854 add_include_dir (struct line_header *lh, char *include_dir)
10855 {
10856   /* Grow the array if necessary.  */
10857   if (lh->include_dirs_size == 0)
10858     {
10859       lh->include_dirs_size = 1; /* for testing */
10860       lh->include_dirs = xmalloc (lh->include_dirs_size
10861                                   * sizeof (*lh->include_dirs));
10862     }
10863   else if (lh->num_include_dirs >= lh->include_dirs_size)
10864     {
10865       lh->include_dirs_size *= 2;
10866       lh->include_dirs = xrealloc (lh->include_dirs,
10867                                    (lh->include_dirs_size
10868                                     * sizeof (*lh->include_dirs)));
10869     }
10870
10871   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10872 }
10873
10874 /* Add an entry to LH's file name table.  */
10875
10876 static void
10877 add_file_name (struct line_header *lh,
10878                char *name,
10879                unsigned int dir_index,
10880                unsigned int mod_time,
10881                unsigned int length)
10882 {
10883   struct file_entry *fe;
10884
10885   /* Grow the array if necessary.  */
10886   if (lh->file_names_size == 0)
10887     {
10888       lh->file_names_size = 1; /* for testing */
10889       lh->file_names = xmalloc (lh->file_names_size
10890                                 * sizeof (*lh->file_names));
10891     }
10892   else if (lh->num_file_names >= lh->file_names_size)
10893     {
10894       lh->file_names_size *= 2;
10895       lh->file_names = xrealloc (lh->file_names,
10896                                  (lh->file_names_size
10897                                   * sizeof (*lh->file_names)));
10898     }
10899
10900   fe = &lh->file_names[lh->num_file_names++];
10901   fe->name = name;
10902   fe->dir_index = dir_index;
10903   fe->mod_time = mod_time;
10904   fe->length = length;
10905   fe->included_p = 0;
10906   fe->symtab = NULL;
10907 }
10908
10909 /* Read the statement program header starting at OFFSET in
10910    .debug_line, according to the endianness of ABFD.  Return a pointer
10911    to a struct line_header, allocated using xmalloc.
10912
10913    NOTE: the strings in the include directory and file name tables of
10914    the returned object point into debug_line_buffer, and must not be
10915    freed.  */
10916
10917 static struct line_header *
10918 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10919                           struct dwarf2_cu *cu)
10920 {
10921   struct cleanup *back_to;
10922   struct line_header *lh;
10923   gdb_byte *line_ptr;
10924   unsigned int bytes_read, offset_size;
10925   int i;
10926   char *cur_dir, *cur_file;
10927
10928   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10929   if (dwarf2_per_objfile->line.buffer == NULL)
10930     {
10931       complaint (&symfile_complaints, _("missing .debug_line section"));
10932       return 0;
10933     }
10934
10935   /* Make sure that at least there's room for the total_length field.
10936      That could be 12 bytes long, but we're just going to fudge that.  */
10937   if (offset + 4 >= dwarf2_per_objfile->line.size)
10938     {
10939       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10940       return 0;
10941     }
10942
10943   lh = xmalloc (sizeof (*lh));
10944   memset (lh, 0, sizeof (*lh));
10945   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10946                           (void *) lh);
10947
10948   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10949
10950   /* Read in the header.  */
10951   lh->total_length =
10952     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10953                                             &bytes_read, &offset_size);
10954   line_ptr += bytes_read;
10955   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10956                                      + dwarf2_per_objfile->line.size))
10957     {
10958       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10959       return 0;
10960     }
10961   lh->statement_program_end = line_ptr + lh->total_length;
10962   lh->version = read_2_bytes (abfd, line_ptr);
10963   line_ptr += 2;
10964   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10965   line_ptr += offset_size;
10966   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10967   line_ptr += 1;
10968   if (lh->version >= 4)
10969     {
10970       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10971       line_ptr += 1;
10972     }
10973   else
10974     lh->maximum_ops_per_instruction = 1;
10975
10976   if (lh->maximum_ops_per_instruction == 0)
10977     {
10978       lh->maximum_ops_per_instruction = 1;
10979       complaint (&symfile_complaints,
10980                  _("invalid maximum_ops_per_instruction "
10981                    "in `.debug_line' section"));
10982     }
10983
10984   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10985   line_ptr += 1;
10986   lh->line_base = read_1_signed_byte (abfd, line_ptr);
10987   line_ptr += 1;
10988   lh->line_range = read_1_byte (abfd, line_ptr);
10989   line_ptr += 1;
10990   lh->opcode_base = read_1_byte (abfd, line_ptr);
10991   line_ptr += 1;
10992   lh->standard_opcode_lengths
10993     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10994
10995   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
10996   for (i = 1; i < lh->opcode_base; ++i)
10997     {
10998       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10999       line_ptr += 1;
11000     }
11001
11002   /* Read directory table.  */
11003   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11004     {
11005       line_ptr += bytes_read;
11006       add_include_dir (lh, cur_dir);
11007     }
11008   line_ptr += bytes_read;
11009
11010   /* Read file name table.  */
11011   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11012     {
11013       unsigned int dir_index, mod_time, length;
11014
11015       line_ptr += bytes_read;
11016       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11017       line_ptr += bytes_read;
11018       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11019       line_ptr += bytes_read;
11020       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11021       line_ptr += bytes_read;
11022
11023       add_file_name (lh, cur_file, dir_index, mod_time, length);
11024     }
11025   line_ptr += bytes_read;
11026   lh->statement_program_start = line_ptr;
11027
11028   if (line_ptr > (dwarf2_per_objfile->line.buffer
11029                   + dwarf2_per_objfile->line.size))
11030     complaint (&symfile_complaints,
11031                _("line number info header doesn't "
11032                  "fit in `.debug_line' section"));
11033
11034   discard_cleanups (back_to);
11035   return lh;
11036 }
11037
11038 /* This function exists to work around a bug in certain compilers
11039    (particularly GCC 2.95), in which the first line number marker of a
11040    function does not show up until after the prologue, right before
11041    the second line number marker.  This function shifts ADDRESS down
11042    to the beginning of the function if necessary, and is called on
11043    addresses passed to record_line.  */
11044
11045 static CORE_ADDR
11046 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
11047 {
11048   struct function_range *fn;
11049
11050   /* Find the function_range containing address.  */
11051   if (!cu->first_fn)
11052     return address;
11053
11054   if (!cu->cached_fn)
11055     cu->cached_fn = cu->first_fn;
11056
11057   fn = cu->cached_fn;
11058   while (fn)
11059     if (fn->lowpc <= address && fn->highpc > address)
11060       goto found;
11061     else
11062       fn = fn->next;
11063
11064   fn = cu->first_fn;
11065   while (fn && fn != cu->cached_fn)
11066     if (fn->lowpc <= address && fn->highpc > address)
11067       goto found;
11068     else
11069       fn = fn->next;
11070
11071   return address;
11072
11073  found:
11074   if (fn->seen_line)
11075     return address;
11076   if (address != fn->lowpc)
11077     complaint (&symfile_complaints,
11078                _("misplaced first line number at 0x%lx for '%s'"),
11079                (unsigned long) address, fn->name);
11080   fn->seen_line = 1;
11081   return fn->lowpc;
11082 }
11083
11084 /* Subroutine of dwarf_decode_lines to simplify it.
11085    Return the file name of the psymtab for included file FILE_INDEX
11086    in line header LH of PST.
11087    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11088    If space for the result is malloc'd, it will be freed by a cleanup.
11089    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
11090
11091 static char *
11092 psymtab_include_file_name (const struct line_header *lh, int file_index,
11093                            const struct partial_symtab *pst,
11094                            const char *comp_dir)
11095 {
11096   const struct file_entry fe = lh->file_names [file_index];
11097   char *include_name = fe.name;
11098   char *include_name_to_compare = include_name;
11099   char *dir_name = NULL;
11100   const char *pst_filename;
11101   char *copied_name = NULL;
11102   int file_is_pst;
11103
11104   if (fe.dir_index)
11105     dir_name = lh->include_dirs[fe.dir_index - 1];
11106
11107   if (!IS_ABSOLUTE_PATH (include_name)
11108       && (dir_name != NULL || comp_dir != NULL))
11109     {
11110       /* Avoid creating a duplicate psymtab for PST.
11111          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11112          Before we do the comparison, however, we need to account
11113          for DIR_NAME and COMP_DIR.
11114          First prepend dir_name (if non-NULL).  If we still don't
11115          have an absolute path prepend comp_dir (if non-NULL).
11116          However, the directory we record in the include-file's
11117          psymtab does not contain COMP_DIR (to match the
11118          corresponding symtab(s)).
11119
11120          Example:
11121
11122          bash$ cd /tmp
11123          bash$ gcc -g ./hello.c
11124          include_name = "hello.c"
11125          dir_name = "."
11126          DW_AT_comp_dir = comp_dir = "/tmp"
11127          DW_AT_name = "./hello.c"  */
11128
11129       if (dir_name != NULL)
11130         {
11131           include_name = concat (dir_name, SLASH_STRING,
11132                                  include_name, (char *)NULL);
11133           include_name_to_compare = include_name;
11134           make_cleanup (xfree, include_name);
11135         }
11136       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11137         {
11138           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11139                                             include_name, (char *)NULL);
11140         }
11141     }
11142
11143   pst_filename = pst->filename;
11144   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11145     {
11146       copied_name = concat (pst->dirname, SLASH_STRING,
11147                             pst_filename, (char *)NULL);
11148       pst_filename = copied_name;
11149     }
11150
11151   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11152
11153   if (include_name_to_compare != include_name)
11154     xfree (include_name_to_compare);
11155   if (copied_name != NULL)
11156     xfree (copied_name);
11157
11158   if (file_is_pst)
11159     return NULL;
11160   return include_name;
11161 }
11162
11163 /* Ignore this record_line request.  */
11164
11165 static void
11166 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11167 {
11168   return;
11169 }
11170
11171 /* Decode the Line Number Program (LNP) for the given line_header
11172    structure and CU.  The actual information extracted and the type
11173    of structures created from the LNP depends on the value of PST.
11174
11175    1. If PST is NULL, then this procedure uses the data from the program
11176       to create all necessary symbol tables, and their linetables.
11177
11178    2. If PST is not NULL, this procedure reads the program to determine
11179       the list of files included by the unit represented by PST, and
11180       builds all the associated partial symbol tables.
11181
11182    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11183    It is used for relative paths in the line table.
11184    NOTE: When processing partial symtabs (pst != NULL),
11185    comp_dir == pst->dirname.
11186
11187    NOTE: It is important that psymtabs have the same file name (via strcmp)
11188    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11189    symtab we don't use it in the name of the psymtabs we create.
11190    E.g. expand_line_sal requires this when finding psymtabs to expand.
11191    A good testcase for this is mb-inline.exp.  */
11192
11193 static void
11194 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11195                     struct dwarf2_cu *cu, struct partial_symtab *pst)
11196 {
11197   gdb_byte *line_ptr, *extended_end;
11198   gdb_byte *line_end;
11199   unsigned int bytes_read, extended_len;
11200   unsigned char op_code, extended_op, adj_opcode;
11201   CORE_ADDR baseaddr;
11202   struct objfile *objfile = cu->objfile;
11203   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11204   const int decode_for_pst_p = (pst != NULL);
11205   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11206   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11207     = record_line;
11208
11209   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11210
11211   line_ptr = lh->statement_program_start;
11212   line_end = lh->statement_program_end;
11213
11214   /* Read the statement sequences until there's nothing left.  */
11215   while (line_ptr < line_end)
11216     {
11217       /* state machine registers  */
11218       CORE_ADDR address = 0;
11219       unsigned int file = 1;
11220       unsigned int line = 1;
11221       unsigned int column = 0;
11222       int is_stmt = lh->default_is_stmt;
11223       int basic_block = 0;
11224       int end_sequence = 0;
11225       CORE_ADDR addr;
11226       unsigned char op_index = 0;
11227
11228       if (!decode_for_pst_p && lh->num_file_names >= file)
11229         {
11230           /* Start a subfile for the current file of the state machine.  */
11231           /* lh->include_dirs and lh->file_names are 0-based, but the
11232              directory and file name numbers in the statement program
11233              are 1-based.  */
11234           struct file_entry *fe = &lh->file_names[file - 1];
11235           char *dir = NULL;
11236
11237           if (fe->dir_index)
11238             dir = lh->include_dirs[fe->dir_index - 1];
11239
11240           dwarf2_start_subfile (fe->name, dir, comp_dir);
11241         }
11242
11243       /* Decode the table.  */
11244       while (!end_sequence)
11245         {
11246           op_code = read_1_byte (abfd, line_ptr);
11247           line_ptr += 1;
11248           if (line_ptr > line_end)
11249             {
11250               dwarf2_debug_line_missing_end_sequence_complaint ();
11251               break;
11252             }
11253
11254           if (op_code >= lh->opcode_base)
11255             {
11256               /* Special operand.  */
11257               adj_opcode = op_code - lh->opcode_base;
11258               address += (((op_index + (adj_opcode / lh->line_range))
11259                            / lh->maximum_ops_per_instruction)
11260                           * lh->minimum_instruction_length);
11261               op_index = ((op_index + (adj_opcode / lh->line_range))
11262                           % lh->maximum_ops_per_instruction);
11263               line += lh->line_base + (adj_opcode % lh->line_range);
11264               if (lh->num_file_names < file || file == 0)
11265                 dwarf2_debug_line_missing_file_complaint ();
11266               /* For now we ignore lines not starting on an
11267                  instruction boundary.  */
11268               else if (op_index == 0)
11269                 {
11270                   lh->file_names[file - 1].included_p = 1;
11271                   if (!decode_for_pst_p && is_stmt)
11272                     {
11273                       if (last_subfile != current_subfile)
11274                         {
11275                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11276                           if (last_subfile)
11277                             (*p_record_line) (last_subfile, 0, addr);
11278                           last_subfile = current_subfile;
11279                         }
11280                       /* Append row to matrix using current values.  */
11281                       addr = check_cu_functions (address, cu);
11282                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
11283                       (*p_record_line) (current_subfile, line, addr);
11284                     }
11285                 }
11286               basic_block = 0;
11287             }
11288           else switch (op_code)
11289             {
11290             case DW_LNS_extended_op:
11291               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11292                                                    &bytes_read);
11293               line_ptr += bytes_read;
11294               extended_end = line_ptr + extended_len;
11295               extended_op = read_1_byte (abfd, line_ptr);
11296               line_ptr += 1;
11297               switch (extended_op)
11298                 {
11299                 case DW_LNE_end_sequence:
11300                   p_record_line = record_line;
11301                   end_sequence = 1;
11302                   break;
11303                 case DW_LNE_set_address:
11304                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11305
11306                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11307                     {
11308                       /* This line table is for a function which has been
11309                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11310
11311                       long line_offset
11312                         = line_ptr - dwarf2_per_objfile->line.buffer;
11313
11314                       complaint (&symfile_complaints,
11315                                  _(".debug_line address at offset 0x%lx is 0 "
11316                                    "[in module %s]"),
11317                                  line_offset, cu->objfile->name);
11318                       p_record_line = noop_record_line;
11319                     }
11320
11321                   op_index = 0;
11322                   line_ptr += bytes_read;
11323                   address += baseaddr;
11324                   break;
11325                 case DW_LNE_define_file:
11326                   {
11327                     char *cur_file;
11328                     unsigned int dir_index, mod_time, length;
11329
11330                     cur_file = read_direct_string (abfd, line_ptr,
11331                                                    &bytes_read);
11332                     line_ptr += bytes_read;
11333                     dir_index =
11334                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11335                     line_ptr += bytes_read;
11336                     mod_time =
11337                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11338                     line_ptr += bytes_read;
11339                     length =
11340                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11341                     line_ptr += bytes_read;
11342                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11343                   }
11344                   break;
11345                 case DW_LNE_set_discriminator:
11346                   /* The discriminator is not interesting to the debugger;
11347                      just ignore it.  */
11348                   line_ptr = extended_end;
11349                   break;
11350                 default:
11351                   complaint (&symfile_complaints,
11352                              _("mangled .debug_line section"));
11353                   return;
11354                 }
11355               /* Make sure that we parsed the extended op correctly.  If e.g.
11356                  we expected a different address size than the producer used,
11357                  we may have read the wrong number of bytes.  */
11358               if (line_ptr != extended_end)
11359                 {
11360                   complaint (&symfile_complaints,
11361                              _("mangled .debug_line section"));
11362                   return;
11363                 }
11364               break;
11365             case DW_LNS_copy:
11366               if (lh->num_file_names < file || file == 0)
11367                 dwarf2_debug_line_missing_file_complaint ();
11368               else
11369                 {
11370                   lh->file_names[file - 1].included_p = 1;
11371                   if (!decode_for_pst_p && is_stmt)
11372                     {
11373                       if (last_subfile != current_subfile)
11374                         {
11375                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11376                           if (last_subfile)
11377                             (*p_record_line) (last_subfile, 0, addr);
11378                           last_subfile = current_subfile;
11379                         }
11380                       addr = check_cu_functions (address, cu);
11381                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
11382                       (*p_record_line) (current_subfile, line, addr);
11383                     }
11384                 }
11385               basic_block = 0;
11386               break;
11387             case DW_LNS_advance_pc:
11388               {
11389                 CORE_ADDR adjust
11390                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11391
11392                 address += (((op_index + adjust)
11393                              / lh->maximum_ops_per_instruction)
11394                             * lh->minimum_instruction_length);
11395                 op_index = ((op_index + adjust)
11396                             % lh->maximum_ops_per_instruction);
11397                 line_ptr += bytes_read;
11398               }
11399               break;
11400             case DW_LNS_advance_line:
11401               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11402               line_ptr += bytes_read;
11403               break;
11404             case DW_LNS_set_file:
11405               {
11406                 /* The arrays lh->include_dirs and lh->file_names are
11407                    0-based, but the directory and file name numbers in
11408                    the statement program are 1-based.  */
11409                 struct file_entry *fe;
11410                 char *dir = NULL;
11411
11412                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11413                 line_ptr += bytes_read;
11414                 if (lh->num_file_names < file || file == 0)
11415                   dwarf2_debug_line_missing_file_complaint ();
11416                 else
11417                   {
11418                     fe = &lh->file_names[file - 1];
11419                     if (fe->dir_index)
11420                       dir = lh->include_dirs[fe->dir_index - 1];
11421                     if (!decode_for_pst_p)
11422                       {
11423                         last_subfile = current_subfile;
11424                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11425                       }
11426                   }
11427               }
11428               break;
11429             case DW_LNS_set_column:
11430               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11431               line_ptr += bytes_read;
11432               break;
11433             case DW_LNS_negate_stmt:
11434               is_stmt = (!is_stmt);
11435               break;
11436             case DW_LNS_set_basic_block:
11437               basic_block = 1;
11438               break;
11439             /* Add to the address register of the state machine the
11440                address increment value corresponding to special opcode
11441                255.  I.e., this value is scaled by the minimum
11442                instruction length since special opcode 255 would have
11443                scaled the increment.  */
11444             case DW_LNS_const_add_pc:
11445               {
11446                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11447
11448                 address += (((op_index + adjust)
11449                              / lh->maximum_ops_per_instruction)
11450                             * lh->minimum_instruction_length);
11451                 op_index = ((op_index + adjust)
11452                             % lh->maximum_ops_per_instruction);
11453               }
11454               break;
11455             case DW_LNS_fixed_advance_pc:
11456               address += read_2_bytes (abfd, line_ptr);
11457               op_index = 0;
11458               line_ptr += 2;
11459               break;
11460             default:
11461               {
11462                 /* Unknown standard opcode, ignore it.  */
11463                 int i;
11464
11465                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11466                   {
11467                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11468                     line_ptr += bytes_read;
11469                   }
11470               }
11471             }
11472         }
11473       if (lh->num_file_names < file || file == 0)
11474         dwarf2_debug_line_missing_file_complaint ();
11475       else
11476         {
11477           lh->file_names[file - 1].included_p = 1;
11478           if (!decode_for_pst_p)
11479             {
11480               addr = gdbarch_addr_bits_remove (gdbarch, address);
11481               (*p_record_line) (current_subfile, 0, addr);
11482             }
11483         }
11484     }
11485
11486   if (decode_for_pst_p)
11487     {
11488       int file_index;
11489
11490       /* Now that we're done scanning the Line Header Program, we can
11491          create the psymtab of each included file.  */
11492       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11493         if (lh->file_names[file_index].included_p == 1)
11494           {
11495             char *include_name =
11496               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11497             if (include_name != NULL)
11498               dwarf2_create_include_psymtab (include_name, pst, objfile);
11499           }
11500     }
11501   else
11502     {
11503       /* Make sure a symtab is created for every file, even files
11504          which contain only variables (i.e. no code with associated
11505          line numbers).  */
11506
11507       int i;
11508       struct file_entry *fe;
11509
11510       for (i = 0; i < lh->num_file_names; i++)
11511         {
11512           char *dir = NULL;
11513
11514           fe = &lh->file_names[i];
11515           if (fe->dir_index)
11516             dir = lh->include_dirs[fe->dir_index - 1];
11517           dwarf2_start_subfile (fe->name, dir, comp_dir);
11518
11519           /* Skip the main file; we don't need it, and it must be
11520              allocated last, so that it will show up before the
11521              non-primary symtabs in the objfile's symtab list.  */
11522           if (current_subfile == first_subfile)
11523             continue;
11524
11525           if (current_subfile->symtab == NULL)
11526             current_subfile->symtab = allocate_symtab (current_subfile->name,
11527                                                        cu->objfile);
11528           fe->symtab = current_subfile->symtab;
11529         }
11530     }
11531 }
11532
11533 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11534    DIRNAME the name of the source directory which contains FILENAME
11535    or NULL if not known.  COMP_DIR is the compilation directory for the
11536    linetable's compilation unit or NULL if not known.
11537    This routine tries to keep line numbers from identical absolute and
11538    relative file names in a common subfile.
11539
11540    Using the `list' example from the GDB testsuite, which resides in
11541    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11542    of /srcdir/list0.c yields the following debugging information for list0.c:
11543
11544    DW_AT_name:          /srcdir/list0.c
11545    DW_AT_comp_dir:              /compdir
11546    files.files[0].name: list0.h
11547    files.files[0].dir:  /srcdir
11548    files.files[1].name: list0.c
11549    files.files[1].dir:  /srcdir
11550
11551    The line number information for list0.c has to end up in a single
11552    subfile, so that `break /srcdir/list0.c:1' works as expected.
11553    start_subfile will ensure that this happens provided that we pass the
11554    concatenation of files.files[1].dir and files.files[1].name as the
11555    subfile's name.  */
11556
11557 static void
11558 dwarf2_start_subfile (char *filename, const char *dirname,
11559                       const char *comp_dir)
11560 {
11561   char *fullname;
11562
11563   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11564      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11565      second argument to start_subfile.  To be consistent, we do the
11566      same here.  In order not to lose the line information directory,
11567      we concatenate it to the filename when it makes sense.
11568      Note that the Dwarf3 standard says (speaking of filenames in line
11569      information): ``The directory index is ignored for file names
11570      that represent full path names''.  Thus ignoring dirname in the
11571      `else' branch below isn't an issue.  */
11572
11573   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11574     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11575   else
11576     fullname = filename;
11577
11578   start_subfile (fullname, comp_dir);
11579
11580   if (fullname != filename)
11581     xfree (fullname);
11582 }
11583
11584 static void
11585 var_decode_location (struct attribute *attr, struct symbol *sym,
11586                      struct dwarf2_cu *cu)
11587 {
11588   struct objfile *objfile = cu->objfile;
11589   struct comp_unit_head *cu_header = &cu->header;
11590
11591   /* NOTE drow/2003-01-30: There used to be a comment and some special
11592      code here to turn a symbol with DW_AT_external and a
11593      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11594      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11595      with some versions of binutils) where shared libraries could have
11596      relocations against symbols in their debug information - the
11597      minimal symbol would have the right address, but the debug info
11598      would not.  It's no longer necessary, because we will explicitly
11599      apply relocations when we read in the debug information now.  */
11600
11601   /* A DW_AT_location attribute with no contents indicates that a
11602      variable has been optimized away.  */
11603   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11604     {
11605       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11606       return;
11607     }
11608
11609   /* Handle one degenerate form of location expression specially, to
11610      preserve GDB's previous behavior when section offsets are
11611      specified.  If this is just a DW_OP_addr then mark this symbol
11612      as LOC_STATIC.  */
11613
11614   if (attr_form_is_block (attr)
11615       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11616       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11617     {
11618       unsigned int dummy;
11619
11620       SYMBOL_VALUE_ADDRESS (sym) =
11621         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11622       SYMBOL_CLASS (sym) = LOC_STATIC;
11623       fixup_symbol_section (sym, objfile);
11624       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11625                                               SYMBOL_SECTION (sym));
11626       return;
11627     }
11628
11629   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11630      expression evaluator, and use LOC_COMPUTED only when necessary
11631      (i.e. when the value of a register or memory location is
11632      referenced, or a thread-local block, etc.).  Then again, it might
11633      not be worthwhile.  I'm assuming that it isn't unless performance
11634      or memory numbers show me otherwise.  */
11635
11636   dwarf2_symbol_mark_computed (attr, sym, cu);
11637   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11638
11639   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11640     cu->has_loclist = 1;
11641 }
11642
11643 /* Given a pointer to a DWARF information entry, figure out if we need
11644    to make a symbol table entry for it, and if so, create a new entry
11645    and return a pointer to it.
11646    If TYPE is NULL, determine symbol type from the die, otherwise
11647    used the passed type.
11648    If SPACE is not NULL, use it to hold the new symbol.  If it is
11649    NULL, allocate a new symbol on the objfile's obstack.  */
11650
11651 static struct symbol *
11652 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11653                  struct symbol *space)
11654 {
11655   struct objfile *objfile = cu->objfile;
11656   struct symbol *sym = NULL;
11657   char *name;
11658   struct attribute *attr = NULL;
11659   struct attribute *attr2 = NULL;
11660   CORE_ADDR baseaddr;
11661   struct pending **list_to_add = NULL;
11662
11663   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11664
11665   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11666
11667   name = dwarf2_name (die, cu);
11668   if (name)
11669     {
11670       const char *linkagename;
11671       int suppress_add = 0;
11672
11673       if (space)
11674         sym = space;
11675       else
11676         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11677       OBJSTAT (objfile, n_syms++);
11678
11679       /* Cache this symbol's name and the name's demangled form (if any).  */
11680       SYMBOL_SET_LANGUAGE (sym, cu->language);
11681       linkagename = dwarf2_physname (name, die, cu);
11682       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11683
11684       /* Fortran does not have mangling standard and the mangling does differ
11685          between gfortran, iFort etc.  */
11686       if (cu->language == language_fortran
11687           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11688         symbol_set_demangled_name (&(sym->ginfo),
11689                                    (char *) dwarf2_full_name (name, die, cu),
11690                                    NULL);
11691
11692       /* Default assumptions.
11693          Use the passed type or decode it from the die.  */
11694       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11695       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11696       if (type != NULL)
11697         SYMBOL_TYPE (sym) = type;
11698       else
11699         SYMBOL_TYPE (sym) = die_type (die, cu);
11700       attr = dwarf2_attr (die,
11701                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11702                           cu);
11703       if (attr)
11704         {
11705           SYMBOL_LINE (sym) = DW_UNSND (attr);
11706         }
11707
11708       attr = dwarf2_attr (die,
11709                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11710                           cu);
11711       if (attr)
11712         {
11713           int file_index = DW_UNSND (attr);
11714
11715           if (cu->line_header == NULL
11716               || file_index > cu->line_header->num_file_names)
11717             complaint (&symfile_complaints,
11718                        _("file index out of range"));
11719           else if (file_index > 0)
11720             {
11721               struct file_entry *fe;
11722
11723               fe = &cu->line_header->file_names[file_index - 1];
11724               SYMBOL_SYMTAB (sym) = fe->symtab;
11725             }
11726         }
11727
11728       switch (die->tag)
11729         {
11730         case DW_TAG_label:
11731           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11732           if (attr)
11733             {
11734               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11735             }
11736           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11737           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11738           SYMBOL_CLASS (sym) = LOC_LABEL;
11739           add_symbol_to_list (sym, cu->list_in_scope);
11740           break;
11741         case DW_TAG_subprogram:
11742           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11743              finish_block.  */
11744           SYMBOL_CLASS (sym) = LOC_BLOCK;
11745           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11746           if ((attr2 && (DW_UNSND (attr2) != 0))
11747               || cu->language == language_ada)
11748             {
11749               /* Subprograms marked external are stored as a global symbol.
11750                  Ada subprograms, whether marked external or not, are always
11751                  stored as a global symbol, because we want to be able to
11752                  access them globally.  For instance, we want to be able
11753                  to break on a nested subprogram without having to
11754                  specify the context.  */
11755               list_to_add = &global_symbols;
11756             }
11757           else
11758             {
11759               list_to_add = cu->list_in_scope;
11760             }
11761           break;
11762         case DW_TAG_inlined_subroutine:
11763           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11764              finish_block.  */
11765           SYMBOL_CLASS (sym) = LOC_BLOCK;
11766           SYMBOL_INLINED (sym) = 1;
11767           /* Do not add the symbol to any lists.  It will be found via
11768              BLOCK_FUNCTION from the blockvector.  */
11769           break;
11770         case DW_TAG_template_value_param:
11771           suppress_add = 1;
11772           /* Fall through.  */
11773         case DW_TAG_constant:
11774         case DW_TAG_variable:
11775         case DW_TAG_member:
11776           /* Compilation with minimal debug info may result in
11777              variables with missing type entries.  Change the
11778              misleading `void' type to something sensible.  */
11779           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11780             SYMBOL_TYPE (sym)
11781               = objfile_type (objfile)->nodebug_data_symbol;
11782
11783           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11784           /* In the case of DW_TAG_member, we should only be called for
11785              static const members.  */
11786           if (die->tag == DW_TAG_member)
11787             {
11788               /* dwarf2_add_field uses die_is_declaration,
11789                  so we do the same.  */
11790               gdb_assert (die_is_declaration (die, cu));
11791               gdb_assert (attr);
11792             }
11793           if (attr)
11794             {
11795               dwarf2_const_value (attr, sym, cu);
11796               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11797               if (!suppress_add)
11798                 {
11799                   if (attr2 && (DW_UNSND (attr2) != 0))
11800                     list_to_add = &global_symbols;
11801                   else
11802                     list_to_add = cu->list_in_scope;
11803                 }
11804               break;
11805             }
11806           attr = dwarf2_attr (die, DW_AT_location, cu);
11807           if (attr)
11808             {
11809               var_decode_location (attr, sym, cu);
11810               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11811               if (SYMBOL_CLASS (sym) == LOC_STATIC
11812                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11813                   && !dwarf2_per_objfile->has_section_at_zero)
11814                 {
11815                   /* When a static variable is eliminated by the linker,
11816                      the corresponding debug information is not stripped
11817                      out, but the variable address is set to null;
11818                      do not add such variables into symbol table.  */
11819                 }
11820               else if (attr2 && (DW_UNSND (attr2) != 0))
11821                 {
11822                   /* Workaround gfortran PR debug/40040 - it uses
11823                      DW_AT_location for variables in -fPIC libraries which may
11824                      get overriden by other libraries/executable and get
11825                      a different address.  Resolve it by the minimal symbol
11826                      which may come from inferior's executable using copy
11827                      relocation.  Make this workaround only for gfortran as for
11828                      other compilers GDB cannot guess the minimal symbol
11829                      Fortran mangling kind.  */
11830                   if (cu->language == language_fortran && die->parent
11831                       && die->parent->tag == DW_TAG_module
11832                       && cu->producer
11833                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11834                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11835
11836                   /* A variable with DW_AT_external is never static,
11837                      but it may be block-scoped.  */
11838                   list_to_add = (cu->list_in_scope == &file_symbols
11839                                  ? &global_symbols : cu->list_in_scope);
11840                 }
11841               else
11842                 list_to_add = cu->list_in_scope;
11843             }
11844           else
11845             {
11846               /* We do not know the address of this symbol.
11847                  If it is an external symbol and we have type information
11848                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11849                  The address of the variable will then be determined from
11850                  the minimal symbol table whenever the variable is
11851                  referenced.  */
11852               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11853               if (attr2 && (DW_UNSND (attr2) != 0)
11854                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11855                 {
11856                   /* A variable with DW_AT_external is never static, but it
11857                      may be block-scoped.  */
11858                   list_to_add = (cu->list_in_scope == &file_symbols
11859                                  ? &global_symbols : cu->list_in_scope);
11860
11861                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11862                 }
11863               else if (!die_is_declaration (die, cu))
11864                 {
11865                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11866                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11867                   if (!suppress_add)
11868                     list_to_add = cu->list_in_scope;
11869                 }
11870             }
11871           break;
11872         case DW_TAG_formal_parameter:
11873           /* If we are inside a function, mark this as an argument.  If
11874              not, we might be looking at an argument to an inlined function
11875              when we do not have enough information to show inlined frames;
11876              pretend it's a local variable in that case so that the user can
11877              still see it.  */
11878           if (context_stack_depth > 0
11879               && context_stack[context_stack_depth - 1].name != NULL)
11880             SYMBOL_IS_ARGUMENT (sym) = 1;
11881           attr = dwarf2_attr (die, DW_AT_location, cu);
11882           if (attr)
11883             {
11884               var_decode_location (attr, sym, cu);
11885             }
11886           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11887           if (attr)
11888             {
11889               dwarf2_const_value (attr, sym, cu);
11890             }
11891
11892           list_to_add = cu->list_in_scope;
11893           break;
11894         case DW_TAG_unspecified_parameters:
11895           /* From varargs functions; gdb doesn't seem to have any
11896              interest in this information, so just ignore it for now.
11897              (FIXME?) */
11898           break;
11899         case DW_TAG_template_type_param:
11900           suppress_add = 1;
11901           /* Fall through.  */
11902         case DW_TAG_class_type:
11903         case DW_TAG_interface_type:
11904         case DW_TAG_structure_type:
11905         case DW_TAG_union_type:
11906         case DW_TAG_set_type:
11907         case DW_TAG_enumeration_type:
11908           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11909           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11910
11911           {
11912             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11913                really ever be static objects: otherwise, if you try
11914                to, say, break of a class's method and you're in a file
11915                which doesn't mention that class, it won't work unless
11916                the check for all static symbols in lookup_symbol_aux
11917                saves you.  See the OtherFileClass tests in
11918                gdb.c++/namespace.exp.  */
11919
11920             if (!suppress_add)
11921               {
11922                 list_to_add = (cu->list_in_scope == &file_symbols
11923                                && (cu->language == language_cplus
11924                                    || cu->language == language_java)
11925                                ? &global_symbols : cu->list_in_scope);
11926
11927                 /* The semantics of C++ state that "struct foo {
11928                    ... }" also defines a typedef for "foo".  A Java
11929                    class declaration also defines a typedef for the
11930                    class.  */
11931                 if (cu->language == language_cplus
11932                     || cu->language == language_java
11933                     || cu->language == language_ada)
11934                   {
11935                     /* The symbol's name is already allocated along
11936                        with this objfile, so we don't need to
11937                        duplicate it for the type.  */
11938                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11939                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11940                   }
11941               }
11942           }
11943           break;
11944         case DW_TAG_typedef:
11945           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11946           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11947           list_to_add = cu->list_in_scope;
11948           break;
11949         case DW_TAG_base_type:
11950         case DW_TAG_subrange_type:
11951           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11952           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11953           list_to_add = cu->list_in_scope;
11954           break;
11955         case DW_TAG_enumerator:
11956           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11957           if (attr)
11958             {
11959               dwarf2_const_value (attr, sym, cu);
11960             }
11961           {
11962             /* NOTE: carlton/2003-11-10: See comment above in the
11963                DW_TAG_class_type, etc. block.  */
11964
11965             list_to_add = (cu->list_in_scope == &file_symbols
11966                            && (cu->language == language_cplus
11967                                || cu->language == language_java)
11968                            ? &global_symbols : cu->list_in_scope);
11969           }
11970           break;
11971         case DW_TAG_namespace:
11972           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11973           list_to_add = &global_symbols;
11974           break;
11975         default:
11976           /* Not a tag we recognize.  Hopefully we aren't processing
11977              trash data, but since we must specifically ignore things
11978              we don't recognize, there is nothing else we should do at
11979              this point.  */
11980           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11981                      dwarf_tag_name (die->tag));
11982           break;
11983         }
11984
11985       if (suppress_add)
11986         {
11987           sym->hash_next = objfile->template_symbols;
11988           objfile->template_symbols = sym;
11989           list_to_add = NULL;
11990         }
11991
11992       if (list_to_add != NULL)
11993         add_symbol_to_list (sym, list_to_add);
11994
11995       /* For the benefit of old versions of GCC, check for anonymous
11996          namespaces based on the demangled name.  */
11997       if (!processing_has_namespace_info
11998           && cu->language == language_cplus)
11999         cp_scan_for_anonymous_namespaces (sym, objfile);
12000     }
12001   return (sym);
12002 }
12003
12004 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
12005
12006 static struct symbol *
12007 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12008 {
12009   return new_symbol_full (die, type, cu, NULL);
12010 }
12011
12012 /* Given an attr with a DW_FORM_dataN value in host byte order,
12013    zero-extend it as appropriate for the symbol's type.  The DWARF
12014    standard (v4) is not entirely clear about the meaning of using
12015    DW_FORM_dataN for a constant with a signed type, where the type is
12016    wider than the data.  The conclusion of a discussion on the DWARF
12017    list was that this is unspecified.  We choose to always zero-extend
12018    because that is the interpretation long in use by GCC.  */
12019
12020 static gdb_byte *
12021 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12022                          const char *name, struct obstack *obstack,
12023                          struct dwarf2_cu *cu, long *value, int bits)
12024 {
12025   struct objfile *objfile = cu->objfile;
12026   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12027                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12028   LONGEST l = DW_UNSND (attr);
12029
12030   if (bits < sizeof (*value) * 8)
12031     {
12032       l &= ((LONGEST) 1 << bits) - 1;
12033       *value = l;
12034     }
12035   else if (bits == sizeof (*value) * 8)
12036     *value = l;
12037   else
12038     {
12039       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12040       store_unsigned_integer (bytes, bits / 8, byte_order, l);
12041       return bytes;
12042     }
12043
12044   return NULL;
12045 }
12046
12047 /* Read a constant value from an attribute.  Either set *VALUE, or if
12048    the value does not fit in *VALUE, set *BYTES - either already
12049    allocated on the objfile obstack, or newly allocated on OBSTACK,
12050    or, set *BATON, if we translated the constant to a location
12051    expression.  */
12052
12053 static void
12054 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12055                          const char *name, struct obstack *obstack,
12056                          struct dwarf2_cu *cu,
12057                          long *value, gdb_byte **bytes,
12058                          struct dwarf2_locexpr_baton **baton)
12059 {
12060   struct objfile *objfile = cu->objfile;
12061   struct comp_unit_head *cu_header = &cu->header;
12062   struct dwarf_block *blk;
12063   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12064                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12065
12066   *value = 0;
12067   *bytes = NULL;
12068   *baton = NULL;
12069
12070   switch (attr->form)
12071     {
12072     case DW_FORM_addr:
12073       {
12074         gdb_byte *data;
12075
12076         if (TYPE_LENGTH (type) != cu_header->addr_size)
12077           dwarf2_const_value_length_mismatch_complaint (name,
12078                                                         cu_header->addr_size,
12079                                                         TYPE_LENGTH (type));
12080         /* Symbols of this form are reasonably rare, so we just
12081            piggyback on the existing location code rather than writing
12082            a new implementation of symbol_computed_ops.  */
12083         *baton = obstack_alloc (&objfile->objfile_obstack,
12084                                 sizeof (struct dwarf2_locexpr_baton));
12085         (*baton)->per_cu = cu->per_cu;
12086         gdb_assert ((*baton)->per_cu);
12087
12088         (*baton)->size = 2 + cu_header->addr_size;
12089         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12090         (*baton)->data = data;
12091
12092         data[0] = DW_OP_addr;
12093         store_unsigned_integer (&data[1], cu_header->addr_size,
12094                                 byte_order, DW_ADDR (attr));
12095         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12096       }
12097       break;
12098     case DW_FORM_string:
12099     case DW_FORM_strp:
12100       /* DW_STRING is already allocated on the objfile obstack, point
12101          directly to it.  */
12102       *bytes = (gdb_byte *) DW_STRING (attr);
12103       break;
12104     case DW_FORM_block1:
12105     case DW_FORM_block2:
12106     case DW_FORM_block4:
12107     case DW_FORM_block:
12108     case DW_FORM_exprloc:
12109       blk = DW_BLOCK (attr);
12110       if (TYPE_LENGTH (type) != blk->size)
12111         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12112                                                       TYPE_LENGTH (type));
12113       *bytes = blk->data;
12114       break;
12115
12116       /* The DW_AT_const_value attributes are supposed to carry the
12117          symbol's value "represented as it would be on the target
12118          architecture."  By the time we get here, it's already been
12119          converted to host endianness, so we just need to sign- or
12120          zero-extend it as appropriate.  */
12121     case DW_FORM_data1:
12122       *bytes = dwarf2_const_value_data (attr, type, name,
12123                                         obstack, cu, value, 8);
12124       break;
12125     case DW_FORM_data2:
12126       *bytes = dwarf2_const_value_data (attr, type, name,
12127                                         obstack, cu, value, 16);
12128       break;
12129     case DW_FORM_data4:
12130       *bytes = dwarf2_const_value_data (attr, type, name,
12131                                         obstack, cu, value, 32);
12132       break;
12133     case DW_FORM_data8:
12134       *bytes = dwarf2_const_value_data (attr, type, name,
12135                                         obstack, cu, value, 64);
12136       break;
12137
12138     case DW_FORM_sdata:
12139       *value = DW_SND (attr);
12140       break;
12141
12142     case DW_FORM_udata:
12143       *value = DW_UNSND (attr);
12144       break;
12145
12146     default:
12147       complaint (&symfile_complaints,
12148                  _("unsupported const value attribute form: '%s'"),
12149                  dwarf_form_name (attr->form));
12150       *value = 0;
12151       break;
12152     }
12153 }
12154
12155
12156 /* Copy constant value from an attribute to a symbol.  */
12157
12158 static void
12159 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12160                     struct dwarf2_cu *cu)
12161 {
12162   struct objfile *objfile = cu->objfile;
12163   struct comp_unit_head *cu_header = &cu->header;
12164   long value;
12165   gdb_byte *bytes;
12166   struct dwarf2_locexpr_baton *baton;
12167
12168   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12169                            SYMBOL_PRINT_NAME (sym),
12170                            &objfile->objfile_obstack, cu,
12171                            &value, &bytes, &baton);
12172
12173   if (baton != NULL)
12174     {
12175       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12176       SYMBOL_LOCATION_BATON (sym) = baton;
12177       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12178     }
12179   else if (bytes != NULL)
12180      {
12181       SYMBOL_VALUE_BYTES (sym) = bytes;
12182       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12183     }
12184   else
12185     {
12186       SYMBOL_VALUE (sym) = value;
12187       SYMBOL_CLASS (sym) = LOC_CONST;
12188     }
12189 }
12190
12191 /* Return the type of the die in question using its DW_AT_type attribute.  */
12192
12193 static struct type *
12194 die_type (struct die_info *die, struct dwarf2_cu *cu)
12195 {
12196   struct attribute *type_attr;
12197
12198   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12199   if (!type_attr)
12200     {
12201       /* A missing DW_AT_type represents a void type.  */
12202       return objfile_type (cu->objfile)->builtin_void;
12203     }
12204
12205   return lookup_die_type (die, type_attr, cu);
12206 }
12207
12208 /* True iff CU's producer generates GNAT Ada auxiliary information
12209    that allows to find parallel types through that information instead
12210    of having to do expensive parallel lookups by type name.  */
12211
12212 static int
12213 need_gnat_info (struct dwarf2_cu *cu)
12214 {
12215   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12216      of GNAT produces this auxiliary information, without any indication
12217      that it is produced.  Part of enhancing the FSF version of GNAT
12218      to produce that information will be to put in place an indicator
12219      that we can use in order to determine whether the descriptive type
12220      info is available or not.  One suggestion that has been made is
12221      to use a new attribute, attached to the CU die.  For now, assume
12222      that the descriptive type info is not available.  */
12223   return 0;
12224 }
12225
12226 /* Return the auxiliary type of the die in question using its
12227    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12228    attribute is not present.  */
12229
12230 static struct type *
12231 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12232 {
12233   struct attribute *type_attr;
12234
12235   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12236   if (!type_attr)
12237     return NULL;
12238
12239   return lookup_die_type (die, type_attr, cu);
12240 }
12241
12242 /* If DIE has a descriptive_type attribute, then set the TYPE's
12243    descriptive type accordingly.  */
12244
12245 static void
12246 set_descriptive_type (struct type *type, struct die_info *die,
12247                       struct dwarf2_cu *cu)
12248 {
12249   struct type *descriptive_type = die_descriptive_type (die, cu);
12250
12251   if (descriptive_type)
12252     {
12253       ALLOCATE_GNAT_AUX_TYPE (type);
12254       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12255     }
12256 }
12257
12258 /* Return the containing type of the die in question using its
12259    DW_AT_containing_type attribute.  */
12260
12261 static struct type *
12262 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12263 {
12264   struct attribute *type_attr;
12265
12266   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12267   if (!type_attr)
12268     error (_("Dwarf Error: Problem turning containing type into gdb type "
12269              "[in module %s]"), cu->objfile->name);
12270
12271   return lookup_die_type (die, type_attr, cu);
12272 }
12273
12274 /* Look up the type of DIE in CU using its type attribute ATTR.
12275    If there is no type substitute an error marker.  */
12276
12277 static struct type *
12278 lookup_die_type (struct die_info *die, struct attribute *attr,
12279                  struct dwarf2_cu *cu)
12280 {
12281   struct type *this_type;
12282
12283   /* First see if we have it cached.  */
12284
12285   if (is_ref_attr (attr))
12286     {
12287       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12288
12289       this_type = get_die_type_at_offset (offset, cu->per_cu);
12290     }
12291   else if (attr->form == DW_FORM_ref_sig8)
12292     {
12293       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12294       struct dwarf2_cu *sig_cu;
12295       unsigned int offset;
12296
12297       /* sig_type will be NULL if the signatured type is missing from
12298          the debug info.  */
12299       if (sig_type == NULL)
12300         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12301                  "at 0x%x [in module %s]"),
12302                die->offset, cu->objfile->name);
12303
12304       gdb_assert (sig_type->per_cu.debug_type_section);
12305       offset = sig_type->per_cu.offset + sig_type->type_offset;
12306       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12307     }
12308   else
12309     {
12310       dump_die_for_error (die);
12311       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12312              dwarf_attr_name (attr->name), cu->objfile->name);
12313     }
12314
12315   /* If not cached we need to read it in.  */
12316
12317   if (this_type == NULL)
12318     {
12319       struct die_info *type_die;
12320       struct dwarf2_cu *type_cu = cu;
12321
12322       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12323       /* If the type is cached, we should have found it above.  */
12324       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12325       this_type = read_type_die_1 (type_die, type_cu);
12326     }
12327
12328   /* If we still don't have a type use an error marker.  */
12329
12330   if (this_type == NULL)
12331     {
12332       char *message, *saved;
12333
12334       /* read_type_die already issued a complaint.  */
12335       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12336                             cu->objfile->name,
12337                             cu->header.offset,
12338                             die->offset);
12339       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12340                              message, strlen (message));
12341       xfree (message);
12342
12343       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12344     }
12345
12346   return this_type;
12347 }
12348
12349 /* Return the type in DIE, CU.
12350    Returns NULL for invalid types.
12351
12352    This first does a lookup in the appropriate type_hash table,
12353    and only reads the die in if necessary.
12354
12355    NOTE: This can be called when reading in partial or full symbols.  */
12356
12357 static struct type *
12358 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12359 {
12360   struct type *this_type;
12361
12362   this_type = get_die_type (die, cu);
12363   if (this_type)
12364     return this_type;
12365
12366   return read_type_die_1 (die, cu);
12367 }
12368
12369 /* Read the type in DIE, CU.
12370    Returns NULL for invalid types.  */
12371
12372 static struct type *
12373 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12374 {
12375   struct type *this_type = NULL;
12376
12377   switch (die->tag)
12378     {
12379     case DW_TAG_class_type:
12380     case DW_TAG_interface_type:
12381     case DW_TAG_structure_type:
12382     case DW_TAG_union_type:
12383       this_type = read_structure_type (die, cu);
12384       break;
12385     case DW_TAG_enumeration_type:
12386       this_type = read_enumeration_type (die, cu);
12387       break;
12388     case DW_TAG_subprogram:
12389     case DW_TAG_subroutine_type:
12390     case DW_TAG_inlined_subroutine:
12391       this_type = read_subroutine_type (die, cu);
12392       break;
12393     case DW_TAG_array_type:
12394       this_type = read_array_type (die, cu);
12395       break;
12396     case DW_TAG_set_type:
12397       this_type = read_set_type (die, cu);
12398       break;
12399     case DW_TAG_pointer_type:
12400       this_type = read_tag_pointer_type (die, cu);
12401       break;
12402     case DW_TAG_ptr_to_member_type:
12403       this_type = read_tag_ptr_to_member_type (die, cu);
12404       break;
12405     case DW_TAG_reference_type:
12406       this_type = read_tag_reference_type (die, cu);
12407       break;
12408     case DW_TAG_const_type:
12409       this_type = read_tag_const_type (die, cu);
12410       break;
12411     case DW_TAG_volatile_type:
12412       this_type = read_tag_volatile_type (die, cu);
12413       break;
12414     case DW_TAG_string_type:
12415       this_type = read_tag_string_type (die, cu);
12416       break;
12417     case DW_TAG_typedef:
12418       this_type = read_typedef (die, cu);
12419       break;
12420     case DW_TAG_subrange_type:
12421       this_type = read_subrange_type (die, cu);
12422       break;
12423     case DW_TAG_base_type:
12424       this_type = read_base_type (die, cu);
12425       break;
12426     case DW_TAG_unspecified_type:
12427       this_type = read_unspecified_type (die, cu);
12428       break;
12429     case DW_TAG_namespace:
12430       this_type = read_namespace_type (die, cu);
12431       break;
12432     case DW_TAG_module:
12433       this_type = read_module_type (die, cu);
12434       break;
12435     default:
12436       complaint (&symfile_complaints,
12437                  _("unexpected tag in read_type_die: '%s'"),
12438                  dwarf_tag_name (die->tag));
12439       break;
12440     }
12441
12442   return this_type;
12443 }
12444
12445 /* See if we can figure out if the class lives in a namespace.  We do
12446    this by looking for a member function; its demangled name will
12447    contain namespace info, if there is any.
12448    Return the computed name or NULL.
12449    Space for the result is allocated on the objfile's obstack.
12450    This is the full-die version of guess_partial_die_structure_name.
12451    In this case we know DIE has no useful parent.  */
12452
12453 static char *
12454 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12455 {
12456   struct die_info *spec_die;
12457   struct dwarf2_cu *spec_cu;
12458   struct die_info *child;
12459
12460   spec_cu = cu;
12461   spec_die = die_specification (die, &spec_cu);
12462   if (spec_die != NULL)
12463     {
12464       die = spec_die;
12465       cu = spec_cu;
12466     }
12467
12468   for (child = die->child;
12469        child != NULL;
12470        child = child->sibling)
12471     {
12472       if (child->tag == DW_TAG_subprogram)
12473         {
12474           struct attribute *attr;
12475
12476           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12477           if (attr == NULL)
12478             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12479           if (attr != NULL)
12480             {
12481               char *actual_name
12482                 = language_class_name_from_physname (cu->language_defn,
12483                                                      DW_STRING (attr));
12484               char *name = NULL;
12485
12486               if (actual_name != NULL)
12487                 {
12488                   char *die_name = dwarf2_name (die, cu);
12489
12490                   if (die_name != NULL
12491                       && strcmp (die_name, actual_name) != 0)
12492                     {
12493                       /* Strip off the class name from the full name.
12494                          We want the prefix.  */
12495                       int die_name_len = strlen (die_name);
12496                       int actual_name_len = strlen (actual_name);
12497
12498                       /* Test for '::' as a sanity check.  */
12499                       if (actual_name_len > die_name_len + 2
12500                           && actual_name[actual_name_len
12501                                          - die_name_len - 1] == ':')
12502                         name =
12503                           obsavestring (actual_name,
12504                                         actual_name_len - die_name_len - 2,
12505                                         &cu->objfile->objfile_obstack);
12506                     }
12507                 }
12508               xfree (actual_name);
12509               return name;
12510             }
12511         }
12512     }
12513
12514   return NULL;
12515 }
12516
12517 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12518    prefix part in such case.  See
12519    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12520
12521 static char *
12522 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12523 {
12524   struct attribute *attr;
12525   char *base;
12526
12527   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12528       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12529     return NULL;
12530
12531   attr = dwarf2_attr (die, DW_AT_name, cu);
12532   if (attr != NULL && DW_STRING (attr) != NULL)
12533     return NULL;
12534
12535   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12536   if (attr == NULL)
12537     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12538   if (attr == NULL || DW_STRING (attr) == NULL)
12539     return NULL;
12540
12541   /* dwarf2_name had to be already called.  */
12542   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12543
12544   /* Strip the base name, keep any leading namespaces/classes.  */
12545   base = strrchr (DW_STRING (attr), ':');
12546   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12547     return "";
12548
12549   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12550                        &cu->objfile->objfile_obstack);
12551 }
12552
12553 /* Return the name of the namespace/class that DIE is defined within,
12554    or "" if we can't tell.  The caller should not xfree the result.
12555
12556    For example, if we're within the method foo() in the following
12557    code:
12558
12559    namespace N {
12560      class C {
12561        void foo () {
12562        }
12563      };
12564    }
12565
12566    then determine_prefix on foo's die will return "N::C".  */
12567
12568 static char *
12569 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12570 {
12571   struct die_info *parent, *spec_die;
12572   struct dwarf2_cu *spec_cu;
12573   struct type *parent_type;
12574   char *retval;
12575
12576   if (cu->language != language_cplus && cu->language != language_java
12577       && cu->language != language_fortran)
12578     return "";
12579
12580   retval = anonymous_struct_prefix (die, cu);
12581   if (retval)
12582     return retval;
12583
12584   /* We have to be careful in the presence of DW_AT_specification.
12585      For example, with GCC 3.4, given the code
12586
12587      namespace N {
12588        void foo() {
12589          // Definition of N::foo.
12590        }
12591      }
12592
12593      then we'll have a tree of DIEs like this:
12594
12595      1: DW_TAG_compile_unit
12596        2: DW_TAG_namespace        // N
12597          3: DW_TAG_subprogram     // declaration of N::foo
12598        4: DW_TAG_subprogram       // definition of N::foo
12599             DW_AT_specification   // refers to die #3
12600
12601      Thus, when processing die #4, we have to pretend that we're in
12602      the context of its DW_AT_specification, namely the contex of die
12603      #3.  */
12604   spec_cu = cu;
12605   spec_die = die_specification (die, &spec_cu);
12606   if (spec_die == NULL)
12607     parent = die->parent;
12608   else
12609     {
12610       parent = spec_die->parent;
12611       cu = spec_cu;
12612     }
12613
12614   if (parent == NULL)
12615     return "";
12616   else if (parent->building_fullname)
12617     {
12618       const char *name;
12619       const char *parent_name;
12620
12621       /* It has been seen on RealView 2.2 built binaries,
12622          DW_TAG_template_type_param types actually _defined_ as
12623          children of the parent class:
12624
12625          enum E {};
12626          template class <class Enum> Class{};
12627          Class<enum E> class_e;
12628
12629          1: DW_TAG_class_type (Class)
12630            2: DW_TAG_enumeration_type (E)
12631              3: DW_TAG_enumerator (enum1:0)
12632              3: DW_TAG_enumerator (enum2:1)
12633              ...
12634            2: DW_TAG_template_type_param
12635               DW_AT_type  DW_FORM_ref_udata (E)
12636
12637          Besides being broken debug info, it can put GDB into an
12638          infinite loop.  Consider:
12639
12640          When we're building the full name for Class<E>, we'll start
12641          at Class, and go look over its template type parameters,
12642          finding E.  We'll then try to build the full name of E, and
12643          reach here.  We're now trying to build the full name of E,
12644          and look over the parent DIE for containing scope.  In the
12645          broken case, if we followed the parent DIE of E, we'd again
12646          find Class, and once again go look at its template type
12647          arguments, etc., etc.  Simply don't consider such parent die
12648          as source-level parent of this die (it can't be, the language
12649          doesn't allow it), and break the loop here.  */
12650       name = dwarf2_name (die, cu);
12651       parent_name = dwarf2_name (parent, cu);
12652       complaint (&symfile_complaints,
12653                  _("template param type '%s' defined within parent '%s'"),
12654                  name ? name : "<unknown>",
12655                  parent_name ? parent_name : "<unknown>");
12656       return "";
12657     }
12658   else
12659     switch (parent->tag)
12660       {
12661       case DW_TAG_namespace:
12662         parent_type = read_type_die (parent, cu);
12663         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12664            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12665            Work around this problem here.  */
12666         if (cu->language == language_cplus
12667             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12668           return "";
12669         /* We give a name to even anonymous namespaces.  */
12670         return TYPE_TAG_NAME (parent_type);
12671       case DW_TAG_class_type:
12672       case DW_TAG_interface_type:
12673       case DW_TAG_structure_type:
12674       case DW_TAG_union_type:
12675       case DW_TAG_module:
12676         parent_type = read_type_die (parent, cu);
12677         if (TYPE_TAG_NAME (parent_type) != NULL)
12678           return TYPE_TAG_NAME (parent_type);
12679         else
12680           /* An anonymous structure is only allowed non-static data
12681              members; no typedefs, no member functions, et cetera.
12682              So it does not need a prefix.  */
12683           return "";
12684       case DW_TAG_compile_unit:
12685         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12686         if (cu->language == language_cplus
12687             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12688             && die->child != NULL
12689             && (die->tag == DW_TAG_class_type
12690                 || die->tag == DW_TAG_structure_type
12691                 || die->tag == DW_TAG_union_type))
12692           {
12693             char *name = guess_full_die_structure_name (die, cu);
12694             if (name != NULL)
12695               return name;
12696           }
12697         return "";
12698       default:
12699         return determine_prefix (parent, cu);
12700       }
12701 }
12702
12703 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12704    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12705    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12706    an obconcat, otherwise allocate storage for the result.  The CU argument is
12707    used to determine the language and hence, the appropriate separator.  */
12708
12709 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12710
12711 static char *
12712 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12713                  int physname, struct dwarf2_cu *cu)
12714 {
12715   const char *lead = "";
12716   const char *sep;
12717
12718   if (suffix == NULL || suffix[0] == '\0'
12719       || prefix == NULL || prefix[0] == '\0')
12720     sep = "";
12721   else if (cu->language == language_java)
12722     sep = ".";
12723   else if (cu->language == language_fortran && physname)
12724     {
12725       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12726          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12727
12728       lead = "__";
12729       sep = "_MOD_";
12730     }
12731   else
12732     sep = "::";
12733
12734   if (prefix == NULL)
12735     prefix = "";
12736   if (suffix == NULL)
12737     suffix = "";
12738
12739   if (obs == NULL)
12740     {
12741       char *retval
12742         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12743
12744       strcpy (retval, lead);
12745       strcat (retval, prefix);
12746       strcat (retval, sep);
12747       strcat (retval, suffix);
12748       return retval;
12749     }
12750   else
12751     {
12752       /* We have an obstack.  */
12753       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12754     }
12755 }
12756
12757 /* Return sibling of die, NULL if no sibling.  */
12758
12759 static struct die_info *
12760 sibling_die (struct die_info *die)
12761 {
12762   return die->sibling;
12763 }
12764
12765 /* Get name of a die, return NULL if not found.  */
12766
12767 static char *
12768 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12769                           struct obstack *obstack)
12770 {
12771   if (name && cu->language == language_cplus)
12772     {
12773       char *canon_name = cp_canonicalize_string (name);
12774
12775       if (canon_name != NULL)
12776         {
12777           if (strcmp (canon_name, name) != 0)
12778             name = obsavestring (canon_name, strlen (canon_name),
12779                                  obstack);
12780           xfree (canon_name);
12781         }
12782     }
12783
12784   return name;
12785 }
12786
12787 /* Get name of a die, return NULL if not found.  */
12788
12789 static char *
12790 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12791 {
12792   struct attribute *attr;
12793
12794   attr = dwarf2_attr (die, DW_AT_name, cu);
12795   if ((!attr || !DW_STRING (attr))
12796       && die->tag != DW_TAG_class_type
12797       && die->tag != DW_TAG_interface_type
12798       && die->tag != DW_TAG_structure_type
12799       && die->tag != DW_TAG_union_type)
12800     return NULL;
12801
12802   switch (die->tag)
12803     {
12804     case DW_TAG_compile_unit:
12805       /* Compilation units have a DW_AT_name that is a filename, not
12806          a source language identifier.  */
12807     case DW_TAG_enumeration_type:
12808     case DW_TAG_enumerator:
12809       /* These tags always have simple identifiers already; no need
12810          to canonicalize them.  */
12811       return DW_STRING (attr);
12812
12813     case DW_TAG_subprogram:
12814       /* Java constructors will all be named "<init>", so return
12815          the class name when we see this special case.  */
12816       if (cu->language == language_java
12817           && DW_STRING (attr) != NULL
12818           && strcmp (DW_STRING (attr), "<init>") == 0)
12819         {
12820           struct dwarf2_cu *spec_cu = cu;
12821           struct die_info *spec_die;
12822
12823           /* GCJ will output '<init>' for Java constructor names.
12824              For this special case, return the name of the parent class.  */
12825
12826           /* GCJ may output suprogram DIEs with AT_specification set.
12827              If so, use the name of the specified DIE.  */
12828           spec_die = die_specification (die, &spec_cu);
12829           if (spec_die != NULL)
12830             return dwarf2_name (spec_die, spec_cu);
12831
12832           do
12833             {
12834               die = die->parent;
12835               if (die->tag == DW_TAG_class_type)
12836                 return dwarf2_name (die, cu);
12837             }
12838           while (die->tag != DW_TAG_compile_unit);
12839         }
12840       break;
12841
12842     case DW_TAG_class_type:
12843     case DW_TAG_interface_type:
12844     case DW_TAG_structure_type:
12845     case DW_TAG_union_type:
12846       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12847          structures or unions.  These were of the form "._%d" in GCC 4.1,
12848          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12849          and GCC 4.4.  We work around this problem by ignoring these.  */
12850       if (attr && DW_STRING (attr)
12851           && (strncmp (DW_STRING (attr), "._", 2) == 0
12852               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12853         return NULL;
12854
12855       /* GCC might emit a nameless typedef that has a linkage name.  See
12856          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12857       if (!attr || DW_STRING (attr) == NULL)
12858         {
12859           char *demangled = NULL;
12860
12861           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12862           if (attr == NULL)
12863             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12864
12865           if (attr == NULL || DW_STRING (attr) == NULL)
12866             return NULL;
12867
12868           /* Avoid demangling DW_STRING (attr) the second time on a second
12869              call for the same DIE.  */
12870           if (!DW_STRING_IS_CANONICAL (attr))
12871             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12872
12873           if (demangled)
12874             {
12875               char *base;
12876
12877               /* FIXME: we already did this for the partial symbol... */
12878               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12879                                                &cu->objfile->objfile_obstack);
12880               DW_STRING_IS_CANONICAL (attr) = 1;
12881               xfree (demangled);
12882
12883               /* Strip any leading namespaces/classes, keep only the base name.
12884                  DW_AT_name for named DIEs does not contain the prefixes.  */
12885               base = strrchr (DW_STRING (attr), ':');
12886               if (base && base > DW_STRING (attr) && base[-1] == ':')
12887                 return &base[1];
12888               else
12889                 return DW_STRING (attr);
12890             }
12891         }
12892       break;
12893
12894     default:
12895       break;
12896     }
12897
12898   if (!DW_STRING_IS_CANONICAL (attr))
12899     {
12900       DW_STRING (attr)
12901         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12902                                     &cu->objfile->objfile_obstack);
12903       DW_STRING_IS_CANONICAL (attr) = 1;
12904     }
12905   return DW_STRING (attr);
12906 }
12907
12908 /* Return the die that this die in an extension of, or NULL if there
12909    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12910    containing the return value on output.  */
12911
12912 static struct die_info *
12913 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12914 {
12915   struct attribute *attr;
12916
12917   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12918   if (attr == NULL)
12919     return NULL;
12920
12921   return follow_die_ref (die, attr, ext_cu);
12922 }
12923
12924 /* Convert a DIE tag into its string name.  */
12925
12926 static char *
12927 dwarf_tag_name (unsigned tag)
12928 {
12929   switch (tag)
12930     {
12931     case DW_TAG_padding:
12932       return "DW_TAG_padding";
12933     case DW_TAG_array_type:
12934       return "DW_TAG_array_type";
12935     case DW_TAG_class_type:
12936       return "DW_TAG_class_type";
12937     case DW_TAG_entry_point:
12938       return "DW_TAG_entry_point";
12939     case DW_TAG_enumeration_type:
12940       return "DW_TAG_enumeration_type";
12941     case DW_TAG_formal_parameter:
12942       return "DW_TAG_formal_parameter";
12943     case DW_TAG_imported_declaration:
12944       return "DW_TAG_imported_declaration";
12945     case DW_TAG_label:
12946       return "DW_TAG_label";
12947     case DW_TAG_lexical_block:
12948       return "DW_TAG_lexical_block";
12949     case DW_TAG_member:
12950       return "DW_TAG_member";
12951     case DW_TAG_pointer_type:
12952       return "DW_TAG_pointer_type";
12953     case DW_TAG_reference_type:
12954       return "DW_TAG_reference_type";
12955     case DW_TAG_compile_unit:
12956       return "DW_TAG_compile_unit";
12957     case DW_TAG_string_type:
12958       return "DW_TAG_string_type";
12959     case DW_TAG_structure_type:
12960       return "DW_TAG_structure_type";
12961     case DW_TAG_subroutine_type:
12962       return "DW_TAG_subroutine_type";
12963     case DW_TAG_typedef:
12964       return "DW_TAG_typedef";
12965     case DW_TAG_union_type:
12966       return "DW_TAG_union_type";
12967     case DW_TAG_unspecified_parameters:
12968       return "DW_TAG_unspecified_parameters";
12969     case DW_TAG_variant:
12970       return "DW_TAG_variant";
12971     case DW_TAG_common_block:
12972       return "DW_TAG_common_block";
12973     case DW_TAG_common_inclusion:
12974       return "DW_TAG_common_inclusion";
12975     case DW_TAG_inheritance:
12976       return "DW_TAG_inheritance";
12977     case DW_TAG_inlined_subroutine:
12978       return "DW_TAG_inlined_subroutine";
12979     case DW_TAG_module:
12980       return "DW_TAG_module";
12981     case DW_TAG_ptr_to_member_type:
12982       return "DW_TAG_ptr_to_member_type";
12983     case DW_TAG_set_type:
12984       return "DW_TAG_set_type";
12985     case DW_TAG_subrange_type:
12986       return "DW_TAG_subrange_type";
12987     case DW_TAG_with_stmt:
12988       return "DW_TAG_with_stmt";
12989     case DW_TAG_access_declaration:
12990       return "DW_TAG_access_declaration";
12991     case DW_TAG_base_type:
12992       return "DW_TAG_base_type";
12993     case DW_TAG_catch_block:
12994       return "DW_TAG_catch_block";
12995     case DW_TAG_const_type:
12996       return "DW_TAG_const_type";
12997     case DW_TAG_constant:
12998       return "DW_TAG_constant";
12999     case DW_TAG_enumerator:
13000       return "DW_TAG_enumerator";
13001     case DW_TAG_file_type:
13002       return "DW_TAG_file_type";
13003     case DW_TAG_friend:
13004       return "DW_TAG_friend";
13005     case DW_TAG_namelist:
13006       return "DW_TAG_namelist";
13007     case DW_TAG_namelist_item:
13008       return "DW_TAG_namelist_item";
13009     case DW_TAG_packed_type:
13010       return "DW_TAG_packed_type";
13011     case DW_TAG_subprogram:
13012       return "DW_TAG_subprogram";
13013     case DW_TAG_template_type_param:
13014       return "DW_TAG_template_type_param";
13015     case DW_TAG_template_value_param:
13016       return "DW_TAG_template_value_param";
13017     case DW_TAG_thrown_type:
13018       return "DW_TAG_thrown_type";
13019     case DW_TAG_try_block:
13020       return "DW_TAG_try_block";
13021     case DW_TAG_variant_part:
13022       return "DW_TAG_variant_part";
13023     case DW_TAG_variable:
13024       return "DW_TAG_variable";
13025     case DW_TAG_volatile_type:
13026       return "DW_TAG_volatile_type";
13027     case DW_TAG_dwarf_procedure:
13028       return "DW_TAG_dwarf_procedure";
13029     case DW_TAG_restrict_type:
13030       return "DW_TAG_restrict_type";
13031     case DW_TAG_interface_type:
13032       return "DW_TAG_interface_type";
13033     case DW_TAG_namespace:
13034       return "DW_TAG_namespace";
13035     case DW_TAG_imported_module:
13036       return "DW_TAG_imported_module";
13037     case DW_TAG_unspecified_type:
13038       return "DW_TAG_unspecified_type";
13039     case DW_TAG_partial_unit:
13040       return "DW_TAG_partial_unit";
13041     case DW_TAG_imported_unit:
13042       return "DW_TAG_imported_unit";
13043     case DW_TAG_condition:
13044       return "DW_TAG_condition";
13045     case DW_TAG_shared_type:
13046       return "DW_TAG_shared_type";
13047     case DW_TAG_type_unit:
13048       return "DW_TAG_type_unit";
13049     case DW_TAG_MIPS_loop:
13050       return "DW_TAG_MIPS_loop";
13051     case DW_TAG_HP_array_descriptor:
13052       return "DW_TAG_HP_array_descriptor";
13053     case DW_TAG_format_label:
13054       return "DW_TAG_format_label";
13055     case DW_TAG_function_template:
13056       return "DW_TAG_function_template";
13057     case DW_TAG_class_template:
13058       return "DW_TAG_class_template";
13059     case DW_TAG_GNU_BINCL:
13060       return "DW_TAG_GNU_BINCL";
13061     case DW_TAG_GNU_EINCL:
13062       return "DW_TAG_GNU_EINCL";
13063     case DW_TAG_upc_shared_type:
13064       return "DW_TAG_upc_shared_type";
13065     case DW_TAG_upc_strict_type:
13066       return "DW_TAG_upc_strict_type";
13067     case DW_TAG_upc_relaxed_type:
13068       return "DW_TAG_upc_relaxed_type";
13069     case DW_TAG_PGI_kanji_type:
13070       return "DW_TAG_PGI_kanji_type";
13071     case DW_TAG_PGI_interface_block:
13072       return "DW_TAG_PGI_interface_block";
13073     case DW_TAG_GNU_call_site:
13074       return "DW_TAG_GNU_call_site";
13075     default:
13076       return "DW_TAG_<unknown>";
13077     }
13078 }
13079
13080 /* Convert a DWARF attribute code into its string name.  */
13081
13082 static char *
13083 dwarf_attr_name (unsigned attr)
13084 {
13085   switch (attr)
13086     {
13087     case DW_AT_sibling:
13088       return "DW_AT_sibling";
13089     case DW_AT_location:
13090       return "DW_AT_location";
13091     case DW_AT_name:
13092       return "DW_AT_name";
13093     case DW_AT_ordering:
13094       return "DW_AT_ordering";
13095     case DW_AT_subscr_data:
13096       return "DW_AT_subscr_data";
13097     case DW_AT_byte_size:
13098       return "DW_AT_byte_size";
13099     case DW_AT_bit_offset:
13100       return "DW_AT_bit_offset";
13101     case DW_AT_bit_size:
13102       return "DW_AT_bit_size";
13103     case DW_AT_element_list:
13104       return "DW_AT_element_list";
13105     case DW_AT_stmt_list:
13106       return "DW_AT_stmt_list";
13107     case DW_AT_low_pc:
13108       return "DW_AT_low_pc";
13109     case DW_AT_high_pc:
13110       return "DW_AT_high_pc";
13111     case DW_AT_language:
13112       return "DW_AT_language";
13113     case DW_AT_member:
13114       return "DW_AT_member";
13115     case DW_AT_discr:
13116       return "DW_AT_discr";
13117     case DW_AT_discr_value:
13118       return "DW_AT_discr_value";
13119     case DW_AT_visibility:
13120       return "DW_AT_visibility";
13121     case DW_AT_import:
13122       return "DW_AT_import";
13123     case DW_AT_string_length:
13124       return "DW_AT_string_length";
13125     case DW_AT_common_reference:
13126       return "DW_AT_common_reference";
13127     case DW_AT_comp_dir:
13128       return "DW_AT_comp_dir";
13129     case DW_AT_const_value:
13130       return "DW_AT_const_value";
13131     case DW_AT_containing_type:
13132       return "DW_AT_containing_type";
13133     case DW_AT_default_value:
13134       return "DW_AT_default_value";
13135     case DW_AT_inline:
13136       return "DW_AT_inline";
13137     case DW_AT_is_optional:
13138       return "DW_AT_is_optional";
13139     case DW_AT_lower_bound:
13140       return "DW_AT_lower_bound";
13141     case DW_AT_producer:
13142       return "DW_AT_producer";
13143     case DW_AT_prototyped:
13144       return "DW_AT_prototyped";
13145     case DW_AT_return_addr:
13146       return "DW_AT_return_addr";
13147     case DW_AT_start_scope:
13148       return "DW_AT_start_scope";
13149     case DW_AT_bit_stride:
13150       return "DW_AT_bit_stride";
13151     case DW_AT_upper_bound:
13152       return "DW_AT_upper_bound";
13153     case DW_AT_abstract_origin:
13154       return "DW_AT_abstract_origin";
13155     case DW_AT_accessibility:
13156       return "DW_AT_accessibility";
13157     case DW_AT_address_class:
13158       return "DW_AT_address_class";
13159     case DW_AT_artificial:
13160       return "DW_AT_artificial";
13161     case DW_AT_base_types:
13162       return "DW_AT_base_types";
13163     case DW_AT_calling_convention:
13164       return "DW_AT_calling_convention";
13165     case DW_AT_count:
13166       return "DW_AT_count";
13167     case DW_AT_data_member_location:
13168       return "DW_AT_data_member_location";
13169     case DW_AT_decl_column:
13170       return "DW_AT_decl_column";
13171     case DW_AT_decl_file:
13172       return "DW_AT_decl_file";
13173     case DW_AT_decl_line:
13174       return "DW_AT_decl_line";
13175     case DW_AT_declaration:
13176       return "DW_AT_declaration";
13177     case DW_AT_discr_list:
13178       return "DW_AT_discr_list";
13179     case DW_AT_encoding:
13180       return "DW_AT_encoding";
13181     case DW_AT_external:
13182       return "DW_AT_external";
13183     case DW_AT_frame_base:
13184       return "DW_AT_frame_base";
13185     case DW_AT_friend:
13186       return "DW_AT_friend";
13187     case DW_AT_identifier_case:
13188       return "DW_AT_identifier_case";
13189     case DW_AT_macro_info:
13190       return "DW_AT_macro_info";
13191     case DW_AT_namelist_items:
13192       return "DW_AT_namelist_items";
13193     case DW_AT_priority:
13194       return "DW_AT_priority";
13195     case DW_AT_segment:
13196       return "DW_AT_segment";
13197     case DW_AT_specification:
13198       return "DW_AT_specification";
13199     case DW_AT_static_link:
13200       return "DW_AT_static_link";
13201     case DW_AT_type:
13202       return "DW_AT_type";
13203     case DW_AT_use_location:
13204       return "DW_AT_use_location";
13205     case DW_AT_variable_parameter:
13206       return "DW_AT_variable_parameter";
13207     case DW_AT_virtuality:
13208       return "DW_AT_virtuality";
13209     case DW_AT_vtable_elem_location:
13210       return "DW_AT_vtable_elem_location";
13211     /* DWARF 3 values.  */
13212     case DW_AT_allocated:
13213       return "DW_AT_allocated";
13214     case DW_AT_associated:
13215       return "DW_AT_associated";
13216     case DW_AT_data_location:
13217       return "DW_AT_data_location";
13218     case DW_AT_byte_stride:
13219       return "DW_AT_byte_stride";
13220     case DW_AT_entry_pc:
13221       return "DW_AT_entry_pc";
13222     case DW_AT_use_UTF8:
13223       return "DW_AT_use_UTF8";
13224     case DW_AT_extension:
13225       return "DW_AT_extension";
13226     case DW_AT_ranges:
13227       return "DW_AT_ranges";
13228     case DW_AT_trampoline:
13229       return "DW_AT_trampoline";
13230     case DW_AT_call_column:
13231       return "DW_AT_call_column";
13232     case DW_AT_call_file:
13233       return "DW_AT_call_file";
13234     case DW_AT_call_line:
13235       return "DW_AT_call_line";
13236     case DW_AT_description:
13237       return "DW_AT_description";
13238     case DW_AT_binary_scale:
13239       return "DW_AT_binary_scale";
13240     case DW_AT_decimal_scale:
13241       return "DW_AT_decimal_scale";
13242     case DW_AT_small:
13243       return "DW_AT_small";
13244     case DW_AT_decimal_sign:
13245       return "DW_AT_decimal_sign";
13246     case DW_AT_digit_count:
13247       return "DW_AT_digit_count";
13248     case DW_AT_picture_string:
13249       return "DW_AT_picture_string";
13250     case DW_AT_mutable:
13251       return "DW_AT_mutable";
13252     case DW_AT_threads_scaled:
13253       return "DW_AT_threads_scaled";
13254     case DW_AT_explicit:
13255       return "DW_AT_explicit";
13256     case DW_AT_object_pointer:
13257       return "DW_AT_object_pointer";
13258     case DW_AT_endianity:
13259       return "DW_AT_endianity";
13260     case DW_AT_elemental:
13261       return "DW_AT_elemental";
13262     case DW_AT_pure:
13263       return "DW_AT_pure";
13264     case DW_AT_recursive:
13265       return "DW_AT_recursive";
13266     /* DWARF 4 values.  */
13267     case DW_AT_signature:
13268       return "DW_AT_signature";
13269     case DW_AT_linkage_name:
13270       return "DW_AT_linkage_name";
13271     /* SGI/MIPS extensions.  */
13272 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13273     case DW_AT_MIPS_fde:
13274       return "DW_AT_MIPS_fde";
13275 #endif
13276     case DW_AT_MIPS_loop_begin:
13277       return "DW_AT_MIPS_loop_begin";
13278     case DW_AT_MIPS_tail_loop_begin:
13279       return "DW_AT_MIPS_tail_loop_begin";
13280     case DW_AT_MIPS_epilog_begin:
13281       return "DW_AT_MIPS_epilog_begin";
13282     case DW_AT_MIPS_loop_unroll_factor:
13283       return "DW_AT_MIPS_loop_unroll_factor";
13284     case DW_AT_MIPS_software_pipeline_depth:
13285       return "DW_AT_MIPS_software_pipeline_depth";
13286     case DW_AT_MIPS_linkage_name:
13287       return "DW_AT_MIPS_linkage_name";
13288     case DW_AT_MIPS_stride:
13289       return "DW_AT_MIPS_stride";
13290     case DW_AT_MIPS_abstract_name:
13291       return "DW_AT_MIPS_abstract_name";
13292     case DW_AT_MIPS_clone_origin:
13293       return "DW_AT_MIPS_clone_origin";
13294     case DW_AT_MIPS_has_inlines:
13295       return "DW_AT_MIPS_has_inlines";
13296     /* HP extensions.  */
13297 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13298     case DW_AT_HP_block_index:
13299       return "DW_AT_HP_block_index";
13300 #endif
13301     case DW_AT_HP_unmodifiable:
13302       return "DW_AT_HP_unmodifiable";
13303     case DW_AT_HP_actuals_stmt_list:
13304       return "DW_AT_HP_actuals_stmt_list";
13305     case DW_AT_HP_proc_per_section:
13306       return "DW_AT_HP_proc_per_section";
13307     case DW_AT_HP_raw_data_ptr:
13308       return "DW_AT_HP_raw_data_ptr";
13309     case DW_AT_HP_pass_by_reference:
13310       return "DW_AT_HP_pass_by_reference";
13311     case DW_AT_HP_opt_level:
13312       return "DW_AT_HP_opt_level";
13313     case DW_AT_HP_prof_version_id:
13314       return "DW_AT_HP_prof_version_id";
13315     case DW_AT_HP_opt_flags:
13316       return "DW_AT_HP_opt_flags";
13317     case DW_AT_HP_cold_region_low_pc:
13318       return "DW_AT_HP_cold_region_low_pc";
13319     case DW_AT_HP_cold_region_high_pc:
13320       return "DW_AT_HP_cold_region_high_pc";
13321     case DW_AT_HP_all_variables_modifiable:
13322       return "DW_AT_HP_all_variables_modifiable";
13323     case DW_AT_HP_linkage_name:
13324       return "DW_AT_HP_linkage_name";
13325     case DW_AT_HP_prof_flags:
13326       return "DW_AT_HP_prof_flags";
13327     /* GNU extensions.  */
13328     case DW_AT_sf_names:
13329       return "DW_AT_sf_names";
13330     case DW_AT_src_info:
13331       return "DW_AT_src_info";
13332     case DW_AT_mac_info:
13333       return "DW_AT_mac_info";
13334     case DW_AT_src_coords:
13335       return "DW_AT_src_coords";
13336     case DW_AT_body_begin:
13337       return "DW_AT_body_begin";
13338     case DW_AT_body_end:
13339       return "DW_AT_body_end";
13340     case DW_AT_GNU_vector:
13341       return "DW_AT_GNU_vector";
13342     case DW_AT_GNU_odr_signature:
13343       return "DW_AT_GNU_odr_signature";
13344     /* VMS extensions.  */
13345     case DW_AT_VMS_rtnbeg_pd_address:
13346       return "DW_AT_VMS_rtnbeg_pd_address";
13347     /* UPC extension.  */
13348     case DW_AT_upc_threads_scaled:
13349       return "DW_AT_upc_threads_scaled";
13350     /* PGI (STMicroelectronics) extensions.  */
13351     case DW_AT_PGI_lbase:
13352       return "DW_AT_PGI_lbase";
13353     case DW_AT_PGI_soffset:
13354       return "DW_AT_PGI_soffset";
13355     case DW_AT_PGI_lstride:
13356       return "DW_AT_PGI_lstride";
13357     default:
13358       return "DW_AT_<unknown>";
13359     }
13360 }
13361
13362 /* Convert a DWARF value form code into its string name.  */
13363
13364 static char *
13365 dwarf_form_name (unsigned form)
13366 {
13367   switch (form)
13368     {
13369     case DW_FORM_addr:
13370       return "DW_FORM_addr";
13371     case DW_FORM_block2:
13372       return "DW_FORM_block2";
13373     case DW_FORM_block4:
13374       return "DW_FORM_block4";
13375     case DW_FORM_data2:
13376       return "DW_FORM_data2";
13377     case DW_FORM_data4:
13378       return "DW_FORM_data4";
13379     case DW_FORM_data8:
13380       return "DW_FORM_data8";
13381     case DW_FORM_string:
13382       return "DW_FORM_string";
13383     case DW_FORM_block:
13384       return "DW_FORM_block";
13385     case DW_FORM_block1:
13386       return "DW_FORM_block1";
13387     case DW_FORM_data1:
13388       return "DW_FORM_data1";
13389     case DW_FORM_flag:
13390       return "DW_FORM_flag";
13391     case DW_FORM_sdata:
13392       return "DW_FORM_sdata";
13393     case DW_FORM_strp:
13394       return "DW_FORM_strp";
13395     case DW_FORM_udata:
13396       return "DW_FORM_udata";
13397     case DW_FORM_ref_addr:
13398       return "DW_FORM_ref_addr";
13399     case DW_FORM_ref1:
13400       return "DW_FORM_ref1";
13401     case DW_FORM_ref2:
13402       return "DW_FORM_ref2";
13403     case DW_FORM_ref4:
13404       return "DW_FORM_ref4";
13405     case DW_FORM_ref8:
13406       return "DW_FORM_ref8";
13407     case DW_FORM_ref_udata:
13408       return "DW_FORM_ref_udata";
13409     case DW_FORM_indirect:
13410       return "DW_FORM_indirect";
13411     case DW_FORM_sec_offset:
13412       return "DW_FORM_sec_offset";
13413     case DW_FORM_exprloc:
13414       return "DW_FORM_exprloc";
13415     case DW_FORM_flag_present:
13416       return "DW_FORM_flag_present";
13417     case DW_FORM_ref_sig8:
13418       return "DW_FORM_ref_sig8";
13419     default:
13420       return "DW_FORM_<unknown>";
13421     }
13422 }
13423
13424 /* Convert a DWARF stack opcode into its string name.  */
13425
13426 const char *
13427 dwarf_stack_op_name (unsigned op)
13428 {
13429   switch (op)
13430     {
13431     case DW_OP_addr:
13432       return "DW_OP_addr";
13433     case DW_OP_deref:
13434       return "DW_OP_deref";
13435     case DW_OP_const1u:
13436       return "DW_OP_const1u";
13437     case DW_OP_const1s:
13438       return "DW_OP_const1s";
13439     case DW_OP_const2u:
13440       return "DW_OP_const2u";
13441     case DW_OP_const2s:
13442       return "DW_OP_const2s";
13443     case DW_OP_const4u:
13444       return "DW_OP_const4u";
13445     case DW_OP_const4s:
13446       return "DW_OP_const4s";
13447     case DW_OP_const8u:
13448       return "DW_OP_const8u";
13449     case DW_OP_const8s:
13450       return "DW_OP_const8s";
13451     case DW_OP_constu:
13452       return "DW_OP_constu";
13453     case DW_OP_consts:
13454       return "DW_OP_consts";
13455     case DW_OP_dup:
13456       return "DW_OP_dup";
13457     case DW_OP_drop:
13458       return "DW_OP_drop";
13459     case DW_OP_over:
13460       return "DW_OP_over";
13461     case DW_OP_pick:
13462       return "DW_OP_pick";
13463     case DW_OP_swap:
13464       return "DW_OP_swap";
13465     case DW_OP_rot:
13466       return "DW_OP_rot";
13467     case DW_OP_xderef:
13468       return "DW_OP_xderef";
13469     case DW_OP_abs:
13470       return "DW_OP_abs";
13471     case DW_OP_and:
13472       return "DW_OP_and";
13473     case DW_OP_div:
13474       return "DW_OP_div";
13475     case DW_OP_minus:
13476       return "DW_OP_minus";
13477     case DW_OP_mod:
13478       return "DW_OP_mod";
13479     case DW_OP_mul:
13480       return "DW_OP_mul";
13481     case DW_OP_neg:
13482       return "DW_OP_neg";
13483     case DW_OP_not:
13484       return "DW_OP_not";
13485     case DW_OP_or:
13486       return "DW_OP_or";
13487     case DW_OP_plus:
13488       return "DW_OP_plus";
13489     case DW_OP_plus_uconst:
13490       return "DW_OP_plus_uconst";
13491     case DW_OP_shl:
13492       return "DW_OP_shl";
13493     case DW_OP_shr:
13494       return "DW_OP_shr";
13495     case DW_OP_shra:
13496       return "DW_OP_shra";
13497     case DW_OP_xor:
13498       return "DW_OP_xor";
13499     case DW_OP_bra:
13500       return "DW_OP_bra";
13501     case DW_OP_eq:
13502       return "DW_OP_eq";
13503     case DW_OP_ge:
13504       return "DW_OP_ge";
13505     case DW_OP_gt:
13506       return "DW_OP_gt";
13507     case DW_OP_le:
13508       return "DW_OP_le";
13509     case DW_OP_lt:
13510       return "DW_OP_lt";
13511     case DW_OP_ne:
13512       return "DW_OP_ne";
13513     case DW_OP_skip:
13514       return "DW_OP_skip";
13515     case DW_OP_lit0:
13516       return "DW_OP_lit0";
13517     case DW_OP_lit1:
13518       return "DW_OP_lit1";
13519     case DW_OP_lit2:
13520       return "DW_OP_lit2";
13521     case DW_OP_lit3:
13522       return "DW_OP_lit3";
13523     case DW_OP_lit4:
13524       return "DW_OP_lit4";
13525     case DW_OP_lit5:
13526       return "DW_OP_lit5";
13527     case DW_OP_lit6:
13528       return "DW_OP_lit6";
13529     case DW_OP_lit7:
13530       return "DW_OP_lit7";
13531     case DW_OP_lit8:
13532       return "DW_OP_lit8";
13533     case DW_OP_lit9:
13534       return "DW_OP_lit9";
13535     case DW_OP_lit10:
13536       return "DW_OP_lit10";
13537     case DW_OP_lit11:
13538       return "DW_OP_lit11";
13539     case DW_OP_lit12:
13540       return "DW_OP_lit12";
13541     case DW_OP_lit13:
13542       return "DW_OP_lit13";
13543     case DW_OP_lit14:
13544       return "DW_OP_lit14";
13545     case DW_OP_lit15:
13546       return "DW_OP_lit15";
13547     case DW_OP_lit16:
13548       return "DW_OP_lit16";
13549     case DW_OP_lit17:
13550       return "DW_OP_lit17";
13551     case DW_OP_lit18:
13552       return "DW_OP_lit18";
13553     case DW_OP_lit19:
13554       return "DW_OP_lit19";
13555     case DW_OP_lit20:
13556       return "DW_OP_lit20";
13557     case DW_OP_lit21:
13558       return "DW_OP_lit21";
13559     case DW_OP_lit22:
13560       return "DW_OP_lit22";
13561     case DW_OP_lit23:
13562       return "DW_OP_lit23";
13563     case DW_OP_lit24:
13564       return "DW_OP_lit24";
13565     case DW_OP_lit25:
13566       return "DW_OP_lit25";
13567     case DW_OP_lit26:
13568       return "DW_OP_lit26";
13569     case DW_OP_lit27:
13570       return "DW_OP_lit27";
13571     case DW_OP_lit28:
13572       return "DW_OP_lit28";
13573     case DW_OP_lit29:
13574       return "DW_OP_lit29";
13575     case DW_OP_lit30:
13576       return "DW_OP_lit30";
13577     case DW_OP_lit31:
13578       return "DW_OP_lit31";
13579     case DW_OP_reg0:
13580       return "DW_OP_reg0";
13581     case DW_OP_reg1:
13582       return "DW_OP_reg1";
13583     case DW_OP_reg2:
13584       return "DW_OP_reg2";
13585     case DW_OP_reg3:
13586       return "DW_OP_reg3";
13587     case DW_OP_reg4:
13588       return "DW_OP_reg4";
13589     case DW_OP_reg5:
13590       return "DW_OP_reg5";
13591     case DW_OP_reg6:
13592       return "DW_OP_reg6";
13593     case DW_OP_reg7:
13594       return "DW_OP_reg7";
13595     case DW_OP_reg8:
13596       return "DW_OP_reg8";
13597     case DW_OP_reg9:
13598       return "DW_OP_reg9";
13599     case DW_OP_reg10:
13600       return "DW_OP_reg10";
13601     case DW_OP_reg11:
13602       return "DW_OP_reg11";
13603     case DW_OP_reg12:
13604       return "DW_OP_reg12";
13605     case DW_OP_reg13:
13606       return "DW_OP_reg13";
13607     case DW_OP_reg14:
13608       return "DW_OP_reg14";
13609     case DW_OP_reg15:
13610       return "DW_OP_reg15";
13611     case DW_OP_reg16:
13612       return "DW_OP_reg16";
13613     case DW_OP_reg17:
13614       return "DW_OP_reg17";
13615     case DW_OP_reg18:
13616       return "DW_OP_reg18";
13617     case DW_OP_reg19:
13618       return "DW_OP_reg19";
13619     case DW_OP_reg20:
13620       return "DW_OP_reg20";
13621     case DW_OP_reg21:
13622       return "DW_OP_reg21";
13623     case DW_OP_reg22:
13624       return "DW_OP_reg22";
13625     case DW_OP_reg23:
13626       return "DW_OP_reg23";
13627     case DW_OP_reg24:
13628       return "DW_OP_reg24";
13629     case DW_OP_reg25:
13630       return "DW_OP_reg25";
13631     case DW_OP_reg26:
13632       return "DW_OP_reg26";
13633     case DW_OP_reg27:
13634       return "DW_OP_reg27";
13635     case DW_OP_reg28:
13636       return "DW_OP_reg28";
13637     case DW_OP_reg29:
13638       return "DW_OP_reg29";
13639     case DW_OP_reg30:
13640       return "DW_OP_reg30";
13641     case DW_OP_reg31:
13642       return "DW_OP_reg31";
13643     case DW_OP_breg0:
13644       return "DW_OP_breg0";
13645     case DW_OP_breg1:
13646       return "DW_OP_breg1";
13647     case DW_OP_breg2:
13648       return "DW_OP_breg2";
13649     case DW_OP_breg3:
13650       return "DW_OP_breg3";
13651     case DW_OP_breg4:
13652       return "DW_OP_breg4";
13653     case DW_OP_breg5:
13654       return "DW_OP_breg5";
13655     case DW_OP_breg6:
13656       return "DW_OP_breg6";
13657     case DW_OP_breg7:
13658       return "DW_OP_breg7";
13659     case DW_OP_breg8:
13660       return "DW_OP_breg8";
13661     case DW_OP_breg9:
13662       return "DW_OP_breg9";
13663     case DW_OP_breg10:
13664       return "DW_OP_breg10";
13665     case DW_OP_breg11:
13666       return "DW_OP_breg11";
13667     case DW_OP_breg12:
13668       return "DW_OP_breg12";
13669     case DW_OP_breg13:
13670       return "DW_OP_breg13";
13671     case DW_OP_breg14:
13672       return "DW_OP_breg14";
13673     case DW_OP_breg15:
13674       return "DW_OP_breg15";
13675     case DW_OP_breg16:
13676       return "DW_OP_breg16";
13677     case DW_OP_breg17:
13678       return "DW_OP_breg17";
13679     case DW_OP_breg18:
13680       return "DW_OP_breg18";
13681     case DW_OP_breg19:
13682       return "DW_OP_breg19";
13683     case DW_OP_breg20:
13684       return "DW_OP_breg20";
13685     case DW_OP_breg21:
13686       return "DW_OP_breg21";
13687     case DW_OP_breg22:
13688       return "DW_OP_breg22";
13689     case DW_OP_breg23:
13690       return "DW_OP_breg23";
13691     case DW_OP_breg24:
13692       return "DW_OP_breg24";
13693     case DW_OP_breg25:
13694       return "DW_OP_breg25";
13695     case DW_OP_breg26:
13696       return "DW_OP_breg26";
13697     case DW_OP_breg27:
13698       return "DW_OP_breg27";
13699     case DW_OP_breg28:
13700       return "DW_OP_breg28";
13701     case DW_OP_breg29:
13702       return "DW_OP_breg29";
13703     case DW_OP_breg30:
13704       return "DW_OP_breg30";
13705     case DW_OP_breg31:
13706       return "DW_OP_breg31";
13707     case DW_OP_regx:
13708       return "DW_OP_regx";
13709     case DW_OP_fbreg:
13710       return "DW_OP_fbreg";
13711     case DW_OP_bregx:
13712       return "DW_OP_bregx";
13713     case DW_OP_piece:
13714       return "DW_OP_piece";
13715     case DW_OP_deref_size:
13716       return "DW_OP_deref_size";
13717     case DW_OP_xderef_size:
13718       return "DW_OP_xderef_size";
13719     case DW_OP_nop:
13720       return "DW_OP_nop";
13721     /* DWARF 3 extensions.  */
13722     case DW_OP_push_object_address:
13723       return "DW_OP_push_object_address";
13724     case DW_OP_call2:
13725       return "DW_OP_call2";
13726     case DW_OP_call4:
13727       return "DW_OP_call4";
13728     case DW_OP_call_ref:
13729       return "DW_OP_call_ref";
13730     case DW_OP_form_tls_address:
13731       return "DW_OP_form_tls_address";
13732     case DW_OP_call_frame_cfa:
13733       return "DW_OP_call_frame_cfa";
13734     case DW_OP_bit_piece:
13735       return "DW_OP_bit_piece";
13736     /* DWARF 4 extensions.  */
13737     case DW_OP_implicit_value:
13738       return "DW_OP_implicit_value";
13739     case DW_OP_stack_value:
13740       return "DW_OP_stack_value";
13741     /* GNU extensions.  */
13742     case DW_OP_GNU_push_tls_address:
13743       return "DW_OP_GNU_push_tls_address";
13744     case DW_OP_GNU_uninit:
13745       return "DW_OP_GNU_uninit";
13746     case DW_OP_GNU_implicit_pointer:
13747       return "DW_OP_GNU_implicit_pointer";
13748     case DW_OP_GNU_entry_value:
13749       return "DW_OP_GNU_entry_value";
13750     case DW_OP_GNU_const_type:
13751       return "DW_OP_GNU_const_type";
13752     case DW_OP_GNU_regval_type:
13753       return "DW_OP_GNU_regval_type";
13754     case DW_OP_GNU_deref_type:
13755       return "DW_OP_GNU_deref_type";
13756     case DW_OP_GNU_convert:
13757       return "DW_OP_GNU_convert";
13758     case DW_OP_GNU_reinterpret:
13759       return "DW_OP_GNU_reinterpret";
13760     default:
13761       return NULL;
13762     }
13763 }
13764
13765 static char *
13766 dwarf_bool_name (unsigned mybool)
13767 {
13768   if (mybool)
13769     return "TRUE";
13770   else
13771     return "FALSE";
13772 }
13773
13774 /* Convert a DWARF type code into its string name.  */
13775
13776 static char *
13777 dwarf_type_encoding_name (unsigned enc)
13778 {
13779   switch (enc)
13780     {
13781     case DW_ATE_void:
13782       return "DW_ATE_void";
13783     case DW_ATE_address:
13784       return "DW_ATE_address";
13785     case DW_ATE_boolean:
13786       return "DW_ATE_boolean";
13787     case DW_ATE_complex_float:
13788       return "DW_ATE_complex_float";
13789     case DW_ATE_float:
13790       return "DW_ATE_float";
13791     case DW_ATE_signed:
13792       return "DW_ATE_signed";
13793     case DW_ATE_signed_char:
13794       return "DW_ATE_signed_char";
13795     case DW_ATE_unsigned:
13796       return "DW_ATE_unsigned";
13797     case DW_ATE_unsigned_char:
13798       return "DW_ATE_unsigned_char";
13799     /* DWARF 3.  */
13800     case DW_ATE_imaginary_float:
13801       return "DW_ATE_imaginary_float";
13802     case DW_ATE_packed_decimal:
13803       return "DW_ATE_packed_decimal";
13804     case DW_ATE_numeric_string:
13805       return "DW_ATE_numeric_string";
13806     case DW_ATE_edited:
13807       return "DW_ATE_edited";
13808     case DW_ATE_signed_fixed:
13809       return "DW_ATE_signed_fixed";
13810     case DW_ATE_unsigned_fixed:
13811       return "DW_ATE_unsigned_fixed";
13812     case DW_ATE_decimal_float:
13813       return "DW_ATE_decimal_float";
13814     /* DWARF 4.  */
13815     case DW_ATE_UTF:
13816       return "DW_ATE_UTF";
13817     /* HP extensions.  */
13818     case DW_ATE_HP_float80:
13819       return "DW_ATE_HP_float80";
13820     case DW_ATE_HP_complex_float80:
13821       return "DW_ATE_HP_complex_float80";
13822     case DW_ATE_HP_float128:
13823       return "DW_ATE_HP_float128";
13824     case DW_ATE_HP_complex_float128:
13825       return "DW_ATE_HP_complex_float128";
13826     case DW_ATE_HP_floathpintel:
13827       return "DW_ATE_HP_floathpintel";
13828     case DW_ATE_HP_imaginary_float80:
13829       return "DW_ATE_HP_imaginary_float80";
13830     case DW_ATE_HP_imaginary_float128:
13831       return "DW_ATE_HP_imaginary_float128";
13832     default:
13833       return "DW_ATE_<unknown>";
13834     }
13835 }
13836
13837 /* Convert a DWARF call frame info operation to its string name.  */
13838
13839 #if 0
13840 static char *
13841 dwarf_cfi_name (unsigned cfi_opc)
13842 {
13843   switch (cfi_opc)
13844     {
13845     case DW_CFA_advance_loc:
13846       return "DW_CFA_advance_loc";
13847     case DW_CFA_offset:
13848       return "DW_CFA_offset";
13849     case DW_CFA_restore:
13850       return "DW_CFA_restore";
13851     case DW_CFA_nop:
13852       return "DW_CFA_nop";
13853     case DW_CFA_set_loc:
13854       return "DW_CFA_set_loc";
13855     case DW_CFA_advance_loc1:
13856       return "DW_CFA_advance_loc1";
13857     case DW_CFA_advance_loc2:
13858       return "DW_CFA_advance_loc2";
13859     case DW_CFA_advance_loc4:
13860       return "DW_CFA_advance_loc4";
13861     case DW_CFA_offset_extended:
13862       return "DW_CFA_offset_extended";
13863     case DW_CFA_restore_extended:
13864       return "DW_CFA_restore_extended";
13865     case DW_CFA_undefined:
13866       return "DW_CFA_undefined";
13867     case DW_CFA_same_value:
13868       return "DW_CFA_same_value";
13869     case DW_CFA_register:
13870       return "DW_CFA_register";
13871     case DW_CFA_remember_state:
13872       return "DW_CFA_remember_state";
13873     case DW_CFA_restore_state:
13874       return "DW_CFA_restore_state";
13875     case DW_CFA_def_cfa:
13876       return "DW_CFA_def_cfa";
13877     case DW_CFA_def_cfa_register:
13878       return "DW_CFA_def_cfa_register";
13879     case DW_CFA_def_cfa_offset:
13880       return "DW_CFA_def_cfa_offset";
13881     /* DWARF 3.  */
13882     case DW_CFA_def_cfa_expression:
13883       return "DW_CFA_def_cfa_expression";
13884     case DW_CFA_expression:
13885       return "DW_CFA_expression";
13886     case DW_CFA_offset_extended_sf:
13887       return "DW_CFA_offset_extended_sf";
13888     case DW_CFA_def_cfa_sf:
13889       return "DW_CFA_def_cfa_sf";
13890     case DW_CFA_def_cfa_offset_sf:
13891       return "DW_CFA_def_cfa_offset_sf";
13892     case DW_CFA_val_offset:
13893       return "DW_CFA_val_offset";
13894     case DW_CFA_val_offset_sf:
13895       return "DW_CFA_val_offset_sf";
13896     case DW_CFA_val_expression:
13897       return "DW_CFA_val_expression";
13898     /* SGI/MIPS specific.  */
13899     case DW_CFA_MIPS_advance_loc8:
13900       return "DW_CFA_MIPS_advance_loc8";
13901     /* GNU extensions.  */
13902     case DW_CFA_GNU_window_save:
13903       return "DW_CFA_GNU_window_save";
13904     case DW_CFA_GNU_args_size:
13905       return "DW_CFA_GNU_args_size";
13906     case DW_CFA_GNU_negative_offset_extended:
13907       return "DW_CFA_GNU_negative_offset_extended";
13908     default:
13909       return "DW_CFA_<unknown>";
13910     }
13911 }
13912 #endif
13913
13914 static void
13915 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13916 {
13917   unsigned int i;
13918
13919   print_spaces (indent, f);
13920   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13921            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13922
13923   if (die->parent != NULL)
13924     {
13925       print_spaces (indent, f);
13926       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13927                           die->parent->offset);
13928     }
13929
13930   print_spaces (indent, f);
13931   fprintf_unfiltered (f, "  has children: %s\n",
13932            dwarf_bool_name (die->child != NULL));
13933
13934   print_spaces (indent, f);
13935   fprintf_unfiltered (f, "  attributes:\n");
13936
13937   for (i = 0; i < die->num_attrs; ++i)
13938     {
13939       print_spaces (indent, f);
13940       fprintf_unfiltered (f, "    %s (%s) ",
13941                dwarf_attr_name (die->attrs[i].name),
13942                dwarf_form_name (die->attrs[i].form));
13943
13944       switch (die->attrs[i].form)
13945         {
13946         case DW_FORM_ref_addr:
13947         case DW_FORM_addr:
13948           fprintf_unfiltered (f, "address: ");
13949           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13950           break;
13951         case DW_FORM_block2:
13952         case DW_FORM_block4:
13953         case DW_FORM_block:
13954         case DW_FORM_block1:
13955           fprintf_unfiltered (f, "block: size %d",
13956                               DW_BLOCK (&die->attrs[i])->size);
13957           break;
13958         case DW_FORM_exprloc:
13959           fprintf_unfiltered (f, "expression: size %u",
13960                               DW_BLOCK (&die->attrs[i])->size);
13961           break;
13962         case DW_FORM_ref1:
13963         case DW_FORM_ref2:
13964         case DW_FORM_ref4:
13965           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13966                               (long) (DW_ADDR (&die->attrs[i])));
13967           break;
13968         case DW_FORM_data1:
13969         case DW_FORM_data2:
13970         case DW_FORM_data4:
13971         case DW_FORM_data8:
13972         case DW_FORM_udata:
13973         case DW_FORM_sdata:
13974           fprintf_unfiltered (f, "constant: %s",
13975                               pulongest (DW_UNSND (&die->attrs[i])));
13976           break;
13977         case DW_FORM_sec_offset:
13978           fprintf_unfiltered (f, "section offset: %s",
13979                               pulongest (DW_UNSND (&die->attrs[i])));
13980           break;
13981         case DW_FORM_ref_sig8:
13982           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13983             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13984                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13985           else
13986             fprintf_unfiltered (f, "signatured type, offset: unknown");
13987           break;
13988         case DW_FORM_string:
13989         case DW_FORM_strp:
13990           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13991                    DW_STRING (&die->attrs[i])
13992                    ? DW_STRING (&die->attrs[i]) : "",
13993                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13994           break;
13995         case DW_FORM_flag:
13996           if (DW_UNSND (&die->attrs[i]))
13997             fprintf_unfiltered (f, "flag: TRUE");
13998           else
13999             fprintf_unfiltered (f, "flag: FALSE");
14000           break;
14001         case DW_FORM_flag_present:
14002           fprintf_unfiltered (f, "flag: TRUE");
14003           break;
14004         case DW_FORM_indirect:
14005           /* The reader will have reduced the indirect form to
14006              the "base form" so this form should not occur.  */
14007           fprintf_unfiltered (f, 
14008                               "unexpected attribute form: DW_FORM_indirect");
14009           break;
14010         default:
14011           fprintf_unfiltered (f, "unsupported attribute form: %d.",
14012                    die->attrs[i].form);
14013           break;
14014         }
14015       fprintf_unfiltered (f, "\n");
14016     }
14017 }
14018
14019 static void
14020 dump_die_for_error (struct die_info *die)
14021 {
14022   dump_die_shallow (gdb_stderr, 0, die);
14023 }
14024
14025 static void
14026 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14027 {
14028   int indent = level * 4;
14029
14030   gdb_assert (die != NULL);
14031
14032   if (level >= max_level)
14033     return;
14034
14035   dump_die_shallow (f, indent, die);
14036
14037   if (die->child != NULL)
14038     {
14039       print_spaces (indent, f);
14040       fprintf_unfiltered (f, "  Children:");
14041       if (level + 1 < max_level)
14042         {
14043           fprintf_unfiltered (f, "\n");
14044           dump_die_1 (f, level + 1, max_level, die->child);
14045         }
14046       else
14047         {
14048           fprintf_unfiltered (f,
14049                               " [not printed, max nesting level reached]\n");
14050         }
14051     }
14052
14053   if (die->sibling != NULL && level > 0)
14054     {
14055       dump_die_1 (f, level, max_level, die->sibling);
14056     }
14057 }
14058
14059 /* This is called from the pdie macro in gdbinit.in.
14060    It's not static so gcc will keep a copy callable from gdb.  */
14061
14062 void
14063 dump_die (struct die_info *die, int max_level)
14064 {
14065   dump_die_1 (gdb_stdlog, 0, max_level, die);
14066 }
14067
14068 static void
14069 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14070 {
14071   void **slot;
14072
14073   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14074
14075   *slot = die;
14076 }
14077
14078 static int
14079 is_ref_attr (struct attribute *attr)
14080 {
14081   switch (attr->form)
14082     {
14083     case DW_FORM_ref_addr:
14084     case DW_FORM_ref1:
14085     case DW_FORM_ref2:
14086     case DW_FORM_ref4:
14087     case DW_FORM_ref8:
14088     case DW_FORM_ref_udata:
14089       return 1;
14090     default:
14091       return 0;
14092     }
14093 }
14094
14095 static unsigned int
14096 dwarf2_get_ref_die_offset (struct attribute *attr)
14097 {
14098   if (is_ref_attr (attr))
14099     return DW_ADDR (attr);
14100
14101   complaint (&symfile_complaints,
14102              _("unsupported die ref attribute form: '%s'"),
14103              dwarf_form_name (attr->form));
14104   return 0;
14105 }
14106
14107 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14108  * the value held by the attribute is not constant.  */
14109
14110 static LONGEST
14111 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14112 {
14113   if (attr->form == DW_FORM_sdata)
14114     return DW_SND (attr);
14115   else if (attr->form == DW_FORM_udata
14116            || attr->form == DW_FORM_data1
14117            || attr->form == DW_FORM_data2
14118            || attr->form == DW_FORM_data4
14119            || attr->form == DW_FORM_data8)
14120     return DW_UNSND (attr);
14121   else
14122     {
14123       complaint (&symfile_complaints,
14124                  _("Attribute value is not a constant (%s)"),
14125                  dwarf_form_name (attr->form));
14126       return default_value;
14127     }
14128 }
14129
14130 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14131    unit and add it to our queue.
14132    The result is non-zero if PER_CU was queued, otherwise the result is zero
14133    meaning either PER_CU is already queued or it is already loaded.  */
14134
14135 static int
14136 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14137                        struct dwarf2_per_cu_data *per_cu)
14138 {
14139   /* We may arrive here during partial symbol reading, if we need full
14140      DIEs to process an unusual case (e.g. template arguments).  Do
14141      not queue PER_CU, just tell our caller to load its DIEs.  */
14142   if (dwarf2_per_objfile->reading_partial_symbols)
14143     {
14144       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14145         return 1;
14146       return 0;
14147     }
14148
14149   /* Mark the dependence relation so that we don't flush PER_CU
14150      too early.  */
14151   dwarf2_add_dependence (this_cu, per_cu);
14152
14153   /* If it's already on the queue, we have nothing to do.  */
14154   if (per_cu->queued)
14155     return 0;
14156
14157   /* If the compilation unit is already loaded, just mark it as
14158      used.  */
14159   if (per_cu->cu != NULL)
14160     {
14161       per_cu->cu->last_used = 0;
14162       return 0;
14163     }
14164
14165   /* Add it to the queue.  */
14166   queue_comp_unit (per_cu, this_cu->objfile);
14167
14168   return 1;
14169 }
14170
14171 /* Follow reference or signature attribute ATTR of SRC_DIE.
14172    On entry *REF_CU is the CU of SRC_DIE.
14173    On exit *REF_CU is the CU of the result.  */
14174
14175 static struct die_info *
14176 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14177                        struct dwarf2_cu **ref_cu)
14178 {
14179   struct die_info *die;
14180
14181   if (is_ref_attr (attr))
14182     die = follow_die_ref (src_die, attr, ref_cu);
14183   else if (attr->form == DW_FORM_ref_sig8)
14184     die = follow_die_sig (src_die, attr, ref_cu);
14185   else
14186     {
14187       dump_die_for_error (src_die);
14188       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14189              (*ref_cu)->objfile->name);
14190     }
14191
14192   return die;
14193 }
14194
14195 /* Follow reference OFFSET.
14196    On entry *REF_CU is the CU of the source die referencing OFFSET.
14197    On exit *REF_CU is the CU of the result.
14198    Returns NULL if OFFSET is invalid.  */
14199
14200 static struct die_info *
14201 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14202 {
14203   struct die_info temp_die;
14204   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14205
14206   gdb_assert (cu->per_cu != NULL);
14207
14208   target_cu = cu;
14209
14210   if (cu->per_cu->debug_type_section)
14211     {
14212       /* .debug_types CUs cannot reference anything outside their CU.
14213          If they need to, they have to reference a signatured type via
14214          DW_FORM_ref_sig8.  */
14215       if (! offset_in_cu_p (&cu->header, offset))
14216         return NULL;
14217     }
14218   else if (! offset_in_cu_p (&cu->header, offset))
14219     {
14220       struct dwarf2_per_cu_data *per_cu;
14221
14222       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14223
14224       /* If necessary, add it to the queue and load its DIEs.  */
14225       if (maybe_queue_comp_unit (cu, per_cu))
14226         load_full_comp_unit (per_cu, cu->objfile);
14227
14228       target_cu = per_cu->cu;
14229     }
14230   else if (cu->dies == NULL)
14231     {
14232       /* We're loading full DIEs during partial symbol reading.  */
14233       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14234       load_full_comp_unit (cu->per_cu, cu->objfile);
14235     }
14236
14237   *ref_cu = target_cu;
14238   temp_die.offset = offset;
14239   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14240 }
14241
14242 /* Follow reference attribute ATTR of SRC_DIE.
14243    On entry *REF_CU is the CU of SRC_DIE.
14244    On exit *REF_CU is the CU of the result.  */
14245
14246 static struct die_info *
14247 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14248                 struct dwarf2_cu **ref_cu)
14249 {
14250   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14251   struct dwarf2_cu *cu = *ref_cu;
14252   struct die_info *die;
14253
14254   die = follow_die_offset (offset, ref_cu);
14255   if (!die)
14256     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14257            "at 0x%x [in module %s]"),
14258            offset, src_die->offset, cu->objfile->name);
14259
14260   return die;
14261 }
14262
14263 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14264    Returned value is intended for DW_OP_call*.  Returned
14265    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14266
14267 struct dwarf2_locexpr_baton
14268 dwarf2_fetch_die_location_block (unsigned int offset,
14269                                  struct dwarf2_per_cu_data *per_cu,
14270                                  CORE_ADDR (*get_frame_pc) (void *baton),
14271                                  void *baton)
14272 {
14273   struct dwarf2_cu *cu;
14274   struct die_info *die;
14275   struct attribute *attr;
14276   struct dwarf2_locexpr_baton retval;
14277
14278   dw2_setup (per_cu->objfile);
14279
14280   if (per_cu->cu == NULL)
14281     load_cu (per_cu);
14282   cu = per_cu->cu;
14283
14284   die = follow_die_offset (offset, &cu);
14285   if (!die)
14286     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14287            offset, per_cu->cu->objfile->name);
14288
14289   attr = dwarf2_attr (die, DW_AT_location, cu);
14290   if (!attr)
14291     {
14292       /* DWARF: "If there is no such attribute, then there is no effect.".
14293          DATA is ignored if SIZE is 0.  */
14294
14295       retval.data = NULL;
14296       retval.size = 0;
14297     }
14298   else if (attr_form_is_section_offset (attr))
14299     {
14300       struct dwarf2_loclist_baton loclist_baton;
14301       CORE_ADDR pc = (*get_frame_pc) (baton);
14302       size_t size;
14303
14304       fill_in_loclist_baton (cu, &loclist_baton, attr);
14305
14306       retval.data = dwarf2_find_location_expression (&loclist_baton,
14307                                                      &size, pc);
14308       retval.size = size;
14309     }
14310   else
14311     {
14312       if (!attr_form_is_block (attr))
14313         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14314                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14315                offset, per_cu->cu->objfile->name);
14316
14317       retval.data = DW_BLOCK (attr)->data;
14318       retval.size = DW_BLOCK (attr)->size;
14319     }
14320   retval.per_cu = cu->per_cu;
14321
14322   age_cached_comp_units ();
14323
14324   return retval;
14325 }
14326
14327 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14328    PER_CU.  */
14329
14330 struct type *
14331 dwarf2_get_die_type (unsigned int die_offset,
14332                      struct dwarf2_per_cu_data *per_cu)
14333 {
14334   dw2_setup (per_cu->objfile);
14335   return get_die_type_at_offset (die_offset, per_cu);
14336 }
14337
14338 /* Follow the signature attribute ATTR in SRC_DIE.
14339    On entry *REF_CU is the CU of SRC_DIE.
14340    On exit *REF_CU is the CU of the result.  */
14341
14342 static struct die_info *
14343 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14344                 struct dwarf2_cu **ref_cu)
14345 {
14346   struct objfile *objfile = (*ref_cu)->objfile;
14347   struct die_info temp_die;
14348   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14349   struct dwarf2_cu *sig_cu;
14350   struct die_info *die;
14351
14352   /* sig_type will be NULL if the signatured type is missing from
14353      the debug info.  */
14354   if (sig_type == NULL)
14355     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14356              "at 0x%x [in module %s]"),
14357            src_die->offset, objfile->name);
14358
14359   /* If necessary, add it to the queue and load its DIEs.  */
14360
14361   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14362     read_signatured_type (objfile, sig_type);
14363
14364   gdb_assert (sig_type->per_cu.cu != NULL);
14365
14366   sig_cu = sig_type->per_cu.cu;
14367   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14368   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14369   if (die)
14370     {
14371       *ref_cu = sig_cu;
14372       return die;
14373     }
14374
14375   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14376          "from DIE at 0x%x [in module %s]"),
14377          sig_type->type_offset, src_die->offset, objfile->name);
14378 }
14379
14380 /* Given an offset of a signatured type, return its signatured_type.  */
14381
14382 static struct signatured_type *
14383 lookup_signatured_type_at_offset (struct objfile *objfile,
14384                                   struct dwarf2_section_info *section,
14385                                   unsigned int offset)
14386 {
14387   gdb_byte *info_ptr = section->buffer + offset;
14388   unsigned int length, initial_length_size;
14389   unsigned int sig_offset;
14390   struct signatured_type find_entry, *type_sig;
14391
14392   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14393   sig_offset = (initial_length_size
14394                 + 2 /*version*/
14395                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14396                 + 1 /*address_size*/);
14397   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14398   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14399
14400   /* This is only used to lookup previously recorded types.
14401      If we didn't find it, it's our bug.  */
14402   gdb_assert (type_sig != NULL);
14403   gdb_assert (offset == type_sig->per_cu.offset);
14404
14405   return type_sig;
14406 }
14407
14408 /* Read in signatured type at OFFSET and build its CU and die(s).  */
14409
14410 static void
14411 read_signatured_type_at_offset (struct objfile *objfile,
14412                                 struct dwarf2_section_info *sect,
14413                                 unsigned int offset)
14414 {
14415   struct signatured_type *type_sig;
14416
14417   dwarf2_read_section (objfile, sect);
14418
14419   /* We have the section offset, but we need the signature to do the
14420      hash table lookup.  */
14421   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14422
14423   gdb_assert (type_sig->per_cu.cu == NULL);
14424
14425   read_signatured_type (objfile, type_sig);
14426
14427   gdb_assert (type_sig->per_cu.cu != NULL);
14428 }
14429
14430 /* Read in a signatured type and build its CU and DIEs.  */
14431
14432 static void
14433 read_signatured_type (struct objfile *objfile,
14434                       struct signatured_type *type_sig)
14435 {
14436   gdb_byte *types_ptr;
14437   struct die_reader_specs reader_specs;
14438   struct dwarf2_cu *cu;
14439   ULONGEST signature;
14440   struct cleanup *back_to, *free_cu_cleanup;
14441   struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14442
14443   dwarf2_read_section (objfile, section);
14444   types_ptr = section->buffer + type_sig->per_cu.offset;
14445
14446   gdb_assert (type_sig->per_cu.cu == NULL);
14447
14448   cu = xmalloc (sizeof (*cu));
14449   init_one_comp_unit (cu, objfile);
14450
14451   type_sig->per_cu.cu = cu;
14452   cu->per_cu = &type_sig->per_cu;
14453
14454   /* If an error occurs while loading, release our storage.  */
14455   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14456
14457   types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14458                                         types_ptr, objfile->obfd);
14459   gdb_assert (signature == type_sig->signature);
14460
14461   cu->die_hash
14462     = htab_create_alloc_ex (cu->header.length / 12,
14463                             die_hash,
14464                             die_eq,
14465                             NULL,
14466                             &cu->comp_unit_obstack,
14467                             hashtab_obstack_allocate,
14468                             dummy_obstack_deallocate);
14469
14470   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14471   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14472
14473   init_cu_die_reader (&reader_specs, cu);
14474
14475   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14476                                     NULL /*parent*/);
14477
14478   /* We try not to read any attributes in this function, because not
14479      all objfiles needed for references have been loaded yet, and symbol
14480      table processing isn't initialized.  But we have to set the CU language,
14481      or we won't be able to build types correctly.  */
14482   prepare_one_comp_unit (cu, cu->dies);
14483
14484   do_cleanups (back_to);
14485
14486   /* We've successfully allocated this compilation unit.  Let our caller
14487      clean it up when finished with it.  */
14488   discard_cleanups (free_cu_cleanup);
14489
14490   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14491   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14492 }
14493
14494 /* Decode simple location descriptions.
14495    Given a pointer to a dwarf block that defines a location, compute
14496    the location and return the value.
14497
14498    NOTE drow/2003-11-18: This function is called in two situations
14499    now: for the address of static or global variables (partial symbols
14500    only) and for offsets into structures which are expected to be
14501    (more or less) constant.  The partial symbol case should go away,
14502    and only the constant case should remain.  That will let this
14503    function complain more accurately.  A few special modes are allowed
14504    without complaint for global variables (for instance, global
14505    register values and thread-local values).
14506
14507    A location description containing no operations indicates that the
14508    object is optimized out.  The return value is 0 for that case.
14509    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14510    callers will only want a very basic result and this can become a
14511    complaint.
14512
14513    Note that stack[0] is unused except as a default error return.  */
14514
14515 static CORE_ADDR
14516 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14517 {
14518   struct objfile *objfile = cu->objfile;
14519   int i;
14520   int size = blk->size;
14521   gdb_byte *data = blk->data;
14522   CORE_ADDR stack[64];
14523   int stacki;
14524   unsigned int bytes_read, unsnd;
14525   gdb_byte op;
14526
14527   i = 0;
14528   stacki = 0;
14529   stack[stacki] = 0;
14530   stack[++stacki] = 0;
14531
14532   while (i < size)
14533     {
14534       op = data[i++];
14535       switch (op)
14536         {
14537         case DW_OP_lit0:
14538         case DW_OP_lit1:
14539         case DW_OP_lit2:
14540         case DW_OP_lit3:
14541         case DW_OP_lit4:
14542         case DW_OP_lit5:
14543         case DW_OP_lit6:
14544         case DW_OP_lit7:
14545         case DW_OP_lit8:
14546         case DW_OP_lit9:
14547         case DW_OP_lit10:
14548         case DW_OP_lit11:
14549         case DW_OP_lit12:
14550         case DW_OP_lit13:
14551         case DW_OP_lit14:
14552         case DW_OP_lit15:
14553         case DW_OP_lit16:
14554         case DW_OP_lit17:
14555         case DW_OP_lit18:
14556         case DW_OP_lit19:
14557         case DW_OP_lit20:
14558         case DW_OP_lit21:
14559         case DW_OP_lit22:
14560         case DW_OP_lit23:
14561         case DW_OP_lit24:
14562         case DW_OP_lit25:
14563         case DW_OP_lit26:
14564         case DW_OP_lit27:
14565         case DW_OP_lit28:
14566         case DW_OP_lit29:
14567         case DW_OP_lit30:
14568         case DW_OP_lit31:
14569           stack[++stacki] = op - DW_OP_lit0;
14570           break;
14571
14572         case DW_OP_reg0:
14573         case DW_OP_reg1:
14574         case DW_OP_reg2:
14575         case DW_OP_reg3:
14576         case DW_OP_reg4:
14577         case DW_OP_reg5:
14578         case DW_OP_reg6:
14579         case DW_OP_reg7:
14580         case DW_OP_reg8:
14581         case DW_OP_reg9:
14582         case DW_OP_reg10:
14583         case DW_OP_reg11:
14584         case DW_OP_reg12:
14585         case DW_OP_reg13:
14586         case DW_OP_reg14:
14587         case DW_OP_reg15:
14588         case DW_OP_reg16:
14589         case DW_OP_reg17:
14590         case DW_OP_reg18:
14591         case DW_OP_reg19:
14592         case DW_OP_reg20:
14593         case DW_OP_reg21:
14594         case DW_OP_reg22:
14595         case DW_OP_reg23:
14596         case DW_OP_reg24:
14597         case DW_OP_reg25:
14598         case DW_OP_reg26:
14599         case DW_OP_reg27:
14600         case DW_OP_reg28:
14601         case DW_OP_reg29:
14602         case DW_OP_reg30:
14603         case DW_OP_reg31:
14604           stack[++stacki] = op - DW_OP_reg0;
14605           if (i < size)
14606             dwarf2_complex_location_expr_complaint ();
14607           break;
14608
14609         case DW_OP_regx:
14610           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14611           i += bytes_read;
14612           stack[++stacki] = unsnd;
14613           if (i < size)
14614             dwarf2_complex_location_expr_complaint ();
14615           break;
14616
14617         case DW_OP_addr:
14618           stack[++stacki] = read_address (objfile->obfd, &data[i],
14619                                           cu, &bytes_read);
14620           i += bytes_read;
14621           break;
14622
14623         case DW_OP_const1u:
14624           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14625           i += 1;
14626           break;
14627
14628         case DW_OP_const1s:
14629           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14630           i += 1;
14631           break;
14632
14633         case DW_OP_const2u:
14634           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14635           i += 2;
14636           break;
14637
14638         case DW_OP_const2s:
14639           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14640           i += 2;
14641           break;
14642
14643         case DW_OP_const4u:
14644           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14645           i += 4;
14646           break;
14647
14648         case DW_OP_const4s:
14649           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14650           i += 4;
14651           break;
14652
14653         case DW_OP_constu:
14654           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14655                                                   &bytes_read);
14656           i += bytes_read;
14657           break;
14658
14659         case DW_OP_consts:
14660           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14661           i += bytes_read;
14662           break;
14663
14664         case DW_OP_dup:
14665           stack[stacki + 1] = stack[stacki];
14666           stacki++;
14667           break;
14668
14669         case DW_OP_plus:
14670           stack[stacki - 1] += stack[stacki];
14671           stacki--;
14672           break;
14673
14674         case DW_OP_plus_uconst:
14675           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14676                                                  &bytes_read);
14677           i += bytes_read;
14678           break;
14679
14680         case DW_OP_minus:
14681           stack[stacki - 1] -= stack[stacki];
14682           stacki--;
14683           break;
14684
14685         case DW_OP_deref:
14686           /* If we're not the last op, then we definitely can't encode
14687              this using GDB's address_class enum.  This is valid for partial
14688              global symbols, although the variable's address will be bogus
14689              in the psymtab.  */
14690           if (i < size)
14691             dwarf2_complex_location_expr_complaint ();
14692           break;
14693
14694         case DW_OP_GNU_push_tls_address:
14695           /* The top of the stack has the offset from the beginning
14696              of the thread control block at which the variable is located.  */
14697           /* Nothing should follow this operator, so the top of stack would
14698              be returned.  */
14699           /* This is valid for partial global symbols, but the variable's
14700              address will be bogus in the psymtab.  */
14701           if (i < size)
14702             dwarf2_complex_location_expr_complaint ();
14703           break;
14704
14705         case DW_OP_GNU_uninit:
14706           break;
14707
14708         default:
14709           {
14710             const char *name = dwarf_stack_op_name (op);
14711
14712             if (name)
14713               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14714                          name);
14715             else
14716               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14717                          op);
14718           }
14719
14720           return (stack[stacki]);
14721         }
14722
14723       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14724          outside of the allocated space.  Also enforce minimum>0.  */
14725       if (stacki >= ARRAY_SIZE (stack) - 1)
14726         {
14727           complaint (&symfile_complaints,
14728                      _("location description stack overflow"));
14729           return 0;
14730         }
14731
14732       if (stacki <= 0)
14733         {
14734           complaint (&symfile_complaints,
14735                      _("location description stack underflow"));
14736           return 0;
14737         }
14738     }
14739   return (stack[stacki]);
14740 }
14741
14742 /* memory allocation interface */
14743
14744 static struct dwarf_block *
14745 dwarf_alloc_block (struct dwarf2_cu *cu)
14746 {
14747   struct dwarf_block *blk;
14748
14749   blk = (struct dwarf_block *)
14750     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14751   return (blk);
14752 }
14753
14754 static struct abbrev_info *
14755 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14756 {
14757   struct abbrev_info *abbrev;
14758
14759   abbrev = (struct abbrev_info *)
14760     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14761   memset (abbrev, 0, sizeof (struct abbrev_info));
14762   return (abbrev);
14763 }
14764
14765 static struct die_info *
14766 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14767 {
14768   struct die_info *die;
14769   size_t size = sizeof (struct die_info);
14770
14771   if (num_attrs > 1)
14772     size += (num_attrs - 1) * sizeof (struct attribute);
14773
14774   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14775   memset (die, 0, sizeof (struct die_info));
14776   return (die);
14777 }
14778
14779 \f
14780 /* Macro support.  */
14781
14782 /* Return the full name of file number I in *LH's file name table.
14783    Use COMP_DIR as the name of the current directory of the
14784    compilation.  The result is allocated using xmalloc; the caller is
14785    responsible for freeing it.  */
14786 static char *
14787 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14788 {
14789   /* Is the file number a valid index into the line header's file name
14790      table?  Remember that file numbers start with one, not zero.  */
14791   if (1 <= file && file <= lh->num_file_names)
14792     {
14793       struct file_entry *fe = &lh->file_names[file - 1];
14794
14795       if (IS_ABSOLUTE_PATH (fe->name))
14796         return xstrdup (fe->name);
14797       else
14798         {
14799           const char *dir;
14800           int dir_len;
14801           char *full_name;
14802
14803           if (fe->dir_index)
14804             dir = lh->include_dirs[fe->dir_index - 1];
14805           else
14806             dir = comp_dir;
14807
14808           if (dir)
14809             {
14810               dir_len = strlen (dir);
14811               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14812               strcpy (full_name, dir);
14813               full_name[dir_len] = '/';
14814               strcpy (full_name + dir_len + 1, fe->name);
14815               return full_name;
14816             }
14817           else
14818             return xstrdup (fe->name);
14819         }
14820     }
14821   else
14822     {
14823       /* The compiler produced a bogus file number.  We can at least
14824          record the macro definitions made in the file, even if we
14825          won't be able to find the file by name.  */
14826       char fake_name[80];
14827
14828       sprintf (fake_name, "<bad macro file number %d>", file);
14829
14830       complaint (&symfile_complaints,
14831                  _("bad file number in macro information (%d)"),
14832                  file);
14833
14834       return xstrdup (fake_name);
14835     }
14836 }
14837
14838
14839 static struct macro_source_file *
14840 macro_start_file (int file, int line,
14841                   struct macro_source_file *current_file,
14842                   const char *comp_dir,
14843                   struct line_header *lh, struct objfile *objfile)
14844 {
14845   /* The full name of this source file.  */
14846   char *full_name = file_full_name (file, lh, comp_dir);
14847
14848   /* We don't create a macro table for this compilation unit
14849      at all until we actually get a filename.  */
14850   if (! pending_macros)
14851     pending_macros = new_macro_table (&objfile->objfile_obstack,
14852                                       objfile->macro_cache);
14853
14854   if (! current_file)
14855     /* If we have no current file, then this must be the start_file
14856        directive for the compilation unit's main source file.  */
14857     current_file = macro_set_main (pending_macros, full_name);
14858   else
14859     current_file = macro_include (current_file, line, full_name);
14860
14861   xfree (full_name);
14862
14863   return current_file;
14864 }
14865
14866
14867 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14868    followed by a null byte.  */
14869 static char *
14870 copy_string (const char *buf, int len)
14871 {
14872   char *s = xmalloc (len + 1);
14873
14874   memcpy (s, buf, len);
14875   s[len] = '\0';
14876   return s;
14877 }
14878
14879
14880 static const char *
14881 consume_improper_spaces (const char *p, const char *body)
14882 {
14883   if (*p == ' ')
14884     {
14885       complaint (&symfile_complaints,
14886                  _("macro definition contains spaces "
14887                    "in formal argument list:\n`%s'"),
14888                  body);
14889
14890       while (*p == ' ')
14891         p++;
14892     }
14893
14894   return p;
14895 }
14896
14897
14898 static void
14899 parse_macro_definition (struct macro_source_file *file, int line,
14900                         const char *body)
14901 {
14902   const char *p;
14903
14904   /* The body string takes one of two forms.  For object-like macro
14905      definitions, it should be:
14906
14907         <macro name> " " <definition>
14908
14909      For function-like macro definitions, it should be:
14910
14911         <macro name> "() " <definition>
14912      or
14913         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14914
14915      Spaces may appear only where explicitly indicated, and in the
14916      <definition>.
14917
14918      The Dwarf 2 spec says that an object-like macro's name is always
14919      followed by a space, but versions of GCC around March 2002 omit
14920      the space when the macro's definition is the empty string.
14921
14922      The Dwarf 2 spec says that there should be no spaces between the
14923      formal arguments in a function-like macro's formal argument list,
14924      but versions of GCC around March 2002 include spaces after the
14925      commas.  */
14926
14927
14928   /* Find the extent of the macro name.  The macro name is terminated
14929      by either a space or null character (for an object-like macro) or
14930      an opening paren (for a function-like macro).  */
14931   for (p = body; *p; p++)
14932     if (*p == ' ' || *p == '(')
14933       break;
14934
14935   if (*p == ' ' || *p == '\0')
14936     {
14937       /* It's an object-like macro.  */
14938       int name_len = p - body;
14939       char *name = copy_string (body, name_len);
14940       const char *replacement;
14941
14942       if (*p == ' ')
14943         replacement = body + name_len + 1;
14944       else
14945         {
14946           dwarf2_macro_malformed_definition_complaint (body);
14947           replacement = body + name_len;
14948         }
14949
14950       macro_define_object (file, line, name, replacement);
14951
14952       xfree (name);
14953     }
14954   else if (*p == '(')
14955     {
14956       /* It's a function-like macro.  */
14957       char *name = copy_string (body, p - body);
14958       int argc = 0;
14959       int argv_size = 1;
14960       char **argv = xmalloc (argv_size * sizeof (*argv));
14961
14962       p++;
14963
14964       p = consume_improper_spaces (p, body);
14965
14966       /* Parse the formal argument list.  */
14967       while (*p && *p != ')')
14968         {
14969           /* Find the extent of the current argument name.  */
14970           const char *arg_start = p;
14971
14972           while (*p && *p != ',' && *p != ')' && *p != ' ')
14973             p++;
14974
14975           if (! *p || p == arg_start)
14976             dwarf2_macro_malformed_definition_complaint (body);
14977           else
14978             {
14979               /* Make sure argv has room for the new argument.  */
14980               if (argc >= argv_size)
14981                 {
14982                   argv_size *= 2;
14983                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14984                 }
14985
14986               argv[argc++] = copy_string (arg_start, p - arg_start);
14987             }
14988
14989           p = consume_improper_spaces (p, body);
14990
14991           /* Consume the comma, if present.  */
14992           if (*p == ',')
14993             {
14994               p++;
14995
14996               p = consume_improper_spaces (p, body);
14997             }
14998         }
14999
15000       if (*p == ')')
15001         {
15002           p++;
15003
15004           if (*p == ' ')
15005             /* Perfectly formed definition, no complaints.  */
15006             macro_define_function (file, line, name,
15007                                    argc, (const char **) argv,
15008                                    p + 1);
15009           else if (*p == '\0')
15010             {
15011               /* Complain, but do define it.  */
15012               dwarf2_macro_malformed_definition_complaint (body);
15013               macro_define_function (file, line, name,
15014                                      argc, (const char **) argv,
15015                                      p);
15016             }
15017           else
15018             /* Just complain.  */
15019             dwarf2_macro_malformed_definition_complaint (body);
15020         }
15021       else
15022         /* Just complain.  */
15023         dwarf2_macro_malformed_definition_complaint (body);
15024
15025       xfree (name);
15026       {
15027         int i;
15028
15029         for (i = 0; i < argc; i++)
15030           xfree (argv[i]);
15031       }
15032       xfree (argv);
15033     }
15034   else
15035     dwarf2_macro_malformed_definition_complaint (body);
15036 }
15037
15038 /* Skip some bytes from BYTES according to the form given in FORM.
15039    Returns the new pointer.  */
15040
15041 static gdb_byte *
15042 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15043                  enum dwarf_form form,
15044                  unsigned int offset_size,
15045                  struct dwarf2_section_info *section)
15046 {
15047   unsigned int bytes_read;
15048
15049   switch (form)
15050     {
15051     case DW_FORM_data1:
15052     case DW_FORM_flag:
15053       ++bytes;
15054       break;
15055
15056     case DW_FORM_data2:
15057       bytes += 2;
15058       break;
15059
15060     case DW_FORM_data4:
15061       bytes += 4;
15062       break;
15063
15064     case DW_FORM_data8:
15065       bytes += 8;
15066       break;
15067
15068     case DW_FORM_string:
15069       read_direct_string (abfd, bytes, &bytes_read);
15070       bytes += bytes_read;
15071       break;
15072
15073     case DW_FORM_sec_offset:
15074     case DW_FORM_strp:
15075       bytes += offset_size;
15076       break;
15077
15078     case DW_FORM_block:
15079       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15080       bytes += bytes_read;
15081       break;
15082
15083     case DW_FORM_block1:
15084       bytes += 1 + read_1_byte (abfd, bytes);
15085       break;
15086     case DW_FORM_block2:
15087       bytes += 2 + read_2_bytes (abfd, bytes);
15088       break;
15089     case DW_FORM_block4:
15090       bytes += 4 + read_4_bytes (abfd, bytes);
15091       break;
15092
15093     case DW_FORM_sdata:
15094     case DW_FORM_udata:
15095       bytes = skip_leb128 (abfd, bytes);
15096       break;
15097
15098     default:
15099       {
15100       complain:
15101         complaint (&symfile_complaints,
15102                    _("invalid form 0x%x in `%s'"),
15103                    form,
15104                    section->asection->name);
15105         return NULL;
15106       }
15107     }
15108
15109   return bytes;
15110 }
15111
15112 /* A helper for dwarf_decode_macros that handles skipping an unknown
15113    opcode.  Returns an updated pointer to the macro data buffer; or,
15114    on error, issues a complaint and returns NULL.  */
15115
15116 static gdb_byte *
15117 skip_unknown_opcode (unsigned int opcode,
15118                      gdb_byte **opcode_definitions,
15119                      gdb_byte *mac_ptr,
15120                      bfd *abfd,
15121                      unsigned int offset_size,
15122                      struct dwarf2_section_info *section)
15123 {
15124   unsigned int bytes_read, i;
15125   unsigned long arg;
15126   gdb_byte *defn;
15127
15128   if (opcode_definitions[opcode] == NULL)
15129     {
15130       complaint (&symfile_complaints,
15131                  _("unrecognized DW_MACFINO opcode 0x%x"),
15132                  opcode);
15133       return NULL;
15134     }
15135
15136   defn = opcode_definitions[opcode];
15137   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15138   defn += bytes_read;
15139
15140   for (i = 0; i < arg; ++i)
15141     {
15142       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15143       if (mac_ptr == NULL)
15144         {
15145           /* skip_form_bytes already issued the complaint.  */
15146           return NULL;
15147         }
15148     }
15149
15150   return mac_ptr;
15151 }
15152
15153 /* A helper function which parses the header of a macro section.
15154    If the macro section is the extended (for now called "GNU") type,
15155    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15156    the header, or issues a complaint and returns NULL on error.  */
15157
15158 static gdb_byte *
15159 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15160                           bfd *abfd,
15161                           gdb_byte *mac_ptr,
15162                           unsigned int *offset_size,
15163                           int section_is_gnu)
15164 {
15165   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15166
15167   if (section_is_gnu)
15168     {
15169       unsigned int version, flags;
15170
15171       version = read_2_bytes (abfd, mac_ptr);
15172       if (version != 4)
15173         {
15174           complaint (&symfile_complaints,
15175                      _("unrecognized version `%d' in .debug_macro section"),
15176                      version);
15177           return NULL;
15178         }
15179       mac_ptr += 2;
15180
15181       flags = read_1_byte (abfd, mac_ptr);
15182       ++mac_ptr;
15183       *offset_size = (flags & 1) ? 8 : 4;
15184
15185       if ((flags & 2) != 0)
15186         /* We don't need the line table offset.  */
15187         mac_ptr += *offset_size;
15188
15189       /* Vendor opcode descriptions.  */
15190       if ((flags & 4) != 0)
15191         {
15192           unsigned int i, count;
15193
15194           count = read_1_byte (abfd, mac_ptr);
15195           ++mac_ptr;
15196           for (i = 0; i < count; ++i)
15197             {
15198               unsigned int opcode, bytes_read;
15199               unsigned long arg;
15200
15201               opcode = read_1_byte (abfd, mac_ptr);
15202               ++mac_ptr;
15203               opcode_definitions[opcode] = mac_ptr;
15204               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15205               mac_ptr += bytes_read;
15206               mac_ptr += arg;
15207             }
15208         }
15209     }
15210
15211   return mac_ptr;
15212 }
15213
15214 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15215    including DW_GNU_MACINFO_transparent_include.  */
15216
15217 static void
15218 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15219                           struct macro_source_file *current_file,
15220                           struct line_header *lh, char *comp_dir,
15221                           struct dwarf2_section_info *section,
15222                           int section_is_gnu,
15223                           unsigned int offset_size,
15224                           struct objfile *objfile)
15225 {
15226   enum dwarf_macro_record_type macinfo_type;
15227   int at_commandline;
15228   gdb_byte *opcode_definitions[256];
15229
15230   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15231                                       &offset_size, section_is_gnu);
15232   if (mac_ptr == NULL)
15233     {
15234       /* We already issued a complaint.  */
15235       return;
15236     }
15237
15238   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15239      GDB is still reading the definitions from command line.  First
15240      DW_MACINFO_start_file will need to be ignored as it was already executed
15241      to create CURRENT_FILE for the main source holding also the command line
15242      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15243      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15244
15245   at_commandline = 1;
15246
15247   do
15248     {
15249       /* Do we at least have room for a macinfo type byte?  */
15250       if (mac_ptr >= mac_end)
15251         {
15252           dwarf2_macros_too_long_complaint (section);
15253           break;
15254         }
15255
15256       macinfo_type = read_1_byte (abfd, mac_ptr);
15257       mac_ptr++;
15258
15259       /* Note that we rely on the fact that the corresponding GNU and
15260          DWARF constants are the same.  */
15261       switch (macinfo_type)
15262         {
15263           /* A zero macinfo type indicates the end of the macro
15264              information.  */
15265         case 0:
15266           break;
15267
15268         case DW_MACRO_GNU_define:
15269         case DW_MACRO_GNU_undef:
15270         case DW_MACRO_GNU_define_indirect:
15271         case DW_MACRO_GNU_undef_indirect:
15272           {
15273             unsigned int bytes_read;
15274             int line;
15275             char *body;
15276             int is_define;
15277
15278             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15279             mac_ptr += bytes_read;
15280
15281             if (macinfo_type == DW_MACRO_GNU_define
15282                 || macinfo_type == DW_MACRO_GNU_undef)
15283               {
15284                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15285                 mac_ptr += bytes_read;
15286               }
15287             else
15288               {
15289                 LONGEST str_offset;
15290
15291                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15292                 mac_ptr += offset_size;
15293
15294                 body = read_indirect_string_at_offset (abfd, str_offset);
15295               }
15296
15297             is_define = (macinfo_type == DW_MACRO_GNU_define
15298                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15299             if (! current_file)
15300               {
15301                 /* DWARF violation as no main source is present.  */
15302                 complaint (&symfile_complaints,
15303                            _("debug info with no main source gives macro %s "
15304                              "on line %d: %s"),
15305                            is_define ? _("definition") : _("undefinition"),
15306                            line, body);
15307                 break;
15308               }
15309             if ((line == 0 && !at_commandline)
15310                 || (line != 0 && at_commandline))
15311               complaint (&symfile_complaints,
15312                          _("debug info gives %s macro %s with %s line %d: %s"),
15313                          at_commandline ? _("command-line") : _("in-file"),
15314                          is_define ? _("definition") : _("undefinition"),
15315                          line == 0 ? _("zero") : _("non-zero"), line, body);
15316
15317             if (is_define)
15318               parse_macro_definition (current_file, line, body);
15319             else
15320               {
15321                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15322                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15323                 macro_undef (current_file, line, body);
15324               }
15325           }
15326           break;
15327
15328         case DW_MACRO_GNU_start_file:
15329           {
15330             unsigned int bytes_read;
15331             int line, file;
15332
15333             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15334             mac_ptr += bytes_read;
15335             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15336             mac_ptr += bytes_read;
15337
15338             if ((line == 0 && !at_commandline)
15339                 || (line != 0 && at_commandline))
15340               complaint (&symfile_complaints,
15341                          _("debug info gives source %d included "
15342                            "from %s at %s line %d"),
15343                          file, at_commandline ? _("command-line") : _("file"),
15344                          line == 0 ? _("zero") : _("non-zero"), line);
15345
15346             if (at_commandline)
15347               {
15348                 /* This DW_MACRO_GNU_start_file was executed in the
15349                    pass one.  */
15350                 at_commandline = 0;
15351               }
15352             else
15353               current_file = macro_start_file (file, line,
15354                                                current_file, comp_dir,
15355                                                lh, objfile);
15356           }
15357           break;
15358
15359         case DW_MACRO_GNU_end_file:
15360           if (! current_file)
15361             complaint (&symfile_complaints,
15362                        _("macro debug info has an unmatched "
15363                          "`close_file' directive"));
15364           else
15365             {
15366               current_file = current_file->included_by;
15367               if (! current_file)
15368                 {
15369                   enum dwarf_macro_record_type next_type;
15370
15371                   /* GCC circa March 2002 doesn't produce the zero
15372                      type byte marking the end of the compilation
15373                      unit.  Complain if it's not there, but exit no
15374                      matter what.  */
15375
15376                   /* Do we at least have room for a macinfo type byte?  */
15377                   if (mac_ptr >= mac_end)
15378                     {
15379                       dwarf2_macros_too_long_complaint (section);
15380                       return;
15381                     }
15382
15383                   /* We don't increment mac_ptr here, so this is just
15384                      a look-ahead.  */
15385                   next_type = read_1_byte (abfd, mac_ptr);
15386                   if (next_type != 0)
15387                     complaint (&symfile_complaints,
15388                                _("no terminating 0-type entry for "
15389                                  "macros in `.debug_macinfo' section"));
15390
15391                   return;
15392                 }
15393             }
15394           break;
15395
15396         case DW_MACRO_GNU_transparent_include:
15397           {
15398             LONGEST offset;
15399
15400             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15401             mac_ptr += offset_size;
15402
15403             dwarf_decode_macro_bytes (abfd,
15404                                       section->buffer + offset,
15405                                       mac_end, current_file,
15406                                       lh, comp_dir,
15407                                       section, section_is_gnu,
15408                                       offset_size, objfile);
15409           }
15410           break;
15411
15412         case DW_MACINFO_vendor_ext:
15413           if (!section_is_gnu)
15414             {
15415               unsigned int bytes_read;
15416               int constant;
15417
15418               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15419               mac_ptr += bytes_read;
15420               read_direct_string (abfd, mac_ptr, &bytes_read);
15421               mac_ptr += bytes_read;
15422
15423               /* We don't recognize any vendor extensions.  */
15424               break;
15425             }
15426           /* FALLTHROUGH */
15427
15428         default:
15429           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15430                                          mac_ptr, abfd, offset_size,
15431                                          section);
15432           if (mac_ptr == NULL)
15433             return;
15434           break;
15435         }
15436     } while (macinfo_type != 0);
15437 }
15438
15439 static void
15440 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15441                      char *comp_dir, bfd *abfd,
15442                      struct dwarf2_cu *cu,
15443                      struct dwarf2_section_info *section,
15444                      int section_is_gnu)
15445 {
15446   gdb_byte *mac_ptr, *mac_end;
15447   struct macro_source_file *current_file = 0;
15448   enum dwarf_macro_record_type macinfo_type;
15449   unsigned int offset_size = cu->header.offset_size;
15450   gdb_byte *opcode_definitions[256];
15451
15452   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15453   if (section->buffer == NULL)
15454     {
15455       complaint (&symfile_complaints, _("missing %s section"),
15456                  section->asection->name);
15457       return;
15458     }
15459
15460   /* First pass: Find the name of the base filename.
15461      This filename is needed in order to process all macros whose definition
15462      (or undefinition) comes from the command line.  These macros are defined
15463      before the first DW_MACINFO_start_file entry, and yet still need to be
15464      associated to the base file.
15465
15466      To determine the base file name, we scan the macro definitions until we
15467      reach the first DW_MACINFO_start_file entry.  We then initialize
15468      CURRENT_FILE accordingly so that any macro definition found before the
15469      first DW_MACINFO_start_file can still be associated to the base file.  */
15470
15471   mac_ptr = section->buffer + offset;
15472   mac_end = section->buffer + section->size;
15473
15474   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15475                                       &offset_size, section_is_gnu);
15476   if (mac_ptr == NULL)
15477     {
15478       /* We already issued a complaint.  */
15479       return;
15480     }
15481
15482   do
15483     {
15484       /* Do we at least have room for a macinfo type byte?  */
15485       if (mac_ptr >= mac_end)
15486         {
15487           /* Complaint is printed during the second pass as GDB will probably
15488              stop the first pass earlier upon finding
15489              DW_MACINFO_start_file.  */
15490           break;
15491         }
15492
15493       macinfo_type = read_1_byte (abfd, mac_ptr);
15494       mac_ptr++;
15495
15496       /* Note that we rely on the fact that the corresponding GNU and
15497          DWARF constants are the same.  */
15498       switch (macinfo_type)
15499         {
15500           /* A zero macinfo type indicates the end of the macro
15501              information.  */
15502         case 0:
15503           break;
15504
15505         case DW_MACRO_GNU_define:
15506         case DW_MACRO_GNU_undef:
15507           /* Only skip the data by MAC_PTR.  */
15508           {
15509             unsigned int bytes_read;
15510
15511             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15512             mac_ptr += bytes_read;
15513             read_direct_string (abfd, mac_ptr, &bytes_read);
15514             mac_ptr += bytes_read;
15515           }
15516           break;
15517
15518         case DW_MACRO_GNU_start_file:
15519           {
15520             unsigned int bytes_read;
15521             int line, file;
15522
15523             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15524             mac_ptr += bytes_read;
15525             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15526             mac_ptr += bytes_read;
15527
15528             current_file = macro_start_file (file, line, current_file,
15529                                              comp_dir, lh, cu->objfile);
15530           }
15531           break;
15532
15533         case DW_MACRO_GNU_end_file:
15534           /* No data to skip by MAC_PTR.  */
15535           break;
15536
15537         case DW_MACRO_GNU_define_indirect:
15538         case DW_MACRO_GNU_undef_indirect:
15539           {
15540             unsigned int bytes_read;
15541
15542             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15543             mac_ptr += bytes_read;
15544             mac_ptr += offset_size;
15545           }
15546           break;
15547
15548         case DW_MACRO_GNU_transparent_include:
15549           /* Note that, according to the spec, a transparent include
15550              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15551              skip this opcode.  */
15552           mac_ptr += offset_size;
15553           break;
15554
15555         case DW_MACINFO_vendor_ext:
15556           /* Only skip the data by MAC_PTR.  */
15557           if (!section_is_gnu)
15558             {
15559               unsigned int bytes_read;
15560
15561               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15562               mac_ptr += bytes_read;
15563               read_direct_string (abfd, mac_ptr, &bytes_read);
15564               mac_ptr += bytes_read;
15565             }
15566           /* FALLTHROUGH */
15567
15568         default:
15569           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15570                                          mac_ptr, abfd, offset_size,
15571                                          section);
15572           if (mac_ptr == NULL)
15573             return;
15574           break;
15575         }
15576     } while (macinfo_type != 0 && current_file == NULL);
15577
15578   /* Second pass: Process all entries.
15579
15580      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15581      command-line macro definitions/undefinitions.  This flag is unset when we
15582      reach the first DW_MACINFO_start_file entry.  */
15583
15584   dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15585                             current_file, lh, comp_dir, section, section_is_gnu,
15586                             offset_size, cu->objfile);
15587 }
15588
15589 /* Check if the attribute's form is a DW_FORM_block*
15590    if so return true else false.  */
15591 static int
15592 attr_form_is_block (struct attribute *attr)
15593 {
15594   return (attr == NULL ? 0 :
15595       attr->form == DW_FORM_block1
15596       || attr->form == DW_FORM_block2
15597       || attr->form == DW_FORM_block4
15598       || attr->form == DW_FORM_block
15599       || attr->form == DW_FORM_exprloc);
15600 }
15601
15602 /* Return non-zero if ATTR's value is a section offset --- classes
15603    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15604    You may use DW_UNSND (attr) to retrieve such offsets.
15605
15606    Section 7.5.4, "Attribute Encodings", explains that no attribute
15607    may have a value that belongs to more than one of these classes; it
15608    would be ambiguous if we did, because we use the same forms for all
15609    of them.  */
15610 static int
15611 attr_form_is_section_offset (struct attribute *attr)
15612 {
15613   return (attr->form == DW_FORM_data4
15614           || attr->form == DW_FORM_data8
15615           || attr->form == DW_FORM_sec_offset);
15616 }
15617
15618
15619 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15620    zero otherwise.  When this function returns true, you can apply
15621    dwarf2_get_attr_constant_value to it.
15622
15623    However, note that for some attributes you must check
15624    attr_form_is_section_offset before using this test.  DW_FORM_data4
15625    and DW_FORM_data8 are members of both the constant class, and of
15626    the classes that contain offsets into other debug sections
15627    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15628    that, if an attribute's can be either a constant or one of the
15629    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15630    taken as section offsets, not constants.  */
15631 static int
15632 attr_form_is_constant (struct attribute *attr)
15633 {
15634   switch (attr->form)
15635     {
15636     case DW_FORM_sdata:
15637     case DW_FORM_udata:
15638     case DW_FORM_data1:
15639     case DW_FORM_data2:
15640     case DW_FORM_data4:
15641     case DW_FORM_data8:
15642       return 1;
15643     default:
15644       return 0;
15645     }
15646 }
15647
15648 /* A helper function that fills in a dwarf2_loclist_baton.  */
15649
15650 static void
15651 fill_in_loclist_baton (struct dwarf2_cu *cu,
15652                        struct dwarf2_loclist_baton *baton,
15653                        struct attribute *attr)
15654 {
15655   dwarf2_read_section (dwarf2_per_objfile->objfile,
15656                        &dwarf2_per_objfile->loc);
15657
15658   baton->per_cu = cu->per_cu;
15659   gdb_assert (baton->per_cu);
15660   /* We don't know how long the location list is, but make sure we
15661      don't run off the edge of the section.  */
15662   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15663   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15664   baton->base_address = cu->base_address;
15665 }
15666
15667 static void
15668 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15669                              struct dwarf2_cu *cu)
15670 {
15671   if (attr_form_is_section_offset (attr)
15672       /* ".debug_loc" may not exist at all, or the offset may be outside
15673          the section.  If so, fall through to the complaint in the
15674          other branch.  */
15675       && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15676                                                 &dwarf2_per_objfile->loc))
15677     {
15678       struct dwarf2_loclist_baton *baton;
15679
15680       baton = obstack_alloc (&cu->objfile->objfile_obstack,
15681                              sizeof (struct dwarf2_loclist_baton));
15682
15683       fill_in_loclist_baton (cu, baton, attr);
15684
15685       if (cu->base_known == 0)
15686         complaint (&symfile_complaints,
15687                    _("Location list used without "
15688                      "specifying the CU base address."));
15689
15690       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15691       SYMBOL_LOCATION_BATON (sym) = baton;
15692     }
15693   else
15694     {
15695       struct dwarf2_locexpr_baton *baton;
15696
15697       baton = obstack_alloc (&cu->objfile->objfile_obstack,
15698                              sizeof (struct dwarf2_locexpr_baton));
15699       baton->per_cu = cu->per_cu;
15700       gdb_assert (baton->per_cu);
15701
15702       if (attr_form_is_block (attr))
15703         {
15704           /* Note that we're just copying the block's data pointer
15705              here, not the actual data.  We're still pointing into the
15706              info_buffer for SYM's objfile; right now we never release
15707              that buffer, but when we do clean up properly this may
15708              need to change.  */
15709           baton->size = DW_BLOCK (attr)->size;
15710           baton->data = DW_BLOCK (attr)->data;
15711         }
15712       else
15713         {
15714           dwarf2_invalid_attrib_class_complaint ("location description",
15715                                                  SYMBOL_NATURAL_NAME (sym));
15716           baton->size = 0;
15717         }
15718
15719       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15720       SYMBOL_LOCATION_BATON (sym) = baton;
15721     }
15722 }
15723
15724 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15725    came from a separate debuginfo file, then the master objfile is
15726    returned.  */
15727
15728 struct objfile *
15729 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15730 {
15731   struct objfile *objfile = per_cu->objfile;
15732
15733   /* Return the master objfile, so that we can report and look up the
15734      correct file containing this variable.  */
15735   if (objfile->separate_debug_objfile_backlink)
15736     objfile = objfile->separate_debug_objfile_backlink;
15737
15738   return objfile;
15739 }
15740
15741 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15742    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15743    CU_HEADERP first.  */
15744
15745 static const struct comp_unit_head *
15746 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15747                        struct dwarf2_per_cu_data *per_cu)
15748 {
15749   struct objfile *objfile;
15750   struct dwarf2_per_objfile *per_objfile;
15751   gdb_byte *info_ptr;
15752
15753   if (per_cu->cu)
15754     return &per_cu->cu->header;
15755
15756   objfile = per_cu->objfile;
15757   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15758   info_ptr = per_objfile->info.buffer + per_cu->offset;
15759
15760   memset (cu_headerp, 0, sizeof (*cu_headerp));
15761   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15762
15763   return cu_headerp;
15764 }
15765
15766 /* Return the address size given in the compilation unit header for CU.  */
15767
15768 CORE_ADDR
15769 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15770 {
15771   struct comp_unit_head cu_header_local;
15772   const struct comp_unit_head *cu_headerp;
15773
15774   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15775
15776   return cu_headerp->addr_size;
15777 }
15778
15779 /* Return the offset size given in the compilation unit header for CU.  */
15780
15781 int
15782 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15783 {
15784   struct comp_unit_head cu_header_local;
15785   const struct comp_unit_head *cu_headerp;
15786
15787   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15788
15789   return cu_headerp->offset_size;
15790 }
15791
15792 /* See its dwarf2loc.h declaration.  */
15793
15794 int
15795 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15796 {
15797   struct comp_unit_head cu_header_local;
15798   const struct comp_unit_head *cu_headerp;
15799
15800   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15801
15802   if (cu_headerp->version == 2)
15803     return cu_headerp->addr_size;
15804   else
15805     return cu_headerp->offset_size;
15806 }
15807
15808 /* Return the text offset of the CU.  The returned offset comes from
15809    this CU's objfile.  If this objfile came from a separate debuginfo
15810    file, then the offset may be different from the corresponding
15811    offset in the parent objfile.  */
15812
15813 CORE_ADDR
15814 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15815 {
15816   struct objfile *objfile = per_cu->objfile;
15817
15818   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15819 }
15820
15821 /* Locate the .debug_info compilation unit from CU's objfile which contains
15822    the DIE at OFFSET.  Raises an error on failure.  */
15823
15824 static struct dwarf2_per_cu_data *
15825 dwarf2_find_containing_comp_unit (unsigned int offset,
15826                                   struct objfile *objfile)
15827 {
15828   struct dwarf2_per_cu_data *this_cu;
15829   int low, high;
15830
15831   low = 0;
15832   high = dwarf2_per_objfile->n_comp_units - 1;
15833   while (high > low)
15834     {
15835       int mid = low + (high - low) / 2;
15836
15837       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15838         high = mid;
15839       else
15840         low = mid + 1;
15841     }
15842   gdb_assert (low == high);
15843   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15844     {
15845       if (low == 0)
15846         error (_("Dwarf Error: could not find partial DIE containing "
15847                "offset 0x%lx [in module %s]"),
15848                (long) offset, bfd_get_filename (objfile->obfd));
15849
15850       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15851       return dwarf2_per_objfile->all_comp_units[low-1];
15852     }
15853   else
15854     {
15855       this_cu = dwarf2_per_objfile->all_comp_units[low];
15856       if (low == dwarf2_per_objfile->n_comp_units - 1
15857           && offset >= this_cu->offset + this_cu->length)
15858         error (_("invalid dwarf2 offset %u"), offset);
15859       gdb_assert (offset < this_cu->offset + this_cu->length);
15860       return this_cu;
15861     }
15862 }
15863
15864 /* Locate the compilation unit from OBJFILE which is located at exactly
15865    OFFSET.  Raises an error on failure.  */
15866
15867 static struct dwarf2_per_cu_data *
15868 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15869 {
15870   struct dwarf2_per_cu_data *this_cu;
15871
15872   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15873   if (this_cu->offset != offset)
15874     error (_("no compilation unit with offset %u."), offset);
15875   return this_cu;
15876 }
15877
15878 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
15879
15880 static void
15881 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15882 {
15883   memset (cu, 0, sizeof (*cu));
15884   cu->objfile = objfile;
15885   obstack_init (&cu->comp_unit_obstack);
15886 }
15887
15888 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15889
15890 static void
15891 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15892 {
15893   struct attribute *attr;
15894
15895   /* Set the language we're debugging.  */
15896   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15897   if (attr)
15898     set_cu_language (DW_UNSND (attr), cu);
15899   else
15900     {
15901       cu->language = language_minimal;
15902       cu->language_defn = language_def (cu->language);
15903     }
15904 }
15905
15906 /* Release one cached compilation unit, CU.  We unlink it from the tree
15907    of compilation units, but we don't remove it from the read_in_chain;
15908    the caller is responsible for that.
15909    NOTE: DATA is a void * because this function is also used as a
15910    cleanup routine.  */
15911
15912 static void
15913 free_one_comp_unit (void *data)
15914 {
15915   struct dwarf2_cu *cu = data;
15916
15917   if (cu->per_cu != NULL)
15918     cu->per_cu->cu = NULL;
15919   cu->per_cu = NULL;
15920
15921   obstack_free (&cu->comp_unit_obstack, NULL);
15922
15923   xfree (cu);
15924 }
15925
15926 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15927    when we're finished with it.  We can't free the pointer itself, but be
15928    sure to unlink it from the cache.  Also release any associated storage
15929    and perform cache maintenance.
15930
15931    Only used during partial symbol parsing.  */
15932
15933 static void
15934 free_stack_comp_unit (void *data)
15935 {
15936   struct dwarf2_cu *cu = data;
15937
15938   obstack_free (&cu->comp_unit_obstack, NULL);
15939   cu->partial_dies = NULL;
15940
15941   if (cu->per_cu != NULL)
15942     {
15943       /* This compilation unit is on the stack in our caller, so we
15944          should not xfree it.  Just unlink it.  */
15945       cu->per_cu->cu = NULL;
15946       cu->per_cu = NULL;
15947
15948       /* If we had a per-cu pointer, then we may have other compilation
15949          units loaded, so age them now.  */
15950       age_cached_comp_units ();
15951     }
15952 }
15953
15954 /* Free all cached compilation units.  */
15955
15956 static void
15957 free_cached_comp_units (void *data)
15958 {
15959   struct dwarf2_per_cu_data *per_cu, **last_chain;
15960
15961   per_cu = dwarf2_per_objfile->read_in_chain;
15962   last_chain = &dwarf2_per_objfile->read_in_chain;
15963   while (per_cu != NULL)
15964     {
15965       struct dwarf2_per_cu_data *next_cu;
15966
15967       next_cu = per_cu->cu->read_in_chain;
15968
15969       free_one_comp_unit (per_cu->cu);
15970       *last_chain = next_cu;
15971
15972       per_cu = next_cu;
15973     }
15974 }
15975
15976 /* Increase the age counter on each cached compilation unit, and free
15977    any that are too old.  */
15978
15979 static void
15980 age_cached_comp_units (void)
15981 {
15982   struct dwarf2_per_cu_data *per_cu, **last_chain;
15983
15984   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15985   per_cu = dwarf2_per_objfile->read_in_chain;
15986   while (per_cu != NULL)
15987     {
15988       per_cu->cu->last_used ++;
15989       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15990         dwarf2_mark (per_cu->cu);
15991       per_cu = per_cu->cu->read_in_chain;
15992     }
15993
15994   per_cu = dwarf2_per_objfile->read_in_chain;
15995   last_chain = &dwarf2_per_objfile->read_in_chain;
15996   while (per_cu != NULL)
15997     {
15998       struct dwarf2_per_cu_data *next_cu;
15999
16000       next_cu = per_cu->cu->read_in_chain;
16001
16002       if (!per_cu->cu->mark)
16003         {
16004           free_one_comp_unit (per_cu->cu);
16005           *last_chain = next_cu;
16006         }
16007       else
16008         last_chain = &per_cu->cu->read_in_chain;
16009
16010       per_cu = next_cu;
16011     }
16012 }
16013
16014 /* Remove a single compilation unit from the cache.  */
16015
16016 static void
16017 free_one_cached_comp_unit (void *target_cu)
16018 {
16019   struct dwarf2_per_cu_data *per_cu, **last_chain;
16020
16021   per_cu = dwarf2_per_objfile->read_in_chain;
16022   last_chain = &dwarf2_per_objfile->read_in_chain;
16023   while (per_cu != NULL)
16024     {
16025       struct dwarf2_per_cu_data *next_cu;
16026
16027       next_cu = per_cu->cu->read_in_chain;
16028
16029       if (per_cu->cu == target_cu)
16030         {
16031           free_one_comp_unit (per_cu->cu);
16032           *last_chain = next_cu;
16033           break;
16034         }
16035       else
16036         last_chain = &per_cu->cu->read_in_chain;
16037
16038       per_cu = next_cu;
16039     }
16040 }
16041
16042 /* Release all extra memory associated with OBJFILE.  */
16043
16044 void
16045 dwarf2_free_objfile (struct objfile *objfile)
16046 {
16047   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16048
16049   if (dwarf2_per_objfile == NULL)
16050     return;
16051
16052   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
16053   free_cached_comp_units (NULL);
16054
16055   if (dwarf2_per_objfile->quick_file_names_table)
16056     htab_delete (dwarf2_per_objfile->quick_file_names_table);
16057
16058   /* Everything else should be on the objfile obstack.  */
16059 }
16060
16061 /* A pair of DIE offset and GDB type pointer.  We store these
16062    in a hash table separate from the DIEs, and preserve them
16063    when the DIEs are flushed out of cache.  */
16064
16065 struct dwarf2_offset_and_type
16066 {
16067   unsigned int offset;
16068   struct type *type;
16069 };
16070
16071 /* Hash function for a dwarf2_offset_and_type.  */
16072
16073 static hashval_t
16074 offset_and_type_hash (const void *item)
16075 {
16076   const struct dwarf2_offset_and_type *ofs = item;
16077
16078   return ofs->offset;
16079 }
16080
16081 /* Equality function for a dwarf2_offset_and_type.  */
16082
16083 static int
16084 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16085 {
16086   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16087   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16088
16089   return ofs_lhs->offset == ofs_rhs->offset;
16090 }
16091
16092 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16093    table if necessary.  For convenience, return TYPE.
16094
16095    The DIEs reading must have careful ordering to:
16096     * Not cause infite loops trying to read in DIEs as a prerequisite for
16097       reading current DIE.
16098     * Not trying to dereference contents of still incompletely read in types
16099       while reading in other DIEs.
16100     * Enable referencing still incompletely read in types just by a pointer to
16101       the type without accessing its fields.
16102
16103    Therefore caller should follow these rules:
16104      * Try to fetch any prerequisite types we may need to build this DIE type
16105        before building the type and calling set_die_type.
16106      * After building type call set_die_type for current DIE as soon as
16107        possible before fetching more types to complete the current type.
16108      * Make the type as complete as possible before fetching more types.  */
16109
16110 static struct type *
16111 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16112 {
16113   struct dwarf2_offset_and_type **slot, ofs;
16114   struct objfile *objfile = cu->objfile;
16115   htab_t *type_hash_ptr;
16116
16117   /* For Ada types, make sure that the gnat-specific data is always
16118      initialized (if not already set).  There are a few types where
16119      we should not be doing so, because the type-specific area is
16120      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16121      where the type-specific area is used to store the floatformat).
16122      But this is not a problem, because the gnat-specific information
16123      is actually not needed for these types.  */
16124   if (need_gnat_info (cu)
16125       && TYPE_CODE (type) != TYPE_CODE_FUNC
16126       && TYPE_CODE (type) != TYPE_CODE_FLT
16127       && !HAVE_GNAT_AUX_INFO (type))
16128     INIT_GNAT_SPECIFIC (type);
16129
16130   if (cu->per_cu->debug_type_section)
16131     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16132   else
16133     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16134
16135   if (*type_hash_ptr == NULL)
16136     {
16137       *type_hash_ptr
16138         = htab_create_alloc_ex (127,
16139                                 offset_and_type_hash,
16140                                 offset_and_type_eq,
16141                                 NULL,
16142                                 &objfile->objfile_obstack,
16143                                 hashtab_obstack_allocate,
16144                                 dummy_obstack_deallocate);
16145     }
16146
16147   ofs.offset = die->offset;
16148   ofs.type = type;
16149   slot = (struct dwarf2_offset_and_type **)
16150     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16151   if (*slot)
16152     complaint (&symfile_complaints,
16153                _("A problem internal to GDB: DIE 0x%x has type already set"),
16154                die->offset);
16155   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16156   **slot = ofs;
16157   return type;
16158 }
16159
16160 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16161    table, or return NULL if the die does not have a saved type.  */
16162
16163 static struct type *
16164 get_die_type_at_offset (unsigned int offset,
16165                         struct dwarf2_per_cu_data *per_cu)
16166 {
16167   struct dwarf2_offset_and_type *slot, ofs;
16168   htab_t type_hash;
16169
16170   if (per_cu->debug_type_section)
16171     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16172   else
16173     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16174   if (type_hash == NULL)
16175     return NULL;
16176
16177   ofs.offset = offset;
16178   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16179   if (slot)
16180     return slot->type;
16181   else
16182     return NULL;
16183 }
16184
16185 /* Look up the type for DIE in the appropriate type_hash table,
16186    or return NULL if DIE does not have a saved type.  */
16187
16188 static struct type *
16189 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16190 {
16191   return get_die_type_at_offset (die->offset, cu->per_cu);
16192 }
16193
16194 /* Add a dependence relationship from CU to REF_PER_CU.  */
16195
16196 static void
16197 dwarf2_add_dependence (struct dwarf2_cu *cu,
16198                        struct dwarf2_per_cu_data *ref_per_cu)
16199 {
16200   void **slot;
16201
16202   if (cu->dependencies == NULL)
16203     cu->dependencies
16204       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16205                               NULL, &cu->comp_unit_obstack,
16206                               hashtab_obstack_allocate,
16207                               dummy_obstack_deallocate);
16208
16209   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16210   if (*slot == NULL)
16211     *slot = ref_per_cu;
16212 }
16213
16214 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16215    Set the mark field in every compilation unit in the
16216    cache that we must keep because we are keeping CU.  */
16217
16218 static int
16219 dwarf2_mark_helper (void **slot, void *data)
16220 {
16221   struct dwarf2_per_cu_data *per_cu;
16222
16223   per_cu = (struct dwarf2_per_cu_data *) *slot;
16224
16225   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16226      reading of the chain.  As such dependencies remain valid it is not much
16227      useful to track and undo them during QUIT cleanups.  */
16228   if (per_cu->cu == NULL)
16229     return 1;
16230
16231   if (per_cu->cu->mark)
16232     return 1;
16233   per_cu->cu->mark = 1;
16234
16235   if (per_cu->cu->dependencies != NULL)
16236     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16237
16238   return 1;
16239 }
16240
16241 /* Set the mark field in CU and in every other compilation unit in the
16242    cache that we must keep because we are keeping CU.  */
16243
16244 static void
16245 dwarf2_mark (struct dwarf2_cu *cu)
16246 {
16247   if (cu->mark)
16248     return;
16249   cu->mark = 1;
16250   if (cu->dependencies != NULL)
16251     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16252 }
16253
16254 static void
16255 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16256 {
16257   while (per_cu)
16258     {
16259       per_cu->cu->mark = 0;
16260       per_cu = per_cu->cu->read_in_chain;
16261     }
16262 }
16263
16264 /* Trivial hash function for partial_die_info: the hash value of a DIE
16265    is its offset in .debug_info for this objfile.  */
16266
16267 static hashval_t
16268 partial_die_hash (const void *item)
16269 {
16270   const struct partial_die_info *part_die = item;
16271
16272   return part_die->offset;
16273 }
16274
16275 /* Trivial comparison function for partial_die_info structures: two DIEs
16276    are equal if they have the same offset.  */
16277
16278 static int
16279 partial_die_eq (const void *item_lhs, const void *item_rhs)
16280 {
16281   const struct partial_die_info *part_die_lhs = item_lhs;
16282   const struct partial_die_info *part_die_rhs = item_rhs;
16283
16284   return part_die_lhs->offset == part_die_rhs->offset;
16285 }
16286
16287 static struct cmd_list_element *set_dwarf2_cmdlist;
16288 static struct cmd_list_element *show_dwarf2_cmdlist;
16289
16290 static void
16291 set_dwarf2_cmd (char *args, int from_tty)
16292 {
16293   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16294 }
16295
16296 static void
16297 show_dwarf2_cmd (char *args, int from_tty)
16298 {
16299   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16300 }
16301
16302 /* If section described by INFO was mmapped, munmap it now.  */
16303
16304 static void
16305 munmap_section_buffer (struct dwarf2_section_info *info)
16306 {
16307   if (info->map_addr != NULL)
16308     {
16309 #ifdef HAVE_MMAP
16310       int res;
16311
16312       res = munmap (info->map_addr, info->map_len);
16313       gdb_assert (res == 0);
16314 #else
16315       /* Without HAVE_MMAP, we should never be here to begin with.  */
16316       gdb_assert_not_reached ("no mmap support");
16317 #endif
16318     }
16319 }
16320
16321 /* munmap debug sections for OBJFILE, if necessary.  */
16322
16323 static void
16324 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16325 {
16326   struct dwarf2_per_objfile *data = d;
16327   int ix;
16328   struct dwarf2_section_info *section;
16329
16330   /* This is sorted according to the order they're defined in to make it easier
16331      to keep in sync.  */
16332   munmap_section_buffer (&data->info);
16333   munmap_section_buffer (&data->abbrev);
16334   munmap_section_buffer (&data->line);
16335   munmap_section_buffer (&data->loc);
16336   munmap_section_buffer (&data->macinfo);
16337   munmap_section_buffer (&data->macro);
16338   munmap_section_buffer (&data->str);
16339   munmap_section_buffer (&data->ranges);
16340   munmap_section_buffer (&data->frame);
16341   munmap_section_buffer (&data->eh_frame);
16342   munmap_section_buffer (&data->gdb_index);
16343
16344   for (ix = 0;
16345        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16346        ++ix)
16347     munmap_section_buffer (section);
16348
16349   VEC_free (dwarf2_section_info_def, data->types);
16350 }
16351
16352 \f
16353 /* The "save gdb-index" command.  */
16354
16355 /* The contents of the hash table we create when building the string
16356    table.  */
16357 struct strtab_entry
16358 {
16359   offset_type offset;
16360   const char *str;
16361 };
16362
16363 /* Hash function for a strtab_entry.
16364
16365    Function is used only during write_hash_table so no index format backward
16366    compatibility is needed.  */
16367
16368 static hashval_t
16369 hash_strtab_entry (const void *e)
16370 {
16371   const struct strtab_entry *entry = e;
16372   return mapped_index_string_hash (INT_MAX, entry->str);
16373 }
16374
16375 /* Equality function for a strtab_entry.  */
16376
16377 static int
16378 eq_strtab_entry (const void *a, const void *b)
16379 {
16380   const struct strtab_entry *ea = a;
16381   const struct strtab_entry *eb = b;
16382   return !strcmp (ea->str, eb->str);
16383 }
16384
16385 /* Create a strtab_entry hash table.  */
16386
16387 static htab_t
16388 create_strtab (void)
16389 {
16390   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16391                             xfree, xcalloc, xfree);
16392 }
16393
16394 /* Add a string to the constant pool.  Return the string's offset in
16395    host order.  */
16396
16397 static offset_type
16398 add_string (htab_t table, struct obstack *cpool, const char *str)
16399 {
16400   void **slot;
16401   struct strtab_entry entry;
16402   struct strtab_entry *result;
16403
16404   entry.str = str;
16405   slot = htab_find_slot (table, &entry, INSERT);
16406   if (*slot)
16407     result = *slot;
16408   else
16409     {
16410       result = XNEW (struct strtab_entry);
16411       result->offset = obstack_object_size (cpool);
16412       result->str = str;
16413       obstack_grow_str0 (cpool, str);
16414       *slot = result;
16415     }
16416   return result->offset;
16417 }
16418
16419 /* An entry in the symbol table.  */
16420 struct symtab_index_entry
16421 {
16422   /* The name of the symbol.  */
16423   const char *name;
16424   /* The offset of the name in the constant pool.  */
16425   offset_type index_offset;
16426   /* A sorted vector of the indices of all the CUs that hold an object
16427      of this name.  */
16428   VEC (offset_type) *cu_indices;
16429 };
16430
16431 /* The symbol table.  This is a power-of-2-sized hash table.  */
16432 struct mapped_symtab
16433 {
16434   offset_type n_elements;
16435   offset_type size;
16436   struct symtab_index_entry **data;
16437 };
16438
16439 /* Hash function for a symtab_index_entry.  */
16440
16441 static hashval_t
16442 hash_symtab_entry (const void *e)
16443 {
16444   const struct symtab_index_entry *entry = e;
16445   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16446                          sizeof (offset_type) * VEC_length (offset_type,
16447                                                             entry->cu_indices),
16448                          0);
16449 }
16450
16451 /* Equality function for a symtab_index_entry.  */
16452
16453 static int
16454 eq_symtab_entry (const void *a, const void *b)
16455 {
16456   const struct symtab_index_entry *ea = a;
16457   const struct symtab_index_entry *eb = b;
16458   int len = VEC_length (offset_type, ea->cu_indices);
16459   if (len != VEC_length (offset_type, eb->cu_indices))
16460     return 0;
16461   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16462                   VEC_address (offset_type, eb->cu_indices),
16463                   sizeof (offset_type) * len);
16464 }
16465
16466 /* Destroy a symtab_index_entry.  */
16467
16468 static void
16469 delete_symtab_entry (void *p)
16470 {
16471   struct symtab_index_entry *entry = p;
16472   VEC_free (offset_type, entry->cu_indices);
16473   xfree (entry);
16474 }
16475
16476 /* Create a hash table holding symtab_index_entry objects.  */
16477
16478 static htab_t
16479 create_symbol_hash_table (void)
16480 {
16481   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16482                             delete_symtab_entry, xcalloc, xfree);
16483 }
16484
16485 /* Create a new mapped symtab object.  */
16486
16487 static struct mapped_symtab *
16488 create_mapped_symtab (void)
16489 {
16490   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16491   symtab->n_elements = 0;
16492   symtab->size = 1024;
16493   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16494   return symtab;
16495 }
16496
16497 /* Destroy a mapped_symtab.  */
16498
16499 static void
16500 cleanup_mapped_symtab (void *p)
16501 {
16502   struct mapped_symtab *symtab = p;
16503   /* The contents of the array are freed when the other hash table is
16504      destroyed.  */
16505   xfree (symtab->data);
16506   xfree (symtab);
16507 }
16508
16509 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16510    the slot.
16511    
16512    Function is used only during write_hash_table so no index format backward
16513    compatibility is needed.  */
16514
16515 static struct symtab_index_entry **
16516 find_slot (struct mapped_symtab *symtab, const char *name)
16517 {
16518   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16519
16520   index = hash & (symtab->size - 1);
16521   step = ((hash * 17) & (symtab->size - 1)) | 1;
16522
16523   for (;;)
16524     {
16525       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16526         return &symtab->data[index];
16527       index = (index + step) & (symtab->size - 1);
16528     }
16529 }
16530
16531 /* Expand SYMTAB's hash table.  */
16532
16533 static void
16534 hash_expand (struct mapped_symtab *symtab)
16535 {
16536   offset_type old_size = symtab->size;
16537   offset_type i;
16538   struct symtab_index_entry **old_entries = symtab->data;
16539
16540   symtab->size *= 2;
16541   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16542
16543   for (i = 0; i < old_size; ++i)
16544     {
16545       if (old_entries[i])
16546         {
16547           struct symtab_index_entry **slot = find_slot (symtab,
16548                                                         old_entries[i]->name);
16549           *slot = old_entries[i];
16550         }
16551     }
16552
16553   xfree (old_entries);
16554 }
16555
16556 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16557    is the index of the CU in which the symbol appears.  */
16558
16559 static void
16560 add_index_entry (struct mapped_symtab *symtab, const char *name,
16561                  offset_type cu_index)
16562 {
16563   struct symtab_index_entry **slot;
16564
16565   ++symtab->n_elements;
16566   if (4 * symtab->n_elements / 3 >= symtab->size)
16567     hash_expand (symtab);
16568
16569   slot = find_slot (symtab, name);
16570   if (!*slot)
16571     {
16572       *slot = XNEW (struct symtab_index_entry);
16573       (*slot)->name = name;
16574       (*slot)->cu_indices = NULL;
16575     }
16576   /* Don't push an index twice.  Due to how we add entries we only
16577      have to check the last one.  */ 
16578   if (VEC_empty (offset_type, (*slot)->cu_indices)
16579       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16580     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16581 }
16582
16583 /* Add a vector of indices to the constant pool.  */
16584
16585 static offset_type
16586 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16587                       struct symtab_index_entry *entry)
16588 {
16589   void **slot;
16590
16591   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16592   if (!*slot)
16593     {
16594       offset_type len = VEC_length (offset_type, entry->cu_indices);
16595       offset_type val = MAYBE_SWAP (len);
16596       offset_type iter;
16597       int i;
16598
16599       *slot = entry;
16600       entry->index_offset = obstack_object_size (cpool);
16601
16602       obstack_grow (cpool, &val, sizeof (val));
16603       for (i = 0;
16604            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16605            ++i)
16606         {
16607           val = MAYBE_SWAP (iter);
16608           obstack_grow (cpool, &val, sizeof (val));
16609         }
16610     }
16611   else
16612     {
16613       struct symtab_index_entry *old_entry = *slot;
16614       entry->index_offset = old_entry->index_offset;
16615       entry = old_entry;
16616     }
16617   return entry->index_offset;
16618 }
16619
16620 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16621    constant pool entries going into the obstack CPOOL.  */
16622
16623 static void
16624 write_hash_table (struct mapped_symtab *symtab,
16625                   struct obstack *output, struct obstack *cpool)
16626 {
16627   offset_type i;
16628   htab_t symbol_hash_table;
16629   htab_t str_table;
16630
16631   symbol_hash_table = create_symbol_hash_table ();
16632   str_table = create_strtab ();
16633
16634   /* We add all the index vectors to the constant pool first, to
16635      ensure alignment is ok.  */
16636   for (i = 0; i < symtab->size; ++i)
16637     {
16638       if (symtab->data[i])
16639         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16640     }
16641
16642   /* Now write out the hash table.  */
16643   for (i = 0; i < symtab->size; ++i)
16644     {
16645       offset_type str_off, vec_off;
16646
16647       if (symtab->data[i])
16648         {
16649           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16650           vec_off = symtab->data[i]->index_offset;
16651         }
16652       else
16653         {
16654           /* While 0 is a valid constant pool index, it is not valid
16655              to have 0 for both offsets.  */
16656           str_off = 0;
16657           vec_off = 0;
16658         }
16659
16660       str_off = MAYBE_SWAP (str_off);
16661       vec_off = MAYBE_SWAP (vec_off);
16662
16663       obstack_grow (output, &str_off, sizeof (str_off));
16664       obstack_grow (output, &vec_off, sizeof (vec_off));
16665     }
16666
16667   htab_delete (str_table);
16668   htab_delete (symbol_hash_table);
16669 }
16670
16671 /* Struct to map psymtab to CU index in the index file.  */
16672 struct psymtab_cu_index_map
16673 {
16674   struct partial_symtab *psymtab;
16675   unsigned int cu_index;
16676 };
16677
16678 static hashval_t
16679 hash_psymtab_cu_index (const void *item)
16680 {
16681   const struct psymtab_cu_index_map *map = item;
16682
16683   return htab_hash_pointer (map->psymtab);
16684 }
16685
16686 static int
16687 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16688 {
16689   const struct psymtab_cu_index_map *lhs = item_lhs;
16690   const struct psymtab_cu_index_map *rhs = item_rhs;
16691
16692   return lhs->psymtab == rhs->psymtab;
16693 }
16694
16695 /* Helper struct for building the address table.  */
16696 struct addrmap_index_data
16697 {
16698   struct objfile *objfile;
16699   struct obstack *addr_obstack;
16700   htab_t cu_index_htab;
16701
16702   /* Non-zero if the previous_* fields are valid.
16703      We can't write an entry until we see the next entry (since it is only then
16704      that we know the end of the entry).  */
16705   int previous_valid;
16706   /* Index of the CU in the table of all CUs in the index file.  */
16707   unsigned int previous_cu_index;
16708   /* Start address of the CU.  */
16709   CORE_ADDR previous_cu_start;
16710 };
16711
16712 /* Write an address entry to OBSTACK.  */
16713
16714 static void
16715 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16716                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16717 {
16718   offset_type cu_index_to_write;
16719   char addr[8];
16720   CORE_ADDR baseaddr;
16721
16722   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16723
16724   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16725   obstack_grow (obstack, addr, 8);
16726   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16727   obstack_grow (obstack, addr, 8);
16728   cu_index_to_write = MAYBE_SWAP (cu_index);
16729   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16730 }
16731
16732 /* Worker function for traversing an addrmap to build the address table.  */
16733
16734 static int
16735 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16736 {
16737   struct addrmap_index_data *data = datap;
16738   struct partial_symtab *pst = obj;
16739   offset_type cu_index;
16740   void **slot;
16741
16742   if (data->previous_valid)
16743     add_address_entry (data->objfile, data->addr_obstack,
16744                        data->previous_cu_start, start_addr,
16745                        data->previous_cu_index);
16746
16747   data->previous_cu_start = start_addr;
16748   if (pst != NULL)
16749     {
16750       struct psymtab_cu_index_map find_map, *map;
16751       find_map.psymtab = pst;
16752       map = htab_find (data->cu_index_htab, &find_map);
16753       gdb_assert (map != NULL);
16754       data->previous_cu_index = map->cu_index;
16755       data->previous_valid = 1;
16756     }
16757   else
16758       data->previous_valid = 0;
16759
16760   return 0;
16761 }
16762
16763 /* Write OBJFILE's address map to OBSTACK.
16764    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16765    in the index file.  */
16766
16767 static void
16768 write_address_map (struct objfile *objfile, struct obstack *obstack,
16769                    htab_t cu_index_htab)
16770 {
16771   struct addrmap_index_data addrmap_index_data;
16772
16773   /* When writing the address table, we have to cope with the fact that
16774      the addrmap iterator only provides the start of a region; we have to
16775      wait until the next invocation to get the start of the next region.  */
16776
16777   addrmap_index_data.objfile = objfile;
16778   addrmap_index_data.addr_obstack = obstack;
16779   addrmap_index_data.cu_index_htab = cu_index_htab;
16780   addrmap_index_data.previous_valid = 0;
16781
16782   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16783                    &addrmap_index_data);
16784
16785   /* It's highly unlikely the last entry (end address = 0xff...ff)
16786      is valid, but we should still handle it.
16787      The end address is recorded as the start of the next region, but that
16788      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16789      anyway.  */
16790   if (addrmap_index_data.previous_valid)
16791     add_address_entry (objfile, obstack,
16792                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16793                        addrmap_index_data.previous_cu_index);
16794 }
16795
16796 /* Add a list of partial symbols to SYMTAB.  */
16797
16798 static void
16799 write_psymbols (struct mapped_symtab *symtab,
16800                 htab_t psyms_seen,
16801                 struct partial_symbol **psymp,
16802                 int count,
16803                 offset_type cu_index,
16804                 int is_static)
16805 {
16806   for (; count-- > 0; ++psymp)
16807     {
16808       void **slot, *lookup;
16809
16810       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16811         error (_("Ada is not currently supported by the index"));
16812
16813       /* We only want to add a given psymbol once.  However, we also
16814          want to account for whether it is global or static.  So, we
16815          may add it twice, using slightly different values.  */
16816       if (is_static)
16817         {
16818           uintptr_t val = 1 | (uintptr_t) *psymp;
16819
16820           lookup = (void *) val;
16821         }
16822       else
16823         lookup = *psymp;
16824
16825       /* Only add a given psymbol once.  */
16826       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16827       if (!*slot)
16828         {
16829           *slot = lookup;
16830           add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16831         }
16832     }
16833 }
16834
16835 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16836    exception if there is an error.  */
16837
16838 static void
16839 write_obstack (FILE *file, struct obstack *obstack)
16840 {
16841   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16842               file)
16843       != obstack_object_size (obstack))
16844     error (_("couldn't data write to file"));
16845 }
16846
16847 /* Unlink a file if the argument is not NULL.  */
16848
16849 static void
16850 unlink_if_set (void *p)
16851 {
16852   char **filename = p;
16853   if (*filename)
16854     unlink (*filename);
16855 }
16856
16857 /* A helper struct used when iterating over debug_types.  */
16858 struct signatured_type_index_data
16859 {
16860   struct objfile *objfile;
16861   struct mapped_symtab *symtab;
16862   struct obstack *types_list;
16863   htab_t psyms_seen;
16864   int cu_index;
16865 };
16866
16867 /* A helper function that writes a single signatured_type to an
16868    obstack.  */
16869
16870 static int
16871 write_one_signatured_type (void **slot, void *d)
16872 {
16873   struct signatured_type_index_data *info = d;
16874   struct signatured_type *entry = (struct signatured_type *) *slot;
16875   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16876   struct partial_symtab *psymtab = per_cu->v.psymtab;
16877   gdb_byte val[8];
16878
16879   write_psymbols (info->symtab,
16880                   info->psyms_seen,
16881                   info->objfile->global_psymbols.list
16882                   + psymtab->globals_offset,
16883                   psymtab->n_global_syms, info->cu_index,
16884                   0);
16885   write_psymbols (info->symtab,
16886                   info->psyms_seen,
16887                   info->objfile->static_psymbols.list
16888                   + psymtab->statics_offset,
16889                   psymtab->n_static_syms, info->cu_index,
16890                   1);
16891
16892   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16893   obstack_grow (info->types_list, val, 8);
16894   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16895   obstack_grow (info->types_list, val, 8);
16896   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16897   obstack_grow (info->types_list, val, 8);
16898
16899   ++info->cu_index;
16900
16901   return 1;
16902 }
16903
16904 /* Create an index file for OBJFILE in the directory DIR.  */
16905
16906 static void
16907 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16908 {
16909   struct cleanup *cleanup;
16910   char *filename, *cleanup_filename;
16911   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16912   struct obstack cu_list, types_cu_list;
16913   int i;
16914   FILE *out_file;
16915   struct mapped_symtab *symtab;
16916   offset_type val, size_of_contents, total_len;
16917   struct stat st;
16918   char buf[8];
16919   htab_t psyms_seen;
16920   htab_t cu_index_htab;
16921   struct psymtab_cu_index_map *psymtab_cu_index_map;
16922
16923   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16924     return;
16925
16926   if (dwarf2_per_objfile->using_index)
16927     error (_("Cannot use an index to create the index"));
16928
16929   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16930     error (_("Cannot make an index when the file has multiple .debug_types sections"));
16931
16932   if (stat (objfile->name, &st) < 0)
16933     perror_with_name (objfile->name);
16934
16935   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16936                      INDEX_SUFFIX, (char *) NULL);
16937   cleanup = make_cleanup (xfree, filename);
16938
16939   out_file = fopen (filename, "wb");
16940   if (!out_file)
16941     error (_("Can't open `%s' for writing"), filename);
16942
16943   cleanup_filename = filename;
16944   make_cleanup (unlink_if_set, &cleanup_filename);
16945
16946   symtab = create_mapped_symtab ();
16947   make_cleanup (cleanup_mapped_symtab, symtab);
16948
16949   obstack_init (&addr_obstack);
16950   make_cleanup_obstack_free (&addr_obstack);
16951
16952   obstack_init (&cu_list);
16953   make_cleanup_obstack_free (&cu_list);
16954
16955   obstack_init (&types_cu_list);
16956   make_cleanup_obstack_free (&types_cu_list);
16957
16958   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16959                                   NULL, xcalloc, xfree);
16960   make_cleanup_htab_delete (psyms_seen);
16961
16962   /* While we're scanning CU's create a table that maps a psymtab pointer
16963      (which is what addrmap records) to its index (which is what is recorded
16964      in the index file).  This will later be needed to write the address
16965      table.  */
16966   cu_index_htab = htab_create_alloc (100,
16967                                      hash_psymtab_cu_index,
16968                                      eq_psymtab_cu_index,
16969                                      NULL, xcalloc, xfree);
16970   make_cleanup_htab_delete (cu_index_htab);
16971   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16972     xmalloc (sizeof (struct psymtab_cu_index_map)
16973              * dwarf2_per_objfile->n_comp_units);
16974   make_cleanup (xfree, psymtab_cu_index_map);
16975
16976   /* The CU list is already sorted, so we don't need to do additional
16977      work here.  Also, the debug_types entries do not appear in
16978      all_comp_units, but only in their own hash table.  */
16979   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16980     {
16981       struct dwarf2_per_cu_data *per_cu
16982         = dwarf2_per_objfile->all_comp_units[i];
16983       struct partial_symtab *psymtab = per_cu->v.psymtab;
16984       gdb_byte val[8];
16985       struct psymtab_cu_index_map *map;
16986       void **slot;
16987
16988       write_psymbols (symtab,
16989                       psyms_seen,
16990                       objfile->global_psymbols.list + psymtab->globals_offset,
16991                       psymtab->n_global_syms, i,
16992                       0);
16993       write_psymbols (symtab,
16994                       psyms_seen,
16995                       objfile->static_psymbols.list + psymtab->statics_offset,
16996                       psymtab->n_static_syms, i,
16997                       1);
16998
16999       map = &psymtab_cu_index_map[i];
17000       map->psymtab = psymtab;
17001       map->cu_index = i;
17002       slot = htab_find_slot (cu_index_htab, map, INSERT);
17003       gdb_assert (slot != NULL);
17004       gdb_assert (*slot == NULL);
17005       *slot = map;
17006
17007       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
17008       obstack_grow (&cu_list, val, 8);
17009       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17010       obstack_grow (&cu_list, val, 8);
17011     }
17012
17013   /* Dump the address map.  */
17014   write_address_map (objfile, &addr_obstack, cu_index_htab);
17015
17016   /* Write out the .debug_type entries, if any.  */
17017   if (dwarf2_per_objfile->signatured_types)
17018     {
17019       struct signatured_type_index_data sig_data;
17020
17021       sig_data.objfile = objfile;
17022       sig_data.symtab = symtab;
17023       sig_data.types_list = &types_cu_list;
17024       sig_data.psyms_seen = psyms_seen;
17025       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17026       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17027                               write_one_signatured_type, &sig_data);
17028     }
17029
17030   obstack_init (&constant_pool);
17031   make_cleanup_obstack_free (&constant_pool);
17032   obstack_init (&symtab_obstack);
17033   make_cleanup_obstack_free (&symtab_obstack);
17034   write_hash_table (symtab, &symtab_obstack, &constant_pool);
17035
17036   obstack_init (&contents);
17037   make_cleanup_obstack_free (&contents);
17038   size_of_contents = 6 * sizeof (offset_type);
17039   total_len = size_of_contents;
17040
17041   /* The version number.  */
17042   val = MAYBE_SWAP (5);
17043   obstack_grow (&contents, &val, sizeof (val));
17044
17045   /* The offset of the CU list from the start of the file.  */
17046   val = MAYBE_SWAP (total_len);
17047   obstack_grow (&contents, &val, sizeof (val));
17048   total_len += obstack_object_size (&cu_list);
17049
17050   /* The offset of the types CU list from the start of the file.  */
17051   val = MAYBE_SWAP (total_len);
17052   obstack_grow (&contents, &val, sizeof (val));
17053   total_len += obstack_object_size (&types_cu_list);
17054
17055   /* The offset of the address table from the start of the file.  */
17056   val = MAYBE_SWAP (total_len);
17057   obstack_grow (&contents, &val, sizeof (val));
17058   total_len += obstack_object_size (&addr_obstack);
17059
17060   /* The offset of the symbol table from the start of the file.  */
17061   val = MAYBE_SWAP (total_len);
17062   obstack_grow (&contents, &val, sizeof (val));
17063   total_len += obstack_object_size (&symtab_obstack);
17064
17065   /* The offset of the constant pool from the start of the file.  */
17066   val = MAYBE_SWAP (total_len);
17067   obstack_grow (&contents, &val, sizeof (val));
17068   total_len += obstack_object_size (&constant_pool);
17069
17070   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17071
17072   write_obstack (out_file, &contents);
17073   write_obstack (out_file, &cu_list);
17074   write_obstack (out_file, &types_cu_list);
17075   write_obstack (out_file, &addr_obstack);
17076   write_obstack (out_file, &symtab_obstack);
17077   write_obstack (out_file, &constant_pool);
17078
17079   fclose (out_file);
17080
17081   /* We want to keep the file, so we set cleanup_filename to NULL
17082      here.  See unlink_if_set.  */
17083   cleanup_filename = NULL;
17084
17085   do_cleanups (cleanup);
17086 }
17087
17088 /* Implementation of the `save gdb-index' command.
17089    
17090    Note that the file format used by this command is documented in the
17091    GDB manual.  Any changes here must be documented there.  */
17092
17093 static void
17094 save_gdb_index_command (char *arg, int from_tty)
17095 {
17096   struct objfile *objfile;
17097
17098   if (!arg || !*arg)
17099     error (_("usage: save gdb-index DIRECTORY"));
17100
17101   ALL_OBJFILES (objfile)
17102   {
17103     struct stat st;
17104
17105     /* If the objfile does not correspond to an actual file, skip it.  */
17106     if (stat (objfile->name, &st) < 0)
17107       continue;
17108
17109     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17110     if (dwarf2_per_objfile)
17111       {
17112         volatile struct gdb_exception except;
17113
17114         TRY_CATCH (except, RETURN_MASK_ERROR)
17115           {
17116             write_psymtabs_to_index (objfile, arg);
17117           }
17118         if (except.reason < 0)
17119           exception_fprintf (gdb_stderr, except,
17120                              _("Error while writing index for `%s': "),
17121                              objfile->name);
17122       }
17123   }
17124 }
17125
17126 \f
17127
17128 int dwarf2_always_disassemble;
17129
17130 static void
17131 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17132                                 struct cmd_list_element *c, const char *value)
17133 {
17134   fprintf_filtered (file,
17135                     _("Whether to always disassemble "
17136                       "DWARF expressions is %s.\n"),
17137                     value);
17138 }
17139
17140 static void
17141 show_check_physname (struct ui_file *file, int from_tty,
17142                      struct cmd_list_element *c, const char *value)
17143 {
17144   fprintf_filtered (file,
17145                     _("Whether to check \"physname\" is %s.\n"),
17146                     value);
17147 }
17148
17149 void _initialize_dwarf2_read (void);
17150
17151 void
17152 _initialize_dwarf2_read (void)
17153 {
17154   struct cmd_list_element *c;
17155
17156   dwarf2_objfile_data_key
17157     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17158
17159   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17160 Set DWARF 2 specific variables.\n\
17161 Configure DWARF 2 variables such as the cache size"),
17162                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17163                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17164
17165   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17166 Show DWARF 2 specific variables\n\
17167 Show DWARF 2 variables such as the cache size"),
17168                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17169                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17170
17171   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17172                             &dwarf2_max_cache_age, _("\
17173 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17174 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17175 A higher limit means that cached compilation units will be stored\n\
17176 in memory longer, and more total memory will be used.  Zero disables\n\
17177 caching, which can slow down startup."),
17178                             NULL,
17179                             show_dwarf2_max_cache_age,
17180                             &set_dwarf2_cmdlist,
17181                             &show_dwarf2_cmdlist);
17182
17183   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17184                            &dwarf2_always_disassemble, _("\
17185 Set whether `info address' always disassembles DWARF expressions."), _("\
17186 Show whether `info address' always disassembles DWARF expressions."), _("\
17187 When enabled, DWARF expressions are always printed in an assembly-like\n\
17188 syntax.  When disabled, expressions will be printed in a more\n\
17189 conversational style, when possible."),
17190                            NULL,
17191                            show_dwarf2_always_disassemble,
17192                            &set_dwarf2_cmdlist,
17193                            &show_dwarf2_cmdlist);
17194
17195   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17196 Set debugging of the dwarf2 DIE reader."), _("\
17197 Show debugging of the dwarf2 DIE reader."), _("\
17198 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17199 The value is the maximum depth to print."),
17200                             NULL,
17201                             NULL,
17202                             &setdebuglist, &showdebuglist);
17203
17204   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17205 Set cross-checking of \"physname\" code against demangler."), _("\
17206 Show cross-checking of \"physname\" code against demangler."), _("\
17207 When enabled, GDB's internal \"physname\" code is checked against\n\
17208 the demangler."),
17209                            NULL, show_check_physname,
17210                            &setdebuglist, &showdebuglist);
17211
17212   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17213                _("\
17214 Save a gdb-index file.\n\
17215 Usage: save gdb-index DIRECTORY"),
17216                &save_cmdlist);
17217   set_cmd_completer (c, filename_completer);
17218 }